Add RED4ext logging shim

This commit is contained in:
2026-06-19 21:44:46 -05:00
parent e3c6087738
commit 1de2f4ab46
11 changed files with 219 additions and 0 deletions
+24
View File
@@ -0,0 +1,24 @@
cmake_minimum_required(VERSION 3.21)
project(EdgeWeightGPS VERSION 0.1.0 LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 23)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
add_library(EdgeWeightGPS SHARED src/Main.cpp)
target_include_directories(
EdgeWeightGPS
PRIVATE
"${CMAKE_CURRENT_LIST_DIR}/include"
"${CMAKE_CURRENT_LIST_DIR}/../../vendor/RED4ext.SDK/include"
)
target_compile_definitions(EdgeWeightGPS PRIVATE WIN32_LEAN_AND_MEAN NOMINMAX)
target_compile_options(EdgeWeightGPS PRIVATE -fmax-errors=12)
set_target_properties(
EdgeWeightGPS
PROPERTIES
PREFIX ""
OUTPUT_NAME "EdgeWeightGPS"
)
@@ -0,0 +1,12 @@
set(CMAKE_SYSTEM_NAME Windows)
set(CMAKE_SYSTEM_PROCESSOR x86_64)
set(CMAKE_C_COMPILER x86_64-w64-mingw32-gcc)
set(CMAKE_CXX_COMPILER x86_64-w64-mingw32-g++)
set(CMAKE_RC_COMPILER x86_64-w64-mingw32-windres)
set(CMAKE_FIND_ROOT_PATH /usr/x86_64-w64-mingw32)
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY)
@@ -0,0 +1,2 @@
#pragma once
#include <RED4ext/RTTI/ERTTIType.hpp>
@@ -0,0 +1,2 @@
#pragma once
#include <RED4ext/RTTI/IType-inl.hpp>
@@ -0,0 +1,2 @@
#pragma once
#include <RED4ext/RTTI/IType.hpp>
+14
View File
@@ -0,0 +1,14 @@
#pragma once
#include_next <windows.h>
#ifndef _MSC_VER
extern "C" inline char InterlockedExchange8(volatile char* aTarget, char aValue)
{
return __atomic_exchange_n(aTarget, aValue, __ATOMIC_SEQ_CST);
}
extern "C" inline char _InterlockedExchangeAdd8(volatile char* aTarget, char aValue)
{
return __atomic_fetch_add(aTarget, aValue, __ATOMIC_SEQ_CST);
}
#endif
+103
View File
@@ -0,0 +1,103 @@
#include <RED4ext/Api/ApiVersion.hpp>
#include <RED4ext/Api/v1/EMainReason.hpp>
#include <RED4ext/Api/v1/PluginHandle.hpp>
#include <RED4ext/Api/v1/PluginInfo.hpp>
#include <RED4ext/Api/v1/Runtime.hpp>
#include <RED4ext/Api/v1/Version.hpp>
#include <RED4ext/Common.hpp>
#include <windows.h>
#include <chrono>
#include <filesystem>
#include <fstream>
#include <iomanip>
#include <mutex>
#include <sstream>
#include <string_view>
namespace RED4ext::v1
{
struct Sdk;
}
namespace
{
HMODULE gModule = nullptr;
std::mutex gLogMutex;
std::filesystem::path GetLogPath()
{
wchar_t modulePath[MAX_PATH]{};
if (gModule && GetModuleFileNameW(gModule, modulePath, MAX_PATH) > 0)
{
auto path = std::filesystem::path(modulePath);
return path.parent_path() / L"EdgeWeightGPS.log";
}
return std::filesystem::path(L"red4ext") / L"plugins" / L"EdgeWeightGPS" / L"EdgeWeightGPS.log";
}
void Log(std::string_view aMessage)
{
std::lock_guard lock(gLogMutex);
auto now = std::chrono::system_clock::now();
auto time = std::chrono::system_clock::to_time_t(now);
std::tm localTime{};
localtime_s(&localTime, &time);
std::ofstream out(GetLogPath(), std::ios::app);
if (!out)
{
return;
}
out << std::put_time(&localTime, "%Y-%m-%d %H:%M:%S") << " " << aMessage << "\n";
}
const char* ReasonToString(RED4ext::v1::EMainReason aReason)
{
switch (aReason)
{
case RED4ext::v1::EMainReason::Load:
return "Load";
case RED4ext::v1::EMainReason::Unload:
return "Unload";
}
return "Unknown";
}
}
BOOL APIENTRY DllMain(HMODULE aModule, DWORD aReason, LPVOID)
{
if (aReason == DLL_PROCESS_ATTACH)
{
gModule = aModule;
}
return TRUE;
}
RED4EXT_C_EXPORT bool RED4EXT_CALL Main(RED4ext::v1::PluginHandle aHandle, RED4ext::v1::EMainReason aReason,
const RED4ext::v1::Sdk* aSdk)
{
std::ostringstream line;
line << "EdgeWeightGPS Main reason=" << ReasonToString(aReason) << " handle=" << aHandle << " sdk=" << aSdk;
Log(line.str());
return true;
}
RED4EXT_C_EXPORT void RED4EXT_CALL Query(RED4ext::v1::PluginInfo* aInfo)
{
aInfo->name = L"EdgeWeightGPS";
aInfo->author = L"salt+Codex";
aInfo->version = RED4ext::v1::SemVer{0, 1, 0, {0, 0}};
aInfo->runtime = RED4ext::v1::FileVer{3, 0, 80, 51928};
aInfo->sdk = RED4ext::v1::SemVer{RED4EXT_VER_MAJOR, RED4EXT_VER_MINOR, RED4EXT_VER_PATCH, {0, 0}};
}
RED4EXT_C_EXPORT uint32_t RED4EXT_CALL Supports()
{
return RED4EXT_API_VERSION_1;
}