From d61ef25cf86c40cd96b0140528825ded3e1446ad Mon Sep 17 00:00:00 2001 From: echo Date: Thu, 27 Mar 2025 11:48:02 +0000 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20src/Main.cpp?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Main.cpp | 245 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 245 insertions(+) diff --git a/src/Main.cpp b/src/Main.cpp index 17fb5e1..54c7287 100644 --- a/src/Main.cpp +++ b/src/Main.cpp @@ -1,8 +1,231 @@ #include +#include + +#include +#include +#include + +#include +#include // PlayerPuppet 的父类 + +#include +#include + +#include +#include + +using namespace std; + +RED4ext::PluginHandle m_handle; +const RED4ext::Sdk* m_sdk; + + +/* + * To run this plugin you need to load it with RED4ext (https://github.com/WopsS/RED4ext). + */ + +void PrintGameTime(RED4ext::IScriptable* aContext, RED4ext::CStackFrame* aFrame, void* aOut, int64_t a4) +{ + RED4EXT_UNUSED_PARAMETER(aContext); + RED4EXT_UNUSED_PARAMETER(aFrame); + RED4EXT_UNUSED_PARAMETER(aOut); + RED4EXT_UNUSED_PARAMETER(a4); + + /* + * Access the property of 'GameTime' instance. + */ + + auto rtti = RED4ext::CRTTISystem::Get(); + auto gameTimeCls = rtti->GetClass("GameTime"); + + RED4ext::GameTime gameTime; + RED4ext::ExecuteFunction("gameTimeSystem", "GetGameTime", &gameTime); + + auto secondsProp = gameTimeCls->GetProperty("seconds"); + auto seconds = secondsProp->GetValue(&gameTime); + std::cout << "Seconds=" << seconds; + + seconds += RED4ext::GameTime::OneDay; + + secondsProp->SetValue(&gameTime, seconds); + std::cout << "Seconds=" << seconds; + + seconds = secondsProp->GetValue(&gameTime); + std::cout << "Seconds=" << seconds; +} + +void IsPlayerCrouched(RED4ext::IScriptable* aContext, RED4ext::CStackFrame* aFrame, void* aOut, int64_t a4) +{ + RED4EXT_UNUSED_PARAMETER(aContext); + RED4EXT_UNUSED_PARAMETER(aFrame); + RED4EXT_UNUSED_PARAMETER(aOut); + RED4EXT_UNUSED_PARAMETER(a4); + + /* + * Check if the player is crouched. + */ + RED4ext::ScriptGameInstance gameInstance; + RED4ext::Handle handle; + RED4ext::ExecuteGlobalFunction("GetPlayer;GameInstance", &handle, gameInstance); + + if (handle) + { + auto rtti = RED4ext::CRTTISystem::Get(); + auto playerPuppetCls = rtti->GetClass("PlayerPuppet"); + auto inCrouch = playerPuppetCls->GetProperty("inCrouch"); + auto value = inCrouch->GetValue(handle.instance); + + std::cout << "inCourch=" << std::boolalpha << value; + } +} + +void DoSomethingWithUISystem(RED4ext::IScriptable* aContext, RED4ext::CStackFrame* aFrame, void* aOut, int64_t a4) +{ + RED4EXT_UNUSED_PARAMETER(aContext); + RED4EXT_UNUSED_PARAMETER(aFrame); + RED4EXT_UNUSED_PARAMETER(aOut); + RED4EXT_UNUSED_PARAMETER(a4); + + RED4ext::ScriptGameInstance gameInstance; + RED4ext::Handle uiManager; + RED4ext::ExecuteFunction("ScriptGameInstance", "GetUISystem", &uiManager, &gameInstance); +} + +void PrintScannerStatus(RED4ext::IScriptable* aContext, RED4ext::CStackFrame* aFrame, void* aOut, int64_t a4) +{ + RED4EXT_UNUSED_PARAMETER(aContext); + RED4EXT_UNUSED_PARAMETER(aFrame); + RED4EXT_UNUSED_PARAMETER(aOut); + RED4EXT_UNUSED_PARAMETER(a4); + + RED4ext::ScriptGameInstance gameInstance; + RED4ext::Handle handle; + RED4ext::ExecuteGlobalFunction("GetPlayer;GameInstance", &handle, gameInstance); + + if (handle) + { + auto rtti = RED4ext::CRTTISystem::Get(); + auto playerPuppetCls = rtti->GetClass("PlayerPuppet"); + auto getHudManagerFunc = playerPuppetCls->GetFunction("GetHudManager"); + + RED4ext::Handle hudManager; + RED4ext::ExecuteFunction(handle, getHudManagerFunc, &hudManager); + + auto hudManagerCls = rtti->GetClass("HUDManager"); + + auto activeModeProp = hudManagerCls->GetProperty("activeMode"); + auto activeMode = activeModeProp->GetValue(hudManager); + + auto stateProp = hudManagerCls->GetProperty("state"); + auto state = stateProp->GetValue(hudManager); + + auto uiScannerVisibleProp = hudManagerCls->GetProperty("uiScannerVisible"); + auto uiScannerVisible = uiScannerVisibleProp->GetValue(hudManager); + + std::cout << std::boolalpha << "activeMode=" << activeMode << " state=" << state + << " uiScannerVisible=" << uiScannerVisible; + } +} + +void BackgroundTask() +{ + while (true) + { + // 你的后台任务代码 + std::this_thread::sleep_for(std::chrono::seconds(5 * 1000)); + m_sdk->logger->Info(m_handle, "BackgroundTask"); + cout << "task runing" << endl; + } +} + +RED4EXT_C_EXPORT bool RED4EXT_CALL BaseInit_OnEnter(RED4ext::CGameApplication* aApp) +{ + m_sdk->logger->Info(m_handle, "开始初始化"); + return true; +} + +RED4EXT_C_EXPORT bool RED4EXT_CALL BaseInit_OnUpdate(RED4ext::CGameApplication* aApp) +{ + m_sdk->logger->Info(m_handle, "初始更新"); + return false; +} + +RED4EXT_C_EXPORT bool RED4EXT_CALL BaseInit_OnExit(RED4ext::CGameApplication* aApp) +{ + m_sdk->logger->Info(m_handle, "初始结束"); + return true; +} + +RED4EXT_C_EXPORT bool RED4EXT_CALL Shutdown_OnUpdate(RED4ext::CGameApplication* aApp) +{ + m_sdk->logger->Info(m_handle, "游戏关闭"); + return false; +} + +// 插件注册函数 +RED4EXT_C_EXPORT bool RED4EXT_CALL RED4ext_PluginRegister(RED4ext::PluginHandle aHandle) +{ + m_sdk->logger->Info(m_handle, "[MyMod] 插件已注册!"); + + // 注册事件监听器 + //RegisterEventListeners(); + return true; +} + +RED4EXT_C_EXPORT void RED4EXT_CALL RegisterTypes() +{ +} + +RED4EXT_C_EXPORT void RED4EXT_CALL PostRegisterTypes() +{ + m_sdk->logger->Info(m_handle, "PostRegisterTypes runing"); + + // 获取游戏系统 + //const auto framework = RED4ext::CGameEngine::Get()->framework; + //auto* gameSystem = RED4ext::CGameEngine::Get()->framework->gameInstance; + + auto rtti = RED4ext::CRTTISystem::Get(); + + m_sdk->logger->Info(m_handle, "PrintGameTime runing"); + { + auto func = RED4ext::CGlobalFunction::Create("PrintGameTime", "PrintGameTime", &PrintGameTime); + rtti->RegisterFunction(func); + } + + m_sdk->logger->Info(m_handle, "IsPlayerCrouched runing"); + { + auto func = RED4ext::CGlobalFunction::Create("IsPlayerCrouched", "IsPlayerCrouched", &IsPlayerCrouched); + rtti->RegisterFunction(func); + } + + m_sdk->logger->Info(m_handle, "DoSomethingWithUISystem runing"); + { + auto func = RED4ext::CGlobalFunction::Create("DoSomethingWithUISystem", "DoSomethingWithUISystem", + &DoSomethingWithUISystem); + rtti->RegisterFunction(func); + } + + m_sdk->logger->Info(m_handle, "PrintScannerStatus runing"); + { + auto func = RED4ext::CGlobalFunction::Create("PrintScannerStatus", "PrintScannerStatus", &PrintScannerStatus); + rtti->RegisterFunction(func); + } + + m_sdk->logger->Info(m_handle, "BackgroundTask begin"); + // 启动后台线程 + std::thread(BackgroundTask).detach(); + m_sdk->logger->Info(m_handle, "BackgroundTask rear"); +} RED4EXT_C_EXPORT bool RED4EXT_CALL Main(RED4ext::PluginHandle aHandle, RED4ext::EMainReason aReason, const RED4ext::Sdk* aSdk) { + RED4EXT_UNUSED_PARAMETER(aHandle); + RED4EXT_UNUSED_PARAMETER(aSdk); + + m_sdk = aSdk; + m_handle = aHandle; + cout << "My mod Main " << endl; switch (aReason) { case RED4ext::EMainReason::Load: @@ -17,6 +240,25 @@ RED4EXT_C_EXPORT bool RED4EXT_CALL Main(RED4ext::PluginHandle aHandle, RED4ext:: * called with "Unload" reason. */ + aSdk->logger->Info(aHandle, "Load my mod"); + aSdk->logger->TraceF(aHandle, "Hello %s!", "World"); + + auto rtti = RED4ext::CRTTISystem::Get(); + + rtti->AddRegisterCallback(RegisterTypes); + aSdk->logger->Info(aHandle, "Load RegisterTypes"); + rtti->AddPostRegisterCallback(PostRegisterTypes); + aSdk->logger->Info(aHandle, "Load PostRegisterTypes"); + + RED4ext::GameState initState; + initState.OnEnter = &BaseInit_OnEnter; + initState.OnUpdate = &BaseInit_OnUpdate; + initState.OnExit = &BaseInit_OnExit; + + RED4ext::GameState shutdownState; + shutdownState.OnUpdate = &Shutdown_OnUpdate; + + aSdk->logger->Info(aHandle, "Load over"); break; } case RED4ext::EMainReason::Unload: @@ -24,6 +266,7 @@ RED4EXT_C_EXPORT bool RED4EXT_CALL Main(RED4ext::PluginHandle aHandle, RED4ext:: /* * Here you can free resources you allocated during initalization or during the time your plugin was executed. */ + cout << "Unload my mod" << endl; break; } } @@ -58,6 +301,8 @@ RED4EXT_C_EXPORT void RED4EXT_CALL Query(RED4ext::PluginInfo* aInfo) aInfo->version = RED4EXT_SEMVER(1, 0, 0); aInfo->runtime = RED4EXT_RUNTIME_LATEST; aInfo->sdk = RED4EXT_SDK_LATEST; + + cout << "Query runing" << endl; } RED4EXT_C_EXPORT uint32_t RED4EXT_CALL Supports()