Skip to content

Commit 34bc5f5

Browse files
refactor: pixel writing and reading separated into pixel_utils module, all pyright errors fixed
1 parent 85047da commit 34bc5f5

File tree

15 files changed

+360
-289
lines changed

15 files changed

+360
-289
lines changed

system/bytestream.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
11
import io
2-
from typing import Literal, Optional, Union
2+
from typing import Literal, Optional
33

44

55
class Reader(io.BytesIO):
66
def __init__(
77
self,
8-
buffer: bytes = b"",
9-
endian: Union[Literal["little"], Literal["big"]] = "little",
8+
initial_buffer: bytes = b"",
9+
endian: Literal["little", "big"] = "little",
1010
):
11-
super().__init__(buffer)
12-
13-
self.buffer = buffer
14-
self.endian = endian
11+
super().__init__(initial_buffer)
12+
self.endian: Literal["little", "big"] = endian
1513

1614
def read_integer(self, length: int, signed=False) -> int:
1715
return int.from_bytes(self.read(length), self.endian, signed=signed)
@@ -47,10 +45,10 @@ def read_string(self) -> str:
4745
class Writer(io.BytesIO):
4846
def __init__(self, endian: Literal["little", "big"] = "little"):
4947
super().__init__()
50-
self.endian = endian
48+
self._endian: Literal["little", "big"] = endian
5149

5250
def write_int(self, integer: int, length: int = 1, signed: bool = False):
53-
self.write(integer.to_bytes(length, self.endian, signed=signed))
51+
self.write(integer.to_bytes(length, self._endian, signed=signed))
5452

5553
def write_ubyte(self, integer: int):
5654
self.write_int(integer)

system/languages/en-EU.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
"decompression_error": "Error while decompressing! Trying to decode as is...",
5454
"skip_not_installed": "%s isn't installed! Reinitialize",
5555
"detected_comp": "%s compression detected",
56-
"unk_type": "Unknown pixel type: %s",
56+
"unknown_pixel_type": "Unknown pixel type: %s",
5757
"crt_pic": "Creating picture...",
5858
"join_pic": "Joining picture...",
5959
"png_save": "Saving to png...",

system/languages/ru-RU.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
"decompression_error": "Ошибка распаковки! Проведите повторную инициализацию!",
5454
"skip_not_installed": "%s не установлен! Пропускаем...",
5555
"detected_comp": "Обнаружено сжатие: %s",
56-
"unk_type": "Неизвестный подтип: %s",
56+
"unknown_pixel_type": "Неизвестный подтип: %s",
5757
"crt_pic": "Создаём картинку...",
5858
"join_pic": "Соединяем картинку...",
5959
"png_save": "Сохраняем в png...",

system/languages/ua-UA.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
"decompression_error": "Помилка при розпакуванні! Намагаємся розпакувати як є...",
5454
"skip_not_installed": "%s не встановлений, повторіть налаштування",
5555
"detected_comp": "Виявлено компресію: %s",
56-
"unk_type": "Невідомий вид пікселів: %s",
56+
"unknown_pixel_type": "Невідомий вид пікселів: %s",
5757
"crt_pic": "Створюємо зоображеня...",
5858
"join_pic": "З'єднуємо зоображення...",
5959
"png_save": "Зберігаємо PNG...",

system/lib/__init__.py

Lines changed: 42 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -89,82 +89,96 @@ def refill_menu():
8989
sc_category = Menu.Category(0, locale.sc_label)
9090
sc_category.add(
9191
Menu.Item(
92-
locale.decode_sc, locale.decode_sc_description, decode_textures_only
92+
name=locale.decode_sc,
93+
description=locale.decode_sc_description,
94+
handler=decode_textures_only,
9395
)
9496
)
9597
sc_category.add(
9698
Menu.Item(
97-
locale.encode_sc, locale.encode_sc_description, encode_textures_only
99+
name=locale.encode_sc,
100+
description=locale.encode_sc_description,
101+
handler=encode_textures_only,
98102
)
99103
)
100104
sc_category.add(
101105
Menu.Item(
102-
locale.decode_by_parts,
103-
locale.decode_by_parts_description,
104-
decode_and_render_objects,
106+
name=locale.decode_by_parts,
107+
description=locale.decode_by_parts_description,
108+
handler=decode_and_render_objects,
105109
)
106110
)
107111
sc_category.add(
108112
Menu.Item(
109-
locale.encode_by_parts,
110-
locale.encode_by_parts_description,
111-
collect_objects_and_encode,
113+
name=locale.encode_by_parts,
114+
description=locale.encode_by_parts_description,
115+
handler=collect_objects_and_encode,
112116
)
113117
)
114118
sc_category.add(
115119
Menu.Item(
116-
locale.overwrite_by_parts,
117-
locale.overwrite_by_parts_description,
118-
lambda: collect_objects_and_encode(True),
120+
name=locale.overwrite_by_parts,
121+
description=locale.overwrite_by_parts_description,
122+
handler=lambda: collect_objects_and_encode(True),
119123
)
120124
)
121125
menu.add_category(sc_category)
122126

123127
csv_category = Menu.Category(1, locale.csv_label)
124128
csv_category.add(
125129
Menu.Item(
126-
locale.decompress_csv, locale.decompress_csv_description, decompress_csv
130+
name=locale.decompress_csv,
131+
description=locale.decompress_csv_description,
132+
handler=decompress_csv,
127133
)
128134
)
129135
csv_category.add(
130136
Menu.Item(
131-
locale.compress_csv, locale.compress_csv_description, compress_csv
137+
name=locale.compress_csv,
138+
description=locale.compress_csv_description,
139+
handler=compress_csv,
132140
)
133141
)
134142
menu.add_category(csv_category)
135143

136144
other = Menu.Category(10, locale.other_features_label)
137145
other.add(
138-
Menu.Item(locale.check_update, locale.version % config.version, check_update)
146+
Menu.Item(
147+
name=locale.check_update,
148+
description=locale.version % config.version,
149+
handler=check_update,
150+
)
139151
)
140-
other.add(Menu.Item(locale.check_for_outdated, None, check_for_outdated))
152+
other.add(Menu.Item(name=locale.check_for_outdated, handler=check_for_outdated))
141153
other.add(
142154
Menu.Item(
143-
locale.reinit,
144-
locale.reinit_description,
145-
lambda: (initialize(), refill_menu()),
155+
name=locale.reinit,
156+
description=locale.reinit_description,
157+
handler=lambda: (initialize(), refill_menu()),
146158
)
147159
)
148160
other.add(
149161
Menu.Item(
150-
locale.change_language,
151-
locale.change_lang_description % config.language,
152-
lambda: (config.change_language(locale.change()), refill_menu()),
162+
name=locale.change_language,
163+
description=locale.change_lang_description % config.language,
164+
handler=lambda: (config.change_language(locale.change()), refill_menu()),
153165
)
154166
)
155167
other.add(
156168
Menu.Item(
157-
locale.clear_directories,
158-
locale.clean_dirs_description,
159-
lambda: clear_directories() if Console.question(locale.clear_qu) else -1,
169+
name=locale.clear_directories,
170+
description=locale.clean_dirs_description,
171+
handler=lambda: clear_directories()
172+
if Console.question(locale.clear_qu)
173+
else -1,
160174
)
161175
)
162176
other.add(
163177
Menu.Item(
164-
locale.toggle_update_auto_checking,
165-
locale.enabled if config.auto_update else locale.disabled,
166-
lambda: (config.toggle_auto_update(), refill_menu()),
178+
name=locale.toggle_update_auto_checking,
179+
description=locale.enabled if config.auto_update else locale.disabled,
180+
handler=lambda: (config.toggle_auto_update(), refill_menu()),
167181
)
168182
)
169-
other.add(Menu.Item(locale.exit, None, lambda: (clear(), exit())))
183+
other.add(Menu.Item(name=locale.exit, handler=lambda: (clear(), exit())))
170184
menu.add_category(other)

system/lib/features/sc/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from system.bytestream import Writer
99
from system.lib.console import Console
1010
from system.lib.features.files import write_sc
11-
from system.lib.images import get_pixel_size, save_texture, split_image
11+
from system.lib.images import get_byte_count_by_pixel_type, save_texture, split_image
1212
from system.lib.xcod import FileInfo
1313
from system.localization import locale
1414

@@ -38,7 +38,7 @@ def compile_sc(
3838
sheet = sheet.resize(sheet_info.size, Image.ANTIALIAS)
3939

4040
width, height = sheet.size
41-
pixel_size = get_pixel_size(pixel_type)
41+
pixel_size = get_byte_count_by_pixel_type(pixel_type)
4242

4343
file_size = width * height * pixel_size + 5
4444

system/lib/features/sc/decode.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ def decode_and_render_objects():
9393
print()
9494

9595

96-
def get_file_basename(swf):
96+
def get_file_basename(swf: SupercellSWF):
9797
return os.path.basename(swf.filename).rsplit(".", 1)[0]
9898

9999

system/lib/helper.py

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
from typing import List, Tuple, Union
1+
from typing import List, Tuple, TypeAlias
22

33
from system.lib.objects.point import Point
44

5+
PointType: TypeAlias = Tuple[float, float] | Tuple[int, int] | Point
6+
57

68
def get_size(left: float, top: float, right: float, bottom: float) -> Tuple[int, int]:
79
"""Returns width and height of given rect.
@@ -16,7 +18,7 @@ def get_size(left: float, top: float, right: float, bottom: float) -> Tuple[int,
1618

1719

1820
def get_sides(
19-
points: Union[List[Tuple[float, float]], List[Tuple[int, int]], List[Point]]
21+
points: List[Tuple[float, float]] | List[Tuple[int, int]] | List[Point]
2022
) -> Tuple[float, float, float, float]:
2123
"""Calculates and returns rect sides.
2224
@@ -25,17 +27,17 @@ def get_sides(
2527
"""
2628

2729
if len(points) > 0:
28-
point = points[0]
30+
point: PointType = points[0]
2931
if isinstance(point, Point):
30-
left = min(point.x for point in points)
31-
top = min(point.y for point in points)
32-
right = max(point.x for point in points)
33-
bottom = max(point.y for point in points)
32+
left = min(point.x for point in points) # type: ignore
33+
top = min(point.y for point in points) # type: ignore
34+
right = max(point.x for point in points) # type: ignore
35+
bottom = max(point.y for point in points) # type: ignore
3436
elif isinstance(point, tuple):
35-
left = min(x for x, _ in points)
36-
top = min(y for _, y in points)
37-
right = max(x for x, _ in points)
38-
bottom = max(y for _, y in points)
37+
left = min(x for x, _ in points) # type: ignore
38+
top = min(y for _, y in points) # type: ignore
39+
right = max(x for x, _ in points) # type: ignore
40+
bottom = max(y for _, y in points) # type: ignore
3941
else:
4042
raise TypeError("Unknown point type.")
4143

0 commit comments

Comments
 (0)