示例#1
0
def compressLIBYAZ0(inb, outf, level=1):
    """
    Compress the file using libyaz0
    """
    try:
        data = compress(inb, 0, level)

        with open(outf, "wb+") as out:
            out.write(data)

    except:
        return False

    else:
        return True
示例#2
0
from texttable import Texttable

import libyaz0
import oead
import wszst_yaz0
from files import TEST_FILES, TEST_FILE_DATA, TEST_FILE_DATA_UNCOMP

ENCODERS = {
    "libyaz0_lv1": lambda data: libyaz0.compress(data, level=1),
    "libyaz0_lv6": lambda data: libyaz0.compress(data, level=6),
    "libyaz0_lv9": lambda data: libyaz0.compress(data, level=9),
    "libyaz0_lv10": lambda data: libyaz0.compress(data, level=10),
    "syaz0_lv6": lambda data: oead.yaz0.compress(data, level=6),
    "syaz0_lv7": lambda data: oead.yaz0.compress(data, level=7),
    "syaz0_lv8": lambda data: oead.yaz0.compress(data, level=8),
    "syaz0_lv9": lambda data: oead.yaz0.compress(data, level=9),
    "wszst_yaz0_lv1": lambda data: wszst_yaz0.compress(data, level=1),
    "wszst_yaz0_lv6": lambda data: wszst_yaz0.compress(data, level=6),
    "wszst_yaz0_lv9": lambda data: wszst_yaz0.compress(data, level=9),
    "wszst_yaz0_lv10": lambda data: wszst_yaz0.compress(data, level=10),
}


def test_file(name) -> None:
    print(f">>>>>>>>>> {name} <<<<<<<<<<")
    print()

    t = Texttable(max_width=100)
    t.set_deco(Texttable.BORDER | Texttable.HEADER | Texttable.VLINES)
    t.set_precision(3)
    t.header(["Encoder", "Size", "Diff", "Diff%"])
示例#3
0
def pack(root, endianness, level, outname):
    """
    Pack the files and folders in the root folder.
    """

    if "\\" in root:
        root = "/".join(root.split("\\"))

    if root[-1] == "/":
        root = root[:-1]

    arc = SarcLib.SARC_Archive(endianness=endianness)
    lenroot = len(root.split("/"))

    for path, dirs, files in os.walk(root):
        if "\\" in path:
            path = "/".join(path.split("\\"))

        lenpath = len(path.split("/"))

        if lenpath == lenroot:
            path = ""

        else:
            path = "/".join(path.split("/")[lenroot - lenpath:])

        for file in files:
            if path:
                filename = ''.join([path, "/", file])

            else:
                filename = file

            print(filename)

            fullname = ''.join([root, "/", filename])

            i = 0
            for folder in filename.split("/")[:-1]:
                if not i:
                    exec(
                        "folder%i = SarcLib.Folder(folder + '/'); arc.addFolder(folder%i)"
                        .replace('%i', str(i)))

                else:
                    exec(
                        "folder%i = SarcLib.Folder(folder + '/'); folder%m.addFolder(folder%i)"
                        .replace('%i', str(i)).replace('%m', str(i - 1)))

                i += 1

            with open(fullname, "rb") as f:
                inb = f.read()

            hasFilename = True
            if file[:5] == "hash_":
                hasFilename = False

            if not i:
                arc.addFile(SarcLib.File(file, inb, hasFilename))

            else:
                exec("folder%m.addFile(SarcLib.File(file, inb, hasFilename))".
                     replace('%m', str(i - 1)))

    data, maxAlignment = arc.save()

    if level != -1:
        outData = libyaz0.compress(data, maxAlignment, level)
        del data

        if not outname:
            outname = ''.join([root, ".szs"])

    else:
        outData = data
        if not outname:
            outname = ''.join([root, ".sarc"])

    with open(outname, "wb+") as output:
        output.write(outData)
def change_water(path, water_type=0):
    water_types = [
        'Normal Water', 'Hot Water', 'Poison', 'Lava', 'Ice Water', 'Mud',
        'Clear Water', 'Sea Water'
    ]

    for parent_dir, dirs, files in os.walk(path):
        for file in files:
            current_path = '{0}{1}'.format(parent_dir, file)

            if 'water.extm.sstera' not in current_path:
                continue

            with open(current_path, 'rb') as infile:
                infile_binary = infile.read()

                while libyaz0.IsYazCompressed(infile_binary):
                    infile_binary = libyaz0.decompress(infile_binary)

                path, extension = os.path.splitext(current_path)
                filename = os.path.basename(current_path)

                if infile_binary[0x00:0x04] != b'SARC':
                    print('Not a sarc. :(')

                sarc = SarcLib.SARC_Archive()
                sarc.load(infile_binary)

                with tempfile.TemporaryDirectory() as temp_dir:
                    for sarc_file in sarc.contents:
                        if isinstance(sarc_file, SarcLib.File):
                            pos = 0
                            data = bytearray(sarc_file.data)
                            while pos + 8 <= len(sarc_file.data):
                                height, x_axis_flow_rate, z_axis_flow_rate, mate_check, mate = \
                                    struct.unpack('<3H2B', data[pos:pos + 0x08])

                                # height = 0x24cc
                                # x_axis_flow_rate = 0x8125
                                # z_axis_flow_rate = 0x8125
                                mate = water_type
                                mate_check = mate + 3

                                data[pos:pos + 0x08] = struct.pack(
                                    '<3H2B', height, x_axis_flow_rate,
                                    z_axis_flow_rate, mate_check, mate)

                                pos += 0x08

                            with open(
                                    '{0}/{1}'.format(temp_dir, sarc_file.name),
                                    'wb+') as outfile:
                                outfile.write(data)

                    sarc = SarcLib.SARC_Archive(endianness='>')

                    for path, dirs, files in os.walk(temp_dir):
                        for file in files:
                            with open('{0}/{1}'.format(path, file),
                                      'rb') as infile:
                                sarc.addFile(
                                    SarcLib.File(file, infile.read(), True))

                    data, alignment = sarc.save()
                    data = libyaz0.compress(data, alignment, 5)

                    destination = '{0}/output/MainField - {1}'.format(
                        os.path.dirname(__file__), water_types[water_type])

                    if not os.path.exists('{0}/output/'.format(
                            os.path.dirname(__file__))):
                        os.makedirs('{0}/output/'.format(
                            os.path.dirname(__file__)))
                    if not os.path.exists('{0}/output/MainField - {1}'.format(
                            os.path.dirname(__file__),
                            water_types[water_type])):
                        os.makedirs('{0}/output/MainField - {1}'.format(
                            os.path.dirname(__file__),
                            water_types[water_type]))

                    with open('{0}/{1}'.format(destination, filename),
                              'wb+') as outfile:
                        print('saving {0}...'.format(filename))
                        outfile.write(data)