forked from loki79uk/FS25_UniversalAutoload
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUniversalAutoloadInstaller.lua
More file actions
2332 lines (1989 loc) · 89.3 KB
/
UniversalAutoloadInstaller.lua
File metadata and controls
2332 lines (1989 loc) · 89.3 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
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
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
-- ============================================================= --
-- Universal Autoload MOD - MANAGER
-- ============================================================= --
-- manager
UniversalAutoloadManager = {}
addModEventListener(UniversalAutoloadManager)
UniversalAutoloadManager.DEBUG_STEPS = nil
-- specialisation
g_specializationManager:addSpecialization('universalAutoload', 'UniversalAutoload', Utils.getFilename('UniversalAutoload.lua', g_currentModDirectory), "")
TypeManager.validateTypes = Utils.appendedFunction(TypeManager.validateTypes, function(self)
if self.typeName == "vehicle" then
print("UAL - VALIDATE TYPES")
UniversalAutoloadManager.injectSpecialisation()
end
end)
local ROOT = getmetatable(_G).__index
ROOT.delete = Utils.appendedFunction(ROOT.delete, function(nodeId)
if UniversalAutoload.SPLITSHAPES_LOOKUP[nodeId] then
-- print("DELETED SPLITSHAPE " .. tostring(nodeId))
local object = UniversalAutoload.SPLITSHAPES_LOOKUP[nodeId]
UniversalAutoload.clearPalletFromAllVehicles(nil, object)
UniversalAutoload.SPLITSHAPES_LOOKUP[nodeId] = nil
end
end)
SplitShapeUtil.splitShape = Utils.appendedFunction(SplitShapeUtil.splitShape, function(nodeId)
if UniversalAutoload.SPLITSHAPES_LOOKUP[nodeId] then
-- print("DO SPLIT SPLITSHAPE " .. tostring(nodeId))
local object = UniversalAutoload.SPLITSHAPES_LOOKUP[nodeId]
UniversalAutoload.clearPalletFromAllVehicles(nil, object)
UniversalAutoload.SPLITSHAPES_LOOKUP[nodeId] = nil
end
end)
-- Create a new store pack to group all UAL supported vehicles
g_storeManager:addModStorePack("UNIVERSALAUTOLOAD", g_i18n:getText("configuration_universalAutoload", g_currentModName), "icons/storePack_ual.dds", g_currentModDirectory)
-- external classes
source(UniversalAutoload.path .. "scripts/BoundingBox.lua")
source(UniversalAutoload.path .. "scripts/LoadingVolume.lua")
source(UniversalAutoload.path .. "gui/InGameMenuUALSettings.lua")
source(UniversalAutoload.path .. "gui/ShopConfigMenuUALSettings.lua")
-- class variables
UniversalAutoload.userSettingsFile = "modSettings/UniversalAutoload.xml"
UniversalAutoload.SHOP_ICON = UniversalAutoload.path .. "icons/shop_icon.dds"
-- class tables
UniversalAutoload.ACTIONS = {
["TOGGLE_LOADING"] = "UNIVERSALAUTOLOAD_TOGGLE_LOADING",
["UNLOAD_ALL"] = "UNIVERSALAUTOLOAD_UNLOAD_ALL",
["TOGGLE_TIPSIDE"] = "UNIVERSALAUTOLOAD_TOGGLE_TIPSIDE",
["TOGGLE_FILTER"] = "UNIVERSALAUTOLOAD_TOGGLE_FILTER",
["TOGGLE_HORIZONTAL"] = "UNIVERSALAUTOLOAD_TOGGLE_HORIZONTAL",
["CYCLE_MATERIAL_FW"] = "UNIVERSALAUTOLOAD_CYCLE_MATERIAL_FW",
["CYCLE_MATERIAL_BW"] = "UNIVERSALAUTOLOAD_CYCLE_MATERIAL_BW",
["SELECT_ALL_MATERIALS"] = "UNIVERSALAUTOLOAD_SELECT_ALL_MATERIALS",
["CYCLE_CONTAINER_FW"] = "UNIVERSALAUTOLOAD_CYCLE_CONTAINER_FW",
["CYCLE_CONTAINER_BW"] = "UNIVERSALAUTOLOAD_CYCLE_CONTAINER_BW",
["SELECT_ALL_CONTAINERS"] = "UNIVERSALAUTOLOAD_SELECT_ALL_CONTAINERS",
-- ["TOGGLE_BELTS"] = "UNIVERSALAUTOLOAD_TOGGLE_BELTS",
-- ["TOGGLE_DOOR"] = "UNIVERSALAUTOLOAD_TOGGLE_DOOR",
-- ["TOGGLE_CURTAIN"] = "UNIVERSALAUTOLOAD_TOGGLE_CURTAIN",
["TOGGLE_SHOW_DEBUG"] = "UNIVERSALAUTOLOAD_TOGGLE_SHOW_DEBUG",
["TOGGLE_SHOW_LOADING"] = "UNIVERSALAUTOLOAD_TOGGLE_SHOW_LOADING",
["TOGGLE_BALE_COLLECTION"] = "UNIVERSALAUTOLOAD_TOGGLE_BALE_COLLECTION",
}
UniversalAutoload.WARNINGS = {
[1] = "warning_UNIVERSALAUTOLOAD_CLEAR_UNLOADING_AREA",
[2] = "warning_UNIVERSALAUTOLOAD_NO_OBJECTS_FOUND",
[3] = "warning_UNIVERSALAUTOLOAD_UNABLE_TO_LOAD_OBJECT_FULL",
[4] = "warning_UNIVERSALAUTOLOAD_UNABLE_TO_LOAD_OBJECT_EMPTY",
[5] = "warning_UNIVERSALAUTOLOAD_NO_LOADING_UNLESS_STATIONARY",
}
UniversalAutoload.WARNINGS_BY_NAME = {
["CLEAR_UNLOADING_AREA"] = 1,
["NO_OBJECTS_FOUND"] = 2,
["UNABLE_TO_LOAD_FULL"] = 3,
["UNABLE_TO_LOAD_EMPTY"] = 4,
["NO_LOADING_UNLESS_STATIONARY"] = 5,
}
UniversalAutoload.CONTAINERS = {
[1] = "ALL",
[2] = "EURO_PALLET",
[3] = "BIGBAG_PALLET",
[4] = "LIQUID_TANK",
[5] = "BIGBAG",
[6] = "BALE",
[7] = "LOGS",
}
-- DEFINE DEFAULTS FOR CONTAINER TYPES
-- UniversalAutoload.ALL = { sizeX = 1.250, sizeY = 0.850, sizeZ = 0.850 }
-- UniversalAutoload.EURO_PALLET = { sizeX = 1.250, sizeY = 0.790, sizeZ = 0.850 }
-- UniversalAutoload.BIGBAG_PALLET = { sizeX = 1.525, sizeY = 1.075, sizeZ = 1.200 }
-- UniversalAutoload.LIQUID_TANK = { sizeX = 1.433, sizeY = 1.500, sizeZ = 1.415 }
-- UniversalAutoload.BIGBAG = { sizeX = 1.050, sizeY = 1.666, sizeZ = 0.866, neverStack=true }
-- UniversalAutoload.BALE = { isBale=true }
UniversalAutoload.VEHICLES = {} -- actual vehicles currently in game
UniversalAutoload.VEHICLE_CONFIGURATIONS = {} -- settings for each vehicle configuration
UniversalAutoload.VEHICLE_TYPES = {} -- vehicleTypes with autoload spec
UniversalAutoload.LOADING_TYPES = {} -- known container object types
UniversalAutoload.GLOBAL_DEFAULTS = {
{id="showDebug", default=false, valueType="BOOL", key="#showDebug", description="Show the full graphical debugging display for all vehicles in game"},
{id="highPriority", default=true, valueType="BOOL", key="#highPriority", description="Apply high priority to all UAL key bindings in the F1 menu"},
{id="disableAutoStrap", default=false, valueType="BOOL", key="#disableAutoStrap", description="Disable the automatic application of tension belts"},
{id="pricePerLog", default=0, valueType="FLOAT", key="#pricePerLog", description="The price charged for each auto-loaded log (default is zero)"},
{id="pricePerBale", default=0, valueType="FLOAT", key="#pricePerBale", description="The price charged for each auto-loaded bale (default is zero)"},
{id="pricePerPallet", default=0, valueType="FLOAT", key="#pricePerPallet", description="The price charged for each auto-loaded pallet (default is zero)"},
{id="minLogLength", default=0, valueType="FLOAT", key="#minLogLength", description="The global minimum length for logs that will be autoloaded (default is zero)"},
}
UniversalAutoload.OPTIONS_DEFAULTS = {
{id="isBoxTrailer", default=false, valueType="BOOL", key="#isBoxTrailer", description="If trailer is enclosed with a rear door"},
{id="isLogTrailer", default=false, valueType="BOOL", key="#isLogTrailer", description="If trailer is a logging trailer - will load only logs, dropped from above"},
{id="isBaleTrailer", default=false, valueType="BOOL", key="#isBaleTrailer", description="If trailer should use an automatic bale collection mode"},
{id="isBaleProcessor", default=false, valueType="BOOL", key="#isBaleProcessor", description="If trailer should consume bales (e.g. TMR Mixer or Straw Blower)"},
{id="isCurtainTrailer", default=false, valueType="BOOL", key="#isCurtainTrailer", description="Automatically detect the available load side (if the trailer has curtain sides)"},
{id="enableRearLoading", default=false, valueType="BOOL", key="#enableRearLoading", description="Use the automatic rear loading trigger"},
{id="enableSideLoading", default=false, valueType="BOOL", key="#enableSideLoading", description="Use the automatic side loading triggers"},
{id="noLoadingIfFolded", default=false, valueType="BOOL", key="#noLoadingIfFolded", description="Prevent loading when folded"},
{id="noLoadingIfUnfolded", default=false, valueType="BOOL", key="#noLoadingIfUnfolded", description="Prevent loading when unfolded"},
{id="noLoadingIfCovered", default=false, valueType="BOOL", key="#noLoadingIfCovered", description="Prevent loading when covered"},
{id="noLoadingIfUncovered", default=false, valueType="BOOL", key="#noLoadingIfUncovered", description="Prevent loading when uncovered"},
{id="rearUnloadingOnly", default=false, valueType="BOOL", key="#rearUnloadingOnly", description="Use rear unloading zone only (not side zones)"},
{id="frontUnloadingOnly", default=false, valueType="BOOL", key="#frontUnloadingOnly", description="Use front unloading zone only (not side zones)"},
{id="horizontalLoading", default=false, valueType="BOOL", key="#horizontalLoading", description="Start with horizontal loading enabled (can be toggled if key is bound)"},
{id="disableAutoStrap", default=false, valueType="BOOL", key="#disableAutoStrap", description="Disable the automatic application of tension belts"},
{id="disableHeightLimit", default=false, valueType="BOOL", key="#disableHeightLimit", description="Disable the density based stacking height limit"},
{id="zonesOverlap", default=false, valueType="BOOL", key="#zonesOverlap", description="Flag to identify when the loading areas overlap each other"},
{id="offsetRoot", default=nil, valueType="STRING", key="#offsetRoot", description="Vehicle i3d node that area offsets are relative to"},
{id="minLogLength", default=0, valueType="FLOAT", key="#minLogLength", description="The minimum length for logs that will be autoloaded (default is zero)"},
{id="showDebug", default=false, valueType="BOOL", key="#showDebug", description="Show the full graphical debugging display for this vehicle"},
}
UniversalAutoload.LOADING_AREA_DEFAULTS = {
{id="offset", default="0 0 0", valueType="VECTOR_TRANS", key="#offset", description="Offset to the centre of the loading area"},
{id="offsetRoot", default=nil, valueType="STRING", key="#offsetRoot", description="Vehicle i3d node that this area offset is relative to"},
{id="width", default=0, valueType="FLOAT", key="#width", description="Width of the loading area"},
{id="length", default=0, valueType="FLOAT", key="#length", description="Length of the loading area"},
{id="height", default=0, valueType="FLOAT", key="#height", description="Height of the loading area"},
{id="baleHeight", default=nil, valueType="FLOAT", key="#baleHeight", description="Height of the loading area for BALES only"},
{id="widthAxis", default=nil, valueType="STRING", key="#widthAxis", description="Axis name to extend width of the loading area"},
{id="lengthAxis", default=nil, valueType="STRING", key="#lengthAxis", description="Axis name to extend length of the loading area"},
{id="heightAxis", default=nil, valueType="STRING", key="#heightAxis", description="Axis name to extend height of the loading area"},
{id="offsetFrontAxis", default=nil, valueType="STRING", key="#offsetFrontAxis", description="Axis name to adjust the front position of the loading area"},
{id="offsetRearAxis", default=nil, valueType="STRING", key="#offsetRearAxis", description="Axis name to adjust the rear position of the loading area"},
{id="reverseWidthAxis", default=false, valueType="BOOL", key="#reverseWidthAxis", description="Reverses direction of width extension if true"},
{id="reverseLengthAxis", default=false, valueType="BOOL", key="#reverseLengthAxis", description="Reverses direction of length extension if true"},
{id="reverseHeightAxis", default=false, valueType="BOOL", key="#reverseHeightAxis", description="Reverses direction of height extension if true"},
{id="noLoadingIfFolded", default=false, valueType="BOOL", key="#noLoadingIfFolded", description="Prevent loading when folded (for this area only)"},
{id="noLoadingIfUnfolded", default=false, valueType="BOOL", key="#noLoadingIfUnfolded", description="Prevent loading when unfolded (for this area only)"},
{id="noLoadingIfCovered", default=false, valueType="BOOL", key="#noLoadingIfCovered", description="Prevent loading when covered (for this area only)"},
{id="noLoadingIfUncovered", default=false, valueType="BOOL", key="#noLoadingIfUncovered", description="Prevent loading when uncovered (for this area only)"},
}
UniversalAutoload.CONFIG_DEFAULTS = {
{id="selectedConfigs", default="ALL", valueType="STRING", key="#selectedConfigs", description="Selected Configuration Names"},
{id="useConfigName", default=nil, valueType="STRING", key="#useConfigName", description="Specific configuration to be used for selected configs"},
{
key = ".loadingArea(?)",
name = "loadingArea",
data = UniversalAutoload.LOADING_AREA_DEFAULTS,
},
{
key = ".options",
name = "options",
data = UniversalAutoload.OPTIONS_DEFAULTS,
},
}
UniversalAutoload.VEHICLE_DEFAULTS = {
{id="configFileName", default=nil, valueType="STRING", key="#configFileName", description="Vehicle config file xml full path - used to identify supported vehicles"},
{
key = ".configuration(?)",
name = "spec",
data = UniversalAutoload.CONFIG_DEFAULTS,
},
}
UniversalAutoload.SAVEGAME_STATE_DEFAULTS = {
{id="tipside", default="none", valueType="STRING", key="#tipside", description="Last used tip side"},
{id="loadside", default="both", valueType="STRING", key="#loadside", description="Last used load side"},
{id="loadWidth", default=0, valueType="FLOAT", key="#loadWidth", description="Last used load width"},
{id="loadLength", default=0, valueType="FLOAT", key="#loadLength", description="Last used load length"},
{id="loadHeight", default=0, valueType="FLOAT", key="#loadHeight", description="Last used load height"},
{id="actualWidth", default=0, valueType="FLOAT", key="#actualWidth", description="Last used expected load width"},
{id="actualLength", default=0, valueType="FLOAT", key="#actualLength", description="Last used complete load length"},
{id="layerCount", default=0, valueType="INT", key="#layerCount", description="Number of layers that are currently loaded"},
{id="layerHeight", default=0, valueType="FLOAT", key="#layerHeight", description="Total height of the currently loaded layers"},
{id="nextLayerHeight", default=0, valueType="FLOAT", key="#nextLayerHeight", description="Height for the next layer (highest point in previous layer)"},
{id="loadAreaIndex", default=1, valueType="INT", key="#loadAreaIndex", description="Last used load area"},
{id="materialIndex", default=1, valueType="INT", key="#materialIndex", description="Last used material type"},
{id="containerIndex", default=1, valueType="INT", key="#containerIndex", description="Last used container type"},
{id="loadingFilter", default=false, valueType="BOOL", key="#loadingFilter", description="TRUE=Load full pallets only; FALSE=Load any pallets"},
{id="useHorizontalLoading", default=false, valueType="BOOL", key="#useHorizontalLoading", description="Last used horizontal loading state"},
{id="baleCollectionMode", default=false, valueType="BOOL", key="#baleCollectionMode", description="Enable manual toggling of the automatic bale collection mode"},
}
function iterateDefaultsTable(tbl, parentKey, currentKey, currentValue, action)
parentKey = parentKey or ""
currentKey = currentKey or ""
action = action or function(k, v, parentKey, currentKey, currentValue, finalValue)
if debugSchema then print(" " .. currentKey .. ": " .. tostring(finalValue)) end
end
for k, v in pairs(tbl) do
if type(v) == "table" then
local newCurrentKey = currentKey
if v.key then
newCurrentKey = newCurrentKey .. v.key
end
local newCurrentValue = currentValue
if v.id ~= nil then
local finalValue = newCurrentValue and newCurrentValue[v.id] or v.default
action(k, v, parentKey, newCurrentKey, newCurrentValue, finalValue)
end
if v.data then
iterateDefaultsTable(v.data, parentKey, newCurrentKey, newCurrentValue, action)
end
end
end
end
print("GLOBAL_DEFAULTS") iterateDefaultsTable(UniversalAutoload.GLOBAL_DEFAULTS)
print("VEHICLE_DEFAULTS") iterateDefaultsTable(UniversalAutoload.VEHICLE_DEFAULTS)
print("SAVEGAME_STATE_DEFAULTS") iterateDefaultsTable(UniversalAutoload.SAVEGAME_STATE_DEFAULTS)
function UniversalAutoloadManager.openUserSettingsXMLFile(xmlFilename)
local xmlFilename = xmlFilename or Utils.getFilename(UniversalAutoload.userSettingsFile, getUserProfileAppPath())
local xmlFile = XMLFile.loadIfExists("settings", xmlFilename, UniversalAutoload.xmlSchema)
if not xmlFile then
print("Creating NEW settings file " .. xmlFilename)
xmlFile = XMLFile.create("settings", xmlFilename, "universalAutoload", UniversalAutoload.xmlSchema)
end
return xmlFile
end
--
function UniversalAutoloadManager.getVehicleConfigFromSettingsXML(configKey, xmlFile)
if not configKey then
print("configuration key required for getVehicleConfigFromSettingsXML")
return
end
local shouldCloseFile = not xmlFile and true
local xmlFile = xmlFile or UniversalAutoloadManager.openUserSettingsXMLFile()
if xmlFile then
local function readSettingFromFile(k, v, parentKey, currentKey, currentValue, finalValue)
if currentKey and currentValue and v.id then
if v.valueType == "VECTOR_TRANS" then
currentValue[v.id] = xmlFile:getValue(currentKey, v.default, true)
else
currentValue[v.id] = xmlFile:getValue(currentKey, v.default)
end
-- print(" << " .. tostring(currentKey) .. " = " .. tostring(currentValue[v.id]))
end
end
local config = {}
local selectedConfigs = xmlFile:getValue(configKey.."#selectedConfigs", "ALL")
local useConfigName = xmlFile:getValue(configKey.."#useConfigName", nil)
iterateDefaultsTable(UniversalAutoload.OPTIONS_DEFAULTS, "", configKey..".options", config, readSettingFromFile)
local j = 1
local hasBaleHeight = false
local loadingArea = {}
while true do
local loadAreaKey = string.format("%s.loadingArea(%d)", configKey, j-1)
if not xmlFile:hasProperty(loadAreaKey) then
break
end
loadingArea[j] = {}
iterateDefaultsTable(UniversalAutoload.LOADING_AREA_DEFAULTS, "", loadAreaKey, loadingArea[j], readSettingFromFile)
hasBaleHeight = hasBaleHeight or type(loadingArea[j].baleHeight) == 'number'
j = j + 1
end
config['loadArea'] = loadingArea
local isBaleTrailer = config.isBaleTrailer
local isBaleProcessor = config.isBaleProcessor
local horizontalLoading = config.horizontalLoading
config.horizontalLoading = horizontalLoading or isBaleTrailer or isBaleProcessor or false
config.isBaleTrailer = isBaleTrailer or hasBaleHeight
if shouldCloseFile then
xmlFile:delete()
end
return config
else
print("ERROR: no settings file " .. tostring(xmlFile))
end
end
--
function UniversalAutoloadManager.countConfigsInSettingsXML(xmlFile)
local shouldCloseFile = not xmlFile and true
local xmlFile = xmlFile or UniversalAutoloadManager.openUserSettingsXMLFile()
if xmlFile then
local i = 0
local counts = {}
while true do
local vehicleKey = string.format(UniversalAutoload.vehicleKey, i)
if not xmlFile:hasProperty(vehicleKey) then
break
end
local j = 0
while true do
local configKey = string.format(UniversalAutoload.vehicleConfigKey, i, j)
if not xmlFile:hasProperty(configKey) then
break
end
j = j + 1
end
i = i + 1
counts[i] = j
end
if shouldCloseFile then
xmlFile:delete()
end
return i, counts
end
end
--
function UniversalAutoloadManager.getConfigSettingsPosition(targetFileName, targetConfigId, xmlFile)
local targetConfigId = targetConfigId or UniversalAutoload.ALL
local shouldCloseFile = not xmlFile and true
local xmlFile = xmlFile or UniversalAutoloadManager.openUserSettingsXMLFile()
if xmlFile then
local i = 0
while true do
local vehicleKey = string.format(UniversalAutoload.vehicleKey, i)
if not xmlFile:hasProperty(vehicleKey) then
break
end
local configFileName = xmlFile:getValue(vehicleKey .. "#configFileName", "MISSING")
configFileName = UniversalAutoloadManager.cleanConfigFileName(configFileName)
targetFileName = UniversalAutoloadManager.cleanConfigFileName(targetFileName)
if tostring(configFileName):lower() == tostring(targetFileName):lower() then
print("targetConfigId: " .. tostring(targetConfigId))
local j = 0
while true do
local configKey = string.format(UniversalAutoload.vehicleConfigKey, i, j)
if not xmlFile:hasProperty(configKey) then
break
end
local selectedConfigs = xmlFile:getValue(configKey .. "#selectedConfigs", "MISSING")
print("selectedConfigs: " .. selectedConfigs)
if selectedConfigs == UniversalAutoload.ALL then
print("FOUND 'ALL' CONFIG AT #" .. j+1)
break
elseif selectedConfigs:find(tostring(targetConfigId)) then
print("FOUND SELECTED CONFIG AT #" .. j+1)
break
end
j = j + 1
end
return i, j
end
i = i + 1
end
if shouldCloseFile then
xmlFile:delete()
end
return nil, nil, i
end
end
--
function UniversalAutoloadManager.getVehicleConfigIndexesForSaving(vehicle, configId, xmlFile)
local spec = vehicle.spec_universalAutoload
local configFileName = UniversalAutoloadManager.cleanConfigFileName(vehicle.configFileName)
local index, subIndex, size = UniversalAutoloadManager.getConfigSettingsPosition(configFileName, configId, xmlFile)
if index then
local key = string.format(UniversalAutoload.vehicleKey, index)
if xmlFile:getValue(key .. "#configFileName") ~= configFileName then
print("CLEANING CONFIG FILE NAME: " .. configFileName)
xmlFile:setValue(key.."#configFileName", configFileName)
end
local configKey = string.format(UniversalAutoload.vehicleConfigKey, index, subIndex)
configId = xmlFile:getValue(configKey .. "#selectedConfigs") or configId
print("UPDATE CONFIG #" .. index + 1 .. " == " .. configId .. " (#" ..subIndex + 1 .. ")")
else
index = size or 0
subIndex = 0
print("INSERT CONFIG INDEX #" .. index)
local key = string.format(UniversalAutoload.vehicleKey, index)
xmlFile:setValue(key.."#configFileName", configFileName)
end
if not UniversalAutoload.VEHICLE_CONFIGURATIONS[configFileName] then
UniversalAutoload.VEHICLE_CONFIGURATIONS[configFileName] = {}
end
if not UniversalAutoload.VEHICLE_CONFIGURATIONS[configFileName][configId] then
print("USING CONFIG SUB-INDEX: #" .. subIndex .. " (" .. configId .. ")")
local key = string.format(UniversalAutoload.vehicleConfigKey, index, subIndex)
xmlFile:setValue(key.."#selectedConfigs", tostring(configId))
if spec.useConfigName then
print("useConfigName: " .. tostring(spec.useConfigName))
xmlFile:setValue(key.."#useConfigName", tostring(spec.useConfigName))
end
UniversalAutoload.VEHICLE_CONFIGURATIONS[configFileName][configId] = {}
end
return index, subIndex
end
--
function UniversalAutoloadManager.getVehicleConfigNames(vehicle)
local spec = vehicle and vehicle.spec_universalAutoload
if not spec or not vehicle.configFileName then
print("Invalid vehicle supplied: " .. tostring(vehicle))
return
end
local configFileName, configId
if spec.selectedConfigs and spec.configFileName then
print("ALREADY SET WITH:")
configId = spec.selectedConfigs
configFileName = spec.configFileName
end
if not configId or not configFileName then
print("FIND CORRECT SETTINGS FILE POSITION:")
configFileName = UniversalAutoloadManager.cleanConfigFileName(vehicle.configFileName)
configId = UniversalAutoloadManager.getValidConfigurationId(vehicle)
end
print("configFileName = " .. tostring(configFileName))
print("selectedConfig = " .. tostring(configId))
print("useConfigName = " .. tostring(spec.useConfigName))
return configFileName, configId
end
--
function UniversalAutoloadManager.saveVehicleConfigToSettingsXML(vehicle, xmlFile)
local spec = vehicle and vehicle.spec_universalAutoload
if not spec or not vehicle.configFileName then
print("Invalid vehicle supplied: " .. tostring(vehicle))
return
end
local shouldCloseFile = not xmlFile and true
local xmlFile = xmlFile or UniversalAutoloadManager.openUserSettingsXMLFile()
if xmlFile then
local function writeSettingToFile(k, v, parentKey, currentKey, currentValue, finalValue)
if currentKey and finalValue ~= nil and finalValue ~= v.default then
print(" >> " .. tostring(currentKey) .. " = " .. tostring(finalValue) .. " - " .. tostring(v.default))
if v.valueType == "VECTOR_TRANS" then
if type(finalValue) == "string" then
local vector = {}
for num in finalValue:gmatch("%S+") do
table.insert(vector, tonumber(num))
end
finalValue = vector
elseif type(finalValue) ~= "table" then
error("Unexpected type for VECTOR_TRANS: " .. tostring(finalValue))
end
end
if type(finalValue) == "table" and v.valueType == "VECTOR_TRANS" then
xmlFile:setValue(parentKey..currentKey, unpack(finalValue))
else
xmlFile:setValue(parentKey..currentKey, finalValue)
end
end
end
if spec.loadArea and #spec.loadArea > 0 then
local configFileName, configId = UniversalAutoloadManager.getVehicleConfigNames(vehicle)
if configFileName and configId and UniversalAutoload.VEHICLE_CONFIGURATIONS[configFileName] then
local oldConfig = UniversalAutoload.VEHICLE_CONFIGURATIONS[configFileName][configId]
if oldConfig and oldConfig.loadArea and #oldConfig.loadArea > 0 then
print("UPDATE CONFIG IN MEMORY")
local newConfig = deepCopy(spec)
for k, v in pairs(oldConfig) do
oldConfig[k] = newConfig[k]
end
end
end
print("SAVE TO SETTINGS FILE")
local index, subIndex = UniversalAutoloadManager.getVehicleConfigIndexesForSaving(vehicle, configId, xmlFile)
print("options:")
local configKey = string.format(UniversalAutoload.vehicleConfigKey, index, subIndex)
iterateDefaultsTable(UniversalAutoload.OPTIONS_DEFAULTS, configKey, ".options", spec, writeSettingToFile)
print("loadingAreas:")
for j, loadArea in pairs(spec.loadArea or {}) do
local loadAreaKey = string.format(".loadingArea(%d)", j-1)
iterateDefaultsTable(UniversalAutoload.LOADING_AREA_DEFAULTS, configKey, loadAreaKey, loadArea, writeSettingToFile)
end
xmlFile:save()
else
print("DID NOT SAVE SETTINGS - loading area was missing")
end
if shouldCloseFile then
xmlFile:delete()
end
end
end
function UniversalAutoloadManager.ImportLocalConfigurations(userSettingsFile, overwriteExisting)
print("UAL - IMPORT CONFIGS")
if not fileExists(userSettingsFile) then
print("CREATING settings file")
-- local defaultSettingsFile = Utils.getFilename("config/UniversalAutoload.xml", UniversalAutoload.path)
-- copyFile(defaultSettingsFile, userSettingsFile, false)
end
UniversalAutoloadManager.ImportGlobalSettings(userSettingsFile, overwriteExisting)
UniversalAutoloadManager.ImportVehicleConfigurations(userSettingsFile, overwriteExisting)
end
--
function UniversalAutoloadManager.ImportGlobalSettings(xmlFilename, overwriteExisting)
print("UAL - IMPORT GLOBAL SETTINGS")
if g_currentMission:getIsServer() then
local xmlFile = UniversalAutoloadManager.openUserSettingsXMLFile(xmlFilename)
if xmlFile ~= 0 and xmlFile ~= nil then
if overwriteExisting or not UniversalAutoload.globalSettingsLoaded then
print("IMPORT Universal Autoload global settings")
UniversalAutoload.globalSettingsLoaded = true
iterateDefaultsTable(UniversalAutoload.GLOBAL_DEFAULTS, UniversalAutoload.globalKey, "", UniversalAutoload,
function(k, v, parentKey, currentKey, currentValue, finalValue)
UniversalAutoload[v.id] = xmlFile:getValue(parentKey..currentKey, v.default)
print(" >> " .. tostring(v.id) .. ": " .. tostring(v.default))
end)
end
xmlFile:delete()
else
print("Universal Autoload - could not open global settings file")
end
else
print("Universal Autoload - global settings are only loaded for the server")
end
end
--
function UniversalAutoloadManager.ImportVehicleConfigurations(xmlFilename, overwriteExisting)
print("UAL - IMPORT VEHICLE CONFIGS")
local xmlFile = UniversalAutoloadManager.openUserSettingsXMLFile(xmlFilename)
if xmlFile then
local i = 0
while true do
local vehicleKey = string.format(UniversalAutoload.vehicleKey, i)
if not xmlFile:hasProperty(vehicleKey) then
break
end
local configFileName = xmlFile:getValue(vehicleKey .. "#configFileName")
configFileName = UniversalAutoloadManager.cleanConfigFileName(configFileName)
if UniversalAutoloadManager.getValidXmlName(configFileName) then
print(" [" .. i + 1 .. "] " .. configFileName)
local j = 0
while true do
local configKey = vehicleKey .. string.format(".configuration(%d)", j)
if not xmlFile:hasProperty(configKey) then
break
end
local configuration = UniversalAutoloadManager.getVehicleConfigFromSettingsXML(configKey, xmlFile)
if not configuration then
print("could not load UAL configuration for: " .. configKey)
end
if not UniversalAutoload.VEHICLE_CONFIGURATIONS[configFileName] then
print("ADDING SHOP ITEM " .. configFileName)
UniversalAutoload.VEHICLE_CONFIGURATIONS[configFileName] = {}
table.addElement(g_storeManager:getPackItems("UNIVERSALAUTOLOAD"), configFileName)
end
local configGroup = UniversalAutoload.VEHICLE_CONFIGURATIONS[configFileName]
local selectedConfigs = xmlFile:getValue(configKey.."#selectedConfigs", UniversalAutoload.ALL)
local useConfigName = xmlFile:getValue(configKey.."#useConfigName", nil)
if not configGroup[selectedConfigs] or overwriteExisting then
configuration.useConfigName = useConfigName
configuration.configFileName = configFileName
configuration.selectedConfigs = selectedConfigs
configGroup[selectedConfigs] = configuration
else
if UniversalAutoload.showDebug then print(" ALREADY EXISTS: "..configFileName.." ["..selectedConfigs.."]") end
end
print(" >> "..configFileName.." ["..selectedConfigs.."] "
.. (useConfigName and ("(" .. useConfigName .. ")") or "")
.. (configuration.showDebug and " DEBUG" or "") )
j = j + 1
end
else
if UniversalAutoload.showDebug then print(" NOT FOUND: " .. tostring(configFileName)) end
end
i = i + 1
end
xmlFile:delete()
return i
end
end
function UniversalAutoloadManager.getValidConfigurationId(vehicle)
-- returns: configId, description
local spec = vehicle and vehicle.spec_universalAutoload
if not spec then return end
local item = g_storeManager:getItemByXMLFilename(vehicle.configFileName)
if not item then
print("could not get store item for " .. tostring(vehicle.configFileName))
return
end
local configName = spec.useConfigName -- or "design"
local configId = configName and vehicle.configurations[configName] and tostring(vehicle.configurations[configName]) or nil
local configurationSets = item.configurationSets or {}
if #configurationSets == 0 then
local fullConfigId = UniversalAutoload.ALL .. (configId and ("|" .. configId) or "")
return fullConfigId, "UNIQUE"
end
local bestMatch = { index = nil, count = 0, name = nil }
for i, config in ipairs(configurationSets) do
local count, match = 0, true
for k, v in pairs(config.configurations or {}) do
if vehicle.configurations[k] == v then
count = count + 1
else
match = false
end
end
if match then
local fullConfigId = i .. (configId and ("|" .. configId) or "")
return fullConfigId, config.name
elseif count > bestMatch.count then
bestMatch = { index = i, count = count, name = config.name }
end
end
if bestMatch.index then
local fullConfigId = bestMatch.index .. (configId and ("|" .. configId) or "")
return fullConfigId, bestMatch.name
end
end
function UniversalAutoloadManager.saveVehicleConfigurationToSettings(vehicle, noEventSend)
print("UAL - SAVE VEHICLE CONFIGURATION")
local spec = vehicle and vehicle.spec_universalAutoload
if not vehicle or not spec then
print("valid UAL vehicle is required to save settings")
return
end
if g_currentMission:getIsServer() then
print("EXPORT VEHICLE SETTINGS: " .. vehicle:getFullName())
UniversalAutoloadManager.saveVehicleConfigToSettingsXML(vehicle)
end
UniversalAutoload.ChangeSettingsEvent.sendEvent(vehicle, noEventSend)
end
function UniversalAutoloadManager:onVehicleBuyEvent(errorCode, leaseVehicle, price)
if errorCode == BuyVehicleEvent.STATE_SUCCESS then
print("UAL - ON VEHICLE BUY EVENT " .. (leaseVehicle and "(leased)" or "(owned)"))
-- do nothing here for now..
-- UniversalAutoloadManager.saveShopConfiguration()
end
end
function UniversalAutoloadManager.getValidXmlName(ualConfigName)
if ualConfigName == nil then
return
end
local xmlFilename = ualConfigName
if g_storeManager:getItemByXMLFilename(xmlFilename) then
return xmlFilename
end
xmlFilename = g_modsDirectory .. ualConfigName
if g_storeManager:getItemByXMLFilename(xmlFilename) then
return xmlFilename
end
for i = 1, #g_dlcsDirectories do
local dlcsDir = g_dlcsDirectories[i].path
xmlFilename = dlcsDir .. ualConfigName
if g_storeManager:getItemByXMLFilename(xmlFilename) then
return xmlFilename
end
end
end
function UniversalAutoloadManager.cleanConfigFileName(configFileName)
if configFileName == nil then
return
end
if configFileName:find(g_modsDirectory) then
-- print("CLEANED MOD FILE NAME")
return configFileName:gsub(g_modsDirectory, "")
end
for i = 1, #g_dlcsDirectories do
local dlcsDir = g_dlcsDirectories[i].path
if configFileName:find(dlcsDir) then
-- print("CLEANED DLC FILE NAME")
return configFileName:gsub(dlcsDir, "")
end
end
return configFileName
end
function UniversalAutoloadManager.injectSpecialisation()
print("UAL - INJECT SPEC:")
for typeName, vehicleType in pairs(g_vehicleTypeManager.types) do
if SpecializationUtil.hasSpecialization(TensionBelts, vehicleType.specializations)
and not SpecializationUtil.hasSpecialization(UniversalAutoload, vehicleType.specializations) then
g_vehicleTypeManager:addSpecialization(typeName, UniversalAutoload.name .. '.universalAutoload')
UniversalAutoload.VEHICLE_TYPES[typeName] = true
end
end
end
function UniversalAutoloadManager:ualInputCallback(target)
print("UAL SHOP INPUT CALLBACK")
UniversalAutoloadManager:onOpenSettingsEvent('UNIVERSALAUTOLOAD_SHOP_CONFIG', 1)
end
function UniversalAutoloadManager:onOpenSettingsEvent(actionName, inputValue, callbackState, isAnalog)
-- print("onOpenSettingsEvent")
if UniversalAutoloadManager.shopCongfigMenu then
g_gui:showDialog("ShopConfigMenuUALSettings")
else
print("UAL menu not created")
end
end
function UniversalAutoloadManager:onEditLoadingAreaEvent(actionName, inputValue, callbackState, isAnalog)
-- print("onEditLoadingAreaEvent")
if UniversalAutoloadManager.shopVehicle then
local spec = UniversalAutoloadManager.shopVehicle.spec_universalAutoload
if spec and spec.isInsideShop then
local shopConfig = UniversalAutoloadManager.shopConfig or {}
UniversalAutoloadManager.pauseOnNextStep = nil
local ctrl = UniversalAutoloadManager.ctrlHeld
local shift = UniversalAutoloadManager.shiftHeld
if shift and ctrl then
spec.resetToDefault = true
else
shopConfig.enableEditing = shopConfig.enableEditing or false
shopConfig.enableEditing = not shopConfig.enableEditing
end
end
end
end
ShopConfigScreen.ualInputCallback = Utils.prependedFunction(ShopConfigScreen.ualInputCallback, UniversalAutoloadManager.ualInputCallback);
function UniversalAutoloadManager.injectMenu()
print("UAL - INJECT MENU")
local function fixInGameMenu(frame, pageName, position, predicateFunc)
local inGameMenu = g_gui.screenControllers[InGameMenu] --g_inGameMenu
local aboveSettings = nil;
--DebugUtil.printTableRecursively(inGameMenu.pagingElement)
-- remove all to avoid warnings
for k, v in pairs({pageName}) do
inGameMenu.controlIDs[v] = nil
end
for i = 1, #inGameMenu.pagingElement.elements do
local child = inGameMenu.pagingElement.elements[i]
if child == inGameMenu["pageSettings"] then
aboveSettings = i;
print("--- found Settings position - "..tostring(i))
end
end
aboveSettings = aboveSettings or position
inGameMenu[pageName] = frame
inGameMenu.pagingElement:addElement(inGameMenu[pageName])
inGameMenu:exposeControlsAsFields(pageName)
for i = 1, #inGameMenu.pagingElement.elements do
local child = inGameMenu.pagingElement.elements[i]
if child == inGameMenu[pageName] then
table.remove(inGameMenu.pagingElement.elements, i)
table.insert(inGameMenu.pagingElement.elements, aboveSettings, child)
break
end
end
for i = 1, #inGameMenu.pagingElement.pages do
local child = inGameMenu.pagingElement.pages[i]
if child.element == inGameMenu[pageName] then
table.remove(inGameMenu.pagingElement.pages, i)
table.insert(inGameMenu.pagingElement.pages, aboveSettings, child)
break
end
end
inGameMenu.pagingElement:updateAbsolutePosition()
inGameMenu.pagingElement:updatePageMapping()
inGameMenu:registerPage(inGameMenu[pageName], position, predicateFunc)
local iconFileName = Utils.getFilename('gui/menu_modSettings.dds', UniversalAutoload.path)
inGameMenu:addPageTab(inGameMenu[pageName], iconFileName, GuiUtils.getUVs({0,0,1024,1024}))
for i = 1, #inGameMenu.pageFrames do
local child = inGameMenu.pageFrames[i]
if child == inGameMenu[pageName] then
table.remove(inGameMenu.pageFrames, i)
table.insert(inGameMenu.pageFrames, aboveSettings, child)
break
end
end
inGameMenu:rebuildTabList()
end
local guiUALSettings = InGameMenuUALSettings.new(g_i18n)
g_gui:loadGui(UniversalAutoload.path .. "gui/InGameMenuUALSettings.xml", "inGameMenuUALSettings", guiUALSettings, true)
local function isEnabledPredicate()
return function () return true end
end
fixInGameMenu(guiUALSettings,"inGameMenuUALSettings", 2, isEnabledPredicate())
end
function UniversalAutoloadManager:mouseEvent(posX, posY, isDown, isUp, button)
if UniversalAutoloadManager.shopVehicle then
local spec = UniversalAutoloadManager.shopVehicle.spec_universalAutoload
if spec and spec.isInsideShop then
local shopConfig = UniversalAutoloadManager.shopConfig or {}
if button == 3 and isUp then
shopConfig.selected = nil
end
if spec.loadingVolume and spec.loadingVolume.state == LoadingVolume.STATE.SHOP_CONFIG then
local function isPointSelected(point)
local sx, sy, _ = project(point[1], point[2], point[3])
if math.abs(posX - sx) < 0.005 and math.abs(posY - sy) < 0.005 then
return true
end
end
for n, bb in pairs(spec.loadingVolume.bbs) do
local centre, points, names = bb:getCubeFaces()
for i, point in pairs(points or {}) do
if isPointSelected(point) then
if button == 3 and isDown then
shopConfig.selected = {n, i}
shopConfig.control = UniversalAutoloadManager.ctrlHeld or false
shopConfig.shift = UniversalAutoloadManager.shiftHeld or false
shopConfig.alt = UniversalAutoloadManager.altHeld or false
else
if not shopConfig.grabbedPoint then
shopConfig.hovered = {n, i}
end
end
else
local hovered = shopConfig.hovered
if hovered and n==hovered[1] and i==hovered[2] then
shopConfig.hovered = {0, 0}
end
end
end
end
shopConfig.mousePos = {posX, posY}
end
end
end
end
function UniversalAutoloadManager:keyEvent(unicode, sym, modifier, isDown)
if UniversalAutoloadManager.shopVehicle and UniversalAutoloadManager.shopConfig then
local spec = UniversalAutoloadManager.shopVehicle.spec_universalAutoload
if spec and spec.isInsideShop then
-- print("KEY: " .. tostring(sym) .. " + " .. tostring(modifier))
if sym == 308 then
UniversalAutoloadManager.altHeld = isDown
end
if sym == 306 then
UniversalAutoloadManager.ctrlHeld = isDown
end
if sym == 304 then
UniversalAutoloadManager.shiftHeld = isDown
end
end
end
end
function UniversalAutoloadManager:removeShopGui()
-- print("removeShopGui")
if UniversalAutoloadManager.configButton then
-- print("UAL - DELETE BUTTON")
UniversalAutoloadManager.configButton:delete()
UniversalAutoloadManager.configButton = nil
end
if UniversalAutoloadManager.shopCongfigMenu then
-- print("UAL - DELETE CONFIG MENU")
UniversalAutoloadManager.shopCongfigMenu:delete()
UniversalAutoloadManager.shopCongfigMenu = nil
end
end
function UniversalAutoloadManager:removeShopActionEvents()
-- print("removeShopActionEvents")
UniversalAutoloadManager.actionIds = UniversalAutoloadManager.actionIds or {}
for _, actionId in pairs(UniversalAutoloadManager.actionIds) do
g_inputBinding:removeActionEvent(actionId)
UniversalAutoloadManager.actionIds[actionId] = nil
end
end
function UniversalAutoloadManager:registerShopGui()
-- print("registerShopGui")
if not UniversalAutoloadManager.configButton then
local function cloneButton(original, title, callback)
local button = original:clone(original.parent)
button:setText(title)
button:setVisible(false)
button:setCallback("onClickCallback", callback)
button:setInputAction(InputAction.UNIVERSALAUTOLOAD_SHOP_CONFIG)
button.parent:invalidateLayout()
return button
end
local buyButton = g_shopConfigScreen.buyButton
local button = cloneButton(buyButton, g_i18n:getText("shop_configuration_text"), "ualInputCallback");