Skip to content

Commit db359ee

Browse files
refactor(KTX): ktx section is optional now, and it raises an exception when used, but not installed
1 parent e2fee73 commit db359ee

File tree

7 files changed

+51
-13
lines changed

7 files changed

+51
-13
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,6 @@ system/config.json
1414

1515
# Python compiled files
1616
*.pyc
17+
18+
# PVRTexToolCLI
19+
PVRTexToolCLI*

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,16 @@ Work with Supercell\`s files on **any** os! SC and CSV are supported for all Sup
3232
- Install loguru using pip;
3333
- In PyDroid open and execute "main.py" file.
3434

35+
### How to enable KTX section
36+
37+
![KTX section demo](docs/KTX section.png)
38+
39+
**Supercell also uses KTX textures in new versions of the games, so it is advisable to perform this step.**
40+
41+
To enable the KTX module, you need to get the "PVRTexToolCLI" binary from the official site: https://developer.imaginationtech.com/pvrtextool/.
42+
43+
Then it is necessary to put CLI in "system/bin/" folder in the main script folder.
44+
3545
### In the plans:
3646
- CSV updating.
3747

docs/KTX section.png

5.53 KB
Loading

system/exceptions/__init__.py

Whitespace-only changes.

system/exceptions/tool_not_found.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
class ToolNotFoundException(Exception):
2+
pass

system/lib/__init__.py

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -136,21 +136,23 @@ def refill_menu():
136136
convert_ktx_textures_to_png,
137137
convert_png_textures_to_ktx,
138138
)
139-
140-
ktx_category.add(
141-
Menu.Item(
142-
name=locale.ktx_from_png_label,
143-
description=locale.ktx_from_png_description,
144-
handler=convert_png_textures_to_ktx,
139+
from system.lib.pvr_tex_tool import can_use_pvr_tex_tool
140+
141+
if can_use_pvr_tex_tool():
142+
ktx_category.add(
143+
Menu.Item(
144+
name=locale.ktx_from_png_label,
145+
description=locale.ktx_from_png_description,
146+
handler=convert_png_textures_to_ktx,
147+
)
145148
)
146-
)
147-
ktx_category.add(
148-
Menu.Item(
149-
name=locale.png_from_ktx_label,
150-
description=locale.png_from_ktx_description,
151-
handler=convert_ktx_textures_to_png,
149+
ktx_category.add(
150+
Menu.Item(
151+
name=locale.png_from_ktx_label,
152+
description=locale.png_from_ktx_description,
153+
handler=convert_ktx_textures_to_png,
154+
)
152155
)
153-
)
154156

155157
csv_category.add(
156158
Menu.Item(

system/lib/pvr_tex_tool.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from PIL import Image
66

77
from system import run
8+
from system.exceptions.tool_not_found import ToolNotFoundException
89

910
TOOL_DIR = os.path.dirname(os.path.dirname(os.path.dirname(__file__)))
1011
COLOR_SPACE = "sRGB"
@@ -13,6 +14,15 @@
1314
CLI_PATH = f"{TOOL_DIR}/system/bin/PVRTexToolCLI"
1415

1516

17+
# Note: a solution from
18+
# https://stackoverflow.com/questions/11210104/check-if-a-program-exists-from-a-python-script
19+
def can_use_pvr_tex_tool() -> bool:
20+
from distutils.spawn import find_executable
21+
22+
executable_path = find_executable(CLI_PATH)
23+
return executable_path is not None
24+
25+
1626
def get_image_from_ktx_data(data: bytes) -> Image.Image:
1727
tmp = tempfile.NamedTemporaryFile(delete=False, suffix=".ktx")
1828
try:
@@ -36,6 +46,8 @@ def get_image_from_ktx(filepath: Path) -> Image.Image:
3646

3747

3848
def convert_ktx_to_png(filepath: Path, output_folder: Path | None = None) -> Path:
49+
_ensure_tool_installed()
50+
3951
output_filepath = filepath.with_suffix(".png")
4052
if output_folder is not None:
4153
output_filepath = output_folder / output_filepath.name
@@ -46,6 +58,8 @@ def convert_ktx_to_png(filepath: Path, output_folder: Path | None = None) -> Pat
4658

4759

4860
def convert_png_to_ktx(filepath: Path, output_folder: Path | None = None) -> Path:
61+
_ensure_tool_installed()
62+
4963
output_filepath = filepath.with_suffix(".ktx")
5064
if output_folder is not None:
5165
output_filepath = output_folder / output_filepath.name
@@ -56,3 +70,10 @@ def convert_png_to_ktx(filepath: Path, output_folder: Path | None = None) -> Pat
5670
)
5771

5872
return output_filepath
73+
74+
75+
def _ensure_tool_installed():
76+
if can_use_pvr_tex_tool():
77+
return
78+
79+
raise ToolNotFoundException("PVRTexTool not found.")

0 commit comments

Comments
 (0)