Skip to content

Commit

Permalink
Handle failure to compress resources
Browse files Browse the repository at this point in the history
  • Loading branch information
LazyDuchess committed Jun 30, 2024
1 parent 4ebbff1 commit 974f794
Showing 1 changed file with 22 additions and 6 deletions.
28 changes: 22 additions & 6 deletions Assets/Scripts/OpenTS2/Files/Formats/DBPF/DBPFFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -471,8 +471,14 @@ public void Serialize(Stream stream)
{
UpdateDIR();
using var writer = new BinaryWriter(stream);
var dirEntry = GetEntryByTGI(ResourceKey.DIR);
var dirAsset = GetAssetByTGI<DIRAsset>(ResourceKey.DIR);
var entries = Entries;
if (dirEntry != null)
{
entries.Remove(dirEntry);
entries.Add(dirEntry);
}
//HeeeADER
writer.Write(new char[] { 'D', 'B', 'P', 'F' });
//major version
Expand Down Expand Up @@ -558,13 +564,23 @@ public void Serialize(Stream stream)
var entryData = entry.GetBytes();
if (dirAsset != null && dirAsset.GetUncompressedSize(entry.TGI) != 0)
{
entryData = QfsCompression.Compress(entryData, true);
var lastPosition = stream.Position;
stream.Position = entryOffset[i] + 4;
writer.Write(entryData.Length);
stream.Position = lastPosition;
var compressedEntryData = QfsCompression.Compress(entryData, true);
if (compressedEntryData != null)
{
var lastPosition = stream.Position;
stream.Position = entryOffset[i] + 4;
writer.Write(entryData.Length);
stream.Position = lastPosition;
}
else
{
Debug.LogWarning($"Failed to compress resource {entry.TGI} in package {FilePath}. Writing uncompressed.");
writer.Write(entryData, 0, entryData.Length);
dirAsset.SizeByInternalTGI[entry.TGI] = 0;
}
}
writer.Write(entryData, 0, entryData.Length);
else
writer.Write(entryData, 0, entryData.Length);
}
}
}
Expand Down

0 comments on commit 974f794

Please sign in to comment.