/
weapbin.py
268 lines (217 loc) · 9.8 KB
/
weapbin.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
#This script writes all the data from weapons.bin into JSON
import struct
import json
import os
import sys
def leBytesInt(file):
#converts the next 2 bytes to an integer
next2 = file.read(2)
print(next2)
converted2Bytes = int.from_bytes(next2, "little",signed = True)
return converted2Bytes
def leBytesFloat(file):
#converts the next 4 bytes to a float
converted4Bytes = struct.unpack("<f", file.read(4))
print(converted4Bytes)
return converted4Bytes
def extract():
with open("weapons.bin", "rb") as f:
end = False
#list for the loadorder.txt (needed for later)
loadorder = []
while not end:
nextID = f.read(2)
#check if next 2 bytes are empty
if nextID == b'':
break
#if they are not empty -> store it as the ship ID
shipID = int.from_bytes(nextID, byteorder="little")
#total amount of hardpoints the ship possesses
totalAmount = leBytesInt(f)
primary = []
primaryX = []
primaryY = []
primaryZ = []
primaryCoords = [primaryX, primaryY, primaryZ]
secondary = []
secondaryX = []
secondaryY = []
secondaryZ = []
secondaryCoords = [secondaryX, secondaryY, secondaryZ]
turret = []
turretX = []
turretY = []
turretZ = []
turretCoords = [turretX, turretY, turretZ]
engine = []
engineX = []
engineY = []
engineZ = []
engineSizeX = []
engineSizeY = []
engineSizeZ = []
engineSizes = [engineSizeX, engineSizeY, engineSizeZ]
engineCoords = [engineX, engineY, engineZ]
primAmount = 0
secAmount = 0
turrAmount = 0
engAmount = 0
for i in range(0, totalAmount):
raw = f.read(2)
nextBytes = int.from_bytes(raw, byteorder="little")
#check if the next 2 bytes are empty
if raw == b'':
end = True
break
#check for primary hardpoint indicator (00 00)
if nextBytes == 0:
print("bytes = 0")
primAmount += 1
primary.append(f"primary_{primAmount}")
#add coordinates to lists
for i in primaryCoords:
i.append(leBytesInt(f))
#check for secondary hardpoint indicator (01 00)
elif nextBytes == 1:
print("bytes = 1")
secAmount += 1
secondary.append(f"secondary_{secAmount}")
#add coordinates to lists
for i in secondaryCoords:
i.append(leBytesInt(f))
#check for turret hardpoint indicator (02 00)
elif nextBytes == 2:
print("bytes = 2")
turrAmount += 1
turret.append(f"turret_{turrAmount}")
#add coordinates to lists
for i in turretCoords:
i.append(leBytesInt(f))
#check for engine hardpoint indicator (03 00)
elif nextBytes == 3:
print("bytes = 3")
engAmount += 1
engine.append(f"engine_{engAmount}")
#add coordinates to lists
for i in engineCoords:
i.append(leBytesInt(f))
#add engine plume sizes to lists
for i in engineSizes:
i.append(leBytesFloat(f))
#create dictionary
shipDict = {'ID': shipID, 'total': totalAmount}
for i in range(0, totalAmount):
if end:
break
pIteration = 0
for i in primary:
shipDict[i + "_key"] = i
shipDict[i + "_coordX"] = primaryX[pIteration]
shipDict[i + "_coordY"] = primaryY[pIteration]
shipDict[i + "_coordZ"] = primaryZ[pIteration]
pIteration += 1
sIteration = 0
for i in secondary:
shipDict[i + "_key"] = i
shipDict[i + "_coordX"] = secondaryX[sIteration]
shipDict[i + "_coordY"] = secondaryY[sIteration]
shipDict[i + "_coordZ"] = secondaryZ[sIteration]
sIteration += 1
tIteration = 0
for i in turret:
shipDict[i + "_key"] = i
shipDict[i + "_coordX"] = turretX[tIteration]
shipDict[i + "_coordY"] = turretY[tIteration]
shipDict[i + "_coordZ"] = turretZ[tIteration]
tIteration +=1
eIteration = 0
for i in engine:
shipDict[i + "_key"] = i
shipDict[i + "_coordX"] = engineX[eIteration]
shipDict[i + "_coordY"] = engineY[eIteration]
shipDict[i + "_coordZ"] = engineZ[eIteration]
shipDict[i + "_sizeX"] = engineSizeX[eIteration]
shipDict[i + "_sizeY"] = engineSizeY[eIteration]
shipDict[i + "_sizeZ"] = engineSizeZ[eIteration]
eIteration += 1
#put dictionary into JSON file
with open(f"ship_{shipID}.json", 'w') as g:
json.dump(shipDict, g, indent=4)
g.close()
print("End reached")
loadorder.append(f"ship_{shipID}.json")
#append to loadorder file
with open(f"loadorder.txt", 'w') as load:
loadorderstr = ""
for i in loadorder:
loadorderstr += f"{i}\n"
load.write(loadorderstr)
load.close()
f.close()
def build():
#replace weapons_built.bin, if it's already been created
if os.path.isfile("weapons_built.bin"):
os.remove("weapons_built.bin")
with open("loadorder.txt") as load:
loadData = load.read()
loadList = loadData.split("\n")
with open("weapons_built.bin", 'ab') as w:
for i in loadList:
if i != "":
l = open(i, 'r')
loadedJSON = json.load(l)
#keywords for short ints
shorts = ["primary", "secondary", 'turret', 'engine']
for j in loadedJSON:
# This loop checks what type of data the key 'j' is, writing it's value
# in the corresponding types (ID, total, indicators and coordinates are shorts,
# engine sizes are floats)
#print("current " + str(j))
if j == 'ID' or j == 'total':
print("writing " + j)
inBytes = struct.pack('<h', loadedJSON[j])
w.write(inBytes)
#print(inBytes)
else:
for k in shorts:
if k in j and "coord" in j:
print("writing " + j)
inBytes = struct.pack('<h', loadedJSON[j])
w.write(inBytes)
elif k in j and "size" in j:
print("writing " + j)
# Float is inside a list object, that's why
# there's the loadedJSON[j][0] statement.
inBytes = struct.pack('<f', loadedJSON[j][0])
w.write(inBytes)
elif k in j and "key" in j:
# "if a keyword AND the word 'key' are in dictionary key -> hardpoint indicator"
# What a mouth-full
if k == "primary":
print("Writing primary seperator")
inBytes = struct.pack('<h', 0)
w.write(inBytes)
elif k == "secondary":
print("Writing secondary seperator")
inBytes = struct.pack('<h', 1)
w.write(inBytes)
elif k == "turret":
print("Writing turret seperator")
inBytes = struct.pack('<h', 2)
w.write(inBytes)
elif k == "engine":
print("Writing engine seperator")
inBytes = struct.pack('<h', 3)
w.write(inBytes)
argNum = len(sys.argv)
if argNum > 2:
print("Please only enter one argument\n1. python .\weapbin.py extract\n2. python .\weapbin.py build")
elif argNum == 1:
print("Please enter an argument:\n1.python .\weapbin.py extract\n2. python .\weapbin.oy build")
else:
if sys.argv[1] == "extract":
extract()
elif sys.argv[1] == "build":
build()
else:
print("Argument not valid. Please enter either 'extract' or 'build' as an argument.")