Skip to content

Commit

Permalink
Add support for writing LSFv3 nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
Norbyte committed Jul 16, 2021
1 parent f4e449c commit 2f16d38
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 10 deletions.
4 changes: 2 additions & 2 deletions ConverterApp/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.15.7.0")]
[assembly: AssemblyFileVersion("1.15.7.0")]
[assembly: AssemblyVersion("1.15.8.0")]
[assembly: AssemblyFileVersion("1.15.8.0")]
2 changes: 1 addition & 1 deletion LSLib/LS/Common.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public static class Common

public const int MinorVersion = 15;

public const int PatchVersion = 7;
public const int PatchVersion = 8;

/// <summary>
/// Returns the version number of the LSLib library
Expand Down
2 changes: 1 addition & 1 deletion LSLib/LS/Enums/Game.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public static FileVersion LSFVersion(this Game game)
}
else
{
return FileVersion.VerBG3;
return FileVersion.VerExtendedHeader;
}
}
}
Expand Down
61 changes: 57 additions & 4 deletions LSLib/LS/LSFWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,13 @@ public class LSFWriter

private List<List<string>> StringHashMap;
private bool ExtendedNodes = false;
private List<int> NextSiblingIndices;

public LSFWriter(Stream stream, FileVersion version)
{
this.Stream = stream;
this.Version = (uint) version;
this.ExtendedNodes = (this.Version >= (uint)FileVersion.VerExtendedNodes);
}

public void Write(Resource resource)
Expand Down Expand Up @@ -62,12 +64,18 @@ public void Write(Resource resource)
NextNodeIndex = 0;
NextAttributeIndex = 0;
NodeIndices = new Dictionary<Node, int>();
NextSiblingIndices = null;
StringHashMap = new List<List<string>>(StringHashMapSize);
while (StringHashMap.Count < StringHashMapSize)
{
StringHashMap.Add(new List<string>());
}

if (ExtendedNodes)
{
ComputeSiblingIndices(resource);
}

WriteRegions(resource);

byte[] stringBuffer = null;
Expand Down Expand Up @@ -122,6 +130,8 @@ public void Write(Resource resource)
meta.Unknown3 = 0;
meta.Extended = ExtendedNodes ? 1u : 0u;

BinUtils.WriteStruct<Header>(Writer, ref header);

if (header.Version < (ulong)FileVersion.VerExtendedHeader)
{
BinUtils.WriteStruct<Metadata>(Writer, ref meta);
Expand All @@ -141,8 +151,51 @@ public void Write(Resource resource)
}
}

private int ComputeSiblingIndices(Node node)
{
int index = NextNodeIndex;
NextNodeIndex++;
NextSiblingIndices.Add(-1);

int lastSiblingIndex = -1;
foreach (var children in node.Children)
{
foreach (var child in children.Value)
{
int childIndex = ComputeSiblingIndices(child);
if (lastSiblingIndex != -1)
{
NextSiblingIndices[lastSiblingIndex] = childIndex;
}

lastSiblingIndex = childIndex;
}
}

return index;
}

private void ComputeSiblingIndices(Resource resource)
{
NextNodeIndex = 0;
NextSiblingIndices = new List<int>();

int lastRegionIndex = -1;
foreach (var region in resource.Regions)
{
int regionIndex = ComputeSiblingIndices(region.Value);
if (lastRegionIndex != -1)
{
NextSiblingIndices[lastRegionIndex] = regionIndex;
}

lastRegionIndex = regionIndex;
}
}

private void WriteRegions(Resource resource)
{
NextNodeIndex = 0;
foreach (var region in resource.Regions)
{
if (Version >= (ulong) FileVersion.VerExtendedNodes
Expand Down Expand Up @@ -197,7 +250,7 @@ private void WriteNodeAttributesV3(Node node)
{
attributeInfo.NextAttributeIndex = NextAttributeIndex + 1;
}
attributeInfo.Offset = (UInt32)ValueStream.Position;
attributeInfo.Offset = lastOffset;
BinUtils.WriteStruct<AttributeEntryV3>(AttributeWriter, ref attributeInfo);

NextAttributeIndex++;
Expand Down Expand Up @@ -269,6 +322,9 @@ private void WriteNodeV3(Node node)

nodeInfo.NameHashTableIndex = AddStaticString(node.Name);

// Assumes we calculated indices first using ComputeSiblingIndices()
nodeInfo.NextSiblingIndex = NextSiblingIndices[NextNodeIndex];

if (node.Attributes.Count > 0)
{
nodeInfo.FirstAttributeIndex = NextAttributeIndex;
Expand All @@ -279,9 +335,6 @@ private void WriteNodeV3(Node node)
nodeInfo.FirstAttributeIndex = -1;
}

// FIXME!
throw new Exception("Writing uncompressed LSFv3 is not supported yet");
nodeInfo.NextSiblingIndex = -1;
BinUtils.WriteStruct<NodeEntryV3>(NodeWriter, ref nodeInfo);
NodeIndices[node] = NextNodeIndex;
NextNodeIndex++;
Expand Down
4 changes: 2 additions & 2 deletions LSLib/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.15.7.0")]
[assembly: AssemblyFileVersion("1.15.7.0")]
[assembly: AssemblyVersion("1.15.8.0")]
[assembly: AssemblyFileVersion("1.15.8.0")]

0 comments on commit 2f16d38

Please sign in to comment.