forked from FAForever/fa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
init_faf.lua
479 lines (409 loc) · 18.7 KB
/
init_faf.lua
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
-- START OF COPY --
-- in an ideal world this file would be loaded (using dofile) by the other
-- initialisation files to prevent code duplication. However, as it stands
-- we can not load in additional init files with the current deployment
-- system and therefore we copy/paste this section into the other init files.
-- imports fa_path to determine where it is installed
dofile(InitFileDir .. '/../fa_path.lua')
LOG("Client version: " .. tostring(ClientVersion))
LOG("Game version: " .. tostring(GameVersion))
LOG("Game type: " .. tostring(GameType))
-- upvalued performance
local dofile = dofile
local StringFind = string.find
local StringGsub = string.gsub
local StringSub = string.sub
local StringLower = string.lower
local IoDir = io.dir
local TableInsert = table.insert
local TableGetn = table.getn
-- read by the engine to determine where to find assets
path = {}
-- read by the engine to determine hook folders
hook = {
'/schook'
}
-- read by the engine to determine supported protocols
protocols = {
'http',
'https',
'mailto',
'ventrilo',
'teamspeak',
'daap',
'im',
}
-- upvalued for performance
local UpvaluedPath = path
local UpvaluedPathNext = 1
--- Lowers the strings of a hash-based table, crashes when other type of keys are used (integers, for example)
local function LowerHashTable(t)
local o = { }
for k, v in t do
o[StringLower(k)] = v
end
return o
end
-- mods that have been integrated, based on folder name
local integratedMods = { }
integratedMods["nvidia fix"] = true
integratedMods = LowerHashTable(integratedMods)
-- mods that are deprecated, based on folder name
local deprecatedMods = { }
deprecatedMods["simspeed++"] = true
deprecatedMods["#quality of performance 2022"] = true
-- as per #4119 the control groups (called selection sets in code) are completely overhauled and extended feature-wise,
-- because of that these mods are no longer viable / broken / integrated
deprecatedMods["group_split"] = true
deprecatedMods["Control Group Zoom Mod"] = true
deprecatedMods["additionalControlGroupStuff"] = true
-- as per #4124 the cursor and command interactions are complete overhauled and extended feature-wise,
-- because of that these mods are no longer viable / broken / integrated
deprecatedMods["additionalCameraStuff"] = true
deprecatedMods["RUI"] = true
deprecatedMods = LowerHashTable(deprecatedMods)
-- typical FA packages
local allowedAssetsScd = { }
allowedAssetsScd["units.scd"] = true
allowedAssetsScd["textures.scd"] = true
allowedAssetsScd["skins.scd"] = true
allowedAssetsScd["schook.scd"] = false -- completely embedded in the repository
allowedAssetsScd["props.scd"] = true
allowedAssetsScd["projectiles.scd"] = true
allowedAssetsScd["objects.scd"] = true
allowedAssetsScd["moholua.scd"] = false -- completely embedded in the repository
allowedAssetsScd["mohodata.scd"] = false -- completely embedded in the repository
allowedAssetsScd["mods.scd"] = true
allowedAssetsScd["meshes.scd"] = true
allowedAssetsScd["lua.scd"] = false -- completely embedded in the repository
allowedAssetsScd["loc_us.scd"] = true
allowedAssetsScd["loc_es.scd"] = true
allowedAssetsScd["loc_fr.scd"] = true
allowedAssetsScd["loc_it.scd"] = true
allowedAssetsScd["loc_de.scd"] = true
allowedAssetsScd["loc_ru.scd"] = true
allowedAssetsScd["env.scd"] = true
allowedAssetsScd["effects.scd"] = true
allowedAssetsScd["editor.scd"] = false -- Unused
allowedAssetsScd["ambience.scd"] = false -- Empty
allowedAssetsScd["sc_music.scd"] = true
allowedAssetsScd = LowerHashTable(allowedAssetsScd)
-- typical backwards compatible packages
local allowedAssetsNxt = { }
allowedAssetsNxt["kyros.nxt"] = true
allowedAssetsNxt["advanced strategic icons.nxt"] = true
allowedAssetsNxt["advanced_strategic_icons.nxt"] = true
allowedAssetsNxt = LowerHashTable(allowedAssetsNxt)
-- default wave banks to prevent collisions
local soundsBlocked = { }
local faSounds = IoDir(fa_path .. '/sounds/*')
for k, v in faSounds do
if v == '.' or v == '..' then
continue
end
soundsBlocked[StringLower(v)] = "FA installation"
end
-- default movie files to prevent collisions
local moviesBlocked = { }
local faMovies = IoDir(fa_path .. '/movies/*')
for k, v in faMovies do
if v == '.' or v == '..' then
continue
end
moviesBlocked[StringLower(v)] = "FA installation"
end
--- Mounts a directory or scd / zip file.
-- @param dir The absolute path to the directory
-- @param mountpoint The path to use in the game (e.g., /maps/...)
local function MountDirectory(dir, mountpoint)
UpvaluedPath[UpvaluedPathNext] = {
dir = dir,
mountpoint = mountpoint
}
UpvaluedPathNext = UpvaluedPathNext + 1
end
--- Mounts all allowed content in a directory, including scd and zip files, directly.
-- @param dir The absolute path to the directory
-- @param mountpoint The path to use in the game (e.g., /maps/...)
local function MountAllowedContent(dir, pattern, allowedAssets)
for _,entry in IoDir(dir .. pattern) do
if entry != '.' and entry != '..' then
local mp = StringLower(entry)
if allowedAssets[mp] then
LOG("mounting content: " .. entry)
MountDirectory(dir .. "/" .. entry, '/')
end
end
end
end
--- Keep track of what maps are loaded to prevent collisions
local loadedMaps = { }
--- A helper function that loads in additional content for maps.
-- @param mountpoint The root folder to look for content in.
local function MountMapContent(dir)
-- look for all directories / maps at the mount point
for _, map in IoDir(dir .. '/*') do
-- prevent capital letters messing things up
map = StringLower(map)
-- do not do anything with the current / previous directory
if map == '.' or map == '..' then
continue
end
-- do not load archives as maps
local extension = StringSub(map, -4)
if extension == ".zip" or extension == ".scd" or extension == ".rar" then
LOG("Prevented loading a map inside a zip / scd / rar file: " .. dir .. "/" .. map)
continue
end
-- check if the folder contains map required map files
local scenarioFile = false
local scmapFile = false
local saveFile = false
local scriptFile = false
for _, file in IoDir(dir .. "/" .. map .. "/*") do
if StringSub(file, -13) == '_scenario.lua' then
scenarioFile = file
elseif StringSub(file, -11) == '_script.lua' then
scriptFile = file
elseif StringSub(file, -9) == '_save.lua' then
saveFile = file
elseif StringSub(file, -6) == '.scmap' then
scmapFile = file
end
end
-- check if it has a scenario file
if not scenarioFile then
LOG("Prevented loading a map with no scenario file: " .. dir .. "/" .. map)
continue
end
if not scmapFile then
LOG("Prevented loading a map with no scmap file: " .. dir .. "/" .. map)
continue
end
if not saveFile then
LOG("Prevented loading a map with no save file: " .. dir .. "/" .. map)
continue
end
if not scriptFile then
LOG("Prevented loading a map with no script file: " .. dir .. "/" .. map)
continue
end
-- tried to load in the scenario file, but in all cases it pollutes the global scope and we can't have that
-- https://stackoverflow.com/questions/9540732/loadfile-without-polluting-global-environment
-- do not load maps twice
if loadedMaps[map] then
LOG("Prevented loading a map twice: " .. map)
continue
end
-- consider this one loaded
loadedMaps[map] = true
-- mount the map
MountDirectory(dir .. "/" .. map, "/maps/" .. map)
-- look at each directory inside this map
for _, folder in IoDir(dir .. '/' .. map .. '/*') do
-- do not do anything with the current / previous directory
if folder == '.' or folder == '..' then
continue
end
if folder == 'movies' then
-- find conflicting files
local conflictingFiles = { }
for _, file in IoDir(dir .. '/' .. map .. '/movies/*') do
if not (file == '.' or file == '..') then
local identifier = StringLower(file)
if moviesBlocked[identifier] then
TableInsert(conflictingFiles, { file = file, conflict = moviesBlocked[identifier] })
else
moviesBlocked[identifier] = StringLower(map)
end
end
end
-- report them if they exist and do not mount
if TableGetn(conflictingFiles) > 0 then
LOG("Found conflicting movie banks for map: '" .. map .. "', cannot mount the movie bank(s):")
for k, v in conflictingFiles do
LOG(" - Conflicting movie bank: '" .. v.file .. "' of map '" .. map .. "' is conflicting with a movie bank from: '" .. v.conflict .. "'" )
end
-- else, mount folder
else
LOG("Mounting movies of map: " .. map )
MountDirectory(dir .. "/" .. map .. '/movies', '/movies')
end
elseif folder == 'sounds' then
-- find conflicting files
local conflictingFiles = { }
for _, file in IoDir(dir .. '/' .. map .. '/sounds/*') do
if not (file == '.' or file == '..') then
local identifier = StringLower(file)
if soundsBlocked[identifier] then
TableInsert(conflictingFiles, { file = file, conflict = soundsBlocked[identifier] })
else
soundsBlocked[identifier] = StringLower(map)
end
end
end
-- report them if they exist and do not mount
if TableGetn(conflictingFiles) > 0 then
LOG("Found conflicting sound banks for map: '" .. map .. "', cannot mount the sound bank(s):")
for k, v in conflictingFiles do
LOG(" - Conflicting sound bank: '" .. v.file .. "' of map '" .. map .. "' is conflicting with a sound bank from: '" .. v.conflict .. "'" )
end
-- else, mount folder
else
LOG("Mounting sounds of map: " .. map )
MountDirectory(dir.. "/" .. map .. '/sounds', '/sounds')
end
end
end
end
end
--- keep track of what mods are loaded to prevent collisions
local loadedMods = { }
--- A helper function that loads in additional content for mods.
-- @param mountpoint The root folder to look for content in.
local function MountModContent(dir)
-- get all directories / mods at the mount point
for _, mod in io.dir(dir..'/*.*') do
-- prevent capital letters messing things up
mod = StringLower(mod)
-- do not do anything with the current / previous directory
if mod == '.' or mod == '..' then
continue
end
-- do not load integrated mods
if integratedMods[mod] then
LOG("Prevented loading a mod that is integrated: " .. mod )
continue
end
-- do not load deprecated mods
if deprecatedMods[mod] then
LOG("Prevented loading a mod that is deprecated: " .. mod )
continue
end
-- do not load archives as mods
local extension = StringSub(mod, -4)
if extension == ".zip" or extension == ".scd" or extension == ".rar" then
LOG("Prevented loading a mod inside a zip / scd / rar file: " .. dir .. "/" .. mod)
continue
end
-- check if the folder contains a _info.lua
local infoFile = false
for _, file in IoDir(dir .. "/" .. mod .. "/*") do
if StringSub(file, -9) == '_info.lua' then
infoFile = file
end
end
-- check if it has a scenario file
if not infoFile then
LOG("Mod doesn't have an info file: " .. dir .. "/" .. mod)
continue
end
-- do not load mods twice
if loadedMods[mod] then
LOG("Prevented loading a mod twice: " .. mod)
continue
end
-- consider this one loaded
loadedMods[mod] = true
-- mount the mod
MountDirectory(dir .. "/" .. mod, "/mods/" .. mod)
-- look at each directory inside this mod
for _, folder in IoDir(dir .. '/' .. mod .. '/*') do
-- if we found a directory named 'sounds' then we mount its content
if folder == 'sounds' then
-- find conflicting files
local conflictingFiles = { }
for _, file in IoDir(dir .. '/' .. mod .. '/sounds/*') do
if not (file == '.' or file == '..') then
local identifier = StringLower(file)
if soundsBlocked[identifier] then
TableInsert(conflictingFiles, { file = file, conflict = soundsBlocked[identifier] })
else
soundsBlocked[identifier] = StringLower(mod)
end
end
end
-- report them if they exist and do not mount
if TableGetn(conflictingFiles) > 0 then
LOG("Found conflicting sound banks for mod: '" .. mod .. "', cannot mount the sound bank(s):")
for k, v in conflictingFiles do
LOG(" - Conflicting sound bank: '" .. v.file .. "' of mod '" .. mod .. "' is conflicting with a sound bank from: '" .. v.conflict .. "'" )
end
-- else, mount folder
else
LOG("Mounting sounds in mod: " .. mod )
MountDirectory(dir .. "/" .. mod .. '/sounds', '/sounds')
end
end
-- if we found a directory named 'custom-strategic-icons' then we mount its content
if folder == 'custom-strategic-icons' then
local mountLocation = '/textures/ui/common/game/strategicicons/' .. mod
LOG('Found mod icons in ' .. mod .. ', mounted at: ' .. mountLocation)
MountDirectory(dir .. '/' .. mod .. '/custom-strategic-icons', mountLocation)
end
-- if we found a file named 'custom-strategic-icons.scd' then we mount its content - good for performance when the number of icons is high
if folder == 'custom-strategic-icons.scd' then
local mountLocation = '/textures/ui/common/game/strategicicons/' .. mod
LOG('Found mod icon package in ' .. mod .. ', mounted at: ' .. mountLocation)
MountDirectory(dir .. '/' .. mod .. '/custom-strategic-icons.scd', mountLocation)
end
end
end
end
--- A helper function to load in all maps and mods on a given location.
-- @param path The root folder for the maps and mods
local function LoadVaultContent(path)
-- load in additional things, like sounds and
MountMapContent(path .. '/maps')
MountModContent(path .. '/mods')
end
-- END OF COPY --
-- minimum viable shader version - should be bumped to the next release version when we change the shaders
local minimumShaderVersion = 3729
-- look for unviable shaders and remove them
local shaderCache = SHGetFolderPath('LOCAL_APPDATA') .. 'Gas Powered Games/Supreme Commander Forged Alliance/cache'
for k, file in IoDir(shaderCache .. '/*') do
if file != '.' and file != '..' then
local version = tonumber(string.sub(file, -4))
if not version or version < minimumShaderVersion then
LOG("Removed incompatible shader: " .. file)
os.remove(shaderCache .. '/' .. file)
end
end
end
-- typical FAF packages
local allowedAssetsNxy = { }
allowedAssetsNxy["effects.nx2"] = true
allowedAssetsNxy["env.nx2"] = true
allowedAssetsNxy["etc.nx2"] = true
allowedAssetsNxy["loc.nx2"] = true
allowedAssetsNxy["lua.nx2"] = true
allowedAssetsNxy["meshes.nx2"] = true
allowedAssetsNxy["mods.nx2"] = true
allowedAssetsNxy["projectiles.nx2"] = true
-- allowedAssetsNxy["schook.nx2"] = true
allowedAssetsNxy["textures.nx2"] = true
allowedAssetsNxy["units.nx2"] = true
allowedAssetsNxy = LowerHashTable(allowedAssetsNxy)
-- load maps / mods from custom vault location, if set by client
if custom_vault_path then
LOG('Loading custom vault path: ' .. custom_vault_path)
LoadVaultContent(custom_vault_path)
else
LOG("No custom vault path defined: loading from backup locations. You should update your client to 2021/10/+.")
-- load maps / mods from backup vault location location
LoadVaultContent(InitFileDir .. '/../user/My Games/Gas Powered Games/Supreme Commander Forged Alliance')
-- load maps / mods from my documents vault location
LoadVaultContent(SHGetFolderPath('PERSONAL') .. 'My Games/Gas Powered Games/Supreme Commander Forged Alliance')
end
-- load in .nxt / .nx2 / .scd files that we allow
MountAllowedContent(InitFileDir .. '/../gamedata/', '*.nxt', allowedAssetsNxt)
MountAllowedContent(InitFileDir .. '/../gamedata/', '*.nx2', allowedAssetsNxy)
MountAllowedContent(fa_path .. '/gamedata/', '*.scd', allowedAssetsScd)
-- get direct access to preferences file, letting us have much more control over its content. This also includes cache and similar
MountDirectory(SHGetFolderPath('LOCAL_APPDATA') .. 'Gas Powered Games/Supreme Commander Forged Alliance', '/preferences')
-- Load in all the data of the steam installation (movies, maps, sound folders)
MountDirectory(fa_path .. "/movies", '/movies')
MountDirectory(fa_path .. "/sounds", '/sounds')
MountDirectory(fa_path .. "/maps", '/maps')
MountDirectory(fa_path .. "/fonts", '/fonts')