Skip to content

Commit

Permalink
Merge pull request #1830 from UnderminersTeam/fix-external-texture-mi…
Browse files Browse the repository at this point in the history
…scount

Fix external textures being counted in object pool
  • Loading branch information
colinator27 authored Jul 19, 2024
2 parents c8e1b9b + 2441b49 commit ee5fe13
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 19 deletions.
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 @@ -1651,17 +1651,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

0 comments on commit ee5fe13

Please sign in to comment.