Skip to content

Commit

Permalink
Further optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
jtmueller committed Dec 28, 2021
1 parent a6a2f3e commit 28fcedb
Show file tree
Hide file tree
Showing 17 changed files with 700 additions and 407 deletions.
63 changes: 63 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
###############################################################################
# Set default behavior to automatically normalize line endings.
###############################################################################
* text=auto

###############################################################################
# Set default behavior for command prompt diff.
#
# This is need for earlier builds of msysgit that does not have it on by
# default for csharp files.
# Note: This is only used by command line
###############################################################################
#*.cs diff=csharp

###############################################################################
# Set the merge driver for project and solution files
#
# Merging from the command prompt will add diff markers to the files if there
# are conflicts (Merging from VS is not affected by the settings below, in VS
# the diff markers are never inserted). Diff markers may cause the following
# file extensions to fail to load in VS. An alternative would be to treat
# these files as binary and thus will always conflict and require user
# intervention with every merge. To do so, just uncomment the entries below
###############################################################################
#*.sln merge=binary
#*.csproj merge=binary
#*.vbproj merge=binary
#*.vcxproj merge=binary
#*.vcproj merge=binary
#*.dbproj merge=binary
#*.fsproj merge=binary
#*.lsproj merge=binary
#*.wixproj merge=binary
#*.modelproj merge=binary
#*.sqlproj merge=binary
#*.wwaproj merge=binary

###############################################################################
# behavior for image files
#
# image files are treated as binary by default.
###############################################################################
#*.jpg binary
#*.png binary
#*.gif binary

###############################################################################
# diff behavior for common document formats
#
# Convert binary document formats to text before diffing them. This feature
# is only available from the command line. Turn it on by uncommenting the
# entries below.
###############################################################################
#*.doc diff=astextplain
#*.DOC diff=astextplain
#*.docx diff=astextplain
#*.DOCX diff=astextplain
#*.dot diff=astextplain
#*.DOT diff=astextplain
#*.pdf diff=astextplain
#*.PDF diff=astextplain
#*.rtf diff=astextplain
#*.RTF diff=astextplain
4 changes: 2 additions & 2 deletions src/IO/BitWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ private void Grow()
}
}

internal void WriteBits(InternalBitArray bits) => WriteBits(bits, bits.Length);
public void WriteBits(IList<bool> bits) => WriteBits(bits, bits.Count);

internal void WriteBits(InternalBitArray bits, int numberOfBits)
public void WriteBits(IList<bool> bits, int numberOfBits)
{
for (int i = 0; i < numberOfBits; i++)
{
Expand Down
2 changes: 2 additions & 0 deletions src/IO/IBitReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public interface IBitReader
byte[] ReadBytes(int numberOfBytes);
IMemoryOwner<byte> ReadBytesPooled(int numberOfBytes);
int ReadBytes(int numberOfBytes, Span<byte> output);
int ReadBytes(Span<byte> output);
int ReadInt32();
int ReadInt32(int bits);
string ReadString(int byteCount);
Expand All @@ -25,4 +26,5 @@ public interface IBitReader
uint ReadUInt32(int bits);
void Seek(int bytePostion);
void SeekBits(int bitPosition);
void AdvanceBits(int bits);
}
2 changes: 2 additions & 0 deletions src/IO/IBitWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ public interface IBitWriter
IMemoryOwner<byte> ToPooledArray();
int GetBytes(Span<byte> output);
void WriteBit(bool value);
void WriteBits(IList<bool> bits);
void WriteBits(IList<bool> bits, int numberOfBits);
void WriteByte(byte value);
void WriteByte(byte value, int size);
void WriteBytes(ReadOnlySpan<byte> value);
Expand Down
2 changes: 1 addition & 1 deletion src/Model/Huffman/HuffmanTree.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public BitArray EncodeChar(char source)
return new BitArray(encodedSymbol.ToArray());
}

public char DecodeChar(BitReader reader)
public char DecodeChar(IBitReader reader)
{
Node current = this.Root;
while(!current.IsLeaf())
Expand Down
8 changes: 4 additions & 4 deletions src/Model/Save/Appearances.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ public class Appearances
public Appearance Special7 { get => _parts[14]; set => _parts[14] = value; }
public Appearance Special8 { get => _parts[15]; set => _parts[15] = value; }

public void Write(BitWriter writer)
public void Write(IBitWriter writer)
{
for (int i = 0; i < _parts.Length; i++)
{
_parts[i].Write(writer);
}
}

public static Appearances Read(BitReader reader)
public static Appearances Read(IBitReader reader)
{
var appearances = new Appearances();
var parts = appearances._parts;
Expand Down Expand Up @@ -70,7 +70,7 @@ public Appearance(byte graphic, byte tint)
Tint = tint;
}

public Appearance(BitReader reader)
public Appearance(IBitReader reader)
{
Graphic = reader.ReadByte();
Tint = reader.ReadByte();
Expand All @@ -79,7 +79,7 @@ public Appearance(BitReader reader)
public readonly byte Graphic { get; }
public readonly byte Tint { get; }

public void Write(BitWriter writer)
public void Write(IBitWriter writer)
{
writer.WriteByte(Graphic);
writer.WriteByte(Tint);
Expand Down
4 changes: 2 additions & 2 deletions src/Model/Save/Attributes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public class Attributes
public ushort? Header { get; set; }
public Dictionary<string, int> Stats { get; set; } = new Dictionary<string, int>();

public static Attributes Read(BitReader reader)
public static Attributes Read(IBitReader reader)
{
var itemStatCost = Core.TXT.ItemStatCostTXT;
var attributes = new Attributes
Expand All @@ -32,7 +32,7 @@ public static Attributes Read(BitReader reader)
return attributes;
}

public void Write(BitWriter writer)
public void Write(IBitWriter writer)
{
var itemStatCost = Core.TXT.ItemStatCostTXT;
writer.WriteUInt16(Header ?? 0x6667);
Expand Down
11 changes: 5 additions & 6 deletions src/Model/Save/D2S.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ public class D2S
//0x2fc
public Attributes Attributes { get; set; }


public ClassSkills ClassSkills { get; set; }

public ItemList PlayerItemList { get; set; }
Expand All @@ -92,7 +91,7 @@ public static D2S Read(ReadOnlySpan<byte> bytes)
Created = reader.ReadUInt32(),
LastPlayed = reader.ReadUInt32(),
Unk0x0034 = reader.ReadBytes(4),
AssignedSkills = Enumerable.Range(0, 16).Select(e => Skill.Read(reader)).ToArray(),
AssignedSkills = Enumerable.Range(0, 16).Select(_ => Skill.Read(reader)).ToArray(),
LeftSkill = Skill.Read(reader),
RightSkill = Skill.Read(reader),
LeftSwapSkill = Skill.Read(reader),
Expand All @@ -103,8 +102,8 @@ public static D2S Read(ReadOnlySpan<byte> bytes)
Unk0x00af = reader.ReadBytes(2),
Mercenary = Mercenary.Read(reader),
RealmData = reader.ReadBytes(140),
Quests = QuestsSection.Read(reader.ReadBytes(302)),
Waypoints = WaypointsSection.Read(reader.ReadBytes(80)),
Quests = QuestsSection.Read(reader),
Waypoints = WaypointsSection.Read(reader),
NPCDialog = NPCDialogSection.Read(reader.ReadBytes(52)),
Attributes = Attributes.Read(reader)
};
Expand Down Expand Up @@ -154,8 +153,8 @@ public static byte[] Write(D2S d2s)
d2s.Mercenary.Write(writer);
//0x00bf [unk = 0x0] (server related data)
writer.WriteBytes(d2s.RealmData ?? new byte[140]);
writer.WriteBytes(QuestsSection.Write(d2s.Quests));
writer.WriteBytes(WaypointsSection.Write(d2s.Waypoints));
d2s.Quests.Write(writer);
d2s.Waypoints.Write(writer);
writer.WriteBytes(NPCDialogSection.Write(d2s.NPCDialog));
d2s.Attributes.Write(writer);
d2s.ClassSkills.Write(writer);
Expand Down
4 changes: 2 additions & 2 deletions src/Model/Save/Header.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ public class Header
//0x000c
public uint Checksum { get; set; }

public void Write(BitWriter writer)
public void Write(IBitWriter writer)
{
writer.WriteUInt32(Magic ?? 0xAA55AA55);
writer.WriteUInt32(Version);
writer.WriteUInt32(Filesize);
writer.WriteUInt32(Checksum);
}

public static Header Read(BitReader reader)
public static Header Read(IBitReader reader)
{
var header = new Header
{
Expand Down
28 changes: 14 additions & 14 deletions src/Model/Save/Items.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ private ItemList(ushort header, ushort count)
public ushort Count { get; set; }
public List<Item> Items { get; }

public void Write(BitWriter writer, uint version)
public void Write(IBitWriter writer, uint version)
{
writer.WriteUInt16(Header ?? 0x4D4A);
writer.WriteUInt16(Count);
Expand All @@ -67,7 +67,7 @@ public void Write(BitWriter writer, uint version)
}
}

public static ItemList Read(BitReader reader, uint version)
public static ItemList Read(IBitReader reader, uint version)
{
var itemList = new ItemList(
header: reader.ReadUInt16(),
Expand Down Expand Up @@ -139,7 +139,7 @@ public class Item
public bool IsPersonalized { get => Flags[24]; set => Flags[24] = value; }
public bool IsRuneword { get => Flags[26]; set => Flags[26] = value; }

public void Write(BitWriter writer, uint version)
public void Write(IBitWriter writer, uint version)
{
if (version <= 0x60)
{
Expand All @@ -164,7 +164,7 @@ public static Item Read(ReadOnlySpan<byte> bytes, uint version)
return Read(reader, version);
}

public static Item Read(BitReader reader, uint version)
public static Item Read(IBitReader reader, uint version)
{
var item = new Item();
if (version <= 0x60)
Expand Down Expand Up @@ -192,7 +192,7 @@ public static byte[] Write(Item item, uint version)
return writer.ToArray();
}

protected static string ReadPlayerName(BitReader reader)
protected static string ReadPlayerName(IBitReader reader)
{
char[] name = new char[15];
for (int i = 0; i < name.Length; i++)
Expand All @@ -206,7 +206,7 @@ protected static string ReadPlayerName(BitReader reader)
return new string(name);
}

protected static void WritePlayerName(BitWriter writer, string name)
protected static void WritePlayerName(IBitWriter writer, string name)
{
byte[] bytes = Encoding.ASCII.GetBytes(name.Replace("\0", ""));
for (int i = 0; i < bytes.Length; i++)
Expand All @@ -216,7 +216,7 @@ protected static void WritePlayerName(BitWriter writer, string name)
writer.WriteByte((byte)'\0', 7);
}

protected static void ReadCompact(BitReader reader, Item item, uint version)
protected static void ReadCompact(IBitReader reader, Item item, uint version)
{
Span<byte> bytes = stackalloc byte[4];
reader.ReadBytes(bytes);
Expand Down Expand Up @@ -258,7 +258,7 @@ protected static void ReadCompact(BitReader reader, Item item, uint version)
}
}

protected static void WriteCompact(BitWriter writer, Item item, uint version)
protected static void WriteCompact(IBitWriter writer, Item item, uint version)
{
if (item.Flags is not InternalBitArray flags)
{
Expand Down Expand Up @@ -317,7 +317,7 @@ protected static void WriteCompact(BitWriter writer, Item item, uint version)

}

protected static void ReadComplete(BitReader reader, Item item, uint version)
protected static void ReadComplete(IBitReader reader, Item item, uint version)
{
item.Id = reader.ReadUInt32();
item.ItemLevel = reader.ReadByte(7);
Expand Down Expand Up @@ -432,7 +432,7 @@ protected static void ReadComplete(BitReader reader, Item item, uint version)
}
}

protected static void WriteComplete(BitWriter writer, Item item, uint version)
protected static void WriteComplete(IBitWriter writer, Item item, uint version)
{
writer.WriteUInt32(item.Id);
writer.WriteByte(item.ItemLevel, 7);
Expand Down Expand Up @@ -562,7 +562,7 @@ public class ItemStatList

public List<ItemStat> Stats { get; set; } = new List<ItemStat>();

public static ItemStatList Read(BitReader reader)
public static ItemStatList Read(IBitReader reader)
{
var itemStatList = new ItemStatList();
ushort id = reader.ReadUInt16(9);
Expand All @@ -584,7 +584,7 @@ public static ItemStatList Read(BitReader reader)
return itemStatList;
}

public static void Write(BitWriter writer, ItemStatList itemStatList)
public static void Write(IBitWriter writer, ItemStatList itemStatList)
{
for (int i = 0; i < itemStatList.Stats.Count; i++)
{
Expand Down Expand Up @@ -622,7 +622,7 @@ public class ItemStat
public int? Param { get; set; }
public int Value { get; set; }

public static ItemStat Read(BitReader reader, ushort id)
public static ItemStat Read(IBitReader reader, ushort id)
{
var itemStat = new ItemStat();
var property = Core.TXT.ItemStatCostTXT[id];
Expand Down Expand Up @@ -676,7 +676,7 @@ public static ItemStat Read(BitReader reader, ushort id)
return itemStat;
}

public static void Write(BitWriter writer, ItemStat stat)
public static void Write(IBitWriter writer, ItemStat stat)
{
var property = GetStatRow(stat);
if (property == null)
Expand Down
8 changes: 4 additions & 4 deletions src/Model/Save/Locations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ public class Locations
public Location Nightmare { get => _locations[1]; set => _locations[1] = value; }
public Location Hell { get => _locations[2]; set => _locations[2] = value; }

public void Write(BitWriter writer)
public void Write(IBitWriter writer)
{
for (int i = 0; i < _locations.Length; i++)
{
_locations[i].Write(writer);
}
}

public static Locations Read(BitReader reader)
public static Locations Read(IBitReader reader)
{
var locations = new Locations();
var places = locations._locations;
Expand Down Expand Up @@ -57,7 +57,7 @@ public Location(bool active, byte act)
public readonly bool Active { get; }
public readonly byte Act { get; }

public void Write(BitWriter writer)
public void Write(IBitWriter writer)
{
byte b = 0x0;
if (Active)
Expand All @@ -70,7 +70,7 @@ public void Write(BitWriter writer)
writer.WriteByte(b);
}

public static Location Read(BitReader reader)
public static Location Read(IBitReader reader)
{
byte b = reader.ReadByte();
return new Location(
Expand Down
Loading

0 comments on commit 28fcedb

Please sign in to comment.