Skip to content

Commit

Permalink
features upload
Browse files Browse the repository at this point in the history
  • Loading branch information
danila-schelkov committed Oct 17, 2021
1 parent 7dd4ab6 commit 2f6957e
Show file tree
Hide file tree
Showing 9 changed files with 311 additions and 6 deletions.
11 changes: 5 additions & 6 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
SC/
CSV/
updates/
.idea/
logs/
/SC/
/CSV/
/updates/
/.idea/
/logs/
system/config.json
*.pyc
log.txt
Empty file.
28 changes: 28 additions & 0 deletions system/lib/features/csv/compress.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import os

from loguru import logger
from sc_compression import compress

from system.localization import locale


def compress_csv():
from sc_compression.signatures import Signatures

folder = './CSV/In-Decompressed'
folder_export = './CSV/Out-Compressed'

for file in os.listdir(folder):
if file.endswith('.csv'):
try:
with open(f'{folder}/{file}', 'rb') as f:
file_data = f.read()
f.close()

with open(f'{folder_export}/{file}', 'wb') as f:
f.write(compress(file_data, Signatures.LZMA))
f.close()
except Exception as exception:
logger.exception(locale.error % (exception.__class__.__module__, exception.__class__.__name__, exception))

print()
26 changes: 26 additions & 0 deletions system/lib/features/csv/decompress.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import os

from loguru import logger
from sc_compression import decompress

from system.localization import locale


def decompress_csv():
folder = './CSV/In-Compressed'
folder_export = './CSV/Out-Decompressed'

for file in os.listdir(folder):
if file.endswith('.csv'):
try:
with open(f'{folder}/{file}', 'rb') as f:
file_data = f.read()
f.close()

with open(f'{folder_export}/{file}', 'wb') as f:
f.write(decompress(file_data)[0])
f.close()
except Exception as exception:
logger.exception(locale.error % (exception.__class__.__module__, exception.__class__.__name__, exception))

print()
91 changes: 91 additions & 0 deletions system/lib/features/sc/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import os
import struct

from PIL import Image
from loguru import logger

from system.bytestream import Writer
from system.lib.console import Console
from system.lib.features.files import write_sc
from system.lib.images import get_pixel_size, pixel_type2str, split_image, rgba2bytes
from system.localization import locale


def compile_sc(_dir, from_memory=None, img_data=None, folder_export=None):
sc_data = None

name = _dir.split('/')[-2]
if from_memory:
files = from_memory
else:
files = []
[files.append(i) if i.endswith('.png') else None for i in os.listdir(_dir)]
files.sort()
if not files:
return logger.info(locale.dir_empty % _dir.split('/')[-2])
files = [Image.open(f'{_dir}{i}') for i in files]

logger.info(locale.collecting_inf)
sc = Writer()

has_xcod = False
use_lzham = False
if from_memory:
use_lzham = img_data['use_lzham']
else:
try:
sc_data = open(f'{_dir}/{name}.xcod', 'rb')
sc_data.read(4)
use_lzham, = struct.unpack('?', sc_data.read(1))
sc_data.read(1)
has_xcod = True
except OSError:
logger.info(locale.not_xcod)
logger.info(locale.default_types)

for picture_index in range(len(files)):
img = files[picture_index]
print()

if from_memory:
file_type = img_data['data'][picture_index]['file_type']
pixel_type = img_data['data'][picture_index]['pixel_type']
else:
if has_xcod:
file_type, pixel_type, width, height = struct.unpack('>BBHH', sc_data.read(6))

if (width, height) != img.size:
logger.info(locale.illegal_size % (width, height, img.width, img.height))
if Console.question(locale.resize_qu):
logger.info(locale.resizing)
img = img.resize((width, height), Image.ANTIALIAS)
else:
file_type, pixel_type = 1, 0

width, height = img.size
pixel_size = get_pixel_size(pixel_type)

img = img.convert('RGBA')
x = Image.new('RGBA', img.size, (0, 0, 0, 1))
x.paste(img, (0, 0), img)
img = x

img = img.convert(pixel_type2str(pixel_type))

file_size = width * height * pixel_size + 5

logger.info(locale.about_sc % (name, picture_index, pixel_type, width, height))

sc.write(struct.pack('<BIBHH', file_type, file_size, pixel_type, width, height))

if file_type in (27, 28):
split_image(img)
print()

rgba2bytes(sc, img, pixel_type)
print()

sc.write(bytes(5))
print()

write_sc(f'{folder_export}/{name}.sc', sc.getvalue(), use_lzham)
27 changes: 27 additions & 0 deletions system/lib/features/sc/assembly_encode.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import os

from loguru import logger

from system.lib.features.place_sprites import place_sprites
from system.lib.features.sc import compile_sc
from system.localization import locale


def sc1_encode(overwrite: bool = False):
folder = './SC/In-Sprites/'
folder_export = './SC/Out-Compressed/'
files = os.listdir(folder)

for file in files:
xcod = file + '.xcod'
if xcod not in os.listdir(f'{folder}{file}/'):
logger.error(locale.not_found % xcod)
else:
try:
logger.info(locale.dec_sc)
sheet_image, sheet_image_data = place_sprites(f'{folder}{file}/{xcod}', f'{folder}{file}', overwrite)
logger.info(locale.dec_sc)
compile_sc(f'{folder}{file}/', sheet_image, sheet_image_data, folder_export)
except Exception as exception:
logger.exception(locale.error % (exception.__class__.__module__, exception.__class__.__name__, exception))
print()
48 changes: 48 additions & 0 deletions system/lib/features/sc/decode.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import os
import shutil
import struct

from loguru import logger

from system.lib.swf import SupercellSWF
from system.localization import locale


def sc_decode():
folder = './SC/In-Compressed'
folder_export = './SC/Out-Decompressed'

files = os.listdir(folder)
for file in files:
if file.endswith('.sc'):
swf = SupercellSWF()
base_name = os.path.basename(file).rsplit('.', 1)[0]
try:
has_texture, use_lzham = swf.load_internal(f'{folder}/{file}', file.endswith('_tex.sc'))

if not has_texture:
base_name += '_tex'
file = base_name + '.sc'
if file in files:
files.remove(file)

has_texture, use_lzham = swf.load_internal(f'{folder}/{file}', True)
else:
continue

current_sub_path = file[::-1].split('.', 1)[1][::-1]
if os.path.isdir(f'{folder_export}/{current_sub_path}'):
shutil.rmtree(f'{folder_export}/{current_sub_path}')
os.mkdir(f'{folder_export}/{current_sub_path}')

data = struct.pack('4s?B', b'XCOD', use_lzham, len(swf.textures)) + swf.xcod_writer.getvalue()

with open(f'{folder_export}/{current_sub_path}/{base_name.rstrip("_")}.xcod', 'wb') as xc:
xc.write(data)
for img_index in range(len(swf.textures)):
filename = base_name + '_' * img_index
swf.textures[img_index].image.save(f'{folder_export}/{current_sub_path}/{filename}.png')
except Exception as exception:
logger.exception(locale.error % (exception.__class__.__module__, exception.__class__.__name__, exception))

print()
67 changes: 67 additions & 0 deletions system/lib/features/sc/decode_and_cut.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import os
import shutil

from loguru import logger

from system.lib.features.cut_sprites import cut_sprites
from system.lib.swf import SupercellSWF
from system.localization import locale


def sc1_decode():
folder = './SC/In-Compressed'
folder_export = './SC/Out-Sprites'
files = os.listdir(folder)

for file in files:
if not file.endswith('_tex.sc'):
xcod_file = None
try:
base_name = os.path.basename(file).rsplit('.', 1)[0]

logger.info(locale.dec_sc)

swf = SupercellSWF()
has_texture, use_lzham = swf.load_internal(f'{folder}/{file}', False)
if not has_texture:
file = base_name + '_tex.sc'
if file not in files:
logger.error(locale.not_found % file)
continue
_, use_lzham = swf.load_internal(f'{folder}/{file}', True)

current_sub_path = file[::-1].split('.', 1)[1][::-1]
if os.path.isdir(f'{folder_export}/{current_sub_path}'):
shutil.rmtree(f'{folder_export}/{current_sub_path}')
os.mkdir(f'{folder_export}/{current_sub_path}')
os.makedirs(f"{folder_export}/{current_sub_path}/textures", exist_ok=True)
base_name = os.path.basename(file).rsplit('.', 1)[0]

with open(f'{folder_export}/{current_sub_path}/{base_name}.xcod', 'wb') as xcod_file:
xcod_file.write(b'XCOD' + bool.to_bytes(use_lzham, 1, 'big') +
int.to_bytes(len(swf.textures), 1, 'big'))

for img_index in range(len(swf.textures)):
filename = base_name + '_' * img_index
swf.textures[img_index].image.save(
f'{folder_export}/{current_sub_path}/textures/{filename}.png'
)

logger.info(locale.dec_sc)

cut_sprites(
swf,
f'{folder_export}/{current_sub_path}'
)
xcod_file.write(swf.xcod_writer.getvalue())
except Exception as exception:
if xcod_file is not None:
xcod_file.close()

logger.exception(locale.error % (
exception.__class__.__module__,
exception.__class__.__name__,
exception
))

print()
19 changes: 19 additions & 0 deletions system/lib/features/sc/sc_encode.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import os

from loguru import logger

from system.lib.features.sc import compile_sc
from system.localization import locale


def sc_encode():
folder = './SC/In-Decompressed'
folder_export = './SC/Out-Compressed'

for file in os.listdir(folder):
try:
compile_sc(f'{folder}/{file}/', folder_export=folder_export)
except Exception as exception:
logger.exception(locale.error % (exception.__class__.__module__, exception.__class__.__name__, exception))

print()

0 comments on commit 2f6957e

Please sign in to comment.