Added generate bindings.json function

This commit is contained in:
antopilo
2025-04-03 21:27:15 -04:00
parent 709633dce6
commit 41dd201128
2 changed files with 148 additions and 87 deletions

View File

@@ -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<std::string>()->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<const char*> argsName = *propArgsName.value().try_cast<std::vector<const char*>>();
for (int k = 0; k < func.arity(); k++)
{
std::string argType = std::string(func.arg(k).info().name());
if (argType == "class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >")
{
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;
}

View File

@@ -103,88 +103,88 @@ public: \
} \
\
#define NUAKEMODULE(moduleName) \
using TypeNameMap = std::map<entt::id_type, std::string>; \
\
class moduleName : public ModuleInstance \
{ \
public: \
std::string Description = "No Description"; \
entt::meta_factory<moduleName> ModuleFactory = entt::meta<moduleName>(); \
TypeNameMap TypeNames; \
\
std::map<entt::id_type, std::vector<std::string>> FuncArgNames; \
\
inline static auto ClassFactory = entt::meta<moduleName>(); \
template<auto T, typename... Args> \
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<T>(typeId); \
return *this; \
} \
template<auto Func> \
static auto RegisterSetting(const char* settingName) \
{ \
return ClassFactory \
.data<Func>(entt::hashed_string(settingName)) \
.prop(Nuake::HashedName::DisplayName, settingName); \
} \
\
template<typename T> \
void RegisterComponent() \
{ \
/*Nuake::SceneSystemDB::Get().RegisterComponent<T>();*/ \
} \
\
template<typename T> \
void RegisterComponentSystem() \
{ \
/*Nuake::SceneSystemDB::Get().RegisterSystem<T>(); */ \
} \
\
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) \
{ \
return FuncArgNames[id]; \
} \
\
std::vector<std::string> GetAllFuncNames() \
{\
std::vector<std::string> 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<entt::id_type, std::string>; \
\
class moduleName : public ModuleInstance \
{ \
public: \
std::string Description = "No Description"; \
entt::meta_factory<moduleName> ModuleFactory = entt::meta<moduleName>(); \
TypeNameMap TypeNames; \
\
std::map<entt::id_type, std::vector<std::string>> FuncArgNames; \
\
inline static auto ClassFactory = entt::meta<moduleName>(); \
template<auto T, typename... Args> \
static void BindFunction(const std::string& name, Args... argNames) \
{ \
entt::id_type typeId = entt::hashed_string(name.c_str()); \
ClassFactory.func<T>(typeId) \
.prop(Nuake::HashedName::DisplayName, name) \
.prop(Nuake::HashedName::ArgsName, std::vector<const char*>({argNames...})); \
} \
\
template<auto Func> \
static auto RegisterSetting(const char* settingName) \
{ \
return ClassFactory \
.data<Func>(entt::hashed_string(settingName)) \
.prop(Nuake::HashedName::DisplayName, settingName); \
} \
\
template<typename T> \
void RegisterComponent() \
{ \
Nuake::SceneSystemDB::Get().RegisterComponent<T>(); \
} \
\
template<typename T> \
void RegisterComponentSystem() \
{ \
Nuake::SceneSystemDB::Get().RegisterSceneSystem<T>(); \
} \
\
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) \
{ \
return FuncArgNames[id]; \
} \
\
std::vector<std::string> GetAllFuncNames() \
{ \
std::vector<std::string> 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<ModuleInstance*>(Modules[name])->Resolve();
}
}
//auto foundIt = std::find_if(Modules.begin(), Modules.end(), moduleName);
//if (foundIt != Modules.end())
//{
// return std::any_cast<ModuleInstance*>(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<std::string, std::any> Modules;