-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcodegen.py.old
More file actions
401 lines (344 loc) · 14.8 KB
/
codegen.py.old
File metadata and controls
401 lines (344 loc) · 14.8 KB
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
import os
import re
from pprint import pprint
import glob
type_map = {
# TYPE |code tpye |method type |is heap |default value
"b": ["int8_t ", "char", False, "0", ],
"B": ["uint8_t ", "byte", False, "0", ],
"h": ["int16_t ", "short", False, "0", ],
"H": ["uint16_t ", "ushort", False, "0", ],
"i": ["int32_t ", "int", False, "0", ],
"I": ["uint32_t ", "uint", False, "0", ],
"l": ["int64_t ", "long", False, "0", ],
"L": ["uint64_t ", "ulong", False, "0", ],
"f": ["float ", "float", False, "0", ],
"d": ["double ", "double", False, "0", ],
"?": ["bool ", "bool", False, "false", ],
"v": ["int32_t ", "varint", False, "0", ],
"s": ["char *", "string", True, "NULL", ],
"p": ["cmc_block_pos ", "position", False, "{.x=0,.y=0,.z=0}", ],
"n": ["cmc_nbt *", "nbt", True, "NULL", ],
"a": ["cmc_buff *", "buff", True, "NULL", ],
"S": ["cmc_slot *", "slot", True, "NULL", ],
"m": ["cmc_entity_metadata ", "entity_metadata", True, "{.size=0,.entries=NULL}", ],
"u": ["cmc_uuid ", "uuid", False, "{.lower=0,.upper=0}", ],
"A": ["cmc_array ", None, True, "{.data=NULL,.size=0}", ],
}
replacement_paths = ["src/*.c", "include/cmc/*.h"]
def split_array_exp(inp):
name = inp[:inp.find("[")]
inp = inp[inp.find("[")+1:]
deepness = 0
for i, c in enumerate(inp):
if c == "[":
deepness += 1
if c == "]":
deepness -= 1
if deepness == -1:
break
return name[1:], inp[:i], inp[i+1:]
def careful_split(exp):
out = []
curr_str = ""
deepness = 0
for c in exp:
if c == ";" and deepness == 0:
out.append(curr_str)
curr_str = ""
else:
curr_str += c
if c == "[":
deepness += 1
elif c == "]":
deepness -= 1
if deepness < 0:
raise ValueError("closed nonexistent array")
out.append(curr_str)
return out
def get_absolute_paths(file_paths):
absolute_paths = []
for file_path in file_paths:
matches = glob.glob(file_path)
for match in matches:
absolute_path = os.path.abspath(match)
absolute_paths.append(absolute_path)
return absolute_paths
def replace_code_segments(replacement_code, tag):
start_tag = f"// CGSS: {tag}" # Code Generator Segment Start
end_tag = f"// CGSE: {tag}" # Code Generator Segment End
pattern = re.compile(
f"({re.escape(start_tag)}).*?({re.escape(end_tag)})", re.DOTALL
)
for file_path in get_absolute_paths(replacement_paths):
with open(file_path, "r") as file:
file_content = file.read()
if not pattern.search(file_content):
continue
file_content = pattern.sub(rf"\1\n{replacement_code}\n\2", file_content)
with open(file_path, "w") as file:
file.write(file_content)
return
raise ValueError(f"didnt find tag {tag}")
def type_def_content(token, packet_name, wrap_name):
typedefs = []
members = []
for sym in careful_split(token):
exp_data = sym[1:]
exp_type = sym[0]
if exp_type == "A":
name, array_exp, _ = split_array_exp(sym)
typedefs.append(type_def_content(array_exp, packet_name, f"{packet_name}_{name}"))
members.append(f"{type_map[exp_type][0]} {name};")
else:
members.append(f"{type_map[exp_type][0]} {exp_data};")
typedefs.append(f"""
typedef struct {{
{''.join(members)}
}} {wrap_name};
""")
return "\n".join(typedefs)
def type_def(inp):
t = ""
if inp["is_empty"]:
return ""
lastdata = None
for pvid in sorted(inp["packet_data"]): # sorted ensures the the lowest come first
print(pvid)
if lastdata != inp["packet_data"][pvid]["content_str"]:
t += type_def_content(inp["packet_data"][pvid]["content_str"], inp["name"], f"cmc_packet_{inp['name']}_{pvid}")
lastdata = inp["packet_data"][pvid]["content_str"]
return t
def unpack_method_content(to_unpack_to, exp, deepness, packet_name):
def handle_value(sym):
exp_type = sym[0]
exp_data = sym[1:]
exp_ctype = type_map[exp_type][1]
return f"{to_unpack_to}{exp_data} = cmc_buff_unpack_{exp_ctype}(buff);"
def handle_array(sym):
name, array_exp, key = split_array_exp(sym)
i = chr(deepness)
return f"""
{to_unpack_to}{name}.size = {to_unpack_to}{key};
{to_unpack_to}{name}.data = CMC_ERRB_ABLE(cmc_malloc({to_unpack_to}{name}.size * sizeof({packet_name}_{name}), &buff->err), goto err;);
for (size_t {i} = 0; {i} < {to_unpack_to}{name}.size; ++{i}) {{
{packet_name}_{name} *p_{name} = &(({packet_name}_{name} *){to_unpack_to}{name}.data)[{i}];
{unpack_method_content(f'p_{name}->', array_exp, deepness + 1, packet_name)}
}}
"""
return "".join(
handle_array(sym) if (sym[0] == "A") else handle_value(sym)
for sym in careful_split(exp)
)
def unpack_method(inp):
if inp["is_empty"]:
return ""
return "".join((
f"""
{inp['name']}_packet unpack_{inp['name']}_packet(cmc_buff *buff) {{
{inp['name']}_packet packet = {{}};
switch(buff->protocol_version) {{
""",
*(
f"""
case CMC_PROTOCOL_VERSION_{pv}: {{
{unpack_method_content('packet.', data['content_str'], ord('i'), inp['name'])}
break;
}}
"""
for pv, data in inp["packet_data"].items()
),
f"""
default:
CMC_ERRB(CMC_ERR_UNSUPPORTED_PROTOCOL_VERSION, return packet;);
}}
CMC_ERRB_ABLE(,goto err);
if(buff->position != buff->length) CMC_ERRB(CMC_ERR_BUFF_UNDERFLOW, goto err;);
return packet;
err:
cmc_free_{inp['name']}_packet(&packet, &buff->err);
return ({inp['name']}_packet){{}};
}}
""",
))
def send_method_content(data, to_send, deepness, packet_name):
def handle_value(symbol):
name = symbol[1:]
return f"cmc_buff_pack_{type_map[symbol[0]][1]}(buff, {to_send}{name});"
def handle_array(symbol):
name, array_exp, _ = split_array_exp(symbol)
i = chr(deepness)
return f"""
for (size_t {i} = 0; {i} < {to_send}{name}.size; ++{i}) {{
{packet_name}_{name} *p_{name} = &(({packet_name}_{name} *){to_send}{name}.data)[{i}];
{send_method_content(array_exp, f'p_{name}->', deepness+1, packet_name)}
}}
"""
return "".join(
handle_array(symbol) if (symbol[0] == "A") else handle_value(symbol)
for symbol in careful_split(data)
)
def send_method(inp):
second_param = "" if inp["is_empty"] else f", {inp['name']}_packet *packet"
return "".join((
f"""
cmc_err cmc_send_{inp['name']}_packet(cmc_conn *conn{second_param}) {{
cmc_buff *buff = cmc_buff_init(conn->protocol_version);
switch(conn->protocol_version) {{
""",
*(
f"""
case CMC_PROTOCOL_VERSION_{pv}: {{
cmc_buff_pack_varint(buff, {data['packet_id']});
{
send_method_content(data['content_str'], 'packet->', ord('i'), inp['name'])
if 'content' in data else ''
}
break;
}}
"""
for pv, data in inp["packet_data"].items()
),
f"""
default:
cmc_buff_free(buff);
CMC_ERRRB(CMC_ERR_UNSUPPORTED_PROTOCOL_VERSION);
}}
cmc_conn_send_packet(conn, buff);
cmc_buff_free(buff);
return CMC_ERR_NO;
}}
""",
))
def packet_ids(mc_packet_exps):
packet_states = ["_".join(exp.split("_")[:2]) for exp in mc_packet_exps]
code = ""
for state in packet_states:
code += f"enum packetids_{state} {{"
for exp in mc_packet_exps:
if exp.startswith(state):
code += f"CMC_PACKETID_{state.upper()}_{'_'.join(exp.split('_')[2:]).split(';')[0].upper()} = {exp.split(';')[1]},"
code += "};"
return code
def free_method_content(exp, tofree, deepness, packet_name):
def handle_value(sym):
exp_type = sym[0]
exp_data = sym[1:]
return f"cmc_{type_map[exp_type][1]}_free({tofree}->{exp_data});"
def handle_array(sym):
name, array_exp, _ = split_array_exp(sym)
i = chr(deepness)
loop_body = free_method_content(array_exp, f"p_{name}", deepness + 1, packet_name)
loop_statement = f"""
for (size_t {i} = 0; {i} < {tofree}->{name}.size; ++{i}) {{
{packet_name}_{name} *p_{name} = &(({packet_name}_{name} *){tofree}->{name}.data)[{i}];
{loop_body}
}}
""" if loop_body else ""
return f"""
{loop_statement}
free({tofree}->{name}.data);
{tofree}->{name}.size = 0;
"""
return "".join(
handle_array(sym) if (sym[0] == "A") else handle_value(sym)
for sym in careful_split(exp)
if type_map[sym[0]][2] # is_heap(exp_type)
)
def free_method(inp):
if inp["is_empty"]:
return ""
return f"""
void cmc_free_{inp['name']}_packet({inp['name']}_packet *packet) {{
{
free_method_content(inp['type_def_content_str'], 'packet', ord('i'), inp['name'])
if inp['is_heap'] else '(void)packet;'
}
(void)err;
}}
"""
def gather_packets():
out = {}
for fname in os.listdir("packets"):
with open("packets/" + fname, "r") as f:
vid = int(fname.split(".")[0])
for exp in f.read().replace(" ", "").split("\n"):
if exp.find("#") != -1:
exp = exp[:exp.find("#")]
if not exp:
continue
packet_name, packet_id, symbol_str = exp.split(";", maxsplit=2)
if packet_name not in out:
out[packet_name] = {}
out[packet_name]["is_empty"] = True
out[packet_name]["is_heap"] = False
if "packet_data" not in out[packet_name]:
out[packet_name]["packet_data"] = {}
if vid not in out[packet_name]["packet_data"]:
out[packet_name]["packet_data"][vid] = {}
if symbol_str:
out[packet_name]["packet_data"][vid]["content"] = careful_split(symbol_str)
out[packet_name]["packet_data"][vid]["content_str"] = symbol_str
out[packet_name]["is_empty"] = False
content = []
for val in out[packet_name]["packet_data"].values():
for field in val["content"]:
if (duplicates := [f for f in content if f[1:] == field[1:]]):
if (existing_field := duplicates[0]) != field:
print(f"fatal conflict between {existing_field} and {field} on {packet_name}!!!")
exit(1)
else:
content.append(field)
if type_map[field[0]][2]:
out[packet_name]["is_heap"] = True
out[packet_name]["type_def_content"] = content
out[packet_name]["type_def_content_str"] = ";".join(content)
out[packet_name]["packet_data"][vid]["packet_id"] = packet_id
out[packet_name]["name"] = packet_name
return list(out.values())
def packet_name_id_define(exps):
return "".join(f"CMC_{exp['name'].upper()}_NAME_ID," for exp in exps)
def packet_id_to_packet_name_id(inp):
code = ""
for packet in inp:
direction, state, *_ = packet["name"].split("_")
for protcole_version, data in packet['packet_data'].items():
code += f"case (COMBINE_VALUES({data['packet_id']}, CMC_CONN_STATE_{state.upper()}, CMC_DIRECTION_{direction}, {protcole_version})): return CMC_{packet['name'].upper()}_NAME_ID;"
return code
def main():
mc_packet_exps = gather_packets()
#pprint(mc_packet_exps)
# type defs
replace_code_segments(
"".join(type_def(mc_packet_exp) for mc_packet_exp in mc_packet_exps),
"packet_types",
)
replace_code_segments("\n".join(f"HELPER(CMC_{inp['name'].upper()}_NAME_ID);" for inp in mc_packet_exps), "packet_name_id_string")
replace_code_segments(packet_name_id_define(mc_packet_exps), "packet_name_id_define")
# packet unpack methods
replace_code_segments(
"".join(unpack_method(mc_packet_exp) for mc_packet_exp in mc_packet_exps),
"unpack_methods_c",
)
replace_code_segments(
"".join(f"{inp['name']}_packet unpack_{inp['name']}_packet(cmc_buff *buff);" if not inp['is_empty'] else "" for inp in mc_packet_exps),
"unpack_methods_h",
)
# packet unpack methods header
replace_code_segments(
"".join(send_method(mc_packet_exp) for mc_packet_exp in mc_packet_exps),
"send_methods_c",
)
# packet send methods header
replace_code_segments(
"".join("cmc_err cmc_send_{}_packet(cmc_conn *conn{});".format(inp['name'], f", {inp['name']}_packet *packet" if not inp["is_empty"] else "") for inp in mc_packet_exps),
"send_methods_h",
)
replace_code_segments(
"\n".join(free_method(sym) for sym in mc_packet_exps), "free_methods_c"
)
replace_code_segments(
"\n".join(f"void cmc_free_{inp['name']}_packet({inp['name']}_packet *packet, cmc_err_extra *err);" for inp in mc_packet_exps if not inp["is_empty"]), "free_methods_h"
)
if __name__ == "__main__":
main()