Exemplo n.º 1
0
def process_path(sourcepath):
    sourcepath = os.path.normpath(sourcepath)
    basestem = os.path.splitext(os.path.basename(sourcepath))[0]
    outtxtpath = basestem + '.ini'
    outtplpath = basestem + '.tpl.txt'
    #print("[{0}]".format(sourcepath), file=stderr)
    with open(sourcepath, 'rb') as data:
        sig, size = unpack('<LL', data.read(0x8))
        status, decbuf = mzx0_decompress(data, os.path.getsize(sourcepath) - 8, size, xorff=True)
        if status != "OK": print("[{0}] {1}".format(sourcepath, status), file=stderr)
        fn = os.path.join("10rawscript", outtxtpath)
        with open(fn, 'wb') as dbg:
            dbg.write(decbuf)
        
        outcoll = []
        counter = 0
        for instr in decbuf.split(b';'):
            counter += 1
            instrtext = instr.decode('cp932')
            if re.search(r'_LVSV|_STTI|_MSAD|_ZM|SEL[R]', instrtext) is not None:
                outcoll.append("<{0:04d}>".format(counter) + instrtext.replace("^", "_r").replace("@n", "_n").replace(",", ";/")); # replace order significant
            elif len(re.sub('[ -~]', '', instrtext)) > 0:
                outcoll.append(u"!" + instrtext)  # flag missing matches containing non-ASCII characters
            else:
                outcoll.append(u"~" + instrtext + u"~")  # non-localizable

        if len(outcoll) > 0:
            fnprep = os.path.join("20decodedscript", outtplpath)
            with open(fnprep, 'wt', encoding="utf-8") as outfile:
                outfile.write(u"\n".join(outcoll))

    return status
Exemplo n.º 2
0
def extract_bin(file: Path, input_file, entries_descriptors, not_mzx):
    output_dir = file.with_name(file.name + '-unpacked').with_suffix('')

    if not output_dir.is_dir():
        output_dir.mkdir()

    for index, entry in enumerate(entries_descriptors):
        input_file.seek(entry.real_offset)
        data = input_file.read(entry.real_size)

        # Desc
        if index == 0:
            desc_file_name = output_dir.joinpath('0desc.bin')
            write_file(data, desc_file_name)
            continue

        file_name = 'tile' + str(index)
        if not_mzx:
            mzx_file_name = output_dir.joinpath(file_name + '.mzx')
            write_file(data, mzx_file_name)
        else:
            extract_file_name = output_dir.joinpath(file_name + '.ucp')
            input_file.seek(entry.real_offset)
            sig, size = unpack('<LL', input_file.read(0x8))
            status, extract_data = mzx0_decompress(input_file,
                                                   entry.real_size - 8, size)
            write_file(extract_data, extract_file_name)
Exemplo n.º 3
0
def extract_bin(file: Path, input_file, entries_descriptors, not_mzx):
    output_dir = file.with_name(file.name + '-unpacked').with_suffix('')

    if not output_dir.is_dir():
        output_dir.mkdir()

    for index, entry in enumerate(entries_descriptors):
        input_file.seek(entry.real_offset)
        data = input_file.read(entry.real_size)

        # Desc
        if index == 0:
            desc_file_name = output_dir.joinpath('0desc.bin')
            write_file(data, desc_file_name)
            continue

        file_name = 'tile' + str(index)
        if not_mzx:
            mzx_file_name = output_dir.joinpath(file_name + '.mzx')
            write_file(data, mzx_file_name)
        else:
            extract_file_name = output_dir.joinpath(file_name + '.ucp')
            input_file.seek(entry.real_offset)
            sig, size = unpack('<LL', input_file.read(0x8))
            status, extract_data = mzx0_decompress(input_file, entry.real_size - 8, size)
            write_file(extract_data, extract_file_name)
Exemplo n.º 4
0
    def extract_tile(self, index):
        entry = self.entries_descriptors[index]
        self.data.seek(entry.real_offset)
        sig, size = unpack('<LL', self.data.read(0x8))
        status, dec_buf = mzx0_decompress(self.data, entry.real_size - 8, size)
        dec_buf = dec_buf.read()
        if self.bitmap_bpp == 4:
            tile_data = b''
            for octet in dec_buf:
                the_byte = Byte(octet)
                tile_data += pack('BB', the_byte.low, the_byte.high)
            dec_buf = tile_data

        # RGB/RGBA true color for 0x08 and 0x0B bmp type
        elif self.bitmap_bpp in [24, 32] and self.bmp_type in [0x08, 0x0B]:
            # 16bpp
            tile_data = b''
            for index in range(self.tile_size):
                P = dec_buf[index * 2]
                Q = dec_buf[(index * 2) + 1]
                b = (P & 0x1f) << 3
                g = (Q & 0x07) << 5 | (P & 0xe0) >> 3
                r = (Q & 0xf8)

                # Offset for 16bpp to 24bpp
                offset_byte = dec_buf[self.tile_size * 2 + index]
                r_offset = offset_byte >> 5
                g_offset = (offset_byte & 0x1f) >> 3
                b_offset = offset_byte & 0x7

                # Alpha
                if self.bitmap_bpp == 32:
                    a = dec_buf[self.tile_size * 3 + index]
                    tile_data += pack('BBBB', r + r_offset, g + g_offset,
                                      b + b_offset, a)
                else:
                    tile_data += pack('BBB', r + r_offset, g + g_offset,
                                      b + b_offset)
            dec_buf = tile_data
        return dec_buf
Exemplo n.º 5
0
def process_path(source_path: Path):
    base_stem = source_path.stem
    out_txt = base_stem + '.ini'
    out_tpl = base_stem + '.tpl.txt'
    with source_path.open('rb') as data:
        sig, size = unpack('<LL', data.read(0x8))
        status, decbuf = mzx0_decompress(data,
                                         source_path.stat().st_size - 8,
                                         size,
                                         xorff=True)
        if status != "OK":
            print("[{0}] {1}".format(status, source_path), file=stderr)
        with raw_script_path.joinpath(out_txt).open('wb') as dbg:
            shutil.copyfileobj(decbuf, dbg)

        outcoll = []
        decbuf.seek(0)
        for index, instr in enumerate(decbuf.read().split(b';')):
            instrtext = instr.decode('cp932', 'surrogateescape')
            if re.search(r'_LVSV|_STTI|_MSAD|_ZM|SEL[R]',
                         instrtext) is not None:
                outcoll.append(
                    "<{0:04d}>".format(index) +
                    instrtext.replace("^", "_r").replace("@n", "_n").replace(
                        ",", ";/"))  # replace order significant
            elif len(re.sub('[ -~]', '', instrtext)) > 0:
                outcoll.append(
                    u"!" + instrtext
                )  # flag missing matches containing non-ASCII characters
            else:
                outcoll.append(u"~" + instrtext + u"~")  # non-localizable

        if outcoll:
            with decoded_script_path.joinpath(out_tpl).open(
                    'wt', encoding="cp932",
                    errors='surrogateescape') as outfile:
                outfile.write('\n'.join(outcoll))
    return status
Exemplo n.º 6
0
    def extract_tile(self, index):
        entry = self.entries_descriptors[index]
        self.data.seek(entry.real_offset)
        sig, size = unpack('<LL', self.data.read(0x8))
        status, dec_buf = mzx0_decompress(self.data, entry.real_size - 8, size)
        dec_buf = dec_buf.read()
        if self.bitmap_bpp == 4:
            tile_data = b''
            for octet in dec_buf:
                the_byte = Byte(octet)
                tile_data += pack('BB', the_byte.low, the_byte.high)
            dec_buf = tile_data

        # RGB/RGBA true color for 0x08 and 0x0B bmp type
        elif self.bitmap_bpp in [24, 32] and self.bmp_type in [0x08, 0x0B]:
            # 16bpp
            tile_data = b''
            for index in range(self.tile_size):
                P = dec_buf[index * 2]
                Q = dec_buf[(index * 2) + 1]
                b = (P & 0x1f) << 3
                g = (Q & 0x07) << 5 | (P & 0xe0) >> 3
                r = (Q & 0xf8)

                # Offset for 16bpp to 24bpp
                offset_byte = dec_buf[self.tile_size * 2 + index]
                r_offset = offset_byte >> 5
                g_offset = (offset_byte & 0x1f) >> 3
                b_offset = offset_byte & 0x7

                # Alpha
                if self.bitmap_bpp == 32:
                    a = dec_buf[self.tile_size * 3 + index]
                    tile_data += pack('BBBB', r + r_offset, g + g_offset, b + b_offset, a)
                else:
                    tile_data += pack('BBB', r + r_offset, g + g_offset, b + b_offset)
            dec_buf = tile_data
        return dec_buf
Exemplo n.º 7
0
#!/usr/bin/env python

import os
import shutil
from sys import stderr, argv
from struct import unpack
from pathlib import Path
from mzx.decomp_mzx0 import mzx0_decompress

if __name__ == '__main__':
    folder_path = Path(argv[1] if len(argv) > 1 else '.')
    for file_path in folder_path.glob('**/*.[Mm][Zz][Xx]'):
        out_path = file_path.with_suffix('.ini')
        with file_path.open('rb') as data:
            offset = 7 if(data.read(2) == b'LV') else 0
            data.seek(offset)
            sig, size = unpack('<LL', data.read(0x8))
            status, dec_buf = mzx0_decompress(data, os.path.getsize(file_path) - 8 - offset, size, True)
            if status != "OK":
                print("[{0}] {1}".format(file_path, status), file=stderr)
            with out_path.open('wb') as dbg:
                shutil.copyfileobj(dec_buf, dbg)
Exemplo n.º 8
0
#!/usr/bin/env python
import os, errno
import sys
from sys import stderr
from struct import unpack
import glob
import re
from mzx.decomp_mzx0 import mzx0_decompress

for filepath in glob.iglob('*.[Mm][Zz][Xx]'):
    basestem = os.path.splitext(os.path.basename(filepath))[0]
    outpath = basestem + '.out'
    with open(filepath, 'rb') as data:
        sig, size = unpack('<LL', data.read(0x8))
        status, decbuf = mzx0_decompress(data, os.path.getsize(filepath) - 8, size)
        if status != "OK": print("[{0}] {1}".format(filepath, status), file=stderr)
        with open(outpath, 'wb') as dbg:
            dbg.write(decbuf)