Skip to content

Commit 8967305

Browse files
Merge pull request #21 from xcoder-tool/pvr-tex-tool-refactoring
refactor(pvr tex tool): first search for cli in the system
2 parents ab416fe + fa3a5ed commit 8967305

File tree

10 files changed

+46
-25
lines changed

10 files changed

+46
-25
lines changed

system/languages/en-EU.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@
7878
"not_found": "File '%s' not found!",
7979
"cut_sprites_process": "Cutting sprites... (%d/%d)",
8080
"place_sprites_process": "Placing sprites... (%d/%d)",
81-
"not_implemented": "This feature will be added in future updates.\nYou can follow XCoder updates here: github.com/Vorono4ka/XCoder",
81+
"not_implemented": "This feature will be added in future updates.\nYou can follow XCoder updates here: https://github.com/Vorono4ka/XCoder",
8282
"error": "ERROR! (%s.%s: %s)",
8383
"e1sc1": "Overwrite SC sprites",
8484
"cgl": "Changelog:\n%s",

system/languages/ru-RU.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@
7878
"not_found": "Файл '%s' не найден!",
7979
"cut_sprites_process": "Вырезаем спрайты... (%d/%d)",
8080
"place_sprites_process": "Ставим спрайты на место... (%d/%d)",
81-
"not_implemented": "Данная возможность будет добавлена в будущих обновлениях.\nЗа обновлениями XCoder вы можете следить здесь: github.com/Vorono4ka/XCoder",
81+
"not_implemented": "Данная возможность будет добавлена в будущих обновлениях.\nЗа обновлениями XCoder вы можете следить здесь: https://github.com/Vorono4ka/XCoder",
8282
"error": "ОШИБКА! (%s.%s: %s)",
8383
"e1sc1": "Перезапись спрайтов",
8484
"cgl": "Список изменений: \n%s",

system/languages/ua-UA.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@
7878
"not_found": "Файл '%s' не знайдено!",
7979
"cut_sprites_process": "Обрізаємо спрайти... (%d/%d)",
8080
"place_sprites_process": "Вставляємо спрайти... (%d/%d)",
81-
"not_implemented": "Ця функція буде додана у наступних оновленнях.\nТи можеш сладкувати за оновленнями тут: github.com/Vorono4ka/XCoder",
81+
"not_implemented": "Ця функція буде додана у наступних оновленнях.\nТи можеш сладкувати за оновленнями тут: https://github.com/Vorono4ka/XCoder",
8282
"error": "Помилка! (%s.%s: %s)",
8383
"e1sc1": "Переписати SC спрайти",
8484
"cgl": "Список змін:\n%s",

system/lib/features/ktx.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
import os
22
from pathlib import Path
33

4+
from loguru import logger
5+
46
from system.lib.pvr_tex_tool import convert_ktx_to_png, convert_png_to_ktx
7+
from system.localization import locale
58

69
IN_PNG_PATH = Path("./TEX/In-PNG")
710
IN_KTX_PATH = Path("./TEX/In-KTX")
@@ -21,6 +24,7 @@ def convert_png_textures_to_ktx():
2124
if not os.path.isfile(png_filepath):
2225
continue
2326

27+
logger.info(locale.collecting_inf % file)
2428
convert_png_to_ktx(png_filepath, output_folder=output_folder)
2529

2630

@@ -36,4 +40,5 @@ def convert_ktx_textures_to_png():
3640
if not os.path.isfile(ktx_filepath):
3741
continue
3842

43+
logger.info(locale.collecting_inf % file)
3944
convert_ktx_to_png(ktx_filepath, output_folder=output_folder)

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/menu.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ def choice(self):
7373
+ colorama.Style.RESET_ALL
7474
).center(console_width + 12)
7575
)
76-
print("github.com/Vorono4ka/XCoder".center(console_width - 1))
76+
print("https://github.com/Vorono4ka/XCoder".center(console_width - 1))
7777
self._print_divider_line(console_width)
7878

7979
for category in self.categories:

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 & 11 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 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,8 +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} "
69-
f"-i {filepath!s} -o {output_filepath!s}"
81+
f"{_cli_path} -f {_format} -q {_quality} -i {filepath!s} -o {output_filepath!s}"
7082
)
7183

7284
return output_filepath

0 commit comments

Comments
 (0)