Skip to content

Commit cdd7e15

Browse files
committed
chore(Texture2DConverter): deduplicate switch handling
1 parent 099f256 commit cdd7e15

File tree

1 file changed

+17
-19
lines changed

1 file changed

+17
-19
lines changed

UnityPy/export/Texture2DConverter.py

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -220,22 +220,9 @@ def image_to_texture2d(
220220
tex_format = TF.RGB24
221221
pil_mode = "RGB"
222222
# everything else defaulted to RGBA
223-
if compress_func:
224-
width, height = img.width, img.height
225-
if TextureSwizzler.is_switch_swizzled(platform, platform_blob):
226-
gobsPerBlock = TextureSwizzler.get_switch_gobs_per_block(platform_blob)
227-
s_tex_format = tex_format
228-
block_size = TextureSwizzler.TEXTUREFORMAT_BLOCK_SIZE_MAP[s_tex_format]
229-
width, height = TextureSwizzler.get_padded_texture_size(
230-
img.width, img.height, *block_size, gobsPerBlock
231-
)
232-
width, height = get_compressed_image_size(width, height, tex_format)
233-
img = pad_image(img, width, height)
234-
enc_img = compress_func(
235-
img.tobytes("raw", "RGBA"), img.width, img.height, tex_format
236-
)
237-
else:
238-
enc_img = img.tobytes("raw", pil_mode)
223+
224+
width, height = img.width, img.height
225+
switch_info = None
239226

240227
if TextureSwizzler.is_switch_swizzled(platform, platform_blob):
241228
if TYPE_CHECKING:
@@ -253,11 +240,22 @@ def image_to_texture2d(
253240
width, height = TextureSwizzler.get_padded_texture_size(
254241
img.width, img.height, *block_size, gobsPerBlock
255242
)
256-
if not compress_func:
257-
# recompress with padding and corrected image mode
243+
switch_info = (block_size, gobsPerBlock)
244+
245+
if compress_func:
246+
width, height = get_compressed_image_size(width, height, tex_format)
247+
img = pad_image(img, width, height)
248+
enc_img = compress_func(
249+
img.tobytes("raw", "RGBA"), img.width, img.height, tex_format
250+
)
251+
else:
252+
if switch_info is not None:
258253
img = pad_image(img, width, height)
259-
enc_img = img.tobytes("raw", pil_mode)
260254

255+
enc_img = img.tobytes("raw", pil_mode)
256+
257+
if switch_info is not None:
258+
block_size, gobsPerBlock = switch_info
261259
enc_img = bytes(
262260
TextureSwizzler.swizzle(enc_img, width, height, *block_size, gobsPerBlock)
263261
)

0 commit comments

Comments
 (0)