Skip to content

Commit 8b9a8a7

Browse files
isHarryhK0lb3
authored andcommitted
fix: reduce astc context lock scope
1 parent 859b742 commit 8b9a8a7

File tree

1 file changed

+17
-17
lines changed

1 file changed

+17
-17
lines changed

UnityPy/export/Texture2DConverter.py

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -71,24 +71,24 @@ def image_to_texture2d(
7171
# ASTC
7272
elif target_texture_format.name.startswith("ASTC"):
7373
raw_img = img.tobytes("raw", "RGBA")
74-
74+
raw_img = astc_encoder.ASTCImage(
75+
astc_encoder.ASTCType.U8, img.width, img.height, 1, raw_img
76+
)
7577
block_size = tuple(
7678
map(int, target_texture_format.name.rsplit("_", 1)[1].split("x"))
7779
)
78-
context, lock = get_astc_context(block_size)
80+
if img.mode == "RGB":
81+
tex_format = getattr(TF, f"ASTC_RGB_{block_size[0]}x{block_size[1]}")
82+
else:
83+
tex_format = getattr(TF, f"ASTC_RGBA_{block_size[0]}x{block_size[1]}")
84+
85+
swizzle = astc_encoder.ASTCSwizzle.from_str("RGBA")
7986

87+
context, lock = get_astc_context(block_size)
8088
with lock:
81-
raw_img = astc_encoder.ASTCImage(
82-
astc_encoder.ASTCType.U8, img.width, img.height, 1, raw_img
83-
)
84-
if img.mode == "RGB":
85-
tex_format = getattr(TF, f"ASTC_RGB_{block_size[0]}x{block_size[1]}")
86-
else:
87-
tex_format = getattr(TF, f"ASTC_RGBA_{block_size[0]}x{block_size[1]}")
88-
89-
swizzle = astc_encoder.ASTCSwizzle.from_str("RGBA")
9089
enc_img = context.compress(raw_img, swizzle)
91-
tex_format = target_texture_format
90+
91+
tex_format = target_texture_format
9292
# A
9393
elif target_texture_format == TF.Alpha8:
9494
enc_img = img.tobytes("raw", "A")
@@ -252,13 +252,13 @@ def atc(image_data: bytes, width: int, height: int, alpha: bool) -> Image.Image:
252252

253253

254254
def astc(image_data: bytes, width: int, height: int, block_size: tuple) -> Image.Image:
255-
context, lock = get_astc_context(block_size)
255+
image = astc_encoder.ASTCImage(astc_encoder.ASTCType.U8, width, height, 1)
256+
texture_size = calculate_astc_compressed_size(width, height, block_size)
257+
if len(image_data) < texture_size:
258+
raise ValueError(f"Invalid ASTC data size: {len(image_data)} < {texture_size}")
256259

260+
context, lock = get_astc_context(block_size)
257261
with lock:
258-
image = astc_encoder.ASTCImage(astc_encoder.ASTCType.U8, width, height, 1)
259-
texture_size = calculate_astc_compressed_size(width, height, block_size)
260-
if len(image_data) < texture_size:
261-
raise ValueError(f"Invalid ASTC data size: {len(image_data)} < {texture_size}")
262262
context.decompress(
263263
image_data[:texture_size], image, astc_encoder.ASTCSwizzle.from_str("RGBA")
264264
)

0 commit comments

Comments
 (0)