Skip to content

Commit

Permalink
...
Browse files Browse the repository at this point in the history
  • Loading branch information
Imrglop committed Jul 13, 2023
1 parent 1592858 commit 0ce5011
Show file tree
Hide file tree
Showing 8 changed files with 94 additions and 25 deletions.
10 changes: 8 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
set (CMAKE_CXX_STANDARD 20)
22 changes: 22 additions & 0 deletions CppProperties.json
Original file line number Diff line number Diff line change
@@ -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"
}
]
}
7 changes: 6 additions & 1 deletion src/client/Latite.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -30,6 +30,11 @@ BOOL WINAPI DllMain(
if (fdwReason == DLL_PROCESS_ATTACH) {
CloseHandle(CreateThread(nullptr, 0, reinterpret_cast<LPTHREAD_START_ROUTINE>(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.
}

Expand Down
2 changes: 1 addition & 1 deletion src/client/module/ModuleManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@

class ModuleManager : public FeatureManager<Module> {
public:

~ModuleManager() = default;
};
34 changes: 21 additions & 13 deletions src/util/Logger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
25 changes: 24 additions & 1 deletion src/util/Logger.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,30 @@
#pragma once
#include <string_view>
#include <format>

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<typename... Args>
extern inline void info(std::string_view fmt, Args&&... args) {
logInternal(Level::Info, std::format(fmt, std::forward<Args>(args)...));
}

template<typename... Args>
extern inline void warn(std::string_view fmt, Args&&... args) {
logInternal(Level::Warn, std::format(fmt, std::forward<Args>(args)...));
}

template<typename... Args>
extern inline void fatal(std::string_view fmt, Args&&... args) {
logInternal(Level::Fatal, std::format(fmt, std::forward<Args>(args)...));
}
}
14 changes: 9 additions & 5 deletions src/util/Util.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "Util.h"
#include "pch.h"

std::wstring util::getRoamingPath()
std::filesystem::path util::getRoamingPath()
{
char* env;
size_t size;
Expand All @@ -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)
Expand All @@ -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<int>(ws.size() + 1), 0, 0, 0, 0);
char* buf = new char[len];
WideCharToMultiByte(CP_ACP, 0, ws.c_str(), static_cast<int>(ws.size() + 1), buf, len, 0, 0);
std::string r(buf);
delete[] buf;
return r;
}
5 changes: 3 additions & 2 deletions src/util/Util.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
#pragma once
#include <string>
#include <filesystem>

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);
}

0 comments on commit 0ce5011

Please sign in to comment.