21
21
import subprocess
22
22
import colorama
23
23
import tempfile
24
-
25
- from sc_compression .decompressor import Decompressor
26
- from sc_compression .compressor import Compressor
27
- from sc_compression .signatures import Signatures
28
24
29
25
from PIL import Image , ImageDraw
26
+
27
+ from sc_compression .signatures import Signatures
28
+ from sc_compression import decompress , compress
30
29
except Exception as e :
31
30
logger .write (e )
32
31
@@ -109,20 +108,6 @@ def colored_print(text, color=None):
109
108
return print (color + colorama .Fore .BLACK + text + ' ' * (10 - len (text )) + colorama .Style .RESET_ALL )
110
109
111
110
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
-
126
111
def welcome_text ():
127
112
load_locale ()
128
113
@@ -159,6 +144,15 @@ def welcome_text():
159
144
return choice
160
145
161
146
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
+
162
156
def get_tags (owner : str , repo : str ):
163
157
api_url = 'https://api.github.com'
164
158
tags = []
@@ -183,6 +177,14 @@ def check_update():
183
177
if len (tags ) > 0 :
184
178
latest_tag = tags [0 ]
185
179
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
+
186
188
if config ['version' ] != latest_tag_name :
187
189
Console .error (locale .not_latest )
188
190
@@ -214,7 +216,6 @@ def download_update(zip_url):
214
216
215
217
216
218
def decompress_csv ():
217
- global errors
218
219
folder = './CSV/In-Compressed'
219
220
folder_export = './CSV/Out-Decompressed'
220
221
@@ -226,10 +227,9 @@ def decompress_csv():
226
227
f .close ()
227
228
228
229
with open (f'{ folder_export } /{ file } ' , 'wb' ) as f :
229
- f .write (decompress (file_data ))
230
+ f .write (decompress (file_data )[ 0 ] )
230
231
f .close ()
231
232
except Exception as exception :
232
- errors += 1
233
233
Console .error (locale .error % (exception .__class__ .__module__ , exception .__class__ .__name__ , exception ))
234
234
logger .write (traceback .format_exc ())
235
235
@@ -239,7 +239,6 @@ def decompress_csv():
239
239
def compress_csv ():
240
240
from sc_compression .signatures import Signatures
241
241
242
- global errors
243
242
folder = './CSV/In-Decompressed'
244
243
folder_export = './CSV/Out-Compressed'
245
244
@@ -253,6 +252,48 @@ def compress_csv():
253
252
with open (f'{ folder_export } /{ file } ' , 'wb' ) as f :
254
253
f .write (compress (file_data , Signatures .LZMA ))
255
254
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' )
256
297
except Exception as exception :
257
298
errors += 1
258
299
Console .error (locale .error % (exception .__class__ .__module__ , exception .__class__ .__name__ , exception ))
@@ -261,6 +302,107 @@ def compress_csv():
261
302
print ()
262
303
263
304
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
+
264
406
def get_pixel_size (_type ):
265
407
if _type in (0 , 1 ):
266
408
return 4
@@ -441,8 +583,8 @@ def write_sc(output_filename: str, buffer: bytes, use_lzham: bool):
441
583
442
584
file_out .write (compressed )
443
585
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 )
446
588
file_out .write (compressed )
447
589
Console .info (locale .compression_done )
448
590
0 commit comments