Skip to content

Commit

Permalink
FIxed array nullpointers
Browse files Browse the repository at this point in the history
  • Loading branch information
Justin113D committed Jan 26, 2024
1 parent 7fee64a commit 7bcf6b7
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 38 deletions.
21 changes: 11 additions & 10 deletions src/SA3D.SA2Event/Model/BigTheCatEntry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,19 +50,21 @@ public BigTheCatEntry(Node? model, int unknown)
/// <returns>Pointer to the written array.</returns>
public uint WriteMotionArray(EndianStackWriter writer, Dictionary<EventMotion, uint> motionLUT, PointerLUT lut)
{
if(Motions.Count == 0)
{
return 0;
}

uint onWrite()
{
uint result = writer.PointerPosition;

foreach((Motion? nodeAnim, Motion? shapeAnim) in Motions)
if(Motions.Count == 0)
{
writer.WriteUInt(motionLUT.GetMotionKey(nodeAnim));
writer.WriteUInt(motionLUT.GetMotionKey(shapeAnim));
writer.WriteEmpty(4);
}
else
{
foreach((Motion? nodeAnim, Motion? shapeAnim) in Motions)
{
writer.WriteUInt(motionLUT.GetMotionKey(nodeAnim));
writer.WriteUInt(motionLUT.GetMotionKey(shapeAnim));
}
}

return result;
Expand All @@ -84,9 +86,8 @@ uint onWrite()
public void Write(EndianStackWriter writer, PointerLUT lut)
{
uint modelAddr = 0;
uint motionAddr = 0;
if((Model != null && !lut.Nodes.TryGetAddress(Model, out modelAddr))
|| (Motions.Count > 0 && !lut.All.TryGetAddress(Motions, out motionAddr)))
|| !lut.All.TryGetAddress(Motions, out uint motionAddr))
{
throw new InvalidOperationException("Big the Cat content has not been written yet");
}
Expand Down
21 changes: 14 additions & 7 deletions src/SA3D.SA2Event/Model/ModelData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -555,15 +555,22 @@ private void WriteEntries(EndianStackWriter writer, Dictionary<EventMotion, uint
{
uint result = writer.PointerPosition;

foreach(EventEntry entity in array)
if(scene.Entries.Count == 0)
{
if(Type == EventType.gc)
{
entity.WriteGC(writer, motionLUT, lut);
}
else
writer.WriteEmpty(4);
}
else
{
foreach(EventEntry entity in array)
{
entity.WriteDC(writer, lut);
if(Type == EventType.gc)
{
entity.WriteGC(writer, motionLUT, lut);
}
else
{
entity.WriteDC(writer, lut);
}
}
}

Expand Down
42 changes: 21 additions & 21 deletions src/SA3D.SA2Event/Model/Scene.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,18 +65,19 @@ public Scene(int frameCount)
/// <returns>Pointer to where the array was written.</returns>
public uint WriteCameraMotionArray(EndianStackWriter writer, Dictionary<EventMotion, uint> motionLUT, PointerLUT lut)
{
if(CameraAnimations.Count == 0)
{
return 0;
}

uint onWrite()
{
uint result = writer.PointerPosition;

foreach(EventMotion motion in CameraAnimations)
if(CameraAnimations.Count == 0)
{
writer.WriteEmpty(4);
}
else
{
writer.WriteUInt(motionLUT.GetMotionKey(motion));
foreach(EventMotion motion in CameraAnimations)
{
writer.WriteUInt(motionLUT.GetMotionKey(motion));
}
}

return result;
Expand All @@ -94,18 +95,20 @@ uint onWrite()
/// <returns>Pointer to where the array was written.</returns>
public uint WriteParticleMotionArray(EndianStackWriter writer, Dictionary<EventMotion, uint> motionLUT, PointerLUT lut)
{
if(ParticleMotions.Count == 0)
{
return 0;
}

uint onWrite()
{
uint result = writer.PointerPosition;

foreach(Motion? motion in ParticleMotions)
if(ParticleMotions.Count == 0)
{
writer.WriteEmpty(4);
}
else
{
writer.WriteUInt(motionLUT.GetMotionKey(motion));
foreach(Motion? motion in ParticleMotions)
{
writer.WriteUInt(motionLUT.GetMotionKey(motion));
}
}

return result;
Expand Down Expand Up @@ -148,14 +151,11 @@ public static void WriteMotionArrays(IEnumerable<Scene> scenes, EndianStackWrite
/// <exception cref="InvalidOperationException"></exception>
public void Write(EndianStackWriter writer, PointerLUT lut)
{
uint entityAddr = 0;
uint camAnimAddr = 0;
uint particleAddr = 0;
uint bigAddr = 0;

if((Entries.Count > 0 && !lut.All.TryGetAddress(Entries, out entityAddr))
|| (CameraAnimations.Count > 0 && !lut.All.TryGetAddress(CameraAnimations, out camAnimAddr))
|| (ParticleMotions.Count > 0 && !lut.All.TryGetAddress(ParticleMotions, out particleAddr))
if((!lut.All.TryGetAddress(Entries, out uint entityAddr))
|| !lut.All.TryGetAddress(CameraAnimations, out uint camAnimAddr)
|| !lut.All.TryGetAddress(ParticleMotions, out uint particleAddr)
|| (BigTheCat != null && !lut.All.TryGetAddress(BigTheCat, out bigAddr)))
{
throw new InvalidOperationException("Scene Content has not yet been written!");
Expand Down

0 comments on commit 7bcf6b7

Please sign in to comment.