Skip to content

Commit

Permalink
df v6 working
Browse files Browse the repository at this point in the history
  • Loading branch information
diogotr7 committed Oct 10, 2024
1 parent 591f977 commit 6864391
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 64 deletions.
30 changes: 15 additions & 15 deletions src/StarBreaker.Forge/DataForge.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public void Extract(string outputFolder, string? fileNameFilter = null, IProgres
var reader = _database.GetReader(offset);
var node = new XmlNode(_database.GetString(structDef.NameOffset));
var node = new XmlNode(structDef.GetName(_database));
FillNode(node, structDef, ref reader);
Expand All @@ -80,7 +80,7 @@ public void Extract(string outputFolder, string? fileNameFilter = null, IProgres
var reader = _database.GetReader(offset);
var node = new XmlNode(_database.GetString(structDef.NameOffset));
var node = new XmlNode(structDef.GetName(_database));
FillNode(node, structDef, ref reader);
Expand Down Expand Up @@ -125,7 +125,7 @@ public void ExtractSingle(string outputFolder, string? fileNameFilter = null, IP
var structDef = _database.StructDefinitions[record.StructIndex];
var offset = _offsets[record.StructIndex][record.InstanceIndex];
var reader = _database.GetReader(offset);
var child = new XmlNode(_database.GetString(structDef.NameOffset));
var child = new XmlNode(structDef.GetName(_database));

FillNode(child, structDef, ref reader);

Expand All @@ -150,7 +150,7 @@ private void FillNode(XmlNode node, DataForgeStructDefinition structDef, ref Spa
{
var structDef3 = _database.StructDefinitions[prop.StructIndex];

var childClass = new XmlNode(_database.GetString(prop.NameOffset));
var childClass = new XmlNode(prop.GetName(_database));

FillNode(childClass, structDef3, ref reader);

Expand All @@ -167,15 +167,15 @@ private void FillNode(XmlNode node, DataForgeStructDefinition structDef, ref Spa

var reader2 = _database.GetReader(offset2);

var child = new XmlNode(_database.GetString(prop.NameOffset));
var child = new XmlNode(prop.GetName(_database));

FillNode(child, structDef2, ref reader2);

node.AppendChild(child);
}
else
{
var name1 = _database.GetString(prop.NameOffset);
var name1 = prop.GetName(_database);
switch (prop.DataType)
{
case DataType.Boolean:
Expand Down Expand Up @@ -239,7 +239,7 @@ private void FillNode(XmlNode node, DataForgeStructDefinition structDef, ref Spa
var count = reader.ReadUInt32();
var firstIndex = reader.ReadUInt32();

var arrayNode = new XmlNode(_database.GetString(prop.NameOffset));
var arrayNode = new XmlNode(prop.GetName(_database));
arrayNode.AppendAttribute(new XmlAttribute<uint>("__count__", count));

for (var i = 0; i < count; i++)
Expand All @@ -252,7 +252,7 @@ private void FillNode(XmlNode node, DataForgeStructDefinition structDef, ref Spa
var offset1 = _offsets[prop.StructIndex][index];
var reader1 = _database.GetReader(offset1);

var child = new XmlNode(_database.GetString(structDef1.NameOffset));
var child = new XmlNode(structDef1.GetName(_database));

FillNode(child, structDef1, ref reader1);

Expand All @@ -273,7 +273,7 @@ private void FillNode(XmlNode node, DataForgeStructDefinition structDef, ref Spa
var offset2 = _offsets[(int)reference.StructIndex][(int)reference.InstanceIndex];
var reader2 = _database.GetReader(offset2);

var child = new XmlNode(_database.GetString(prop.NameOffset));
var child = new XmlNode(prop.GetName(_database));

FillNode(child, structDef2, ref reader2);

Expand Down Expand Up @@ -387,7 +387,7 @@ public Dictionary<string, string[]> ExportEnums()
enumValues[i] = _database.GetString(_database.EnumOptions[enumDef.FirstValueIndex + i]);
}

result.Add(_database.GetString(enumDef.NameOffset), enumValues);
result.Add(enumDef.GetName(_database), enumValues);
}

return result;
Expand All @@ -397,14 +397,14 @@ public void WriteTo(TextWriter writer, DataForgeStructDefinition structDef, ref
{
writer.Write('<');

writer.Write(_database.GetString(structDef.NameOffset));
writer.Write(structDef.GetName(_database));

var properties = structDef.EnumerateProperties(_database.StructDefinitions, _database.PropertyDefinitions);
foreach (var property in properties.Where(a => a.IsAttribute))
{
//these properties are attributes
writer.Write(' ');
writer.Write(_database.GetString(property.NameOffset));
writer.Write(property.GetName(_database));
writer.Write('=');
writer.Write('"');
switch (property.DataType)
Expand Down Expand Up @@ -473,7 +473,7 @@ public void WriteTo(TextWriter writer, DataForgeStructDefinition structDef, ref
var firstIndex = reader.ReadUInt32();

writer.Write('<');
writer.Write(_database.GetString(property.NameOffset));
writer.Write(property.GetName(_database));
writer.Write(' ');
writer.Write("__count__");
writer.Write('=');
Expand Down Expand Up @@ -627,7 +627,7 @@ public void X(string recordFileName, TextWriter writer)
var offsetMain = _offsets[mainRecord.StructIndex][mainRecord.InstanceIndex];
var readerMain = _database.GetReader(offsetMain);

var mainNode = new XmlNode(_database.GetString(structDefMain.NameOffset));
var mainNode = new XmlNode(structDefMain.GetName(_database));

FillNode(mainNode, structDefMain, ref readerMain);

Expand All @@ -643,7 +643,7 @@ public void X(string recordFileName, TextWriter writer)
var structDef = _database.StructDefinitions[record.StructIndex];
var offset = _offsets[record.StructIndex][record.InstanceIndex];
var reader = _database.GetReader(offset);
var child = new XmlNode(_database.GetString(structDef.NameOffset));
var child = new XmlNode(structDef.GetName(_database));

FillNode(child, structDef, ref reader);

Expand Down
4 changes: 3 additions & 1 deletion src/StarBreaker.Forge/DataForgeEnumDefinition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ namespace StarBreaker.Forge;
[StructLayout(LayoutKind.Sequential, Pack = 1)]
public readonly record struct DataForgeEnumDefinition
{
public readonly DataForgeStringId NameOffset;
private readonly DataForgeStringId NameOffset;
public readonly ushort ValueCount;
public readonly ushort FirstValueIndex;

public string GetName(Database db) => db.GetString2(NameOffset);
}
4 changes: 3 additions & 1 deletion src/StarBreaker.Forge/DataForgePropertyDefinition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ namespace StarBreaker.Forge;
[StructLayout(LayoutKind.Sequential, Pack = 1)]
public readonly record struct DataForgePropertyDefinition
{
public readonly DataForgeStringId NameOffset;
private readonly DataForgeStringId NameOffset;
public readonly ushort StructIndex;
public readonly DataType DataType;
public readonly ConversionType ConversionType;
private readonly ushort _padding;

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

public bool IsAttribute => ConversionType == ConversionType.Attribute &&
DataType != DataType.Class &&
Expand Down
4 changes: 3 additions & 1 deletion src/StarBreaker.Forge/DataForgeRecord.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ namespace StarBreaker.Forge;
[StructLayout(LayoutKind.Sequential, Pack = 1)]
public readonly record struct DataForgeRecord
{
public readonly DataForgeStringId NameOffset;
private readonly DataForgeStringId NameOffset;
public readonly DataForgeStringId FileNameOffset;
public readonly int StructIndex;
public readonly CigGuid Hash;
public readonly ushort InstanceIndex;
public readonly ushort OtherIndex;

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

#if DEBUG
public DataForgeStructDefinition Struct => DebugGlobal.Database.StructDefinitions[(int)StructIndex];
#endif
Expand Down
17 changes: 0 additions & 17 deletions src/StarBreaker.Forge/DataForgeStringId.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,4 @@ 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

public DataForgeStringId2(int id)
{
Id = id;
}
}
4 changes: 3 additions & 1 deletion src/StarBreaker.Forge/DataForgeStructDefinition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ namespace StarBreaker.Forge;
public readonly record struct DataForgeStructDefinition
{
private static readonly ConcurrentDictionary<DataForgeStructDefinition, DataForgePropertyDefinition[]> _propertiesCache = new();
public readonly DataForgeStringId NameOffset;
private readonly DataForgeStringId 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)
Expand Down
48 changes: 20 additions & 28 deletions src/StarBreaker.Forge/Database.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,33 +112,8 @@ public Database(string filePath, out int bytesRead)
ReferenceValues = reader.ReadArray<DataForgeReference>(referenceValueCount);
EnumOptions = reader.ReadArray<DataForgeStringId>(enumOptionCount);

var stringSpan = reader.ReadBytes((int)textLength).AsSpan();
var stringSpan2 = reader.ReadBytes((int)textLength2).AsSpan();

var strings = new Dictionary<int, string>();
var offset = 0;
while (offset < stringSpan.Length)
{
var length = stringSpan[offset..].IndexOf((byte)0);
var useful = stringSpan[offset..(offset + length)];
var str = Encoding.ASCII.GetString(useful);
strings[offset] = str;
offset += length + 1;
}

var strings2 = new Dictionary<int, string>();
var offset2 = 0;
while (offset2 < stringSpan2.Length)
{
var length = stringSpan2[offset2..].IndexOf((byte)0);
var useful = stringSpan2[offset2..(offset2 + length)];
var str = Encoding.ASCII.GetString(useful);
strings2[offset2] = str;
offset2 += length + 1;
}

_cachedStrings = strings.ToFrozenDictionary();
_cachedStrings2 = strings2.ToFrozenDictionary();
_cachedStrings = ReadStringTable(reader.ReadBytes((int)textLength).AsSpan());
_cachedStrings2 = ReadStringTable(reader.ReadBytes((int)textLength2).AsSpan());

bytesRead = (int)fs.Position;

Expand All @@ -154,5 +129,22 @@ 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(DataForgeStringId2 id) => _cachedStrings2[id.Id];
public string GetString2(DataForgeStringId id) => _cachedStrings2[id.Id];

private static FrozenDictionary<int, string> ReadStringTable(ReadOnlySpan<byte> span)
{
var strings = new Dictionary<int, string>();
var offset = 0;

while (offset < span.Length)
{
var length = span[offset..].IndexOf((byte)0);
var useful = span[offset..(offset + length)];
var str = Encoding.ASCII.GetString(useful);
strings[offset] = str;
offset += length + 1;
}

return strings.ToFrozenDictionary();
}
}

0 comments on commit 6864391

Please sign in to comment.