Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions src/Imcodec.ObjectProperty/ObjectSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -253,19 +253,22 @@ public virtual bool PreWriteObject(BitWriter writer,
/// to compress.</param>
/// <returns>A <see cref="BitWriter"/> containing the compressed data.</returns>
protected virtual BitWriter Compress(BitWriter writer) {
var uncompressedSize = writer.GetData().Length;
var compressedData = Compression.Compress(writer.GetData());
// Compress the data and at the start of the buffer, write the uncompressed size.
var writerData = writer.GetData();
var uncompressedSize = writerData.Length;
var compressedData = Compression.Compress(writerData);

var bufferSize = compressedData.Length + 4;
var tempBuffer = new byte[bufferSize];
var uncompressedSizeWriteLength = sizeof(int);
var deflatedBufferSize = uncompressedSizeWriteLength + compressedData.Length;
var deflatedBuffer = new byte[deflatedBufferSize];

using var memoryStream = new MemoryStream(tempBuffer);
using var memoryStream = new MemoryStream(deflatedBuffer);
using var binaryWriter = new BinaryWriter(memoryStream);

binaryWriter.Write(uncompressedSize);
binaryWriter.Write(compressedData);

return new BitWriter(tempBuffer);
return new BitWriter(deflatedBuffer);
}

/// <summary>
Expand Down
42 changes: 42 additions & 0 deletions test/ObjectPropertyTest/LootTableTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public class LootTableTest {
*/

private const string LootTableBlob = "2A0367480100000089876B65050000000000050000005E39841B0100000002000000";
private const string LootTableBlobCompressed = "2200000078DAD3624EF760646060E86CCF4E65650001101967D9220D126502620057C70401";

[Fact]
public void TryDeserializeLootTableBlob() {
Expand Down Expand Up @@ -71,4 +72,45 @@ public void TrySerializeLootTable() {
Assert.Equal(LootTableBlob, hexBlob);
}

[Fact]
public void TrySerializeWithCompression() {
// Serialize a loot info list with compression and see if it matches the expected blob.
var serializer = new ObjectSerializer(false, SerializerFlags.Compress, new DummyTypeRegistry());
var lootTable = new LootInfoList {
m_goldInfo = new GoldLootInfo {
m_goldAmount = 2,
m_lootType = LOOT_TYPE.LOOT_TYPE_GOLD
},
m_loot = [
new MagicXPLootInfo { m_lootType = LOOT_TYPE.LOOT_TYPE_MAGIC_XP, m_experience = 5 }
]
};

var serializeSuccess = serializer.Serialize(lootTable, (PropertyFlags) 31, out var byteBlob);
Assert.True(serializeSuccess);
Assert.True(byteBlob is not null);

var hexBlob = BitConverter.ToString(byteBlob).Replace("-", "");
Assert.Equal(LootTableBlobCompressed, hexBlob);
}

[Fact]
public void TryDeserializeWithCompression() {
// Deserialize a compressed loot info list and see if it matches the expected blob.
var serializer = new ObjectSerializer(false, SerializerFlags.Compress, new DummyTypeRegistry());
var byteBlob = Convert.FromHexString(LootTableBlobCompressed);
var deserializeSuccess = serializer.Deserialize<LootInfoList>(byteBlob, (PropertyFlags) 31, out var lootTable);

Assert.True(deserializeSuccess);
Assert.NotNull(lootTable);
Assert.NotNull(lootTable.m_goldInfo);
Assert.True(lootTable.m_goldInfo.m_goldAmount == 2);
Assert.True(lootTable.m_goldInfo.m_lootType == LOOT_TYPE.LOOT_TYPE_GOLD);

Assert.NotNull(lootTable.m_loot);
Assert.True(lootTable.m_loot.Count == 1);
Assert.True(lootTable.m_loot[0] is MagicXPLootInfo);
Assert.True(lootTable.m_loot[0].m_lootType == LOOT_TYPE.LOOT_TYPE_MAGIC_XP);
}

}
Loading