Started units spawn functions and major refactoring

This commit is contained in:
Pax1601
2022-12-07 19:46:54 +01:00
parent a10762ac0c
commit 3a46a6df21
41 changed files with 1104 additions and 1121 deletions

View File

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

View File

@@ -1,15 +0,0 @@
#pragma once
#include "framework.h"
namespace LUAUtils
{
void stackUpdate(lua_State* L, int& stackDepth, int initialStack = 0);
void stackPop(lua_State* L, int popDepth = 1);
void stackClean(lua_State* L, int stackDepth);
json::value tableToJSON(lua_State* L, int index);
}
#define STACK_UPDATE LUAUtils::stackUpdate(L, stackDepth, initialStack);
#define STACK_INIT int stackDepth = 0; int initialStack = 0; LUAUtils::stackUpdate(L, initialStack);
#define STACK_POP(X) LUAUtils::stackPop(L, X); STACK_UPDATE;
#define STACK_CLEAN STACK_UPDATE; LUAUtils::stackClean(L, stackDepth);

View File

@@ -1,26 +0,0 @@
#pragma once
#include "framework.h"
#define LOGGER Logger::GetLogger()
class Logger
{
public:
void Log(const std::string& sMessage);
void Log(const std::wstring& sMessage);
void Log(const char* format, ...);
Logger& operator<<(const string& 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 std::string m_sFileName;
static Logger* m_pThis;
static ofstream m_Logfile;
void Open();
void Close();
};

View File

@@ -7,15 +7,12 @@ struct Coords {
double alt = 0;
};
bool operator== (const Coords& a, const Coords& b);
bool operator!= (const Coords& a, const Coords& b);
bool operator== (const Coords& a, const int& b);
bool operator!= (const Coords& a, const int& b);
// Get current date/time, format is YYYY-MM-DD.HH:mm:ss
const DllExport std::string CurrentDateTime();
std::wstring DllExport to_wstring(const std::string& str);
std::string DllExport to_string(const std::wstring& wstr);
namespace Utils
{
// Get current date/time, format is YYYY-MM-DD.HH:mm:ss
const std::string CurrentDateTime();
std::wstring to_wstring(const std::string& str);
std::string to_string(const std::wstring& wstr);
}
bool DllExport operator== (const Coords& a, const Coords& b);
bool DllExport operator!= (const Coords& a, const Coords& b);
bool DllExport operator== (const Coords& a, const int& b);
bool DllExport operator!= (const Coords& a, const int& b);

View File

@@ -1,96 +0,0 @@
#include "DCSUtils.h"
#include "Logger.h"
void DCSUtils::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;
DCSUtils::Log(L, message, infoLevel);
}
void DCSUtils::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;
DCSUtils::Log(L, message, warningLevel);
}
void DCSUtils::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;
DCSUtils::Log(L, message, errorLevel);
}
void DCSUtils::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> DCSUtils::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)
{
DCSUtils::LogError(L, "Error retrieving World Objects");
goto exit;
}
if (!lua_istable(L, 2))
{
DCSUtils::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] = LUAUtils::tableToJSON(L, -1);
STACK_POP(1)
}
}
exit:
STACK_CLEAN;
return units;
}

View File

@@ -1,57 +0,0 @@
#include "LUAUtils.h"
#include "Logger.h"
#include "Utils.h"
void LUAUtils::stackUpdate(lua_State* L, int& stackDepth, int initialStack)
{
stackDepth = lua_gettop(L) - initialStack;
}
void LUAUtils::stackPop(lua_State* L, int popDepth)
{
lua_pop(L, popDepth);
}
void LUAUtils::stackClean(lua_State* L, int stackDepth)
{
lua_pop(L, stackDepth);
}
json::value LUAUtils::tableToJSON(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[Utils::to_wstring(key)] = tableToJSON(L, -2);
}
else if (lua_isnumber(L, -2))
{
json[Utils::to_wstring(key)] = json::value::number(lua_tonumber(L, -2));
}
else if (lua_isboolean(L, -2))
{
json[Utils::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
{
json[Utils::to_wstring(key)] = json::value::string(Utils::to_wstring(lua_tostring(L, -2)));
}
lua_pop(L, 2);
}
lua_pop(L, 1);
STACK_CLEAN;
}
return json;
}

View File

@@ -1,79 +0,0 @@
#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 char* format, ...)
{
Open();
char* sMessage = NULL;
int nLength = 0;
va_list args;
va_start(args, format);
// Return the number of characters in the string referenced the list of arguments.
// _vscprintf doesn't count terminating '\0' (that's why +1)
nLength = _vscprintf(format, args) + 1;
sMessage = new char[nLength];
vsprintf_s(sMessage, nLength, format, args);
//vsprintf(sMessage, format, args);
m_Logfile << Utils::CurrentDateTime() << ":\t";
m_Logfile << sMessage << "\n";
va_end(args);
Close();
delete[] sMessage;
}
void Logger::Log(const string& sMessage)
{
Open();
m_Logfile << Utils::CurrentDateTime() << ":\t";
m_Logfile << sMessage << "\n";
Close();
}
void Logger::Log(const wstring& sMessage)
{
Open();
m_Logfile << Utils::CurrentDateTime() << ":\t";
m_Logfile << Utils::to_string(sMessage) << "\n";
Close();
}
Logger& Logger::operator<<(const string& sMessage)
{
Open();
m_Logfile << "\n" << Utils::CurrentDateTime() << ":\t";
m_Logfile << sMessage << "\n";
return *this;
Close();
}

View File

@@ -1,8 +1,8 @@
#include "framework.h"
#include "Utils.h"
#include "utils.h"
// Get current date/time, format is YYYY-MM-DD.HH:mm:ss
const std::string Utils::CurrentDateTime()
const std::string CurrentDateTime()
{
time_t now = time(NULL);
struct tm tstruct;
@@ -12,7 +12,7 @@ const std::string Utils::CurrentDateTime()
return buf;
}
std::wstring Utils::to_wstring(const std::string& str)
std::wstring to_wstring(const std::string& str)
{
int size_needed = MultiByteToWideChar(CP_UTF8, 0, &str[0], (int)str.size(), NULL, 0);
std::wstring wstrTo(size_needed, 0);
@@ -20,7 +20,7 @@ std::wstring Utils::to_wstring(const std::string& str)
return wstrTo;
}
std::string Utils::to_string(const std::wstring& wstr)
std::string to_string(const std::wstring& wstr)
{
if (wstr.empty())
{

View File

@@ -1,18 +0,0 @@
#include "framework.h"
BOOL APIENTRY DllMain( HMODULE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}

View File

@@ -19,17 +19,10 @@
</ProjectConfiguration>
</ItemGroup>
<ItemGroup>
<ClInclude Include="include\DCSUtils.h" />
<ClInclude Include="include\Logger.h" />
<ClInclude Include="include\LUAUtils.h" />
<ClInclude Include="include\Utils.h" />
<ClInclude Include="include\utils.h" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="src\DCSUtils.cpp" />
<ClCompile Include="src\main.cpp" />
<ClCompile Include="src\Logger.cpp" />
<ClCompile Include="src\LUAUtils.cpp" />
<ClCompile Include="src\Utils.cpp" />
<ClCompile Include="src\utils.cpp" />
</ItemGroup>
<PropertyGroup Label="Globals">
<VCProjectVersion>16.0</VCProjectVersion>
@@ -59,7 +52,7 @@
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>StaticLibrary</ConfigurationType>
<ConfigurationType>DynamicLibrary</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
@@ -151,9 +144,10 @@
<PreprocessorDefinitions>NDEBUG;UTILS_EXPORTS;_WINDOWS;_USRDLL;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<PrecompiledHeader>NotUsing</PrecompiledHeader>
<PrecompiledHeaderFile>pch.h</PrecompiledHeaderFile>
<PrecompiledHeaderFile>
</PrecompiledHeaderFile>
<AdditionalIncludeDirectories>include;..\..\third-party\lua\include;..\shared\include</AdditionalIncludeDirectories>
<LanguageStandard>stdcpp17</LanguageStandard>
<LanguageStandard>stdcpp20</LanguageStandard>
</ClCompile>
<Link>
<SubSystem>Windows</SubSystem>

View File

@@ -11,33 +11,12 @@
</Filter>
</ItemGroup>
<ItemGroup>
<ClInclude Include="include\DCSUtils.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="include\LUAUtils.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="include\Utils.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="include\Logger.h">
<ClInclude Include="include\utils.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="src\DCSUtils.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="src\main.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="src\LUAUtils.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="src\Utils.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="src\Logger.cpp">
<ClCompile Include="src\utils.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>