forked from Nubb3r/COH_Opponent_Bot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCOHOpponentBot_ReplayParser.py
More file actions
433 lines (346 loc) · 13.7 KB
/
COHOpponentBot_ReplayParser.py
File metadata and controls
433 lines (346 loc) · 13.7 KB
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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
import logging
from COHOpponentBot_Parameters import Parameters
import re
import os
import datetime
from functools import partial
class COH_Replay_Parser:
"""Parses a company of heroes 1 replay to extract as much useful information from it as possible."""
def __init__(self, filePath = None, parameters = None) -> None:
if parameters:
self.parameters = parameters
else:
self.parameters = Parameters()
self.filePath = filePath
self.fileVersion = None
self.chunkyVersion = None
self.randomStart = None
self.highResources = None
self.VPCount = None
self.matchType = None
self.localDateString = None
self.localDate = None
self.unknownDate = None
self.replayName = None
self.gameVersion = None
self.modName = None
self.mapName = None
self.mapNameFull = None
self.mapDescription = None
self.mapDescriptionFull = None
self.mapFileName = None
self.mapWidth = None
self.mapHeight = None
self.playerList = []
self.data = None
self.dataIndex = 0
if filePath:
self.load(self.filePath)
def read_UnsignedLong4Bytes(self) -> int:
"""Reads 4 bytes as an unsigned long int."""
try:
if self.data:
fourBytes = bytearray(self.data[self.dataIndex:self.dataIndex+4])
self.dataIndex += 4
theInt = int.from_bytes(fourBytes, byteorder='little', signed=False)
return theInt
except Exception as e:
logging.error("Failed to read 4 bytes")
logging.exception("Stack Trace: ")
def read_Bytes(self, numberOfBytes):
"""reads a number of bytes from the data array"""
try:
if self.data:
output = bytearray(self.data[self.dataIndex:self.dataIndex+numberOfBytes])
self.dataIndex += numberOfBytes
return output
except Exception as e:
logging.error("Failed to Read bytes")
logging.exception("Stack Trace: ")
def read_LengthString(self):
"""Reads the first 4 bytes containing the string length and then the rest of the string."""
try:
if self.data:
stringLength = self.read_UnsignedLong4Bytes()
theString = self.read_2ByteString(stringLength =stringLength)
return theString
except Exception as e:
logging.error("Failed to read a string of specified length")
logging.exception("Stack Trace: ")
def read_2ByteString(self, stringLength=0 ) -> str:
"""Reads a 2byte encoded little-endian string of specified length."""
try:
if self.data:
theBytes = bytearray(self.data[self.dataIndex:self.dataIndex+(stringLength*2)])
self.dataIndex += stringLength*2
theString = theBytes.decode('utf-16le')
return theString
except Exception as e:
logging.error("Failed to read a string of specified length")
logging.exception("Stack Trace: ")
def read_LengthASCIIString(self) -> str:
"""Reads ASCII string, the length defined by the first four bytes."""
try:
if self.data:
stringLength = self.read_UnsignedLong4Bytes()
theString = self.read_ASCIIString(stringLength=stringLength)
return theString
except Exception as e:
logging.error("Failed to read a string of specified length")
logging.exception("Stack Trace: ")
def read_ASCIIString(self, stringLength=0) -> str:
"""Reads a byte array of spcified length and attempts to convert it into a string."""
try:
if self.data:
theBytes = bytearray(self.data[self.dataIndex:self.dataIndex+stringLength])
self.dataIndex += stringLength
theString = theBytes.decode('ascii')
return theString
except Exception as e:
logging.error("Failed to read a string of specified length")
logging.exception("Stack Trace: ")
def read_NULLTerminated_2ByteString(self) -> str:
"""Reads a Utf-16 little endian character string until the first two byte NULL value."""
try:
if self.data:
characters = ""
for character in iter(partial(self.read_Bytes, 2) , bytearray(b"\x00\x00")):
characters += bytearray(character).decode('utf-16le')
return characters
except Exception as e:
logging.error("Failed to read a string of specified length")
logging.exception("Stack Trace: ")
def read_NULLTerminated_ASCIIString(self) -> str:
"""Reads a byte array until the first NULL and converts to a string."""
try:
if self.data:
characters = ""
for character in iter(partial(self.read_Bytes, 1) , bytearray(b"\x00")):
characters += bytearray(character).decode('ascii')
return characters
except Exception as e:
logging.error("Failed to read a string of specified length")
logging.exception("Stack Trace: ")
def seek(self, numberOfBytes, relative = 0):
"""Moves the file index a number of bytes forward or backward"""
try:
numberOfBytes = int(numberOfBytes)
relative = int(relative)
if relative == 0:
assert(0 <= numberOfBytes <= len(self.data))
self.dataIndex = numberOfBytes
if relative == 1:
assert(0 <= (numberOfBytes+self.dataIndex) <= len(self.data))
self.dataIndex += numberOfBytes
if relative == 2:
assert(0 <= (len(self.data) - numberOfBytes) <= len(self.data))
self.dataIndex = len(self.data) - numberOfBytes
except Exception as e:
logging.error("Failed move file Index")
logging.exception("Stack Trace: ")
return None
def load(self, filePath = ""):
with open(filePath, "rb") as fileHandle:
self.data = fileHandle.read()
self.processData()
def processData(self):
#Process the file Header
self.fileVersion = self.read_UnsignedLong4Bytes()
cohrec = self.read_ASCIIString(stringLength= 8)
self.localDateString = self.read_NULLTerminated_2ByteString()
# Parse localDateString as a datetime object
self.localDate = self.decodeDate(self.localDateString)
self.seek(76,0)
firstRelicChunkyAddress = self.dataIndex
relicChunky = self.read_ASCIIString(stringLength=12)
unknown = self.read_UnsignedLong4Bytes()
self.chunkyVersion = self.read_UnsignedLong4Bytes() # 3
unknown = self.read_UnsignedLong4Bytes()
self.chunkyHeaderLength = self.read_UnsignedLong4Bytes()
self.seek(-28,1) # sets file pointer back to start of relic chunky
self.seek(self.chunkyHeaderLength, 1) # seeks to begining of FOLDPOST
self.seek(firstRelicChunkyAddress, 0)
self.seek(96,1) # move pointer to the position of the second relic chunky
secondRelicChunkyAddress = self.dataIndex
relicChunky = self.read_ASCIIString(stringLength=12)
unknown = self.read_UnsignedLong4Bytes()
chunkyVersion = self.read_UnsignedLong4Bytes() # 3
unknown = self.read_UnsignedLong4Bytes()
chunkLength = self.read_UnsignedLong4Bytes()
self.seek(secondRelicChunkyAddress, 0)
self.seek(chunkLength, 1) # seek to position of first viable chunk
self.parseChunk(0)
self.parseChunk(0)
#get mapname and mapdescription from ucs file if they exist there
self.mapNameFull = UCS(parameters=self.parameters).compareUCS(self.mapName)
self.mapDescriptionFull = UCS(parameters=self.parameters).compareUCS(self.mapDescription)
def parseChunk(self, level):
chunkType = self.read_ASCIIString(stringLength= 8) # Reads FOLDFOLD, FOLDDATA, DATASDSC, DATAINFO etc
chunkVersion = self.read_UnsignedLong4Bytes()
chunkLength = self.read_UnsignedLong4Bytes()
chunkNameLength = self.read_UnsignedLong4Bytes()
self.seek(8,1)
chunkName = ""
if chunkNameLength > 0:
chunkName = self.read_ASCIIString(stringLength=chunkNameLength)
chunkStart = self.dataIndex
#Here we start a recusive loop
if chunkType:
if (chunkType.startswith("FOLD")):
while (self.dataIndex < (chunkStart + chunkLength )):
self.parseChunk(level=level+1)
if (chunkType == "DATASDSC") and (int(chunkVersion) == 2004):
unknown = self.read_UnsignedLong4Bytes()
self.unknownDate = self.read_LengthString()
unknown = self.read_UnsignedLong4Bytes()
unknown = self.read_UnsignedLong4Bytes()
unknown = self.read_UnsignedLong4Bytes()
self.modName = self.read_LengthASCIIString()
self.mapFileName = self.read_LengthASCIIString()
unknown = self.read_UnsignedLong4Bytes()
unknown = self.read_UnsignedLong4Bytes()
unknown = self.read_UnsignedLong4Bytes()
unknown = self.read_UnsignedLong4Bytes()
unknown = self.read_UnsignedLong4Bytes()
self.mapName = self.read_LengthString()
value = self.read_UnsignedLong4Bytes()
if value != 0: # test to see if data is replicated or not
unknown = self.read_2ByteString(value)
self.mapDescription = self.read_LengthString()
unknown = self.read_UnsignedLong4Bytes()
self.mapWidth = self.read_UnsignedLong4Bytes()
self.mapHeight = self.read_UnsignedLong4Bytes()
unknown = self.read_UnsignedLong4Bytes()
unknown = self.read_UnsignedLong4Bytes()
unknown = self.read_UnsignedLong4Bytes()
if(chunkType == "DATABASE") and (int(chunkVersion == 11)):
self.seek(16, 1)
self.randomStart = True
self.randomStart = not (self.read_UnsignedLong4Bytes() == 0) # 0 is fixed 1 is random
COLS = self.read_UnsignedLong4Bytes()
self.highResources = (self.read_UnsignedLong4Bytes() == 1)
TSSR = self.read_UnsignedLong4Bytes()
self.VPCount = 250 * (1 << (int)(self.read_UnsignedLong4Bytes()))
self.seek(5, 1)
self.replayName = self.read_LengthString()
self.seek(8, 1)
self.VPGame = (self.read_UnsignedLong4Bytes() == 0x603872a3)
self.seek(23 , 1)
self.read_LengthASCIIString()
self.seek(4,1)
self.read_LengthASCIIString()
self.seek(8,1)
if (self.read_UnsignedLong4Bytes() == 2):
self.read_LengthASCIIString()
self.gameVersion = self.read_LengthASCIIString()
self.read_LengthASCIIString()
self.matchType = self.read_LengthASCIIString()
if(chunkType == "DATAINFO") and (chunkVersion == 6):
userName = self.read_LengthString()
self.read_UnsignedLong4Bytes()
self.read_UnsignedLong4Bytes()
faction = self.read_LengthASCIIString()
self.read_UnsignedLong4Bytes()
self.read_UnsignedLong4Bytes()
self.playerList.append({'name':userName,'faction':faction})
self.seek(chunkStart + chunkLength, 0)
def decodeDate(self, timeString) -> datetime:
"""
Processes the date string that is in a different format depending on the game locale (US, EURO and ASIAN dates strings follow a different string format)
"""
#24hr: DD-MM-YYYY HH:mm
reEuro = re.compile(r"(\d\d).(\d\d).(\d\d\d\d)\s(\d\d).(\d\d)")
match = re.match(reEuro, timeString)
if match:
print("Euro String")
print(match.groups())
try:
day = int(match.group(1))
month = int(match.group(2))
year = int(match.group(3))
hour = int(match.group(4))
minute = int(match.group(5))
return datetime.datetime(year = year, month=month, day = day, hour = hour, minute = minute)
except Exception as e:
logging.error(str(e))
logging.exception("Exception : ")
#12hr: MM/DD/YYYY hh:mm XM *numbers are not 0-padded
reUS = re.compile(r"(\d{1,2}).(\d{1,2}).(\d\d\d\d)\s(\d{1,2}).(\d{1,2}).*?(\w)M")
match = re.match(reUS, timeString)
if match:
print("US String")
print(match.groups())
try:
day = int(match.group(2))
month = int(match.group(1))
year = int(match.group(3))
hour = int(match.group(4))
minute = int(match.group(5))
meridiem = str(match.group(6))
if "p" in meridiem.lower():
hour = hour + 12
return datetime.datetime(year = year, month=month, day = day, hour = hour, minute = minute)
except Exception as e:
logging.error(str(e))
logging.exception("Exception : ")
#YYYY/MM/DD HH:MM
reAsian = re.compile(r"(\d\d\d\d).(\d\d).(\d\d)\s(\d\d).(\d\d)")
match = re.match(reAsian, (" ".join( (str(timeString).encode("ascii", "ignore").decode()).split() ) ) )
if match:
print("Asian String")
print(match.groups())
try:
day = int(match.group(3))
month = int(match.group(2))
year = int(match.group(1))
hour = int(match.group(4))
minute = int(match.group(5))
return datetime.datetime(year = year, month=month, day = day, hour = hour, minute = minute)
except Exception as e:
logging.error(str(e))
logging.exception("Exception : ")
def __str__(self) -> str:
output = "Data:\n"
output += "fileVersion : {}\n".format(self.fileVersion)
output += "chunkyVersion : {}\n".format(self.chunkyVersion)
output += "randomStart : {}\n".format(self.randomStart)
output += "highResources : {}\n".format(self.highResources)
output += "VPCount : {}\n".format(self.VPCount)
output += "matchType : {}\n".format(self.matchType)
output += "localDateString : {}\n".format(self.localDateString)
output += "localDate : {}\n".format(self.localDate)
output += "unknownDate : {}\n".format(self.unknownDate)
output += "replayName : {}\n".format(self.replayName)
output += "gameVersion : {}\n".format(self.gameVersion)
output += "modName : {}\n".format(self.modName)
output += "mapName : {}\n".format(self.mapName)
output += "mapNameFull : {}\n".format(self.mapNameFull)
output += "mapDescription : {}\n".format(self.mapDescription)
output += "mapDescriptionFull : {}\n".format(self.mapDescriptionFull)
output += "mapFileName : {}\n".format(self.mapFileName)
output += "mapWidth : {}\n".format(self.mapWidth)
output += "mapHeight : {}\n".format(self.mapHeight)
output += "playerList : {}\n".format(len(self.playerList))
output += "playerList : {}\n".format(self.playerList)
return output
class UCS:
def __init__(self, parameters = None) -> None:
if parameters:
self.parameters = parameters
else:
self.parameters = Parameters()
self.ucsPath = self.parameters.data.get('cohUCSPath')
def compareUCS(self, compareString):
try:
if (os.path.isfile(self.ucsPath)):
linenumber = 1
with open(self.ucsPath, 'r', encoding='utf16') as f:
for line in f:
linenumber += 1
firstString = str(line.split('\t')[0])
if str(compareString[1:].strip()) == str(firstString):
if len(line.split('\t')) > 1:
return " ".join(line.split()[1:])
except Exception as e:
logging.error(str(e))
logging.exception("Exception : ")