Skip to content

Commit 3df53a8

Browse files
Changed ZSTD to LZMA cuz compress into ZSTD doesn't work.
And some refactoring
1 parent afecab3 commit 3df53a8

File tree

5 files changed

+170
-183
lines changed

5 files changed

+170
-183
lines changed

lib.py

Lines changed: 166 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,11 @@
2121
import subprocess
2222
import colorama
2323
import tempfile
24-
25-
from sc_compression.decompressor import Decompressor
26-
from sc_compression.compressor import Compressor
27-
from sc_compression.signatures import Signatures
2824

2925
from PIL import Image, ImageDraw
26+
27+
from sc_compression.signatures import Signatures
28+
from sc_compression import decompress, compress
3029
except Exception as e:
3130
logger.write(e)
3231

@@ -109,20 +108,6 @@ def colored_print(text, color=None):
109108
return print(color + colorama.Fore.BLACK + text + ' ' * (10 - len(text)) + colorama.Style.RESET_ALL)
110109

111110

112-
def decompress(buffer: bytes) -> (bytes, int):
113-
decompressor = Decompressor()
114-
decompressed = decompressor.decompress(buffer)
115-
116-
return decompressed, decompressor.signatures.last_signature
117-
118-
119-
def compress(buffer: bytes, signature: int) -> bytes:
120-
compressor = Compressor()
121-
compressed = compressor.compress(buffer, signature)
122-
123-
return compressed
124-
125-
126111
def welcome_text():
127112
load_locale()
128113

@@ -159,6 +144,15 @@ def welcome_text():
159144
return choice
160145

161146

147+
def get_pip_info(outdated: bool = False) -> list:
148+
output = get_run_output(f'pip --disable-pip-version-check list {"-o" if outdated else ""}')
149+
output = output.splitlines()
150+
output = output[2:]
151+
packages = [package.split() for package in output]
152+
153+
return packages
154+
155+
162156
def get_tags(owner: str, repo: str):
163157
api_url = 'https://api.github.com'
164158
tags = []
@@ -183,6 +177,14 @@ def check_update():
183177
if len(tags) > 0:
184178
latest_tag = tags[0]
185179
latest_tag_name = latest_tag['name'][1:] # clear char 'v' at string start
180+
181+
Console.info(locale.check_for_outdated)
182+
required_packages = [pkg.rstrip('\n').lower() for pkg in open('requirements.txt').readlines()]
183+
outdated_packages = [pkg[0].lower() for pkg in get_pip_info(True)]
184+
for package in required_packages:
185+
if package in outdated_packages:
186+
run(f'pip3 install --upgrade {package}')
187+
186188
if config['version'] != latest_tag_name:
187189
Console.error(locale.not_latest)
188190

@@ -214,7 +216,6 @@ def download_update(zip_url):
214216

215217

216218
def decompress_csv():
217-
global errors
218219
folder = './CSV/In-Compressed'
219220
folder_export = './CSV/Out-Decompressed'
220221

@@ -226,10 +227,9 @@ def decompress_csv():
226227
f.close()
227228

228229
with open(f'{folder_export}/{file}', 'wb') as f:
229-
f.write(decompress(file_data))
230+
f.write(decompress(file_data)[0])
230231
f.close()
231232
except Exception as exception:
232-
errors += 1
233233
Console.error(locale.error % (exception.__class__.__module__, exception.__class__.__name__, exception))
234234
logger.write(traceback.format_exc())
235235

@@ -239,7 +239,6 @@ def decompress_csv():
239239
def compress_csv():
240240
from sc_compression.signatures import Signatures
241241

242-
global errors
243242
folder = './CSV/In-Decompressed'
244243
folder_export = './CSV/Out-Compressed'
245244

@@ -253,6 +252,48 @@ def compress_csv():
253252
with open(f'{folder_export}/{file}', 'wb') as f:
254253
f.write(compress(file_data, Signatures.LZMA))
255254
f.close()
255+
except Exception as exception:
256+
Console.error(locale.error % (exception.__class__.__module__, exception.__class__.__name__, exception))
257+
logger.write(traceback.format_exc())
258+
259+
print()
260+
261+
262+
def sc_decode():
263+
global errors
264+
folder = './SC/In-Compressed'
265+
folder_export = './SC/Out-Decompressed'
266+
267+
files = os.listdir(folder)
268+
for file in files:
269+
if file.endswith('.sc'):
270+
swf = SupercellSWF()
271+
base_name = os.path.basename(file).rsplit('.', 1)[0]
272+
try:
273+
has_texture, use_lzham = swf.load_internal(f'{folder}/{file}', file.endswith('_tex.sc'))
274+
275+
if not has_texture:
276+
base_name += '_tex'
277+
file = base_name + '.sc'
278+
if file in files:
279+
files.remove(file)
280+
281+
has_texture, use_lzham = swf.load_internal(f'{folder}/{file}', True)
282+
else:
283+
continue
284+
285+
current_sub_path = file[::-1].split('.', 1)[1][::-1]
286+
if os.path.isdir(f'{folder_export}/{current_sub_path}'):
287+
shutil.rmtree(f'{folder_export}/{current_sub_path}')
288+
os.mkdir(f'{folder_export}/{current_sub_path}')
289+
290+
data = struct.pack('4s?B', b'XCOD', use_lzham, len(swf.textures)) + swf.xcod_writer.getvalue()
291+
292+
with open(f'{folder_export}/{current_sub_path}/{base_name.rstrip("_")}.xcod', 'wb') as xc:
293+
xc.write(data)
294+
for img_index in range(len(swf.textures)):
295+
filename = base_name + '_' * img_index
296+
swf.textures[img_index].image.save(f'{folder_export}/{current_sub_path}/{filename}.png')
256297
except Exception as exception:
257298
errors += 1
258299
Console.error(locale.error % (exception.__class__.__module__, exception.__class__.__name__, exception))
@@ -261,6 +302,107 @@ def compress_csv():
261302
print()
262303

263304

305+
def sc_encode():
306+
global errors
307+
folder = './SC/In-Decompressed'
308+
folder_export = './SC/Out-Compressed'
309+
310+
for file in os.listdir(folder):
311+
try:
312+
compile_sc(f'{folder}/{file}/', folder_export=folder_export)
313+
except Exception as exception:
314+
errors += 1
315+
Console.error(locale.error % (exception.__class__.__module__, exception.__class__.__name__, exception))
316+
logger.write(traceback.format_exc())
317+
318+
print()
319+
320+
321+
def sc1_decode():
322+
global errors
323+
folder = './SC/In-Compressed'
324+
folder_export = './SC/Out-Sprites'
325+
files = os.listdir(folder)
326+
327+
for file in files:
328+
if not file.endswith('_tex.sc'):
329+
xc = None
330+
try:
331+
base_name = os.path.basename(file).rsplit('.', 1)[0]
332+
333+
Console.info(locale.dec_sc)
334+
335+
swf = SupercellSWF()
336+
has_texture, use_lzham = swf.load_internal(f'{folder}/{file}', False)
337+
if not has_texture:
338+
file = base_name + '_tex.sc'
339+
if file not in files:
340+
Console.error(locale.not_found % file)
341+
continue
342+
_, use_lzham = swf.load_internal(f'{folder}/{file}', True)
343+
344+
current_sub_path = file[::-1].split('.', 1)[1][::-1]
345+
if os.path.isdir(f'{folder_export}/{current_sub_path}'):
346+
shutil.rmtree(f'{folder_export}/{current_sub_path}')
347+
os.mkdir(f'{folder_export}/{current_sub_path}')
348+
os.makedirs(f"{folder_export}/{current_sub_path}/textures", exist_ok=True)
349+
base_name = os.path.basename(file).rsplit('.', 1)[0]
350+
351+
xc = open(f'{folder_export}/{current_sub_path}/{base_name}.xcod', 'wb')
352+
data = struct.pack('4s?B', b'XCOD', use_lzham, len(swf.textures)) + swf.xcod_writer.getvalue()
353+
354+
for img_index in range(len(swf.textures)):
355+
filename = base_name + '_' * img_index
356+
swf.textures[img_index].image.save(f'{folder_export}/{current_sub_path}/textures/{filename}.png')
357+
358+
xc.write(data)
359+
360+
Console.info(locale.dec_sc)
361+
362+
cut_sprites(
363+
swf.movie_clips,
364+
swf.textures,
365+
xc,
366+
f'{folder_export}/{current_sub_path}'
367+
)
368+
except Exception as exception:
369+
if xc is not None:
370+
xc.close()
371+
372+
errors += 1
373+
Console.error(locale.error % (
374+
exception.__class__.__module__,
375+
exception.__class__.__name__,
376+
exception
377+
))
378+
logger.write(traceback.format_exc())
379+
380+
print()
381+
382+
383+
def sc1_encode(overwrite: bool = False):
384+
global errors
385+
folder = './SC/In-Sprites/'
386+
folder_export = './SC/Out-Compressed/'
387+
files = os.listdir(folder)
388+
389+
for file in files:
390+
xcod = file + '.xcod'
391+
if xcod not in os.listdir(f'{folder}{file}/'):
392+
Console.error(locale.not_found % xcod)
393+
else:
394+
try:
395+
Console.info(locale.dec_sc)
396+
sheet_image, sheet_image_data = place_sprites(f'{folder}{file}/{xcod}', f'{folder}{file}', overwrite)
397+
Console.info(locale.dec_sc)
398+
compile_sc(f'{folder}{file}/', sheet_image, sheet_image_data, folder_export)
399+
except Exception as exception:
400+
errors += 1
401+
Console.error(locale.error % (exception.__class__.__module__, exception.__class__.__name__, exception))
402+
logger.write(traceback.format_exc())
403+
print()
404+
405+
264406
def get_pixel_size(_type):
265407
if _type in (0, 1):
266408
return 4
@@ -441,8 +583,8 @@ def write_sc(output_filename: str, buffer: bytes, use_lzham: bool):
441583

442584
file_out.write(compressed)
443585
else:
444-
Console.info(locale.compressing_with % 'ZSTD')
445-
compressed = compress(buffer, Signatures.SC)
586+
Console.info(locale.compressing_with % 'LZMA')
587+
compressed = compress(buffer, Signatures.SC, 1)
446588
file_out.write(compressed)
447589
Console.info(locale.compression_done)
448590

0 commit comments

Comments
 (0)