Skip to content

Commit

Permalink
Implement FUntypedBulkData (UE3).
Browse files Browse the repository at this point in the history
  • Loading branch information
EliotVU committed Nov 9, 2022
1 parent acb3cdc commit 02bea77
Show file tree
Hide file tree
Showing 6 changed files with 382 additions and 70 deletions.
13 changes: 12 additions & 1 deletion Test/UnrealFlagsTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using UELib.Branch;
using UELib.Core;
using UELib.Flags;

namespace Eliot.UELib.Test
Expand Down Expand Up @@ -33,5 +34,15 @@ public void TestUnrealPackageFlags()
Assert.IsTrue(flags.HasFlags((uint)DefaultEngineBranch.PackageFlagsDefault.AllowDownload));
Assert.IsFalse(flags.HasFlags((uint)DefaultEngineBranch.PackageFlagsDefault.ClientOptional));
}

[TestMethod]
public void TestBulkDataToCompressionFlags()
{
const BulkDataFlags dataFlags = BulkDataFlags.Unused | BulkDataFlags.CompressedLZX;

var compressionFlags = dataFlags.ToCompressionFlags();
Assert.IsTrue(compressionFlags.HasFlag(CompressionFlags.ZLX));
Assert.IsTrue(compressionFlags == CompressionFlags.ZLX);
}
}
}
}
62 changes: 54 additions & 8 deletions Test/UnrealStreamTests.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using System;
using System.IO;
using System.Reflection;
using System.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using UELib;
using UELib.Branch;
using UELib.Core;

namespace Eliot.UELib.Test
Expand All @@ -12,19 +12,19 @@ namespace Eliot.UELib.Test
public class UnrealStreamTests
{
// HACK: Ugly workaround the issues with UPackageStream
private static UPackageStream CreateTempStream(string name = "test.u")
private static UPackageStream CreateTempStream()
{
string tempFilePath = Path.Join(Assembly.GetExecutingAssembly().Location, "../", name);
string tempFilePath = Path.Join(Path.GetTempFileName());
File.WriteAllBytes(tempFilePath, BitConverter.GetBytes(UnrealPackage.Signature));

var stream = new UPackageStream(tempFilePath, FileMode.Open, FileAccess.ReadWrite);
return stream;
}

[TestMethod]
public void ReadString()
{
using var stream = CreateTempStream("string.u");
using var stream = CreateTempStream();
using var linker = new UnrealPackage(stream);
linker.Summary = new UnrealPackage.PackageFileSummary
{
Expand Down Expand Up @@ -62,7 +62,7 @@ public void ReadString()
[TestMethod]
public void ReadAtomicStruct()
{
using var stream = CreateTempStream("atomicstruct.u");
using var stream = CreateTempStream();
using var linker = new UnrealPackage(stream);
linker.Summary = new UnrealPackage.PackageFileSummary
{
Expand All @@ -73,7 +73,7 @@ public void ReadAtomicStruct()
using var writer = new BinaryWriter(stream);
// Skip past the signature
writer.Seek(sizeof(int), SeekOrigin.Begin);

// B, G, R, A;
var inColor = new UColor(255, 128, 64, 80);
stream.WriteAtomicStruct(ref inColor);
Expand All @@ -82,11 +82,57 @@ public void ReadAtomicStruct()
stream.Seek(sizeof(int), SeekOrigin.Begin);
stream.ReadAtomicStruct(out UColor outColor);
Assert.AreEqual(8, stream.Position);

Assert.AreEqual(255, outColor.B);
Assert.AreEqual(128, outColor.G);
Assert.AreEqual(64, outColor.R);
Assert.AreEqual(80, outColor.A);
}

[TestMethod]
public void TestBulkData()
{
using var stream = CreateTempStream();
using var linker = new UnrealPackage(stream);
linker.Summary = new UnrealPackage.PackageFileSummary
{
Build = new UnrealPackage.GameBuild(linker),
};

using var writer = new BinaryWriter(stream);
// Skip past the signature
writer.Seek(sizeof(int), SeekOrigin.Begin);

// Verify the oldest stage of LazyArray.
//linker.Summary.Version = (uint)PackageObjectLegacyVersion.LazyArraySkipCountToSkipOffset - 1;
//TestBulkDataSerialization(stream);

//linker.Summary.Version = (uint)PackageObjectLegacyVersion.LazyArraySkipCountToSkipOffset;
//TestBulkDataSerialization(stream);

linker.Summary.Version = (uint)PackageObjectLegacyVersion.LazyArrayReplacedWithBulkData;
TestBulkDataSerialization(stream);
}

private void TestBulkDataSerialization(IUnrealStream stream)
{
byte[] rawData = Encoding.ASCII.GetBytes("LET'S PRETEND THAT THIS IS BULK DATA!");
var bulkData = new UBulkData<byte>(0, rawData);

long bulkPosition = stream.Position;
stream.Write(ref bulkData);
Assert.AreEqual(rawData.Length, bulkData.StorageSize);

stream.Position = bulkPosition;
stream.Read(out UBulkData<byte> readBulkData);
Assert.IsNull(readBulkData.ElementData);
Assert.AreEqual(bulkData.StorageSize, readBulkData.StorageSize);
Assert.AreEqual(bulkData.StorageOffset, readBulkData.StorageOffset);
Assert.AreEqual(bulkData.ElementCount, readBulkData.ElementCount);

readBulkData.LoadData(stream);
Assert.IsNotNull(readBulkData.ElementData);
Assert.AreEqual(bulkData.ElementData.Length, readBulkData.ElementData.Length);
}
}
}
30 changes: 18 additions & 12 deletions src/Branch/PackageObjectLegacyVersion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,17 @@ public enum PackageObjectLegacyVersion
ReturnExpressionAddedToReturnToken = 62,

SphereExtendsPlane = 62,
LazyArraySkipCountToSkipOffset = 62,
LazyArraySkipCountChangedToSkipOffset = 62,

CharRemapAddedToUFont = 69,

/// <summary>
/// FIXME: Unknown version.
/// </summary>
CastStringSizeTokenDeprecated = 70,

PanUVRemovedFromPoly = 78,

CompMipsDeprecated = 84,

/// <summary>
Expand All @@ -36,28 +36,34 @@ public enum PackageObjectLegacyVersion
// The estimated version changes that came after the latest known UE2 build.
TextureDeprecatedFromPoly = 170,
MaterialAddedToPoly = 170,


UE3 = 178,

/// <summary>
/// Present in all released UE3 games (starting with RoboBlitz).
///
/// FIXME: Unknown version.
/// </summary>
IsLocalAddedToDelegateFunctionToken = 181,

UE3 = 184,


// FIXME: Version
RangeConstTokenDeprecated = UE3,

// FIXME: Version
FastSerializeStructs = UE3,

// FIXME: Version
EnumTagNameAddedToBytePropertyTag = UE3,

// 227 according to the GoW client
FixedVerticesToArrayFromPoly = 227,

// Thanks to @https://www.gildor.org/ for reverse-engineering the lazy-loader version changes.
LazyLoaderFlagsAddedToLazyArray = 251,
StorageSizeAddedToLazyArray = 254,
L8AddedToLazyArray = 260,
LazyArrayReplacedWithBulkData = 266,

// FIXME: Not attested in the GoW client, must have been before v321
LightMapScaleRemovedFromPoly = 300,

Expand All @@ -66,10 +72,10 @@ public enum PackageObjectLegacyVersion

// 321 according to the GoW client
ElementOwnerAddedToUPolys = 321,

// 417 according to the GoW client
LightingChannelsAddedToPoly = 417,

VerticalOffsetAddedToUFont = 506,
CleanupFonts = 511,

Expand All @@ -88,4 +94,4 @@ public enum PackageObjectLegacyVersion
ForceScriptOrderAddedToUClass = 749,
SuperReferenceMovedToUStruct = 756,
}
}
}
Loading

0 comments on commit 02bea77

Please sign in to comment.