Skip to content

Commit

Permalink
Game data loads by copying every chunk to the buffer.
Browse files Browse the repository at this point in the history
  • Loading branch information
VladiStep committed Feb 26, 2023
1 parent 2c55f33 commit 6cd0be0
Show file tree
Hide file tree
Showing 16 changed files with 478 additions and 174 deletions.
18 changes: 9 additions & 9 deletions UndertaleModLib/Models/UndertaleCode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1114,11 +1114,11 @@ public void Unserialize(UndertaleReader reader)
Instructions.Clear();
Instructions.Capacity = reader.InstructionArraysLengths[CurrCodeIndex];

uint here = reader.Position;
uint here = reader.AbsPosition;
uint stop = here + Length;
while (reader.Position < stop)
while (reader.AbsPosition < stop)
{
uint a = (reader.Position - here) / 4;
uint a = (reader.AbsPosition - here) / 4;
UndertaleInstruction instr = reader.ReadUndertaleObject<UndertaleInstruction>();
instr.Address = a;
Instructions.Add(instr);
Expand All @@ -1135,7 +1135,7 @@ public void Unserialize(UndertaleReader reader)
WeirdLocalFlag = true;
}
int BytecodeRelativeAddress = reader.ReadInt32();
_bytecodeAbsoluteAddress = (uint)((int)reader.Position - 4 + BytecodeRelativeAddress);
_bytecodeAbsoluteAddress = (uint)((int)reader.AbsPosition - 4 + BytecodeRelativeAddress);

if (Length > 0 && reader.GMS2_3 && reader.GetOffsetMap().TryGetValue(_bytecodeAbsoluteAddress, out var i))
{
Expand All @@ -1146,22 +1146,22 @@ public void Unserialize(UndertaleReader reader)
return;
}

uint here = reader.Position;
reader.Position = _bytecodeAbsoluteAddress;
uint here = reader.AbsPosition;
reader.AbsPosition = _bytecodeAbsoluteAddress;

Instructions.Clear();
Instructions.Capacity = reader.InstructionArraysLengths[CurrCodeIndex];
while (reader.Position < _bytecodeAbsoluteAddress + Length)
while (reader.AbsPosition < _bytecodeAbsoluteAddress + Length)
{
uint a = (reader.Position - _bytecodeAbsoluteAddress) / 4;
uint a = (reader.AbsPosition - _bytecodeAbsoluteAddress) / 4;
UndertaleInstruction instr = reader.ReadUndertaleObject<UndertaleInstruction>();
instr.Address = a;
Instructions.Add(instr);
}
if (ParentEntry == null && Instructions.Count != 0)
Instructions[0].Entry = this;

reader.Position = here;
reader.AbsPosition = here;
Offset = reader.ReadUInt32();
}

Expand Down
2 changes: 1 addition & 1 deletion UndertaleModLib/Models/UndertaleEmbeddedAudio.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public void Unserialize(UndertaleReader reader)
/// <inheritdoc />
public void UnserializePadding(UndertaleReader reader)
{
while (reader.Position % 4 != 0)
while (reader.AbsPosition % 4 != 0)
if (reader.ReadByte() != 0)
throw new IOException("Padding error!");
}
Expand Down
4 changes: 2 additions & 2 deletions UndertaleModLib/Models/UndertaleEmbeddedTexture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ public void UnserializeBlob(UndertaleReader reader)
if (_textureData == null || TextureExternal)
return;

while (reader.Position % 0x80 != 0)
while (reader.AbsPosition % 0x80 != 0)
if (reader.ReadByte() != 0)
throw new IOException("Padding error!");

Expand Down Expand Up @@ -414,7 +414,7 @@ public void Unserialize(UndertaleReader reader)
/// <summary>
/// Unserializes the texture from any type of reader (can be from any source).
/// </summary>
public void Unserialize(FileBinaryReader reader, bool is_2022_5)
public void Unserialize(IBinaryReader reader, bool is_2022_5)
{
sharedStream ??= new();

Expand Down
4 changes: 2 additions & 2 deletions UndertaleModLib/Models/UndertaleExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -382,9 +382,9 @@ public static uint UnserializeChildObjectCount(UndertaleReader reader)
uint filesPtr = reader.ReadUInt32();
uint optionsPtr = reader.ReadUInt32();

reader.Position = filesPtr;
reader.AbsPosition = filesPtr;
count += 1 + UndertalePointerList<UndertaleExtensionFile>.UnserializeChildObjectCount(reader);
reader.Position = optionsPtr;
reader.AbsPosition = optionsPtr;
count += 1 + UndertalePointerList<UndertaleExtensionOption>.UnserializeChildObjectCount(reader);
}
else
Expand Down
22 changes: 11 additions & 11 deletions UndertaleModLib/Models/UndertaleRoom.cs
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ private static void CheckForGMS2_2_2_302(UndertaleReader reader)
uint gameObjPtr = reader.ReadUInt32();
uint tilePtr = reader.ReadUInt32();

reader.Position = gameObjPtr; // "GameObjects"
reader.AbsPosition = gameObjPtr; // "GameObjects"
uint objCount = reader.ReadUInt32();
if (objCount > 0)
{
Expand Down Expand Up @@ -462,23 +462,23 @@ public static uint UnserializeChildObjectCount(UndertaleReader reader)
sequencesPtr = reader.ReadUInt32();
}

reader.Position = backgroundPtr;
reader.AbsPosition = backgroundPtr;
count += 1 + UndertalePointerList<Background>.UnserializeChildObjectCount(reader);
reader.Position = viewsPtr;
reader.AbsPosition = viewsPtr;
count += 1 + UndertalePointerList<View>.UnserializeChildObjectCount(reader);
reader.Position = gameObjsPtr;
reader.AbsPosition = gameObjsPtr;
count += 1 + UndertalePointerList<GameObject>.UnserializeChildObjectCount(reader);
reader.Position = tilesPtr;
reader.AbsPosition = tilesPtr;
count += 1 + UndertalePointerList<Tile>.UnserializeChildObjectCount(reader);

if (reader.GMS2)
{
reader.Position = layersPtr;
reader.AbsPosition = layersPtr;
count += 1 + UndertalePointerList<Layer>.UnserializeChildObjectCount(reader);

if (reader.GMS2_3)
{
reader.Position = sequencesPtr;
reader.AbsPosition = sequencesPtr;
count += 1 + UndertaleSimpleList<UndertaleResourceById<UndertaleSequence, UndertaleChunkSEQN>>
.UnserializeChildObjectCount(reader);
}
Expand Down Expand Up @@ -1858,17 +1858,17 @@ public static uint UnserializeChildObjectCount(UndertaleReader reader)
nineSlicesPtr = reader.ReadUInt32();
}

reader.Position = legacyTilesPtr;
reader.AbsPosition = legacyTilesPtr;
count += 1 + UndertalePointerList<Tile>.UnserializeChildObjectCount(reader);
reader.Position = spritesPtr;
reader.AbsPosition = spritesPtr;
count += 1 + UndertalePointerList<SpriteInstance>.UnserializeChildObjectCount(reader);
if (reader.GMS2_3)
{
reader.Position = sequencesPtr;
reader.AbsPosition = sequencesPtr;
count += 1 + UndertalePointerList<SequenceInstance>.UnserializeChildObjectCount(reader);
if (!reader.undertaleData.GMS2_3_2)
{
reader.Position = nineSlicesPtr;
reader.AbsPosition = nineSlicesPtr;
count += 1 + UndertalePointerList<SpriteInstance>.UnserializeChildObjectCount(reader);
}
}
Expand Down
16 changes: 8 additions & 8 deletions UndertaleModLib/Models/UndertaleSequence.cs
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ UndertaleString ForceReadString()
return res;

uint returnTo = reader.Position;
reader.Position = reader.GetOffsetMapRev()[res];
reader.AbsPosition = reader.GetOffsetMapRev()[res];
reader.ReadUndertaleObject<UndertaleString>();
reader.Position = returnTo;
return res;
Expand Down Expand Up @@ -415,7 +415,7 @@ string ForceReadString()
uint strPtr = reader.ReadUInt32();

uint returnTo = reader.Position;
reader.Position = strPtr - 4;
reader.AbsPosition = strPtr - 4;
string res = reader.ReadGMString();
reader.Position = returnTo;
return res;
Expand Down Expand Up @@ -500,7 +500,7 @@ public virtual void Serialize(UndertaleWriter writer)
/// <inheritdoc />
public virtual void Unserialize(UndertaleReader reader)
{
while (reader.Position % 4 != 0)
while (reader.AbsPosition % 4 != 0)
if (reader.ReadByte() != 0)
throw new IOException("Padding error!");

Expand All @@ -511,7 +511,7 @@ public virtual void Unserialize(UndertaleReader reader)
/// <inheritdoc cref="UndertaleObject.UnserializeChildObjectCount(UndertaleReader)"/>
public static uint UnserializeChildObjectCount(UndertaleReader reader)
{
while (reader.Position % 4 != 0)
while (reader.AbsPosition % 4 != 0)
reader.Position++;

return UndertaleSimpleList<Keyframe<T>>.UnserializeChildObjectCount(reader);
Expand Down Expand Up @@ -756,7 +756,7 @@ public override void Serialize(UndertaleWriter writer)
/// <inheritdoc />
public override void Unserialize(UndertaleReader reader)
{
while (reader.Position % 4 != 0)
while (reader.AbsPosition % 4 != 0)
if (reader.ReadByte() != 0)
throw new IOException("Padding error!");

Expand All @@ -769,7 +769,7 @@ public override void Unserialize(UndertaleReader reader)
/// <inheritdoc cref="UndertaleObject.UnserializeChildObjectCount(UndertaleReader)"/>
public static new uint UnserializeChildObjectCount(UndertaleReader reader)
{
while (reader.Position % 4 != 0)
while (reader.AbsPosition % 4 != 0)
reader.Position++;

reader.Position += 4; // "Interpolation"
Expand Down Expand Up @@ -823,7 +823,7 @@ public override void Serialize(UndertaleWriter writer)
/// <inheritdoc />
public override void Unserialize(UndertaleReader reader)
{
while (reader.Position % 4 != 0)
while (reader.AbsPosition % 4 != 0)
if (reader.ReadByte() != 0)
throw new IOException("Padding error!");

Expand All @@ -836,7 +836,7 @@ public override void Unserialize(UndertaleReader reader)
/// <inheritdoc cref="UndertaleObject.UnserializeChildObjectCount(UndertaleReader)"/>
public static new uint UnserializeChildObjectCount(UndertaleReader reader)
{
while (reader.Position % 4 != 0)
while (reader.AbsPosition % 4 != 0)
reader.Position++;

reader.Position += 4; // "Interpolation"
Expand Down
20 changes: 10 additions & 10 deletions UndertaleModLib/Models/UndertaleShader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ private static void WritePadding(UndertaleWriter writer, int amount)

private static void ReadPadding(UndertaleReader reader, int amount)
{
while ((reader.Position & amount) != 0)
while ((reader.AbsPosition & amount) != 0)
{
if (reader.ReadByte() != 0)
throw new UndertaleSerializationException("Failed to read shader padding: should be some zero bytes");
Expand Down Expand Up @@ -335,7 +335,7 @@ public void Unserialize(UndertaleReader reader)
next = HLSL11_PixelData._Position;
else
next = EntryEnd;
int length = (int)(next - reader.Position);
int length = (int)(next - reader.AbsPosition);
HLSL11_VertexData.ReadData(reader, length);
}
if (!HLSL11_PixelData.IsNull)
Expand All @@ -348,7 +348,7 @@ public void Unserialize(UndertaleReader reader)
next = PSSL_VertexData._Position;
else
next = EntryEnd;
int length = (int)(next - reader.Position);
int length = (int)(next - reader.AbsPosition);
HLSL11_PixelData.ReadData(reader, length);
}

Expand All @@ -362,7 +362,7 @@ public void Unserialize(UndertaleReader reader)
next = PSSL_PixelData._Position;
else
next = EntryEnd;
int length = (int)(next - reader.Position);
int length = (int)(next - reader.AbsPosition);
PSSL_VertexData.ReadData(reader, length);
}
if (!PSSL_PixelData.IsNull)
Expand All @@ -375,7 +375,7 @@ public void Unserialize(UndertaleReader reader)
next = Cg_PSVita_VertexData._Position;
else
next = EntryEnd;
int length = (int)(next - reader.Position);
int length = (int)(next - reader.AbsPosition);
PSSL_PixelData.ReadData(reader, length);
}

Expand All @@ -389,7 +389,7 @@ public void Unserialize(UndertaleReader reader)
next = Cg_PSVita_PixelData._Position;
else
next = EntryEnd;
int length = (int)(next - reader.Position);
int length = (int)(next - reader.AbsPosition);
Cg_PSVita_VertexData.ReadData(reader, length);
}
if (!Cg_PSVita_PixelData.IsNull)
Expand All @@ -402,7 +402,7 @@ public void Unserialize(UndertaleReader reader)
next = Cg_PS3_VertexData._Position;
else
next = EntryEnd;
int length = (int)(next - reader.Position);
int length = (int)(next - reader.AbsPosition);
Cg_PSVita_PixelData.ReadData(reader, length);
}

Expand All @@ -418,7 +418,7 @@ public void Unserialize(UndertaleReader reader)
next = Cg_PS3_PixelData._Position;
else
next = EntryEnd;
int length = (int)(next - reader.Position);
int length = (int)(next - reader.AbsPosition);
Cg_PS3_VertexData.ReadData(reader, length);
}
if (!Cg_PS3_PixelData.IsNull)
Expand All @@ -427,7 +427,7 @@ public void Unserialize(UndertaleReader reader)

// Calculate length of data
uint next = EntryEnd; // final possible data, nothing else to check for
int length = (int)(next - reader.Position);
int length = (int)(next - reader.AbsPosition);
Cg_PS3_PixelData.ReadData(reader, length);
}
}
Expand Down Expand Up @@ -513,7 +513,7 @@ public void Serialize(UndertaleWriter writer, bool writeLength = true)

public void Unserialize(UndertaleReader reader, bool readLength = true)
{
_PointerLocation = reader.Position;
_PointerLocation = reader.AbsPosition;
_Position = reader.ReadUInt32();
if (readLength)
_Length = reader.ReadUInt32();
Expand Down
10 changes: 5 additions & 5 deletions UndertaleModLib/Models/UndertaleTextureGroupInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -187,23 +187,23 @@ public static uint UnserializeChildObjectCount(UndertaleReader reader)
uint fontsPtr = reader.ReadUInt32();
uint tilesetsPtr = reader.ReadUInt32();

reader.Position = texPagesPtr;
reader.AbsPosition = texPagesPtr;
count += 1 + UndertaleSimpleResourcesList<UndertaleEmbeddedTexture, UndertaleChunkTXTR>
.UnserializeChildObjectCount(reader);

reader.Position = spritesPtr;
reader.AbsPosition = spritesPtr;
count += 1 + UndertaleSimpleResourcesList<UndertaleSprite, UndertaleChunkSPRT>
.UnserializeChildObjectCount(reader);

reader.Position = spineSpritesPtr;
reader.AbsPosition = spineSpritesPtr;
count += 1 + UndertaleSimpleResourcesList<UndertaleSprite, UndertaleChunkSPRT>
.UnserializeChildObjectCount(reader);

reader.Position = fontsPtr;
reader.AbsPosition = fontsPtr;
count += 1 + UndertaleSimpleResourcesList<UndertaleFont, UndertaleChunkFONT>
.UnserializeChildObjectCount(reader);

reader.Position = tilesetsPtr;
reader.AbsPosition = tilesetsPtr;
count += 1 + UndertaleSimpleResourcesList<UndertaleBackground, UndertaleChunkBGND>
.UnserializeChildObjectCount(reader);

Expand Down
2 changes: 1 addition & 1 deletion UndertaleModLib/Models/UndertaleTimeline.cs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ public void Unserialize(UndertaleReader reader)
// Read the actions for each moment
for (int i = 0; i < momentCount; i++)
{
if (reader.Position != unnecessaryPointers[i])
if (reader.AbsPosition != unnecessaryPointers[i])
throw new UndertaleSerializationException("Invalid action list pointer");

// Read action list and assign time point (put into list)
Expand Down
Loading

0 comments on commit 6cd0be0

Please sign in to comment.