-
Notifications
You must be signed in to change notification settings - Fork 0
/
minescript.py
165 lines (143 loc) · 5.46 KB
/
minescript.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
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
import logging
import os
import pprint
import shutil
from antlr4 import CommonTokenStream, FileStream
from exceptions import CompileTimeException, MappingException
from MappingVisitor import MappingVisitor
from MineScriptLexer import MineScriptLexer
from MineScriptParser import MineScriptParser
from Visitor import Visitor
logging.basicConfig(level=logging.INFO, format="%(message)s")
packmeta = """{
"pack": {
"pack_format": 1,
"description": "%s"
}
}
"""
load_file = """{
"values": [
"%s:load"
]
}"""
tick_file = """{
"values": [
"%s:tick"
]
}"""
def parent(path):
return os.path.abspath(os.path.join(path, os.pardir))
def mkdir(*args):
try:
os.mkdir(*args)
except FileExistsError:
pass
def create_structure(name, description, path):
try:
os.mkdir(os.path.join(path, name))
except FileExistsError:
shutil.rmtree(os.path.join(path, name))
os.mkdir(os.path.join(path, name))
mkdir(os.path.join(path, name, "data"))
with open(os.path.join(path, name, "pack.mcmeta"), "w") as file:
file.write(packmeta%description)
mkdir(os.path.join(path, name, "data", "minecraft"))
mkdir(os.path.join(path, name, "data", name))
mkdir(os.path.join(path, name, "data", "minecraft", "tags"))
mkdir(os.path.join(path, name, "data", "minecraft", "tags", "functions"))
mkdir(os.path.join(path, name, "data", name, "functions"))
mkdir(os.path.join(path, name, "data", name, "tags"))
with open(os.path.join(path, name, "data", "minecraft", "tags", "functions", "load.json"), "w") as file:
file.write(load_file%name)
with open(os.path.join(path, name, "data", "minecraft", "tags", "functions", "tick.json"), "w") as file:
file.write(tick_file%name)
def assemble_pack(name, visitor, path):
commands = 0
added = set()
with open(os.path.join(path, name, "data", name, "functions", "_setup.mcfunction"), "w") as usrvar:
with open(os.path.join(path, name, "data", name, "functions", "_vars.mcfunction"), "w") as tempvar:
for variable in visitor.igmemory:
if variable.startswith("_"):
file = tempvar
else:
file = usrvar
if not visitor.igmemory[variable].endswith("[]") or variable.startswith("_"):
file.write(f"scoreboard objectives add {variable} dummy \"{variable}\"\n")
commands += 1
for func in visitor.local:
for variable in visitor.local[func]:
if variable not in added:
if variable.startswith("_"):
file = tempvar
else:
file = usrvar
if not visitor.local[func][variable].endswith("[]") or variable.startswith("_"):
file.write(f"scoreboard objectives add {variable}+local dummy \"{variable}\"\n")
commands += 1
added.add(variable)
for loop in visitor.igloops:
with open(os.path.join(path, name, "data", name, "functions", f"{loop}.mcfunction"), "w") as file:
for command in visitor.igloops[loop]:
file.write(command + "\n")
commands += 1
for function in visitor.igfunctions:
with open(os.path.join(path, name, "data", name, "functions", f"{function}.mcfunction"), "w") as file:
if function == "load":
file.write(f"function {name}:_setup\n")
file.write(f"function {name}:_vars\n")
commands += 2
for command in visitor.igfunctions[function]["code"]:
file.write(command + "\n")
commands += 1
if "load" not in visitor.igfunctions:
with open(os.path.join(path, name, "data", name, "functions", "load.mcfunction"), "w") as file:
file.write(f"function {name}:_setup\n")
file.write(f"function {name}:_vars\n")
commands += 2
print(commands)
def get_tree(file):
inp = FileStream(file)
lexer = MineScriptLexer(inp)
stream = CommonTokenStream(lexer)
parser = MineScriptParser(stream)
tree = parser.prog()
return tree
def visit(name, file):
tree = get_tree(file)
mapvisitor = MappingVisitor(name, file)
try:
mapvisitor.visit(tree)
except MappingException:
return None
visitor = Visitor(name, file)
visitor.igfunctions = mapvisitor.igfunctions
visitor.igmemory = mapvisitor.igmemory
try:
visitor.visit(tree)
except CompileTimeException:
return None
print(visitor.tempvars)
print(visitor.memory)
return visitor
def main(name, file):
path = parent(file)
path = os.path.join(path, "build")
mkdir(path)
distpath = os.path.join(parent(file), "dist")
mkdir(distpath)
create_structure(name, "Generated using MineScript 2.0", path)
visitor = visit(name, file)
if visitor is None:
return
assemble_pack(name, visitor, path)
shutil.make_archive(os.path.join(distpath, name), 'zip', os.path.join(path, name))
if __name__ == "__main__":
# file = "test.txt"
# v = visit("test", file)
# if v is not None:
# print(v.igmemory)
# print(v.tempvars)
# pprint.pprint(v.igfunctions)
# pprint.pprint(v.igloops)
main("test", "test.txt")