Started air fighter AI

This commit is contained in:
Pax1601
2022-12-21 19:18:20 +01:00
parent 3a46a6df21
commit de055c5a98
1766 changed files with 137925 additions and 501 deletions

View File

@@ -19,58 +19,110 @@ Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|x64 = Debug|x64
Debug|x86 = Debug|x86
MinSizeRel|x64 = MinSizeRel|x64
MinSizeRel|x86 = MinSizeRel|x86
Release|x64 = Release|x64
Release|x86 = Release|x86
RelWithDebInfo|x64 = RelWithDebInfo|x64
RelWithDebInfo|x86 = RelWithDebInfo|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{8A48D855-0E01-42BA-BD8C-07B0877C68DF}.Debug|x64.ActiveCfg = Debug|x64
{8A48D855-0E01-42BA-BD8C-07B0877C68DF}.Debug|x64.Build.0 = Debug|x64
{8A48D855-0E01-42BA-BD8C-07B0877C68DF}.Debug|x86.ActiveCfg = Debug|Win32
{8A48D855-0E01-42BA-BD8C-07B0877C68DF}.Debug|x86.Build.0 = Debug|Win32
{8A48D855-0E01-42BA-BD8C-07B0877C68DF}.MinSizeRel|x64.ActiveCfg = Release|x64
{8A48D855-0E01-42BA-BD8C-07B0877C68DF}.MinSizeRel|x64.Build.0 = Release|x64
{8A48D855-0E01-42BA-BD8C-07B0877C68DF}.MinSizeRel|x86.ActiveCfg = Release|Win32
{8A48D855-0E01-42BA-BD8C-07B0877C68DF}.MinSizeRel|x86.Build.0 = Release|Win32
{8A48D855-0E01-42BA-BD8C-07B0877C68DF}.Release|x64.ActiveCfg = Release|x64
{8A48D855-0E01-42BA-BD8C-07B0877C68DF}.Release|x64.Build.0 = Release|x64
{8A48D855-0E01-42BA-BD8C-07B0877C68DF}.Release|x86.ActiveCfg = Release|Win32
{8A48D855-0E01-42BA-BD8C-07B0877C68DF}.Release|x86.Build.0 = Release|Win32
{8A48D855-0E01-42BA-BD8C-07B0877C68DF}.RelWithDebInfo|x64.ActiveCfg = Release|x64
{8A48D855-0E01-42BA-BD8C-07B0877C68DF}.RelWithDebInfo|x64.Build.0 = Release|x64
{8A48D855-0E01-42BA-BD8C-07B0877C68DF}.RelWithDebInfo|x86.ActiveCfg = Release|Win32
{8A48D855-0E01-42BA-BD8C-07B0877C68DF}.RelWithDebInfo|x86.Build.0 = Release|Win32
{873ECABE-FCFE-4217-AC15-91959C3CF1C6}.Debug|x64.ActiveCfg = Debug|x64
{873ECABE-FCFE-4217-AC15-91959C3CF1C6}.Debug|x64.Build.0 = Debug|x64
{873ECABE-FCFE-4217-AC15-91959C3CF1C6}.Debug|x86.ActiveCfg = Debug|Win32
{873ECABE-FCFE-4217-AC15-91959C3CF1C6}.Debug|x86.Build.0 = Debug|Win32
{873ECABE-FCFE-4217-AC15-91959C3CF1C6}.MinSizeRel|x64.ActiveCfg = Release|x64
{873ECABE-FCFE-4217-AC15-91959C3CF1C6}.MinSizeRel|x64.Build.0 = Release|x64
{873ECABE-FCFE-4217-AC15-91959C3CF1C6}.MinSizeRel|x86.ActiveCfg = Release|Win32
{873ECABE-FCFE-4217-AC15-91959C3CF1C6}.MinSizeRel|x86.Build.0 = Release|Win32
{873ECABE-FCFE-4217-AC15-91959C3CF1C6}.Release|x64.ActiveCfg = Release|x64
{873ECABE-FCFE-4217-AC15-91959C3CF1C6}.Release|x64.Build.0 = Release|x64
{873ECABE-FCFE-4217-AC15-91959C3CF1C6}.Release|x86.ActiveCfg = Release|Win32
{873ECABE-FCFE-4217-AC15-91959C3CF1C6}.Release|x86.Build.0 = Release|Win32
{873ECABE-FCFE-4217-AC15-91959C3CF1C6}.RelWithDebInfo|x64.ActiveCfg = Release|x64
{873ECABE-FCFE-4217-AC15-91959C3CF1C6}.RelWithDebInfo|x64.Build.0 = Release|x64
{873ECABE-FCFE-4217-AC15-91959C3CF1C6}.RelWithDebInfo|x86.ActiveCfg = Release|Win32
{873ECABE-FCFE-4217-AC15-91959C3CF1C6}.RelWithDebInfo|x86.Build.0 = Release|Win32
{2B255368-39A0-431A-A6DE-CC739AC70DC1}.Debug|x64.ActiveCfg = Debug|x64
{2B255368-39A0-431A-A6DE-CC739AC70DC1}.Debug|x64.Build.0 = Debug|x64
{2B255368-39A0-431A-A6DE-CC739AC70DC1}.Debug|x86.ActiveCfg = Debug|Win32
{2B255368-39A0-431A-A6DE-CC739AC70DC1}.Debug|x86.Build.0 = Debug|Win32
{2B255368-39A0-431A-A6DE-CC739AC70DC1}.MinSizeRel|x64.ActiveCfg = Release|x64
{2B255368-39A0-431A-A6DE-CC739AC70DC1}.MinSizeRel|x64.Build.0 = Release|x64
{2B255368-39A0-431A-A6DE-CC739AC70DC1}.MinSizeRel|x86.ActiveCfg = Release|Win32
{2B255368-39A0-431A-A6DE-CC739AC70DC1}.MinSizeRel|x86.Build.0 = Release|Win32
{2B255368-39A0-431A-A6DE-CC739AC70DC1}.Release|x64.ActiveCfg = Release|x64
{2B255368-39A0-431A-A6DE-CC739AC70DC1}.Release|x64.Build.0 = Release|x64
{2B255368-39A0-431A-A6DE-CC739AC70DC1}.Release|x86.ActiveCfg = Release|Win32
{2B255368-39A0-431A-A6DE-CC739AC70DC1}.Release|x86.Build.0 = Release|Win32
{2B255368-39A0-431A-A6DE-CC739AC70DC1}.RelWithDebInfo|x64.ActiveCfg = Release|x64
{2B255368-39A0-431A-A6DE-CC739AC70DC1}.RelWithDebInfo|x64.Build.0 = Release|x64
{2B255368-39A0-431A-A6DE-CC739AC70DC1}.RelWithDebInfo|x86.ActiveCfg = Release|Win32
{2B255368-39A0-431A-A6DE-CC739AC70DC1}.RelWithDebInfo|x86.Build.0 = Release|Win32
{DE139EC1-4F88-47D5-BE73-F41915FE14A3}.Debug|x64.ActiveCfg = Debug|x64
{DE139EC1-4F88-47D5-BE73-F41915FE14A3}.Debug|x64.Build.0 = Debug|x64
{DE139EC1-4F88-47D5-BE73-F41915FE14A3}.Debug|x86.ActiveCfg = Debug|Win32
{DE139EC1-4F88-47D5-BE73-F41915FE14A3}.Debug|x86.Build.0 = Debug|Win32
{DE139EC1-4F88-47D5-BE73-F41915FE14A3}.MinSizeRel|x64.ActiveCfg = Release|x64
{DE139EC1-4F88-47D5-BE73-F41915FE14A3}.MinSizeRel|x64.Build.0 = Release|x64
{DE139EC1-4F88-47D5-BE73-F41915FE14A3}.MinSizeRel|x86.ActiveCfg = Release|Win32
{DE139EC1-4F88-47D5-BE73-F41915FE14A3}.MinSizeRel|x86.Build.0 = Release|Win32
{DE139EC1-4F88-47D5-BE73-F41915FE14A3}.Release|x64.ActiveCfg = Release|x64
{DE139EC1-4F88-47D5-BE73-F41915FE14A3}.Release|x64.Build.0 = Release|x64
{DE139EC1-4F88-47D5-BE73-F41915FE14A3}.Release|x86.ActiveCfg = Release|Win32
{DE139EC1-4F88-47D5-BE73-F41915FE14A3}.Release|x86.Build.0 = Release|Win32
{DE139EC1-4F88-47D5-BE73-F41915FE14A3}.RelWithDebInfo|x64.ActiveCfg = Release|x64
{DE139EC1-4F88-47D5-BE73-F41915FE14A3}.RelWithDebInfo|x64.Build.0 = Release|x64
{DE139EC1-4F88-47D5-BE73-F41915FE14A3}.RelWithDebInfo|x86.ActiveCfg = Release|Win32
{DE139EC1-4F88-47D5-BE73-F41915FE14A3}.RelWithDebInfo|x86.Build.0 = Release|Win32
{B85009CE-4A5C-4A5A-B85D-001B3A2651B2}.Debug|x64.ActiveCfg = Debug|x64
{B85009CE-4A5C-4A5A-B85D-001B3A2651B2}.Debug|x64.Build.0 = Debug|x64
{B85009CE-4A5C-4A5A-B85D-001B3A2651B2}.Debug|x86.ActiveCfg = Debug|Win32
{B85009CE-4A5C-4A5A-B85D-001B3A2651B2}.Debug|x86.Build.0 = Debug|Win32
{B85009CE-4A5C-4A5A-B85D-001B3A2651B2}.MinSizeRel|x64.ActiveCfg = Release|x64
{B85009CE-4A5C-4A5A-B85D-001B3A2651B2}.MinSizeRel|x64.Build.0 = Release|x64
{B85009CE-4A5C-4A5A-B85D-001B3A2651B2}.MinSizeRel|x86.ActiveCfg = Release|Win32
{B85009CE-4A5C-4A5A-B85D-001B3A2651B2}.MinSizeRel|x86.Build.0 = Release|Win32
{B85009CE-4A5C-4A5A-B85D-001B3A2651B2}.Release|x64.ActiveCfg = Release|x64
{B85009CE-4A5C-4A5A-B85D-001B3A2651B2}.Release|x64.Build.0 = Release|x64
{B85009CE-4A5C-4A5A-B85D-001B3A2651B2}.Release|x86.ActiveCfg = Release|Win32
{B85009CE-4A5C-4A5A-B85D-001B3A2651B2}.Release|x86.Build.0 = Release|Win32
{B85009CE-4A5C-4A5A-B85D-001B3A2651B2}.RelWithDebInfo|x64.ActiveCfg = Release|x64
{B85009CE-4A5C-4A5A-B85D-001B3A2651B2}.RelWithDebInfo|x64.Build.0 = Release|x64
{B85009CE-4A5C-4A5A-B85D-001B3A2651B2}.RelWithDebInfo|x86.ActiveCfg = Release|Win32
{B85009CE-4A5C-4A5A-B85D-001B3A2651B2}.RelWithDebInfo|x86.Build.0 = Release|Win32
{5F3FC91E-1FBC-4223-8011-9708DE913474}.Debug|x64.ActiveCfg = Debug|x64
{5F3FC91E-1FBC-4223-8011-9708DE913474}.Debug|x64.Build.0 = Debug|x64
{5F3FC91E-1FBC-4223-8011-9708DE913474}.Debug|x86.ActiveCfg = Debug|x64
{5F3FC91E-1FBC-4223-8011-9708DE913474}.Debug|x86.Build.0 = Debug|x64
{5F3FC91E-1FBC-4223-8011-9708DE913474}.MinSizeRel|x64.ActiveCfg = Release|x64
{5F3FC91E-1FBC-4223-8011-9708DE913474}.MinSizeRel|x64.Build.0 = Release|x64
{5F3FC91E-1FBC-4223-8011-9708DE913474}.MinSizeRel|x86.ActiveCfg = Release|x64
{5F3FC91E-1FBC-4223-8011-9708DE913474}.MinSizeRel|x86.Build.0 = Release|x64
{5F3FC91E-1FBC-4223-8011-9708DE913474}.Release|x64.ActiveCfg = Release|x64
{5F3FC91E-1FBC-4223-8011-9708DE913474}.Release|x64.Build.0 = Release|x64
{5F3FC91E-1FBC-4223-8011-9708DE913474}.Release|x86.ActiveCfg = Release|x64
{5F3FC91E-1FBC-4223-8011-9708DE913474}.Release|x86.Build.0 = Release|x64
{5F3FC91E-1FBC-4223-8011-9708DE913474}.RelWithDebInfo|x64.ActiveCfg = Release|x64
{5F3FC91E-1FBC-4223-8011-9708DE913474}.RelWithDebInfo|x64.Build.0 = Release|x64
{5F3FC91E-1FBC-4223-8011-9708DE913474}.RelWithDebInfo|x86.ActiveCfg = Release|x64
{5F3FC91E-1FBC-4223-8011-9708DE913474}.RelWithDebInfo|x86.Build.0 = Release|x64
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE

152
src/Olympus/src/olympus.cpp Normal file
View File

@@ -0,0 +1,152 @@
#include "framework.h"
#include "dcstools.h"
#include "logger.h"
#include "utils.h"
/* 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_coreDeinit)(lua_State* L);
typedef int(__stdcall* f_coreFrame)(lua_State* L);
typedef int(__stdcall* f_coreMissionData)(lua_State* L);
f_coreInit coreInit = nullptr;
f_coreDeinit coreDeinit = nullptr;
f_coreFrame coreFrame = nullptr;
f_coreMissionData coreMissionData = nullptr;
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";
log("Loading core.dll");
hGetProcIDDLL = LoadLibrary(to_wstring(dllLocation).c_str());
if (!hGetProcIDDLL) {
LogError(L, "Error loading core DLL");
goto error;
}
log("Core DLL loaded successfully");
coreInit = (f_coreInit)GetProcAddress(hGetProcIDDLL, "coreInit");
if (!coreInit)
{
LogError(L, "Error getting coreInit ProcAddress from DLL");
goto error;
}
coreDeinit = (f_coreDeinit)GetProcAddress(hGetProcIDDLL, "coreDeinit");
if (!coreInit)
{
LogError(L, "Error getting coreDeinit ProcAddress from DLL");
goto error;
}
coreFrame = (f_coreFrame)GetProcAddress(hGetProcIDDLL, "coreFrame");
if (!coreFrame)
{
LogError(L, "Error getting coreFrame ProcAddress from DLL");
goto error;
}
coreMissionData = (f_coreFrame)GetProcAddress(hGetProcIDDLL, "coreMissionData");
if (!coreFrame)
{
LogError(L, "Error getting coreMissionData ProcAddress from DLL");
goto error;
}
coreInit(L);
LogInfo(L, "Module loaded and started successfully.");
return 0;
error:
LogError(L, "Error while loading module, see Olympus.log in temporary folder for additional details.");
return 0;
}
static int onSimulationFrame(lua_State* L)
{
if (coreFrame)
{
coreFrame(L);
}
return 0;
}
static int onSimulationStop(lua_State* L)
{
log("onSimulationStop callback called successfully");
if (hGetProcIDDLL)
{
log("Trying to unload core DLL");
if (coreDeinit)
{
coreDeinit(L);
}
if (FreeLibrary(hGetProcIDDLL))
{
log("Core DLL unloaded successfully");
}
else
{
LogError(L, "Error unloading DLL");
goto error;
}
coreInit = nullptr;
coreDeinit = nullptr;
coreFrame = nullptr;
coreMissionData = nullptr;
}
hGetProcIDDLL = NULL;
return 0;
error:
LogError(L, "Error while unloading module, see Olympus.log in temporary folder for additional details.");
return 0;
}
static int setMissionData(lua_State* L)
{
if (coreMissionData)
{
coreMissionData(L);
}
return 0;
}
static const luaL_Reg Map[] = {
{"onSimulationStart", onSimulationStart},
{"onSimulationFrame", onSimulationFrame},
{"onSimulationStop", onSimulationStop},
{"setMissionData", setMissionData },
{NULL, NULL}
};
extern "C" DllExport int luaopen_olympus(lua_State * L)
{
luaL_register(L, "olympus", Map);
return 1;
}

View File

@@ -171,7 +171,7 @@
<PrecompiledHeader>NotUsing</PrecompiledHeader>
<PrecompiledHeaderFile>
</PrecompiledHeaderFile>
<AdditionalIncludeDirectories>include;..\..\third-party\lua\include;..\utils\include;..\shared\include;..\dcstools\include;..\logger\include;..\luatools\include</AdditionalIncludeDirectories>
<AdditionalIncludeDirectories>include;..\..\third-party\GeographicLib-2.1.1\include;..\..\third-party\lua\include;..\utils\include;..\shared\include;..\dcstools\include;..\logger\include;..\luatools\include</AdditionalIncludeDirectories>
<LanguageStandard>stdcpp20</LanguageStandard>
</ClCompile>
<Link>
@@ -180,8 +180,8 @@
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation>
<EnableUAC>false</EnableUAC>
<AdditionalDependencies>lua.lib;%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalLibraryDirectories>..\..\third-party\lua</AdditionalLibraryDirectories>
<AdditionalDependencies>lua.lib; GeographicLib-i.lib;%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalLibraryDirectories>..\..\third-party\lua; ..\..\third-party\GeographicLib-2.1.1\lib</AdditionalLibraryDirectories>
</Link>
</ItemDefinitionGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />

View File

@@ -8,7 +8,7 @@ namespace CommandPriority {
};
namespace CommandType {
enum CommandTypes { NO_TYPE, MOVE, SMOKE, LASE, EXPLODE, SPAWN_AIR, SPAWN_GROUND };
enum CommandTypes { NO_TYPE, MOVE, SMOKE, LASE, EXPLODE, SPAWN_AIR, SPAWN_GROUND, ATTACK_UNIT };
};
/* Base command class */
@@ -102,4 +102,24 @@ private:
const wstring coalition;
const wstring unitType;
const Coords location;
};
/* Attack unit command */
class AttackUnitCommand : public Command
{
public:
AttackUnitCommand(wstring unitName, wstring targetName, Coords location) :
unitName(unitName),
targetName(targetName),
location(location)
{
priority = CommandPriority::MEDIUM;
type = CommandType::ATTACK_UNIT;
};
virtual void execute(lua_State* L);
private:
const wstring unitName;
const wstring targetName;
const Coords location;
};

View File

@@ -15,6 +15,6 @@ public:
private:
list<Command*> commands;
mutex lock;
mutex mutexLock;
};

View File

@@ -4,6 +4,9 @@
#include "dcstools.h"
#include "luatools.h"
#define GROUND_DEST_DIST_THR 100
#define AIR_DEST_DIST_THR 2000
namespace UnitCategory {
enum UnitCategories { NO_CATEGORY, AIR, GROUND, NAVY }; // Do not edit, this codes are tied to values in DCS
};
@@ -30,12 +33,15 @@ public:
double getLongitude() { return longitude; }
double getAltitude() { return altitude; }
double getHeading() { return heading; }
json::value getFlags() { return flags; }
int getCategory();
Coords getActiveDestination() { return activeDestination; }
json::value json();
protected:
int ID;
bool AI = false;
bool alive = true;
wstring name = L"undefined";
wstring unitName = L"undefined";
@@ -47,11 +53,15 @@ protected:
double longitude = 0;
double altitude = 0;
double heading = 0;
json::value flags = json::value::null();
list<Coords> activePath;
Coords activeDestination;
Coords activeDestination = Coords(0);
private:
virtual void AIloop();
double oldDist = 0;
mutex mutexLock;
};

View File

@@ -0,0 +1,4 @@
#pragma once
#include "framework.h"
void registerLuaFunctions(lua_State* L);

29
src/core/include/server.h Normal file
View File

@@ -0,0 +1,29 @@
#pragma once
#include "framework.h"
#include "luatools.h"
using namespace web::http;
using namespace web::http::experimental::listener;
class UnitsFactory;
class Scheduler;
class Server
{
public:
Server(lua_State* L);
~Server();
private:
std::thread* serverThread;
void handle_options(http_request request);
void handle_get(http_request request);
void handle_request(http_request request, function<void(json::value const&, json::value&)> action);
void handle_put(http_request request);
void task();
atomic<bool> runListener;
};

View File

@@ -0,0 +1,23 @@
#pragma once
#include "framework.h"
#include "dcstools.h"
class Unit;
class UnitsFactory
{
public:
UnitsFactory(lua_State* L);
~UnitsFactory();
Unit* getUnit(int ID);
void getMissionDB(lua_State* L);
void update(lua_State* L);
void updateAnswer(json::value& answer);
private:
map<int, Unit*> units;
json::value missionDB;
};

View File

@@ -77,10 +77,31 @@ void SpawnAirCommand::execute(lua_State* L)
lua_pushstring(L, command.str().c_str());
if (lua_pcall(L, 2, 0, 0) != 0)
{
log("Error executing SpawnGroundCommand");
log("Error executing SpawnAirCommand");
}
else
{
log("SpawnAirCommand executed successfully");
}
}
/* Attack unit command */
void AttackUnitCommand::execute(lua_State* L)
{
std::ostringstream command;
command.precision(10);
command << "Olympus.attackUnit(\"" << to_string(unitName) << "\", \"" << to_string(targetName) << "\", " << location.lat << ", " << location.lng << ")";
lua_getglobal(L, "net");
lua_getfield(L, -1, "dostring_in");
lua_pushstring(L, "server");
lua_pushstring(L, command.str().c_str());
if (lua_pcall(L, 2, 0, 0) != 0)
{
log("Error executing AttackUnitCommand");
}
else
{
log("AttackUnitCommand executed successfully");
}
}

View File

@@ -24,7 +24,8 @@ void Scheduler::appendCommand(Command* command)
void Scheduler::execute(lua_State* L)
{
lock.lock();
/* Lock for thread safety */
lock_guard<mutex> guard(mutexLock);
int priority = CommandPriority::HIGH;
while (priority >= CommandPriority::LOW)
{
@@ -63,26 +64,29 @@ void Scheduler::execute(lua_State* L)
commands.remove(command);
break;
}
case CommandType::ATTACK_UNIT:
{
AttackUnitCommand* attackCommand = dynamic_cast<AttackUnitCommand*>(command);
attackCommand->execute(L);
commands.remove(command);
break;
}
default:
log("Unknown command of type " + to_string(command->getType()));
commands.remove(command);
break;
}
goto exit;
return;
}
}
priority--;
}
exit:
lock.unlock();
return;
}
void Scheduler::handleRequest(wstring key, json::value value)
{
lock.lock();
/* Lock for thread safety */
lock_guard<mutex> guard(mutexLock);
Command* command = nullptr;
log(L"Received request with ID: " + key);
@@ -141,6 +145,44 @@ void Scheduler::handleRequest(wstring key, json::value value)
Coords loc; loc.lat = lat; loc.lng = lng;
command = dynamic_cast<Command*>(new SpawnAirCommand(coalition, type, loc));
}
else if (key.compare(L"attackUnit") == 0)
{
int unitID = value[L"unitID"].as_integer();
int targetID = value[L"targetID"].as_integer();
Unit* unit = unitsFactory->getUnit(unitID);
Unit* target = unitsFactory->getUnit(targetID);
wstring unitName;
wstring targetName;
Coords loc;
if (unit != nullptr)
{
unitName = unit->getUnitName();
loc = unit->getActiveDestination();
}
else
{
return;
}
if (target != nullptr)
{
targetName = target->getUnitName();
if (loc == NULL)
{
loc = Coords(target->getLatitude(), target->getLongitude(), target->getAltitude());
}
}
else
{
return;
}
log(L"Unit " + unitName + L" attacking unit " + targetName);
command = dynamic_cast<Command*>(new AttackUnitCommand(unitName, targetName, loc));
}
else
{
log(L"Unknown command: " + key);
@@ -150,6 +192,5 @@ void Scheduler::handleRequest(wstring key, json::value value)
{
appendCommand(command);
}
lock.unlock();
}

View File

@@ -4,8 +4,13 @@
#include "commands.h"
#include "scheduler.h"
#include <GeographicLib/Geodesic.hpp>
using namespace GeographicLib;
extern Scheduler* scheduler;
const Geodesic& geod = Geodesic::WGS84();
Unit::Unit(json::value json, int ID) :
ID(ID)
{
@@ -22,7 +27,7 @@ int Unit::getCategory()
{
if (type.has_number_field(L"level1"))
{
return type[L"level1"].as_number().is_int32();
return type[L"level1"].as_number().to_int32();
}
else
{
@@ -32,19 +37,41 @@ int Unit::getCategory()
void Unit::update(json::value json)
{
name = json[L"Name"].as_string();
unitName = json[L"UnitName"].as_string();
groupName = json[L"GroupName"].as_string();
type = json[L"Type"];
country = json[L"Country"].as_number().to_int32();
coalitionID = json[L"CoalitionID"].as_number().to_int32();
latitude = json[L"LatLongAlt"][L"Lat"].as_number().to_double();
longitude = json[L"LatLongAlt"][L"Long"].as_number().to_double();
altitude = json[L"LatLongAlt"][L"Alt"].as_number().to_double();
heading = json[L"Heading"].as_number().to_double();
/* Lock for thread safety */
lock_guard<mutex> guard(mutexLock);
/* If the unit is alive, run the AI Loop that performs the requested commands and instructions (moving, attacking, etc) */
if (alive)
if (json.has_string_field(L"Name"))
name = json[L"Name"].as_string();
if (json.has_string_field(L"UnitName"))
unitName = json[L"UnitName"].as_string();
if (json.has_string_field(L"GroupName"))
groupName = json[L"GroupName"].as_string();
if (json.has_object_field(L"Type"))
type = json[L"Type"];
if (json.has_number_field(L"Country"))
country = json[L"Country"].as_number().to_int32();
if (json.has_number_field(L"CoalitionID"))
coalitionID = json[L"CoalitionID"].as_number().to_int32();
if (json.has_object_field(L"LatLongAlt"))
{
latitude = json[L"LatLongAlt"][L"Lat"].as_number().to_double();
longitude = json[L"LatLongAlt"][L"Long"].as_number().to_double();
altitude = json[L"LatLongAlt"][L"Alt"].as_number().to_double();
}
if (json.has_number_field(L"Heading"))
heading = json[L"Heading"].as_number().to_double();
if (json.has_object_field(L"Flags"))
flags = json[L"Flags"];
/* All units which contain the name "Olympus" are automatically under AI control */
/* TODO: I don't really like using this method */
if (unitName.find(L"Olympus") != wstring::npos)
{
AI = true;
}
/* If the unit is alive and it is not a human, run the AI Loop that performs the requested commands and instructions (moving, attacking, etc) */
if (AI && alive && flags[L"Human"].as_bool() == false)
{
AIloop();
}
@@ -57,6 +84,7 @@ void Unit::setPath(list<Coords> path)
void Unit::AIloop()
{
/* Set the active destination to be always equal to the first point of the active path */
if (activePath.size() > 0)
{
if (activeDestination != activePath.front())
@@ -66,10 +94,67 @@ void Unit::AIloop()
scheduler->appendCommand(command);
}
}
else
{
if (activeDestination != NULL)
{
log(unitName + L" no more points in active path");
activeDestination = Coords(0); // Set the active path to NULL
}
}
/* Ground unit AI Loop */
if (getCategory() == UnitCategory::GROUND)
{
if (activeDestination != NULL)
{
double newDist = 0;
geod.Inverse(latitude, longitude, activeDestination.lat, activeDestination.lng, newDist);
if (newDist < GROUND_DEST_DIST_THR)
{
/* Destination reached */
activePath.pop_front();
log(unitName + L" destination reached");
}
}
}
/* Air unit AI Loop */
if (getCategory() == UnitCategory::AIR)
{
if (activeDestination != NULL)
{
double newDist = 0;
geod.Inverse(latitude, longitude, activeDestination.lat, activeDestination.lng, newDist);
if (newDist < AIR_DEST_DIST_THR)
{
/* Destination reached */
activePath.pop_front();
log(name + L" destination reached");
}
}
else
{
/* Air units must ALWAYS have a destination or they will RTB and may become uncontrollable */
Coords point1;
Coords point2;
Coords point3;
geod.Direct(latitude, longitude, 45, 18520, point1.lat, point1.lng);
geod.Direct(point1.lat, point1.lng, 135, 18520, point2.lat, point2.lng);
geod.Direct(point2.lat, point2.lng, 225, 18520, point3.lat, point3.lng);
activePath.push_back(point1);
activePath.push_back(point2);
activePath.push_back(point3);
activePath.push_back(Coords(latitude, longitude));
}
}
}
json::value Unit::json()
{
/* Lock for thread safety */
lock_guard<mutex> guard(mutexLock);
auto json = json::value::object();
json[L"alive"] = alive;
@@ -83,6 +168,7 @@ json::value Unit::json()
json[L"longitude"] = longitude;
json[L"altitude"] = altitude;
json[L"heading"] = heading;
json[L"flags"] = flags;
/* Send the active path as a json object */
if (activePath.size() > 0) {

105
src/core/src/core.cpp Normal file
View File

@@ -0,0 +1,105 @@
#include "dcstools.h"
#include "logger.h"
#include "defines.h"
#include "unitsFactory.h"
#include "server.h"
#include "scheduler.h"
#include "scriptLoader.h"
#include "luatools.h"
auto before = std::chrono::system_clock::now();
UnitsFactory* unitsFactory = nullptr;
Server* server = nullptr;
Scheduler* scheduler = nullptr;
json::value missionData;
/* Called when DCS simulation stops. All singleton instances are deleted. */
extern "C" DllExport int coreDeinit(lua_State* L)
{
log("Olympus coreDeinit called successfully");
delete unitsFactory;
delete server;
delete scheduler;
log("All singletons objects destroyed successfully");
return(0);
}
/* 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)
{
unitsFactory = new UnitsFactory(L);
server = new Server(L);
scheduler = new Scheduler(L);
registerLuaFunctions(L);
return(0);
}
extern "C" DllExport int coreFrame(lua_State* L)
{
const std::chrono::duration<double> duration = std::chrono::system_clock::now() - before;
// TODO make intervals editable
if (duration.count() > UPDATE_TIME_INTERVAL)
{
if (unitsFactory != nullptr)
{
unitsFactory->update(L);
}
// TODO allow for different intervals
if (scheduler != nullptr)
{
scheduler->execute(L);
}
before = std::chrono::system_clock::now();
}
return(0);
}
extern "C" DllExport int coreMissionData(lua_State * L)
{
lua_getglobal(L, "Olympus");
lua_getfield(L, -1, "missionData");
missionData = luaTableToJSON(L, -1);
return(0);
}
BOOL WINAPI DllMain(
HINSTANCE hinstDLL, // handle to DLL module
DWORD fdwReason, // reason for calling function
LPVOID lpvReserved) // reserved
{
// Perform actions based on the reason for calling.
switch (fdwReason)
{
case DLL_PROCESS_ATTACH:
// Initialize once for each new process.
// Return FALSE to fail DLL load.
break;
case DLL_THREAD_ATTACH:
// Do thread-specific initialization.
break;
case DLL_THREAD_DETACH:
// Do thread-specific cleanup.
break;
case DLL_PROCESS_DETACH:
if (lpvReserved != nullptr)
{
break; // do not do cleanup if process termination scenario
}
// Perform any necessary cleanup.
break;
}
return TRUE; // Successful DLL_PROCESS_ATTACH.
}

View File

@@ -0,0 +1,85 @@
#include "scriptLoader.h"
#include "logger.h"
#include "luatools.h"
/* Executes the "OlympusCommand.lua" file to load in the "Server" Lua space all the Lua functions necessary to perform DCS commands (like moving units) */
void registerLuaFunctions(lua_State* L)
{
string modLocation;
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;
}
{
ifstream f(modLocation + "\\Scripts\\mist_4_4_90.lua");
string str;
log("Reading MIST from " + modLocation + "\\Scripts\\mist_4_4_90.lua");
if (f) {
ostringstream ss;
ss << f.rdbuf();
str = ss.str();
log("MIST read succesfully");
}
else
{
log("Error reading MIST");
return;
}
lua_getglobal(L, "net");
lua_getfield(L, -1, "dostring_in");
lua_pushstring(L, "server");
lua_pushstring(L, str.c_str());
if (lua_pcall(L, 2, 0, 0) != 0)
{
log("Error registering MIST");
}
else
{
log("MIST registered successfully");
}
}
{
ifstream f(modLocation + "\\Scripts\\OlympusCommand.lua");
string str;
log("Reading OlympusCommand.lua from " + modLocation + "\\Scripts\\OlympusCommand.lua");
if (f) {
ostringstream ss;
ss << f.rdbuf();
str = ss.str();
log("OlympusCommand.lua read succesfully");
}
else
{
log("Error reading OlympusCommand.lua");
return;
}
lua_getglobal(L, "net");
lua_getfield(L, -1, "dostring_in");
lua_pushstring(L, "server");
lua_pushstring(L, str.c_str());
if (lua_pcall(L, 2, 0, 0) != 0)
{
log("Error registering OlympusCommand.lua");
}
else
{
log("OlympusCommand.lua registered successfully");
}
}
}

151
src/core/src/server.cpp Normal file
View File

@@ -0,0 +1,151 @@
#include "server.h"
#include "logger.h"
#include "defines.h"
#include "unitsFactory.h"
#include "scheduler.h"
#include "luatools.h"
#include <exception>
#include <stdexcept>
extern UnitsFactory* unitsFactory;
extern Scheduler* scheduler;
extern json::value missionData;
void handle_eptr(std::exception_ptr eptr)
{
try {
if (eptr) {
std::rethrow_exception(eptr);
}
}
catch (const std::exception& e) {
log(e.what());
}
}
Server::Server(lua_State* L):
runListener(true)
{
LogInfo(L, "Starting RESTServer");
serverThread = new thread(&Server::task, this);
}
Server::~Server()
{
runListener = false;
}
void Server::handle_options(http_request request)
{
http_response response(status_codes::OK);
response.headers().add(U("Allow"), U("GET, POST, PUT, OPTIONS"));
response.headers().add(U("Access-Control-Allow-Origin"), U("*"));
response.headers().add(U("Access-Control-Allow-Methods"), U("GET, POST, PUT, OPTIONS"));
response.headers().add(U("Access-Control-Allow-Headers"), U("Content-Type"));
request.reply(response);
}
void Server::handle_get(http_request request)
{
http_response response(status_codes::OK);
response.headers().add(U("Allow"), U("GET, POST, PUT, OPTIONS"));
response.headers().add(U("Access-Control-Allow-Origin"), U("*"));
response.headers().add(U("Access-Control-Allow-Methods"), U("GET, POST, PUT, OPTIONS"));
response.headers().add(U("Access-Control-Allow-Headers"), U("Content-Type"));
auto answer = json::value::object();
std::exception_ptr eptr;
try {
unitsFactory->updateAnswer(answer);
answer[L"missionData"] = missionData;
response.set_body(answer);
}
catch (...) {
eptr = std::current_exception(); // capture
}
handle_eptr(eptr);
request.reply(response);
}
void Server::handle_request(http_request request, function<void(json::value const&, json::value&)> action)
{
auto answer = json::value::object();
request.extract_json().then([&answer, &action](pplx::task<json::value> task)
{
try
{
auto const& jvalue = task.get();
if (!jvalue.is_null())
{
action(jvalue, answer);
}
}
catch (http_exception const& e)
{
log(e.what());
}
}).wait();
http_response response(status_codes::OK);
response.headers().add(U("Allow"), U("GET, POST, PUT, OPTIONS"));
response.headers().add(U("Access-Control-Allow-Origin"), U("*"));
response.headers().add(U("Access-Control-Allow-Methods"), U("GET, POST, PUT, OPTIONS"));
response.headers().add(U("Access-Control-Allow-Headers"), U("Content-Type"));
response.set_body(answer);
request.reply(response);
}
void Server::handle_put(http_request request)
{
handle_request(
request,
[](json::value const& jvalue, json::value& answer)
{
for (auto const& e : jvalue.as_object())
{
auto key = e.first;
auto value = e.second;
std::exception_ptr eptr;
try {
scheduler->handleRequest(key, value);
}
catch (...) {
eptr = std::current_exception(); // capture
}
handle_eptr(eptr);
}
});
}
void Server::task()
{
http_listener listener(REST_ADDRESS);
std::function<void(http_request)> handle_options = std::bind(&Server::handle_options, this, std::placeholders::_1);
std::function<void(http_request)> handle_get = std::bind(&Server::handle_get, this, std::placeholders::_1);
std::function<void(http_request)> handle_put = std::bind(&Server::handle_put, this, std::placeholders::_1);
listener.support(methods::OPTIONS, handle_options);
listener.support(methods::GET, handle_get);
listener.support(methods::PUT, handle_put);
try
{
listener.open()
.then([&listener]() {log("RESTServer starting to listen"); })
.wait();
while (runListener);
listener.close();
log("RESTServer stopped listening");
}
catch (exception const& e)
{
log(e.what());
}
}

View File

@@ -0,0 +1,63 @@
#include "framework.h"
#include "unitsFactory.h"
#include "logger.h"
#include "unit.h"
#include "utils.h"
UnitsFactory::UnitsFactory(lua_State* L)
{
LogInfo(L, "Units Factory constructor called successfully");
}
UnitsFactory::~UnitsFactory()
{
}
Unit* UnitsFactory::getUnit(int ID)
{
if (units.find(ID) == units.end()) {
return nullptr;
}
else {
return units[ID];
}
}
void UnitsFactory::update(lua_State* L)
{
map<int, json::value> unitJSONs = getAllUnits(L);
/* Update all units, create them if needed */
for (auto const& p : unitJSONs)
{
int ID = p.first;
if (units.count(ID) == 0)
{
units[ID] = new Unit(p.second, ID);
}
units[ID]->update(p.second);
}
/* Set the units that are not present in the JSON as dead (probably have been destroyed) */
for (auto const& unit : units)
{
if (unitJSONs.find(unit.first) == unitJSONs.end())
{
unit.second->setAlive(false);
}
}
}
void UnitsFactory::updateAnswer(json::value& answer)
{
// TODO THREAT SAFEY!
auto unitsJson = json::value::object();
for (auto const& p : units)
{
unitsJson[to_wstring(p.first)] = p.second->json();
}
answer[L"units"] = unitsJson;
}

View File

@@ -0,0 +1,166 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|Win32">
<Configuration>Debug</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|Win32">
<Configuration>Release</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Debug|x64">
<Configuration>Debug</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|x64">
<Configuration>Release</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
</ItemGroup>
<ItemGroup>
<ClCompile Include="src\dcstools.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="include\dcstools.h" />
</ItemGroup>
<ItemGroup>
<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>
</ItemGroup>
<PropertyGroup Label="Globals">
<VCProjectVersion>16.0</VCProjectVersion>
<Keyword>Win32Proj</Keyword>
<ProjectGuid>{2b255368-39a0-431a-a6de-cc739ac70dc1}</ProjectGuid>
<RootNamespace>dcs</RootNamespace>
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>DynamicLibrary</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>DynamicLibrary</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>DynamicLibrary</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>StaticLibrary</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
</ImportGroup>
<ImportGroup Label="Shared">
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<PropertyGroup Label="UserMacros" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<OutDir>.\..\..\bin\</OutDir>
</PropertyGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>WIN32;_DEBUG;DCS_EXPORTS;_WINDOWS;_USRDLL;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<PrecompiledHeader>Use</PrecompiledHeader>
<PrecompiledHeaderFile>pch.h</PrecompiledHeaderFile>
</ClCompile>
<Link>
<SubSystem>Windows</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<EnableUAC>false</EnableUAC>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>WIN32;NDEBUG;DCS_EXPORTS;_WINDOWS;_USRDLL;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<PrecompiledHeader>Use</PrecompiledHeader>
<PrecompiledHeaderFile>pch.h</PrecompiledHeaderFile>
</ClCompile>
<Link>
<SubSystem>Windows</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation>
<EnableUAC>false</EnableUAC>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>_DEBUG;DCS_EXPORTS;_WINDOWS;_USRDLL;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<PrecompiledHeader>Use</PrecompiledHeader>
<PrecompiledHeaderFile>pch.h</PrecompiledHeaderFile>
</ClCompile>
<Link>
<SubSystem>Windows</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<EnableUAC>false</EnableUAC>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>NDEBUG;DCSTOOLS_EXPORTS;_WINDOWS;_USRDLL;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<PrecompiledHeader>NotUsing</PrecompiledHeader>
<PrecompiledHeaderFile>
</PrecompiledHeaderFile>
<AdditionalIncludeDirectories>include;..\..\third-party\lua\include;..\shared\include;..\utils\include;..\luatools\include</AdditionalIncludeDirectories>
<LanguageStandard>stdcpp20</LanguageStandard>
</ClCompile>
<Link>
<SubSystem>Windows</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation>
<EnableUAC>false</EnableUAC>
<AdditionalDependencies>lua.lib;%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalLibraryDirectories>..\..\third-party\lua</AdditionalLibraryDirectories>
</Link>
</ItemDefinitionGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>

View File

@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<Filter Include="Source Files">
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
<Extensions>cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
</Filter>
<Filter Include="Header Files">
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
<Extensions>h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd</Extensions>
</Filter>
<Filter Include="Resource Files">
<UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
</Filter>
</ItemGroup>
<ItemGroup>
<ClCompile Include="src\dcstools.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="include\dcstools.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
</Project>

View File

@@ -0,0 +1,11 @@
#pragma once
#include "framework.h"
#include "luatools.h"
void DllExport LogInfo(lua_State* L, string message);
void DllExport LogWarning(lua_State* L, string message);
void DllExport LogError(lua_State* L, string message);
void DllExport Log(lua_State* L, string message, int level);
map<int, json::value> DllExport getAllUnits(lua_State* L);

View File

@@ -0,0 +1,95 @@
#include "dcstools.h"
void LogInfo(lua_State* L, string message)
{
STACK_INIT;
lua_getglobal(L, "log");
lua_getfield(L, -1, "INFO");
int infoLevel = (int)lua_tointeger(L, -1);
STACK_POP(1);
STACK_CLEAN;
Log(L, message, infoLevel);
}
void LogWarning(lua_State* L, string message)
{
STACK_INIT;
lua_getglobal(L, "log");
lua_getfield(L, -1, "WARNING");
int warningLevel = (int)lua_tointeger(L, -1);
STACK_POP(1);
STACK_CLEAN;
Log(L, message, warningLevel);
}
void LogError(lua_State* L, string message)
{
STACK_INIT;
lua_getglobal(L, "log");
lua_getfield(L, -1, "ERROR");
int errorLevel = (int)lua_tointeger(L, -1);
STACK_POP(1);
STACK_CLEAN;
Log(L, message, errorLevel);
}
void Log(lua_State* L, string message, int level)
{
STACK_INIT;
lua_getglobal(L, "log");
lua_getfield(L, -1, "write");
lua_pushstring(L, "Olympus.dll");
lua_pushnumber(L, level);
lua_pushstring(L, message.c_str());
lua_pcall(L, 3, 0, 0);
STACK_CLEAN;
}
map<int, json::value> getAllUnits(lua_State* L)
{
int res = 0;
map<int, json::value> units;
STACK_INIT;
lua_getglobal(L, "Export");
lua_getfield(L, -1, "LoGetWorldObjects");
res = lua_pcall(L, 0, 1, 0);
if (res != 0)
{
LogError(L, "Error retrieving World Objects");
goto exit;
}
if (!lua_istable(L, 2))
{
LogError(L, "Error retrieving World Objects");
goto exit;
}
else
{
lua_pushnil(L);
while (lua_next(L, 2) != 0)
{
int ID = lua_tonumber(L, -2);
units[ID] = luaTableToJSON(L, -1);
STACK_POP(1)
}
}
exit:
STACK_CLEAN;
return units;
}

View File

@@ -0,0 +1,5 @@
#pragma once
#include "framework.h"
void DllExport log(const std::string& sMessage);
void DllExport log(const std::wstring& sMessage);

View File

@@ -0,0 +1,25 @@
#pragma once
#include "framework.h"
#include "interface.h"
class Logger
{
public:
void Log(const string& sMessage);
void Log(const wstring& sMessage);
static Logger* GetLogger();
private:
Logger();
Logger(const Logger&) {}; // copy constructor is private
Logger& operator=(const Logger&) { return *this; }; // assignment operator is private
static const string m_sFileName;
static Logger* m_pThis;
static ofstream m_Logfile;
void Open();
void Close();
};

163
src/logger/logger.vcxproj Normal file
View File

@@ -0,0 +1,163 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|Win32">
<Configuration>Debug</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|Win32">
<Configuration>Release</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Debug|x64">
<Configuration>Debug</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|x64">
<Configuration>Release</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
</ItemGroup>
<ItemGroup>
<ClCompile Include="src\interface.cpp" />
<ClCompile Include="src\logger.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="include\interface.h" />
<ClInclude Include="include\logger.h" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\utils\utils.vcxproj">
<Project>{b85009ce-4a5c-4a5a-b85d-001b3a2651b2}</Project>
</ProjectReference>
</ItemGroup>
<PropertyGroup Label="Globals">
<VCProjectVersion>16.0</VCProjectVersion>
<Keyword>Win32Proj</Keyword>
<ProjectGuid>{873ecabe-fcfe-4217-ac15-91959c3cf1c6}</ProjectGuid>
<RootNamespace>logger</RootNamespace>
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>DynamicLibrary</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>DynamicLibrary</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>DynamicLibrary</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>DynamicLibrary</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
</ImportGroup>
<ImportGroup Label="Shared">
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<PropertyGroup Label="UserMacros" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<OutDir>.\..\..\bin\</OutDir>
</PropertyGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>WIN32;_DEBUG;LOGGER_EXPORTS;_WINDOWS;_USRDLL;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<PrecompiledHeader>Use</PrecompiledHeader>
<PrecompiledHeaderFile>pch.h</PrecompiledHeaderFile>
</ClCompile>
<Link>
<SubSystem>Windows</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<EnableUAC>false</EnableUAC>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>WIN32;NDEBUG;LOGGER_EXPORTS;_WINDOWS;_USRDLL;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<PrecompiledHeader>Use</PrecompiledHeader>
<PrecompiledHeaderFile>pch.h</PrecompiledHeaderFile>
</ClCompile>
<Link>
<SubSystem>Windows</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation>
<EnableUAC>false</EnableUAC>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>_DEBUG;LOGGER_EXPORTS;_WINDOWS;_USRDLL;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<PrecompiledHeader>Use</PrecompiledHeader>
<PrecompiledHeaderFile>pch.h</PrecompiledHeaderFile>
</ClCompile>
<Link>
<SubSystem>Windows</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<EnableUAC>false</EnableUAC>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>NDEBUG;LOGGER_EXPORTS;_WINDOWS;_USRDLL;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<PrecompiledHeader>NotUsing</PrecompiledHeader>
<PrecompiledHeaderFile>
</PrecompiledHeaderFile>
<AdditionalIncludeDirectories>include;..\..\third-party\lua\include;..\shared\include;..\utils\include</AdditionalIncludeDirectories>
<LanguageStandard>stdcpp20</LanguageStandard>
</ClCompile>
<Link>
<SubSystem>Windows</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation>
<EnableUAC>false</EnableUAC>
</Link>
</ItemDefinitionGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>

View File

@@ -0,0 +1,33 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<Filter Include="Source Files">
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
<Extensions>cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
</Filter>
<Filter Include="Header Files">
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
<Extensions>h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd</Extensions>
</Filter>
<Filter Include="Resource Files">
<UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
</Filter>
</ItemGroup>
<ItemGroup>
<ClCompile Include="src\logger.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="src\interface.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="include\logger.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="include\interface.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
</Project>

View File

@@ -0,0 +1,15 @@
#include "framework.h"
#include "logger.h"
#include "interface.h"
#define LOGGER Logger::GetLogger()
void log(const string& message)
{
LOGGER->Log(message);
}
void log(const wstring& message)
{
LOGGER->Log(message);
}

49
src/logger/src/logger.cpp Normal file
View File

@@ -0,0 +1,49 @@
#include "logger.h"
#include "utils.h"
#include "defines.h"
const string Logger::m_sFileName = LOG_NAME;
Logger* Logger::m_pThis = NULL;
ofstream Logger::m_Logfile;
Logger::Logger()
{
}
Logger* Logger::GetLogger()
{
if (m_pThis == NULL) {
m_pThis = new Logger();
std::filesystem::path dirPath = std::filesystem::temp_directory_path();
m_Logfile.open((dirPath.string() + m_sFileName).c_str(), ios::out | ios::app);
m_pThis->Log("**************************************************");
}
return m_pThis;
}
void Logger::Open()
{
std::filesystem::path dirPath = std::filesystem::temp_directory_path();
m_Logfile.open((dirPath.string() + m_sFileName).c_str(), ios::out | ios::app);
}
void Logger::Close()
{
m_Logfile.close();
}
void Logger::Log(const string& message)
{
Open();
m_Logfile << CurrentDateTime() << ":\t";
m_Logfile << message << "\n";
Close();
}
void Logger::Log(const wstring& message)
{
Open();
m_Logfile << CurrentDateTime() << ":\t";
m_Logfile << to_string(message) << "\n";
Close();
}

View File

@@ -0,0 +1,12 @@
#pragma once
#include "framework.h"
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);
json::value DllExport luaTableToJSON(lua_State* L, int index);
#define STACK_UPDATE stackUpdate(L, stackDepth, initialStack);
#define STACK_INIT int stackDepth = 0; int initialStack = 0; stackUpdate(L, initialStack);
#define STACK_POP(X) stackPop(L, X); STACK_UPDATE;
#define STACK_CLEAN STACK_UPDATE; stackClean(L, stackDepth);

View File

@@ -0,0 +1,167 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|Win32">
<Configuration>Debug</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|Win32">
<Configuration>Release</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Debug|x64">
<Configuration>Debug</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|x64">
<Configuration>Release</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
</ItemGroup>
<PropertyGroup Label="Globals">
<VCProjectVersion>16.0</VCProjectVersion>
<Keyword>Win32Proj</Keyword>
<ProjectGuid>{de139ec1-4f88-47d5-be73-f41915fe14a3}</ProjectGuid>
<RootNamespace>lua</RootNamespace>
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
<ProjectName>luatools</ProjectName>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>DynamicLibrary</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>DynamicLibrary</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>DynamicLibrary</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>StaticLibrary</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
</ImportGroup>
<ImportGroup Label="Shared">
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<PropertyGroup Label="UserMacros" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<OutDir>.\..\..\bin\</OutDir>
</PropertyGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>WIN32;_DEBUG;LUA_EXPORTS;_WINDOWS;_USRDLL;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<PrecompiledHeader>Use</PrecompiledHeader>
<PrecompiledHeaderFile>pch.h</PrecompiledHeaderFile>
</ClCompile>
<Link>
<SubSystem>Windows</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<EnableUAC>false</EnableUAC>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>WIN32;NDEBUG;LUA_EXPORTS;_WINDOWS;_USRDLL;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<PrecompiledHeader>Use</PrecompiledHeader>
<PrecompiledHeaderFile>pch.h</PrecompiledHeaderFile>
</ClCompile>
<Link>
<SubSystem>Windows</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation>
<EnableUAC>false</EnableUAC>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>_DEBUG;LUA_EXPORTS;_WINDOWS;_USRDLL;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<PrecompiledHeader>Use</PrecompiledHeader>
<PrecompiledHeaderFile>pch.h</PrecompiledHeaderFile>
</ClCompile>
<Link>
<SubSystem>Windows</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<EnableUAC>false</EnableUAC>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>NDEBUG;LUATOOLS_EXPORTS;_WINDOWS;_USRDLL;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<PrecompiledHeader>NotUsing</PrecompiledHeader>
<PrecompiledHeaderFile>
</PrecompiledHeaderFile>
<AdditionalIncludeDirectories>include; ..\..\third-party\lua\include; ..\utils\include; ..\shared\include; ..\logger\include;</AdditionalIncludeDirectories>
<LanguageStandard>stdcpp20</LanguageStandard>
</ClCompile>
<Link>
<SubSystem>Windows</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation>
<EnableUAC>false</EnableUAC>
<AdditionalDependencies>lua.lib;%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalLibraryDirectories>..\..\third-party\lua</AdditionalLibraryDirectories>
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ProjectReference Include="..\logger\logger.vcxproj">
<Project>{873ecabe-fcfe-4217-ac15-91959c3cf1c6}</Project>
</ProjectReference>
<ProjectReference Include="..\utils\utils.vcxproj">
<Project>{b85009ce-4a5c-4a5a-b85d-001b3a2651b2}</Project>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<ClCompile Include="src\luatools.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="include\luatools.h" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>

View File

@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<Filter Include="Source Files">
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
<Extensions>cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
</Filter>
<Filter Include="Header Files">
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
<Extensions>h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd</Extensions>
</Filter>
<Filter Include="Resource Files">
<UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
</Filter>
</ItemGroup>
<ItemGroup>
<ClCompile Include="src\luatools.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="include\luatools.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
</Project>

View File

@@ -0,0 +1,59 @@
#include "framework.h"
#include "luaTools.h"
#include "logger.h"
#include "utils.h"
void stackUpdate(lua_State* L, int& stackDepth, int initialStack)
{
stackDepth = lua_gettop(L) - initialStack;
}
void stackPop(lua_State* L, int popDepth)
{
lua_pop(L, popDepth);
}
void stackClean(lua_State* L, int stackDepth)
{
lua_pop(L, stackDepth);
}
json::value luaTableToJSON(lua_State* L, int index)
{
auto json = json::value::object();
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);
if (lua_istable(L, -2))
{
json[to_wstring(key)] = luaTableToJSON(L, -2);
}
else if (lua_isnumber(L, -2))
{
json[to_wstring(key)] = json::value::number(lua_tonumber(L, -2));
}
else if (lua_isboolean(L, -2))
{
json[to_wstring(key)] = json::value::boolean(lua_toboolean(L, -2));
}
else if (lua_isstring(L, -2)) // Keep last, only checks if it can be stringified (not if it actually IS a string)
{
json[to_wstring(key)] = json::value::string(to_wstring(lua_tostring(L, -2)));
}
lua_pop(L, 2);
}
lua_pop(L, 1);
STACK_CLEAN;
}
return json;
}