summaryrefslogtreecommitdiff
path: root/extract_assets.py
blob: b3551bff5d06c09e316c2dd80d3328e9fa0243fb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#!/usr/bin/env python3

import os, sys, shutil
import shutil
from rom_info import Z64Rom
import rom_chooser
import struct
import subprocess
import argparse

def BuildOTR(xmlRoot, xmlVersion, rom, isMM, zapd_exe=None, genHeaders=None, customAssetsPath=None, customOtrFile=None, portVer=None):
    if not zapd_exe:
        zapd_exe = "x64\\Release\\ZAPD.exe" if sys.platform == "win32" else "../ZAPDTR/ZAPD.out"
    xmlPath = os.path.join(xmlRoot, xmlVersion)
    exec_cmd = [zapd_exe, "ed", "-i", xmlPath, "-b", rom, "-fl", "assets/extractor/filelists",
                "-o", "placeholder", "-osf", "placeholder", "-rconf"]
    configFileStr = "assets/extractor/Config_" + xmlVersion + ".xml"
    exec_cmd.extend([configFileStr])

    otrFileName = "oot.o2r"
    if isMM:
        otrFileName = "mm.o2r"


    # generate headers, but not otrs by excluding the otr exporter
    if genHeaders:
        exec_cmd.extend(["-gsf", "1"])
    else:
        # generate otrs, but not headers
        exec_cmd.extend(["-gsf", "0", "-se", "OTR", "--customAssetsPath", customAssetsPath,
                    "--customOtrFile", customOtrFile, "--otrfile", otrFileName])

    if portVer:
        exec_cmd.extend(["--portVer", portVer])

    print(exec_cmd)
    print(os.getcwd())
    exitValue = subprocess.call(exec_cmd)
    if exitValue != 0:
        print("\n")
        print("Error when building the OTR file...", file=os.sys.stderr)
        print("Aborting...", file=os.sys.stderr)
        print("\n")

def BuildCustomOtr(zapd_exe=None, assets_path=None, otrfile=None, portVer=None):
    if not zapd_exe:
        zapd_exe = "x64\\Release\\ZAPD.exe" if sys.platform == "win32" else "../ZAPDTR/ZAPD.out"

    if not assets_path or not otrfile:
        print("\n")
        print("Assets path or otrfile name not provided. Exiting...", file=os.sys.stderr)
        print("\n")
        return

    exec_cmd = [zapd_exe, "botr", "-se", "OTR", "--norom", "--customAssetsPath", assets_path, "--customOtrFile", otrfile]

    if portVer:
        exec_cmd.extend(["--portVer", portVer])

    print(exec_cmd)
    exitValue = subprocess.call(exec_cmd)
    if exitValue != 0:
        print("\n")
        print("Error when building custom otr file...", file=os.sys.stderr)
        print("Aborting...", file=os.sys.stderr)
        print("\n")

def main():
    parser = argparse.ArgumentParser()
    parser.add_argument("-z", "--zapd", help="Path to ZAPD executable", dest="zapd_exe", type=str)
    parser.add_argument("rom", help="Path to the rom", type=str, nargs="?")
    parser.add_argument("--non-interactive", help="Runs the script non-interactively for use in build scripts.", dest="non_interactive", action="store_true")
    parser.add_argument("-v", "--verbose", help="Display rom's header checksums and their corresponding xml folder", dest="verbose", action="store_true")
    parser.add_argument("--gen-headers", help="Generate source headers to be checked in", dest="gen_headers", action="store_true")
    parser.add_argument("--norom", help="Generate only custom otr to be bundled to the game", dest="norom", action="store_true")
    parser.add_argument("--xml-root", help="Root path for the rom xmls", dest="xml_root", type=str)
    parser.add_argument("--custom-assets-path", help="Path to custom assets for the custom otr file", dest="custom_assets_path", type=str)
    parser.add_argument("--custom-otr-file", help="Name for custom otr file", dest="custom_otr_file", type=str)
    parser.add_argument("--port-ver", help="Store the port version in the otr", dest="port_ver", type=str)

    args = parser.parse_args()

    if args.norom:
        BuildCustomOtr(args.zapd_exe, args.custom_assets_path, args.custom_otr_file, portVer=args.port_ver)
        return

    roms = [ Z64Rom(args.rom) ] if args.rom else rom_chooser.chooseROM(args.verbose, args.non_interactive)
    for rom in roms:
        BuildOTR(args.xml_root, rom.version.xml_ver, rom.file_path, rom.version.is_mm, zapd_exe=args.zapd_exe, genHeaders=args.gen_headers,
                 customAssetsPath=args.custom_assets_path, customOtrFile=args.custom_otr_file, portVer=args.port_ver)

if __name__ == "__main__":
    main()