-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathencode.py
More file actions
76 lines (54 loc) · 2.23 KB
/
encode.py
File metadata and controls
76 lines (54 loc) · 2.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import os
from pathlib import Path
from loguru import logger
from PIL import Image
from xcoder.features.place_sprites import place_sprites
from xcoder.features.sc import compile_sc
from xcoder.localization import locale
from xcoder.xcod import parse_info
OUT_COMPRESSED_PATH = Path("./SC/Out-Compressed")
IN_DECOMPRESSED_PATH = Path("./SC/In-Decompressed")
IN_SPRITES_PATH = Path("./SC/In-Sprites/")
def encode_textures_only():
input_folder = IN_DECOMPRESSED_PATH
output_folder = OUT_COMPRESSED_PATH
for folder in os.listdir(input_folder):
textures_input_folder = input_folder / folder
if not os.path.isdir(textures_input_folder):
continue
xcod_path = _ensure_metadata_exists(textures_input_folder, folder)
if xcod_path is None:
continue
file_info = parse_info(xcod_path, False)
sheets = _load_sheets(textures_input_folder)
compile_sc(output_folder, file_info, sheets)
def collect_objects_and_encode(overwrite: bool = False) -> None:
input_folder = IN_SPRITES_PATH
output_folder = OUT_COMPRESSED_PATH
for folder in os.listdir(input_folder):
objects_input_folder = input_folder / folder
if not os.path.isdir(objects_input_folder):
continue
xcod_path = _ensure_metadata_exists(objects_input_folder, folder)
if xcod_path is None:
continue
file_info = parse_info(xcod_path, True)
sheets = place_sprites(file_info, objects_input_folder, overwrite)
compile_sc(output_folder, file_info, sheets)
def _ensure_metadata_exists(input_folder: Path, file: str) -> Path | None:
metadata_file_name = f"{file}.xcod"
metadata_file_path = input_folder / metadata_file_name
if not os.path.exists(metadata_file_path):
logger.error(locale.file_not_found % metadata_file_name)
print()
return None
return metadata_file_path
def _load_sheets(input_folder: Path) -> list[Image.Image]:
files = []
for i in os.listdir(input_folder):
if i.endswith(".png"):
files.append(i)
files.sort()
if not files:
raise RuntimeError(locale.dir_empty % input_folder.name)
return [Image.open(input_folder / file) for file in files]