-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathgenerate-options.py
116 lines (109 loc) · 4.63 KB
/
generate-options.py
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
import json
def type_to_offset(given_type):
if given_type == "bool":
# OpenGOAL "bools" are usually "symbols" but symbols are 4 bytes. TOO BIG instead we use uint8's set to 0 or 1
return 1
elif given_type == "uint8":
return 1
elif given_type == "uint16":
return 2
elif given_type == "uint32":
return 4
elif given_type.startswith("uint8["):
return 1 * int(given_type.split("[")[1].split("]")[0])
def type_to_typename(given_type):
# Convert to C# type names
if given_type == "bool":
return "byte"
elif given_type == "uint8":
return "byte"
elif given_type == "uint16":
return "ushort"
elif given_type == "uint32":
return "uint"
def type_to_splitval(given_type):
if given_type == "bool":
# OpenGOAL "bools" are usually "symbols" but symbols are 4 bytes. TOO BIG instead we use uint8's set to 0 or 1
return "1"
else:
return "null"
def generate_option_string(listVarName, label, offset, type, splitVal, name):
return " AddOption(vars.{}, \"{}\", {}, typeof({}), {}, false, \"{}\", false);\n".format(listVarName, label, offset, type, splitVal, name)
def generate_options(input_file, output_file):
# Settings To Emit
preset_settings = []
manual_settings = []
option_map = {}
# Read in json file
with open(input_file, 'r') as f:
options = json.load(f)
curr_offset = 0
for option in options["structFields"]:
option_name = option["label"]
if "name" in option:
option_name = option["name"]
option_str = generate_option_string("manualOptions", option["label"], curr_offset, type_to_typename(option["type"]), type_to_splitval(option["type"]), option_name)
if "hidden" not in option or option["hidden"] is not True:
# Add to manual settings
manual_settings.append(option_str)
option_map[option["label"]] = {
"name": option_name,
"label": option["label"],
"offset": curr_offset,
"type": type_to_typename(option["type"]),
"splitVal": type_to_splitval(option["type"])
}
curr_offset = curr_offset + type_to_offset(option["type"])
# Process presets
for preset in options["presets"]:
curr_preset_lines = []
curr_preset_lines.append(" vars.{} = new List<Dictionary<String, dynamic>>();\n".format(preset["varName"]))
for option in preset["options"]:
optionLabel = ""
splitVal = ""
optionName = ""
if isinstance(option, str):
optionLabel = option
splitVal = option_map[optionLabel]["splitVal"]
optionName = option_map[optionLabel]["name"]
else:
optionLabel = option["label"]
splitVal = option["splitVal"]
optionName = option["name"]
option_str = generate_option_string(preset["varName"], "{}_{}".format(preset["varName"], optionLabel), option_map[optionLabel]["offset"], option_map[optionLabel]["type"], splitVal, optionName)
curr_preset_lines.append(option_str)
curr_preset_lines.append(" settings.Add(\"preset_{}\", false, \"{}\");\n".format(preset["varName"], preset["name"]))
curr_preset_lines.append(" AddToSettings(vars.{}, \"preset_{}\");\n".format(preset["varName"], preset["varName"]))
curr_preset_lines.append(" vars.optionLists.Add(vars.{});\n".format(preset["varName"]))
preset_settings.append(curr_preset_lines)
# Write to output file
new_lines = []
with open(output_file, 'r') as f:
lines = f.readlines()
ignore_lines = False
for line in lines:
if "// __AUTOGENERATED__ START" in line:
ignore_lines = True
new_lines.append(line)
# Add our settings!
# Presets First
for preset in preset_settings:
for preset_line in preset:
new_lines.append(preset_line)
# Then all the manual options
new_lines.append(" vars.manualOptions = new List<Dictionary<String, dynamic>>();\n")
for setting in manual_settings:
new_lines.append(setting)
new_lines.append(" settings.Add(\"manual_options\", false, \"Manual Options\");\n")
new_lines.append(" AddToSettings(vars.manualOptions, \"manual_options\");\n")
new_lines.append(" vars.optionLists.Add(vars.manualOptions);\n")
continue
elif "// __AUTOGENERATED__ END" in line:
ignore_lines = False
new_lines.append(line)
continue
if not ignore_lines:
new_lines.append(line)
with open(output_file, 'w') as f:
f.writelines(new_lines)
generate_options("./jak3/options.json", "./jak3/opengoal-jak3-autosplitter.asl")