feat: added native manager header
This commit is contained in:
76
src/detail/fxn.hpp
Normal file
76
src/detail/fxn.hpp
Normal 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>();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user