summaryrefslogtreecommitdiff
path: root/tools/assets_config.py
blob: fa2987cf9d8e119face9822716fe33d36ac41f92 (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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import curses
from sys import argv
import json
from os import path

CONFIG_DESCRIPTIONS = {
    "oead_yaz0": "Use oead (pip install oead) for asset (de)compression (faster)",
    "decompress_assets": "Decompress Yaz0 compressed assets (appends .c to the filename)",
    "extract_arc": "Extract archive (.arc) files",
    "convert_stages": "Convert Stage Files (.dzs and .dzr) to json",
    "convert_textures": "Convert Textures (.bti) to png",
    "update_copydate": "Update COPYDATE on build",
    "package_maps": "Package Symbol Map (.map) files on build",
}

CONFIG_DEFAULTS = {
    "oead_yaz0": False,
    "decompress_assets": True,
    "extract_arc": True,
    "convert_stages": True,
    "convert_textures": False,
    "update_copydate": True,
    "package_maps": True,
}

CONFIGFILE_DEFAULT = "asset_config.json"


def printMenu(stdscr, selected_idx, options):
    stdscr.clear()
    height, width = stdscr.getmaxyx()

    left = min([(width // 2) - (len(option) // 2) for option in options])
    top = (height // 2) - (len(options) // 2)

    stdscr.addstr(top - 2, left, "Configure Asset Extraction/Packaging (q to save):")

    for idx, option in enumerate(options):
        x = left
        y = top + idx

        if idx == selected_idx:
            stdscr.attron(curses.color_pair(1))
            stdscr.addstr(y, x, option)
            stdscr.attroff(curses.color_pair(1))
        else:
            stdscr.addstr(y, x, option)

    stdscr.refresh()


def cursesMenu(stdscr, values):
    curses.curs_set(0)
    curses.init_pair(1, curses.COLOR_BLACK, curses.COLOR_WHITE)

    keys = list(values.keys())
    options = [
        f"{CONFIG_DESCRIPTIONS[key]}: ({'X' if values[key] else ' '})" for key in keys
    ]
    bool_values = [values[key] for key in keys]

    selected_idx = 0

    while True:
        printMenu(stdscr, selected_idx, options)

        key = stdscr.getch()

        if key == curses.KEY_UP and selected_idx > 0:
            selected_idx -= 1
        elif key == curses.KEY_DOWN and selected_idx < len(options) - 1:
            selected_idx += 1
        elif key in (curses.KEY_ENTER, ord("\n"), 13):
            bool_values[selected_idx] = not bool_values[selected_idx]
            options[selected_idx] = (
                f"{CONFIG_DESCRIPTIONS[keys[selected_idx]]}: ({'X' if bool_values[selected_idx] else ' '})"
            )
        elif key in (27, ord("q")):  # Escape, q
            break

    return {key: value for key, value in zip(keys, bool_values)}


def saveConfig(values, configfile=CONFIGFILE_DEFAULT):
    with open(configfile, "w") as f:
        json.dump(values, f)


def updateConfig(configfile=CONFIGFILE_DEFAULT):
    values = getConfig(configfile)

    values = curses.wrapper(cursesMenu, values)
    saveConfig(values, configfile)
    return values


def getConfig(configfile=CONFIGFILE_DEFAULT, update=False):
    values = CONFIG_DEFAULTS

    if path.exists(configfile):
        with open(configfile, "r") as f:
            config = json.load(f)
            for key, value in config.items():
                if key in CONFIG_DEFAULTS:
                    values[key] = value
    elif update == True:
        values = updateConfig(configfile)

    return values


def main():
    if len(argv) < 2:
        print(f"{argv[0]} Usage: python3 {argv[0]} asset_config.json")
        exit(1)

    updateConfig(argv[1])


if __name__ == "__main__":
    main()