From 93ca0e3f2212c82333c79503999ce2732ab1830a Mon Sep 17 00:00:00 2001 From: Pax1601 Date: Fri, 1 Dec 2023 17:44:03 +0100 Subject: [PATCH 1/3] Added read from lfs --- scripts/OlympusCommand.lua | 6 ++++++ src/core/src/core.cpp | 18 ++++++++++++++++++ src/core/src/server.cpp | 7 ++++++- src/olympus/src/olympus.cpp | 20 ++++++++++++++++++++ 4 files changed, 50 insertions(+), 1 deletion(-) diff --git a/scripts/OlympusCommand.lua b/scripts/OlympusCommand.lua index 3f186e90..b8daca05 100644 --- a/scripts/OlympusCommand.lua +++ b/scripts/OlympusCommand.lua @@ -1345,3 +1345,9 @@ 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.OlympusDLL.setInstancePath() + diff --git a/src/core/src/core.cpp b/src/core/src/core.cpp index 35822d46..76cebf8c 100644 --- a/src/core/src/core.cpp +++ b/src/core/src/core.cpp @@ -25,6 +25,7 @@ json::value missionData = json::value::object(); mutex mutexLock; string sessionHash; +string instancePath; bool initialized = false; @@ -153,3 +154,20 @@ extern "C" DllExport int coreMissionData(lua_State * L) return(0); } + +extern "C" DllExport int coreInstancePath(lua_State * L) +{ + if (!initialized) + return (0); + + /* 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); +} \ No newline at end of file diff --git a/src/core/src/server.cpp b/src/core/src/server.cpp index e3be2f2f..d3ff44d3 100644 --- a/src/core/src/server.cpp +++ b/src/core/src/server.cpp @@ -19,6 +19,7 @@ extern Scheduler* scheduler; extern json::value missionData; extern mutex mutexLock; extern string sessionHash; +extern string instancePath; void handle_eptr(std::exception_ptr eptr) { @@ -291,7 +292,11 @@ void Server::task() size_t sz = 0; if (_dupenv_s(&buf, &sz, "DCSOLYMPUS_PATH") == 0 && buf != nullptr) { - std::ifstream ifstream(string(buf) + OLYMPUS_JSON_PATH); + string jsonLocation = string(buf) + OLYMPUS_JSON_PATH; + if (instancePath != "") + jsonLocation = instancePath + OLYMPUS_JSON_PATH; + + std::ifstream ifstream(jsonLocation); std::stringstream ss; ss << ifstream.rdbuf(); std::error_code errorCode; diff --git a/src/olympus/src/olympus.cpp b/src/olympus/src/olympus.cpp index 601a3ebb..7ed63792 100644 --- a/src/olympus/src/olympus.cpp +++ b/src/olympus/src/olympus.cpp @@ -11,12 +11,14 @@ 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_coreSetInstancePath)(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_coreSetInstancePath coreInstancePath = nullptr; static int onSimulationStart(lua_State* L) { @@ -90,6 +92,13 @@ static int onSimulationStart(lua_State* L) goto error; } + coreInstancePath = (f_coreSetInstancePath)GetProcAddress(hGetProcIDDLL, "coreSetInstancePath"); + if (!coreInstancePath) + { + LogError(L, "Error getting coreSetInstancePath ProcAddress from DLL"); + goto error; + } + coreInit(L); LogInfo(L, "Module loaded and started successfully."); @@ -137,6 +146,7 @@ static int onSimulationStop(lua_State* L) coreUnitsData = nullptr; coreWeaponsData = nullptr; coreMissionData = nullptr; + coreInstancePath = nullptr; } hGetProcIDDLL = NULL; @@ -175,6 +185,15 @@ 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}, @@ -182,6 +201,7 @@ static const luaL_Reg Map[] = { {"setUnitsData", setUnitsData }, {"setWeaponsData", setWeaponsData }, {"setMissionData", setMissionData }, + {"setInstancePath", setInstancePath }, {NULL, NULL} }; From 423e799a82198db24b90e188b8c29d65d728dcac Mon Sep 17 00:00:00 2001 From: Pax1601 Date: Sat, 2 Dec 2023 10:40:03 +0100 Subject: [PATCH 2/3] Completed reading from current instance --- scripts/OlympusCommand.lua | 2 ++ src/core/src/aircraft.cpp | 26 +++++++----------- src/core/src/core.cpp | 31 ++++++++++------------ src/core/src/groundunit.cpp | 26 +++++++----------- src/core/src/helicopter.cpp | 26 +++++++----------- src/core/src/navyunit.cpp | 26 +++++++----------- src/core/src/server.cpp | 53 +++++++++++++++---------------------- src/olympus/src/olympus.cpp | 6 ++--- 8 files changed, 80 insertions(+), 116 deletions(-) diff --git a/scripts/OlympusCommand.lua b/scripts/OlympusCommand.lua index b8daca05..2037c261 100644 --- a/scripts/OlympusCommand.lua +++ b/scripts/OlympusCommand.lua @@ -1349,5 +1349,7 @@ Olympus.notify("OlympusCommand script " .. version .. " loaded successfully", 2, 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/src/core/src/aircraft.cpp b/src/core/src/aircraft.cpp index be3021f9..eb716c01 100644 --- a/src/core/src/aircraft.cpp +++ b/src/core/src/aircraft.cpp @@ -12,24 +12,18 @@ using namespace GeographicLib; extern Scheduler* scheduler; extern UnitsManager* unitsManager; json::value Aircraft::database = json::value(); +extern string instancePath; void Aircraft::loadDatabase(string path) { - char* buf = nullptr; - size_t sz = 0; - if (_dupenv_s(&buf, &sz, "DCSOLYMPUS_PATH") == 0 && buf != nullptr) - { - std::ifstream ifstream(string(buf) + path); - std::stringstream ss; - ss << ifstream.rdbuf(); - std::error_code errorCode; - database = json::value::parse(ss.str(), errorCode); - if (database.is_object()) - log("Aircrafts database loaded correctly"); - else - log("Error reading Aircrafts database file"); - - free(buf); - } + std::ifstream ifstream(instancePath + path); + std::stringstream ss; + ss << ifstream.rdbuf(); + std::error_code errorCode; + database = json::value::parse(ss.str(), errorCode); + if (database.is_object()) + log("Aircrafts database loaded correctly from " + instancePath + path); + else + log("Error reading Aircrafts database file"); } /* Aircraft */ diff --git a/src/core/src/core.cpp b/src/core/src/core.cpp index 76cebf8c..d59432ce 100644 --- a/src/core/src/core.cpp +++ b/src/core/src/core.cpp @@ -70,6 +70,20 @@ 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) @@ -154,20 +168,3 @@ extern "C" DllExport int coreMissionData(lua_State * L) return(0); } - -extern "C" DllExport int coreInstancePath(lua_State * L) -{ - if (!initialized) - return (0); - - /* 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); -} \ No newline at end of file diff --git a/src/core/src/groundunit.cpp b/src/core/src/groundunit.cpp index 56ba31d9..81891d5d 100644 --- a/src/core/src/groundunit.cpp +++ b/src/core/src/groundunit.cpp @@ -12,27 +12,21 @@ using namespace GeographicLib; extern Scheduler* scheduler; extern UnitsManager* unitsManager; json::value GroundUnit::database = json::value(); +extern string instancePath; #define RANDOM_ZERO_TO_ONE (double)(rand()) / (double)(RAND_MAX) #define RANDOM_MINUS_ONE_TO_ONE (((double)(rand()) / (double)(RAND_MAX) - 0.5) * 2) void GroundUnit::loadDatabase(string path) { - char* buf = nullptr; - size_t sz = 0; - if (_dupenv_s(&buf, &sz, "DCSOLYMPUS_PATH") == 0 && buf != nullptr) - { - std::ifstream ifstream(string(buf) + path); - std::stringstream ss; - ss << ifstream.rdbuf(); - std::error_code errorCode; - database = json::value::parse(ss.str(), errorCode); - if (database.is_object()) - log("Ground Units database loaded correctly"); - else - log("Error reading Ground Units database file"); - - free(buf); - } + std::ifstream ifstream(instancePath + path); + std::stringstream ss; + ss << ifstream.rdbuf(); + std::error_code errorCode; + database = json::value::parse(ss.str(), errorCode); + if (database.is_object()) + log("GroundUnits database loaded correctly from " + instancePath + path); + else + log("Error reading GroundUnits database file"); } /* Ground unit */ diff --git a/src/core/src/helicopter.cpp b/src/core/src/helicopter.cpp index c452f118..f12a5d3e 100644 --- a/src/core/src/helicopter.cpp +++ b/src/core/src/helicopter.cpp @@ -12,24 +12,18 @@ using namespace GeographicLib; extern Scheduler* scheduler; extern UnitsManager* unitsManager; json::value Helicopter::database = json::value(); +extern string instancePath; void Helicopter::loadDatabase(string path) { - char* buf = nullptr; - size_t sz = 0; - if (_dupenv_s(&buf, &sz, "DCSOLYMPUS_PATH") == 0 && buf != nullptr) - { - std::ifstream ifstream(string(buf) + path); - std::stringstream ss; - ss << ifstream.rdbuf(); - std::error_code errorCode; - database = json::value::parse(ss.str(), errorCode); - if (database.is_object()) - log("Helicopters database loaded correctly"); - else - log("Error reading Helicopters database file"); - - free(buf); - } + std::ifstream ifstream(instancePath + path); + std::stringstream ss; + ss << ifstream.rdbuf(); + std::error_code errorCode; + database = json::value::parse(ss.str(), errorCode); + if (database.is_object()) + log("Helicopters database loaded correctly from " + instancePath + path); + else + log("Error reading Helicopters database file"); } /* Helicopter */ diff --git a/src/core/src/navyunit.cpp b/src/core/src/navyunit.cpp index b965d423..dd292be8 100644 --- a/src/core/src/navyunit.cpp +++ b/src/core/src/navyunit.cpp @@ -12,24 +12,18 @@ using namespace GeographicLib; extern Scheduler* scheduler; extern UnitsManager* unitsManager; json::value NavyUnit::database = json::value(); +extern string instancePath; void NavyUnit::loadDatabase(string path) { - char* buf = nullptr; - size_t sz = 0; - if (_dupenv_s(&buf, &sz, "DCSOLYMPUS_PATH") == 0 && buf != nullptr) - { - std::ifstream ifstream(string(buf) + path); - std::stringstream ss; - ss << ifstream.rdbuf(); - std::error_code errorCode; - database = json::value::parse(ss.str(), errorCode); - if (database.is_object()) - log("Navy Units database loaded correctly"); - else - log("Error reading Navy Units database file"); - - free(buf); - } + std::ifstream ifstream(instancePath + path); + std::stringstream ss; + ss << ifstream.rdbuf(); + std::error_code errorCode; + database = json::value::parse(ss.str(), errorCode); + if (database.is_object()) + log("NavyUnits database loaded correctly from " + instancePath + path); + else + log("Error reading NavyUnits database file"); } /* Navy Unit */ diff --git a/src/core/src/server.cpp b/src/core/src/server.cpp index d3ff44d3..b810db13 100644 --- a/src/core/src/server.cpp +++ b/src/core/src/server.cpp @@ -287,43 +287,32 @@ string Server::extractPassword(http_request& request) { void Server::task() { string address = REST_ADDRESS; - string modLocation; - char* buf = nullptr; - size_t sz = 0; - if (_dupenv_s(&buf, &sz, "DCSOLYMPUS_PATH") == 0 && buf != nullptr) + string jsonLocation = instancePath + OLYMPUS_JSON_PATH; + + log("Reading configuration from " + jsonLocation); + + std::ifstream ifstream(jsonLocation); + std::stringstream ss; + ss << ifstream.rdbuf(); + std::error_code errorCode; + json::value config = json::value::parse(ss.str(), errorCode); + if (config.is_object() && config.has_object_field(L"server") && + config[L"server"].has_string_field(L"address") && config[L"server"].has_number_field(L"port")) { - string jsonLocation = string(buf) + OLYMPUS_JSON_PATH; - if (instancePath != "") - jsonLocation = instancePath + OLYMPUS_JSON_PATH; - - std::ifstream ifstream(jsonLocation); - std::stringstream ss; - ss << ifstream.rdbuf(); - std::error_code errorCode; - json::value config = json::value::parse(ss.str(), errorCode); - if (config.is_object() && config.has_object_field(L"server") && - config[L"server"].has_string_field(L"address") && config[L"server"].has_number_field(L"port")) - { - address = "http://" + to_string(config[L"server"][L"address"]) + ":" + to_string(config[L"server"][L"port"].as_number().to_int32()); - log("Starting server on " + address); - } - else - log("Error reading configuration file. Starting server on " + address); - - if (config.is_object() && config.has_object_field(L"authentication")) - { - if (config[L"authentication"].has_string_field(L"gameMasterPassword")) gameMasterPassword = to_string(config[L"authentication"][L"gameMasterPassword"]); - if (config[L"authentication"].has_string_field(L"blueCommanderPassword")) blueCommanderPassword = to_string(config[L"authentication"][L"blueCommanderPassword"]); - if (config[L"authentication"].has_string_field(L"redCommanderPassword")) redCommanderPassword = to_string(config[L"authentication"][L"redCommanderPassword"]); - } - else - log("Error reading configuration file. No password set."); - free(buf); + address = "http://" + to_string(config[L"server"][L"address"]) + ":" + to_string(config[L"server"][L"port"].as_number().to_int32()); + log("Starting server on " + address); } else + log("Error reading configuration file. Starting server on " + address); + + if (config.is_object() && config.has_object_field(L"authentication")) { - log("DCSOLYMPUS_PATH environment variable is missing, starting server on " + address); + if (config[L"authentication"].has_string_field(L"gameMasterPassword")) gameMasterPassword = to_string(config[L"authentication"][L"gameMasterPassword"]); + if (config[L"authentication"].has_string_field(L"blueCommanderPassword")) blueCommanderPassword = to_string(config[L"authentication"][L"blueCommanderPassword"]); + if (config[L"authentication"].has_string_field(L"redCommanderPassword")) redCommanderPassword = to_string(config[L"authentication"][L"redCommanderPassword"]); } + else + log("Error reading configuration file. No password set."); http_listener listener(to_wstring(address + "/" + REST_URI)); diff --git a/src/olympus/src/olympus.cpp b/src/olympus/src/olympus.cpp index 7ed63792..cb6910fc 100644 --- a/src/olympus/src/olympus.cpp +++ b/src/olympus/src/olympus.cpp @@ -11,14 +11,14 @@ 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_coreSetInstancePath)(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_coreSetInstancePath coreInstancePath = nullptr; +f_coreInstancePath coreInstancePath = nullptr; static int onSimulationStart(lua_State* L) { @@ -92,7 +92,7 @@ static int onSimulationStart(lua_State* L) goto error; } - coreInstancePath = (f_coreSetInstancePath)GetProcAddress(hGetProcIDDLL, "coreSetInstancePath"); + coreInstancePath = (f_coreInstancePath)GetProcAddress(hGetProcIDDLL, "coreInstancePath"); if (!coreInstancePath) { LogError(L, "Error getting coreSetInstancePath ProcAddress from DLL"); From 563f673fb3581359bfee8b759a4e12a9ea9ff392 Mon Sep 17 00:00:00 2001 From: Pax1601 Date: Sat, 2 Dec 2023 10:46:59 +0100 Subject: [PATCH 3/3] Minor tweak --- src/olympus/src/olympus.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/olympus/src/olympus.cpp b/src/olympus/src/olympus.cpp index cb6910fc..2417b054 100644 --- a/src/olympus/src/olympus.cpp +++ b/src/olympus/src/olympus.cpp @@ -95,7 +95,7 @@ static int onSimulationStart(lua_State* L) coreInstancePath = (f_coreInstancePath)GetProcAddress(hGetProcIDDLL, "coreInstancePath"); if (!coreInstancePath) { - LogError(L, "Error getting coreSetInstancePath ProcAddress from DLL"); + LogError(L, "Error getting coreInstancePath ProcAddress from DLL"); goto error; }