-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweapon_parser.py
executable file
·252 lines (207 loc) · 8.76 KB
/
weapon_parser.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
#!/usr/bin/python3
import sys
import glob
import json
import pprint
import uuid
import re
import argparse
from pathlib import Path, PureWindowsPath
pp = pprint.PrettyPrinter(indent=4, width=100)
WEAPONS = [
'rogue_blaster',
'rogue_crossbow',
'rogue_egun',
'rogue_heavymachinegun',
'rogue_heavysniper',
'rogue_machinegun',
'rogue_rocketlauncher',
'rogue_shaft',
'rogue_submachinegun',
'rogue_supershotgun',
'rogue_revolver',
'rogue_grenadelauncher',
# secondaries
'rogue_revolver_secondary',
'rogue_voidcannon_secondary',
'rogue_rocketlauncher_secondary',
'rogue_blaster_secondary',
'rogue_grenadelauncher_secondary',
# referenced but not in the game yet
'rogue_voidcannon',
'rogue_hammerswing',
'rogue_left_hand',
'rogue_doublebarreled_shotgun',
'rogue_bow',
'rogue_pistol',
'rogue_pncr',
'rogue_sword',
'rogue_tommygun',
'arena_left_hand',
'fists',
]
# DENYLIST = [
# ]
class bcolors:
HEADER = '\033[95m'
OKBLUE = '\033[94m'
OKCYAN = '\033[96m'
OKGREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
def line_starts_with_any(prefixes, line):
for prefix in prefixes:
if line.startswith(prefix):
return True
return False
def create_asset_json(src_dir, dst_dir):
data = []
# data = {}
# can't do keyed objects because there are 60 duplicate asset_names :(
for file in glob.iglob('./' + str(src_dir) + '/**/*.weapon', recursive=True):
data = data + parse_assets(file)
# data.update(parse_assets(file))
# data = [d for d in data if d.get('weapon_id') not in DENYLIST]
with open('./' + str(dst_dir) + '/weapons.json', 'w', encoding='utf-8') as f:
json.dump(data, f, ensure_ascii=False, indent=4)
# with open('./' + str(dst_dir) + '/weapons.min.json', 'w', encoding='utf-8') as f:
# json.dump(data, f, ensure_ascii=False)
# for a in data:
# pp.pprint(a['asset_name'])
# pp.pprint(data)
# print(len(data))
def parse_assets(file):
print(f'processing {file} ...')
assets = []
# assets = {}
current_asset = None
current_dynamic_rule = None
with open(file, 'r') as file_object:
lines = list(line.strip() for line in file_object) # all lines
lines = list(line for line in lines if line) # filter blank lines
lines = list(line for line in lines if not line.startswith('/')) # filter comments
lines = list(re.sub(' +', ' ', line) for line in lines) # replace multi whitespaces
lines = list(re.sub(' +', ' ', line) for line in lines) # replace multi whitespaces
lines = list(re.sub('\t+', ' ', line) for line in lines) # replace tabs
lines = list(re.sub('\u00A8', '', line) for line in lines) # remove ¨
lines = list(re.sub('\u00A7', '', line) for line in lines) # remove §
lines = list(line for line in lines if line) # filter blank lines
for i, l in enumerate(lines):
line = l.strip()
next_line = None
previous_line = None
# print(line)
# continue
if i+1 < len(lines):
next_line = lines[i+1].strip()
if i > 1:
previous_line = lines[i-1].strip()
if line_starts_with_any(WEAPONS, line):
if not next_line.startswith('{'):
print(f"{bcolors.FAIL}Error: asset next line is not bracket on line {i} in {file} {bcolors.ENDC}")
exit(1)
weapon_id = line.split(' ')[0]
weapon_type = line.split(' ')[1] if 1 < len(line.split(' ')) else "base"
augment_id = line.split(' ')[2] if 2 < len(line.split(' ')) else ""
parent_id = ""
# is augment
if augment_id != "":
parent_id = weapon_id
weapon_id = augment_id
current_asset = {
'id': weapon_id,
'parent_id': parent_id,
'weapon_type': weapon_type,
}
elif line.startswith('{'):
if previous_line and ((not line_starts_with_any(WEAPONS, previous_line)) and (not previous_line.startswith('dynamic_rule'))):
print(f"{bcolors.WARNING}WARNING: opening bracket without a top level context on line {i} in {file} {bcolors.ENDC}")
if current_asset is not None:
print(f"{bcolors.WARNING}assuming new dynamic_rule{bcolors.ENDC}")
current_dynamic_rule = {
'conditions': []
}
else:
name = str(uuid.uuid4())
print(f"{bcolors.WARNING}assuming new asset with random name {name}{bcolors.ENDC}")
current_asset = {
'asset_name': name,
'dynamic_rules': []
}
if not previous_line and current_asset is None:
name = str(uuid.uuid4())
print(f"{bcolors.WARNING}WARNING: opening bracket without a top level context on line {i} in {file} {bcolors.ENDC}")
print(f"{bcolors.WARNING}assuming new asset with random name {name}{bcolors.ENDC}")
current_asset = {
'asset_name': name,
'dynamic_rules': []
}
continue
# work around syntax error in some files
elif line.startswith('xmin'):
continue
elif line.startswith('}'):
if current_dynamic_rule is not None:
current_asset['dynamic_rules'].append(current_dynamic_rule)
current_dynamic_rule = None
elif current_asset is not None:
assets.append(current_asset)
# assets[current_asset['asset_name']] = current_asset
current_asset = None
elif i+1 == len(lines):
print(f"{bcolors.WARNING}WARNING: couldn't match closing bracket on last line in (ignoring) {file} {bcolors.ENDC}")
else:
print(f"{bcolors.FAIL}Error: couldn't match closing bracket on line {i} in {file} {bcolors.ENDC}")
exit(1)
# any other line apart from the structure ones
else:
key = line.split(' ', 1)[0]
if len(line.split(' ', 1)) == 1:
print(f"{bcolors.WARNING}WARNING: key without a value, ignoring line {i} {file} {bcolors.ENDC}")
continue
else:
value = line.split(' ', 1)[1]
if len(value.split(' ', 1)) > 1:
value = value.split(' ')
if key == 'dynamic_dimensions':
value = {
'xmin': value.split(' ')[0],
'ymin': value.split(' ')[1],
'zmin': value.split(' ')[2],
'xmax': value.split(' ')[3],
'ymax': value.split(' ')[4],
'zmax': value.split(' ')[5],
}
if current_dynamic_rule is not None:
if key == 'if':
current_dynamic_rule['conditions'].append(value)
elif key == 'select':
current_dynamic_rule['select'] = value.split(',')
elif key == 'pick':
current_dynamic_rule['pick'] = value.split(',')
else:
current_dynamic_rule[key] = value
elif current_asset is not None:
current_asset[key] = value
# print(f"{i}\t{line}")
# if there's still open contexts, close them
if current_dynamic_rule is not None:
current_asset['dynamic_rules'].append(current_dynamic_rule)
current_dynamic_rule = None
if current_asset is not None:
assets.append(current_asset)
# assets[current_asset['asset_name']] = current_asset
current_asset = None
return assets
if __name__ == '__main__':
parser = argparse.ArgumentParser(prog='weapon-parser.py', description='.weapon file parser for Diabotical')
parser.add_argument('src_directory', type=Path, help="this directory will be recursively searched for .weapon files")
parser.add_argument('dest_directory', type=Path, help="put the resulting json into this directory")
if len(sys.argv)==1:
parser.print_help(sys.stderr)
sys.exit(1)
args = parser.parse_args()
create_asset_json(args.src_directory, args.dest_directory)