Skip to content

Commit

Permalink
cleanup stringid2
Browse files Browse the repository at this point in the history
  • Loading branch information
diogotr7 committed Oct 10, 2024
1 parent 6864391 commit 5ed06ee
Show file tree
Hide file tree
Showing 9 changed files with 63 additions and 55 deletions.
2 changes: 1 addition & 1 deletion src/StarBreaker.Forge/DataForgeEnumDefinition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace StarBreaker.Forge;
[StructLayout(LayoutKind.Sequential, Pack = 1)]
public readonly record struct DataForgeEnumDefinition
{
private readonly DataForgeStringId NameOffset;
private readonly DataForgeStringId2 NameOffset;
public readonly ushort ValueCount;
public readonly ushort FirstValueIndex;

Expand Down
2 changes: 1 addition & 1 deletion src/StarBreaker.Forge/DataForgePropertyDefinition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace StarBreaker.Forge;
[StructLayout(LayoutKind.Sequential, Pack = 1)]
public readonly record struct DataForgePropertyDefinition
{
private readonly DataForgeStringId NameOffset;
private readonly DataForgeStringId2 NameOffset;
public readonly ushort StructIndex;
public readonly DataType DataType;
public readonly ConversionType ConversionType;
Expand Down
2 changes: 1 addition & 1 deletion src/StarBreaker.Forge/DataForgeRecord.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace StarBreaker.Forge;
[StructLayout(LayoutKind.Sequential, Pack = 1)]
public readonly record struct DataForgeRecord
{
private readonly DataForgeStringId NameOffset;
private readonly DataForgeStringId2 NameOffset;
public readonly DataForgeStringId FileNameOffset;
public readonly int StructIndex;
public readonly CigGuid Hash;
Expand Down
15 changes: 11 additions & 4 deletions src/StarBreaker.Forge/DataForgeStringId.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,16 @@ public readonly record struct DataForgeStringId
public string Name => DebugGlobal.Database.GetString(this);
public override string ToString() => Name;
#endif
}

public DataForgeStringId(int id)
{
Id = id;
}
[StructLayout(LayoutKind.Sequential, Pack = 1)]
public readonly record struct DataForgeStringId2
{
// ReSharper disable once PrivateFieldCanBeConvertedToLocalVariable
public readonly int Id;

#if DEBUG
public string Name => DebugGlobal.Database.GetString2(this);
public override string ToString() => Name;
#endif
}
77 changes: 38 additions & 39 deletions src/StarBreaker.Forge/DataForgeStructDefinition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,52 +9,51 @@ namespace StarBreaker.Forge;
public readonly record struct DataForgeStructDefinition
{
private static readonly ConcurrentDictionary<DataForgeStructDefinition, DataForgePropertyDefinition[]> _propertiesCache = new();
private readonly DataForgeStringId NameOffset;
private readonly DataForgeStringId2 NameOffset;
public readonly uint ParentTypeIndex;
public readonly ushort AttributeCount;
public readonly ushort FirstAttributeIndex;
public readonly uint NodeType;

public string GetName(Database db) => db.GetString2(NameOffset);

[MethodImpl(MethodImplOptions.AggressiveInlining)]

public int CalculateSize(ReadOnlySpan<DataForgeStructDefinition> structs, ReadOnlySpan<DataForgePropertyDefinition> properties)
{
var size = 0;

foreach (var attribute in EnumerateProperties(structs, properties))
{
if (attribute.ConversionType == ConversionType.Attribute)
{
size += attribute.DataType switch
{
DataType.Reference => Unsafe.SizeOf<DataForgeReference>(),
DataType.WeakPointer => Unsafe.SizeOf<uint>() * 2,
DataType.StrongPointer => Unsafe.SizeOf<uint>() * 2,
DataType.EnumChoice => Unsafe.SizeOf<DataForgeStringId>(),
DataType.Guid => Unsafe.SizeOf<CigGuid>(),
DataType.Locale => Unsafe.SizeOf<DataForgeStringId>(),
DataType.Double => Unsafe.SizeOf<double>(),
DataType.Single => Unsafe.SizeOf<float>(),
DataType.String => Unsafe.SizeOf<DataForgeStringId>(),
DataType.UInt64 => Unsafe.SizeOf<ulong>(),
DataType.UInt32 => Unsafe.SizeOf<uint>(),
DataType.UInt16 => Unsafe.SizeOf<ushort>(),
DataType.Byte => Unsafe.SizeOf<byte>(),
DataType.Int64 => Unsafe.SizeOf<long>(),
DataType.Int32 => Unsafe.SizeOf<int>(),
DataType.Int16 => Unsafe.SizeOf<short>(),
DataType.SByte => Unsafe.SizeOf<sbyte>(),
DataType.Boolean => Unsafe.SizeOf<byte>(),
DataType.Class => structs[attribute.StructIndex].CalculateSize(structs, properties),
_ => throw new ArgumentOutOfRangeException()
};
}
else
if (attribute.ConversionType != ConversionType.Attribute)
{
//array count + array offset
size += sizeof(int) * 2;
continue;
}

size += attribute.DataType switch
{
DataType.Reference => Unsafe.SizeOf<DataForgeReference>(),
DataType.WeakPointer => Unsafe.SizeOf<uint>() * 2,
DataType.StrongPointer => Unsafe.SizeOf<uint>() * 2,
DataType.EnumChoice => Unsafe.SizeOf<DataForgeStringId>(),
DataType.Guid => Unsafe.SizeOf<CigGuid>(),
DataType.Locale => Unsafe.SizeOf<DataForgeStringId>(),
DataType.Double => Unsafe.SizeOf<double>(),
DataType.Single => Unsafe.SizeOf<float>(),
DataType.String => Unsafe.SizeOf<DataForgeStringId>(),
DataType.UInt64 => Unsafe.SizeOf<ulong>(),
DataType.UInt32 => Unsafe.SizeOf<uint>(),
DataType.UInt16 => Unsafe.SizeOf<ushort>(),
DataType.Byte => Unsafe.SizeOf<byte>(),
DataType.Int64 => Unsafe.SizeOf<long>(),
DataType.Int32 => Unsafe.SizeOf<int>(),
DataType.Int16 => Unsafe.SizeOf<short>(),
DataType.SByte => Unsafe.SizeOf<sbyte>(),
DataType.Boolean => Unsafe.SizeOf<byte>(),
DataType.Class => structs[attribute.StructIndex].CalculateSize(structs, properties),
_ => throw new ArgumentOutOfRangeException()
};
}

return size;
Expand All @@ -63,29 +62,29 @@ public int CalculateSize(ReadOnlySpan<DataForgeStructDefinition> structs, ReadOn
public DataForgePropertyDefinition[] EnumerateProperties(
ReadOnlySpan<DataForgeStructDefinition> structs,
ReadOnlySpan<DataForgePropertyDefinition> properties
)
)
{
if (_propertiesCache.TryGetValue(this, out var cachedProperties))
return cachedProperties;

var _properties = new List<DataForgePropertyDefinition>();
_properties.AddRange(properties.Slice(FirstAttributeIndex, AttributeCount));

var baseStruct = this;
while (baseStruct.ParentTypeIndex != 0xFFFFFFFF)
{
baseStruct = structs[(int)baseStruct.ParentTypeIndex];
_properties.InsertRange(0, properties.Slice(baseStruct.FirstAttributeIndex, baseStruct.AttributeCount));
}

var arr = _properties.ToArray();
_propertiesCache.TryAdd(this, arr);

return arr;
}
#if DEBUG

#if DEBUG
public DataForgePropertyDefinition[] Properties => EnumerateProperties(DebugGlobal.Database.StructDefinitions, DebugGlobal.Database.PropertyDefinitions);
public DataForgeStructDefinition? Parent => ParentTypeIndex == 0xffffffff ? null : DebugGlobal.Database.StructDefinitions[(int)ParentTypeIndex];
#endif
}
#endif
}
2 changes: 1 addition & 1 deletion src/StarBreaker.Forge/Database.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ public Database(string filePath, out int bytesRead)

public SpanReader GetReader(int offset) => new(DataSection, offset - DataSectionOffset);
public string GetString(DataForgeStringId id) => _cachedStrings[id.Id];
public string GetString2(DataForgeStringId id) => _cachedStrings2[id.Id];
public string GetString2(DataForgeStringId2 id) => _cachedStrings2[id.Id];

private static FrozenDictionary<int, string> ReadStringTable(ReadOnlySpan<byte> span)
{
Expand Down
9 changes: 7 additions & 2 deletions src/StarBreaker.Forge/XmlAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@ public override void WriteTo(TextWriter writer)
writer.Write('=');

writer.Write('"');
WriteValue(writer);
writer.Write('"');
}

private void WriteValue(TextWriter writer)
{
//we do this instead of passing the value directly to the writer to avoid boxing
switch (Value)
{
case sbyte sb:
Expand Down Expand Up @@ -84,7 +91,5 @@ public override void WriteTo(TextWriter writer)
default:
throw new NotImplementedException();
}

writer.Write('"');
}
}
7 changes: 2 additions & 5 deletions src/StarBreaker.Forge/XmlNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,14 @@ public sealed class XmlNode
public XmlNode(string name)
{
_name = name;
_children = new List<XmlNode>();
_attributes = new List<XmlAttribute>();
_children = [];
_attributes = [];
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void AppendChild(XmlNode child) => _children.Add(child);

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void AppendAttribute(XmlAttribute xmlAttribute) => _attributes.Add(xmlAttribute);

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void WriteTo(TextWriter writer, int depth)
{
for (var i = 0; i < depth; i++)
Expand Down
2 changes: 1 addition & 1 deletion src/StarBreaker.Tests/DataForgeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public void Setup()
[Test]
public void TestTagDatabase()
{
var forge = new DataForge(@"D:\StarCitizenExport\Data\Game.dcb");
var forge = new DataForge(@"D:\out\Data\Game.dcb");

var stringwriter = new StringWriter();
forge.X(@"libs/foundry/records/tagdatabase/tagdatabase.tagdatabase.xml", stringwriter);
Expand Down

0 comments on commit 5ed06ee

Please sign in to comment.