From 41dd201128ffc032c2c091dac8dc0da41b0930cc Mon Sep 17 00:00:00 2001 From: antopilo Date: Thu, 3 Apr 2025 21:27:15 -0400 Subject: [PATCH] Added generate bindings.json function --- Nuake/Source/Nuake/Modules/ModuleDB.cpp | 64 +++++++++ Nuake/Source/Nuake/Modules/ModuleDB.h | 171 ++++++++++++------------ 2 files changed, 148 insertions(+), 87 deletions(-) create mode 100644 Nuake/Source/Nuake/Modules/ModuleDB.cpp diff --git a/Nuake/Source/Nuake/Modules/ModuleDB.cpp b/Nuake/Source/Nuake/Modules/ModuleDB.cpp new file mode 100644 index 00000000..f2329fbd --- /dev/null +++ b/Nuake/Source/Nuake/Modules/ModuleDB.cpp @@ -0,0 +1,64 @@ +#include "ModuleDB.h" + +using namespace Nuake; + +json ModuleDB::GenerateModuleAPI() +{ + json moduleAPI; + + int i = 0; + for (auto& [name, module] : Modules) + { + json moduleData; + moduleData["Name"] = name; + + auto meta = entt::resolve(entt::hashed_string(name.c_str())); + + int j = 0; + for (auto [id, func] : meta.func()) + { + json funcData; + + auto funcName = func.prop(HashedName::DisplayName); + funcData["Name"] = funcName.value().try_cast()->c_str(); + funcData["ReturnType"] = func.ret().info().name(); + funcData["NumArgs"] = func.arity(); + + if (func.arity() > 0) + { + auto propArgsName = func.prop(HashedName::ArgsName); + if (propArgsName) + { + json argsData; + std::vector argsName = *propArgsName.value().try_cast>(); + for (int k = 0; k < func.arity(); k++) + { + std::string argType = std::string(func.arg(k).info().name()); + + if (argType == "class std::basic_string,class std::allocator >") + { + argType = "string"; + } + + json argData; + argData["Name"] = argsName[k]; + argData["Type"] = argType; + + argsData[k] = argData; + } + + funcData["Args"] = argsData; + } + } + + moduleData["Functions"][j] = funcData; + j++; + } + + moduleAPI["Modules"][i] = moduleData; + + i++; + } + + return moduleAPI; +} \ No newline at end of file diff --git a/Nuake/Source/Nuake/Modules/ModuleDB.h b/Nuake/Source/Nuake/Modules/ModuleDB.h index 0153c3ca..ffac004e 100644 --- a/Nuake/Source/Nuake/Modules/ModuleDB.h +++ b/Nuake/Source/Nuake/Modules/ModuleDB.h @@ -103,88 +103,88 @@ public: \ } \ \ -#define NUAKEMODULE(moduleName) \ -using TypeNameMap = std::map; \ - \ -class moduleName : public ModuleInstance \ -{ \ -public: \ - std::string Description = "No Description"; \ - entt::meta_factory ModuleFactory = entt::meta(); \ - TypeNameMap TypeNames; \ - \ - std::map> FuncArgNames; \ -\ - inline static auto ClassFactory = entt::meta(); \ - template \ - inline moduleName& BindFunction(const std::string& name, Args... argNames) \ - { \ - entt::id_type typeId = entt::hashed_string(name.c_str()); \ - TypeNames[typeId] = name; \ - FuncArgNames[typeId] = { argNames ... }; \ - ModuleFactory.func(typeId); \ - return *this; \ - } \ - template \ - static auto RegisterSetting(const char* settingName) \ - { \ - return ClassFactory \ - .data(entt::hashed_string(settingName)) \ - .prop(Nuake::HashedName::DisplayName, settingName); \ - } \ - \ - template \ - void RegisterComponent() \ - { \ - /*Nuake::SceneSystemDB::Get().RegisterComponent();*/ \ - } \ -\ - template \ - void RegisterComponentSystem() \ - { \ - /*Nuake::SceneSystemDB::Get().RegisterSystem(); */ \ - } \ - \ - 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) \ -{ \ - return FuncArgNames[id]; \ -} \ -\ -std::vector GetAllFuncNames() \ -{\ - std::vector names; \ - names.reserve(TypeNames.size()); \ - for (const auto& [_, name] : TypeNames) \ - { \ - names.push_back(name);\ - }\ - return names;\ -}\ -\ - std::string_view GetTypeName(entt::id_type id) \ - { \ - if (TypeNames.contains(id)) \ - { \ - return TypeNames[id]; \ - } \ - return "Unknown"; \ - }\ -}; +#define NUAKEMODULE(moduleName) \ +using TypeNameMap = std::map; \ + \ +class moduleName : public ModuleInstance \ +{ \ +public: \ + std::string Description = "No Description"; \ + entt::meta_factory ModuleFactory = entt::meta(); \ + TypeNameMap TypeNames; \ + \ + std::map> FuncArgNames; \ + \ + inline static auto ClassFactory = entt::meta(); \ + template \ + static void BindFunction(const std::string& name, Args... argNames) \ + { \ + entt::id_type typeId = entt::hashed_string(name.c_str()); \ + ClassFactory.func(typeId) \ + .prop(Nuake::HashedName::DisplayName, name) \ + .prop(Nuake::HashedName::ArgsName, std::vector({argNames...})); \ + } \ + \ + template \ + static auto RegisterSetting(const char* settingName) \ + { \ + return ClassFactory \ + .data(entt::hashed_string(settingName)) \ + .prop(Nuake::HashedName::DisplayName, settingName); \ + } \ + \ + template \ + void RegisterComponent() \ + { \ + Nuake::SceneSystemDB::Get().RegisterComponent(); \ + } \ + \ + template \ + void RegisterComponentSystem() \ + { \ + Nuake::SceneSystemDB::Get().RegisterSceneSystem(); \ + } \ + \ + 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) \ + { \ + return FuncArgNames[id]; \ + } \ + \ + std::vector GetAllFuncNames() \ + { \ + std::vector names; \ + names.reserve(TypeNames.size()); \ + for (const auto& [_, name] : TypeNames) \ + { \ + names.push_back(name); \ + } \ + return names; \ + } \ + \ + std::string_view GetTypeName(entt::id_type id) \ + { \ + if (TypeNames.contains(id)) \ + { \ + return TypeNames[id]; \ + } \ + return "Unknown"; \ + } \ + }; namespace Nuake { @@ -245,11 +245,6 @@ namespace Nuake return std::any_cast(Modules[name])->Resolve(); } } - //auto foundIt = std::find_if(Modules.begin(), Modules.end(), moduleName); - //if (foundIt != Modules.end()) - //{ - // return std::any_cast(foundIt->second)->Resolve(); - //} assert(false && "Module not found"); return entt::meta_type(); @@ -268,6 +263,8 @@ namespace Nuake return moduleNames; } + json GenerateModuleAPI(); + private: std::map Modules;