-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
324 lines (262 loc) · 12.8 KB
/
main.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
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
# All library stuff
import tkinter as tk
from tkinter import messagebox as mb
from tkinter.filedialog import askopenfilename
import re
import os
from typing import List, AnyStr, Tuple
def open_file():
path = askopenfilename(title="Select Forge 1.16 EntityModel .java class")
if path == "":
root.destroy()
return
file = open(path)
file_lines = file.readlines()
file.close()
file_lines.insert(0, "// Forge model conversion from 1.16 to 1.17 by Steven (Steaf23), program outline loosely "
"based on https://github.com/Globox1997/ModelConverter\n")
file_lines.insert(1, "// Generate all required imports yourself\n")
# Initialize model parts
replace_model_renderer(file_lines)
# get texture size
texture_size = get_and_remove_texture_size(file_lines)
# replace constructor and return cube parts for later
class_name, _extension = os.path.basename(path).split(".")
parts, ending_index = convert_constructor(class_name, file_lines)
file_lines = remove_after_line(ending_index, file_lines)
add_layer_definition(parts, ending_index, file_lines, texture_size)
add_anim_and_render(file_lines, parts)
replace_entity(file_lines, class_name)
# Filter empty elements
file_lines = list(filter(lambda item: item != rem_line, file_lines))
newfile = open(path.replace(".java", "") + "Converted.java", "w")
newfile.writelines(file_lines)
newfile.close()
root.destroy()
def replace_model_renderer(file_lines: List[AnyStr]) -> None:
# look for ModelRenderers
found_model_init: bool = False
for i, renderer_string in enumerate(file_lines):
if "final ModelRenderer" in renderer_string:
file_lines[i] = renderer_string.replace("ModelRenderer", "ModelPart")
found_model_init = True
elif found_model_init:
break
def get_and_remove_texture_size(file_lines: List[AnyStr]) -> List[int]:
texture_size = []
for texture_string in file_lines:
if "setTextureSize" in texture_string:
texture_size.append(int(re.search(r'\d+', texture_string).group()))
texture_size.append(texture_size[0])
break
else:
# check for blockbenchlike texture size
if "textureWidth" in texture_string:
texture_size.append(get_numbers(texture_string)[0])
next_line = file_lines[file_lines.index(texture_string) + 1]
if "textureHeight" in next_line:
texture_size.append(get_numbers(next_line)[0])
else:
texture_size.append(texture_size[0])
if file_lines[file_lines.index(texture_string) + 2] == "\n":
file_lines[file_lines.index(texture_string) + 2] = rem_line
file_lines[file_lines.index(texture_string) + 1] = rem_line
file_lines[file_lines.index(texture_string)] = rem_line
break
return texture_size
def convert_constructor(class_name: AnyStr, file_lines: List[AnyStr]) -> Tuple[List[dict], int]:
constructor_found = False
for line_index, line in enumerate(file_lines):
if (class_name + "()") in line:
file_lines[line_index] = file_lines[line_index].replace(class_name + "()", class_name + "(ModelPart model)")
constructor_found = True
if constructor_found:
ending_index = 0
parts = build_part_list(file_lines)
deleted = False
add_i = 1
while not deleted:
final_index = line_index + add_i
line = file_lines[final_index]
if "}" in line:
ending_index = file_lines.index(line) + len(parts) + 1
add_new_constructor(final_index - 1, file_lines, parts)
break
file_lines[final_index] = rem_line
add_i += 1
return parts, ending_index
else:
print("Couldn't find main method")
mb.showerror(title=None, message="Error: Constructor not found!\n"
"File name has to be the same as the class name!\n"
"No constructor overloading!\n"
"No constructor parameters!\n")
root.destroy()
exit(-1)
return [], -1
def build_part_list(file_lines) -> List[dict]:
parts: List = []
for i, line in enumerate(file_lines):
if "new ModelRenderer(this)" in line:
field_name = get_field_name(line)
part: dict = {"name": field_name,
"id_name": field_name.lower()}
part_i = 1
cubes = []
while line != "\n":
line = file_lines[i + part_i]
if "setRotationPoint" in line:
part["pivot"] = get_numbers(line.split(part["id_name"], 1)[1])
if "addChild" in line:
part["parent"] = get_field_name(line)
if "setRotationAngle" in line:
part["rotation"] = get_numbers(line.split(part["id_name"], 1)[1])
if "setTextureOffset" in line:
f = get_numbers(line.split(part["id_name"], 1)[1])
cube: dict = {"uv_position": [f[0], f[1]],
"origin": [f[2], f[3], f[4]],
"size": [f[5], f[6], f[7]],
"inflate": f[8],
"mirrored": get_booleans(line.split(part["id_name"], 1)[1])[0]}
cubes.append(cube)
part["cubes"] = cubes
if "rotation" not in part:
part["rotation"] = [0.0, 0.0, 0.0]
part_i += 1
parts.append(part)
p_i = 0
for part in parts:
if "parent" not in part:
p_i -= 1
p_i += 1
part["id"] = p_i
parts = reorient_parts(parts)
return parts
def add_new_constructor(starting_index: int, file_lines: List[AnyStr], parts: List[dict]) -> None:
for i, part in enumerate(parts):
assignment_string = "\t\tthis." + part["name"] + " = "
if "parent" in part:
# if the part has a grandparent
if "parent" in get_part_by_name(part["parent"], parts):
parent = "this." + part["parent"]
else:
parent = "model"
assignment_string += '%s.getChild("%s");' % (parent, part["id_name"])
else:
assignment_string += "model;"
file_lines.insert(starting_index + i, assignment_string + "\n")
i += 1
def add_layer_definition(parts: List[dict], starting_index: int, file_lines: List[AnyStr],
texture_size: List[int]) -> None:
line_index = starting_index
file_lines.append("\n"
"\tpublic static LayerDefinition createBodyLayer() {\n"
"\t\tMeshDefinition meshDefinition = new MeshDefinition();\n"
"\t\tPartDefinition partDefinition = meshDefinition.getRoot();\n")
line_index += 1
for part in parts:
# root note, get partdefinition from meshDefinition
if "parent" not in part:
part_def = "partDefinition."
else:
parent_idx = get_part_by_name(part["parent"], parts)["id"]
parent_idx = parent_idx if parent_idx != 0 else ""
if has_children(part, parts):
part_def = "PartDefinition partDefinition%s = " % str(part["id"] if part["id"] != 0 else "")
else:
part_def = ""
part_def += "partDefinition%s." % str(parent_idx)
file_lines.append('\n\t\t%saddOrReplaceChild("%s", CubeListBuilder.create()' % (part_def, part["id_name"]))
if "cubes" in part:
for cube in part["cubes"]:
tex_offs_string = "\n\t\t\t\t\t\t.texOffs(%d, %d)" % (cube["uv_position"][0],
cube["uv_position"][1])
box_string = ".addBox(%sf, %sf, %sf, %sf, %sf, %sf" % (cube["origin"][0],
cube["origin"][1],
cube["origin"][2],
cube["size"][0],
cube["size"][1],
cube["size"][2])
inflate_string = ", new CubeDeformation(%.2ff))" % (cube["inflate"]) if cube["inflate"] != 0.0 else ")"
mirror_string = ".mirror()" if cube["mirrored"] else ""
cube_string = tex_offs_string + box_string + inflate_string + mirror_string
file_lines.append(cube_string)
file_lines.append(",")
pose_string = "\n\t\t\t\tPartPose.offsetAndRotation" \
"(%sf, %sf, %sf, %sf, %sf, %sf));\n" % (part["pivot"][0],
part["pivot"][1],
part["pivot"][2],
part["rotation"][0],
part["rotation"][1],
part["rotation"][2],)
file_lines.append(pose_string)
file_lines.append("\n\t\treturn LayerDefinition.create(meshDefinition, %s, %s);\n\t}\n" % (str(texture_size[0]),
str(texture_size[1])))
def add_anim_and_render(file_lines: List[AnyStr], parts: List[dict]) -> None:
file_lines.append("\n\t@Override"
"\n\tpublic void setupAnim(Entity entity, float limbSwing, float limbSwingAmount, "
"float ageInTicks, float netHeadYaw, float headPitch) {"
"\n\t\t// Use this method to setup the animation and rotation angles"
"\n\t}\n")
file_lines.append("\n\t@Override"
"\n\tpublic void renderToBuffer(PoseStack poseStack, VertexConsumer buffer, "
"int packedLight, int packedOverlay, float red, float green, float blue, float alpha) {")
for part in parts:
if "parent" not in part:
file_lines.append("\n\t\t%s.render(poseStack, buffer, packedLight, packedOverlay);" % part["name"])
file_lines.append("\n\t}\n}\n")
def replace_entity(file_lines: List[AnyStr], filename: str):
for line in file_lines:
file_lines[file_lines.index(line)] = re.sub(r"(?<![\w])Entity(?![a-zA-Z])", filename, line)
def remove_after_line(index: int, file_lines: List[AnyStr]) -> List[AnyStr]:
return file_lines[:index]
def get_part_by_name(name: str, parts: List[dict]) -> dict:
for p in parts:
if name in p["name"]:
return p
def reorient_parts(parts: List[dict]) -> List[dict]:
for part in parts:
parent_part = get_parent_part(part, parts)
if parent_part is not None and part != parent_part:
if "parent" not in parent_part:
new_y = part["pivot"][1] + parent_part["pivot"][1]
part["pivot"][1] = new_y
return parts
def get_parent_part(part: dict, parts: List[dict]) -> dict:
if "parent" in part:
return get_part_by_name(part["parent"], parts)
return None
def has_children(part: dict, parts: List[dict]) -> bool:
for parent_part in parts:
if part == get_parent_part(parent_part, parts):
return True
return False
def find_root_parent(part: dict, parts: List[dict]) -> dict:
while True:
parent = get_parent_part(part, parts)
if parent is None:
return part
else:
part = parent
def get_numbers(line: str) -> List:
nums = re.findall(r"[-+]?\d*\.\d+|\d+", line)
nums = [float(num) if "." in num else int(num) for num in nums]
return nums
def get_booleans(line: str) -> List[float]:
bools = re.findall(r"(true)|(false)", line)
bools = [True if "true" in b else False for b in bools]
return bools
# returns the first word or words seperated by _, meaning its a field name
def get_field_name(line: str) -> str:
string = re.split(r"[.\s]+", line, maxsplit=2)[1]
return string
if __name__ == "__main__":
root = tk.Tk()
root.geometry('300x100')
root.title("Forge EntityModel converter 1.16 -> 1.17")
newline = "%(new_line)%"
rem_line = "%(r)%"
btn = tk.Button(root, text='Select File', command=lambda: open_file())
btn.grid(row=1, column=1)
btn.place(relx=0.5, rely=0.5, anchor=tk.CENTER)
tk.mainloop()