-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathInject.lua
69 lines (66 loc) · 3.38 KB
/
Inject.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
--[[
Insert this code into "DCSWorld\Scripts\Input\Data.lua" inside the function declaration for "loadDeviceProfileFromFile"
search for the line 'local function loadDeviceProfileFromFile(filename, deviceName, folder,keep_G_untouched)' and paste this function below it
Then add the line:
QuagglesInputCommandInjector(deviceGenericName, filename, folder, env, result)
into the "loadDeviceProfileFromFile" function below the line:
status, result = pcall(f)
]]--
local function QuagglesInputCommandInjector(deviceGenericName, filename, folder, env, result)
local quagglesLogName = 'Quaggles.InputCommandInjector'
local quagglesLoggingEnabled = false
-- Returns true if string starts with supplied string
local function StartsWith(String,Start)
return string.sub(String,1,string.len(Start))==Start
end
if quagglesLoggingEnabled then log.write(quagglesLogName, log.INFO, 'Detected loading of type: "'..deviceGenericName..'", filename: "'..filename..'"') end
-- Only operate on files that are in this folder
local targetPrefixForAircrafts = "./Mods/aircraft/"
local targetPrefixForDotConfig = "./Config/Input/"
local targetPrefixForConfig = "Config/Input/"
local targetPrefix = nil
if StartsWith(filename, targetPrefixForAircrafts) and StartsWith(folder, targetPrefixForAircrafts) then
targetPrefix = targetPrefixForAircrafts
elseif StartsWith(filename, targetPrefixForDotConfig) and StartsWith(folder, targetPrefixForDotConfig) then
targetPrefix = targetPrefixForDotConfig
elseif StartsWith(filename, targetPrefixForConfig) then
targetPrefix = targetPrefixForConfig
end
if targetPrefix then
-- Transform path to user folder
local newFileName = filename:gsub(targetPrefix, lfs.writedir():gsub('\\','/').."InputCommands/")
if quagglesLoggingEnabled then log.write(quagglesLogName, log.INFO, '--Translated path: "'..newFileName..'"') end
-- If the user has put a file there continue
if lfs.attributes(newFileName) then
if quagglesLoggingEnabled then log.write(quagglesLogName, log.INFO, '----Found merge at: "'..newFileName..'"') end
--Configure file to run in same environment as the default command entry file
local f, err = loadfile(newFileName)
if err ~= nil then
log.write(quagglesLogName, log.ERROR, '------Failure loading: "'..tostring(newFileName)..'"'..' Error: "'..tostring(err)..'"')
return
else
setfenv(f, env)
local statusInj, resultInj
statusInj, resultInj = pcall(f)
-- Merge resulting tables
if statusInj then
if result.keyCommands and resultInj.keyCommands then -- If both exist then join
env.join(result.keyCommands, resultInj.keyCommands)
elseif resultInj.keyCommands then -- If just the injected one exists then use it
result.keyCommands = resultInj.keyCommands
end
if deviceGenericName ~= "Keyboard" then -- Don't add axisCommands for keyboard
if result.axisCommands and resultInj.axisCommands then -- If both exist then join
env.join(result.axisCommands, resultInj.axisCommands)
elseif resultInj.axisCommands then -- If just the injected one exists then use it
result.axisCommands = resultInj.axisCommands
end
end
if quagglesLoggingEnabled then log.write(quagglesLogName, log.INFO, '------Merge successful') end
else
if quagglesLoggingEnabled then log.write(quagglesLogName, log.INFO, '------Merge failed: "'..tostring(statusInj)..'"') end
end
end
end
end
end