From fa3a5ed1100956729b28be4d3cd7289d6ce002bf Mon Sep 17 00:00:00 2001 From: Danila Date: Mon, 12 Aug 2024 22:55:46 +0300 Subject: [PATCH] refactor(pvr tex tool): first search for cli in the system --- system/languages/en-EU.json | 2 +- system/languages/ru-RU.json | 2 +- system/languages/ua-UA.json | 2 +- system/lib/features/ktx.py | 5 ++++ system/lib/features/place_sprites.py | 4 ++-- system/lib/features/sc/__init__.py | 2 +- system/lib/menu.py | 2 +- system/lib/objects/movie_clip.py | 8 ++++--- system/lib/objects/shape.py | 10 ++++---- system/lib/pvr_tex_tool.py | 34 +++++++++++++++++++--------- 10 files changed, 46 insertions(+), 25 deletions(-) diff --git a/system/languages/en-EU.json b/system/languages/en-EU.json index 8f4e5ce..0fc6509 100644 --- a/system/languages/en-EU.json +++ b/system/languages/en-EU.json @@ -78,7 +78,7 @@ "not_found": "File '%s' not found!", "cut_sprites_process": "Cutting sprites... (%d/%d)", "place_sprites_process": "Placing sprites... (%d/%d)", - "not_implemented": "This feature will be added in future updates.\nYou can follow XCoder updates here: github.com/Vorono4ka/XCoder", + "not_implemented": "This feature will be added in future updates.\nYou can follow XCoder updates here: https://github.com/Vorono4ka/XCoder", "error": "ERROR! (%s.%s: %s)", "e1sc1": "Overwrite SC sprites", "cgl": "Changelog:\n%s", diff --git a/system/languages/ru-RU.json b/system/languages/ru-RU.json index 9df9991..afa55b6 100644 --- a/system/languages/ru-RU.json +++ b/system/languages/ru-RU.json @@ -78,7 +78,7 @@ "not_found": "Файл '%s' не найден!", "cut_sprites_process": "Вырезаем спрайты... (%d/%d)", "place_sprites_process": "Ставим спрайты на место... (%d/%d)", - "not_implemented": "Данная возможность будет добавлена в будущих обновлениях.\nЗа обновлениями XCoder вы можете следить здесь: github.com/Vorono4ka/XCoder", + "not_implemented": "Данная возможность будет добавлена в будущих обновлениях.\nЗа обновлениями XCoder вы можете следить здесь: https://github.com/Vorono4ka/XCoder", "error": "ОШИБКА! (%s.%s: %s)", "e1sc1": "Перезапись спрайтов", "cgl": "Список изменений: \n%s", diff --git a/system/languages/ua-UA.json b/system/languages/ua-UA.json index d82048d..b10f1f3 100644 --- a/system/languages/ua-UA.json +++ b/system/languages/ua-UA.json @@ -78,7 +78,7 @@ "not_found": "Файл '%s' не знайдено!", "cut_sprites_process": "Обрізаємо спрайти... (%d/%d)", "place_sprites_process": "Вставляємо спрайти... (%d/%d)", - "not_implemented": "Ця функція буде додана у наступних оновленнях.\nТи можеш сладкувати за оновленнями тут: github.com/Vorono4ka/XCoder", + "not_implemented": "Ця функція буде додана у наступних оновленнях.\nТи можеш сладкувати за оновленнями тут: https://github.com/Vorono4ka/XCoder", "error": "Помилка! (%s.%s: %s)", "e1sc1": "Переписати SC спрайти", "cgl": "Список змін:\n%s", diff --git a/system/lib/features/ktx.py b/system/lib/features/ktx.py index ea9fd95..22eb4cb 100644 --- a/system/lib/features/ktx.py +++ b/system/lib/features/ktx.py @@ -1,7 +1,10 @@ import os from pathlib import Path +from loguru import logger + from system.lib.pvr_tex_tool import convert_ktx_to_png, convert_png_to_ktx +from system.localization import locale IN_PNG_PATH = Path("./TEX/In-PNG") IN_KTX_PATH = Path("./TEX/In-KTX") @@ -21,6 +24,7 @@ def convert_png_textures_to_ktx(): if not os.path.isfile(png_filepath): continue + logger.info(locale.collecting_inf % file) convert_png_to_ktx(png_filepath, output_folder=output_folder) @@ -36,4 +40,5 @@ def convert_ktx_textures_to_png(): if not os.path.isfile(ktx_filepath): continue + logger.info(locale.collecting_inf % file) convert_ktx_to_png(ktx_filepath, output_folder=output_folder) diff --git a/system/lib/features/place_sprites.py b/system/lib/features/place_sprites.py index 0a039a7..2cf8171 100644 --- a/system/lib/features/place_sprites.py +++ b/system/lib/features/place_sprites.py @@ -85,9 +85,9 @@ def place_sprites( f'{folder}{"/overwrite" if overwrite else ""}/{filename}' ).convert("RGBA") if region_info.is_mirrored: - tmp_region = tmp_region.transpose(Image.FLIP_LEFT_RIGHT) + tmp_region = tmp_region.transpose(Image.Transpose.FLIP_LEFT_RIGHT) tmp_region = tmp_region.rotate(region_info.rotation, expand=True) - tmp_region = tmp_region.resize((width, height), Image.ANTIALIAS) + tmp_region = tmp_region.resize((width, height), Image.Resampling.LANCZOS) sheets[region_info.texture_id].paste( Image.new("RGBA", (width, height)), (left, top), img_mask.crop(bbox) diff --git a/system/lib/features/sc/__init__.py b/system/lib/features/sc/__init__.py index f07e999..94b0f6f 100644 --- a/system/lib/features/sc/__init__.py +++ b/system/lib/features/sc/__init__.py @@ -35,7 +35,7 @@ def compile_sc( if Console.question(locale.resize_qu): logger.info(locale.resizing) - sheet = sheet.resize(sheet_info.size, Image.ANTIALIAS) + sheet = sheet.resize(sheet_info.size, Image.Resampling.LANCZOS) width, height = sheet.size pixel_size = get_byte_count_by_pixel_type(pixel_type) diff --git a/system/lib/menu.py b/system/lib/menu.py index 76afbf3..385b9c3 100644 --- a/system/lib/menu.py +++ b/system/lib/menu.py @@ -73,7 +73,7 @@ def choice(self): + colorama.Style.RESET_ALL ).center(console_width + 12) ) - print("github.com/Vorono4ka/XCoder".center(console_width - 1)) + print("https://github.com/Vorono4ka/XCoder".center(console_width - 1)) self._print_divider_line(console_width) for category in self.categories: diff --git a/system/lib/objects/movie_clip.py b/system/lib/objects/movie_clip.py index 148c635..196ea9a 100644 --- a/system/lib/objects/movie_clip.py +++ b/system/lib/objects/movie_clip.py @@ -1,3 +1,5 @@ +from __future__ import annotations + from math import ceil from typing import TYPE_CHECKING, List, Tuple @@ -53,7 +55,7 @@ def __init__(self): self.binds: List[int] = [] self.matrix_bank_index: int = 0 - def load(self, swf: "SupercellSWF", tag: int): + def load(self, swf: SupercellSWF, tag: int): self.id = swf.reader.read_ushort() self.fps = swf.reader.read_char() @@ -115,7 +117,7 @@ def load(self, swf: "SupercellSWF", tag: int): else: swf.reader.read(frame_length) - def render(self, swf: "SupercellSWF", matrix=None) -> Image.Image: + def render(self, swf: SupercellSWF, matrix=None) -> Image.Image: if self in CACHE: return CACHE[self].copy() @@ -150,7 +152,7 @@ def render(self, swf: "SupercellSWF", matrix=None) -> Image.Image: return image - def get_sides(self, swf: "SupercellSWF") -> Tuple[float, float, float, float]: + def get_sides(self, swf: SupercellSWF) -> Tuple[float, float, float, float]: matrix_bank: MatrixBank = swf.get_matrix_bank(self.matrix_bank_index) left = 0 diff --git a/system/lib/objects/shape.py b/system/lib/objects/shape.py index 42d45b8..3949442 100644 --- a/system/lib/objects/shape.py +++ b/system/lib/objects/shape.py @@ -1,3 +1,5 @@ +from __future__ import annotations + from math import atan2, ceil, degrees from typing import TYPE_CHECKING, List, Optional, Tuple @@ -17,7 +19,7 @@ def __init__(self): self.id = 0 self.regions: List[Region] = [] - def load(self, swf: "SupercellSWF", tag: int): + def load(self, swf: SupercellSWF, tag: int): self.id = swf.reader.read_ushort() swf.reader.read_ushort() # regions_count @@ -101,7 +103,7 @@ def __init__(self): self.texture: SWFTexture - def load(self, swf: "SupercellSWF", tag: int): + def load(self, swf: SupercellSWF, tag: int): self.texture_index = swf.reader.read_uchar() self.texture = swf.textures[self.texture_index] @@ -161,10 +163,10 @@ def render(self, use_original_size: bool = False) -> Image.Image: rendered_region = rendered_region.rotate(-self.rotation, expand=True) if self.is_mirrored: - rendered_region = rendered_region.transpose(Image.FLIP_LEFT_RIGHT) + rendered_region = rendered_region.transpose(Image.Transpose.FLIP_LEFT_RIGHT) if use_original_size: return rendered_region - return rendered_region.resize((width, height), Image.ANTIALIAS) + return rendered_region.resize((width, height), Image.Resampling.LANCZOS) def get_image(self) -> Image.Image: left, top, right, bottom = get_sides(self._uv_points) diff --git a/system/lib/pvr_tex_tool.py b/system/lib/pvr_tex_tool.py index 8ed85e1..3340453 100644 --- a/system/lib/pvr_tex_tool.py +++ b/system/lib/pvr_tex_tool.py @@ -7,20 +7,31 @@ from system import run from system.exceptions.tool_not_found import ToolNotFoundException -TOOL_DIR = os.path.dirname(os.path.dirname(os.path.dirname(__file__))) -COLOR_SPACE = "sRGB" -KTX_FORMAT = "ETC1,UBN,lRGB" -QUALITY = "etcfast" -CLI_PATH = f"{TOOL_DIR}/system/bin/PVRTexToolCLI" +_main_dir = os.path.dirname(os.path.dirname(os.path.dirname(__file__))) +_color_space = "sRGB" +_format = "ETC1,UBN,lRGB" +_quality = "etcfast" # Note: a solution from # https://stackoverflow.com/questions/11210104/check-if-a-program-exists-from-a-python-script -def can_use_pvr_tex_tool() -> bool: +def _get_executable_path(*paths: str) -> str | None: from distutils.spawn import find_executable - executable_path = find_executable(CLI_PATH) - return executable_path is not None + for path in paths: + executable_path = find_executable(path) + if executable_path is not None: + return path + + return None + + +_cli_name = "PVRTexToolCLI" +_cli_path = _get_executable_path(_cli_name, f"{_main_dir}/system/bin/{_cli_name}") + + +def can_use_pvr_tex_tool() -> bool: + return _cli_path is not None 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 if output_folder is not None: output_filepath = output_folder / output_filepath.name - run(f"{CLI_PATH} -noout -ics {COLOR_SPACE} -i {filepath!s} -d {output_filepath!s}") + run( + f"{_cli_path} -noout -ics {_color_space} -i {filepath!s} -d {output_filepath!s}" + ) return output_filepath @@ -65,8 +78,7 @@ def convert_png_to_ktx(filepath: Path, output_folder: Path | None = None) -> Pat output_filepath = output_folder / output_filepath.name run( - f"{CLI_PATH} -f {KTX_FORMAT} -q {QUALITY} " - f"-i {filepath!s} -o {output_filepath!s}" + f"{_cli_path} -f {_format} -q {_quality} -i {filepath!s} -o {output_filepath!s}" ) return output_filepath