Skip to content

Commit

Permalink
Place sprites fix, refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
danila-schelkov committed Apr 17, 2022
1 parent 4784aa4 commit 747bea1
Show file tree
Hide file tree
Showing 10 changed files with 131 additions and 99 deletions.
19 changes: 9 additions & 10 deletions system/languages/en-EU.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"encode_sc_description": "Converts PNG to SC",
"decode_by_parts_description": "Converts SC in PNG and cuts texture to sprites",
"encode_by_parts_description": "Puts sprites on the texture and converts PNG in SC",
"overwrite_by_parts_description": "Repeats the action of the previous function but consider the \"overwrite\" folder",
"overwrite_by_parts_description": "Do the same as the previous but consider the \"overwrite\" folder",

"csv_label": "CSV",
"decompress_csv": "Decompress the CSV",
Expand All @@ -51,15 +51,14 @@
"collecting_inf": "Collecting information...",
"about_sc": "About texture. Filename: %s (%d), Pixel type: %d, Size: %sx%s",
"decompression_error": "Error while decompressing! Trying to decode as is...",
"skip_not_installed": "%s isn\"t installed! Reinitialize",
"skip_not_installed": "%s isn't installed! Reinitialize",
"detected_comp": "Detected %s compression!",
"unk_type": "Unknown pixel type: %s",
"crt_pic": "Creating picture...",
"join_pic": "Joining picture...",
"png_save": "Saving to png...",
"saved": "Saving completed!",
"not_xcod": ".xcod file doesn\"t exist!",
"default_types": "We will use default fileType and subType (1, 0).\n And also LZMA compression (as in Brawl Stars sc)",
"xcod_not_found": "File '%s.xcod' doesn't exist!",
"illegal_size": "Illegal image size! Expected %sx%s but we got %sx%s",
"resize_qu": "Would you like to resize an image?",
"resizing": "Resizing...",
Expand All @@ -69,16 +68,16 @@
"compressing_with": "Compressing texture with %s...",
"compression_error": "Compression failed",
"compression_done": "Compression done!",
"dir_empty": "Dir \"%s\" is empty!",
"not_found": "\"%s\" file isn\"t found!",
"cut_sprites_process": "Cutting sprites... (%s/%s)",
"place_sprites_process": "Placing sprites... (%s/%s)",
"dir_empty": "Dir '%s' is empty!",
"not_found": "File '%s' not found!",
"cut_sprites_process": "Cutting sprites... (%d/%d)",
"place_sprites_process": "Placing sprites... (%d/%d)",
"not_implemented": "This feature will be added in future updates.\nYou can follow XCoder updates here: github.com/Vorono4ka/XCoder",
"want_exit": "Want to exit?",
"dec_sc": "Decoding .sc file...",
"error": "ERROR! (%s.%s: %s)",
"e1sc1": "Overwrite SC sprites",
"cgl": "Changelog:\n%s\n",
"cgl": "Changelog:\n%s",
"upd_av": "\nUpdate is available!\nVersion: %s\n",
"upd_qu": "Do you want to update?",
"upd": "Updating...",
Expand All @@ -89,5 +88,5 @@
"enabled": "Enabled",
"disabled": "Disabled",

"install_to_unlock": "Install \"%s\" to unlock more functions!"
"install_to_unlock": "Install '%s' to unlock more functions!"
}
17 changes: 8 additions & 9 deletions system/languages/ru-RU.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"encode_sc_description": "Конвертирует PNG в SC",
"decode_by_parts_description": "Конвертирует SC в PNG и разрезает текстуру на части",
"encode_by_parts_description": "Собирает части текстуры и конвертирует PNG в SC",
"overwrite_by_parts_description": "Повторяет действие предыдущей функции, но учитывает папку \"overwrite\"",
"overwrite_by_parts_description": "Повторяет действие предыдущей функции, но учитывает папку 'overwrite'",

"csv_label": "CSV - Таблицы",
"decompress_csv": "Разжать CSV",
Expand Down Expand Up @@ -58,8 +58,7 @@
"join_pic": "Соединяем картинку...",
"png_save": "Сохраняем в png...",
"saved": "Сохранение прошло успешно!",
"not_xcod": ".xcod файл не обнаружен!",
"default_types": "Мы используем стандартные типы текстур (1, 0).\n И ещё LZMA сжатие (как в SC из Brawl Stars)",
"xcod_not_found": "Файл '%s.xcod' не обнаружен!",
"illegal_size": "Размер картинки не совпадает с оригиналом! Ожидалось %sx%s, но мы получили %sx%s",
"resize_qu": "Хотите изменить размер?",
"resizing": "Изменяем размер...",
Expand All @@ -69,16 +68,16 @@
"compressing_with": "Сохраняем с применением %s сжатия...",
"compression_error": "Сжатие не удалось",
"compression_done": "Сжатие прошло успешно!",
"dir_empty": "Папка \"%s\" пуста!",
"not_found": "Не был найден \"%s\" файл!",
"cut_sprites_process": "Вырезаем спрайты... (%s/%s)",
"place_sprites_process": "Ставим спрайты на место... (%s/%s)",
"dir_empty": "Папка '%s' пуста!",
"not_found": "Файл '%s' не найден!",
"cut_sprites_process": "Вырезаем спрайты... (%d/%d)",
"place_sprites_process": "Ставим спрайты на место... (%d/%d)",
"not_implemented": "Данная возможность будет добавлена в будущих обновлениях.\nЗа обновлениями XCoder вы можете следить здесь: github.com/Vorono4ka/XCoder",
"want_exit": "Хотите выйти?",
"dec_sc": "Декодируем SC...",
"error": "ОШИБКА! (%s.%s: %s)",
"e1sc1": "Перезапись спрайтов",
"cgl": "Список изменений:\n%s\n",
"cgl": "Список изменений: \n%s",
"upd_av": "\nДоступно обновление!\nВерсия: %s\n",
"upd_qu": "Обновить?",
"upd": "Обновляем...",
Expand All @@ -89,5 +88,5 @@
"enabled": "Включено",
"disabled": "Выключено",

"install_to_unlock": "Установите \"%s\" чтобы открыть больше функций!"
"install_to_unlock": "Установите '%s' чтобы открыть больше функций!"
}
67 changes: 32 additions & 35 deletions system/lib/features/place_sprites.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,52 +2,47 @@

from PIL import Image, ImageDraw

from system.bytestream import Reader
from system.lib import Console
from system.lib.images import pixel_type2str
from system.lib.xcod import parse_info, FileInfo
from system.localization import locale


def place_sprites(xcod, folder, overwrite=False):
xcod = Reader(open(xcod, 'rb').read(), 'big')
files = os.listdir(f'{folder}{"/overwrite" if overwrite else ""}')
tex = os.listdir(f'{folder}/textures')

xcod.read(4)
use_lzham, pictures_count = xcod.read_ubyte(), xcod.read_ubyte()
sheet_image = []
sheet_image_data = {'use_lzham': use_lzham, 'data': []}
for i in range(pictures_count):
file_type, sub_type, width, height = xcod.read_ubyte(), \
xcod.read_ubyte(), \
xcod.read_uint16(), \
xcod.read_uint16()
sheet_image.append(
Image.open(f'{folder}/textures/{tex[i]}')
def place_sprites(xcod_path: str, folder: str, overwrite: bool = False) -> (list, FileInfo):
file_info, xcod = parse_info(xcod_path)

files_to_overwrite = os.listdir(f'{folder}{"/overwrite" if overwrite else ""}')
texture_files = os.listdir(f'{folder}/textures')

sheets = []
for i in range(len(file_info.sheets)):
sheet_info = file_info.sheets[i]

sheets.append(
Image.open(f'{folder}/textures/{texture_files[i]}')
if overwrite else
Image.new('RGBA', (width, height)))
sheet_image_data['data'].append({'file_type': file_type, 'pixel_type': sub_type})
Image.new(pixel_type2str(sheet_info.pixel_type), sheet_info.size)
)

shapes_count = xcod.read_uint16()
for shape_index in range(shapes_count):
Console.progress_bar(locale.place_sprites_process % (shape_index + 1, shapes_count), shape_index, shapes_count)
shape_id = xcod.read_uint16()
regions_count = xcod.read_uint16()

regions_count = xcod.read_uint16()
for region_index in range(regions_count):
texture_id, points_count = xcod.read_ubyte(), xcod.read_ubyte()
texture_width, texture_height = sheet_image[texture_id].width, sheet_image[texture_id].height
texture_width, texture_height = sheets[texture_id].width, sheets[texture_id].height
polygon = [(xcod.read_uint16(), xcod.read_uint16()) for _ in range(points_count)]
mirroring, rotation = xcod.read_ubyte() == 1, xcod.read_ubyte() * 90

filename = f'shape_{shape_id}_{region_index}.png'
if filename not in files:
if filename not in files_to_overwrite:
continue

tmp_region = Image.open(
f'{folder}{"/overwrite" if overwrite else ""}/{filename}'
) \
.convert('RGBA') \
.rotate(360 - rotation, expand=True)
).convert('RGBA').rotate(360 - rotation, expand=True)

img_mask = Image.new('L', (texture_width, texture_height), 0)
color = 255
Expand All @@ -71,23 +66,25 @@ def place_sprites(xcod, folder, overwrite=False):
img_mask.putpixel((max_x - 1, max_y - 1), color)
bbox = img_mask.getbbox()

a, b, c, d = bbox
if c - a - 1:
c -= 1
if d - b - 1:
d -= 1
left, top, right, bottom = bbox
if right - left - 1:
right -= 1
if bottom - top - 1:
bottom -= 1

bbox = a, b, c, d
bbox = left, top, right, bottom

region_size = bbox[2] - bbox[0], bbox[3] - bbox[1]
width = right - left
height = bottom - top
region_size = width, height
tmp_region = tmp_region.resize(region_size, Image.ANTIALIAS)

if mirroring:
tmp_region = tmp_region.transform(region_size, Image.EXTENT,
(tmp_region.width, 0, 0, tmp_region.height))

sheet_image[texture_id].paste(Image.new('RGBA', region_size), bbox[:2], img_mask.crop(bbox))
sheet_image[texture_id].paste(tmp_region, bbox[:2], tmp_region)
sheets[texture_id].paste(Image.new('RGBA', region_size), (left, top), img_mask.crop(bbox))
sheets[texture_id].paste(tmp_region, (left, top), tmp_region)
print()

return sheet_image, sheet_image_data
return sheets, file_info
51 changes: 17 additions & 34 deletions system/lib/features/sc/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@
from system.lib.console import Console
from system.lib.features.files import write_sc
from system.lib.images import get_pixel_size, split_image, rgba2bytes
from system.lib.xcod import FileInfo
from system.localization import locale


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

def compile_sc(_dir, file_info: FileInfo, sheets: list = None, output_folder: str = None):
name = _dir.split('/')[-2]
if from_memory:
files = from_memory

if sheets:
files = sheets
else:
files = []
[files.append(i) if i.endswith('.png') else None for i in os.listdir(_dir)]
Expand All @@ -28,39 +28,22 @@ def compile_sc(_dir, from_memory=None, img_data=None, folder_export=None):
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)
use_lzham = file_info.use_lzham

for picture_index in range(len(files)):
sheet_info = file_info.sheets[picture_index]
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
file_type = sheet_info.file_type
pixel_type = sheet_info.pixel_type

if img.size != sheet_info.size:
logger.info(locale.illegal_size % (sheet_info.width, sheet_info.height, img.width, img.height))

if Console.question(locale.resize_qu):
logger.info(locale.resizing)
img = img.resize(sheet_info.size, Image.ANTIALIAS)

width, height = img.size
pixel_size = get_pixel_size(pixel_type)
Expand All @@ -81,4 +64,4 @@ def compile_sc(_dir, from_memory=None, img_data=None, folder_export=None):
sc.write(bytes(5))
print()

write_sc(f'{folder_export}/{name}.sc', sc.getvalue(), use_lzham)
write_sc(f'{output_folder}/{name}.sc', sc.getvalue(), use_lzham)
6 changes: 3 additions & 3 deletions system/lib/features/sc/assembly_encode.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

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

for file in files:
Expand All @@ -19,9 +19,9 @@ def sc1_encode(overwrite: bool = False):
else:
try:
logger.info(locale.dec_sc)
sheet_image, sheet_image_data = place_sprites(f'{folder}{file}/{xcod}', f'{folder}{file}', overwrite)
sheets, file_info = 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)
compile_sc(f'{folder}{file}/', file_info, sheets, output_folder)
except Exception as exception:
logger.exception(locale.error % (exception.__class__.__module__, exception.__class__.__name__, exception))
print()
15 changes: 10 additions & 5 deletions system/lib/features/sc/sc_encode.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,22 @@
from loguru import logger

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


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

for file in os.listdir(folder):
for folder in os.listdir(input_folder):
try:
compile_sc(f'{folder}/{file}/', folder_export=folder_export)
file_info, _ = parse_info(f'{input_folder}/{folder}/{folder}.xcod')

compile_sc(f'{input_folder}/{folder}/', file_info, output_folder=output_folder)
except FileNotFoundError:
logger.info(locale.xcod_not_found % folder)
except Exception as exception:
logger.exception(locale.error % (exception.__class__.__module__, exception.__class__.__name__, exception))

print()
print()
1 change: 1 addition & 0 deletions system/lib/features/update/check.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ def check_update():

check_for_outdated()

logger.info(locale.check_update)
if config.version != latest_tag_name:
logger.error(locale.not_latest)

Expand Down
2 changes: 1 addition & 1 deletion system/lib/images.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ def get_pixel_size(_type):


def pixel_type2str(_type):
if _type in range(4):
if _type in (0, 1, 2, 3):
return 'RGBA'
elif _type == 4:
return 'RGB'
Expand Down
Loading

0 comments on commit 747bea1

Please sign in to comment.