More progress on adding invokation API
This commit is contained in:
@@ -1,11 +1,11 @@
|
||||
#include "AudioModule.h"
|
||||
#include "AudioSceneSystem.h"
|
||||
|
||||
#include "Nuake/Core/Maths.h"
|
||||
#include "Nuake/Scene/SceneSystems.h"
|
||||
#include "Nuake/Modules/ModuleDB.h"
|
||||
#include <Nuake/Core/Logger.h>
|
||||
|
||||
void PlayAudio(int id, float volume)
|
||||
void PlayAudio(int id, Nuake::Matrix4 volume)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
@@ -43,11 +43,24 @@ void ExampleModule_Startup()
|
||||
using namespace Nuake;
|
||||
|
||||
auto& module = ModuleDB::Get().RegisterModule<ExampleModule>();
|
||||
module.instance = module.Resolve().construct();
|
||||
|
||||
module.Description = "This is an example module";
|
||||
|
||||
// This is to expose functions to the rest of the engine
|
||||
module.BindFunction<ExampleFunction>("ExampleFunction");
|
||||
module.BindFunction<ExampleModuleLog>("ExampleModuleLog", "hi");
|
||||
module.Invoke("ExampleModuleLog", "Hello World");
|
||||
|
||||
entt::id_type typeId = entt::hashed_string("ExampleModuleLog");
|
||||
entt::meta_type metaType = entt::resolve(typeId);
|
||||
auto resolvedFunc = metaType.func(typeId);
|
||||
|
||||
if (resolvedFunc)
|
||||
{
|
||||
resolvedFunc.invoke(module.instance, std::string("Hello has been invoked!"));
|
||||
}
|
||||
|
||||
|
||||
Logger::Log("ExampleModule exposed API:", "Module", VERBOSE);
|
||||
|
||||
@@ -56,9 +69,12 @@ void ExampleModule_Startup()
|
||||
{
|
||||
const std::string_view returnType = func.ret().info().name();
|
||||
const std::string_view funcName = module.GetTypeName(id);
|
||||
|
||||
|
||||
if (funcName == "ExampleModuleLog")
|
||||
{
|
||||
func.invoke(module.instance, "Hi");
|
||||
}
|
||||
std::string msg = std::string(returnType) + " " + std::string(funcName) + "(";
|
||||
|
||||
auto argNames = module.GetFuncArgNames(id);
|
||||
std::vector<std::string_view> args;
|
||||
for (int i = 0; i < func.arity(); i++)
|
||||
|
||||
@@ -5,20 +5,21 @@
|
||||
|
||||
#include <entt/entt.hpp>
|
||||
|
||||
#define NUAKEMODULE(name) \
|
||||
#define NUAKEMODULE(moduleName) \
|
||||
using TypeNameMap = std::map<entt::id_type, std::string>; \
|
||||
\
|
||||
struct name \
|
||||
struct moduleName \
|
||||
{ \
|
||||
const std::string Name = #name; \
|
||||
entt::meta_any instance; \
|
||||
const std::string Name = #moduleName; \
|
||||
std::string Description = "No Description"; \
|
||||
entt::meta_factory<name> ModuleFactory = entt::meta<name>(); \
|
||||
entt::meta_factory<moduleName> ModuleFactory = entt::meta<moduleName>(); \
|
||||
TypeNameMap TypeNames; \
|
||||
\
|
||||
std::map<entt::id_type, std::vector<std::string>> FuncArgNames; \
|
||||
\
|
||||
template<auto T, typename... Args> \
|
||||
inline name& BindFunction(const std::string& name, Args... argNames) \
|
||||
inline moduleName& BindFunction(const std::string& name, Args... argNames) \
|
||||
{ \
|
||||
entt::id_type typeId = entt::hashed_string(name.c_str()); \
|
||||
TypeNames[typeId] = name; \
|
||||
@@ -29,8 +30,23 @@ struct name \
|
||||
\
|
||||
auto Resolve() \
|
||||
{ \
|
||||
return entt::resolve<name>(); \
|
||||
return entt::resolve<moduleName>(); \
|
||||
} \
|
||||
\
|
||||
template<typename... Args> \
|
||||
auto Invoke(const std::string& funcName, Args... args) \
|
||||
{ \
|
||||
auto reflection = Resolve();\
|
||||
for (auto [id, func] : reflection.func())\
|
||||
{\
|
||||
const std::string_view returnType = func.ret().info().name();\
|
||||
const std::string_view funcTypeName = GetTypeName(id);\
|
||||
if (funcName == funcTypeName)\
|
||||
{\
|
||||
return func.invoke(instance, args...);\
|
||||
}\
|
||||
} \
|
||||
} \
|
||||
\
|
||||
std::vector<std::string> GetFuncArgNames(entt::id_type id) \
|
||||
{ \
|
||||
@@ -56,7 +72,7 @@ std::vector<std::string> GetAllFuncNames() \
|
||||
} \
|
||||
return "Unknown"; \
|
||||
}\
|
||||
};
|
||||
};
|
||||
|
||||
namespace Nuake
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user