From 13e102bdc05cd5fc078dbff9c91f1a8373db97d4 Mon Sep 17 00:00:00 2001 From: Rudolf Kolbe Date: Thu, 21 Nov 2024 00:31:39 +0100 Subject: [PATCH] 1.20.16 - Texture2DConverter - astc decompress - fix decompress import by checking length --- UnityPy/__init__.py | 2 +- UnityPy/export/Texture2DConverter.py | 17 ++++++++++++++++- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/UnityPy/__init__.py b/UnityPy/__init__.py index 479ca0f2..db60f483 100644 --- a/UnityPy/__init__.py +++ b/UnityPy/__init__.py @@ -1,4 +1,4 @@ -__version__ = "1.20.15" +__version__ = "1.20.16" from .environment import Environment as Environment from .helpers.ArchiveStorageManager import ( diff --git a/UnityPy/export/Texture2DConverter.py b/UnityPy/export/Texture2DConverter.py index 8b41ea0c..caf9fa0f 100644 --- a/UnityPy/export/Texture2DConverter.py +++ b/UnityPy/export/Texture2DConverter.py @@ -268,11 +268,26 @@ def astc(image_data: bytes, width: int, height: int, block_size: tuple) -> Image context = ASTC_CONTEXTS[block_size] = astc_encoder.ASTCContext(config) image = astc_encoder.ASTCImage(astc_encoder.ASTCType.U8, width, height, 1) - context.decompress(image_data, image, astc_encoder.ASTCSwizzle.from_str("RGBA")) + texture_size = calculate_astc_compressed_size(width, height, block_size) + if len(image_data) < texture_size: + raise ValueError(f"Invalid ASTC data size: {len(image_data)} < {texture_size}") + context.decompress( + image_data[:texture_size], image, astc_encoder.ASTCSwizzle.from_str("RGBA") + ) return Image.frombytes("RGBA", (width, height), image.data, "raw", "RGBA") +def calculate_astc_compressed_size(width: int, height: int, block_size: tuple) -> int: + """Calculate the size of the compressed data for ASTC.""" + # calculate the number of blocks + block_count_x = (width + block_size[0] - 1) // block_size[0] + block_count_y = (height + block_size[1] - 1) // block_size[1] + # ignore depth for 2D textures + # calculate the size of the compressed data + return block_count_x * block_count_y * 16 + + def pvrtc(image_data: bytes, width: int, height: int, fmt: bool) -> Image.Image: image_data = texture2ddecoder.decode_pvrtc(image_data, width, height, fmt) return Image.frombytes("RGBA", (width, height), image_data, "raw", "BGRA")