Skip to content

Commit 2f6957e

Browse files
features upload
1 parent 7dd4ab6 commit 2f6957e

File tree

9 files changed

+311
-6
lines changed

9 files changed

+311
-6
lines changed

.gitignore

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
SC/
2-
CSV/
3-
updates/
4-
.idea/
5-
logs/
1+
/SC/
2+
/CSV/
3+
/updates/
4+
/.idea/
5+
/logs/
66
system/config.json
77
*.pyc
8-
log.txt

system/lib/features/csv/__init__.py

Whitespace-only changes.

system/lib/features/csv/compress.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import os
2+
3+
from loguru import logger
4+
from sc_compression import compress
5+
6+
from system.localization import locale
7+
8+
9+
def compress_csv():
10+
from sc_compression.signatures import Signatures
11+
12+
folder = './CSV/In-Decompressed'
13+
folder_export = './CSV/Out-Compressed'
14+
15+
for file in os.listdir(folder):
16+
if file.endswith('.csv'):
17+
try:
18+
with open(f'{folder}/{file}', 'rb') as f:
19+
file_data = f.read()
20+
f.close()
21+
22+
with open(f'{folder_export}/{file}', 'wb') as f:
23+
f.write(compress(file_data, Signatures.LZMA))
24+
f.close()
25+
except Exception as exception:
26+
logger.exception(locale.error % (exception.__class__.__module__, exception.__class__.__name__, exception))
27+
28+
print()

system/lib/features/csv/decompress.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import os
2+
3+
from loguru import logger
4+
from sc_compression import decompress
5+
6+
from system.localization import locale
7+
8+
9+
def decompress_csv():
10+
folder = './CSV/In-Compressed'
11+
folder_export = './CSV/Out-Decompressed'
12+
13+
for file in os.listdir(folder):
14+
if file.endswith('.csv'):
15+
try:
16+
with open(f'{folder}/{file}', 'rb') as f:
17+
file_data = f.read()
18+
f.close()
19+
20+
with open(f'{folder_export}/{file}', 'wb') as f:
21+
f.write(decompress(file_data)[0])
22+
f.close()
23+
except Exception as exception:
24+
logger.exception(locale.error % (exception.__class__.__module__, exception.__class__.__name__, exception))
25+
26+
print()

system/lib/features/sc/__init__.py

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
import os
2+
import struct
3+
4+
from PIL import Image
5+
from loguru import logger
6+
7+
from system.bytestream import Writer
8+
from system.lib.console import Console
9+
from system.lib.features.files import write_sc
10+
from system.lib.images import get_pixel_size, pixel_type2str, split_image, rgba2bytes
11+
from system.localization import locale
12+
13+
14+
def compile_sc(_dir, from_memory=None, img_data=None, folder_export=None):
15+
sc_data = None
16+
17+
name = _dir.split('/')[-2]
18+
if from_memory:
19+
files = from_memory
20+
else:
21+
files = []
22+
[files.append(i) if i.endswith('.png') else None for i in os.listdir(_dir)]
23+
files.sort()
24+
if not files:
25+
return logger.info(locale.dir_empty % _dir.split('/')[-2])
26+
files = [Image.open(f'{_dir}{i}') for i in files]
27+
28+
logger.info(locale.collecting_inf)
29+
sc = Writer()
30+
31+
has_xcod = False
32+
use_lzham = False
33+
if from_memory:
34+
use_lzham = img_data['use_lzham']
35+
else:
36+
try:
37+
sc_data = open(f'{_dir}/{name}.xcod', 'rb')
38+
sc_data.read(4)
39+
use_lzham, = struct.unpack('?', sc_data.read(1))
40+
sc_data.read(1)
41+
has_xcod = True
42+
except OSError:
43+
logger.info(locale.not_xcod)
44+
logger.info(locale.default_types)
45+
46+
for picture_index in range(len(files)):
47+
img = files[picture_index]
48+
print()
49+
50+
if from_memory:
51+
file_type = img_data['data'][picture_index]['file_type']
52+
pixel_type = img_data['data'][picture_index]['pixel_type']
53+
else:
54+
if has_xcod:
55+
file_type, pixel_type, width, height = struct.unpack('>BBHH', sc_data.read(6))
56+
57+
if (width, height) != img.size:
58+
logger.info(locale.illegal_size % (width, height, img.width, img.height))
59+
if Console.question(locale.resize_qu):
60+
logger.info(locale.resizing)
61+
img = img.resize((width, height), Image.ANTIALIAS)
62+
else:
63+
file_type, pixel_type = 1, 0
64+
65+
width, height = img.size
66+
pixel_size = get_pixel_size(pixel_type)
67+
68+
img = img.convert('RGBA')
69+
x = Image.new('RGBA', img.size, (0, 0, 0, 1))
70+
x.paste(img, (0, 0), img)
71+
img = x
72+
73+
img = img.convert(pixel_type2str(pixel_type))
74+
75+
file_size = width * height * pixel_size + 5
76+
77+
logger.info(locale.about_sc % (name, picture_index, pixel_type, width, height))
78+
79+
sc.write(struct.pack('<BIBHH', file_type, file_size, pixel_type, width, height))
80+
81+
if file_type in (27, 28):
82+
split_image(img)
83+
print()
84+
85+
rgba2bytes(sc, img, pixel_type)
86+
print()
87+
88+
sc.write(bytes(5))
89+
print()
90+
91+
write_sc(f'{folder_export}/{name}.sc', sc.getvalue(), use_lzham)
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import os
2+
3+
from loguru import logger
4+
5+
from system.lib.features.place_sprites import place_sprites
6+
from system.lib.features.sc import compile_sc
7+
from system.localization import locale
8+
9+
10+
def sc1_encode(overwrite: bool = False):
11+
folder = './SC/In-Sprites/'
12+
folder_export = './SC/Out-Compressed/'
13+
files = os.listdir(folder)
14+
15+
for file in files:
16+
xcod = file + '.xcod'
17+
if xcod not in os.listdir(f'{folder}{file}/'):
18+
logger.error(locale.not_found % xcod)
19+
else:
20+
try:
21+
logger.info(locale.dec_sc)
22+
sheet_image, sheet_image_data = place_sprites(f'{folder}{file}/{xcod}', f'{folder}{file}', overwrite)
23+
logger.info(locale.dec_sc)
24+
compile_sc(f'{folder}{file}/', sheet_image, sheet_image_data, folder_export)
25+
except Exception as exception:
26+
logger.exception(locale.error % (exception.__class__.__module__, exception.__class__.__name__, exception))
27+
print()

system/lib/features/sc/decode.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import os
2+
import shutil
3+
import struct
4+
5+
from loguru import logger
6+
7+
from system.lib.swf import SupercellSWF
8+
from system.localization import locale
9+
10+
11+
def sc_decode():
12+
folder = './SC/In-Compressed'
13+
folder_export = './SC/Out-Decompressed'
14+
15+
files = os.listdir(folder)
16+
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()
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import os
2+
import shutil
3+
4+
from loguru import logger
5+
6+
from system.lib.features.cut_sprites import cut_sprites
7+
from system.lib.swf import SupercellSWF
8+
from system.localization import locale
9+
10+
11+
def sc1_decode():
12+
folder = './SC/In-Compressed'
13+
folder_export = './SC/Out-Sprites'
14+
files = os.listdir(folder)
15+
16+
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]
21+
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)
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+
os.makedirs(f"{folder_export}/{current_sub_path}/textures", exist_ok=True)
38+
base_name = os.path.basename(file).rsplit('.', 1)[0]
39+
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'))
43+
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+
)
49+
50+
logger.info(locale.dec_sc)
51+
52+
cut_sprites(
53+
swf,
54+
f'{folder_export}/{current_sub_path}'
55+
)
56+
xcod_file.write(swf.xcod_writer.getvalue())
57+
except Exception as exception:
58+
if xcod_file is not None:
59+
xcod_file.close()
60+
61+
logger.exception(locale.error % (
62+
exception.__class__.__module__,
63+
exception.__class__.__name__,
64+
exception
65+
))
66+
67+
print()

system/lib/features/sc/sc_encode.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import os
2+
3+
from loguru import logger
4+
5+
from system.lib.features.sc import compile_sc
6+
from system.localization import locale
7+
8+
9+
def sc_encode():
10+
folder = './SC/In-Decompressed'
11+
folder_export = './SC/Out-Compressed'
12+
13+
for file in os.listdir(folder):
14+
try:
15+
compile_sc(f'{folder}/{file}/', folder_export=folder_export)
16+
except Exception as exception:
17+
logger.exception(locale.error % (exception.__class__.__module__, exception.__class__.__name__, exception))
18+
19+
print()

0 commit comments

Comments
 (0)