Skip to content

Commit

Permalink
Sample chunk resource: data reading for models fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
Justin113D authored and Sajidur78 committed Jan 22, 2025
1 parent c0650d0 commit 9e18721
Showing 1 changed file with 35 additions and 11 deletions.
46 changes: 35 additions & 11 deletions Source/SharpNeedle/HedgehogEngine/Mirage/SampleChunkResource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,11 @@ public override void Read(IFile file)
Name = Path.GetFileNameWithoutExtension(file.Name);

using BinaryObjectReader reader = new(file.Open(), this is IStreamable ? StreamOwnership.Retain : StreamOwnership.Transfer, Endianness.Big);
ReadResource(reader);
}

public void ReadResource(BinaryObjectReader reader)
{
SampleChunkNode.Flags v2Flags = reader.Read<SampleChunkNode.Flags>();
reader.Seek(-4, SeekOrigin.Current);

Expand Down Expand Up @@ -87,25 +91,34 @@ private void ReadResourceV2(BinaryObjectReader reader)
long rootStart = reader.Position;
Root = reader.ReadObject<SampleChunkNode>();

SampleChunkNode contexts = Root.FindNode("Contexts");
if(contexts == null)
SampleChunkNode contexts = Root.FindNode("Contexts")
?? throw new InvalidDataException("No Contexts node!");

DataVersion = contexts.Value;

SampleChunkNode dataNode = contexts;
while(dataNode != null && dataNode.DataSize == 0)
{
return;
dataNode = dataNode.Parent;
}

if(dataNode == null || dataNode == Root)
{
throw new InvalidDataException("No data found!");
}

using(SeekToken token = reader.At())
{
// Using a substream to ensure we can't read outside the nodes data
SubStream dataStream = new(reader.GetBaseStream(), contexts.DataOffset, contexts.DataSize);
SubStream dataStream = new(reader.GetBaseStream(), dataNode.DataOffset, dataNode.DataSize);
BinaryObjectReader dataReader = new(dataStream, StreamOwnership.Retain, reader.Endianness);

dataReader.OffsetHandler.PushOffsetOrigin(rootStart + 0x10 - contexts.DataOffset);
dataReader.OffsetHandler.PushOffsetOrigin(rootStart + 0x10 - dataNode.DataOffset);
Read(dataReader);
dataReader.PopOffsetOrigin();
}

contexts.Data = this;
DataVersion = contexts.Value;
dataNode.Data = this;
}


Expand All @@ -117,10 +130,19 @@ public override void Write(IFile file)
public void Write(IFile file, bool writeNodes)
{
using BinaryObjectWriter writer = new(file.Open(FileAccess.Write), StreamOwnership.Transfer, Endianness.Big);
WriteResource(writer, writeNodes, file.Name);
}

public void WriteResource(BinaryObjectWriter writer, string filename)
{
WriteResource(writer, Root != null, filename);
}

public void WriteResource(BinaryObjectWriter writer, bool writeNodes, string filename)
{
if(!writeNodes)
{
WriteResourceV1(file, writer);
WriteResourceV1(filename, writer);
}
else
{
Expand All @@ -133,7 +155,7 @@ public void Write(IFile file, bool writeNodes)
}
}

private void WriteResourceV1(IFile file, BinaryObjectWriter writer)
private void WriteResourceV1(string filename, BinaryObjectWriter writer)
{
SeekToken beginToken = writer.At();

Expand Down Expand Up @@ -183,7 +205,7 @@ private void WriteResourceV1(IFile file, BinaryObjectWriter writer)

writer.WriteOffset(() =>
{
writer.WriteString(StringBinaryFormat.NullTerminated, file.Name);
writer.WriteString(StringBinaryFormat.NullTerminated, filename);
writer.Align(4);
}, 4);

Expand All @@ -198,9 +220,11 @@ private void WriteResourceV1(IFile file, BinaryObjectWriter writer)

private void WriteResourceV2(BinaryObjectWriter writer)
{
SampleChunkNode contexts = Root.FindNode("Contexts");
SampleChunkNode contexts = Root?.FindNode("Contexts");
if(contexts != null)
{
contexts.Value = DataVersion;
}

writer.WriteObject(Root);
}
Expand Down

0 comments on commit 9e18721

Please sign in to comment.