Skip to content

Commit

Permalink
refactor(meta info): signature added in xcod
Browse files Browse the repository at this point in the history
  • Loading branch information
danila-schelkov committed Aug 20, 2024
1 parent e706662 commit 0303531
Show file tree
Hide file tree
Showing 12 changed files with 60 additions and 62 deletions.
1 change: 0 additions & 1 deletion system/languages/en-EU.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@
"resizing": "Resizing...",
"split_pic": "Splitting picture...",
"writing_pic": "Writing pixels...",
"header_done": "Header wrote!",
"compressing_with": "Compressing texture with %s...",
"compression_error": "Compression failed",
"compression_done": "Compression done!",
Expand Down
1 change: 0 additions & 1 deletion system/languages/ru-RU.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@
"resizing": "Изменяем размер...",
"split_pic": "Разделяем картинку...",
"writing_pic": "Конвертируем пиксели...",
"header_done": "Заголовок записан!",
"compressing_with": "Сохраняем с применением %s сжатия...",
"compression_error": "Сжатие не удалось",
"compression_done": "Сжатие прошло успешно!",
Expand Down
1 change: 0 additions & 1 deletion system/languages/ua-UA.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@
"resizing": "змінюємо розмір...",
"split_pic": "Розділюємо зоображення...",
"writing_pic": "Записуємо пікселі...",
"header_done": "Написали Header!",
"compressing_with": "Запаковуємо з %s...",
"compression_error": "Запаковування не вдалося",
"compression_done": "Запаковування виконане!",
Expand Down
2 changes: 0 additions & 2 deletions system/lib/features/cut_sprites.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,5 +75,3 @@ def render_objects(swf: SupercellSWF, output_folder: Path):
movie_clip_index,
movie_clip_count,
)

print()
37 changes: 9 additions & 28 deletions system/lib/features/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,43 +7,24 @@
from system.localization import locale


def write_sc(output_filename: str | os.PathLike, buffer: bytes, use_lzham: bool):
def write_sc(
output_filename: str | os.PathLike,
buffer: bytes,
signature: Signatures,
version: int | None = None,
):
with open(output_filename, "wb") as file_out:
logger.info(locale.header_done)
file_out.write(compress(buffer, signature, version))

if use_lzham:
logger.info(locale.compressing_with % "LZHAM")
# Why is this here? It's included in the compression module
# file_out.write(struct.pack("<4sBI", b"SCLZ", 18, len(buffer)))
compressed = compress(buffer, Signatures.SCLZ)

file_out.write(compressed)
else:
logger.info(locale.compressing_with % "LZMA")
compressed = compress(buffer, Signatures.SC, 3)
file_out.write(compressed)
logger.info(locale.compression_done)
print()


def open_sc(input_filename: str) -> tuple[bytes, bool]:
use_lzham = False

def open_sc(input_filename: str) -> tuple[bytes, Signatures]:
with open(input_filename, "rb") as f:
file_data = f.read()

try:
if b"START" in file_data:
file_data = file_data[: file_data.index(b"START")]
decompressed_data, signature = decompress(file_data)

if signature.name != Signatures.NONE:
logger.info(locale.detected_comp % signature.name.upper())

if signature == Signatures.SCLZ:
use_lzham = True
return decompress(file_data)
except TypeError:
logger.info(locale.decompression_error)
exit(1)

return decompressed_data, use_lzham
10 changes: 9 additions & 1 deletion system/lib/features/sc/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,12 @@ def compile_sc(

sc.write(bytes(5))

write_sc(output_folder / f"{file_info.name}.sc", sc.getvalue(), file_info.use_lzham)
logger.info(locale.compressing_with % file_info.signature.name.upper())
write_sc(
output_folder / f"{file_info.name}.sc",
sc.getvalue(),
file_info.signature,
file_info.signature_version,
)
logger.info(locale.compression_done)
print()
28 changes: 18 additions & 10 deletions system/lib/features/sc/decode.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
from pathlib import Path

from loguru import logger
from sc_compression import Signatures

from system.bytestream import Writer
from system.lib.features.cut_sprites import render_objects
from system.lib.swf import SupercellSWF
from system.localization import locale
Expand All @@ -25,7 +27,7 @@ def decode_textures_only():
swf = SupercellSWF()
base_name = os.path.basename(file).rsplit(".", 1)[0]
try:
texture_loaded, use_lzham = swf.load(f"{input_folder / file}")
texture_loaded, signature = swf.load(f"{input_folder / file}")
if not texture_loaded:
logger.error(locale.not_found % f"{base_name}_tex.sc")
continue
Expand All @@ -37,7 +39,7 @@ def decode_textures_only():
)

_save_meta_file(
swf, objects_output_folder, base_name.rstrip("_"), use_lzham
swf, objects_output_folder, base_name.rstrip("_"), signature
)
_save_textures(swf, objects_output_folder, base_name)
except Exception as exception:
Expand Down Expand Up @@ -66,7 +68,7 @@ def decode_and_render_objects():
base_name = os.path.basename(file).rsplit(".", 1)[0]

swf = SupercellSWF()
texture_loaded, use_lzham = swf.load(input_folder / file)
texture_loaded, signature = swf.load(input_folder / file)
if not texture_loaded:
logger.error(locale.not_found % f"{base_name}_tex.sc")
continue
Expand All @@ -79,7 +81,7 @@ def decode_and_render_objects():

_save_textures(swf, objects_output_folder / "textures", base_name)
render_objects(swf, objects_output_folder)
_save_meta_file(swf, objects_output_folder, base_name, use_lzham)
_save_meta_file(swf, objects_output_folder, base_name, signature)
except Exception as exception:
logger.exception(
locale.error
Expand Down Expand Up @@ -113,10 +115,16 @@ def _save_textures(swf: SupercellSWF, textures_output: Path, base_name: str) ->


def _save_meta_file(
swf: SupercellSWF, objects_output_folder: Path, base_name: str, use_lzham: bool
swf: SupercellSWF,
objects_output_folder: Path,
base_name: str,
signature: Signatures,
) -> None:
with open(objects_output_folder / f"{base_name}.xcod", "wb") as xcod_file:
xcod_file.write(b"XCOD")
xcod_file.write(bool.to_bytes(use_lzham, 1, "big"))
xcod_file.write(int.to_bytes(len(swf.textures), 1, "big"))
xcod_file.write(swf.xcod_writer.getvalue())
writer = Writer()
writer.write(b"XCOD")
writer.write_string(signature.name)
writer.write_ubyte(len(swf.textures))
writer.write(swf.xcod_writer.getvalue())

with open(objects_output_folder / f"{base_name}.xcod", "wb") as file:
file.write(writer.getvalue())
1 change: 0 additions & 1 deletion system/lib/images.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,6 @@ def load_texture(reader: Reader, pixel_type: int, img: Image.Image) -> None:

with open("pixel_buffer", "wb") as pixel_buffer:
pixel_buffer.write(channel_count.to_bytes(1, "little"))
print()

width, height = img.size
point = -1
Expand Down
4 changes: 0 additions & 4 deletions system/lib/objects/shape/region.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,3 @@ def get_y(self, index: int):

def calculate_bounds(self, matrix: Matrix2x3 | None = None) -> Rect:
return get_rect(apply_matrix(self._xy_points, matrix))

@property
def xy_points(self):
return self._xy_points
23 changes: 15 additions & 8 deletions system/lib/swf.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from pathlib import Path

from loguru import logger
from sc_compression import Signatures

from system.bytestream import Reader, Writer
from system.lib.features.files import open_sc
Expand Down Expand Up @@ -53,32 +54,37 @@ def __init__(self):
self._matrix_banks: list[MatrixBank] = []
self._matrix_bank: MatrixBank | None = None

def load(self, filepath: str | os.PathLike) -> tuple[bool, bool]:
def load(self, filepath: str | os.PathLike) -> tuple[bool, Signatures]:
self._filepath = Path(filepath)

texture_loaded, use_lzham = self._load_internal(
texture_loaded, signature = self._load_internal(
self._filepath, self._filepath.name.endswith("_tex.sc")
)

if not texture_loaded:
if self._use_uncommon_texture:
texture_loaded, use_lzham = self._load_internal(
texture_loaded, signature = self._load_internal(
self._uncommon_texture_path, True
)
else:
texture_path = str(self._filepath)[:-3] + SupercellSWF.TEXTURE_EXTENSION
texture_loaded, use_lzham = self._load_internal(texture_path, True)
texture_loaded, signature = self._load_internal(texture_path, True)

return texture_loaded, use_lzham
return texture_loaded, signature

def _load_internal(
self, filepath: str | os.PathLike, is_texture_file: bool
) -> tuple[bool, bool]:
) -> tuple[bool, Signatures]:
self.filename = os.path.basename(filepath)

logger.info(locale.collecting_inf % self.filename)

decompressed_data, use_lzham = open_sc(filepath)
decompressed_data, signature = open_sc(filepath)

if signature.name != Signatures.NONE:
logger.info(locale.detected_comp % signature.name.upper())
print()

self.reader = Reader(decompressed_data)
del decompressed_data

Expand Down Expand Up @@ -127,7 +133,7 @@ def _load_internal(
if isinstance(movie_clip, MovieClip):
movie_clip.export_name = export_name

return loaded, use_lzham
return loaded, signature

def _load_tags(self, is_texture_file: bool) -> bool:
has_texture = True
Expand Down Expand Up @@ -163,6 +169,7 @@ def _load_tags(self, is_texture_file: bool) -> bool:
texture.height,
)
)
print()

self.xcod_writer.write_ubyte(tag)
self.xcod_writer.write_ubyte(texture.pixel_type)
Expand Down
13 changes: 9 additions & 4 deletions system/lib/xcod.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from pathlib import Path

from loguru import logger
from sc_compression import Signatures

from system.bytestream import Reader
from system.lib.math.point import Point
Expand Down Expand Up @@ -41,7 +42,8 @@ class ShapeInfo:
@dataclass
class FileInfo:
name: str
use_lzham: bool
signature: Signatures
signature_version: int | None
sheets: list[SheetInfo]
shapes: list[ShapeInfo]

Expand All @@ -55,7 +57,9 @@ def parse_info(metadata_file_path: Path, has_detailed_info: bool) -> FileInfo:

ensure_magic_known(reader)

file_info = FileInfo(os.path.splitext(metadata_file_path.name)[0], False, [], [])
file_info = FileInfo(
os.path.splitext(metadata_file_path.name)[0], Signatures.NONE, None, [], []
)
parse_base_info(file_info, reader)

if has_detailed_info:
Expand All @@ -65,8 +69,9 @@ def parse_info(metadata_file_path: Path, has_detailed_info: bool) -> FileInfo:


def parse_base_info(file_info: FileInfo, reader: Reader) -> None:
use_lzham = reader.read_uchar() == 1
file_info.use_lzham = use_lzham
file_info.signature = Signatures.SC
file_info.signature_version = 1 if reader.read_string() == "LZMA" else 3

sheets_count = reader.read_uchar()
for i in range(sheets_count):
file_type = reader.read_uchar()
Expand Down
1 change: 0 additions & 1 deletion system/localization.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ def __init__(self):
self.resizing: str = DEFAULT_STRING
self.split_pic: str = DEFAULT_STRING
self.writing_pic: str = DEFAULT_STRING
self.header_done: str = DEFAULT_STRING
self.compressing_with: str = DEFAULT_STRING
self.compression_error: str = DEFAULT_STRING
self.compression_done: str = DEFAULT_STRING
Expand Down

0 comments on commit 0303531

Please sign in to comment.