First commit

This commit is contained in:
Pax1601
2022-11-20 12:05:38 +01:00
commit 3aa1cfe104
162 changed files with 4318 additions and 0 deletions

View File

@@ -0,0 +1,13 @@
#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

@@ -0,0 +1,15 @@
#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

@@ -0,0 +1,26 @@
#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();
};

21
src/utils/include/Utils.h Normal file
View File

@@ -0,0 +1,21 @@
#pragma once
#include "framework.h"
struct Coords {
double lat = 0;
double lng = 0;
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);
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);
}

View File

@@ -0,0 +1,96 @@
#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

@@ -0,0 +1,57 @@
#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;
}

79
src/utils/src/Logger.cpp Normal file
View File

@@ -0,0 +1,79 @@
#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();
}

44
src/utils/src/Utils.cpp Normal file
View File

@@ -0,0 +1,44 @@
#include "framework.h"
#include "Utils.h"
// Get current date/time, format is YYYY-MM-DD.HH:mm:ss
const std::string Utils::CurrentDateTime()
{
time_t now = time(NULL);
struct tm tstruct;
char buf[80];
localtime_s(&tstruct, &now);
strftime(buf, sizeof(buf), "%Y-%m-%d.%X", &tstruct);
return buf;
}
std::wstring Utils::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);
MultiByteToWideChar(CP_UTF8, 0, &str[0], (int)str.size(), &wstrTo[0], size_needed);
return wstrTo;
}
std::string Utils::to_string(const std::wstring& wstr)
{
if (wstr.empty())
{
return "";
}
const auto size_needed = WideCharToMultiByte(CP_UTF8, 0, &wstr.at(0), (int)wstr.size(), nullptr, 0, nullptr, nullptr);
if (size_needed <= 0)
{
throw std::runtime_error("WideCharToMultiByte() failed: " + std::to_string(size_needed));
}
std::string result(size_needed, 0);
WideCharToMultiByte(CP_UTF8, 0, &wstr.at(0), (int)wstr.size(), &result.at(0), size_needed, nullptr, nullptr);
return result;
}
bool operator== (const Coords& a, const Coords& b) { return a.lat == b.lat && a.lng == b.lng && a.alt == b.alt; }
bool operator!= (const Coords& a, const Coords& b) { return !(a == b); }
bool operator== (const Coords& a, const int& b) { return a.lat == b && a.lng == b && a.alt == b; }
bool operator!= (const Coords& a, const int& b) { return !(a == b); }

18
src/utils/src/main.cpp Normal file
View File

@@ -0,0 +1,18 @@
#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;
}

5
src/utils/src/pch.cpp Normal file
View File

@@ -0,0 +1,5 @@
// pch.cpp: source file corresponding to the pre-compiled header
#include "pch.h"
// When you are using pre-compiled headers, this source file is necessary for compilation to succeed.

171
src/utils/utils.vcxproj Normal file
View File

@@ -0,0 +1,171 @@
<?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>
<ClInclude Include="include\DCSUtils.h" />
<ClInclude Include="include\Logger.h" />
<ClInclude Include="include\LUAUtils.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" />
</ItemGroup>
<PropertyGroup Label="Globals">
<VCProjectVersion>16.0</VCProjectVersion>
<Keyword>Win32Proj</Keyword>
<ProjectGuid>{b85009ce-4a5c-4a5a-b85d-001b3a2651b2}</ProjectGuid>
<RootNamespace>utils</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>StaticLibrary</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\$(Platform)\$(Configuration)\</OutDir>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<OutDir>.\..\..\bin\$(Platform)\$(Configuration)\</OutDir>
</PropertyGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>WIN32;_DEBUG;UTILS_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;UTILS_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;UTILS_EXPORTS;_WINDOWS;_USRDLL;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<PrecompiledHeader>NotUsing</PrecompiledHeader>
<PrecompiledHeaderFile>pch.h</PrecompiledHeaderFile>
<AdditionalIncludeDirectories>include;..\..\third-party\lua\include;..\shared\include</AdditionalIncludeDirectories>
<LanguageStandard>stdcpp17</LanguageStandard>
</ClCompile>
<Link>
<SubSystem>Windows</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<EnableUAC>false</EnableUAC>
<AdditionalLibraryDirectories>..\..\third-party\lua</AdditionalLibraryDirectories>
<AdditionalDependencies>lua.lib; $(CoreLibraryDependencies);%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>NDEBUG;UTILS_EXPORTS;_WINDOWS;_USRDLL;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<PrecompiledHeader>NotUsing</PrecompiledHeader>
<PrecompiledHeaderFile>pch.h</PrecompiledHeaderFile>
<AdditionalIncludeDirectories>include;..\..\third-party\lua\include;..\shared\include</AdditionalIncludeDirectories>
<LanguageStandard>stdcpp17</LanguageStandard>
</ClCompile>
<Link>
<SubSystem>Windows</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation>
<EnableUAC>false</EnableUAC>
<AdditionalDependencies>lua.lib; $(CoreLibraryDependencies);%(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,44 @@
<?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>
</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">
<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">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
</Project>