Skip to content

Commit

Permalink
include module paths in ancestry when available
Browse files Browse the repository at this point in the history
  • Loading branch information
atenfyr committed Jun 26, 2024
1 parent f4cd56e commit 00cc980
Show file tree
Hide file tree
Showing 18 changed files with 156 additions and 68 deletions.
8 changes: 4 additions & 4 deletions UAssetAPI/ExportTypes/DataTableExport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,15 @@ public override void Read(AssetBinaryReader reader, int nextStarting)
Table = new UDataTable();

int numEntries = reader.ReadInt32();
FName pcen = reader.Asset.GetParentClassExportName();
FName pcen = reader.Asset.GetParentClassExportName(out FName pcen2);
for (int i = 0; i < numEntries; i++)
{
FName rowName = reader.ReadFName();
var nextStruct = new StructPropertyData(rowName)
{
StructType = decidedStructType
};
nextStruct.Ancestry.Initialize(null, pcen);
nextStruct.Ancestry.Initialize(null, pcen, pcen2);
nextStruct.Read(reader, false, 1);
Table.Data.Add(nextStruct);
}
Expand All @@ -83,8 +83,8 @@ public override void Read(AssetBinaryReader reader, int nextStarting)
public override void ResolveAncestries(UnrealPackage asset, AncestryInfo ancestrySoFar)
{
var ancestryNew = (AncestryInfo)ancestrySoFar.Clone();
FName pcen = asset.GetParentClassExportName();
ancestryNew.SetAsParent(pcen);
FName pcen = asset.GetParentClassExportName(out FName pcen2);
ancestryNew.SetAsParent(pcen, pcen2);

if (Data != null)
{
Expand Down
14 changes: 9 additions & 5 deletions UAssetAPI/ExportTypes/Export.cs
Original file line number Diff line number Diff line change
Expand Up @@ -480,17 +480,21 @@ public FName GetExportClassType()
return this.ClassIndex.IsImport() ? this.ClassIndex.ToImport(Asset).ObjectName : FName.DefineDummy(Asset, this.ClassIndex.Index.ToString());
}

public FName GetClassTypeForAncestry(UnrealPackage asset = null)
public FName GetClassTypeForAncestry(UnrealPackage asset, out FName modulePath)
{
if (asset == null) asset = Asset;
return GetClassTypeForAncestry(this.ClassIndex, asset);
return GetClassTypeForAncestry(this.ClassIndex, asset, out modulePath);
}

public static FName GetClassTypeForAncestry(FPackageIndex classIndex, UnrealPackage asset = null)
public static FName GetClassTypeForAncestry(FPackageIndex classIndex, UnrealPackage asset, out FName modulePath)
{
modulePath = null;
if (classIndex.IsNull()) return null;
if (classIndex.IsImport()) return classIndex.ToImport(asset).ObjectName;
return classIndex.ToExport(asset).ObjectName;
if (classIndex.IsExport()) return classIndex.ToExport(asset).ObjectName;

var imp = classIndex.ToImport(asset);
if (imp.OuterIndex.IsImport()) modulePath = imp.OuterIndex.ToImport(asset).ObjectName;
return imp.ObjectName;
}

public override string ToString()
Expand Down
10 changes: 7 additions & 3 deletions UAssetAPI/ExportTypes/NormalExport.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System.Reflection.PortableExecutable;
using UAssetAPI.PropertyTypes.Objects;
using UAssetAPI.UnrealTypes;
using UAssetAPI.Unversioned;
Expand Down Expand Up @@ -100,7 +101,8 @@ public override void Read(AssetBinaryReader reader, int nextStarting = 0)
PropertyData bit;

var unversionedHeader = new FUnversionedHeader(reader);
while ((bit = MainSerializer.Read(reader, null, GetClassTypeForAncestry(reader.Asset), unversionedHeader, true)) != null)
FName parentName = GetClassTypeForAncestry(reader.Asset, out FName parentModulePath);
while ((bit = MainSerializer.Read(reader, null, parentName, parentModulePath, unversionedHeader, true)) != null)
{
Data.Add(bit);
}
Expand All @@ -109,7 +111,7 @@ public override void Read(AssetBinaryReader reader, int nextStarting = 0)
public override void ResolveAncestries(UnrealPackage asset, AncestryInfo ancestrySoFar)
{
var ancestryNew = (AncestryInfo)ancestrySoFar.Clone();
ancestryNew.SetAsParent(GetClassTypeForAncestry(asset));
ancestryNew.SetAsParent(GetClassTypeForAncestry(asset, out FName modulePath), modulePath);

if (Data != null)
{
Expand All @@ -120,7 +122,9 @@ public override void ResolveAncestries(UnrealPackage asset, AncestryInfo ancestr

public override void Write(AssetBinaryWriter writer)
{
MainSerializer.GenerateUnversionedHeader(ref Data, GetClassTypeForAncestry(writer.Asset), writer.Asset)?.Write(writer);
FName parentName = GetClassTypeForAncestry(writer.Asset, out FName parentModulePath);

MainSerializer.GenerateUnversionedHeader(ref Data, parentName, parentModulePath, writer.Asset)?.Write(writer);
for (int j = 0; j < Data.Count; j++)
{
PropertyData current = Data[j];
Expand Down
6 changes: 3 additions & 3 deletions UAssetAPI/ExportTypes/UserDefinedStructExport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public override void Read(AssetBinaryReader reader, int nextStarting)
PropertyData bit;

var unversionedHeader = new FUnversionedHeader(reader);
while ((bit = MainSerializer.Read(reader, null, this.ObjectName, unversionedHeader, true)) != null)
while ((bit = MainSerializer.Read(reader, null, this.ObjectName, null, unversionedHeader, true)) != null)
{
StructData.Add(bit);
}
Expand All @@ -52,7 +52,7 @@ public override void Read(AssetBinaryReader reader, int nextStarting)
public override void ResolveAncestries(UnrealPackage asset, AncestryInfo ancestrySoFar)
{
var ancestryNew = (AncestryInfo)ancestrySoFar.Clone();
ancestryNew.SetAsParent(this.ObjectName);
ancestryNew.SetAsParent(this.ObjectName, null);

if (StructData != null)
{
Expand All @@ -69,7 +69,7 @@ public override void Write(AssetBinaryWriter writer)

writer.Write(StructFlags);

MainSerializer.GenerateUnversionedHeader(ref StructData, this.ObjectName, writer.Asset)?.Write(writer);
MainSerializer.GenerateUnversionedHeader(ref StructData, this.ObjectName, null, writer.Asset)?.Write(writer);
for (int j = 0; j < StructData.Count; j++)
{
PropertyData current = StructData[j];
Expand Down
2 changes: 1 addition & 1 deletion UAssetAPI/IO/ZenAsset.cs
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ public override void GetParentClass(out FName parentClassPath, out FName parentC
throw new NotImplementedException("Unimplemented method ZenAsset.GetParentClass");
}

internal override FName GetParentClassExportName()
internal override FName GetParentClassExportName(out FName modulePath)
{
throw new NotImplementedException("Unimplemented method ZenAsset.GetParentClassExportName");
}
Expand Down
23 changes: 13 additions & 10 deletions UAssetAPI/MainSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,9 @@ private static void InitializePropertyTypeRegistry()
/// </summary>
/// <param name="data">The list of properties to sort and generate an unversioned header from.</param>
/// <param name="parentName">The name of the parent of all the properties.</param>
/// <param name="parentModulePath">The path to the module that the parent class/struct of this property is contained within.</param>
/// <param name="asset">The UnrealPackage which the properties are contained within.</param>
public static FUnversionedHeader GenerateUnversionedHeader(ref List<PropertyData> data, FName parentName, UnrealPackage asset)
public static FUnversionedHeader GenerateUnversionedHeader(ref List<PropertyData> data, FName parentName, FName parentModulePath, UnrealPackage asset)
{
var sortedProps = new List<PropertyData>();
if (!asset.HasUnversionedProperties) return null; // no point in wasting time generating it
Expand Down Expand Up @@ -218,17 +219,17 @@ public static FUnversionedHeader GenerateUnversionedHeader(ref List<PropertyData
{
// add "blank" fragment
// i'm pretty sure that any SkipNum should work here as long as ValueNum = 0, but this is what the engine does
string highestSchema = parentName?.Value?.Value;
string highestSchema = parentName?.ToString();

// i doubt that this is true, empirically tested; need more data
int numSkip = 0;
if (asset.ObjectVersionUE5 >= ObjectVersionUE5.ADD_SOFTOBJECTPATH_LIST)
{
numSkip = Math.Min(asset.Mappings.GetAllProperties(highestSchema).Count, FFragment.SkipMax);
numSkip = Math.Min(asset.Mappings.GetAllProperties(highestSchema, parentModulePath?.ToString()).Count, FFragment.SkipMax);
}
else
{
numSkip = asset.Mappings.Schemas[highestSchema].Properties.Count == 0 ? 0 : Math.Min(asset.Mappings.GetAllProperties(highestSchema).Count, FFragment.SkipMax);
numSkip = asset.Mappings.Schemas[highestSchema].Properties.Count == 0 ? 0 : Math.Min(asset.Mappings.GetAllProperties(highestSchema, parentModulePath?.ToString()).Count, FFragment.SkipMax);
}
allFrags.Add(new FFragment(numSkip, 0, true, false));
}
Expand Down Expand Up @@ -273,14 +274,15 @@ public static FUnversionedHeader GenerateUnversionedHeader(ref List<PropertyData
/// <param name="name">The serialized name of this property.</param>
/// <param name="ancestry">The ancestry of the parent of this property.</param>
/// <param name="parentName">The name of the parent class/struct of this property.</param>
/// <param name="parentModulePath">The path to the module that the parent class/struct of this property is contained within.</param>
/// <param name="asset">The UnrealPackage which this property is contained within.</param>
/// <param name="reader">The BinaryReader to read from. If left unspecified, you must call the <see cref="PropertyData.Read(AssetBinaryReader, bool, long, long, PropertySerializationContext)"/> method manually.</param>
/// <param name="leng">The length of this property on disk in bytes.</param>
/// <param name="duplicationIndex">The duplication index of this property.</param>
/// <param name="includeHeader">Does this property serialize its header in the current context?</param>
/// <param name="isZero">Is the body of this property empty?</param>
/// <returns>A new PropertyData instance based off of the passed parameters.</returns>
public static PropertyData TypeToClass(FName type, FName name, AncestryInfo ancestry, FName parentName, UnrealPackage asset, AssetBinaryReader reader = null, int leng = 0, int duplicationIndex = 0, bool includeHeader = true, bool isZero = false)
public static PropertyData TypeToClass(FName type, FName name, AncestryInfo ancestry, FName parentName, FName parentModulePath, UnrealPackage asset, AssetBinaryReader reader = null, int leng = 0, int duplicationIndex = 0, bool includeHeader = true, bool isZero = false)
{
long startingOffset = 0;
if (reader != null) startingOffset = reader.BaseStream.Position;
Expand Down Expand Up @@ -334,7 +336,7 @@ public static PropertyData TypeToClass(FName type, FName name, AncestryInfo ance
#endif

data.IsZero = isZero;
data.Ancestry.Initialize(ancestry, parentName);
data.Ancestry.Initialize(ancestry, parentName, parentModulePath);
data.DuplicationIndex = duplicationIndex;
if (reader != null && !isZero)
{
Expand All @@ -349,7 +351,7 @@ public static PropertyData TypeToClass(FName type, FName name, AncestryInfo ance
if (data is StructPropertyData && !reader.Asset.HasUnversionedProperties)
{
data = new RawStructPropertyData(name);
data.Ancestry.Initialize(ancestry, parentName);
data.Ancestry.Initialize(ancestry, parentName, parentModulePath);
data.DuplicationIndex = duplicationIndex;
data.Read(reader, includeHeader, leng);
}
Expand All @@ -370,10 +372,11 @@ public static PropertyData TypeToClass(FName type, FName name, AncestryInfo ance
/// <param name="reader">The BinaryReader to read from. The underlying stream should be at the position of the property to be read.</param>
/// <param name="ancestry">The ancestry of the parent of this property.</param>
/// <param name="parentName">The name of the parent class/struct of this property.</param>
/// <param name="parentModulePath">The path to the module that the parent class/struct of this property is contained within.</param>
/// <param name="header">The unversioned header to be used when reading this property. Leave null if none exists.</param>
/// <param name="includeHeader">Does this property serialize its header in the current context?</param>
/// <returns>The property read from disk.</returns>
public static PropertyData Read(AssetBinaryReader reader, AncestryInfo ancestry, FName parentName, FUnversionedHeader header, bool includeHeader)
public static PropertyData Read(AssetBinaryReader reader, AncestryInfo ancestry, FName parentName, FName parentModulePath, FUnversionedHeader header, bool includeHeader)
{
long startingOffset = reader.BaseStream.Position;
FName name = null;
Expand All @@ -390,7 +393,7 @@ public static PropertyData Read(AssetBinaryReader reader, AncestryInfo ancestry,
throw new InvalidMappingsException();
}

UsmapSchema relevantSchema = reader.Asset.Mappings.GetSchemaFromName(parentName.Value.Value, reader.Asset);
UsmapSchema relevantSchema = reader.Asset.Mappings.GetSchemaFromName(parentName.Value.Value, reader.Asset, parentModulePath?.Value.Value);
while (header.UnversionedPropertyIndex > header.CurrentFragment.Value.LastNum)
{
if (header.CurrentFragment.Value.bIsLast) return null;
Expand Down Expand Up @@ -432,7 +435,7 @@ public static PropertyData Read(AssetBinaryReader reader, AncestryInfo ancestry,
duplicationIndex = reader.ReadInt32();
}

PropertyData result = TypeToClass(type, name, ancestry, parentName, reader.Asset, reader, leng, duplicationIndex, includeHeader, isZero);
PropertyData result = TypeToClass(type, name, ancestry, parentName, parentModulePath, reader.Asset, reader, leng, duplicationIndex, includeHeader, isZero);
if (structType != null && result is StructPropertyData strucProp) strucProp.StructType = FName.DefineDummy(reader.Asset, structType);
result.Offset = startingOffset;
//Debug.WriteLine(type);
Expand Down
2 changes: 1 addition & 1 deletion UAssetAPI/PropertyTypes/Objects/ArrayPropertyData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ public override void Read(AssetBinaryReader reader, bool includeHeader, long len
if (averageSizeEstimate <= 0) averageSizeEstimate = 1; // missing possible edge case where leng1 = 4 and numEntries = 2, may result is wrong size
for (int i = 0; i < numEntries; i++)
{
results[i] = MainSerializer.TypeToClass(ArrayType, FName.DefineDummy(reader.Asset, i.ToString(), int.MinValue), Ancestry, Name, reader.Asset);
results[i] = MainSerializer.TypeToClass(ArrayType, FName.DefineDummy(reader.Asset, i.ToString(), int.MinValue), Ancestry, Name, null, reader.Asset);
results[i].Offset = reader.BaseStream.Position;
if (results[i] is StructPropertyData data) data.StructType = arrayStructType == null ? FName.DefineDummy(reader.Asset, "Generic") : arrayStructType;
results[i].Read(reader, false, averageSizeEstimate, 0, PropertySerializationContext.Array);
Expand Down
2 changes: 1 addition & 1 deletion UAssetAPI/PropertyTypes/Objects/MapPropertyData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ private PropertyData MapTypeToClass(FName type, FName name, AssetBinaryReader re
data.Read(reader, false, 1, 0, PropertySerializationContext.Map);
return data;
default:
var res = MainSerializer.TypeToClass(type, name, Ancestry, Name, reader.Asset, null, leng);
var res = MainSerializer.TypeToClass(type, name, Ancestry, Name, null, reader.Asset, null, leng);
res.Ancestry.Initialize(Ancestry, Name);
res.Read(reader, includeHeader, leng, 0, PropertySerializationContext.Map);
return res;
Expand Down
8 changes: 4 additions & 4 deletions UAssetAPI/PropertyTypes/Objects/PropertyData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,16 @@ public AncestryInfo CloneWithoutParent()
return res;
}

public void Initialize(AncestryInfo ancestors, FName dad)
public void Initialize(AncestryInfo ancestors, FName dad, FName modulePath = null)
{
Ancestors.Clear();
if (ancestors != null) Ancestors.AddRange(ancestors.Ancestors);
SetAsParent(dad);
SetAsParent(dad, modulePath);
}

public void SetAsParent(FName dad)
public void SetAsParent(FName dad, FName modulePath = null)
{
if (dad != null) Ancestors.Add(dad);
if (dad != null) Ancestors.Add(modulePath == null ? dad : FName.DefineDummy(null, modulePath.Value.Value + "." + dad.Value.Value));
}
}

Expand Down
4 changes: 2 additions & 2 deletions UAssetAPI/PropertyTypes/Structs/Movies/MovieScene.cs
Original file line number Diff line number Diff line change
Expand Up @@ -654,7 +654,7 @@ public FSectionEvaluationDataTree Read(AssetBinaryReader reader) {
List<PropertyData> resultingList = new List<PropertyData>();
PropertyData data = null;
var unversionedHeader = new FUnversionedHeader(reader);
while ((data = MainSerializer.Read(reader, null, null, unversionedHeader, true)) != null) {
while ((data = MainSerializer.Read(reader, null, null, null, unversionedHeader, true)) != null) {
resultingList.Add(data);
}
items[i] = resultingList;
Expand Down Expand Up @@ -696,7 +696,7 @@ public void Write(AssetBinaryWriter writer)

if (Tree.Data.Items[i] != null) {
var dat = Tree.Data.Items[i];
MainSerializer.GenerateUnversionedHeader(ref dat, FName.DefineDummy(writer.Asset, "SectionEvaluationDataTree"), writer.Asset)?.Write(writer);
MainSerializer.GenerateUnversionedHeader(ref dat, FName.DefineDummy(writer.Asset, "SectionEvaluationDataTree"), null, writer.Asset)?.Write(writer);
foreach (var t in dat) {
MainSerializer.Write(t, writer, true);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public override int Write(AssetBinaryWriter writer, bool includeHeader, Property
if (type.Value != null)
{
var dat = Value.Except([type]).ToList();
MainSerializer.GenerateUnversionedHeader(ref dat, Name, writer.Asset)?.Write(writer);
MainSerializer.GenerateUnversionedHeader(ref dat, Name, null, writer.Asset)?.Write(writer);

foreach (var t in dat)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public override void Read(AssetBinaryReader reader, bool includeHeader, long len
List<PropertyData> resultingList = new List<PropertyData>();
PropertyData data = null;
var unversionedHeader = new FUnversionedHeader(reader);
while ((data = MainSerializer.Read(reader, Ancestry, Name, unversionedHeader, true)) != null)
while ((data = MainSerializer.Read(reader, Ancestry, Name, null, unversionedHeader, true)) != null)
{
resultingList.Add(data);
}
Expand Down Expand Up @@ -80,7 +80,7 @@ public override int Write(AssetBinaryWriter writer, bool includeHeader, Property
if (Value.Impls[i] != null)
{
var dat = Value.Impls[i];
MainSerializer.GenerateUnversionedHeader(ref dat, Name, writer.Asset)?.Write(writer);
MainSerializer.GenerateUnversionedHeader(ref dat, Name, null, writer.Asset)?.Write(writer);
foreach (var t in dat)
{
MainSerializer.Write(t, writer, true);
Expand Down
Loading

0 comments on commit 00cc980

Please sign in to comment.