-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompile.py
124 lines (94 loc) · 3.99 KB
/
compile.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
import os
import template
import utils
import variable
"""
Main script
Run it at the root directory of your project
It uses current working directory as the starting point
"""
def compile_message(path, file):
"""Compile one *.msg file"""
print('Compiling {}'.format(file))
with open(os.path.join(path, file), encoding='utf-8') as src_file:
lines = src_file.readlines()
name = utils.replace_ending(file, ".msg", "")
dst_file = os.path.join(path, "{}.cs".format(name))
package = "NoPackage"
usings = {
"NeoSeusCore.Network.Transport"
}
variables = []
reads = []
writes = []
for line in lines:
parts = line.split()
if len(parts) < 1:
continue
key = parts[0]
if key == "package":
# Message package definition
package = parts[1]
elif key == "use":
# Custom dependencies
usings.add(parts[1])
elif key == "//":
# Just a *.msg comment (will not be compiled to *.cs)
pass
elif key == "%":
# Separator between variables (empty line between them in *.cs)
if len(parts) >= 2:
# and optional comment
space = utils.add_space("", 2)
variables.append("{}\n{}// {}".format(space, space, " ".join(parts[1:])))
else:
variables.append(utils.add_space("", 2))
reads.append(utils.add_space("", 3))
writes.append(utils.add_space("", 3))
continue
elif key == "#" or key == "@":
# Variable of specified type (#) or List<specified type> (@)
parts_count = len(parts)
if parts_count >= 3:
# Optional inline comment for variable definition
comment = ""
if parts_count >= 4:
comment = " ".join(parts[3:])
definition = variable.compile_variable(key, parts[1], parts[2], comment)
if definition is None:
print("Warning! Unknown key {} for type {} for variable {}".format(key, parts[1], parts[2]))
continue
# Variable can require some usings (e.g. List require System.Collections.Generic)
for line_using in definition.usings:
usings.add(line_using)
for line_variable in definition.variables:
variables.append(line_variable)
for line_read in definition.reads:
reads.append(line_read)
for line_write in definition.writes:
writes.append(line_write)
continue
result = template.make_template()
result = result.replace("%variables%", "\n".join(variables))
result = result.replace("%read%", "\n".join(reads))
result = result.replace("%write%", "\n".join(writes))
result = result.replace("%package%", package)
result = result.replace("%name%", name)
# Our networking library require all messages to inherit NeoSeusCore.Messages.Message
# so if we not in this namespace, we need to autoimport it
if not package.startswith("NeoSeusCore.Messages"):
usings.add("NeoSeusCore.Messages")
usings_new = []
for using in usings:
usings_new.append("using {};".format(using))
result = result.replace("%usings%", "\n".join(usings_new))
with open(dst_file, 'w', encoding='utf-8') as write_to:
write_to.write(result)
def compile_all():
exclude = {'.git', '.idea', 'obj', 'bin', 'packages'}
for root, dirs, files in os.walk(os.curdir):
dirs[:] = [d for d in dirs if d not in exclude]
for file in files:
if file.endswith('.msg'):
compile_message(root, file)
compile_all()