Skip to content

Commit

Permalink
Remove need for path variable
Browse files Browse the repository at this point in the history
  • Loading branch information
Pax1601 committed Dec 6, 2023
1 parent dcff462 commit fa06439
Show file tree
Hide file tree
Showing 10 changed files with 89 additions and 112 deletions.
25 changes: 0 additions & 25 deletions installer/olympus.iss
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
19 changes: 9 additions & 10 deletions scripts/OlympusCommand.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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
------------------------------------------------------------------------------------------------------
Expand All @@ -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')
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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()

6 changes: 3 additions & 3 deletions scripts/OlympusHook.lua
Original file line number Diff line number Diff line change
@@ -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')

Expand All @@ -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
Expand Down
20 changes: 5 additions & 15 deletions src/core/src/core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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<mutex> 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)
Expand Down
23 changes: 6 additions & 17 deletions src/core/src/scriptloader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

#include <algorithm>

extern string instancePath;

bool executeLuaScript(lua_State* L, string path)
{
replace(path.begin(), path.end(), '\\', '/');
Expand Down Expand Up @@ -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");
}
1 change: 1 addition & 0 deletions src/luatools/include/luatools.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
23 changes: 23 additions & 0 deletions src/luatools/src/luatools.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
4 changes: 4 additions & 0 deletions src/olympus/olympus.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
<ProjectReference Include="..\logger\logger.vcxproj">
<Project>{873ecabe-fcfe-4217-ac15-91959c3cf1c6}</Project>
</ProjectReference>
<ProjectReference Include="..\luatools\luatools.vcxproj">
<Project>{de139ec1-4f88-47d5-be73-f41915fe14a3}</Project>
</ProjectReference>
<ProjectReference Include="..\utils\utils.vcxproj">
<Project>{b85009ce-4a5c-4a5a-b85d-001b3a2651b2}</Project>
</ProjectReference>
Expand Down Expand Up @@ -112,6 +115,7 @@
<EnableUAC>false</EnableUAC>
<AdditionalDependencies>lua.lib</AdditionalDependencies>
<AdditionalLibraryDirectories>..\..\third-party\lua</AdditionalLibraryDirectories>
<DelayLoadDLLs>dcstools.dll;logger.dll;utils.dll;%(DelayLoadDLLs)</DelayLoadDLLs>
</Link>
</ItemDefinitionGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
Expand Down
70 changes: 33 additions & 37 deletions src/olympus/src/olympus.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down Expand Up @@ -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.");

Expand Down Expand Up @@ -146,7 +125,6 @@ static int onSimulationStop(lua_State* L)
coreUnitsData = nullptr;
coreWeaponsData = nullptr;
coreMissionData = nullptr;
coreInstancePath = nullptr;
}

hGetProcIDDLL = NULL;
Expand Down Expand Up @@ -185,28 +163,46 @@ 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},
{"onSimulationStop", onSimulationStop},
{"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;
}
10 changes: 5 additions & 5 deletions src/shared/include/defines.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"

0 comments on commit fa06439

Please sign in to comment.