forked from Ahrkylien/BF1942-Extraction-Readout-Scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbf42_script.py
782 lines (730 loc) · 37.9 KB
/
bf42_script.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
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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
import os
import re
import math
import pickle
import json
import sys
from pathlib import PurePosixPath as BFPath
# method to store objects as strings:
def dumps(objectToDump):
return(json.dumps(objectToDump))
# return(pickle.dumps(objectToDump).hex())
def loads(stringToLoad):
return(json.loads(stringToLoad))
# return(pickle.loads(bytes.fromhex(stringToLoad)))
class BF42_vec3:
x = 0
y = 0
z = 0
def __init__(self, vertex):
if type(vertex) is str:
v_str = vertex.split('/')
v = []
for vert_str in v_str:
try:
v.append(float(vert_str))
except ValueError:
pass
if len(v) == 1 and len(v_str) == 1:
self.x = v[0]
self.y = v[0]
self.z = v[0]
elif len(v) == 2 and len(v_str) == 2:
self.x = v[0]
self.y = v[1]
elif len(v) == 3 and len(v_str) == 3:
self.x = v[0]
self.y = v[1]
self.z = v[2]
else:
self.x = vertex[0]
self.y = vertex[1]
self.z = vertex[2]
def __eq__(self, other):
if not isinstance(other, BF42_vec3):
return False
return self.x == other.x and self.y == other.y and self.z == other.z
def str(self, numberOfSignif=6):
strings = []
for v in self.lst():
if v == 0:
v = 0 # prevent negative 0 (cosmetic)
nrOfDigitsBeforeDot = int(math.log10(abs(v)))+1 if v != 0 else 0
significance = max(6,4+nrOfDigitsBeforeDot)
strings.append("%.*g" % (significance, v))
return(strings[0]+"/"+strings[1]+"/"+strings[2])
def str_floor(self, numberOfSignif=6): #for lightmap
strings = []
for v in self.lst():
if v == 0:
v = 0 # prevent negative 0 (cosmetic)
nrOfDigitsBeforeDot = int(math.log10(abs(v)))+1 if v != 0 else 0
significance = max(6,4+nrOfDigitsBeforeDot)
string = "%.*g" % (significance, v)
if "e" in string:
strings.append("0")
else:
strings.append(string.split(".")[0])
return(strings[0]+"-"+strings[1]+"-"+strings[2])
def lst(self): # return new vector
return([self.x, self.y, self.z])
def toBlend(self,sceneScale = 1):
return([self.x*sceneScale, self.z*sceneScale, self.y*sceneScale])
def rotate(self, vec): # rotate vector yaw/pitch/roll
ref = self.copy()
self.x = ref.x*math.cos(math.radians(vec.x)) + ref.z*math.sin(math.radians(vec.x))
self.z = -ref.x*math.sin(math.radians(vec.x)) + ref.z*math.cos(math.radians(vec.x))
ref = self.copy()
self.y = ref.y*math.cos(math.radians(vec.y)) - ref.z*math.sin(math.radians(vec.y))
self.z = ref.y*math.sin(math.radians(vec.y)) + ref.z*math.cos(math.radians(vec.y))
ref = self.copy()
self.x = ref.x*math.cos(math.radians(vec.z)) - ref.y*math.sin(math.radians(vec.z))
self.y = ref.x*math.sin(math.radians(vec.z)) + ref.y*math.cos(math.radians(vec.z))
return(self)
def add(self, vec): # return new vector
self.x += vec.x
self.y += vec.y
self.z += vec.z
return(self)
def copy(self): # return new vector
return(BF42_vec3((self.x,self.y,self.z)))
def bf42_vec3_Add(v1,v2):
v = BF42_vec3((v1.x,v1.y,v1.z))
return(v.add(v2))
class BF42_command:
def __init__(self, cmd_str):
self.className = None; self.method = None; self.arguments = []; self.targetVariable = None
# regex = "^([\t\f ]*)([^\t^\f^ ^\.^\n]*)(?:(\.)([^\t^\f^ ^\n]*)){0,1}([\t\f ]*)([^\n]*)" # this is the regex to parse a whole file
regex = "^[\t\f ]*([^\t^\f^ ^\.]*)(?:\.([^\t^\f^ ^\n]*)){0,1}[\t\f ]*(.*)"
regexResult = re.findall(regex, cmd_str, flags = re.ASCII|re.IGNORECASE|re.MULTILINE)
if len(regexResult) == 1:
splitted = regexResult[0]
if splitted[0] != "": self.className = splitted[0]
if splitted[1] != "": self.method = splitted[1]
if splitted[2] != "":
arguments_str = splitted[2]
regex = r'(?:"(?:(?:.*?")|(?:.*)))|(?:[^\t^\f^ ]+)'
self.arguments = [argument.replace('"', '') for argument in re.findall(regex, arguments_str, flags = re.ASCII)]
if len(self.arguments) > 1 and self.arguments[-2] == "->" and self.arguments[-1].lower().startswith("v_"):
self.targetVariable = self.arguments[-1]
self.arguments = self.arguments[:-2]
def __eq__(self, commandString): #commandString has className.method as format where either part can be empty
parts = commandString.split('.', 1)
className = True if parts[0] in ['', '*'] else parts[0]
method = True if len(parts) == 1 or parts[1] in ['', '*'] else parts[1]
if className != True:
className = self.className.lower() == className.lower()
if method != True:
method = isMethod(self.method, method)
return className == True and method == True
def isMethod(method, methodReference):
return(method.lower() == methodReference.lower() or method.lower() == "set"+methodReference.lower())
def bf42_is_linked(template):
return(type(template) != str or type(template) == int)
class BF42_data:
def __init__(self):
self.objectTemplates = []
self.networkableInfos = []
self.geometryTemplates = []
self.objects = []
self.staticObjects = [] # subCatergory of objects
self.active_ObjectTemplate = None
self.active_NetworkableInfo = None
self.active_GeometryTemplate = None
self.active_Object = None
self.textureManager_alternativePaths = []
self.console_worldSize = None
self.game = BF42_Game()
self.variables = {}
self.constants = {}
self.lastObjectTemplateID = -1
self.lastObjectID = -1
with open('constants.txt') as file:
for line in file:
parts = line.strip().split()
self.constants[parts[0]] = parts[1]
def getNextObjectTemplateID(self):
self.lastObjectTemplateID += 1
return(self.lastObjectTemplateID)
def getNextObjectID(self):
self.lastObjectID += 1
return(self.lastObjectID)
def getObject(self, name):
for object in self.objects:
if object.name.lower() == name.lower():
return(objectTemplate)
return(None)
def getObjectTemplate(self, name):
for objectTemplate in self.objectTemplates:
if objectTemplate.name.lower() == name.lower():
return(objectTemplate)
# print("could not find objectTemplate: "+name)
return(None)
def getNetworkableInfo(self, name):
for networkableInfo in self.networkableInfos:
if networkableInfo.name.lower() == name.lower():
return(networkableInfo)
return(None)
def getGeometryTemplate(self, name):
for geometryTemplate in self.geometryTemplates:
if geometryTemplate.name.lower() == name.lower():
return(geometryTemplate)
return(None)
def creatLinks(self):
for object in self.objects:
if not bf42_is_linked(object.template):
template = self.getObjectTemplate(object.template)
if template != None:
object.template = template
for objectTemplate in self.objectTemplates:
for child in objectTemplate.childeren:
if not bf42_is_linked(child.template):
template = self.getObjectTemplate(child.template)
if template != None:
child.template = template
template.parents.append(objectTemplate)
if objectTemplate.networkableInfo:
if not bf42_is_linked(objectTemplate.networkableInfo):
objectTemplate.networkableInfo = self.getNetworkableInfo(objectTemplate.networkableInfo)
geometry = self.getGeometryTemplate(objectTemplate.geometry)
if geometry != None:
objectTemplate.geometry = geometry
def dumps(self):
list_dump = [[],[],[],[]]
for objectTemplate in self.objectTemplates:
geometry = self.geometryTemplates.index(objectTemplate.geometry) if bf42_is_linked(objectTemplate.geometry) else objectTemplate.geometry
childeren = []
for child in objectTemplate.childeren:
template = self.objectTemplates.index(child.template) if bf42_is_linked(child.template) else child.template
childeren.append([template, child.setPosition.lst(), child.setRotation.lst()])
linePoints = [linePoint.lst() for linePoint in objectTemplate.linePoints]
list_dump[0].append([objectTemplate.type, objectTemplate.name, geometry, objectTemplate.triggerRadius, linePoints, childeren])
for geometryTemplate in self.geometryTemplates:
list_dump[1].append([geometryTemplate.type, geometryTemplate.name, geometryTemplate.scale.lst(), geometryTemplate.file, geometryTemplate.materialSize, geometryTemplate.worldSize, geometryTemplate.yScale, geometryTemplate.waterLevel])
for object in self.objects:
template = self.objectTemplates.index(object.template) if bf42_is_linked(object.template) else object.template
list_dump[2].append([template, object.absolutePosition.lst(), object.rotation.lst(), object.geometry_scale.lst()])
for staticObject in self.staticObjects:
list_dump[3].append(self.objects.index(staticObject))
return(dumps(list_dump))
def loads(self, dataDump):
list_dump = loads(dataDump)
# load objectTemplates
for (type, name, geometry, triggerRadius, linePoints, childeren) in list_dump[0]:
objectTemplate = BF42_ObjectTemplate(type, name)
objectTemplate.geometry = geometry
objectTemplate.triggerRadius = triggerRadius
objectTemplate.linePoints = [BF42_vec3(linePoint) for linePoint in linePoints]
for (template, setPosition, setRotation) in childeren:
objectTemplateChild = BF42_ObjectTemplateChild(template)
objectTemplateChild.setPosition = BF42_vec3(setPosition)
objectTemplateChild.setRotation = BF42_vec3(setRotation)
objectTemplate.childeren.append(objectTemplateChild)
self.objectTemplates.append(objectTemplate)
# load geometryTemplates
for (type, name, scale, file, materialSize, worldSize, yScale, waterLevel) in list_dump[1]:
geometryTemplate = BF42_GeometryTemplate(type, name)
geometryTemplate.scale = BF42_vec3(scale)
geometryTemplate.file = file
geometryTemplate.materialSize = materialSize
geometryTemplate.worldSize = worldSize
geometryTemplate.yScale = yScale
geometryTemplate.waterLevel = waterLevel
self.geometryTemplates.append(geometryTemplate)
# link objectTemplates
for objectTemplate in self.objectTemplates:
for child in objectTemplate.childeren:
if bf42_is_linked(child.template):
child.template = self.objectTemplates[child.template]
if bf42_is_linked(objectTemplate.geometry):
objectTemplate.geometry = self.geometryTemplates[objectTemplate.geometry]
# load and link objects
for (template, absolutePosition, rotation, geometry_scale) in list_dump[2]:
object = BF42_Object("")
object.template = self.objectTemplates[template] if bf42_is_linked(template) else template
object.absolutePosition = BF42_vec3(absolutePosition)
object.rotation = BF42_vec3(rotation)
object.geometry_scale = BF42_vec3(geometry_scale)
self.objects.append(object)
# load and link staticObjects
for i in list_dump[3]:
self.staticObjects.append(self.objects[i])
return(self)
predictionModeEnum = ['PMNone', 'PMLinear', 'PMCubic', 'PMUsePhysics']
class BF42_Game:
def __init__(self):
self.mapId = None
self.activeCombatArea = None
self.customGameName = None
self.customGameVersion = None
self.multiplayerBriefingObjectives = None
self.objectiveBriefing = None
self.modPaths = []
def execMethod(self, methodName, arguments):
def setMapId(value): self.mapId = value
def setActiveCombatArea(a,b,c,d): self.activeCombatArea = (int(a), int(b), int(c), int(d))
def customGameName(value = None):
if value != None: self.customGameName = value
return(self.customGameName)
def customGameVersion(value = None):
if value != None: self.customGameVersion = value
return(self.customGameVersion)
def addModPath(value): self.modPaths.append(value)
def setMultiplayerBriefingObjectives(value): self.multiplayerBriefingObjectives = value
def setObjectiveBriefing(value): self.objectiveBriefing = value
methods = locals()
methods = {name: methods[name] for name in methods if not name in ['methodName', 'arguments']}
for method in methods:
if isMethod(methodName, method):
try: return(methods[method](*arguments))
except: pass
break
return(False)
class BF42_ObjectTemplate:
def __init__(self, type, name, ID):
self.ID = ID
self.type = type
self.name = name
self.networkableInfo = None
self.geometry = "" # string will be replaced by a reference after linking
self.maxHitPoints = 10
self.minRotation = BF42_vec3((0,0,0))
self.maxRotation = BF42_vec3((0,0,0))
self.maxSpeed = BF42_vec3((1,1,1))
self.acceleration = BF42_vec3((0.1,0.1,0.1))
self.inputToYaw = 55
self.inputToPitch = 55
self.inputToRoll = 55
self.automaticReset = False
self.magSize = 30
self.numOfMag = 3
self.numberOfGears = None
self.gearUp = 0.7
self.gearDown = 0.3
self.triggerRadius = 0
self.linePoints = []
self.controlPointName = ""
self.team = None
self.unableToChangeTeam = None
self.MinSpawnDelay = None
self.MaxSpawnDelay = None
self.SpawnDelayAtStart = None
self.TimeToLive = None
self.Distance = None
self.DamageWhenLost = None
self.maxNrOfObjectSpawned = None
self.teamOnVehicle = None
self.objectTemplates = {} # for objectSpawners
self.childeren = []
self.active_child = None
self.parents = [] # not used inside module
def execMethod(self, methodName, arguments):
def networkableInfo(value):
if value != None: self.networkableInfo = value
return(self.networkableInfo)
def geometry(value):
if value != None: self.geometry = value
return(self.geometry)
def maxHitPoints(value = None):
if value != None: self.maxHitPoints = float(value)
return(self.maxHitPoints)
def minRotation(value = None):
if value != None: self.minRotation = BF42_vec3(value)
return(self.minRotation)
def maxRotation(value = None):
if value != None: self.maxRotation = BF42_vec3(value)
return(self.maxRotation)
def maxSpeed(value = None):
if value != None: self.maxSpeed = BF42_vec3(value)
return(self.maxSpeed)
def acceleration(value = None):
if value != None: self.acceleration = BF42_vec3(value)
return(self.acceleration)
def inputToPitch(value = None):
if value != None: self.inputToPitch = int(value)
return(self.inputToPitch)
def inputToYaw(value = None):
if value != None: self.inputToYaw = int(value)
return(self.inputToYaw)
def inputToRoll(value = None):
if value != None: self.inputToRoll = int(value)
return(self.inputToRoll)
def automaticReset(value = None):
if value != None: self.automaticReset = bool(int(value))
return(self.automaticReset)
def magSize(value = None):
if value != None: self.magSize = int(value)
return(self.magSize)
def numOfMag(value = None):
if value != None: self.numOfMag = int(value)
return(self.numOfMag)
def numberOfGears(value = None):
if value != None: self.numberOfGears = int(value)
return(self.numberOfGears)
def gearUp(value = None):
if value != None: self.gearUp = float(value)
return(self.gearUp )
def gearDown(value = None):
if value != None: self.gearDown = float(value)
return(self.gearDown)
def triggerRadius(value): self.triggerRadius = int(value)
def addLinePoint(value): self.linePoints.append(BF42_vec3(value))
def controlPointName(value): self.controlPointName = value
def team(value): self.team = value
def unableToChangeTeam(value): self.unableToChangeTeam = value
def MinSpawnDelay(value): self.MinSpawnDelay = value
def MaxSpawnDelay(value): self.MaxSpawnDelay = value
def SpawnDelayAtStart(value): self.SpawnDelayAtStart = value
def TimeToLive(value): self.TimeToLive = value
def Distance(value): self.Distance = value
def DamageWhenLost(value): self.DamageWhenLost = value
def maxNrOfObjectSpawned(value): self.maxNrOfObjectSpawned = value
def teamOnVehicle(value): self.teamOnVehicle = value
def setObjectTemplate(key, value): self.objectTemplates[int(key)] = value
def addTemplate(value):
self.active_child = BF42_ObjectTemplateChild(value)
self.childeren.append(self.active_child)
def setActiveTemplate(value):
if len(self.childeren) > int(value):
self.active_child = self.childeren[int(value)]
def removeTemplate(value):
if len(self.childeren) > int(value):
self.childeren.pop(int(value))
def setPosition(value):
if self.active_child != None:
self.active_child.setPosition = BF42_vec3(value)
def setRotation(value):
if self.active_child != None:
self.active_child.setRotation = BF42_vec3(value)
methods = locals()
methods = {name: methods[name] for name in methods if not name in ['methodName', 'arguments']}
for method in methods:
if isMethod(methodName, method):
try: return(methods[method](*arguments))
except: pass
break
return(False)
class BF42_ObjectTemplateChild:
def __init__(self, template):
self.template = template
self.setPosition = BF42_vec3((0,0,0))
self.setRotation = BF42_vec3((0,0,0))
class BF42_NetworkableInfo:
def __init__(self, name):
self.name = name
self.isUnique = False
self.basePriority = 1.0
self.predictionMode = 0 # PMNone
self.predictionMode = 0 # PMNone
self.forceNetworkableId = False
def execMethod(self, methodName, arguments):
def setBasePriority(value): self.basePriority = float(value)
def setIsUnique(value): self.isUnique = bool(int(value))
def setPredictionMode(value): self.predictionMode = predictionModeEnum.index(value)
methods = locals()
methods = {name: methods[name] for name in methods if not name in ['methodName', 'arguments']}
for method in methods:
if isMethod(methodName, method):
try: return(methods[method](*arguments))
except: pass
break
return(False)
class BF42_GeometryTemplate:
def __init__(self, type, name):
self.type = type
self.name = name
self.scale = BF42_vec3((1,1,1))
self.file = None
self.materialSize = 256
self.worldSize = 1024
self.yScale = 1
self.waterLevel = 0
def execMethod(self, methodName, arguments):
def scale(value): self.scale = BF42_vec3(value)
def file(value): self.file = value.replace("\\","/")
def materialsize(value): self.materialsize = int(value)
def worldsize(value): self.worldsize = int(value)
def yscale(value): self.yscale = float(value)
def waterlevel(value): self.waterlevel = float(value)
methods = locals()
methods = {name: methods[name] for name in methods if not name in ['methodName', 'arguments']}
for method in methods:
if isMethod(methodName, method):
try: methods[method](*arguments)
except: pass
break
class BF42_Object:
def __init__(self, template, ID):
self.ID = ID
self.template = template
self.name = "" # toDo: find out the object ID/name logic/generation
self.absolutePosition = BF42_vec3((0,0,0))
self.rotation = BF42_vec3((0,0,0))
self.geometry_scale = BF42_vec3((1,1,1))
self.OSId = None
self.team = None
def setProperty(self, name, arguments):
if len(arguments) == 1: # all used commands thus far require 1 argument
value = arguments[0]
if isMethod(name, "absoluteposition"): self.absolutePosition = BF42_vec3(value)
elif isMethod(name, "rotation"): self.rotation = BF42_vec3(value)
elif isMethod(name, "geometry.scale"): self.geometry_scale = BF42_vec3(value)
elif isMethod(name, "osid"): self.OSId = value
elif isMethod(name, "team"): self.team = value
elif isMethod(name, "name"): self.name = value
def bf42_evaluate(value1, operator, value2):
if operator == "==":
return(value1.lower() == value2.lower())
return(False)
class BF42_script:
def __init__(self, data = None, rfaGroup = None):
if data == None: data = BF42_data()
self.REM = False
self.IFs = [] # 0 = False, 1 = True, 2 = has already been True
self.rfaGroup = rfaGroup
self.data = data
def read(self, path, staticObjects = False, forceExternalPath = False, v_args = None):
data = self.data
if v_args != None:
for i, v_arg in enumerate(v_args):
data.variables["v_arg"+str(i+1)] = v_arg
lines = []
try:
if self.rfaGroup == None or forceExternalPath:
fp = open(path, 'r', errors='replace')
lines = fp.readlines()
else:
fileString = self.rfaGroup.extractFile(str(path), asString = True)
if fileString == False:
raise Exception(f"Can't find path in RFA: {path}")
lines = iter(fileString.splitlines())
except:
print("Could not find file: "+path, file = sys.stderr)
for lineNumber, line_raw in enumerate(lines):
try:
line = line_raw.strip()
command = BF42_command(line)
if command.className != None:
numArgs = len(command.arguments)
if command not in ["var", "const"]:
for i in range(numArgs):
if command.arguments[i].lower().startswith('v_'):
command.arguments[i] = data.variables[command.arguments[i]] if command.arguments[i] in data.variables else command.arguments[i]
elif command.arguments[i].lower().startswith('c_'):
command.arguments[i] = data.constants[command.arguments[i]] if command.arguments[i] in data.constants else command.arguments[i]
if command == "beginrem":
self.REM = True
elif command == "endrem":
self.REM = False
elif not command == "rem" and not self.REM:
if command == "if" and numArgs == 3:
if bf42_evaluate(*command.arguments):
self.IFs.append(1)
else:
self.IFs.append(0)
elif command == "elseif" and numArgs == 3:
if len(self.IFs) > 0:
if self.IFs[-1] == 0:
if bf42_evaluate(*command.arguments):
self.IFs[-1] = 1
elif self.IFs[-1] == 1:
self.IFs[-1] = 2
else:
pass # elseif without if
elif command == "else" and numArgs == 0:
if len(self.IFs) > 0:
if self.IFs[-1] == 0:
self.IFs[-1] = 1
elif self.IFs[-1] == 1:
self.IFs[-1] = 2
else:
pass # else without if
elif command == "endif" and numArgs == 0:
if len(self.IFs) > 0:
self.IFs.pop()
else:
pass # endif without if
elif not any(x in self.IFs for x in [0, 2]):
if command.method != None:
if command == "objectTemplate":
if command == ".create":
if numArgs == 2:
if data.getObjectTemplate(command.arguments[1]) == None:
data.active_ObjectTemplate = BF42_ObjectTemplate(command.arguments[0], command.arguments[1], data.getNextObjectTemplateID())
data.objectTemplates.append(data.active_ObjectTemplate)
elif command == ".active":
if numArgs == 1:
refered_ObjectTemplate = data.getObjectTemplate(command.arguments[0])
if refered_ObjectTemplate != None:
data.active_ObjectTemplate = refered_ObjectTemplate
else:
if data.active_ObjectTemplate != None:
data.active_ObjectTemplate.execMethod(command.method, command.arguments)
if command == "networkableInfo":
if command == ".createNewInfo":
if numArgs == 1:
if data.getNetworkableInfo(command.arguments[0]) == None:
data.active_NetworkableInfo = BF42_NetworkableInfo(command.arguments[0])
data.networkableInfos.append(data.active_NetworkableInfo)
else:
if data.active_NetworkableInfo != None:
data.active_NetworkableInfo.execMethod(command.method, command.arguments)
if command == "geometryTemplate":
if command == ".create":
if numArgs == 2:
if data.getGeometryTemplate(command.arguments[1]) == None:
data.active_GeometryTemplate = BF42_GeometryTemplate(command.arguments[0], command.arguments[1])
data.geometryTemplates.append(data.active_GeometryTemplate)
elif command == ".active":
if numArgs == 1:
refered_GeometryTemplate = data.getGeometryTemplate(command.arguments[0])
if refered_GeometryTemplate != None:
data.active_GeometryTemplate = refered_GeometryTemplate
else:
if data.active_GeometryTemplate != None:
data.active_GeometryTemplate.execMethod(command.method, command.arguments)
elif command == "object":
if command == ".create":
if numArgs == 1:
data.active_Object = BF42_Object(command.arguments[0], data.getNextObjectID())
data.objects.append(data.active_Object)
if staticObjects:
data.staticObjects.append(data.active_Object)
elif command == ".active":
if numArgs == 1:
refered_Object = data.getObject(command.arguments[0])
if refered_Object != None:
data.active_Object = refered_Object
else:
if data.active_Object != None:
data.active_Object.setProperty(command.method, command.arguments)
elif command == "texturemanager":
if command == ".alternativepath":
if numArgs == 1:
data.textureManager_alternativePaths.append(command.arguments[0])
elif command == "game":
returnValue = data.game.execMethod(command.method, command.arguments)
if command.targetVariable != None and returnValue != False and command.targetVariable in data.variables:
data.variables[command.targetVariable] = returnValue
elif command == "console":
if command == ".worldsize":
if numArgs == 1:
data.console_worldSize = int(command.arguments[0])
else:
if command == "include":
if numArgs == 1:
path_include = os.path.relpath(str(BFPath(path).parent / command.arguments[0]))
self.read(path_include, data)
elif command == "run":
if numArgs >= 1:
path_run = BFPath(command.arguments[0])
if path_run.suffix == "":
path_run = path_run.with_suffix(".con")
path_run = os.path.relpath(str(BFPath(path).parent / path_run))
v_args_run = command.arguments[1:] if len(command.arguments) > 1 else []
BF42_script(data = data, rfaGroup = self.rfaGroup).read(path_run, v_args = v_args_run)
elif command == "var":
if numArgs == 3:
data.variables[command.arguments[0]] = command.arguments[2]
elif numArgs == 1:
if command.arguments[0] not in data.variables:
data.variables[command.arguments[0]] = "" # or should it be set to None?
elif command == "const":
if numArgs == 3:
data.constants[command.arguments[0]] = command.arguments[2]
elif numArgs == 1:
if command.arguments[0] not in data.constants:
data.constants[command.arguments[0]] = "" # or should it be set to None?
elif command.className.lower().startswith('v_'):
if numArgs == 2:
if command.className in data.variables:
data.variables[command.className] = command.arguments[1]
elif command.className.lower().startswith('c_'):
if numArgs == 2:
if command.className in data.constants:
data.constants[command.className] = command.arguments[1]
except:
print(f'Exception in BF42_script.read(): {path} ({lineNumber}): {line}', file = sys.stderr)
return(self.data)
def bf42_readAllScripts(bf42_data, base_path, level = None):
for path, subdirs, files in os.walk(BFPath(base_path) / "Objects"):
for name in files:
filePath = BFPath(path, name)
if filePath.suffix.lower() == ".con":
BF42_script(bf42_data).read(filePath)
if level != None:
BF42_script(bf42_data).read(BFPath(base_path) / "Bf1942/Levels" / level / "Init.con", v_args = ["host"])
BF42_script(bf42_data).read(BFPath(base_path) / "Bf1942/Levels" / level / "Conquest.con", v_args = ["host"])
BF42_script(bf42_data).read(BFPath(base_path) / "Bf1942/Levels" / level / "StaticObjects.con", staticObjects = True, v_args = ["host"])
def bf42_writeStaticCon(path, objects, data):
data.objects = objects
data.creatLinks()
with open(path, 'w') as f:
for object in objects:
templateName = object.template.name if bf42_is_linked(object.template) else object.template
f.write("object.create "+templateName+"\n")
f.write("object.absolutePosition "+object.absolutePosition.str()+"\n")
f.write("object.rotation "+object.rotation.str()+"\n")
if bf42_is_linked(object.template):
meshes = bf42_listAllGeometries(object.template)
for mesh in meshes[0]:
if mesh[1].lower() == "treemesh":
f.write("object.geometry.scale 1\n")
break
f.write("\n")
return objects
def bf42_readAllConFiles(base_path, level):
bf42_data = BF42_data()
bf42_readAllScripts(bf42_data, base_path, level)
bf42_data.creatLinks()
return(bf42_data)
# These two functions are for processing in Blender:
def bf42_listAllGeometries(objectTemplate, pos = None, rot = None, isFarLod = False):
# ToDo:
# child templates are first moved and then rotated (relative to the parent origin)
if pos == None:
pos = BF42_vec3((0,0,0))
if rot == None:
rot = BF42_vec3((0,0,0))
list = [[],[]] # [[close LOD] , [far LOD]]
if bf42_is_linked(objectTemplate.geometry):
geometryTemplate = objectTemplate.geometry
if geometryTemplate.file != "":
list[1 if isFarLod else 0].append((geometryTemplate.file, geometryTemplate.type, pos, rot))
for i, child in enumerate(objectTemplate.childeren):
if objectTemplate.type.lower() == "lodobject":
if not len(objectTemplate.childeren) in [2,3]:
print("Error: "+objectTemplate.name+" has wrong number of children for LodObject!!")
if i == 1:
isFarLod = True
if i == 2: # dont add destroyed LOD
break
if bf42_is_linked(child.template):
subList = bf42_listAllGeometries(child.template, bf42_vec3_Add(pos, child.setPosition.copy().rotate(rot)), bf42_vec3_Add(rot, child.setRotation), isFarLod) # can I add rotation vectors?
list[0] += subList[0]
list[1] += subList[1]
return(list)
def bf42_listAllGeometries_new(objectTemplate, pos = None, rot = None, isFarLod = False):
# ToDo:
# child templates are first moved and then rotated (relative to the parent origin)
if pos == None:
pos = BF42_vec3((0,0,0))
if rot == None:
rot = BF42_vec3((0,0,0))
list = [[],[]] # [[close LOD] , [far LOD]]
if bf42_is_linked(objectTemplate.geometry):
if objectTemplate.geometry.file != "":
list[1 if isFarLod else 0].append((objectTemplate.geometry, pos, rot))
for i, child in enumerate(objectTemplate.childeren):
if objectTemplate.type.lower() == "lodobject":
if not len(objectTemplate.childeren) in [2,3]:
print("Error: "+objectTemplate.name+" has wrong number of childeren for LodObject!!")
if i == 1:
isFarLod = True
if i == 2: # dont add destroyed LOD
break
if bf42_is_linked(child.template):
subList = bf42_listAllGeometries_new(child.template, bf42_vec3_Add(pos, child.setPosition.copy().rotate(rot)), bf42_vec3_Add(rot, child.setRotation), isFarLod) # can I add rotation vectors?
list[0] += subList[0]
list[1] += subList[1]
return(list)