diff --git a/Nuake/Source/Nuake/Modules/AudioModule/AudioModule.cpp b/Nuake/Source/Nuake/Modules/AudioModule/AudioModule.cpp index 3a5cf247..7cb8dee1 100644 --- a/Nuake/Source/Nuake/Modules/AudioModule/AudioModule.cpp +++ b/Nuake/Source/Nuake/Modules/AudioModule/AudioModule.cpp @@ -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 -void PlayAudio(int id, float volume) +void PlayAudio(int id, Nuake::Matrix4 volume) { } diff --git a/Nuake/Source/Nuake/Modules/ExampleModule/ExampleModule.cpp b/Nuake/Source/Nuake/Modules/ExampleModule/ExampleModule.cpp index 40713a95..bbe47f59 100644 --- a/Nuake/Source/Nuake/Modules/ExampleModule/ExampleModule.cpp +++ b/Nuake/Source/Nuake/Modules/ExampleModule/ExampleModule.cpp @@ -43,11 +43,24 @@ void ExampleModule_Startup() using namespace Nuake; auto& module = ModuleDB::Get().RegisterModule(); + 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"); module.BindFunction("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 args; for (int i = 0; i < func.arity(); i++) diff --git a/Nuake/Source/Nuake/Modules/ModuleDB.h b/Nuake/Source/Nuake/Modules/ModuleDB.h index 26f7f251..0239440a 100644 --- a/Nuake/Source/Nuake/Modules/ModuleDB.h +++ b/Nuake/Source/Nuake/Modules/ModuleDB.h @@ -5,20 +5,21 @@ #include -#define NUAKEMODULE(name) \ +#define NUAKEMODULE(moduleName) \ using TypeNameMap = std::map; \ \ -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 ModuleFactory = entt::meta(); \ + entt::meta_factory ModuleFactory = entt::meta(); \ TypeNameMap TypeNames; \ \ std::map> FuncArgNames; \ \ template \ - 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(); \ + return entt::resolve(); \ } \ +\ + template \ + 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 GetFuncArgNames(entt::id_type id) \ { \ @@ -56,7 +72,7 @@ std::vector GetAllFuncNames() \ } \ return "Unknown"; \ }\ -}; +}; namespace Nuake {