-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathgenerate_json.py
104 lines (88 loc) · 3.81 KB
/
generate_json.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
import re
import os
import json
def read_data():
with open('tweak_bit_names.json','r') as f:
return json.loads(f.read())
data = read_data()
cs_filename = 'JsonConst.cs'
cpp_filename = 'json_const.h'
def make_var_name(desc: str) -> str:
desc = re.sub(r'\s+', '_', desc)
desc = re.sub(r'[^\w]', '', desc)
var_name: str = desc.title()
return var_name.replace('_', '')
def make_cpp_func_name(ram_addr: str) -> str:
return ram_addr.replace('$', 'j').lower()
def make_cs_class_name(ram_addr: str) -> str:
return ram_addr.replace('$', 'Value')
cpp_prelude = "#include <nlohmann/json.hpp>\n"
cs_prelude = "using Newtonsoft.Json;\n\n"
cpp_func_proto = """
auto {}(const nlohmann::json& j) {{
unsigned char c = 0;
auto& byte = j["{}"];
{}
return c;
}}
"""
cs_class_proto = """
public class {} : ByteRepresentant
{{
{}
}}
"""
def construct_cpp_func(address: str, values) -> str:
num_val = len(values)
if num_val == 8:
cpp_func_body = '\n'.join([f' c |= (byte["{v}"] ? 0x{0x01 << i:02X} : 0);' for i, v in enumerate(values)])
else:
diff = 8 - num_val
if address == "$166E":
first_val = values[0]
palette_val = values[1]
cpp_func_body = f' c |= (byte["{first_val}"] ? 0x01 : 0);\n'
cpp_func_body += f' c |= (((int)byte["{palette_val}"] & 0x07) << 1);\n'
diff += 1
cut = 2
else:
first_val = values[0]
cpp_func_body = f' c |= (((int)byte["{first_val}"] & 0x{(0x01 << (diff + 1)) - 1:02X}) << 0);\n'
cut = 1
cpp_func_body += '\n'.join([f' c |= (byte["{v}"] ? 0x{0x01 << (i + diff + 1):02X} : 0);' for i, v in enumerate(values[cut:])])
return str.format(cpp_func_proto, make_cpp_func_name(address), address, cpp_func_body)
def construct_cs_class(address: str, values) -> str:
num_val = len(values)
if num_val == 8:
cs_func_body = '\n'.join([f' [JsonProperty(Order = {i}, PropertyName = "{v}")]\n public bool {make_var_name(v)} {{ get; set; }}' for i, v in enumerate(values)])
else:
diff = 8 - num_val
if address == "$166E":
first_val = values[0]
palette_val = values[1]
cs_func_body = f' [JsonProperty(Order = 0, PropertyName = "{first_val}")]\n public bool {make_var_name(first_val)} {{ get; set; }}\n'
cs_func_body += f' [JsonProperty(Order = 1, PropertyName = "{palette_val}")]\n [JsonIntProperty(Size = {diff + 1})]\n public int {make_var_name(palette_val)} {{ get; set; }}\n'
diff += 1
cut = 2
else:
first_val = values[0]
cs_func_body = f' [JsonProperty(Order = 0, PropertyName = "{first_val}")]\n [JsonIntProperty(Size = {diff + 1})]\n public int {make_var_name(first_val)} {{ get; set; }}\n'
cut = 1
cs_func_body += '\n'.join([f' [JsonProperty(Order = {i + diff + 1}, PropertyName = "{v}")]\n public bool {make_var_name(v)} {{ get; set; }}' for i, v in enumerate(values[cut:])])
return str.format(cs_class_proto, make_cs_class_name(address), cs_func_body)
def construct_cpp_file():
with open(cpp_filename, 'w') as f:
f.write(cpp_prelude)
f.writelines([construct_cpp_func(addr, vals) for addr, vals in data.items()])
def construct_cs_file():
with open(cs_filename, 'w') as f:
f.write(cs_prelude)
f.write('namespace CFG.Json\n{')
f.writelines([construct_cs_class(addr, vals) for addr, vals in data.items()])
f.write('}')
def move_files():
os.replace(cpp_filename, os.path.join('src', cpp_filename))
os.replace(cs_filename, os.path.join('src', 'CFG Editor', 'CFG Editor', 'Json', cs_filename))
construct_cpp_file()
construct_cs_file()
move_files()