-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmods.lua
More file actions
1285 lines (1004 loc) · 35 KB
/
mods.lua
File metadata and controls
1285 lines (1004 loc) · 35 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
local localDeleteFolder = deleteFolder
local localDeleteFile = deleteFile
g_uniqueDlcNamePrefix = "pdlc_"
g_modEventListeners = {}
g_dlcsDirectories = {}
g_forceNeedsDlcsAndModsReload = false
g_lastCheckDlcPaths = {}
g_modIsLoaded = {}
g_globalMods = {}
g_modNameToDirectory = {}
local isReloadingDlcs = false
g_dlcModNameHasPrefix = {}
g_internalModsDirectory = "internalMods/"
local internalMods = {
{
name = "agcoIdealSimulator"
},
{
version = "1.0.4.0",
name = "baleStacking"
},
{
version = "1.0.3.0",
name = "arena"
}
}
local internalScriptMods = {
"FS22_precisionFarming",
"FS22_lindnerLintracSupercup",
"FS22_GrimmePack",
"FS22_emergencyPack",
"FS22_strawHarvestPack"
}
local outdatedMods = {
FS22_BetterContracts = {
1,
2,
4,
0
}
}
modOnCreate = {}
function loadDlcs()
storeHaveDlcsChanged()
local loadedDlcs = {}
for i = 1, table.getn(g_dlcsDirectories) do
local dir = g_dlcsDirectories[i]
if dir.isLoaded then
loadDlcsFromDirectory(dir.path, loadedDlcs)
end
end
end
function loadDlcsFromDirectory(dlcsDir, loadedDlcs)
local appBasePath = getAppBasePath()
if isAbsolutPath(dlcsDir) and (appBasePath:len() == 0 or not string.startsWith(dlcsDir, appBasePath)) then
createFolder(dlcsDir)
end
local files = Files.new(dlcsDir)
for _, v in pairs(files.files) do
local addDLCPrefix = false
local dlcFileHash, dlcName, xmlFilename = nil
if v.isDirectory then
if g_isDevelopmentVersion or not GS_PLATFORM_PC then
dlcName = v.filename
xmlFilename = "dlcDesc.xml"
addDLCPrefix = true
if not GS_PLATFORM_PC then
dlcFileHash = getFileMD5(dlcsDir .. v.filename, dlcName)
end
if dlcFileHash == nil and g_isDevelopmentVersion then
dlcFileHash = getMD5("Dev_" .. v.filename)
end
end
else
local len = v.filename:len()
if len > 4 then
local ext = v.filename:sub(len - 3)
if ext == ".dlc" then
dlcName = v.filename:sub(1, len - 4)
dlcFileHash = getFileMD5(dlcsDir .. v.filename, dlcName)
xmlFilename = "dlcDesc.xml"
addDLCPrefix = true
elseif ext == ".zip" or ext == ".gar" then
dlcName = v.filename:sub(1, len - 4)
dlcFileHash = getFileMD5(dlcsDir .. v.filename, dlcName)
xmlFilename = "modDesc.xml"
addDLCPrefix = false
end
end
end
if dlcName ~= nil and xmlFilename ~= nil and g_dlcModNameHasPrefix[dlcName] == nil then
local dlcDir = dlcsDir .. dlcName .. "/"
local dlcFile = dlcDir .. xmlFilename
g_dlcModNameHasPrefix[dlcName] = addDLCPrefix
loadModDesc(dlcName, dlcDir, dlcFile, dlcFileHash, dlcsDir .. v.filename, v.isDirectory, addDLCPrefix, false)
end
end
end
function loadMods()
haveModsChanged()
local loadedMods = {}
local modsDir = g_modsDirectory
g_showIllegalActivityInfo = false
local files = Files.new(modsDir)
for _, v in pairs(files.files) do
local modFileHash, modName = nil
if v.isDirectory then
modName = v.filename
if g_isDevelopmentVersion then
modFileHash = getMD5("DevMod_" .. v.filename)
end
else
local len = v.filename:len()
if len > 4 then
local ext = v.filename:sub(len - 3)
if ext == ".zip" or ext == ".gar" then
modName = v.filename:sub(1, len - 4)
modFileHash = getFileMD5(modsDir .. v.filename, modName)
end
end
end
if modName ~= nil then
local modDir = modsDir .. modName .. "/"
local modFile = modDir .. "modDesc.xml"
if loadedMods[modFile] == nil then
loadModDesc(modName, modDir, modFile, modFileHash, modsDir .. v.filename, v.isDirectory, false, false)
loadedMods[modFile] = true
end
end
end
if g_showIllegalActivityInfo then
print("Info: This game protects you from illegal activity")
end
g_showIllegalActivityInfo = nil
end
function loadInternalMods()
haveModsChanged()
for _, modInfo in pairs(internalMods) do
local modName = modInfo.name
local modDir = g_internalModsDirectory .. modName .. "/"
local modFile = modDir .. "modDesc.xml"
local modFileHash = nil
local garFile = g_internalModsDirectory .. modName .. ".gar"
if fileExists(garFile) then
modFileHash = getFileMD5(garFile, modName)
elseif (g_isDevelopmentVersion or GS_IS_CONSOLE_VERSION) and fileExists(modFile) then
modFileHash = getFileMD5(modFile, modName)
end
if modFileHash ~= nil then
loadModDesc(modName, modDir, modFile, modFileHash, g_internalModsDirectory .. modName, true, false, true)
end
end
end
function postInitMods()
for _, v in pairs(g_modEventListeners) do
if v.onPostInit ~= nil then
v:onPostInit()
end
end
end
local function getIsValidModDir(modDir)
if modDir:len() == 0 then
return false
end
if string.startsWith(modDir, g_uniqueDlcNamePrefix) then
return false
end
if modDir:find("%d") == 1 then
return false
end
if modDir:find("[^%w_]") ~= nil then
return false
end
return true
end
local function getIsInternalScriptMod(modName)
for i = 1, #internalScriptMods do
local internalModName = internalScriptMods[i]
if modName == internalModName or modName == internalModName .. "_update" then
return true
end
end
return false
end
local function getIsInternalMod(modName)
for _, modInfo in ipairs(internalMods) do
local internalModName = modInfo.name
if modName == internalModName or modName == internalModName .. "_update" then
return true
end
end
return false
end
local function getIsInternalModInfo(modName)
for _, modInfo in ipairs(internalMods) do
local internalModName = modInfo.name
if modName == internalModName or modName == internalModName .. "_update" then
return modInfo
end
end
return nil
end
local function resolveInternalScriptModFilename(filename, modName, modDir)
if filename:sub(1, modDir:len()) == modDir then
for i = 1, #internalScriptMods do
local internalModName = internalScriptMods[i]
if (modName == internalModName or modName == internalModName .. "_update") and (not fileExists(filename) or GS_IS_CONSOLE_VERSION) then
return "dataS/scripts/internalMods/" .. internalModName .. "/" .. filename:sub(modDir:len() + 1)
end
end
end
return filename
end
function loadModDesc(modName, modDir, modFile, modFileHash, absBaseFilename, isDirectory, addDLCPrefix, isInternalMod)
if not getIsValidModDir(modName) then
print("Error: Invalid mod name '" .. modName .. "'! Characters allowed: (_, A-Z, a-z, 0-9). The first character must not be a digit")
return
end
local origModName = modName
if addDLCPrefix then
modName = g_uniqueDlcNamePrefix .. modName
end
if g_modNameToDirectory[modName] ~= nil then
Logging.error("Mod name '" .. modName .. "' already in use. Ignore it!")
return
end
g_modNameToDirectory[modName] = modDir
local isDLCFile = false
if string.endsWith(modFile, "dlcDesc.xml") then
isDLCFile = true
if not fileExists(modFile) then
if GS_IS_EPIC_VERSION and string.startsWith(modDir, getAppBasePath() .. "pdlc/") then
print("Info: No license for dlc " .. modName .. ".")
else
print("Error: No license for dlc " .. modName .. ". Please reinstall.")
end
return
end
end
setModInstalled(absBaseFilename, addDLCPrefix)
local xmlFile = XMLFile.load("ModFile", modFile)
if xmlFile == nil then
return
end
local modVersion = xmlFile:getString("modDesc.version")
local versionStr = ""
if modVersion ~= nil and modVersion ~= "" then
versionStr = " (Version: " .. modVersion .. ")"
end
if isInternalMod then
local modInfo = getIsInternalModInfo(modName)
if modVersion ~= modInfo.version then
print("Error: Outdated mod version in mod " .. modName)
xmlFile:delete()
return
end
end
local hashStr = ""
if modFileHash ~= nil then
hashStr = "(Hash: " .. modFileHash .. ")"
end
if isDLCFile then
print("Available dlc: " .. hashStr .. versionStr .. " " .. modName)
else
print("Available mod: " .. hashStr .. versionStr .. " " .. modName)
end
local modDescVersion = xmlFile:getInt("modDesc#descVersion")
if modDescVersion == nil then
print("Error: Missing descVersion attribute in mod " .. modName)
xmlFile:delete()
return
end
if modDescVersion < g_minModDescVersion or g_maxModDescVersion < modDescVersion then
print("Error: Unsupported mod description version in mod " .. modName)
xmlFile:delete()
return
end
if _G[modName] ~= nil and not isReloadingDlcs then
print("Error: Invalid mod name '" .. modName .. "'")
xmlFile:delete()
return
end
local outdatedModData = outdatedMods[modName]
if outdatedModData ~= nil then
local versionParts = string.split(modVersion, ".")
local isOutdated = true
for k, part in ipairs(versionParts) do
if k > 4 then
break
end
local number = tonumber(part) or 0
if number ~= nil and outdatedModData[k] ~= nil and outdatedModData[k] < number then
isOutdated = false
break
end
end
if isOutdated then
print("Error: Mod '" .. modName .. "' in version '" .. modVersion .. "' and lower is not supported anymore. Please update the mod!")
xmlFile:delete()
return
end
end
if isDLCFile then
local requiredModName = xmlFile:getString("modDesc.multiplayer#requiredModName")
if requiredModName ~= nil and requiredModName ~= origModName then
print("Error: Do not rename dlcs. Name: '" .. origModName .. "'. Expect: '" .. requiredModName .. "'")
xmlFile:delete()
return
end
end
local isSelectable = xmlFile:getBool("modDesc.isSelectable", true)
local modEnv = {}
if GS_IS_CONSOLE_VERSION then
modEnv = Utils.getNoNil(_G[modName], modEnv)
end
g_globalsNameCheckDisabled = true
_G[modName] = modEnv
g_globalsNameCheckDisabled = false
local modEnv_mt = {
__index = _G
}
setmetatable(modEnv, modEnv_mt)
if not isDLCFile and not isInternalMod then
modEnv._G = modEnv
end
local gEnv = _G
local orgGetfenv = getfenv
function modEnv.getfenv(obj)
local ret = orgGetfenv(obj)
if ret == gEnv then
return modEnv
end
return ret
end
modEnv.g_i18n = g_i18n:addModI18N(modName)
function modEnv.loadstring(str, chunkname)
str = "setfenv(1," .. modName .. "); " .. str
return loadstring(str, chunkname)
end
function modEnv.source(filename, env)
if isAbsolutPath(filename) or isInternalMod then
filename = resolveInternalScriptModFilename(filename, modName, modDir)
source(filename, modName)
else
source(filename)
end
end
function modEnv.InitEventClass(classObject, className)
InitEventClass(classObject, modName .. "." .. className)
end
function modEnv.InitObjectClass(classObject, className)
InitObjectClass(classObject, modName .. "." .. className)
end
function modEnv.registerObjectClassName(object, className)
registerObjectClassName(object, modName .. "." .. className)
end
modEnv.g_constructionBrushTypeManager = {
addBrushType = function (self, typeName, className, filename, customEnvironment)
if isAbsolutPath(filename) or isInternalMod then
filename = resolveInternalScriptModFilename(filename, modName, modDir)
customEnvironment = modName
typeName = modName .. "." .. typeName
className = modName .. "." .. className
end
g_constructionBrushTypeManager:addBrushType(typeName, className, filename, customEnvironment)
end,
getClassObjectByTypeName = function (self, typeName)
local classObj = g_constructionBrushTypeManager:getClassObjectByTypeName(typeName)
if classObj == nil then
classObj = g_constructionBrushTypeManager:getClassObjectByTypeName(modName .. "." .. typeName)
end
return classObj
end
}
setmetatable(modEnv.g_constructionBrushTypeManager, {
__index = g_constructionBrushTypeManager
})
modEnv.g_specializationManager = {
addSpecialization = function (self, name, className, filename, customEnvironment, ...)
if type(self) ~= "table" then
Logging.error("Invalid self object given for SpecializationManager.addSpecialization. Usage: 'g_specializationManager:addSpecialization(name, className, filename, customEnvironment)'")
printCallstack()
end
if customEnvironment ~= nil and type(customEnvironment) ~= "string" then
Logging.error("Invalid customEnvironment given for SpecializationManager.addSpecialization. Should be a string or nil.")
printCallstack()
end
if select("#", ...) > 0 then
Logging.error("Too many arguments for SpecializationManager.addSpecialization. (Arguments should be: name, className, filename, customEnvironment)")
printCallstack()
end
if isAbsolutPath(filename) and (customEnvironment == nil or customEnvironment == "" or customEnvironment == modName) or isInternalMod then
filename = resolveInternalScriptModFilename(filename, modName, modDir)
customEnvironment = modName
name = modName .. "." .. name
className = modName .. "." .. className
end
g_specializationManager:addSpecialization(name, className, filename, customEnvironment)
end,
getSpecializationByName = function (self, name)
local spec = g_specializationManager:getSpecializationByName(name)
if spec == nil then
spec = g_specializationManager:getSpecializationByName(modName .. "." .. name)
end
return spec
end,
getSpecializationObjectByName = function (self, name)
local spec = g_specializationManager:getSpecializationObjectByName(name)
if spec == nil then
spec = g_specializationManager:getSpecializationObjectByName(modName .. "." .. name)
end
return spec
end
}
setmetatable(modEnv.g_specializationManager, {
__index = g_specializationManager
})
modEnv.g_placeableSpecializationManager = {
addSpecialization = function (self, name, className, filename, customEnvironment, ...)
if type(self) ~= "table" then
Logging.error("Invalid self object given for SpecializationManager.addSpecialization. Usage: 'g_specializationManager:addSpecialization(name, className, filename, customEnvironment)'")
printCallstack()
end
if customEnvironment ~= nil and type(customEnvironment) ~= "string" then
Logging.error("Invalid customEnvironment given for SpecializationManager.addSpecialization. Should be a string or nil.")
printCallstack()
end
if select("#", ...) > 0 then
Logging.error("Too many arguments for SpecializationManager.addSpecialization. (Arguments should be: name, className, filename, customEnvironment)")
printCallstack()
end
if isAbsolutPath(filename) and (customEnvironment == nil or customEnvironment == "" or customEnvironment == modName) or isInternalMod then
filename = resolveInternalScriptModFilename(filename, modName, modDir)
customEnvironment = modName
name = modName .. "." .. name
className = modName .. "." .. className
end
g_placeableSpecializationManager:addSpecialization(name, className, filename, customEnvironment)
end,
getSpecializationByName = function (self, name)
local spec = g_placeableSpecializationManager:getSpecializationByName(name)
if spec == nil then
spec = g_placeableSpecializationManager:getSpecializationByName(modName .. "." .. name)
end
return spec
end
}
setmetatable(modEnv.g_placeableSpecializationManager, {
__index = g_placeableSpecializationManager
})
modEnv.g_vehicleTypeManager = {
addType = function (self, typeName, className, filename, customEnvironment)
if isAbsolutPath(filename) or isInternalMod then
filename = resolveInternalScriptModFilename(filename, modName, modDir)
customEnvironment = modName
typeName = modName .. "." .. typeName
className = modName .. "." .. className
end
g_vehicleTypeManager:addType(typeName, className, filename, customEnvironment)
end,
addSpecialization = function (self, typeName, specName)
local spec = g_specializationManager:getSpecializationByName(specName)
if spec == nil and g_specializationManager:getSpecializationByName(modName .. "." .. specName) ~= nil then
specName = modName .. "." .. specName
end
g_vehicleTypeManager:addSpecialization(typeName, specName)
end
}
setmetatable(modEnv.g_vehicleTypeManager, {
__index = g_vehicleTypeManager
})
modEnv.g_placeableTypeManager = {
addType = function (self, typeName, className, filename, customEnvironment)
if isAbsolutPath(filename) or isInternalMod then
filename = resolveInternalScriptModFilename(filename, modName, modDir)
customEnvironment = modName
typeName = modName .. "." .. typeName
className = modName .. "." .. className
end
g_placeableTypeManager:addType(typeName, className, filename, customEnvironment)
end,
addSpecialization = function (self, typeName, specName)
local spec = g_placeableSpecializationManager:getSpecializationByName(specName)
if spec == nil and g_placeableSpecializationManager:getSpecializationByName(modName .. "." .. specName) ~= nil then
specName = modName .. "." .. specName
end
g_placeableTypeManager:addSpecialization(typeName, specName)
end,
getClassObjectByTypeName = function (self, typeName)
local classObj = g_placeableTypeManager:getClassObjectByTypeName(typeName)
if classObj == nil then
classObj = g_placeableTypeManager:getClassObjectByTypeName(modName .. "." .. typeName)
end
return classObj
end
}
setmetatable(modEnv.g_placeableTypeManager, {
__index = g_placeableTypeManager
})
modEnv.g_effectManager = {
registerEffectClass = function (self, className, effectClass)
if not ClassUtil.getIsValidClassName(className) then
print("Error: Invalid effect class name: " .. className)
return
end
_G.g_effectManager:registerEffectClass(modName .. "." .. className, effectClass)
end,
getEffectClass = function (self, className)
local effectClass = _G.g_effectManager:getEffectClass(className)
if effectClass == nil then
effectClass = _G.g_effectManager:getEffectClass(modName .. "." .. className)
end
return effectClass
end
}
setmetatable(modEnv.g_effectManager, {
__index = _G.g_effectManager
})
local userProfilePath = getUserProfileAppPath()
local sub = string.sub
local len = string.len
local function protectedDelete(func)
return function (filename)
local modSettingsDirectory = userProfilePath .. "modSettings/" .. modName
if sub(filename, 1, len(modSettingsDirectory)) == modSettingsDirectory then
func(filename)
else
print(string.format("Error: No access to folder '%s'", filename))
print(string.format("Info: Mod has full access in '%s'", modSettingsDirectory))
printCallstack()
end
end
end
modEnv.g_adsSystem = {}
modEnv.InitStaticEventClass = ""
modEnv.InitStaticObjectClass = ""
modEnv.loadMod = ""
modEnv.loadModDesc = ""
modEnv.loadDlcs = ""
modEnv.loadDlcsFromDirectory = ""
modEnv.loadMods = ""
modEnv.reloadDlcsAndMods = ""
modEnv.verifyDlcs = ""
modEnv.deleteFile = protectedDelete(localDeleteFile)
modEnv.deleteFolder = protectedDelete(localDeleteFolder)
modEnv.isAbsolutPath = isAbsolutPath
modEnv.g_isDevelopmentVersion = g_isDevelopmentVersion
modEnv.GS_IS_CONSOLE_VERSION = GS_IS_CONSOLE_VERSION
if not isDLCFile and not isInternalMod then
modEnv.ClassUtil = {
getClassModName = function (self, className)
local classModName = _G.ClassUtil.getClassModName(className)
if classModName == nil then
classModName = _G.ClassUtil.getClassModName(modName .. "." .. className)
end
return classModName
end
}
end
local onCreateUtil = {
onCreateFunctions = {}
}
modEnv.g_onCreateUtil = onCreateUtil
function onCreateUtil.addOnCreateFunction(name, func)
onCreateUtil.onCreateFunctions[name] = func
end
function onCreateUtil.activateOnCreateFunctions()
for name, func in pairs(onCreateUtil.onCreateFunctions) do
modOnCreate[name] = function (self, id)
func(id)
end
end
end
function onCreateUtil.deactivateOnCreateFunctions()
for name, _ in pairs(onCreateUtil.onCreateFunctions) do
modOnCreate[name] = nil
end
end
xmlFile:iterate("modDesc.l10n.text", function (_, baseName)
local name = xmlFile:getString(baseName .. "#name")
local text = xmlFile:getString(baseName .. "." .. g_languageShort)
if text == nil then
text = xmlFile:getString(baseName .. ".en")
if text == nil then
text = xmlFile:getString(baseName .. ".de")
end
end
if text == nil or name == nil then
print("Warning: No l10n text found for entry '" .. name .. "' in mod '" .. modName .. "'")
elseif modEnv.g_i18n:hasModText(name) then
print("Warning: Duplicate l10n entry '" .. name .. "' in mod '" .. modName .. "'. Ignoring this definition.")
else
modEnv.g_i18n:setText(name, text)
end
end)
local l10nFilenamePrefix = xmlFile:getString("modDesc.l10n#filenamePrefix")
if l10nFilenamePrefix ~= nil then
local l10nFilenamePrefixFull = Utils.getFilename(l10nFilenamePrefix, modDir)
local l10nXmlFile, l10nFilename = nil
local langs = {
g_languageShort,
"en",
"de"
}
for _, lang in ipairs(langs) do
l10nFilename = l10nFilenamePrefixFull .. "_" .. lang .. ".xml"
if fileExists(l10nFilename) then
l10nXmlFile = loadXMLFile("modL10n", l10nFilename)
break
end
end
if l10nXmlFile ~= nil then
local textI = 0
while true do
local key = string.format("l10n.texts.text(%d)", textI)
if not hasXMLProperty(l10nXmlFile, key) then
break
end
local name = getXMLString(l10nXmlFile, key .. "#name")
local text = getXMLString(l10nXmlFile, key .. "#text")
if name ~= nil and text ~= nil then
if modEnv.g_i18n:hasModText(name) then
print("Warning: Duplicate l10n entry '" .. name .. "' in '" .. l10nFilename .. "'. Ignoring this definition.")
else
modEnv.g_i18n:setText(name, text:gsub("\r\n", "\n"))
end
end
textI = textI + 1
end
textI = 0
while true do
local key = string.format("l10n.elements.e(%d)", textI)
if not hasXMLProperty(l10nXmlFile, key) then
break
end
local name = getXMLString(l10nXmlFile, key .. "#k")
local text = getXMLString(l10nXmlFile, key .. "#v")
if name ~= nil and text ~= nil then
if modEnv.g_i18n:hasModText(name) then
print("Warning: Duplicate l10n entry '" .. name .. "' in '" .. l10nFilename .. "'. Ignoring this definition.")
else
modEnv.g_i18n:setText(name, text:gsub("\r\n", "\n"))
end
end
textI = textI + 1
end
delete(l10nXmlFile)
else
print("Warning: No l10n file found for '" .. l10nFilenamePrefix .. "' in mod '" .. modName .. "'")
end
end
local title = xmlFile:getI18NValue("modDesc.title", "", modName, true)
local desc = xmlFile:getI18NValue("modDesc.description", "", modName, true)
local iconFilename = xmlFile:getI18NValue("modDesc.iconFilename", "", modName, true)
if title == "" then
print("Error: Missing title in mod " .. modName)
xmlFile:delete()
return
end
if desc == "" then
print("Error: Missing description in mod " .. modName)
xmlFile:delete()
return
end
local isMultiplayerSupported = xmlFile:getBool("modDesc.multiplayer#supported", false)
local isOnlyMultiplayerSupported = xmlFile:getBool("modDesc.multiplayer#only", false)
if modFileHash == nil then
if isMultiplayerSupported then
print("Warning: Only zip mods are supported in multiplayer. You need to zip the mod " .. modName .. " to use it in multiplayer.")
end
isMultiplayerSupported = false
end
if not isMultiplayerSupported and isOnlyMultiplayerSupported then
print("Error: Both multiplayer and singleplayer are unsupported in mod " .. modName)
xmlFile:delete()
return
end
if isMultiplayerSupported and iconFilename == "" then
print("Error: Missing icon filename in mod " .. modName)
xmlFile:delete()
return
end
xmlFile:iterate("modDesc.maps.map", function (_, baseName)
g_mapManager:loadMapFromXML(xmlFile, baseName, modDir, modName, isMultiplayerSupported, isDLCFile, true, isInternalMod)
end)
xmlFile:iterate("modDesc.scenarios.scenario", function (_, baseName)
g_scenarioManager:loadScenarioFromXML(xmlFile, baseName, modDir, modName, isDLCFile)
end)
local author = xmlFile:getI18NValue("modDesc.author", "", modName, true)
if isDLCFile then
local dlcProductId = xmlFile:getString("modDesc.productId")
if dlcProductId == nil or modVersion == nil then
print("Error: invalid product id or version in DLC " .. modName)
else
addNotificationFilter(dlcProductId, modVersion)
end
end
local hasScripts = xmlFile:hasProperty("modDesc.extraSourceFiles.sourceFile(0)") or xmlFile:hasProperty("modDesc.specializations.specialization(0)")
local dependencies = nil
if xmlFile:hasProperty("modDesc.dependencies.dependency(0)") then
dependencies = {}
xmlFile:iterate("modDesc.dependencies.dependency", function (index, key)
local name = xmlFile:getString(key)
if name ~= nil and name:len() > 0 then
dependencies[#dependencies + 1] = name
end
end)
end
local uniqueType = xmlFile:getString("modDesc.uniqueType")
xmlFile:iterate("modDesc.extraContent.key", function (_, xmlKey)
local key = xmlFile:getString(xmlKey)
if key ~= nil then
local item, errorCode = g_extraContentSystem:unlockItem(key, true)
if item ~= nil and errorCode == ExtraContentSystem.UNLOCKED then
print("ExtraContent: Unlocked '" .. item.id .. "'")
g_extraContentSystem:saveToProfile()
end
end
end)
if isInternalMod then
g_currentModDirectory = modDir
g_currentModName = modName
xmlFile:iterate("modDesc.preLoadSourceFiles.sourceFile", function (_, key)
local filename = xmlFile:getString(key .. "#filename")
if filename ~= nil then
modEnv.source(modDir .. filename, modName)
end
end)
end
iconFilename = Utils.getFilename(iconFilename, modDir)
g_modManager:addMod(title, desc, modVersion, modDescVersion, author, iconFilename, modName, modDir, modFile, isMultiplayerSupported, modFileHash, absBaseFilename, isDirectory, isDLCFile, hasScripts, dependencies, isOnlyMultiplayerSupported, isSelectable, uniqueType, getIsInternalScriptMod(modName))
xmlFile:delete()
end
function resetModOnCreateFunctions()
modOnCreate = {}
end
function loadMod(modName, modDir, modFile, modTitle)
if g_modIsLoaded[modName] then
return
end
g_modIsLoaded[modName] = true
g_modNameToDirectory[modName] = modDir
local modEnv = _G[modName]
if modEnv == nil then
return
end
local xmlFile = XMLFile.load("ModFile", modFile)
local isDLCFile = false
if string.endsWith(modFile, "dlcDesc.xml") then
isDLCFile = true
end
if isDLCFile then
print(" Load dlc: " .. modName)
else
print(" Load mod: " .. modName)
end
local allowScripts = not GS_IS_CONSOLE_VERSION or isDLCFile or g_isDevelopmentConsoleScriptModTesting or getIsInternalScriptMod(modName) or getIsInternalMod(modName)
g_currentModDirectory = modDir
g_currentModName = modName
if g_modSettingsDirectory ~= nil then
g_currentModSettingsDirectory = g_modSettingsDirectory .. modName .. "/"
end
if allowScripts then
xmlFile:iterate("modDesc.extraSourceFiles.sourceFile", function (_, key)
local filename = xmlFile:getString(key .. "#filename")
if filename ~= nil then
modEnv.source(modDir .. filename, modName)
end
end)
end
xmlFile:iterate("modDesc.splitTypes.splitType", function (_, key)
local name = xmlFile:getString(key .. "#name")
local l10nKey = xmlFile:getString(key .. "#l10nKey")
local splitTypeIndex = xmlFile:getInt(key .. "#splitTypeIndex")
local pricePerLiter = xmlFile:getFloat(key .. "#pricePerLiter")
local woodChipsPerLiter = xmlFile:getFloat(key .. "#woodChipsPerLiter")
local allowsWoodHarvester = xmlFile:getBool(key .. "#allowsWoodHarvester")
local volumeToLiter = xmlFile:getInt(key .. "#volumeToLiter")
g_splitTypeManager:addSplitType(name, l10nKey, splitTypeIndex, pricePerLiter, woodChipsPerLiter, allowsWoodHarvester, modName, volumeToLiter)