Skip to content

Commit e706662

Browse files
fix(rendering): movie clip rendering
1 parent bd535eb commit e706662

18 files changed

+461
-326
lines changed

main.py

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,20 @@
1-
# Refactored by Vorono4ka
2-
31
import time
42

3+
from system.lib.config import config
4+
from system.lib.main_menu import (
5+
check_auto_update,
6+
check_files_updated,
7+
menu,
8+
refill_menu,
9+
)
10+
from system.localization import locale
11+
512
try:
613
from loguru import logger
714
except ImportError:
815
raise RuntimeError("Please, install loguru using pip")
916

1017
from system import clear
11-
from system.lib import (
12-
config,
13-
locale,
14-
refill_menu,
15-
menu,
16-
check_auto_update,
17-
check_files_updated,
18-
)
1918
from system.lib.features.initialization import initialize
2019

2120

@@ -50,4 +49,3 @@ def main():
5049
main()
5150
except KeyboardInterrupt:
5251
logger.info("Exit.")
53-
pass

system/bytestream.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def read_twip(self) -> float:
3737

3838
def read_string(self) -> str:
3939
length = self.read_uchar()
40-
if length != 255:
40+
if length != 0xFF:
4141
return self.read(length).decode()
4242
return ""
4343

@@ -70,7 +70,7 @@ def write_int32(self, integer: int):
7070

7171
def write_string(self, string: str | None = None):
7272
if string is None:
73-
self.write_byte(255)
73+
self.write_byte(0xFF)
7474
return
7575
encoded = string.encode()
7676
self.write_byte(len(encoded))

system/lib/__init__.py

Lines changed: 0 additions & 190 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,7 @@
11
import sys
2-
import time
32

43
from loguru import logger
54

6-
from system import clear
7-
from system.lib.config import config
8-
from system.lib.console import Console
9-
from system.lib.features.directories import clear_directories
10-
from system.lib.features.initialization import initialize
11-
from system.lib.features.update.check import check_for_outdated, check_update, get_tags
12-
from system.lib.menu import Menu, menu
13-
from system.localization import locale
14-
155
logger.remove()
166
logger.add(
177
"./logs/info/{time:YYYY-MM-DD}.log",
@@ -28,183 +18,3 @@
2818
level="ERROR",
2919
)
3020
logger.add(sys.stdout, format="<lvl>[{level}] {message}</lvl>", level="INFO")
31-
32-
33-
locale.load(config.language)
34-
35-
36-
def check_auto_update():
37-
if config.auto_update and time.time() - config.last_update > 60 * 60 * 24 * 7:
38-
check_update()
39-
config.last_update = int(time.time())
40-
config.dump()
41-
42-
43-
def check_files_updated():
44-
if config.has_update:
45-
logger.opt(colors=True).info(f'<green>{locale.update_done % ""}</green>')
46-
if Console.question(locale.done_qu):
47-
latest_tag = get_tags("vorono4ka", "xcoder")[0]
48-
latest_tag_name = latest_tag["name"][1:]
49-
50-
config.has_update = False
51-
config.version = latest_tag_name
52-
config.last_update = int(time.time())
53-
config.dump()
54-
else:
55-
exit()
56-
57-
58-
# noinspection PyUnresolvedReferences
59-
@logger.catch()
60-
def refill_menu():
61-
menu.categories.clear()
62-
63-
sc_category = Menu.Category(0, locale.sc_label)
64-
ktx_category = Menu.Category(1, locale.ktx_label)
65-
csv_category = Menu.Category(2, locale.csv_label)
66-
other = Menu.Category(10, locale.other_features_label)
67-
68-
menu.add_category(sc_category)
69-
menu.add_category(ktx_category)
70-
menu.add_category(csv_category)
71-
menu.add_category(other)
72-
73-
try:
74-
import sc_compression
75-
76-
del sc_compression
77-
except ImportError:
78-
logger.warning(locale.install_to_unlock % "sc-compression")
79-
else:
80-
from system.lib.features.csv.compress import compress_csv
81-
from system.lib.features.csv.decompress import decompress_csv
82-
83-
try:
84-
import PIL
85-
86-
del PIL
87-
except ImportError:
88-
logger.warning(locale.install_to_unlock % "PILLOW")
89-
else:
90-
from system.lib.features.sc.decode import (
91-
decode_and_render_objects,
92-
decode_textures_only,
93-
)
94-
from system.lib.features.sc.encode import (
95-
collect_objects_and_encode,
96-
encode_textures_only,
97-
)
98-
99-
sc_category.add(
100-
Menu.Item(
101-
name=locale.decode_sc,
102-
description=locale.decode_sc_description,
103-
handler=decode_textures_only,
104-
)
105-
)
106-
sc_category.add(
107-
Menu.Item(
108-
name=locale.encode_sc,
109-
description=locale.encode_sc_description,
110-
handler=encode_textures_only,
111-
)
112-
)
113-
sc_category.add(
114-
Menu.Item(
115-
name=locale.decode_by_parts,
116-
description=locale.decode_by_parts_description,
117-
handler=decode_and_render_objects,
118-
)
119-
)
120-
sc_category.add(
121-
Menu.Item(
122-
name=locale.encode_by_parts,
123-
description=locale.encode_by_parts_description,
124-
handler=collect_objects_and_encode,
125-
)
126-
)
127-
sc_category.add(
128-
Menu.Item(
129-
name=locale.overwrite_by_parts,
130-
description=locale.overwrite_by_parts_description,
131-
handler=lambda: collect_objects_and_encode(True),
132-
)
133-
)
134-
135-
from system.lib.features.ktx import (
136-
convert_ktx_textures_to_png,
137-
convert_png_textures_to_ktx,
138-
)
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-
)
148-
)
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-
)
155-
)
156-
157-
csv_category.add(
158-
Menu.Item(
159-
name=locale.decompress_csv,
160-
description=locale.decompress_csv_description,
161-
handler=decompress_csv,
162-
)
163-
)
164-
csv_category.add(
165-
Menu.Item(
166-
name=locale.compress_csv,
167-
description=locale.compress_csv_description,
168-
handler=compress_csv,
169-
)
170-
)
171-
172-
other.add(
173-
Menu.Item(
174-
name=locale.check_update,
175-
description=locale.version % config.version,
176-
handler=check_update,
177-
)
178-
)
179-
other.add(Menu.Item(name=locale.check_for_outdated, handler=check_for_outdated))
180-
other.add(
181-
Menu.Item(
182-
name=locale.reinit,
183-
description=locale.reinit_description,
184-
handler=lambda: (initialize(), refill_menu()),
185-
)
186-
)
187-
other.add(
188-
Menu.Item(
189-
name=locale.change_language,
190-
description=locale.change_lang_description % config.language,
191-
handler=lambda: (config.change_language(locale.change()), refill_menu()),
192-
)
193-
)
194-
other.add(
195-
Menu.Item(
196-
name=locale.clear_directories,
197-
description=locale.clean_dirs_description,
198-
handler=lambda: (
199-
clear_directories() if Console.question(locale.clear_qu) else -1
200-
),
201-
)
202-
)
203-
other.add(
204-
Menu.Item(
205-
name=locale.toggle_update_auto_checking,
206-
description=locale.enabled if config.auto_update else locale.disabled,
207-
handler=lambda: (config.toggle_auto_update(), refill_menu()),
208-
)
209-
)
210-
other.add(Menu.Item(name=locale.exit, handler=lambda: (clear(), exit())))

system/lib/features/cut_sprites.py

Lines changed: 35 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -15,34 +15,24 @@ def render_objects(swf: SupercellSWF, output_folder: Path):
1515
os.makedirs(output_folder / "shapes", exist_ok=True)
1616
os.makedirs(output_folder / "movie_clips", exist_ok=True)
1717

18-
# TODO: Too slow, fix it
19-
movie_clips_skipped = 0
20-
movie_clip_count = len(swf.movie_clips)
21-
for movie_clip_index in range(movie_clip_count):
22-
movie_clip = swf.movie_clips[movie_clip_index]
18+
shapes_count = len(swf.shapes)
2319

24-
rendered_movie_clip = create_renderable_from_plain(swf, movie_clip).render(
25-
Matrix2x3()
26-
)
27-
if sum(rendered_movie_clip.size) >= 2:
28-
clip_name = movie_clip.export_name or movie_clip.id
29-
rendered_movie_clip.save(f"{output_folder}/movie_clips/{clip_name}.png")
30-
else:
31-
# For debug:
32-
# logger.warning(f'MovieClip {movie_clip.id} cannot be rendered.')
33-
movie_clips_skipped += 1
20+
swf.xcod_writer.write_uint16(shapes_count)
21+
for shape_index in range(shapes_count):
22+
shape = swf.shapes[shape_index]
3423

35-
Console.progress_bar(
36-
"Rendering movie clips (%d/%d). Skipped count: %d"
37-
% (movie_clip_index + 1, movie_clip_count, movie_clips_skipped),
38-
movie_clip_index,
39-
movie_clip_count,
40-
)
24+
regions_count = len(shape.regions)
25+
swf.xcod_writer.write_uint16(shape.id)
26+
swf.xcod_writer.write_uint16(regions_count)
27+
for region_index in range(regions_count):
28+
region = shape.regions[region_index]
4129

42-
print()
30+
swf.xcod_writer.write_ubyte(region.texture_index)
31+
swf.xcod_writer.write_ubyte(region.get_point_count())
4332

44-
shapes_count = len(swf.shapes)
45-
swf.xcod_writer.write_uint16(shapes_count)
33+
for i in range(region.get_point_count()):
34+
swf.xcod_writer.write_uint16(int(region.get_u(i)))
35+
swf.xcod_writer.write_uint16(int(region.get_v(i)))
4636

4737
for shape_index in range(shapes_count):
4838
shape = swf.shapes[shape_index]
@@ -63,18 +53,27 @@ def render_objects(swf: SupercellSWF, output_folder: Path):
6353
rendered_region = region.get_image()
6454
rendered_region.save(f"{output_folder}/shape_{shape.id}_{region_index}.png")
6555

66-
for shape_index in range(shapes_count):
67-
shape = swf.shapes[shape_index]
56+
movie_clips_skipped = 0
57+
movie_clip_count = len(swf.movie_clips)
58+
for movie_clip_index in range(movie_clip_count):
59+
movie_clip = swf.movie_clips[movie_clip_index]
6860

69-
regions_count = len(shape.regions)
70-
swf.xcod_writer.write_uint16(shape.id)
71-
swf.xcod_writer.write_uint16(regions_count)
72-
for region_index in range(regions_count):
73-
region = shape.regions[region_index]
61+
rendered_movie_clip = create_renderable_from_plain(swf, movie_clip).render(
62+
Matrix2x3()
63+
)
64+
if sum(rendered_movie_clip.size) >= 2:
65+
clip_name = movie_clip.export_name or movie_clip.id
66+
rendered_movie_clip.save(f"{output_folder}/movie_clips/{clip_name}.png")
67+
else:
68+
# For debug:
69+
# logger.warning(f'MovieClip {movie_clip.id} cannot be rendered.')
70+
movie_clips_skipped += 1
7471

75-
swf.xcod_writer.write_ubyte(region.texture_index)
76-
swf.xcod_writer.write_ubyte(region.get_point_count())
72+
Console.progress_bar(
73+
"Rendering movie clips (%d/%d). Skipped count: %d"
74+
% (movie_clip_index + 1, movie_clip_count, movie_clips_skipped),
75+
movie_clip_index,
76+
movie_clip_count,
77+
)
7778

78-
for i in range(region.get_point_count()):
79-
swf.xcod_writer.write_uint16(int(region.get_u(i)))
80-
swf.xcod_writer.write_uint16(int(region.get_v(i)))
79+
print()

system/lib/features/place_sprites.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
from PIL import Image
55

6-
from system.lib import Console
6+
from system.lib.console import Console
77
from system.lib.images import create_filled_polygon_image, get_format_by_pixel_type
88
from system.lib.math.polygon import get_rect
99
from system.lib.xcod import FileInfo

0 commit comments

Comments
 (0)