Skip to content

Commit

Permalink
refactor(pvr tex tool): first search for cli in the system
Browse files Browse the repository at this point in the history
  • Loading branch information
danila-schelkov committed Aug 12, 2024
1 parent ab416fe commit f0c0260
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 20 deletions.
4 changes: 2 additions & 2 deletions system/lib/features/place_sprites.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion system/lib/features/sc/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 5 additions & 3 deletions system/lib/objects/movie_clip.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

from math import ceil
from typing import TYPE_CHECKING, List, Tuple

Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()

Expand Down Expand Up @@ -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
Expand Down
10 changes: 6 additions & 4 deletions system/lib/objects/shape.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

from math import atan2, ceil, degrees
from typing import TYPE_CHECKING, List, Optional, Tuple

Expand All @@ -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
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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)
Expand Down
33 changes: 23 additions & 10 deletions system/lib/pvr_tex_tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 executable_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:
Expand Down Expand Up @@ -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

Expand All @@ -65,7 +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"{_cli_path} -f {_format} -q {_quality} "
f"-i {filepath!s} -o {output_filepath!s}"
)

Expand Down

0 comments on commit f0c0260

Please sign in to comment.