ddnet/datasrc/crosscompile.py

76 lines
1.8 KiB
Python
Raw Normal View History

2022-04-03 00:20:53 +00:00
import sys
2020-12-02 14:22:26 +00:00
import network
import seven.network
2020-04-16 08:46:43 +00:00
2020-12-02 14:22:26 +00:00
def get_msgs():
return ["NETMSG_INVALID"] + [m.enum_name for m in network.Messages]
2020-04-16 08:46:43 +00:00
def get_msgs_7():
2020-12-02 14:22:26 +00:00
return ["NETMSG_INVALID"] + [m.enum_name for m in seven.network.Messages]
2020-04-16 08:46:43 +00:00
2020-06-12 19:22:54 +00:00
def get_objs():
2020-12-02 14:22:26 +00:00
return ["NETOBJ_INVALID"] + [m.enum_name for m in network.Objects if m.ex is None]
2020-06-12 19:22:54 +00:00
def get_objs_7():
2020-12-02 14:22:26 +00:00
return ["NETOBJ_INVALID"] + [m.enum_name for m in seven.network.Objects]
2020-06-12 19:22:54 +00:00
2020-04-16 08:46:43 +00:00
def generate_map(a, b):
2020-12-02 14:22:26 +00:00
result = []
for m in a:
try:
result += [b.index(m)]
except ValueError:
result += [-1]
2020-04-16 08:46:43 +00:00
2020-12-02 14:22:26 +00:00
return result
2020-04-16 08:46:43 +00:00
2022-04-03 00:20:53 +00:00
def output_map_header(name, m):
2022-07-07 11:55:23 +00:00
print(f"extern const int gs_{name}[{len(m)}];")
print(f"inline int {name}(int a) {{ if(a < 0 || a >= {len(m)}) return -1; return gs_{name}[a]; }}")
2020-04-16 08:46:43 +00:00
2022-04-03 00:20:53 +00:00
def output_map_source(name, m):
2022-07-07 11:55:23 +00:00
print(f"const int gs_{name}[{len(m)}] = {{")
2020-12-02 14:22:26 +00:00
print(*m, sep=',')
print("};")
2020-04-16 08:46:43 +00:00
def main():
2022-04-03 00:20:53 +00:00
map_header = "map_header" in sys.argv
map_source = "map_source" in sys.argv
2020-12-02 14:22:26 +00:00
guard = "GAME_GENERATED_PROTOCOLGLUE"
2022-04-03 00:20:53 +00:00
if map_header:
print("#ifndef " + guard)
print("#define " + guard)
elif map_source:
print("#include \"protocolglue.h\"")
2020-12-02 14:22:26 +00:00
msgs = get_msgs()
msgs7 = get_msgs_7()
2020-04-16 08:46:43 +00:00
2022-04-03 00:20:53 +00:00
map6to7 = generate_map(msgs, msgs7)
map7to6 = generate_map(msgs7, msgs)
if map_header:
output_map_header("Msg_SixToSeven", map6to7)
output_map_header("Msg_SevenToSix", map7to6)
elif map_source:
output_map_source("Msg_SixToSeven", map6to7)
output_map_source("Msg_SevenToSix", map7to6)
2020-06-12 19:22:54 +00:00
2020-12-02 14:22:26 +00:00
objs = get_objs()
objs7 = get_objs_7()
2020-04-16 08:46:43 +00:00
2022-04-03 00:20:53 +00:00
objs6to7 = generate_map(objs, objs7)
objs7to6 = generate_map(objs7, objs)
if map_header:
output_map_header("Obj_SixToSeven", objs6to7)
output_map_header("Obj_SevenToSix", objs7to6)
print("#endif //" + guard)
elif map_source:
output_map_source("Obj_SixToSeven", objs6to7)
output_map_source("Obj_SevenToSix", objs7to6)
2020-04-16 08:46:43 +00:00
if __name__ == "__main__":
2020-12-02 14:22:26 +00:00
main()