From bf6caea9baa7c1b463e766dabbf7e08523582948 Mon Sep 17 00:00:00 2001 From: slayercio Date: Tue, 18 Nov 2025 19:51:02 +0100 Subject: [PATCH] feat: added native manager header --- src/detail/fxn.hpp | 76 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 src/detail/fxn.hpp diff --git a/src/detail/fxn.hpp b/src/detail/fxn.hpp new file mode 100644 index 0000000..f52b7c4 --- /dev/null +++ b/src/detail/fxn.hpp @@ -0,0 +1,76 @@ +#pragma once +#include +#include + +#include + +namespace fxn +{ + using NativeHandler = void(*)(fxn::NativeContext*); + using NativeHash = std::uint64_t; + using NativeHook = std::function; + + 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 + R Invoke(const NativeHash hash, Args&&... args) + { + fxn::NativeContext context(hash); + + (context.PushArgument(std::forward(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) + { + return context.GetResult(); + } + } + + template + R Schedule(const NativeHash hash, Args&&... args) + { + fxn::NativeContext context(hash); + + (context.PushArgument(std::forward(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) + { + return context.GetResult(); + } + } + }; +} \ No newline at end of file