forked from OoTRandomizer/OoT-Randomizer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SettingsToJson.py
executable file
·274 lines (223 loc) · 9.78 KB
/
SettingsToJson.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
#!/usr/bin/env python3
from Hints import HintDistFiles
from SettingsList import setting_infos, setting_map, get_setting_info, get_settings_from_section, get_settings_from_tab
from Utils import data_path, read_json
import sys
import json
import copy
tab_keys = ['text', 'app_type', 'footer']
section_keys = ['text', 'is_colors', 'is_sfx', 'col_span', 'row_span', 'subheader']
setting_keys = ['hide_when_disabled', 'min', 'max', 'size', 'max_length', 'file_types', 'no_line_break', 'function', 'option_remove']
types_with_options = ['Checkbutton', 'Radiobutton', 'Combobox', 'SearchBox', 'MultipleSelect']
def RemoveTrailingLines(text):
while text.endswith('<br>'):
text = text[:-4]
while text.startswith('<br>'):
text = text[4:]
return text
def deep_update(source, new_dict):
for k, v in new_dict.items():
if isinstance(v, dict):
source[k] = deep_update(source.get(k, { }), v)
elif isinstance(v, list):
source[k] = (source.get(k, []) + v)
else:
source[k] = v
return source
def add_disable_option_to_json(disable_option, optionJson):
if disable_option.get('settings') != None:
if 'controls_visibility_setting' not in optionJson:
optionJson['controls_visibility_setting'] = ','.join(disable_option['settings'])
else:
optionJson['controls_visibility_setting'] += ',' + ','.join(disable_option['settings'])
if disable_option.get('sections') != None:
if 'controls_visibility_section' not in optionJson:
optionJson['controls_visibility_section'] = ','.join(disable_option['sections'])
else:
optionJson['controls_visibility_section'] += ',' + ','.join(disable_option['sections'])
if disable_option.get('tabs') != None:
if 'controls_visibility_tab' not in optionJson:
optionJson['controls_visibility_tab'] = ','.join(disable_option['tabs'])
else:
optionJson['controls_visibility_tab'] += ',' + ','.join(disable_option['tabs'])
def GetSettingJson(setting, web_version, as_array=False):
try:
setting_info = get_setting_info(setting)
except KeyError:
if as_array:
return {'name': setting}
else:
return {}
if setting_info.gui_text is None:
return None
settingJson = {
'options': [],
'default': setting_info.default,
'text': setting_info.gui_text,
'tooltip': RemoveTrailingLines('<br>'.join(line.strip() for line in setting_info.gui_tooltip.split('\n'))),
'type': setting_info.gui_type,
'shared': setting_info.shared,
}
if as_array:
settingJson['name'] = setting_info.name
else:
settingJson['current_value'] = setting_info.default
setting_disable = {}
if setting_info.disable != None:
setting_disable = copy.deepcopy(setting_info.disable)
version_specific_keys = []
for key, value in setting_info.gui_params.items():
version_specific = False
if key.startswith('web:'):
if web_version:
key = key[4:]
version_specific_keys.append(key)
version_specific = True
else:
continue
if key.startswith('electron:'):
if not web_version:
key = key[9:]
version_specific_keys.append(key)
version_specific = True
else:
continue
if key in setting_keys and (key not in version_specific_keys or version_specific):
settingJson[key] = value
if key == 'disable':
for option,types in value.items():
for s in types.get('settings', []):
if get_setting_info(s).shared:
raise ValueError(f'Cannot disable setting {s}. Disabling "shared" settings in the gui_params is forbidden. Use the non gui_param version of disable instead.')
for section in types.get('sections', []):
for s in get_settings_from_section(section):
if get_setting_info(s).shared:
raise ValueError(f'Cannot disable setting {s} in {section}. Disabling "shared" settings in the gui_params is forbidden. Use the non gui_param version of disable instead.')
for tab in types.get('tabs', []):
for s in get_settings_from_tab(tab):
if get_setting_info(s).shared:
raise ValueError(f'Cannot disable setting {s} in {tab}. Disabling "shared" settings in the gui_params is forbidden. Use the non gui_param version of disable instead.')
deep_update(setting_disable, value)
if settingJson['type'] in types_with_options:
if as_array:
settingJson['options'] = []
else:
settingJson['options'] = {}
tags_list = []
for option_name in setting_info.choice_list:
if option_name in settingJson.get('option_remove', []):
continue
if as_array:
optionJson = {
'name': option_name,
'text': setting_info.choices[option_name],
}
else:
optionJson = {
'text': setting_info.choices[option_name],
}
if option_name in setting_disable:
add_disable_option_to_json(setting_disable[option_name], optionJson)
option_tooltip = setting_info.gui_params.get('choice_tooltip', {}).get(option_name, None)
if option_tooltip != None:
optionJson['tooltip'] = RemoveTrailingLines('<br>'.join(line.strip() for line in option_tooltip.split('\n')))
option_filter = setting_info.gui_params.get('filterdata', {}).get(option_name, None)
if option_filter != None:
optionJson['tags'] = option_filter
for tag in option_filter:
if tag not in tags_list:
tags_list.append(tag)
if as_array:
settingJson['options'].append(optionJson)
else:
settingJson['options'][option_name] = optionJson
# For disables with '!', add disable settings to all options other than the one marked.
for option_name in setting_disable:
if isinstance(option_name, str) and option_name[0] == '!':
if as_array:
for option in settingJson['options']:
if option['name'] != option_name[1:]:
add_disable_option_to_json(setting_disable[option_name], option)
else:
for name, option in settingJson['options'].items():
if name != option_name[1:]:
add_disable_option_to_json(setting_disable[option_name], option)
if tags_list:
tags_list.sort()
settingJson['tags'] = ['(all)'] + tags_list
settingJson['filter_by_tag'] = True
return settingJson
def GetSectionJson(section, web_version, as_array=False):
if as_array:
sectionJson = {
'name' : section['name'],
'settings' : []
}
else:
sectionJson = {
'settings' : {}
}
for key, value in section.items():
if key in section_keys:
sectionJson[key] = value
for setting in section['settings']:
settingJson = GetSettingJson(setting, web_version, as_array)
if as_array:
sectionJson['settings'].append(settingJson)
else:
sectionJson['settings'][setting] = settingJson
return sectionJson
def GetTabJson(tab, web_version, as_array=False):
if as_array:
tabJson = {
'name' : tab['name'],
'sections' : []
}
else:
tabJson = {
'sections' : {}
}
for key, value in tab.items():
if key in tab_keys:
tabJson[key] = value
for section in tab['sections']:
sectionJson = GetSectionJson(section, web_version, as_array)
if as_array:
tabJson['sections'].append(sectionJson)
else:
tabJson['sections'][section['name']] = sectionJson
return tabJson
def CreateJSON(path, web_version=False):
settingOutputJson = {
'settingsObj' : {},
'settingsArray' : [],
'cosmeticsObj' : {},
'cosmeticsArray': [],
'distroArray' : [],
}
for tab in setting_map['Tabs']:
if tab.get('exclude_from_web', False) and web_version:
continue
elif tab.get('exclude_from_electron', False) and not web_version:
continue
tabJsonObj = GetTabJson(tab, web_version, as_array=False)
tabJsonArr = GetTabJson(tab, web_version, as_array=True)
settingOutputJson['settingsObj'][tab['name']] = tabJsonObj
settingOutputJson['settingsArray'].append(tabJsonArr)
if tab.get('is_cosmetics', False):
settingOutputJson['cosmeticsObj'][tab['name']] = tabJsonObj
settingOutputJson['cosmeticsArray'].append(tabJsonArr)
for d in HintDistFiles():
dist = read_json(d)
if ('distribution' in dist and
'goal' in dist['distribution'] and
(dist['distribution']['goal']['fixed'] != 0 or
dist['distribution']['goal']['weight'] != 0)):
settingOutputJson['distroArray'].append(dist['name'])
with open(path, 'w') as f:
json.dump(settingOutputJson, f)
def settingToJsonMain():
web_version = '--web' in sys.argv
CreateJSON(data_path('generated/settings_list.json'), web_version)
if __name__ == '__main__':
settingToJsonMain()