Skip to content

Commit

Permalink
preserve extra appended null bytes for archengius + trumank zen project
Browse files Browse the repository at this point in the history
  • Loading branch information
atenfyr committed Jan 26, 2025
1 parent e77ff11 commit 5c5122c
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 1 deletion.
1 change: 1 addition & 0 deletions UAssetAPI.Tests/AssetUnitTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -575,6 +575,7 @@ public void TestTraditionalUE5_4()
TestUE5_4Subsection("BlankGame", EngineVersion.VER_UE5_4, new Usmap(Path.Combine("TestAssets", "TestUE5_4", "BlankGame", "BlankGame_Dumper-7.usmap")));
TestUE5_4Subsection("Bellwright", EngineVersion.VER_UE5_4, new Usmap(Path.Combine("TestAssets", "TestUE5_4", "Bellwright", "Bellwright.usmap")));
TestUE5_4Subsection("TheForeverWinter", EngineVersion.VER_UE5_4, new Usmap(Path.Combine("TestAssets", "TestUE5_4", "TheForeverWinter", "TheForeverWinter.usmap")));
TestUE5_4Subsection("Billiards", EngineVersion.VER_UE5_4, new Usmap(Path.Combine("TestAssets", "TestUE5_4", "Billiards", "5.4.3-34507850+++UE5+Release-5.4-DeepSpace7.usmap")));
}

/// <summary>
Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
9 changes: 9 additions & 0 deletions UAssetAPI.Tests/UAssetAPI.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,15 @@
<None Update="TestAssets\TestUE5_4\Bellwright\UMG_TreeViewDemo.uasset">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="TestAssets\TestUE5_4\Billiards\5.4.3-34507850+++UE5+Release-5.4-DeepSpace7.usmap">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="TestAssets\TestUE5_4\Billiards\BP_Table_Lamp.uasset">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="TestAssets\TestUE5_4\Billiards\BP_Table_Lamp.uexp">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="TestAssets\TestUE5_4\BlankGame\BP_CubePawn.combined">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
Expand Down
34 changes: 33 additions & 1 deletion UAssetAPI/UAsset.cs
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,12 @@ public bool HasUnversionedProperties
/// </summary>
public FWorldTileInfo WorldTileInfo;

/// <summary>
/// The number of null bytes appended to the end of the package header (.uasset file).
/// This should typically be zero, but may be greater when reading assets generated by external tools.
/// </summary>
public int AppendedNullBytes;

/// <summary>
/// In MapProperties that have StructProperties as their keys or values, there is no universal, context-free way to determine the type of the struct.
/// <para />
Expand Down Expand Up @@ -1872,8 +1878,10 @@ public virtual void Read(AssetBinaryReader reader, int[] manualSkips = null, int
// SeaOfThievesGarbageData
if (SeaOfThievesGarbageDataOffset > 0 && SeaOfThievesGarbageDataLength > 0)
{
long before = reader.BaseStream.Position;
reader.BaseStream.Seek(SeaOfThievesGarbageDataOffset, SeekOrigin.Begin);
SeaOfThievesGarbageData = reader.ReadBytes(SeaOfThievesGarbageDataLength);
reader.BaseStream.Seek(before, SeekOrigin.Begin);
}
else if (SeaOfThievesGarbageDataOffset == 0 || SeaOfThievesGarbageDataLength == 0)
{
Expand All @@ -1887,8 +1895,10 @@ public virtual void Read(AssetBinaryReader reader, int[] manualSkips = null, int
BulkData = [];
if (BulkDataStartOffset > 0 && reader.LoadUexp)
{
long before = reader.BaseStream.Position;
reader.BaseStream.Seek(BulkDataStartOffset, SeekOrigin.Begin);
BulkData = reader.ReadBytes((int)(reader.BaseStream.Length - BulkDataStartOffset));
reader.BaseStream.Seek(before, SeekOrigin.Begin);
}

// WorldTileInfoDataOffset
Expand All @@ -1905,9 +1915,11 @@ public virtual void Read(AssetBinaryReader reader, int[] manualSkips = null, int
}

// PreloadDependencies
if (PreloadDependencyOffset > 0) reader.BaseStream.Seek(PreloadDependencyOffset, SeekOrigin.Begin); // needed so that we're at a sensible offset for AppendedNullBytes if no preload deps
for (int i = 0; i < Exports.Count; i++)
{
if (Exports[i].FirstExportDependencyOffset < 0) continue;
if (PreloadDependencyOffset <= 0) continue;
if (Exports[i].FirstExportDependencyOffset < 0) continue; // not <= 0
this.UsesEventDrivenLoader = true;

reader.BaseStream.Seek(PreloadDependencyOffset, SeekOrigin.Begin);
Expand Down Expand Up @@ -1949,6 +1961,20 @@ public virtual void Read(AssetBinaryReader reader, int[] manualSkips = null, int
}
}

// possible for some null bytes to exist at end of .uasset file as part of Archengius + trumank zen to legacy conversion project
// as with other changes made by external tools that are accepted by the engine, we would like to maintain these bytes
if (Exports.Count > 0)
{
long offsetDiff = Exports[0].SerialOffset - reader.BaseStream.Position;
byte[] paddingBytes = reader.ReadBytes((int)offsetDiff);
foreach (byte byt in paddingBytes)
{
// if non-null then we expect that some serialization problem has occurred
if (byt != 0) throw new FormatException("Encountered additional non-null data at end of legacy header data");
}
AppendedNullBytes = paddingBytes.Length; // int rather than byte[] so easy to understand in JSON
}

if (reader.LoadUexp)
{
bool skipParsingExports = CustomSerializationFlags.HasFlag(CustomSerializationFlags.SkipParsingExports);
Expand Down Expand Up @@ -2575,6 +2601,12 @@ public virtual MemoryStream WriteData()
}
}

if (AppendedNullBytes > 0)
{
// new arrays guaranteed to be zero'd
writer.Write(new byte[AppendedNullBytes]);
}

// Export data
int oldOffset = this.SectionSixOffset;
this.SectionSixOffset = (int)writer.BaseStream.Position;
Expand Down

0 comments on commit 5c5122c

Please sign in to comment.