forked from CleverRaven/Cataclysm-DDA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vehicle_reformatter.py
executable file
·149 lines (131 loc) · 5.28 KB
/
vehicle_reformatter.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
#!/usr/bin/env python3
import json
import argparse
import copy
import os
import subprocess
LINE_LIMIT = 58
def get_data(argsDict, resource_name):
resource = []
resource_sources = argsDict.get(resource_name, [])
if not isinstance(resource_sources, list):
resource_sources = [resource_sources]
for resource_filename in resource_sources:
if resource_filename.endswith(".json"):
try:
with open(
resource_filename, encoding="utf-8") as resource_file:
resource += json.load(resource_file)
except FileNotFoundError:
exit("Failed: could not find {}".format(resource_filename))
else:
print("Invalid filename {}".format(resource_filename))
return resource
# stupid stinking Python 2 versus Python 3 syntax
def write_to_json(pathname, data):
with open(pathname, "w", encoding="utf-8") as fp:
try:
json.dump(data, fp, ensure_ascii=False)
except ValueError:
fp.write(json.dumps(data))
json_formatter = "./tools/format/json_formatter.cgi"
if os.path.isfile(json_formatter):
cmd = [json_formatter, pathname]
subprocess.call(cmd)
def really_add_parts(xpoint, ypoint, xparts, new_parts):
y_part = xparts.get(ypoint, {})
if not y_part:
return
pretty_parts = []
pretty_subparts = []
temp_parts = ""
for part in y_part["parts"]:
temp_parts += '"{}"'.format(part)
# debug - are we getting the line length test string correct?
# print("{}: {}".format(len(temp_parts), temp_parts))
if len(temp_parts) > LINE_LIMIT:
pretty_parts.append(pretty_subparts)
pretty_subparts = []
temp_parts = '"{}"'.format(part)
temp_parts += ", "
pretty_subparts.append(part)
pretty_parts.append(pretty_subparts)
# debug - correct list of parts sublists?
# print("{}".format(pretty_parts))
for subpart_list in pretty_parts:
if len(subpart_list) > 1:
rev_part = {"x": xpoint, "y": ypoint, "parts": subpart_list}
else:
subpart = subpart_list[0]
if isinstance(subpart, str):
rev_part = {"x": xpoint, "y": ypoint, "part": subpart}
else:
rev_part = {"x": xpoint, "y": ypoint}
for key, value in subpart.items():
if key == "x" or key == "y":
continue
rev_part[key] = value
revised_parts.append(rev_part)
def add_parts(revised_parts, xpoint, last_center, new_parts):
xparts = new_parts.get(xpoint, {})
if not xparts:
return 0
min_y = 122
max_y = -122
for ypoint in xparts:
min_y = min(min_y, ypoint)
max_y = max(max_y, ypoint)
if xpoint == 0:
really_add_parts(xpoint, last_center, xparts, new_parts)
for ypoint in range(min_y - 1, last_center, 1):
really_add_parts(xpoint, ypoint, xparts, new_parts)
if xpoint != 0:
really_add_parts(xpoint, last_center, xparts, new_parts)
for ypoint in range(last_center + 1, max_y + 1):
really_add_parts(xpoint, ypoint, xparts, new_parts)
return int((min_y + max_y) / 2)
args = argparse.ArgumentParser(
description="Reformat vehicles using parts arrays.")
args.add_argument("vehicle_sources", action="store", nargs="+",
help="specify jsons file to convert to new format.")
argsDict = vars(args.parse_args())
for datafile in argsDict.get("vehicle_sources", []):
min_x = 122
max_x = -122
vehicles = get_data(argsDict, "vehicle_sources")
for old_vehicle in vehicles:
#print(old_vehicle.get("id"))
new_parts = {}
for part in old_vehicle.get("parts", []):
part_x = part.get("x", 0)
part_y = part.get("y", 0)
min_x = min(min_x, part_x)
max_x = max(max_x, part_x)
new_parts.setdefault(part_x, {})
new_parts[part_x].setdefault(part_y, {"parts": []})
part_list = []
if part.get("parts"):
for new_part in part.get("parts"):
new_parts[part_x][part_y]["parts"].append(new_part)
else:
if part.get("fuel") or part.get("ammo"):
new_part = copy.deepcopy(part)
del new_part["x"]
del new_part["y"]
new_parts[part_x][part_y]["parts"].append(new_part)
else:
new_parts[part_x][part_y]["parts"].append(part.get("part"))
# debug - did everything get copied over correctly?
# print(json.dumps(new_parts, indent=2))
revised_parts = []
last_center = add_parts(revised_parts, 0, 0, new_parts)
# print("last center {}".format(last_center))
for xpoint in range(1, max_x + 1):
last_center = add_parts(revised_parts, xpoint, last_center,
new_parts)
# print("x {}, last center {}".format(xpoint, last_center))
for xpoint in range(-1, min_x - 1, -1):
last_center = add_parts(revised_parts, xpoint, last_center,
new_parts)
old_vehicle["parts"] = revised_parts
write_to_json(datafile, vehicles)