feat: added native manager header

This commit is contained in:
2025-11-18 19:51:02 +01:00
parent 057a34dbb1
commit bf6caea9ba

76
src/detail/fxn.hpp Normal file
View File

@@ -0,0 +1,76 @@
#pragma once
#include <functional>
#include <cstdint>
#include <fxn/context.hpp>
namespace fxn
{
using NativeHandler = void(*)(fxn::NativeContext*);
using NativeHash = std::uint64_t;
using NativeHook = std::function<bool(fxn::NativeContext*, bool& callOriginal)>;
class FxnManager
{
private:
struct Impl;
Impl* m_Impl;
public:
static FxnManager& GetInstance();
static void Destroy();
public:
void Initialize();
void Shutdown();
public:
const NativeHandler& GetNativeHandler(const NativeHash hash) const;
void RegisterNativeHandler(const NativeHash hash, const NativeHandler handler);
public:
void RegisterNativeHook(const NativeHash hash, const NativeHook& hook);
void UnregisterNativeHook(const NativeHash hash);
public:
bool Invoke(fxn::NativeContext* context);
bool Schedule(fxn::NativeContext* context, bool waitForCompletion = true);
public:
template<typename R, typename... Args>
R Invoke(const NativeHash hash, Args&&... args)
{
fxn::NativeContext context(hash);
(context.PushArgument(std::forward<Args>(args)), ...);
if (!this->Invoke(&context))
{
throw std::runtime_error("Failed to invoke native with hash: " + std::to_string(hash));
}
if constexpr (!std::is_same_v<R, void>)
{
return context.GetResult<R>();
}
}
template<typename R, typename... Args>
R Schedule(const NativeHash hash, Args&&... args)
{
fxn::NativeContext context(hash);
(context.PushArgument(std::forward<Args>(args)), ...);
if (!this->Schedule(&context, true))
{
throw std::runtime_error("Failed to schedule native with hash: " + std::to_string(hash));
}
if constexpr (!std::is_same_v<R, void>)
{
return context.GetResult<R>();
}
}
};
}