Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix external textures being counted in object pool #1830

Merged
merged 1 commit into from
Jul 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 24 additions & 8 deletions UndertaleModLib/Models/UndertaleEmbeddedTexture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,8 @@ namespace UndertaleModLib.Models;
/// An embedded texture entry in the data file.
/// </summary>
[PropertyChanged.AddINotifyPropertyChangedInterface]
public class UndertaleEmbeddedTexture : UndertaleNamedResource, IDisposable,
IStaticChildObjCount, IStaticChildObjectsSize
public class UndertaleEmbeddedTexture : UndertaleNamedResource, IDisposable
{
/// <inheritdoc cref="IStaticChildObjCount.ChildObjectCount" />
public static readonly uint ChildObjectCount = 1;

/// <inheritdoc cref="IStaticChildObjectsSize.ChildObjectsSize" />
public static readonly uint ChildObjectsSize = 4; // minimal size

/// <summary>
/// The name of the embedded texture entry.
/// </summary>
Expand Down Expand Up @@ -196,6 +189,29 @@ public void UnserializeBlob(UndertaleReader reader)
TextureLoaded = true;
}

/// <inheritdoc cref="UndertaleObject.UnserializeChildObjectCount(UndertaleReader)"/>
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;
}

/// <summary>
/// Assigns texture group info to every embedded texture in the supplied data file.
/// </summary>
Expand Down
11 changes: 0 additions & 11 deletions UndertaleModLib/UndertaleChunks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Loading