Skip to content

Commit f0c0260

Browse files
refactor(pvr tex tool): first search for cli in the system
1 parent ab416fe commit f0c0260

File tree

5 files changed

+37
-20
lines changed

5 files changed

+37
-20
lines changed

system/lib/features/place_sprites.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,9 @@ def place_sprites(
8585
f'{folder}{"/overwrite" if overwrite else ""}/{filename}'
8686
).convert("RGBA")
8787
if region_info.is_mirrored:
88-
tmp_region = tmp_region.transpose(Image.FLIP_LEFT_RIGHT)
88+
tmp_region = tmp_region.transpose(Image.Transpose.FLIP_LEFT_RIGHT)
8989
tmp_region = tmp_region.rotate(region_info.rotation, expand=True)
90-
tmp_region = tmp_region.resize((width, height), Image.ANTIALIAS)
90+
tmp_region = tmp_region.resize((width, height), Image.Resampling.LANCZOS)
9191

9292
sheets[region_info.texture_id].paste(
9393
Image.new("RGBA", (width, height)), (left, top), img_mask.crop(bbox)

system/lib/features/sc/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def compile_sc(
3535

3636
if Console.question(locale.resize_qu):
3737
logger.info(locale.resizing)
38-
sheet = sheet.resize(sheet_info.size, Image.ANTIALIAS)
38+
sheet = sheet.resize(sheet_info.size, Image.Resampling.LANCZOS)
3939

4040
width, height = sheet.size
4141
pixel_size = get_byte_count_by_pixel_type(pixel_type)

system/lib/objects/movie_clip.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import annotations
2+
13
from math import ceil
24
from typing import TYPE_CHECKING, List, Tuple
35

@@ -53,7 +55,7 @@ def __init__(self):
5355
self.binds: List[int] = []
5456
self.matrix_bank_index: int = 0
5557

56-
def load(self, swf: "SupercellSWF", tag: int):
58+
def load(self, swf: SupercellSWF, tag: int):
5759
self.id = swf.reader.read_ushort()
5860

5961
self.fps = swf.reader.read_char()
@@ -115,7 +117,7 @@ def load(self, swf: "SupercellSWF", tag: int):
115117
else:
116118
swf.reader.read(frame_length)
117119

118-
def render(self, swf: "SupercellSWF", matrix=None) -> Image.Image:
120+
def render(self, swf: SupercellSWF, matrix=None) -> Image.Image:
119121
if self in CACHE:
120122
return CACHE[self].copy()
121123

@@ -150,7 +152,7 @@ def render(self, swf: "SupercellSWF", matrix=None) -> Image.Image:
150152

151153
return image
152154

153-
def get_sides(self, swf: "SupercellSWF") -> Tuple[float, float, float, float]:
155+
def get_sides(self, swf: SupercellSWF) -> Tuple[float, float, float, float]:
154156
matrix_bank: MatrixBank = swf.get_matrix_bank(self.matrix_bank_index)
155157

156158
left = 0

system/lib/objects/shape.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import annotations
2+
13
from math import atan2, ceil, degrees
24
from typing import TYPE_CHECKING, List, Optional, Tuple
35

@@ -17,7 +19,7 @@ def __init__(self):
1719
self.id = 0
1820
self.regions: List[Region] = []
1921

20-
def load(self, swf: "SupercellSWF", tag: int):
22+
def load(self, swf: SupercellSWF, tag: int):
2123
self.id = swf.reader.read_ushort()
2224

2325
swf.reader.read_ushort() # regions_count
@@ -101,7 +103,7 @@ def __init__(self):
101103

102104
self.texture: SWFTexture
103105

104-
def load(self, swf: "SupercellSWF", tag: int):
106+
def load(self, swf: SupercellSWF, tag: int):
105107
self.texture_index = swf.reader.read_uchar()
106108

107109
self.texture = swf.textures[self.texture_index]
@@ -161,10 +163,10 @@ def render(self, use_original_size: bool = False) -> Image.Image:
161163

162164
rendered_region = rendered_region.rotate(-self.rotation, expand=True)
163165
if self.is_mirrored:
164-
rendered_region = rendered_region.transpose(Image.FLIP_LEFT_RIGHT)
166+
rendered_region = rendered_region.transpose(Image.Transpose.FLIP_LEFT_RIGHT)
165167
if use_original_size:
166168
return rendered_region
167-
return rendered_region.resize((width, height), Image.ANTIALIAS)
169+
return rendered_region.resize((width, height), Image.Resampling.LANCZOS)
168170

169171
def get_image(self) -> Image.Image:
170172
left, top, right, bottom = get_sides(self._uv_points)

system/lib/pvr_tex_tool.py

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,31 @@
77
from system import run
88
from system.exceptions.tool_not_found import ToolNotFoundException
99

10-
TOOL_DIR = os.path.dirname(os.path.dirname(os.path.dirname(__file__)))
11-
COLOR_SPACE = "sRGB"
12-
KTX_FORMAT = "ETC1,UBN,lRGB"
13-
QUALITY = "etcfast"
14-
CLI_PATH = f"{TOOL_DIR}/system/bin/PVRTexToolCLI"
10+
_main_dir = os.path.dirname(os.path.dirname(os.path.dirname(__file__)))
11+
_color_space = "sRGB"
12+
_format = "ETC1,UBN,lRGB"
13+
_quality = "etcfast"
1514

1615

1716
# Note: a solution from
1817
# https://stackoverflow.com/questions/11210104/check-if-a-program-exists-from-a-python-script
19-
def can_use_pvr_tex_tool() -> bool:
18+
def _get_executable_path(*paths: str) -> str | None:
2019
from distutils.spawn import find_executable
2120

22-
executable_path = find_executable(CLI_PATH)
23-
return executable_path is not None
21+
for path in paths:
22+
executable_path = find_executable(path)
23+
if executable_path is not None:
24+
return executable_path
25+
26+
return None
27+
28+
29+
_cli_name = "PVRTexToolCLI"
30+
_cli_path = _get_executable_path(_cli_name, f"{_main_dir}/system/bin/{_cli_name}")
31+
32+
33+
def can_use_pvr_tex_tool() -> bool:
34+
return _cli_path is not None
2435

2536

2637
def get_image_from_ktx_data(data: bytes) -> Image.Image:
@@ -52,7 +63,9 @@ def convert_ktx_to_png(filepath: Path, output_folder: Path | None = None) -> Pat
5263
if output_folder is not None:
5364
output_filepath = output_folder / output_filepath.name
5465

55-
run(f"{CLI_PATH} -noout -ics {COLOR_SPACE} -i {filepath!s} -d {output_filepath!s}")
66+
run(
67+
f"{_cli_path} -noout -ics {_color_space} -i {filepath!s} -d {output_filepath!s}"
68+
)
5669

5770
return output_filepath
5871

@@ -65,7 +78,7 @@ def convert_png_to_ktx(filepath: Path, output_folder: Path | None = None) -> Pat
6578
output_filepath = output_folder / output_filepath.name
6679

6780
run(
68-
f"{CLI_PATH} -f {KTX_FORMAT} -q {QUALITY} "
81+
f"{_cli_path} -f {_format} -q {_quality} "
6982
f"-i {filepath!s} -o {output_filepath!s}"
7083
)
7184

0 commit comments

Comments
 (0)