Skip to content

Commit c8212a5

Browse files
Merge remote-tracking branch 'github-desktop-Yura3313/master' into pr/1
2 parents d2eaeaa + e75ff48 commit c8212a5

File tree

8 files changed

+160
-150
lines changed

8 files changed

+160
-150
lines changed

system/lib/features/files.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def write_sc(output_filename: str, buffer: bytes, use_lzham: bool):
2525

2626

2727
def open_sc(input_filename: str):
28-
decompressed = None
28+
decompressed_data = None
2929
use_lzham = False
3030

3131
logger.info(locale.collecting_inf)
@@ -36,7 +36,7 @@ def open_sc(input_filename: str):
3636
try:
3737
if b'START' in file_data:
3838
file_data = file_data[:file_data.index(b'START')]
39-
decompressed, signature = decompress(file_data)
39+
decompressed_data, signature = decompress(file_data)
4040
#
4141
# logger.info(locale.detected_comp % signature.upper())
4242
#
@@ -46,4 +46,4 @@ def open_sc(input_filename: str):
4646
logger.info(locale.decompression_error)
4747
exit(1)
4848

49-
return decompressed, use_lzham
49+
return decompressed_data, use_lzham

system/lib/features/sc/__init__.py

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

@@ -58,7 +58,7 @@ def compile_sc(_dir, file_info: FileInfo, sheets: list = None, output_folder: st
5858
split_image(img)
5959
print()
6060

61-
rgba2bytes(sc, img, pixel_type)
61+
save_texture(sc, img, pixel_type)
6262
print()
6363

6464
sc.write(bytes(5))

system/lib/features/sc/decode.py

Lines changed: 27 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -14,35 +14,30 @@ def sc_decode():
1414

1515
files = os.listdir(folder)
1616
for file in files:
17-
if file.endswith('.sc'):
18-
swf = SupercellSWF()
19-
base_name = os.path.basename(file).rsplit('.', 1)[0]
20-
try:
21-
has_texture, use_lzham = swf.load_internal(f'{folder}/{file}', file.endswith('_tex.sc'))
22-
23-
if not has_texture:
24-
base_name += '_tex'
25-
file = base_name + '.sc'
26-
if file in files:
27-
files.remove(file)
28-
29-
has_texture, use_lzham = swf.load_internal(f'{folder}/{file}', True)
30-
else:
31-
continue
32-
33-
current_sub_path = file[::-1].split('.', 1)[1][::-1]
34-
if os.path.isdir(f'{folder_export}/{current_sub_path}'):
35-
shutil.rmtree(f'{folder_export}/{current_sub_path}')
36-
os.mkdir(f'{folder_export}/{current_sub_path}')
37-
38-
data = struct.pack('4s?B', b'XCOD', use_lzham, len(swf.textures)) + swf.xcod_writer.getvalue()
39-
40-
with open(f'{folder_export}/{current_sub_path}/{base_name.rstrip("_")}.xcod', 'wb') as xc:
41-
xc.write(data)
42-
for img_index in range(len(swf.textures)):
43-
filename = base_name + '_' * img_index
44-
swf.textures[img_index].image.save(f'{folder_export}/{current_sub_path}/{filename}.png')
45-
except Exception as exception:
46-
logger.exception(locale.error % (exception.__class__.__module__, exception.__class__.__name__, exception))
47-
48-
print()
17+
if not file.endswith('.sc') or file.endswith('_tex.sc'):
18+
continue
19+
20+
swf = SupercellSWF()
21+
base_name = os.path.basename(file).rsplit('.', 1)[0]
22+
try:
23+
texture_loaded, use_lzham = swf.load(f'{folder}/{file}')
24+
25+
if not texture_loaded:
26+
continue
27+
28+
current_sub_path = os.path.basename(swf.filename).rsplit('.', 1)[0]
29+
if os.path.isdir(f'{folder_export}/{current_sub_path}'):
30+
shutil.rmtree(f'{folder_export}/{current_sub_path}')
31+
os.mkdir(f'{folder_export}/{current_sub_path}')
32+
33+
data = struct.pack('4s?B', b'XCOD', use_lzham, len(swf.textures)) + swf.xcod_writer.getvalue()
34+
35+
with open(f'{folder_export}/{current_sub_path}/{base_name.rstrip("_")}.xcod', 'wb') as xc:
36+
xc.write(data)
37+
for img_index in range(len(swf.textures)):
38+
filename = base_name + '_' * img_index
39+
swf.textures[img_index].image.save(f'{folder_export}/{current_sub_path}/{filename}.png')
40+
except Exception as exception:
41+
logger.exception(locale.error % (exception.__class__.__module__, exception.__class__.__name__, exception))
42+
43+
print()

system/lib/features/sc/decode_and_cut.py

Lines changed: 44 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -9,59 +9,58 @@
99

1010

1111
def sc1_decode():
12-
folder = './SC/In-Compressed'
13-
folder_export = './SC/Out-Sprites'
14-
files = os.listdir(folder)
12+
input_folder = './SC/In-Compressed'
13+
output_folder = './SC/Out-Sprites'
14+
files = os.listdir(input_folder)
1515

1616
for file in files:
17-
if not file.endswith('_tex.sc'):
18-
xcod_file = None
19-
try:
20-
base_name = os.path.basename(file).rsplit('.', 1)[0]
17+
if file.endswith('_tex.sc'):
18+
continue
2119

22-
logger.info(locale.dec_sc)
23-
24-
swf = SupercellSWF()
25-
has_texture, use_lzham = swf.load_internal(f'{folder}/{file}', False)
26-
if not has_texture:
27-
file = base_name + '_tex.sc'
28-
if file not in files:
29-
logger.error(locale.not_found % file)
30-
continue
31-
_, use_lzham = swf.load_internal(f'{folder}/{file}', True)
20+
xcod_file = None
21+
try:
22+
base_name = os.path.basename(file).rsplit('.', 1)[0]
3223

33-
current_sub_path = file[::-1].split('.', 1)[1][::-1]
34-
if os.path.isdir(f'{folder_export}/{current_sub_path}'):
35-
shutil.rmtree(f'{folder_export}/{current_sub_path}')
36-
os.mkdir(f'{folder_export}/{current_sub_path}')
37-
os.makedirs(f"{folder_export}/{current_sub_path}/textures", exist_ok=True)
38-
base_name = os.path.basename(file).rsplit('.', 1)[0]
24+
logger.info(locale.dec_sc)
3925

40-
with open(f'{folder_export}/{current_sub_path}/{base_name}.xcod', 'wb') as xcod_file:
41-
xcod_file.write(b'XCOD' + bool.to_bytes(use_lzham, 1, 'big') +
42-
int.to_bytes(len(swf.textures), 1, 'big'))
26+
swf = SupercellSWF()
27+
texture_loaded, use_lzham = swf.load(f'{input_folder}/{file}')
28+
if not texture_loaded:
29+
logger.error(locale.not_found % (base_name + '_tex.sc'))
30+
continue
4331

44-
for img_index in range(len(swf.textures)):
45-
filename = base_name + '_' * img_index
46-
swf.textures[img_index].image.save(
47-
f'{folder_export}/{current_sub_path}/textures/{filename}.png'
48-
)
32+
current_sub_path = os.path.basename(swf.filename).rsplit('.', 1)[0]
33+
if os.path.isdir(f'{output_folder}/{current_sub_path}'):
34+
shutil.rmtree(f'{output_folder}/{current_sub_path}')
35+
os.mkdir(f'{output_folder}/{current_sub_path}')
36+
os.makedirs(f"{output_folder}/{current_sub_path}/textures", exist_ok=True)
37+
base_name = os.path.basename(file).rsplit('.', 1)[0]
4938

50-
logger.info(locale.dec_sc)
39+
with open(f'{output_folder}/{current_sub_path}/{base_name}.xcod', 'wb') as xcod_file:
40+
xcod_file.write(b'XCOD' + bool.to_bytes(use_lzham, 1, 'big') +
41+
int.to_bytes(len(swf.textures), 1, 'big'))
5142

52-
cut_sprites(
53-
swf,
54-
f'{folder_export}/{current_sub_path}'
43+
for img_index in range(len(swf.textures)):
44+
filename = base_name + '_' * img_index
45+
swf.textures[img_index].image.save(
46+
f'{output_folder}/{current_sub_path}/textures/{filename}.png'
5547
)
56-
xcod_file.write(swf.xcod_writer.getvalue())
57-
except Exception as exception:
58-
if xcod_file is not None:
59-
xcod_file.close()
6048

61-
logger.exception(locale.error % (
62-
exception.__class__.__module__,
63-
exception.__class__.__name__,
64-
exception
65-
))
49+
logger.info(locale.dec_sc)
50+
51+
cut_sprites(
52+
swf,
53+
f'{output_folder}/{current_sub_path}'
54+
)
55+
xcod_file.write(swf.xcod_writer.getvalue())
56+
except Exception as exception:
57+
if xcod_file is not None:
58+
xcod_file.close()
59+
60+
logger.exception(locale.error % (
61+
exception.__class__.__module__,
62+
exception.__class__.__name__,
63+
exception
64+
))
6665

67-
print()
66+
print()

system/lib/images.py

Lines changed: 25 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -30,33 +30,22 @@ def join_image(img):
3030

3131
x_chunks_count = width // chunk_size
3232
y_chunks_count = height // chunk_size
33-
x_rest = width % chunk_size
34-
y_rest = height % chunk_size
3533

36-
for y_chunk in range(y_chunks_count):
37-
for x_chunk in range(x_chunks_count):
34+
for y_chunk in range(y_chunks_count + 1):
35+
for x_chunk in range(x_chunks_count + 1):
3836
for y in range(chunk_size):
37+
pixel_y = y_chunk * chunk_size + y
38+
if pixel_y >= height:
39+
break
40+
3941
for x in range(chunk_size):
40-
loaded_img[x_chunk * chunk_size + x, y_chunk * chunk_size + y] = tuple(
41-
pixel_buffer.read(channels_count)
42-
)
42+
pixel_x = x_chunk * chunk_size + x
43+
if pixel_x >= width:
44+
break
4345

44-
for y in range(chunk_size):
45-
for x in range(x_rest):
46-
loaded_img[(width - x_rest) + x, y_chunk * chunk_size + y] = tuple(
47-
pixel_buffer.read(channels_count)
48-
)
49-
Console.progress_bar(locale.join_pic, y_chunk, y_chunks_count)
50-
for x_chunk in range(x_chunks_count):
51-
for y in range(y_rest):
52-
for x in range(chunk_size):
53-
loaded_img[x_chunk * chunk_size + x, (height - y_rest) + y] = tuple(
54-
pixel_buffer.read(channels_count)
55-
)
46+
loaded_img[pixel_x, pixel_y] = tuple(pixel_buffer.read(channels_count))
5647

57-
for y in range(y_rest):
58-
for x in range(x_rest):
59-
loaded_img[x + (width - x_rest), y + (height - y_rest)] = tuple(pixel_buffer.read(channels_count))
48+
Console.progress_bar(locale.join_pic, y_chunk, y_chunks_count + 1)
6049

6150

6251
def split_image(img: Image):
@@ -70,34 +59,25 @@ def add_pixel(pixel: tuple):
7059

7160
x_chunks_count = width // chunk_size
7261
y_chunks_count = height // chunk_size
73-
x_rest = width % chunk_size
74-
y_rest = height % chunk_size
7562

7663
pixel_index = 0
7764

78-
for y_chunk in range(y_chunks_count):
79-
for x_chunk in range(x_chunks_count):
65+
for y_chunk in range(y_chunks_count + 1):
66+
for x_chunk in range(x_chunks_count + 1):
8067
for y in range(chunk_size):
81-
for x in range(chunk_size):
82-
add_pixel(loaded_clone[x + (x_chunk * chunk_size), y + (y_chunk * chunk_size)])
83-
pixel_index += 1
68+
pixel_y = (y_chunk * chunk_size) + y
69+
if pixel_y >= height:
70+
break
8471

85-
for y in range(chunk_size):
86-
for x in range(x_rest):
87-
add_pixel(loaded_clone[x + (width - x_rest), y + (y_chunk * chunk_size)])
88-
pixel_index += 1
89-
Console.progress_bar(locale.split_pic, y_chunk, y_chunks_count)
72+
for x in range(chunk_size):
73+
pixel_x = (x_chunk * chunk_size) + x
74+
if pixel_x >= width:
75+
break
9076

91-
for x_chunk in range(width // chunk_size):
92-
for y in range(y_rest):
93-
for x in range(chunk_size):
94-
add_pixel(loaded_clone[x + (x_chunk * chunk_size), y + (height - y_rest)])
95-
pixel_index += 1
77+
add_pixel(loaded_clone[pixel_x, pixel_y])
78+
pixel_index += 1
9679

97-
for y in range(y_rest):
98-
for x in range(x_rest):
99-
add_pixel(loaded_clone[x + (width - x_rest), y + (height - y_rest)])
100-
pixel_index += 1
80+
Console.progress_bar(locale.split_pic, y_chunk, y_chunks_count + 1)
10181

10282

10383
def get_pixel_size(_type):
@@ -123,7 +103,7 @@ def pixel_type2str(_type):
123103
raise Exception(locale.unk_type % _type)
124104

125105

126-
def bytes2rgba(data: Reader, _type, img):
106+
def load_texture(data: Reader, _type, img):
127107
read_pixel = None
128108
channels_count = 4
129109
if _type in (0, 1):
@@ -175,7 +155,7 @@ def read_pixel():
175155
print()
176156

177157

178-
def rgba2bytes(sc, img, _type):
158+
def save_texture(sc, img, _type):
179159
write_pixel = None
180160
if _type in (0, 1):
181161
def write_pixel(pixel):

system/lib/objects/shape.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -97,13 +97,15 @@ def load(self, swf, tag: int):
9797
self.shape_points = [_class() for _class in [Point] * self.points_count]
9898
self.sheet_points = [_class() for _class in [Point] * self.points_count]
9999

100+
multiplier = 0.5 if swf.use_lowres_texture else 1
101+
100102
for z in range(self.points_count):
101-
self.shape_points[z].x = swf.reader.read_int32() / 20
102-
self.shape_points[z].y = swf.reader.read_int32() / 20
103+
self.shape_points[z].x = swf.reader.read_int32() / 20 * multiplier
104+
self.shape_points[z].y = swf.reader.read_int32() / 20 * multiplier
103105
for z in range(self.points_count):
104-
w, h = [swf.reader.read_uint16() * swf.textures[self.texture_id].width / 0xffff,
105-
swf.reader.read_uint16() * swf.textures[self.texture_id].height / 0xffff]
106-
x, y = [ceil(i) for i in (w, h)]
106+
w, h = [swf.reader.read_uint16() * swf.textures[self.texture_id].width / 0xffff * multiplier,
107+
swf.reader.read_uint16() * swf.textures[self.texture_id].height / 0xffff * multiplier]
108+
x, y = map(ceil, (w, h))
107109
if int(w) == x:
108110
x += 1
109111
if int(h) == y:

system/lib/objects/texture.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from PIL import Image
44

5-
from system.lib.images import pixel_type2str, bytes2rgba, join_image, load_image_from_buffer
5+
from system.lib.images import pixel_type2str, load_texture, join_image, load_image_from_buffer
66

77

88
class SWFTexture:
@@ -21,7 +21,7 @@ def load(self, swf, tag: int, has_texture: bool):
2121
if has_texture:
2222
img = Image.new(pixel_type2str(self.pixel_type), (self.width, self.height))
2323

24-
bytes2rgba(swf.reader, self.pixel_type, img)
24+
load_texture(swf.reader, self.pixel_type, img)
2525

2626
if tag in (27, 28):
2727
join_image(img)

0 commit comments

Comments
 (0)