Log RED4ext lifecycle callbacks
This commit is contained in:
@@ -9,6 +9,7 @@
|
|||||||
#include <windows.h>
|
#include <windows.h>
|
||||||
|
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
|
#include <cstdint>
|
||||||
#include <filesystem>
|
#include <filesystem>
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include <iomanip>
|
#include <iomanip>
|
||||||
@@ -18,13 +19,74 @@
|
|||||||
|
|
||||||
namespace RED4ext::v1
|
namespace RED4ext::v1
|
||||||
{
|
{
|
||||||
|
struct Logger
|
||||||
|
{
|
||||||
|
void (*Trace)(PluginHandle aHandle, const char* aMessage);
|
||||||
|
void (*TraceF)(PluginHandle aHandle, const char* aFormat, ...);
|
||||||
|
void (*TraceW)(PluginHandle aHandle, const wchar_t* aMessage);
|
||||||
|
void (*TraceWF)(PluginHandle aHandle, const wchar_t* aFormat, ...);
|
||||||
|
void (*Debug)(PluginHandle aHandle, const char* aMessage);
|
||||||
|
void (*DebugF)(PluginHandle aHandle, const char* aFormat, ...);
|
||||||
|
void (*DebugW)(PluginHandle aHandle, const wchar_t* aMessage);
|
||||||
|
void (*DebugWF)(PluginHandle aHandle, const wchar_t* aFormat, ...);
|
||||||
|
void (*Info)(PluginHandle aHandle, const char* aMessage);
|
||||||
|
void (*InfoF)(PluginHandle aHandle, const char* aFormat, ...);
|
||||||
|
void (*InfoW)(PluginHandle aHandle, const wchar_t* aMessage);
|
||||||
|
void (*InfoWF)(PluginHandle aHandle, const wchar_t* aFormat, ...);
|
||||||
|
void (*Warn)(PluginHandle aHandle, const char* aMessage);
|
||||||
|
void (*WarnF)(PluginHandle aHandle, const char* aFormat, ...);
|
||||||
|
void (*WarnW)(PluginHandle aHandle, const wchar_t* aMessage);
|
||||||
|
void (*WarnWF)(PluginHandle aHandle, const wchar_t* aFormat, ...);
|
||||||
|
void (*Error)(PluginHandle aHandle, const char* aMessage);
|
||||||
|
void (*ErrorF)(PluginHandle aHandle, const char* aFormat, ...);
|
||||||
|
void (*ErrorW)(PluginHandle aHandle, const wchar_t* aMessage);
|
||||||
|
void (*ErrorWF)(PluginHandle aHandle, const wchar_t* aFormat, ...);
|
||||||
|
void (*Critical)(PluginHandle aHandle, const char* aMessage);
|
||||||
|
void (*CriticalF)(PluginHandle aHandle, const char* aFormat, ...);
|
||||||
|
void (*CriticalW)(PluginHandle aHandle, const wchar_t* aMessage);
|
||||||
|
void (*CriticalWF)(PluginHandle aHandle, const wchar_t* aFormat, ...);
|
||||||
|
};
|
||||||
|
|
||||||
|
struct GameState
|
||||||
|
{
|
||||||
|
bool (*OnEnter)(void* aApp);
|
||||||
|
bool (*OnUpdate)(void* aApp);
|
||||||
|
bool (*OnExit)(void* aApp);
|
||||||
|
};
|
||||||
|
|
||||||
|
struct GameStates
|
||||||
|
{
|
||||||
|
bool (*Add)(PluginHandle aHandle, uint32_t aType, GameState* aState);
|
||||||
|
};
|
||||||
|
|
||||||
|
struct Scripts
|
||||||
|
{
|
||||||
|
bool (*Add)(PluginHandle aHandle, const wchar_t* aPath);
|
||||||
|
bool (*RegisterNeverRefType)(const char* aType);
|
||||||
|
bool (*RegisterMixedRefType)(const char* aType);
|
||||||
|
};
|
||||||
|
|
||||||
struct Sdk;
|
struct Sdk;
|
||||||
|
|
||||||
|
struct Sdk
|
||||||
|
{
|
||||||
|
SemVer* runtime;
|
||||||
|
Logger* logger;
|
||||||
|
void* hooking;
|
||||||
|
GameStates* gameStates;
|
||||||
|
Scripts* scripts;
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
namespace
|
namespace
|
||||||
{
|
{
|
||||||
|
constexpr uint32_t kGameStateRunning = 2;
|
||||||
|
|
||||||
HMODULE gModule = nullptr;
|
HMODULE gModule = nullptr;
|
||||||
std::mutex gLogMutex;
|
std::mutex gLogMutex;
|
||||||
|
RED4ext::v1::PluginHandle gHandle = nullptr;
|
||||||
|
const RED4ext::v1::Sdk* gSdk = nullptr;
|
||||||
|
uint32_t gUpdateLogCount = 0;
|
||||||
|
|
||||||
std::filesystem::path GetLogPath()
|
std::filesystem::path GetLogPath()
|
||||||
{
|
{
|
||||||
@@ -56,6 +118,17 @@ void Log(std::string_view aMessage)
|
|||||||
out << std::put_time(&localTime, "%Y-%m-%d %H:%M:%S") << " " << aMessage << "\n";
|
out << std::put_time(&localTime, "%Y-%m-%d %H:%M:%S") << " " << aMessage << "\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void LogRed4ext(std::string_view aMessage)
|
||||||
|
{
|
||||||
|
Log(aMessage);
|
||||||
|
|
||||||
|
if (gSdk && gSdk->logger && gSdk->logger->Info && gHandle)
|
||||||
|
{
|
||||||
|
std::string text(aMessage);
|
||||||
|
gSdk->logger->Info(gHandle, text.c_str());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const char* ReasonToString(RED4ext::v1::EMainReason aReason)
|
const char* ReasonToString(RED4ext::v1::EMainReason aReason)
|
||||||
{
|
{
|
||||||
switch (aReason)
|
switch (aReason)
|
||||||
@@ -68,6 +141,41 @@ const char* ReasonToString(RED4ext::v1::EMainReason aReason)
|
|||||||
|
|
||||||
return "Unknown";
|
return "Unknown";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool OnRunningEnter(void* aApp)
|
||||||
|
{
|
||||||
|
std::ostringstream line;
|
||||||
|
line << "GameState Running OnEnter app=" << aApp;
|
||||||
|
LogRed4ext(line.str());
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool OnRunningUpdate(void* aApp)
|
||||||
|
{
|
||||||
|
if (gUpdateLogCount < 5)
|
||||||
|
{
|
||||||
|
std::ostringstream line;
|
||||||
|
line << "GameState Running OnUpdate app=" << aApp << " count=" << gUpdateLogCount;
|
||||||
|
LogRed4ext(line.str());
|
||||||
|
++gUpdateLogCount;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool OnRunningExit(void* aApp)
|
||||||
|
{
|
||||||
|
std::ostringstream line;
|
||||||
|
line << "GameState Running OnExit app=" << aApp;
|
||||||
|
LogRed4ext(line.str());
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
RED4ext::v1::GameState gRunningState{
|
||||||
|
.OnEnter = OnRunningEnter,
|
||||||
|
.OnUpdate = OnRunningUpdate,
|
||||||
|
.OnExit = OnRunningExit,
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
BOOL APIENTRY DllMain(HMODULE aModule, DWORD aReason, LPVOID)
|
BOOL APIENTRY DllMain(HMODULE aModule, DWORD aReason, LPVOID)
|
||||||
@@ -82,9 +190,24 @@ BOOL APIENTRY DllMain(HMODULE aModule, DWORD aReason, LPVOID)
|
|||||||
RED4EXT_C_EXPORT bool RED4EXT_CALL Main(RED4ext::v1::PluginHandle aHandle, RED4ext::v1::EMainReason aReason,
|
RED4EXT_C_EXPORT bool RED4EXT_CALL Main(RED4ext::v1::PluginHandle aHandle, RED4ext::v1::EMainReason aReason,
|
||||||
const RED4ext::v1::Sdk* aSdk)
|
const RED4ext::v1::Sdk* aSdk)
|
||||||
{
|
{
|
||||||
|
gHandle = aHandle;
|
||||||
|
gSdk = aSdk;
|
||||||
|
|
||||||
std::ostringstream line;
|
std::ostringstream line;
|
||||||
line << "EdgeWeightGPS Main reason=" << ReasonToString(aReason) << " handle=" << aHandle << " sdk=" << aSdk;
|
line << "EdgeWeightGPS Main reason=" << ReasonToString(aReason) << " handle=" << aHandle << " sdk=" << aSdk;
|
||||||
Log(line.str());
|
if (aSdk && aSdk->runtime)
|
||||||
|
{
|
||||||
|
line << " runtime=" << static_cast<uint32_t>(aSdk->runtime->major) << "." << aSdk->runtime->minor << "."
|
||||||
|
<< aSdk->runtime->patch;
|
||||||
|
}
|
||||||
|
LogRed4ext(line.str());
|
||||||
|
|
||||||
|
if (aReason == RED4ext::v1::EMainReason::Load && aSdk && aSdk->gameStates && aSdk->gameStates->Add)
|
||||||
|
{
|
||||||
|
const auto added = aSdk->gameStates->Add(aHandle, kGameStateRunning, &gRunningState);
|
||||||
|
LogRed4ext(added ? "registered Running game-state callbacks" : "failed to register Running game-state callbacks");
|
||||||
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user