更新 src/Main.cpp
Some checks failed
Build Workflow / Build (${{ matrix.config }}) (Debug) (push) Has been cancelled
Build Workflow / Build (${{ matrix.config }}) (Release) (push) Has been cancelled

This commit is contained in:
echo 2025-03-27 11:48:02 +00:00
parent bfe0e905d9
commit d61ef25cf8

View File

@ -1,8 +1,231 @@
#include <RED4ext/RED4ext.hpp>
#include <iostream>
#include <RED4ext/RED4ext.hpp>
#include <RED4ext/Scripting/Natives/GameTime.hpp>
#include <RED4ext/Scripting/Natives/ScriptGameInstance.hpp>
#include <RED4ext/Scripting/IScriptable.hpp>
#include <RED4ext/Scripting/Natives/Generated/ent/Entity.hpp> // PlayerPuppet 的父类
#include <RED4ext/Scripting/Natives/Generated/game/IEntitySpawnerEventsBroadcaster.hpp>
#include <RED4ext/GameEngine.hpp>
#include <RED4ext/GameStates.hpp>
#include <RED4ext/Callback.hpp>
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<uint32_t>(&gameTime);
std::cout << "Seconds=" << seconds;
seconds += RED4ext::GameTime::OneDay;
secondsProp->SetValue<uint32_t>(&gameTime, seconds);
std::cout << "Seconds=" << seconds;
seconds = secondsProp->GetValue<uint32_t>(&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<RED4ext::IScriptable> 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<bool>(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<RED4ext::IScriptable> 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<RED4ext::IScriptable> 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<RED4ext::IScriptable> hudManager;
RED4ext::ExecuteFunction(handle, getHudManagerFunc, &hudManager);
auto hudManagerCls = rtti->GetClass("HUDManager");
auto activeModeProp = hudManagerCls->GetProperty("activeMode");
auto activeMode = activeModeProp->GetValue<int32_t>(hudManager);
auto stateProp = hudManagerCls->GetProperty("state");
auto state = stateProp->GetValue<int32_t>(hudManager);
auto uiScannerVisibleProp = hudManagerCls->GetProperty("uiScannerVisible");
auto uiScannerVisible = uiScannerVisibleProp->GetValue<bool>(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()