From 2441b49c197cb2ffdfde0265e530e63eac767aae Mon Sep 17 00:00:00 2001
From: colinator27 <17358554+colinator27@users.noreply.github.com>
Date: Sat, 13 Jul 2024 13:01:16 -0400
Subject: [PATCH] Fix external textures being counted in object pool
---
.../Models/UndertaleEmbeddedTexture.cs | 32 ++++++++++++++-----
UndertaleModLib/UndertaleChunks.cs | 11 -------
2 files changed, 24 insertions(+), 19 deletions(-)
diff --git a/UndertaleModLib/Models/UndertaleEmbeddedTexture.cs b/UndertaleModLib/Models/UndertaleEmbeddedTexture.cs
index 61b3599d3..d5949e566 100644
--- a/UndertaleModLib/Models/UndertaleEmbeddedTexture.cs
+++ b/UndertaleModLib/Models/UndertaleEmbeddedTexture.cs
@@ -15,15 +15,8 @@ namespace UndertaleModLib.Models;
/// An embedded texture entry in the data file.
///
[PropertyChanged.AddINotifyPropertyChangedInterface]
-public class UndertaleEmbeddedTexture : UndertaleNamedResource, IDisposable,
- IStaticChildObjCount, IStaticChildObjectsSize
+public class UndertaleEmbeddedTexture : UndertaleNamedResource, IDisposable
{
- ///
- public static readonly uint ChildObjectCount = 1;
-
- ///
- public static readonly uint ChildObjectsSize = 4; // minimal size
-
///
/// The name of the embedded texture entry.
///
@@ -196,6 +189,29 @@ public void UnserializeBlob(UndertaleReader reader)
TextureLoaded = true;
}
+ ///
+ public static uint UnserializeChildObjectCount(UndertaleReader reader)
+ {
+ uint count = 0;
+
+ reader.Position += 4; // "Scaled"
+ if (reader.undertaleData.IsVersionAtLeast(2, 0, 6))
+ reader.Position += 4; // "GeneratedMips"
+ if (reader.undertaleData.IsVersionAtLeast(2022, 3))
+ reader.Position += 4; // "_textureBlockSize"
+ if (reader.undertaleData.IsVersionAtLeast(2022, 9))
+ reader.Position += 12; // "TextureWidth", "TextureHeight", "IndexInGroup"
+
+ if (reader.ReadUInt32() != 0)
+ {
+ // If the texture data pointer isn't null, then this is an internal texture,
+ // which will create another object in the pool when reading the blob.
+ count++;
+ }
+
+ return count;
+ }
+
///
/// Assigns texture group info to every embedded texture in the supplied data file.
///
diff --git a/UndertaleModLib/UndertaleChunks.cs b/UndertaleModLib/UndertaleChunks.cs
index 9c3610227..be4ff02e0 100644
--- a/UndertaleModLib/UndertaleChunks.cs
+++ b/UndertaleModLib/UndertaleChunks.cs
@@ -1431,17 +1431,6 @@ internal override uint UnserializeObjectCount(UndertaleReader reader)
CheckFor2022_3And5(reader);
- uint txtrSize = UndertaleEmbeddedTexture.ChildObjectsSize;
- if (reader.undertaleData.IsVersionAtLeast(2, 0, 6))
- txtrSize += 4; // "GeneratedMips"
- if (reader.undertaleData.IsVersionAtLeast(2022, 3))
- txtrSize += 4; // "TextureBlockSize"
- if (reader.undertaleData.IsVersionAtLeast(2022, 9))
- txtrSize += 12;
-
- if (txtrSize != UndertaleEmbeddedTexture.ChildObjectsSize)
- reader.SetStaticChildObjectsSize(typeof(UndertaleEmbeddedTexture), txtrSize);
-
// Texture blobs are already included in the count
return base.UnserializeObjectCount(reader);
}