diff --git a/src/Imcodec.ObjectProperty/ObjectSerializer.cs b/src/Imcodec.ObjectProperty/ObjectSerializer.cs
index 6b6b03a..911d4ca 100644
--- a/src/Imcodec.ObjectProperty/ObjectSerializer.cs
+++ b/src/Imcodec.ObjectProperty/ObjectSerializer.cs
@@ -253,19 +253,22 @@ public virtual bool PreWriteObject(BitWriter writer,
/// to compress.
/// A containing the compressed data.
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);
}
///
diff --git a/test/ObjectPropertyTest/LootTableTest.cs b/test/ObjectPropertyTest/LootTableTest.cs
index 45e5271..1cd0522 100644
--- a/test/ObjectPropertyTest/LootTableTest.cs
+++ b/test/ObjectPropertyTest/LootTableTest.cs
@@ -30,6 +30,7 @@ public class LootTableTest {
*/
private const string LootTableBlob = "2A0367480100000089876B65050000000000050000005E39841B0100000002000000";
+ private const string LootTableBlobCompressed = "2200000078DAD3624EF760646060E86CCF4E65650001101967D9220D126502620057C70401";
[Fact]
public void TryDeserializeLootTableBlob() {
@@ -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(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);
+ }
+
}