File: crosscompile.py

package info (click to toggle)
ddnet 19.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 68,960 kB
  • sloc: cpp: 195,050; ansic: 58,572; python: 5,568; asm: 946; sh: 941; java: 366; xml: 206; makefile: 31
file content (75 lines) | stat: -rw-r--r-- 1,866 bytes parent folder | download | duplicates (2)
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
import sys
import network
import seven.network

def get_msgs():
	return ["NETMSG_INVALID"] + [m.enum_name for m in network.Messages]

def get_msgs_7():
	return ["NETMSG_INVALID"] + [m.enum_name for m in seven.network.Messages]

def get_objs():
	return ["NETOBJ_INVALID"] + [m.enum_name for m in network.Objects if m.ex is None]

def get_objs_7():
	return ["NETOBJ_INVALID"] + [m.enum_name for m in seven.network.Objects]

def generate_map(a, b):
	result = []
	for m in a:
		try:
			result += [b.index(m)]
		except ValueError:
			result += [-1]

	return result

def output_map_header(name, m):
	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]; }}")

def output_map_source(name, m):
	print(f"const int gs_{name}[{len(m)}] = {{")
	print(*m, sep=',')
	print("};")

def main():
	map_header = "map_header" in sys.argv
	map_source = "map_source" in sys.argv
	guard = "GAME_GENERATED_PROTOCOLGLUE"
	if map_header:
		print("#ifndef " + guard)
		print("#define " + guard)
	elif map_source:
		print("#include \"protocolglue.h\"")

	msgs = get_msgs()
	msgs7 = get_msgs_7()

	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)

	objs = get_objs()
	objs7 = get_objs_7()

	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)


if __name__ == "__main__":
	main()