diff --git a/installer/olympus.iss b/installer/olympus.iss index b7908211..daaa6976 100644 --- a/installer/olympus.iss +++ b/installer/olympus.iss @@ -51,36 +51,11 @@ Source: "..\scripts\python\configurator\dist\configurator.exe"; DestDir: "{app}\ [Run] Filename: "{app}\Mods\Services\Olympus\configurator.exe"; Parameters: -a {code:GetAddress} -c {code:GetClientPort} -b {code:GetBackendPort} -p {code:GetPassword} -bp {code:GetBluePassword} -rp {code:GetRedPassword}; Check: CheckCallConfigurator -;[Registry] -;Root: HKCU; Subkey: "Environment"; ValueType: string; ValueName: "DCSOLYMPUS_PATH"; ValueData: "{app}\Mods\Services\Olympus"; Flags: preservestringtype -;Root: HKCU; Subkey: "Environment"; ValueType: expandsz; ValueName: "Path"; ValueData: "{olddata};%DCSOLYMPUS_PATH%\bin"; Check: NeedsAddPath('%DCSOLYMPUS_PATH%\bin'); - -;[Setup] -; Tell Windows Explorer to reload the environment -;ChangesEnvironment=yes - [Icons] Name: "{userdesktop}\DCS Olympus Client"; Filename: "{app}\Mods\Services\Olympus\client\nw.exe"; Tasks: desktopicon; IconFilename: "{app}\Mods\Services\Olympus\img\olympus.ico"; Check: CheckLocalInstall Name: "{userdesktop}\DCS Olympus Server"; Filename: "{app}\Mods\Services\Olympus\client\node.exe"; Tasks: desktopicon; IconFilename: "{app}\Mods\Services\Olympus\img\olympus_server.ico"; Parameters: ".\bin\www"; Check: CheckServerInstall Name: "{userdesktop}\DCS Olympus Configurator"; Filename: "{app}\Mods\Services\Olympus\configurator.exe"; Tasks: desktopicon; IconFilename: "{app}\Mods\Services\Olympus\img\olympus_configurator.ico"; Check: CheckServerInstall -;[Code] -;function NeedsAddPath(Param: string): boolean; -;var -; OrigPath: string; -;begin -; if not RegQueryStringValue(HKCU, -; 'Environment', -; 'Path', OrigPath) -; then begin -; Result := True; -; exit; -; end; -; { look for the path with leading and trailing semicolon } -; { Pos() returns 0 if not found } -; Result := Pos(';' + Param + ';', ';' + OrigPath + ';') = 0; -;end; - [Code] var lblLocalInstall: TLabel; diff --git a/scripts/OlympusCommand.lua b/scripts/OlympusCommand.lua index 29100d4e..6eb4789e 100644 --- a/scripts/OlympusCommand.lua +++ b/scripts/OlympusCommand.lua @@ -5,7 +5,6 @@ local debug = false -- True enables debug printing using DCS messages -- .dll related variables Olympus.OlympusDLL = nil Olympus.DLLsloaded = false -Olympus.OlympusModPath = os.getenv('DCSOLYMPUS_PATH')..'\\bin\\' -- Logger reference Olympus.log = mist.Logger:new("Olympus", 'info') @@ -31,6 +30,10 @@ Olympus.weapons = {} -- Table holding references to all the currently existing Olympus.missionStartTime = DCS.getRealTime() Olympus.napalmCounter = 1 Olympus.fireCounter = 1 + +-- Load the current instance folder +local lfs = require('lfs') + ------------------------------------------------------------------------------------------------------ -- Olympus functions ------------------------------------------------------------------------------------------------------ @@ -49,7 +52,7 @@ end -- Loads the olympus .dll function Olympus.loadDLLs() -- Add the .dll paths - package.cpath = package.cpath..';'..Olympus.OlympusModPath..'?.dll;' + package.cpath = package.cpath..';'..Olympus.instancePath..'?.dll;' local status status, Olympus.OlympusDLL = pcall(require, 'olympus') @@ -1361,6 +1364,10 @@ end ------------------------------------------------------------------------------------------------------ -- Olympus startup script ------------------------------------------------------------------------------------------------------ +Olympus.instancePath = lfs.writedir().."Mods\\Services\\Olympus\\bin\\" +Olympus.notify("Starting DCS Olympus backend session in "..Olympus.instancePath, 2) + + local OlympusName = 'Olympus ' .. version .. ' C++ module'; Olympus.DLLsloaded = Olympus.loadDLLs() if Olympus.DLLsloaded then @@ -1402,11 +1409,3 @@ Olympus.initializeUnits() Olympus.notify("OlympusCommand script " .. version .. " loaded successfully", 2, true) --- Load the current instance folder -local lfs = require('lfs') - -Olympus.instancePath = lfs.writedir().."Mods\\Services\\Olympus" - -Olympus.notify("Starting DCS Olympus backend session in "..Olympus.instancePath, 2) -Olympus.OlympusDLL.setInstancePath() - diff --git a/scripts/OlympusHook.lua b/scripts/OlympusHook.lua index 2e60f60c..62f30981 100644 --- a/scripts/OlympusHook.lua +++ b/scripts/OlympusHook.lua @@ -1,10 +1,10 @@ local version = 'v0.4.11-alpha-rc3' +local lfs = require("lfs") Olympus = {} Olympus.OlympusDLL = nil -Olympus.cppRESTDLL = nil Olympus.DLLsloaded = false -Olympus.OlympusModPath = os.getenv('DCSOLYMPUS_PATH')..'\\bin\\' +Olympus.OlympusModPath = lfs.writedir().."Mods\\Services\\Olympus\\bin\\" log.write('Olympus.HOOKS.LUA', log.INFO,'Executing OlympusHook.lua') @@ -14,7 +14,7 @@ function Olympus.loadDLLs() local status log.write('Olympus.HOOKS.LUA', log.INFO, 'Loading olympus.dll from ['..Olympus.OlympusModPath..']') - status, Olympus.OlympusDLL = pcall(require, 'olympus') + status, Olympus.OlympusDLL = require("olympus") if status then log.write('Olympus.HOOKS.LUA', log.INFO, 'olympus.dll loaded successfully') return true diff --git a/src/core/src/core.cpp b/src/core/src/core.cpp index d59432ce..cf31da84 100644 --- a/src/core/src/core.cpp +++ b/src/core/src/core.cpp @@ -52,8 +52,12 @@ extern "C" DllExport int coreDeinit(lua_State* L) } /* Called when DCS simulation starts. All singletons are instantiated, and the custom Lua functions are registered in the Lua state. */ -extern "C" DllExport int coreInit(lua_State* L) +extern "C" DllExport int coreInit(lua_State* L, const char* path) { + instancePath = path; + + log("Initializing core.dll with instance path " + instancePath); + sessionHash = random_string(16); unitsManager = new UnitsManager(L); weaponsManager = new WeaponsManager(L); @@ -70,20 +74,6 @@ extern "C" DllExport int coreInit(lua_State* L) return(0); } -extern "C" DllExport int coreInstancePath(lua_State * L) -{ - /* Lock for thread safety */ - lock_guard guard(mutexLock); - - lua_getglobal(L, "Olympus"); - lua_getfield(L, -1, "instancePath"); - instancePath = lua_tostring(L, -1); - - log("Setting instance path to " + instancePath); - - return(0); -} - extern "C" DllExport int coreFrame(lua_State* L) { if (!initialized) diff --git a/src/core/src/scriptloader.cpp b/src/core/src/scriptloader.cpp index 12ed68c8..aefc793e 100644 --- a/src/core/src/scriptloader.cpp +++ b/src/core/src/scriptloader.cpp @@ -5,6 +5,8 @@ #include +extern string instancePath; + bool executeLuaScript(lua_State* L, string path) { replace(path.begin(), path.end(), '\\', '/'); @@ -36,21 +38,8 @@ void registerLuaFunctions(lua_State* L) log("protectedCall registered successfully"); } - char* buf = nullptr; - size_t sz = 0; - if (_dupenv_s(&buf, &sz, "DCSOLYMPUS_PATH") == 0 && buf != nullptr) - { - modLocation = buf; - free(buf); - } - else - { - log("DCSOLYMPUS_PATH environment variable is missing"); - return; - } - - executeLuaScript(L, modLocation + "\\Scripts\\mist.lua"); - executeLuaScript(L, modLocation + "\\Scripts\\OlympusCommand.lua"); - executeLuaScript(L, modLocation + "\\Scripts\\unitPayloads.lua"); - executeLuaScript(L, modLocation + "\\Scripts\\templates.lua"); + executeLuaScript(L, instancePath + "..\\Scripts\\mist.lua"); + executeLuaScript(L, instancePath + "..\\Scripts\\OlympusCommand.lua"); + executeLuaScript(L, instancePath + "..\\Scripts\\unitPayloads.lua"); + executeLuaScript(L, instancePath + "..\\Scripts\\templates.lua"); } diff --git a/src/luatools/include/luatools.h b/src/luatools/include/luatools.h index efc5cc03..a97a3308 100644 --- a/src/luatools/include/luatools.h +++ b/src/luatools/include/luatools.h @@ -5,6 +5,7 @@ void DllExport stackUpdate(lua_State* L, int& stackDepth, int initialStack = 0); void DllExport stackPop(lua_State* L, int popDepth = 1); void DllExport stackClean(lua_State* L, int stackDepth); void DllExport luaTableToJSON(lua_State* L, int index, json::value& json, bool logKeys = false); +void DllExport luaLogTableKeys(lua_State* L, int index); #define STACK_UPDATE stackUpdate(L, stackDepth, initialStack); #define STACK_INIT int stackDepth = 0; int initialStack = 0; stackUpdate(L, initialStack); diff --git a/src/luatools/src/luatools.cpp b/src/luatools/src/luatools.cpp index affe2267..7c288103 100644 --- a/src/luatools/src/luatools.cpp +++ b/src/luatools/src/luatools.cpp @@ -18,6 +18,29 @@ void stackClean(lua_State* L, int stackDepth) lua_pop(L, stackDepth); } + +void luaLogTableKeys(lua_State* L, int index) +{ + if (lua_istable(L, index)) + { + STACK_INIT; + + lua_pushvalue(L, index); + lua_pushnil(L); + while (lua_next(L, -2)) + { + lua_pushvalue(L, -2); + const char* key = lua_tostring(L, -1); + log(key); + lua_pop(L, 2); + } + lua_pop(L, 1); + + STACK_CLEAN; + } +} + + void luaTableToJSON(lua_State* L, int index, json::value& json, bool logKeys) { if (lua_istable(L, index)) diff --git a/src/olympus/olympus.vcxproj b/src/olympus/olympus.vcxproj index 50e68d5c..7463c140 100644 --- a/src/olympus/olympus.vcxproj +++ b/src/olympus/olympus.vcxproj @@ -20,6 +20,9 @@ {873ecabe-fcfe-4217-ac15-91959c3cf1c6} + + {de139ec1-4f88-47d5-be73-f41915fe14a3} + {b85009ce-4a5c-4a5a-b85d-001b3a2651b2} @@ -112,6 +115,7 @@ false lua.lib ..\..\third-party\lua + dcstools.dll;logger.dll;utils.dll;%(DelayLoadDLLs) diff --git a/src/olympus/src/olympus.cpp b/src/olympus/src/olympus.cpp index 2417b054..6951684d 100644 --- a/src/olympus/src/olympus.cpp +++ b/src/olympus/src/olympus.cpp @@ -5,40 +5,26 @@ /* Run-time linking to core dll allows for "hot swap". This is useful for development but could be removed when stable.*/ HINSTANCE hGetProcIDDLL = NULL; -typedef int(__stdcall* f_coreInit)(lua_State* L); +typedef int(__stdcall* f_coreInit)(lua_State* L, const char* path); typedef int(__stdcall* f_coreDeinit)(lua_State* L); typedef int(__stdcall* f_coreFrame)(lua_State* L); typedef int(__stdcall* f_coreUnitsData)(lua_State* L); typedef int(__stdcall* f_coreWeaponsData)(lua_State* L); typedef int(__stdcall* f_coreMissionData)(lua_State* L); -typedef int(__stdcall* f_coreInstancePath)(lua_State* L); f_coreInit coreInit = nullptr; f_coreDeinit coreDeinit = nullptr; f_coreFrame coreFrame = nullptr; f_coreUnitsData coreUnitsData = nullptr; f_coreWeaponsData coreWeaponsData = nullptr; f_coreMissionData coreMissionData = nullptr; -f_coreInstancePath coreInstancePath = nullptr; + +string modPath; static int onSimulationStart(lua_State* L) { log("onSimulationStart callback called successfully"); - string modLocation; - string dllLocation; - char* buf = nullptr; - size_t sz = 0; - if (_dupenv_s(&buf, &sz, "DCSOLYMPUS_PATH") == 0 && buf != nullptr) - { - modLocation = buf; - free(buf); - } - else - { - log("DCSOLYMPUS_PATH environment variable is missing"); - goto error; - } - dllLocation = modLocation + "\\bin\\core.dll"; + string dllLocation = modPath + "\\core.dll"; log("Loading core.dll"); hGetProcIDDLL = LoadLibrary(to_wstring(dllLocation).c_str()); @@ -92,14 +78,7 @@ static int onSimulationStart(lua_State* L) goto error; } - coreInstancePath = (f_coreInstancePath)GetProcAddress(hGetProcIDDLL, "coreInstancePath"); - if (!coreInstancePath) - { - LogError(L, "Error getting coreInstancePath ProcAddress from DLL"); - goto error; - } - - coreInit(L); + coreInit(L, modPath.c_str()); LogInfo(L, "Module loaded and started successfully."); @@ -146,7 +125,6 @@ static int onSimulationStop(lua_State* L) coreUnitsData = nullptr; coreWeaponsData = nullptr; coreMissionData = nullptr; - coreInstancePath = nullptr; } hGetProcIDDLL = NULL; @@ -185,15 +163,6 @@ static int setMissionData(lua_State* L) return 0; } -static int setInstancePath(lua_State* L) -{ - if (coreInstancePath) - { - coreInstancePath(L); - } - return 0; -} - static const luaL_Reg Map[] = { {"onSimulationStart", onSimulationStart}, {"onSimulationFrame", onSimulationFrame}, @@ -201,12 +170,39 @@ static const luaL_Reg Map[] = { {"setUnitsData", setUnitsData }, {"setWeaponsData", setWeaponsData }, {"setMissionData", setMissionData }, - {"setInstancePath", setInstancePath }, {NULL, NULL} }; extern "C" DllExport int luaopen_olympus(lua_State * L) { + lua_getglobal(L, "require"); + lua_pushstring(L, "lfs"); + lua_pcall(L, 1, 1, 0); + lua_getfield(L, -1, "writedir"); + lua_pcall(L, 0, 1, 0); + if (lua_isstring(L, -1)) { + modPath = string(lua_tostring(L, -1)) + "Mods\\Services\\Olympus\\bin\\"; + SetDllDirectoryA(modPath.c_str()); + LogInfo(L, "Instance location retrieved successfully"); + } + else { + /* Log without using the helper dlls because we have not loaded them yet here */ + lua_getglobal(L, "log"); + lua_getfield(L, -1, "ERROR"); + int errorLevel = (int)lua_tointeger(L, -1); + + lua_getglobal(L, "log"); + lua_getfield(L, -1, "write"); + lua_pushstring(L, "Olympus.dll"); + lua_pushnumber(L, errorLevel); + lua_pushstring(L, "An error has occurred while trying to retrieve Olympus's instance location"); + lua_pcall(L, 3, 0, 0); + + return 0; + } + + LogInfo(L, "Loading .dlls from " + modPath); + luaL_register(L, "olympus", Map); return 1; } \ No newline at end of file diff --git a/src/shared/include/defines.h b/src/shared/include/defines.h index bc2b6291..46dd9e26 100644 --- a/src/shared/include/defines.h +++ b/src/shared/include/defines.h @@ -14,8 +14,8 @@ #define FRAMERATE_TIME_INTERVAL 0.05 -#define OLYMPUS_JSON_PATH "\\olympus.json" -#define AIRCRAFT_DATABASE_PATH "\\client\\public\\databases\\units\\aircraftdatabase.json" -#define HELICOPTER_DATABASE_PATH "\\client\\public\\databases\\units\\helicopterdatabase.json" -#define GROUNDUNIT_DATABASE_PATH "\\client\\public\\databases\\units\\groundunitdatabase.json" -#define NAVYUNIT_DATABASE_PATH "\\client\\public\\databases\\units\\navyunitdatabase.json" +#define OLYMPUS_JSON_PATH "..\\olympus.json" +#define AIRCRAFT_DATABASE_PATH "..\\client\\public\\databases\\units\\aircraftdatabase.json" +#define HELICOPTER_DATABASE_PATH "..\\client\\public\\databases\\units\\helicopterdatabase.json" +#define GROUNDUNIT_DATABASE_PATH "..\\client\\public\\databases\\units\\groundunitdatabase.json" +#define NAVYUNIT_DATABASE_PATH "..\\client\\public\\databases\\units\\navyunitdatabase.json"