Skip to content

Commit

Permalink
Initail commit
Browse files Browse the repository at this point in the history
  • Loading branch information
muczc1wek committed Sep 6, 2024
0 parents commit 013519d
Show file tree
Hide file tree
Showing 8 changed files with 786 additions and 0 deletions.
417 changes: 417 additions & 0 deletions .gitignore

Large diffs are not rendered by default.

34 changes: 34 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
cmake_minimum_required(VERSION 3.25)
project(zMultilogue VERSION 0.0.1 LANGUAGES CXX)

set(PLUGIN_NAME zMultilogue)

set(CMAKE_CXX_STANDARD 23)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

include(FetchContent)
FetchContent_Declare(
union-api
GIT_REPOSITORY https://gitlab.com/union-framework/union-api.git
GIT_TAG 21508ea42aad44c7c6fea4ae06ffc159ec85e69e
)
FetchContent_Declare(
gothic-api
GIT_REPOSITORY https://gitlab.com/union-framework/gothic-api.git
GIT_TAG a6a66de7386f990c30fec41019d27053372058f2
)
FetchContent_MakeAvailable(union-api)
FetchContent_MakeAvailable(gothic-api)

add_library(${PLUGIN_NAME} SHARED)
target_link_libraries(${PLUGIN_NAME} PRIVATE union_api_lib gothic_api)
target_include_directories(${PLUGIN_NAME} PRIVATE src)
target_include_directories(${PLUGIN_NAME} PRIVATE BEFORE ${CMAKE_CURRENT_SOURCE_DIR}/userapi)
file(GLOB_RECURSE SOURCES "${CMAKE_CURRENT_SOURCE_DIR}/src/**.cpp")
target_sources(${PLUGIN_NAME} PRIVATE ${SOURCES})

target_compile_options(${PLUGIN_NAME} PRIVATE
/permissive- /std:c++latest /Zc:__cplusplus /Zc:preprocessor /utf-8
/experimental:external /external:W0 /external:anglebrackets
/external:I ${CMAKE_BINARY_DIR}/_deps/gothic-api-src
)
41 changes: 41 additions & 0 deletions CMakePresets.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
"version": 3,
"configurePresets": [
{
"name": "windows-base",
"hidden": true,
"generator": "Ninja",
"binaryDir": "${sourceDir}/out/build/${presetName}",
"installDir": "${sourceDir}/out/install/${presetName}",
"cacheVariables": {
"CMAKE_C_COMPILER": "cl.exe",
"CMAKE_CXX_COMPILER": "cl.exe"
},
"condition": {
"type": "equals",
"lhs": "${hostSystemName}",
"rhs": "Windows"
}
},
{
"name": "x86-debug",
"displayName": "x86 Debug",
"inherits": "windows-base",
"architecture": {
"value": "x86",
"strategy": "external"
},
"cacheVariables": {
"CMAKE_BUILD_TYPE": "Debug"
}
},
{
"name": "x86-release",
"displayName": "x86 Release",
"inherits": "x86-debug",
"cacheVariables": {
"CMAKE_BUILD_TYPE": "Release"
}
}
]
}
Empty file added src/Gothic/Hooks.hpp
Empty file.
101 changes: 101 additions & 0 deletions src/NH/Logger.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
#include "Logger.h"

#include <ZenGin/zGothicAPI.h>

#include <utility>

namespace NH {
String LoggerLevelToString(LoggerLevel level) {
String values[] = {
"NONE",
"FATAL",
"ERROR",
"WARN",
"INFO",
"DEBUG",
"TRACE"
};
return values[(size_t) level];
}

LoggerLevel StringToLoggerLevel(String level) {
String values[] = {
"NONE",
"FATAL",
"ERROR",
"WARN",
"INFO",
"DEBUG",
"TRACE"
};
size_t count = sizeof(values) / sizeof(String);
for (size_t i = 0; i < count; i++) {
if (level.MakeUpper() == values[i]) {
return (LoggerLevel) i;
}
}
return LoggerLevel::None;
}

void Logger::Message(LoggerLevel level, const String &message) {
if (level == LoggerLevel::None) {
return;
}

size_t count = m_Adapters.GetCount();
for (size_t i = 0; i < count; i++) {
ILoggerAdapter *adapter = m_Adapters[i];
if (adapter->CanLog(level)) {
adapter->Message(level, m_LoggerName, message);
}
}
}

UnionConsoleLoggerAdapter::UnionConsoleLoggerAdapter(LoggerLevel level) : ILoggerAdapter(level) {
SetLevelColor(LoggerLevel::Fatal, Color{"\x1B[31m", "\x1B[41;1m\x1B[37;1m", "\x1B[41m\x1B[37;1m"});
SetLevelColor(LoggerLevel::Error, Color{"\x1B[31m", "\x1B[41;1m\x1B[37;1m", "\x1B[31;1m"});
SetLevelColor(LoggerLevel::Warn, Color{"\x1B[33m", "\x1B[43;1m\x1B[37m", "\x1B[0m\x1B[33;1m"});
SetLevelColor(LoggerLevel::Info, Color{"\x1b[37m", "\x1B[47;1m\x1B[30m", "\x1B[0m"});
SetLevelColor(LoggerLevel::Debug, Color{"\x1B[32m", "\x1B[42;1m\x1B[37;1m", "\x1B[0m\x1B[32;1m"});
SetLevelColor(LoggerLevel::Trace, Color{"\x1B[30;1m", "\x1B[47;1m\x1B[30m", "\x1B[37m"});
}

void UnionConsoleLoggerAdapter::SetLevelColor(LoggerLevel level, Color color) {
m_ColorTable[(size_t) level] = std::move(color);
}

void UnionConsoleLoggerAdapter::Message(LoggerLevel level, const String &channel, const String &message) {
Color color = m_ColorTable[(size_t) level];

String::Format("\x1B[0m{0} {1} \x1B[0m {2}[{3}]\x1B[0m {4}{5}\x1B[0m",
color.Level, LoggerLevelToString(level),
color.Channel, channel,
color.Message, message)
.StdPrintLine();
}

void ZSpyLoggerAdapter::Message(LoggerLevel level, const String &channel, const String &message) {
String formattedMessage = String(m_Prefix) + ":\t" + channel + ": [" + LoggerLevelToString(level) + "]" + message;

switch (GetGameVersion()) {
case Engine_G2A:
Gothic_II_Addon::zerr->Message(Gothic_II_Addon::zSTRING(formattedMessage.ToChar()));
break;
}
}

LoggerFactory *LoggerFactory::s_Instance = null;

Logger *LoggerFactory::Create(const String &name, LoggerLevel level) {
auto *logger = new NH::Logger(name, {
new NH::UnionConsoleLoggerAdapter(level),
new NH::ZSpyLoggerAdapter(level, "E")
});
m_Loggers.Insert(logger);
return logger;
}

Logger *CreateLogger(const String &name) {
return LoggerFactory::GetInstance()->Create(name, LoggerLevel::Trace);
}
}
173 changes: 173 additions & 0 deletions src/NH/Logger.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
#pragma once

#include <Union/String.h>
#include <Union/Array.h>

namespace NH
{
using String = Union::StringUTF8;

enum class LoggerLevel : size_t
{
None = 0,
Fatal = 1,
Error = 2,
Warn = 3,
Info = 4,
Debug = 5,
Trace = 6
};

String LoggerLevelToString(LoggerLevel level);

LoggerLevel StringToLoggerLevel(String level);

class Logger;

class ILoggerAdapter
{
friend Logger;

protected:
LoggerLevel m_Level;

explicit ILoggerAdapter(LoggerLevel level = LoggerLevel::Debug) : m_Level(level) {}

bool CanLog(LoggerLevel level) const { return level <= m_Level; }

virtual void Message(LoggerLevel level, const String& channel, const String& message) = 0;

public:
void SetLoggerLevel(LoggerLevel level) { m_Level = level; };
};

class Logger
{
private:
const String m_LoggerName;
Union::Array<ILoggerAdapter*> m_Adapters;

public:
explicit Logger(const String& name) : m_LoggerName(name) {};

explicit Logger(const String& name, Union::Array<ILoggerAdapter*> adapters) : m_LoggerName(name), m_Adapters(adapters) {};

void Message(LoggerLevel level, const String& message);

template<typename... Args>
void Message(LoggerLevel level, const char* format, Args... args) { Message(level, String::Format(format, args...)); }

void Fatal(const String& message) { Message(LoggerLevel::Fatal, message); };

template<typename... Args>
void Fatal(const char* format, Args... args) { Fatal(String::Format(format, args...)); }

void Error(const String& message) { Message(LoggerLevel::Error, message); };

template<typename... Args>
void Error(const char* format, Args... args) { Error(String::Format(format, args...)); }

void Warning(const String& message) { Message(LoggerLevel::Warn, message); };

template<typename... Args>
void Warning(const char* format, Args... args) { Warning(String::Format(format, args...)); }

void Info(const String& message) { Message(LoggerLevel::Info, message); };

template<typename... Args>
void Info(const char* format, Args... args) { Info(String::Format(format, args...)); }

void Debug(const String& message) { Message(LoggerLevel::Debug, message); };

template<typename... Args>
void Debug(const char* format, Args... args) { Debug(String::Format(format, args...)); }

void Trace(const String& message) { Message(LoggerLevel::Trace, message); };

template<typename... Args>
void Trace(const char* format, Args... args) { Trace(String::Format(format, args...)); }

void SetLoggerLevel(LoggerLevel level)
{
for (int i = 0; i < m_Adapters.GetCount(); i++)
{
m_Adapters[i]->SetLoggerLevel(level);
}
}

Union::Array<ILoggerAdapter*> GetAdapters() const { return m_Adapters; };

template <class T>
T* GetAdapter() const
{
for (int i = 0; i < m_Adapters.GetCount(); i++)
{
ILoggerAdapter* adapter = m_Adapters[i];
T* ptr = dynamic_cast<T*>(adapter);
if (ptr)
{
return ptr;
}
}
return nullptr;
}
};

class UnionConsoleLoggerAdapter : public ILoggerAdapter
{
public:
struct Color
{
String Channel;
String Level;
String Message;
};

explicit UnionConsoleLoggerAdapter(LoggerLevel level = LoggerLevel::Debug);

void SetLevelColor(LoggerLevel level, Color color);

protected:
void Message(LoggerLevel level, const String& channel, const String& message) override;

private:
Color m_ColorTable[7];
};

class ZSpyLoggerAdapter : public ILoggerAdapter
{
private:
String m_Prefix;

public:
explicit ZSpyLoggerAdapter(LoggerLevel level = LoggerLevel::Debug, const String& prefix = "N") : ILoggerAdapter(level), m_Prefix(prefix) {}

protected:
void Message(LoggerLevel level, const String& channel, const String& message) override;
};

class LoggerFactory
{
private:
static LoggerFactory* s_Instance;
Union::Array<Logger*> m_Loggers{};

LoggerFactory() = default;

public:
Logger* Create(const String& name, LoggerLevel level = LoggerLevel::Trace);

Union::Array<Logger*> GetLoggers() { return m_Loggers.Share(); };

static LoggerFactory* GetInstance()
{
if (!s_Instance)
{
s_Instance = new LoggerFactory();
}
return s_Instance;
}
};

Logger* CreateLogger(const String& name);
}
19 changes: 19 additions & 0 deletions src/Plugin.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Disable macro redefinition warning
#pragma warning(disable: 4005)

#include <Union/Hook.h>
#include <NH/Logger.h>
#include <ZenGin/zGothicAPI.h>

#ifdef __G2A
#define GOTHIC_NAMESPACE Gothic_II_Addon
#define ENGINE Engine_G2A
HOOKSPACE(Gothic_II_Addon, GetGameVersion() == ENGINE);

#include "Plugin.hpp"

#endif

#undef GOTHIC_NAMESPACE
#undef ENGNE
HOOKSPACE(Global, true);
1 change: 1 addition & 0 deletions src/Plugin.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#include <Gothic/Hooks.hpp>

0 comments on commit 013519d

Please sign in to comment.