Skip to content

Commit

Permalink
Slightly simplify previous fix.
Browse files Browse the repository at this point in the history
  • Loading branch information
awgil committed Aug 18, 2024
1 parent 64f2594 commit 913cc35
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 26 deletions.
31 changes: 6 additions & 25 deletions vnavmesh/Navmesh.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,9 @@ public static Navmesh Deserialize(BinaryReader reader, int expectedCustomization
if (customizationVersion != expectedCustomizationVersion)
throw new Exception("Outdated customization version");

var compressedDataLength = reader.ReadInt32();
//Service.Log.Debug($"[Deserialize] Header, Magic {magic:X} , Version {version}, cv {customizationVersion}, length {compressedDataLength}");
byte[] compressedData = reader.ReadBytes(compressedDataLength);

using var compressedStream = new MemoryStream(compressedData);
using var decompressedStream = new BrotliStream(compressedStream, CompressionMode.Decompress, true);
using var decompressedReader = new BinaryReader(decompressedStream);

var mesh = DeserializeMesh(decompressedReader);
var volume = DeserializeVolume(decompressedReader);

using var compressedReader = new BinaryReader(new BrotliStream(reader.BaseStream, CompressionMode.Decompress, true));
var mesh = DeserializeMesh(compressedReader);
var volume = DeserializeVolume(compressedReader);
return new(customizationVersion, mesh, volume);
}

Expand All @@ -44,20 +36,9 @@ public void Serialize(BinaryWriter writer)
writer.Write(Version);
writer.Write(CustomizationVersion);

using (var memoryStream = new MemoryStream()) {
using (var compressedStream = new BrotliStream(memoryStream, CompressionLevel.Optimal, true)) {
using (var bufferWriter = new BinaryWriter(compressedStream)) {
// Serialize data into the BrotliStream
SerializeMesh(bufferWriter, Mesh);
SerializeVolume(bufferWriter, Volume);
}
}

byte[] compressedData = memoryStream.ToArray();
//Service.Log.Debug($"[Serialize] Header, Magic {Magic:X} , Version {Version}, cv {CustomizationVersion}, length {compressedData.Length}");
writer.Write(compressedData.Length);
writer.Write(compressedData); // Write the compressed data itself
}
using var compressedWriter = new BinaryWriter(new BrotliStream(writer.BaseStream, CompressionLevel.Optimal, true));
SerializeMesh(compressedWriter, Mesh);
SerializeVolume(compressedWriter, Volume);
}

private static DtNavMesh DeserializeMesh(BinaryReader reader)
Expand Down
2 changes: 1 addition & 1 deletion vnavmesh/NavmeshManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ private Navmesh BuildNavmesh(SceneDefinition scene, string cacheKey, bool allowL
// write results to cache
{
Service.Log.Debug($"Writing cache: {cache.FullName}");
using var stream = new FileStream(cache.FullName, FileMode.Create, FileAccess.Write);
using var stream = cache.Open(FileMode.Create, FileAccess.Write, FileShare.None);
using var writer = new BinaryWriter(stream);
builder.Navmesh.Serialize(writer);
}
Expand Down

0 comments on commit 913cc35

Please sign in to comment.