Skip to content

Commit

Permalink
Resolving warnings.
Browse files Browse the repository at this point in the history
  • Loading branch information
jtmueller committed Dec 28, 2021
1 parent c1cf7d3 commit 257ec12
Show file tree
Hide file tree
Showing 6 changed files with 165 additions and 191 deletions.
171 changes: 85 additions & 86 deletions src/Model/Huffman/HuffmanTree.cs
Original file line number Diff line number Diff line change
@@ -1,113 +1,112 @@
using D2SLib.IO;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;

namespace D2SLib.Model.Huffman
namespace D2SLib.Model.Huffman;

//hardcoded....
public class HuffmanTree
{
//hardcoded....
public class HuffmanTree
{
public Node Root { get; set; }
public Node? Root { get; set; }

public static Dictionary<char, string> TABLE = new Dictionary<char, string>
{
{'0', "11111011"},
{' ', "10"},
{'1', "1111100"},
{'2', "001100"},
{'3', "1101101"},
{'4', "11111010"},
{'5', "00010110"},
{'6', "1101111"},
{'7', "01111"},
{'8', "000100"},
{'9', "01110"},
{'a', "11110"},
{'b', "0101"},
{'c', "01000"},
{'d', "110001"},
{'e', "110000"},
{'f', "010011"},
{'g', "11010"},
{'h', "00011"},
{'i', "1111110"},
{'j', "000101110"},
{'k', "010010"},
{'l', "11101"},
{'m', "01101"},
{'n', "001101"},
{'o', "1111111"},
{'p', "11001"},
{'q', "11011001"},
{'r', "11100"},
{'s', "0010"},
{'t', "01100"},
{'u', "00001"},
{'v', "1101110"},
{'w', "00000"},
{'x', "00111"},
{'y', "0001010"},
{'z', "11011000"}
};
public static readonly IReadOnlyDictionary<char, string> TABLE = new Dictionary<char, string>(38)
{
{'0', "11111011"},
{' ', "10"},
{'1', "1111100"},
{'2', "001100"},
{'3', "1101101"},
{'4', "11111010"},
{'5', "00010110"},
{'6', "1101111"},
{'7', "01111"},
{'8', "000100"},
{'9', "01110"},
{'a', "11110"},
{'b', "0101"},
{'c', "01000"},
{'d', "110001"},
{'e', "110000"},
{'f', "010011"},
{'g', "11010"},
{'h', "00011"},
{'i', "1111110"},
{'j', "000101110"},
{'k', "010010"},
{'l', "11101"},
{'m', "01101"},
{'n', "001101"},
{'o', "1111111"},
{'p', "11001"},
{'q', "11011001"},
{'r', "11100"},
{'s', "0010"},
{'t', "01100"},
{'u', "00001"},
{'v', "1101110"},
{'w', "00000"},
{'x', "00111"},
{'y', "0001010"},
{'z', "11011000"}
};

//todo find a way to build this like d2?
public void Build(List<string> items)
//todo find a way to build this like d2?
public void Build(List<string> items)
{
Root = new Node();
foreach (var entry in TABLE)
{
Root = new Node();
foreach(KeyValuePair<char, string> entry in TABLE)
var current = Root;
foreach (char bit in entry.Value)
{
var Current = Root;
foreach(char bit in entry.Value)
if (bit == '1')
{
if(bit == '1')
if (current.Right == null)
{
if(Current.Right == null)
{
Current.Right = new Node();
}
Current = Current.Right;
} else if(bit == '0')
current.Right = new Node();
}
current = current.Right;
}
else if (bit == '0')
{
if (current.Left == null)
{
if (Current.Left == null)
{
Current.Left = new Node();
}
Current = Current.Left;
current.Left = new Node();
}
current = current.Left;
}
Current.Symbol = entry.Key;
}
current.Symbol = entry.Key;
}
}

public BitArray EncodeChar(char source)
{
List<bool> encodedSymbol = this.Root.Traverse(source, new List<bool>());
return new BitArray(encodedSymbol.ToArray());
}
public BitArray EncodeChar(char source)
{
var encodedSymbol = Root?.Traverse(source, new List<bool>());
if (encodedSymbol is null)
throw new InvalidOperationException("Could not encode with an empty tree.");
return new BitArray(encodedSymbol.ToArray());
}

public char DecodeChar(IBitReader reader)
public char DecodeChar(IBitReader reader)
{
var current = Root;
while (!(current?.IsLeaf() ?? true))
{
Node current = this.Root;
while(!current.IsLeaf())
if (reader.ReadBit())
{
if (reader.ReadBit())
if (current.Right is not null)
{
if (current.Right != null)
{
current = current.Right;
}
current = current.Right;
}
else
}
else
{
if (current.Left is not null)
{
if (current.Left != null)
{
current = current.Left;
}
current = current.Left;
}
}
return current.Symbol;
}
return current?.Symbol ?? '\0';
}
}
89 changes: 38 additions & 51 deletions src/Model/Huffman/Node.cs
Original file line number Diff line number Diff line change
@@ -1,66 +1,53 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace D2SLib.Model.Huffman;

namespace D2SLib.Model.Huffman
public class Node
{
public class Node
{
public char Symbol { get; set; }
public int Frequency { get; set; }
public Node Right { get; set; }
public Node Left { get; set; }
public char Symbol { get; set; }
public int Frequency { get; set; }
public Node? Right { get; set; }
public Node? Left { get; set; }

public List<bool> Traverse(char symbol, List<bool> data)
public List<bool>? Traverse(char symbol, List<bool> data)
{
// Leaf
if (Right == null && Left == null)
{
// Leaf
if (Right == null && Left == null)
if (symbol.Equals(Symbol))
{
if (symbol.Equals(this.Symbol))
{
return data;
}
else
{
return null;
}
return data;
}
else
{
List<bool> left = null;
List<bool> right = null;

if (Left != null)
{
List<bool> leftPath = new List<bool>();
leftPath.AddRange(data);
leftPath.Add(false);

left = Left.Traverse(symbol, leftPath);
}
return null;
}
}
else
{
List<bool>? left = null;
List<bool>? right = null;

if (Right != null)
{
List<bool> rightPath = new List<bool>();
rightPath.AddRange(data);
rightPath.Add(true);
right = Right.Traverse(symbol, rightPath);
}
if (Left is not null)
{
var leftPath = new List<bool>(data) { false };
left = Left.Traverse(symbol, leftPath);
}

if (left != null)
{
return left;
}
else
{
return right;
}
if (Right is not null)
{
var rightPath = new List<bool>(data) { true };
right = Right.Traverse(symbol, rightPath);
}
}

public bool IsLeaf()
{
return (Left == null && Right == null);
if (left != null)
{
return left;
}
else
{
return right;
}
}
}

public bool IsLeaf() => Left is null && Right is null;
}
18 changes: 8 additions & 10 deletions src/Model/Save/Corpses.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,13 @@ public static byte[] Write(CorpseList corpseList, uint version)

public sealed class Corpse
{
#nullable disable
private Corpse() { }
#nullable enable
private Corpse(IBitReader reader, uint version)
{
Unk0x0 = reader.ReadUInt32();
X = reader.ReadUInt32();
Y = reader.ReadUInt32();
ItemList = ItemList.Read(reader, version);
}

public uint? Unk0x0 { get; set; }
public uint X { get; set; }
Expand All @@ -68,13 +72,7 @@ public void Write(IBitWriter writer, uint version)

public static Corpse Read(IBitReader reader, uint version)
{
var corpse = new Corpse
{
Unk0x0 = reader.ReadUInt32(),
X = reader.ReadUInt32(),
Y = reader.ReadUInt32(),
ItemList = ItemList.Read(reader, version)
};
var corpse = new Corpse(reader, version);
return corpse;
}

Expand Down
26 changes: 12 additions & 14 deletions src/Model/Save/Golem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,33 @@ namespace D2SLib.Model.Save;

public class Golem
{
#nullable disable
private Golem() { }
#nullable enable
private Golem(IBitReader reader, uint version)
{
Header = reader.ReadUInt16();
Exists = reader.ReadByte() == 1;
if (Exists)
{
Item = Item.Read(reader, version);
}
}

public ushort? Header { get; set; }
public bool Exists { get; set; }
public Item Item { get; set; }
public Item? Item { get; set; }

public void Write(IBitWriter writer, uint version)
{
writer.WriteUInt16(Header ?? 0x666B);
writer.WriteByte((byte)(Exists ? 1 : 0));
if (Exists)
{
Item.Write(writer, version);
Item?.Write(writer, version);
}
}

public static Golem Read(IBitReader reader, uint version)
{
var golem = new Golem
{
Header = reader.ReadUInt16(),
Exists = reader.ReadByte() == 1
};
if (golem.Exists)
{
golem.Item = Item.Read(reader, version);
}
var golem = new Golem(reader, version);
return golem;
}

Expand Down
Loading

0 comments on commit 257ec12

Please sign in to comment.