-
Notifications
You must be signed in to change notification settings - Fork 1
/
i3dChecker.py
570 lines (473 loc) · 19.1 KB
/
i3dChecker.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
# pylama:ignore=W605,E221,C901
"""
__ ______ __ _ _
/ / / ) / /| / / ,
-------/----/-------\-------/| /-|----__----__-/----__-/--------__----__-
/ / \ / |/ | / ) / / / / / / ) / )
_(___/____/______(____/___/__/___|_(___/_(___/___(___/___/___/___/_(___/_
/
i3dChecker.py - v1.0.2 (_ /
Version History:
v1.0.0 - Initial Release
"""
import argparse
import os
import sys
import math
import glob
import xml.etree.ElementTree as ET
from pathlib import Path
COMMON_MASKS = [
"0", "223", "8194", "262367", "524288", "1048576", "2097152", "2105410", "2109442", "3145728",
"16777216", "16781314", "16781578", "16791586", "18874368", "134217728", "134225920",
"134225922", "1073741824"
]
UNUSUAL_MASKS = [
"1024", "2048", "8196", "8212", "8450", "12290", "14591", "28704", "30736", "131072", "262399",
"524255", "2109440", "2109464", "2121728", "3170304", "16777224", "16779264", "16781312",
"16789506", "19922944", "33554432", "1075838976", "1088421888", "1090521088", "2147483648",
]
LIGHT_MAP = {
"frontLight_01": "frontLight01",
"frontLightCone_01": "frontLight02",
"frontLightOval_01": "frontLight03",
"frontLightOval_02": "frontLight04",
"frontLightOval_03": "frontLight05",
"frontLightRectangle_01": "frontLight06White",
"frontLightRectangle_01Orange": "frontLight06Orange",
"frontLightRectangle_02": "frontLight07",
"rear2ChamberLed_01": "rearLight01",
"rear2ChamberLight_01": "rearLight36",
"rear2ChamberLight_02": "rearLight02",
"rear2ChamberLight_03": "rearLight03",
"rear2ChamberLight_04": "rearLight04",
"rear2ChamberLight_05": "rearLight05",
"rear2ChamberLight_06": "rearLight06",
"rear2ChamberLight_07": "rearLight07",
"rear2ChamberTurnLed_01": "turnLight03",
"rear3ChamberLight_01": "rearLight08",
"rear3ChamberLight_02": "rearLight09",
"rear3ChamberLight_03": "rearLight10",
"rear3ChamberLight_04": "rearLight11",
"rear3ChamberLight_05": "rearLight12",
"rear3ChamberLight_06": "rearLight34",
"rear3ChamberLight_07": "rearLight35",
"rear4ChamberLight_01": "rearLight13",
"rear4ChamberLight_02": "rearLight14",
"rear5ChamberLight_01": "rearLight15",
"rear5ChamberLight_02": "rearLight16",
"rearLEDLight_01": "rearLight17",
"rearLight_01": "rearLight18",
"rearLightCircle_01Orange": "rearLight19Orange",
"rearLightCircle_01Red": "rearLight19Red",
"rearLightCircleLEDRed_01": "rearLight20",
"rearLightCircleOrange_01": "rearLight21Orange",
"rearLightCircleOrange_02": "rearLight22Orange",
"rearLightCircleRed_01": "rearLight21Red",
"rearLightCircleRed_02": "rearLight22Red",
"rearLightCircleWhite_02": "rearLight22White",
"rearLightOvalLEDChromeWhite_01": "rearLight24",
"rearLightOvalLEDOrange_01": "rearLight23Orange",
"rearLightOvalLEDRed_01": "rearLight23Red",
"rearLightOvalLEDRed_02": "rearLight25",
"rearLightOvalLEDWhite_01": "rearLight22White",
"rearLightOvalOrange_01": "rearLight26Orange",
"rearLightOvalRed_01": "rearLight26Red",
"rearLightOvalWhite_01": "rearLight26White",
"rearLightSquare_01Orange": "rearLight27Orange",
"rearMultipointLEDLight_01": "rearLight30",
"rearMultipointLight_01": "rearLight31",
"rearMultipointLight_02": "rearLight32",
"rearMultipointLight_03": "rearLight33",
"rearMultipointLight_04": "rearLight28",
"rearMultipointLight_05": "rearLight29",
"rearPlateNumberLight_01": "plateNumberLight01",
"sideMarker_01": "sideMarker01",
"sideMarker_02": "sideMarker02",
"sideMarker_03": "sideMarker03",
"sideMarker_04": "sideMarker04",
"sideMarker_04Orange": "sideMarker05Orange",
"sideMarker_04Red": "sideMarker05Red",
"sideMarker_04White": "sideMarker05White",
"sideMarker_05": "sideMarker05",
"sideMarker_05Orange": "sideMarker06Orange",
"sideMarker_05Red": "sideMarker06Red",
"sideMarker_05White": "sideMarker06White",
"sideMarker_06": "sideMarker06",
"sideMarker_06Orange": "sideMarker07Orange",
"sideMarker_06Red": "sideMarker07Red",
"sideMarker_06White": "sideMarker07White",
"sideMarker_07Orange": "sideMarker08Orange",
"sideMarker_07White": "sideMarker08White",
"sideMarker_08Orange": "sideMarker09Orange",
"sideMarker_08White": "sideMarker09White",
"sideMarker_09Orange_turn": "turnLight02",
"sideMarker_09Orange": "sideMarker10Orange",
"sideMarker_09Red": "sideMarker10Red",
"sideMarker_09White": "sideMarker10White",
"sideMarker_10Orange": "sideMarker11Orange",
"sideMarker_10Red": "sideMarker11Red",
"sideMarker_14White": "sideMarker14White",
"turnLight_01": "turnLight01",
"workingLightCircle_01": "workingLight01",
"workingLightCircle_02": "workingLight02",
"workingLightOval_01": "workingLight03",
"workingLightOval_02": "workingLight04",
"workingLightOval_03": "workingLight05",
"workingLightOval_04": "workingLight06",
"workingLightOval_05": "workingLight07",
"workingLightSquare_01": "workingLight08",
"workingLightSquare_02": "workingLight09",
"workingLightSquare_03": "workingLight10",
"workingLightSquare_04": "workingLight11",
}
def getDataFilesPath(override):
""" Try and find the data files """
INSTALL_LOCATIONS = [
"C:\Program Files (x86)\Farming Simulator 2022\data",
"C:\Program Files (x86)\Steam\steamapps\common\Farming Simulator 22\data",
"C:\Program Files\Epic Games\FarmingSimulator22\data",
]
if override is not False:
if (
not override.endswith("data/")
and not override.endswith("data")
and not override.endswith("data/")
):
print("WARNING: DATA FILES NOT FOUND: supplied path should end with \"data\\\"")
elif not os.path.isdir(override):
print("WARNING: DATA FILES NOT FOUND: Invalid Path Supplied")
else:
return override
else:
editPath = findInstalledEditor()
if editPath:
return editPath
else:
for testPath in INSTALL_LOCATIONS:
if os.path.isdir(testPath):
return testPath
print("WARNING: DATA FILES NOT FOUND: checking linked $data entries disabled.")
return ""
def findInstalledEditor():
""" Find a locally installed Giants Editor"""
localAppData = os.getenv('localappdata')
editorFolders = glob.glob(localAppData + "/GIANTS Editor*")
editorVersion = 0
editorFolder = False
try:
if len(editorFolders):
for thisFolder in editorFolders:
versionInt = int(thisFolder[-5:].replace(".", ""))
if editorVersion < versionInt:
editorFolder = thisFolder
editorVersion = versionInt
if editorFolder is not False:
if os.path.isfile(editorFolder + "/editor.xml"):
editorTree = ET.parse(editorFolder + '/editor.xml')
editorRoot = editorTree.getroot()
for thisTag in editorRoot.findall(".//gameinstallationpath"):
return thisTag.text + "data/"
except BaseException:
return False
return False
def enter_key_exit():
""" Exit when enter key pressed """
print("Press ENTER to close terminal.")
try:
input()
except BaseException:
pass
exit()
def check_local_file_cache(filename, baseFolder):
""" check if a local file exists, cache results to lessen IO """
global dataFilesPath, cachedLocalFiles
if filename in cachedLocalFiles:
return False
if os.path.isabs(filename):
return(
" FILE ERROR: " + filename +
" appears to be an absolute path. This is probably wrong."
)
absFile = clean_path(baseFolder, filename)
absFolder = os.path.normpath(baseFolder)
if not os.path.exists(absFile):
if absFile.endswith(".png"):
absFile = absFile[:-3] + "dds"
if not os.path.exists(absFile):
return(" FILE NOT FOUND: " + filename)
else:
return(" FILE NOT FOUND: " + filename)
casedFile = str(Path(absFile).resolve())
if casedFile != absFile:
return(
" FILE CASE MISMATCH: " + absFile.replace(absFolder, '') +
" vs detected:" + casedFile.replace(absFolder, '')
)
return False
def check_data_file_cache(filename):
""" check if a data file exists, cache results to lessen IO """
global dataFilesPath, cachedDataFiles
origName = filename
filename = filename[6:]
if filename in cachedDataFiles:
return False
if dataFilesPath == "":
""" Data files not found, don't do this."""
cachedDataFiles.append(filename)
return False
absFile = clean_path(dataFilesPath, filename)
absFolder = os.path.normpath(dataFilesPath)
if not os.path.isfile(absFile):
if absFile.endswith(".png"):
absFile = absFile[:-3] + "dds"
if not os.path.exists(absFile):
return(" FILE NOT FOUND: " + origName)
else:
return(" FILE NOT FOUND: " + origName)
else:
casedFile = str(Path(absFile).resolve())
if casedFile == absFile:
cachedDataFiles.append(filename)
return False
else:
return(
" FILE CASE MISMATCH: " + absFile.replace(absFolder, '') +
" vs detected:" + casedFile.replace(absFolder, '')
)
return False
def none_attrib(element, key):
""" Grab an attribute value by key, null safe"""
return None if (element.attrib is None or key not in element.attrib) else element.attrib[key]
def na_attrib(element, key):
""" Grab an attribute, use n/a instead of None"""
return "n/a" if none_attrib(element, key) is None else none_attrib(element, key)
def yn_attrib(element, key):
""" Grab an attribute, use yes/no for booleans"""
tmpVal = none_attrib(element, key)
return ("no", "yes")[tmpVal is not None and (tmpVal is True or tmpVal.lower() == 'true')]
def is_xml_true(value):
""" String convert bool and handle None """
return value is not None and (value is True or value.lower() == 'true')
def mask_uses_old_bits(mask):
""" See if any of the old bits are set."""
oldBits = [2, 6, 7, 22, 23, 26]
binMaskReversed = format(int(mask), '032b')[::-1]
for testBit in oldBits:
if binMaskReversed[testBit] == "1":
return True
return False
def mask_string_to_hex(mask):
""" Turn collision mask into a HEX number"""
return "0x" + '%X' % int(mask)
def sRGB_string_to_hex(color):
""" Convert space delimited sRGB color to HEX string"""
if color is None:
return "#FFFFFF"
return "#" + "".join(['%02X' % int(math.floor(float(part) * 255)) for part in color.split()])
def clean_path(baseFolder, filename):
baseFolderTuple = os.path.splitdrive(baseFolder)
return os.path.normpath(os.path.join(baseFolderTuple[0], baseFolderTuple[1], filename))
def check_file_links(xmlTree):
textList = ["\nCheck path of linked files:"]
badCache = []
for thisTag in xmlTree.findall(".//File"):
thisFileName = none_attrib(thisTag, "filename")
if thisFileName is not None and thisFileName not in badCache:
if thisFileName.startswith("$data/"):
fileStatus = check_data_file_cache(thisFileName)
else:
fileStatus = check_local_file_cache(thisFileName, thisFolder)
if fileStatus is not False:
textList.append(fileStatus)
badCache.append(thisFileName)
if len(textList) == 1:
textList.append(" all good.")
return textList
def check_light_attributes(xmlTree):
textList = ["\nRealLights Information:"]
for thisTag in xmlTree.findall(".//Light"):
textList.append(" Node Name: " + na_attrib(thisTag, "name"))
thisLine = " Type: " + na_attrib(thisTag, "type")
thisLine += ", Color: " + sRGB_string_to_hex(none_attrib(thisTag, "color"))
thisLine += ", Shadows: " + yn_attrib(thisTag, "castShadowMap")
thisLine += ", Range: " + na_attrib(thisTag, "range") + " m"
thisLine += ", Cone Angle: " + na_attrib(thisTag, "coneAngle") + " °"
thisLine += ", Drop Off: " + na_attrib(thisTag, "dropOff") + " m"
textList.append(thisLine)
if len(textList) == 1:
textList.append(" no lights found.")
return textList
def check_shadows(xmlTree):
textList = ["\nhasShadowMap | castsShadowMap on renderable shapes:"]
for thisTag in xmlTree.findall(".//Shape"):
thisTagNonRender = none_attrib(thisTag, "nonRenderable")
thisTagCasts = none_attrib(thisTag, "castsShadows")
thisTagReceives = none_attrib(thisTag, "receiveShadows")
if (not is_xml_true(thisTagReceives) or not is_xml_true(thisTagCasts)):
# Either missing cast or receive, check visible...
if (not is_xml_true(thisTagNonRender)):
textList.append(" Node Name: " + na_attrib(thisTag, "name"))
textList.append(
" Casts Shadow Map: " + yn_attrib(thisTag, "castsShadows") +
" | Receives Shadow Map: " + yn_attrib(thisTag, "receiveShadows")
)
if len(textList) == 1:
textList.append(" all good.")
return textList
def check_light_links(xmlTree):
textList = ["\nLinks to old lights in i3d:"]
for thisTag in xmlTree.findall(".//File"):
thisFileName = none_attrib(thisTag, "filename")
if thisFileName is not None:
for thisTestName in LIGHT_MAP.keys():
if thisTestName + "." in thisFileName:
textList.append(
" Linked Light with fileId '" + na_attrib(thisTag, "fileId") +
"' is '" + thisTestName + "', should be '" +
LIGHT_MAP[thisTestName] + "'"
)
if len(textList) == 1:
textList.append(" all good.")
return textList
def check_collisions(xmlTree):
textList = ["\nUnknown, uncommon, or depreciated collisionMasks:"]
for thisTag in xmlTree.findall(".//*[@collisionMask]"):
thisColMaskDec = none_attrib(thisTag, "collisionMask")
thisColMaskHex = mask_string_to_hex(thisColMaskDec)
thisName = na_attrib(thisTag, "name")
if thisColMaskDec not in COMMON_MASKS:
if thisColMaskDec in UNUSUAL_MASKS:
textList.append(
" collisionMask for '" + thisName + "' (" + thisColMaskDec + ")/(" +
thisColMaskHex + ") is unusual, but does exist in some giants files"
)
else:
if mask_uses_old_bits(thisColMaskDec):
textList.append(
" collisionMask for '" + thisName + "' (" + thisColMaskDec + ")/(" +
thisColMaskHex + ") uses depreciated bits and needs corrected"
)
else:
textList.append(
" collisionMask for '" + thisName + "' (" + thisColMaskDec + ")/(" +
thisColMaskHex + ") is unknown, and should be checked"
)
if len(textList) == 1:
textList.append(" all good.")
return textList
def check_rotation_scale(xmlTree):
textList = ["\nPossibly suspect rotations:"]
for thisTag in xmlTree.findall(".//Shape[@rotation]"):
if "e" in thisTag.attrib["rotation"]:
textList.append(
" Sci Notation on node: " +
na_attrib(thisTag, "name") +
" set to: " +
na_attrib(thisTag, "rotation")
)
if "-0" in thisTag.attrib["rotation"] and "180" in thisTag.attrib["rotation"]:
textList.append(
" Inverse Zero (possible negative scale in blender) on node: " +
na_attrib(thisTag, "name") +
" set to: " +
na_attrib(thisTag, "rotation")
)
if len(textList) == 1:
textList.append(" all good.")
textList.append("\nNon-Applied Scales:")
for thisTag in xmlTree.findall(".//Shape[@scale]"):
textList.append(
" Scale is not applied on: " +
na_attrib(thisTag, "name") +
" set to: " +
na_attrib(thisTag, "scale")
)
if len(textList) == 3:
textList.append(" all good.")
return textList
print("-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-")
print(" i3Checker v1.0.2")
print(" by JTSModding")
print("-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-\n")
parser = argparse.ArgumentParser(description='Sanity check an i3d file.')
parser.add_argument(
'files', metavar='files', nargs='+', type=argparse.FileType('r', encoding='utf-8')
)
parser.add_argument(
'--shadows',
help="Check for shadow maps on visible shapes",
action=argparse.BooleanOptionalAction,
default=True
)
parser.add_argument(
'--link-check',
help="Check that linked files exist",
action=argparse.BooleanOptionalAction,
default=True
)
parser.add_argument(
'--light-check',
help="Check linked lights",
action=argparse.BooleanOptionalAction,
default=True
)
parser.add_argument(
'--light-info',
help="Output realLight attributes",
action=argparse.BooleanOptionalAction,
default=True
)
parser.add_argument(
'--collisions',
help="Check for unusual or unknown collision masks",
action=argparse.BooleanOptionalAction,
default=True
)
parser.add_argument(
'--rotation-scale',
help="Check for part rotations that seem odd and find scaled parts",
action=argparse.BooleanOptionalAction,
default=True
)
parser.add_argument(
'--install-path',
help="Installation path to FS data files (.../data/)",
dest="installPath",
default=False
)
try:
args = parser.parse_args()
except BaseException:
enter_key_exit()
dataFilesPath = getDataFilesPath(args.installPath)
print("Files Found: " + str(len(args.files)))
for file in args.files:
cachedDataFiles = []
cachedLocalFiles = []
thisName = os.path.basename(file.name)
thisFolder = os.path.dirname(os.path.abspath(file.name))
print("\nTesting: " + thisName)
try:
if not file.name.endswith("i3d"):
raise BaseException
thisXML = ET.fromstring(file.read())
except BaseException:
print("ERROR: Unable to read / parse file '" + thisName + "'")
enter_key_exit()
if args.light_info:
print("\n".join(check_light_attributes(thisXML)))
if args.rotation_scale:
print("\n".join(check_rotation_scale(thisXML)))
if args.light_check:
print("\n".join(check_light_links(thisXML)))
if args.shadows:
print("\n".join(check_shadows(thisXML)))
if args.collisions:
print("\n".join(check_collisions(thisXML)))
if args.link_check:
print("\n".join(check_file_links(thisXML)))
print('\ndone.')
if sys.stdout.isatty():
# Don't pause on finish if we re-directed to a file.
enter_key_exit()