From 0ce5011b2b397f9d2c32f7b9e7e85f5d07752a36 Mon Sep 17 00:00:00 2001 From: Imrglop <69129770+Imrglop@users.noreply.github.com> Date: Thu, 13 Jul 2023 18:38:42 -0400 Subject: [PATCH] ... --- CMakeLists.txt | 10 +++++++-- CppProperties.json | 22 ++++++++++++++++++++ src/client/Latite.cpp | 7 ++++++- src/client/module/ModuleManager.h | 2 +- src/util/Logger.cpp | 34 +++++++++++++++++++------------ src/util/Logger.h | 25 ++++++++++++++++++++++- src/util/Util.cpp | 14 ++++++++----- src/util/Util.h | 5 +++-- 8 files changed, 94 insertions(+), 25 deletions(-) create mode 100644 CppProperties.json diff --git a/CMakeLists.txt b/CMakeLists.txt index a8043cde..30375bdf 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -13,16 +13,22 @@ project ("LatiteRecode") # Include sub-projects. add_subdirectory ("src") -file (GLOB SOURCES "util/Util.cpp" "util/Logger.cpp" "src/client/Latite.cpp" ) +file (GLOB SOURCES "src/util/Util.cpp" "src/util/Logger.cpp" "src/client/Latite.cpp" ) include_directories("src") add_library(Latite SHARED ${SOURCES} "src/sdk/common/world/Minecraft.h" "src/sdk/common/world/Level.h" "src/sdk/server/ServerInstance.h" "src/api/manager/Manager.h" "src/client/module/ModuleManager.h" "src/client/module/Module.h" "src/api/manager/FeatureManager.h" "src/sdk/client/win/winrt/HidControllerWinRT.h" "src/client/command/Command.h" "src/client/script/ScriptManager.h" "src/client/script/JsScript.h") +if (MSVC) + # There is a bug in visual studio that prevents intellisense from realizing + # /std:c++latest is on the command line if you only use target_compile_features(cxx_std_20) + target_compile_options(Latite PUBLIC "/std:c++20") +endif() + target_precompile_headers(Latite PUBLIC src/pch.h ) set_property(TARGET Latite PROPERTY CXX_STANDARD 20) -set (CMAKE_CXX_STANDARD 20) \ No newline at end of file +set (CMAKE_CXX_STANDARD 20) diff --git a/CppProperties.json b/CppProperties.json new file mode 100644 index 00000000..358b86a8 --- /dev/null +++ b/CppProperties.json @@ -0,0 +1,22 @@ +{ + "configurations": [ + { + "inheritEnvironments": [ + "msvc_x64" + ], + "name": "x64-Debug", + "includePath": [ + "${env.INCLUDE}", + "${workspaceRoot}\\**" + ], + "defines": [ + "WIN32", + "_DEBUG", + "UNICODE", + "_UNICODE" + ], + "intelliSenseMode": "windows-msvc-x64", + "cppStandard": "c++20" + } + ] +} \ No newline at end of file diff --git a/src/client/Latite.cpp b/src/client/Latite.cpp index e2656a9e..703998da 100644 --- a/src/client/Latite.cpp +++ b/src/client/Latite.cpp @@ -17,7 +17,7 @@ DWORD __stdcall startThread(HINSTANCE dll) { new (mmgrBuf) ModuleManager; Latite::get().initialize(dll); Logger::setup(); - Logger::log("Initializing Latite Client {}", "test"); + Logger::info("Initializing Latite Client {}", "test"); return 0ul; } @@ -30,6 +30,11 @@ BOOL WINAPI DllMain( if (fdwReason == DLL_PROCESS_ATTACH) { CloseHandle(CreateThread(nullptr, 0, reinterpret_cast(startThread), hinstDLL, 0, nullptr)); } + else if (fdwReason == DLL_PROCESS_DETACH) { + // Remove singletons + Latite::get().getModuleManager().~ModuleManager(); + Latite::get().~Latite(); + } return TRUE; // Successful DLL_PROCESS_ATTACH. } diff --git a/src/client/module/ModuleManager.h b/src/client/module/ModuleManager.h index 52ea4809..78145f34 100644 --- a/src/client/module/ModuleManager.h +++ b/src/client/module/ModuleManager.h @@ -4,5 +4,5 @@ class ModuleManager : public FeatureManager { public: - + ~ModuleManager() = default; }; \ No newline at end of file diff --git a/src/util/Logger.cpp b/src/util/Logger.cpp index 38596fdc..c27773dd 100644 --- a/src/util/Logger.cpp +++ b/src/util/Logger.cpp @@ -4,27 +4,35 @@ void Logger::setup() { - std::wstring path = util::getLatitePath(); + auto path = util::getLatitePath(); std::filesystem::create_directory(path); + std::filesystem::create_directory(path / "Logs"); } -void Logger::log(std::string_view fmt, ... args) +void Logger::logInternal(Level level, std::string str) { - std::wstring path = util::getLatitePath(); - std::wstring logFolder = path + L"\\Logs"; - - va_list args; - va_start(args, fmt); - - std::string formatted = std::vformat(fmt, args); + std::string prefix = ""; + switch (level) { + case Level::Info: + prefix = "INFO"; + break; + case Level::Warn: + prefix = "WARN"; + break; + case Level::Fatal: + prefix = "FATAL"; + break; + } - va_end(args); + str = "[" + prefix + "] " + str + "\n"; + auto path = util::getLatitePath(); + auto logPath = path / "Logs" / "log.txt"; std::ofstream ofs; - ofs.open(fmt, std::ios::app); + ofs.open(logPath, std::ios::app); if (!ofs.fail()) { - ofs << formatted; + ofs << str; } - OutputDebugStringA(formatted.c_str()); + OutputDebugStringA(str.c_str()); } diff --git a/src/util/Logger.h b/src/util/Logger.h index 0a62b47b..a2fe9ce9 100644 --- a/src/util/Logger.h +++ b/src/util/Logger.h @@ -1,7 +1,30 @@ #pragma once #include +#include namespace Logger { + enum class Level { + Info, + Warn, + Fatal + }; + extern void setup(); - extern void log(std::string_view fmt, ...); + + extern void logInternal(Level level, std::string str); + + template + extern inline void info(std::string_view fmt, Args&&... args) { + logInternal(Level::Info, std::format(fmt, std::forward(args)...)); + } + + template + extern inline void warn(std::string_view fmt, Args&&... args) { + logInternal(Level::Warn, std::format(fmt, std::forward(args)...)); + } + + template + extern inline void fatal(std::string_view fmt, Args&&... args) { + logInternal(Level::Fatal, std::format(fmt, std::forward(args)...)); + } } \ No newline at end of file diff --git a/src/util/Util.cpp b/src/util/Util.cpp index c6c17fbf..4207b279 100644 --- a/src/util/Util.cpp +++ b/src/util/Util.cpp @@ -1,7 +1,7 @@ #include "Util.h" #include "pch.h" -std::wstring util::getRoamingPath() +std::filesystem::path util::getRoamingPath() { char* env; size_t size; @@ -13,11 +13,10 @@ std::wstring util::getRoamingPath() return std::wstring(); } -std::wstring util::getLatitePath() +std::filesystem::path util::getLatitePath() { // TODO: Rename to Latite - getRoamingPath() + L"\\LatiteRecode"; - return std::wstring(); + return getRoamingPath()/"LatiteRecode"; } std::wstring util::strToWstr(std::string const& s) @@ -33,5 +32,10 @@ std::wstring util::strToWstr(std::string const& s) std::string util::wstrToStr(std::wstring const& ws) { - return std::string(); + int len = WideCharToMultiByte(CP_ACP, 0, ws.c_str(), static_cast(ws.size() + 1), 0, 0, 0, 0); + char* buf = new char[len]; + WideCharToMultiByte(CP_ACP, 0, ws.c_str(), static_cast(ws.size() + 1), buf, len, 0, 0); + std::string r(buf); + delete[] buf; + return r; } diff --git a/src/util/Util.h b/src/util/Util.h index 15706c8a..4d27cd7c 100644 --- a/src/util/Util.h +++ b/src/util/Util.h @@ -1,9 +1,10 @@ #pragma once #include +#include namespace util { - extern std::wstring getRoamingPath(); - extern std::wstring getLatitePath(); + extern std::filesystem::path getRoamingPath(); + extern std::filesystem::path getLatitePath(); extern std::wstring strToWstr(std::string const& s); extern std::string wstrToStr(std::wstring const& ws); } \ No newline at end of file