Skip to content

Commit

Permalink
Null instance handling in deserialization
Browse files Browse the repository at this point in the history
  • Loading branch information
Jacky720 committed Apr 16, 2022
1 parent 14d3295 commit fc6402a
Showing 1 changed file with 27 additions and 45 deletions.
72 changes: 27 additions & 45 deletions UndertaleModLib/Models/UndertaleRoom.cs
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,19 @@ public void Unserialize(UndertaleReader reader)
layer.InstancesData.Instances.Clear();
foreach (var id in layer.InstancesData._InstanceIds)
{
layer.InstancesData.Instances.Add(GameObjects.ByInstanceID(id));
if (GameObjects.ByInstanceID(id) != null)
layer.InstancesData.Instances.Add(GameObjects.ByInstanceID(id));
else
{
/* Attempt to resolve null objects.
* Sometimes, the instance ID in GameObjects will end up a duplicate
* of a previous ID, rather than the correct one.
* So, we traverse the object list a little to find the correct one.
* If you can get two broken objects in a row... it'll probably crash.
*/
int foundIndex = GameObjects.IndexOf(GameObjects.ByInstanceID(id - 1));
layer.InstancesData.Instances.Add(GameObjects[foundIndex + 1]);
}
}
}
}
Expand Down Expand Up @@ -1002,29 +1014,14 @@ public void Serialize(UndertaleWriter writer)
writer.WriteUndertaleObject(EffectProperties);
}

if (LayerType == LayerType.Instances)
{
writer.WriteUndertaleObject(InstancesData);
}
else if (LayerType == LayerType.Tiles)
{
writer.WriteUndertaleObject(TilesData);
}
else if (LayerType == LayerType.Background)
{
writer.WriteUndertaleObject(BackgroundData);
}
else if (LayerType == LayerType.Assets)
{
writer.WriteUndertaleObject(AssetsData);
}
else if (LayerType == LayerType.Effect)
{
writer.WriteUndertaleObject(EffectData);
}
else
switch (LayerType)
{
throw new Exception("Unsupported layer type " + LayerType);
case LayerType.Instances: writer.WriteUndertaleObject(InstancesData); break;
case LayerType.Tiles: writer.WriteUndertaleObject(TilesData); break;
case LayerType.Background: writer.WriteUndertaleObject(BackgroundData); break;
case LayerType.Assets: writer.WriteUndertaleObject(AssetsData); break;
case LayerType.Effect: writer.WriteUndertaleObject(EffectData); break;
default: throw new Exception("Unsupported layer type " + LayerType);
}
}

Expand All @@ -1047,29 +1044,14 @@ public void Unserialize(UndertaleReader reader)
EffectProperties = reader.ReadUndertaleObject<UndertaleSimpleList<EffectProperty>>();
}

if (LayerType == LayerType.Instances)
{
Data = reader.ReadUndertaleObject<LayerInstancesData>();
}
else if (LayerType == LayerType.Tiles)
{
Data = reader.ReadUndertaleObject<LayerTilesData>();
}
else if (LayerType == LayerType.Background)
{
Data = reader.ReadUndertaleObject<LayerBackgroundData>();
}
else if (LayerType == LayerType.Assets)
{
Data = reader.ReadUndertaleObject<LayerAssetsData>();
}
else if (LayerType == LayerType.Effect)
{
Data = reader.ReadUndertaleObject<LayerEffectData>();
}
else
switch (LayerType)
{
throw new Exception("Unsupported layer type " + LayerType);
case LayerType.Instances: Data = reader.ReadUndertaleObject<LayerInstancesData>(); break;
case LayerType.Tiles: Data = reader.ReadUndertaleObject<LayerTilesData>(); break;
case LayerType.Background: Data = reader.ReadUndertaleObject<LayerBackgroundData>(); break;
case LayerType.Assets: Data = reader.ReadUndertaleObject<LayerAssetsData>(); break;
case LayerType.Effect: Data = reader.ReadUndertaleObject<LayerEffectData>(); break;
default: throw new Exception("Unsupported layer type " + LayerType);
}
}

Expand Down

0 comments on commit fc6402a

Please sign in to comment.