diff --git a/SDK/include/NDK/LuaBinding.hpp b/SDK/include/NDK/LuaBinding.hpp index 265e9e452..0c1dea15e 100644 --- a/SDK/include/NDK/LuaBinding.hpp +++ b/SDK/include/NDK/LuaBinding.hpp @@ -83,12 +83,6 @@ namespace Ndk Nz::LuaClass nodeClass; // SDK - Nz::LuaClass application; - Nz::LuaClass entityClass; - Nz::LuaClass nodeComponent; - Nz::LuaClass velocityComponent; - Nz::LuaClass worldClass; - using AddComponentFunc = int(*)(Nz::LuaInstance&, EntityHandle&); using GetComponentFunc = int(*)(Nz::LuaInstance&, BaseComponent&); @@ -100,7 +94,16 @@ namespace Ndk Nz::String name; }; + ComponentBinding* QueryComponentIndex(Nz::LuaInstance& lua, int argIndex = 1); + + Nz::LuaClass application; + Nz::LuaClass entityClass; + Nz::LuaClass nodeComponent; + Nz::LuaClass velocityComponent; + Nz::LuaClass worldClass; + std::vector m_componentBinding; + std::unordered_map m_componentBindingByName; #ifndef NDK_SERVER // Audio diff --git a/SDK/include/NDK/LuaBinding.inl b/SDK/include/NDK/LuaBinding.inl index 5ca290398..64e3eb453 100644 --- a/SDK/include/NDK/LuaBinding.inl +++ b/SDK/include/NDK/LuaBinding.inl @@ -21,6 +21,7 @@ namespace Ndk m_componentBinding.resize(T::componentIndex + 1); m_componentBinding[T::componentIndex] = std::move(binding); + m_componentBindingByName[name] = T::componentIndex; } template diff --git a/SDK/src/NDK/LuaBinding_SDK.cpp b/SDK/src/NDK/LuaBinding_SDK.cpp index 289e316a3..b371e76e3 100644 --- a/SDK/src/NDK/LuaBinding_SDK.cpp +++ b/SDK/src/NDK/LuaBinding_SDK.cpp @@ -59,56 +59,29 @@ namespace Ndk entityClass.BindMethod("Kill", &Entity::Kill); entityClass.BindMethod("IsEnabled", &Entity::IsEnabled); entityClass.BindMethod("IsValid", &Entity::IsValid); - entityClass.BindMethod("RemoveComponent", (void(Entity::*)(ComponentIndex)) &Entity::RemoveComponent); entityClass.BindMethod("RemoveAllComponents", &Entity::RemoveAllComponents); entityClass.BindMethod("__tostring", &EntityHandle::ToString); - entityClass.BindMethod("AddComponent", [this] (Nz::LuaInstance& lua, EntityHandle& handle) -> int + entityClass.BindMethod("AddComponent", [this] (Nz::LuaInstance& instance, EntityHandle& handle) -> int { - int index = 1; - ComponentIndex componentIndex = lua.Check(&index); + ComponentBinding* binding = QueryComponentIndex(instance); - if (componentIndex > m_componentBinding.size()) - { - lua.Error("Invalid component index"); - return 0; - } - - ComponentBinding& binding = m_componentBinding[componentIndex]; - if (binding.name.IsEmpty()) - { - lua.Error("This component is not available to the LuaAPI"); - return 0; - } - - return binding.adder(lua, handle); + return binding->adder(instance, handle); }); - entityClass.BindMethod("GetComponent", [this] (Nz::LuaInstance& lua, EntityHandle& handle) -> int + entityClass.BindMethod("GetComponent", [this] (Nz::LuaInstance& instance, EntityHandle& handle) -> int { - int index = 1; - ComponentIndex componentIndex = lua.Check(&index); + ComponentBinding* binding = QueryComponentIndex(instance); - if (!handle->HasComponent(componentIndex)) - { - lua.PushNil(); - return 1; - } + return binding->getter(instance, handle->GetComponent(binding->index)); + }); - if (componentIndex > m_componentBinding.size()) - { - lua.Error("Invalid component index"); - return 0; - } + entityClass.BindMethod("RemoveComponent", [this] (Nz::LuaInstance& instance, EntityHandle& handle) -> int + { + ComponentBinding* binding = QueryComponentIndex(instance); - ComponentBinding& binding = m_componentBinding[componentIndex]; - if (binding.name.IsEmpty()) - { - lua.Error("This component is not available to the LuaAPI"); - return 0; - } - - return binding.getter(lua, handle->GetComponent(componentIndex)); + handle->RemoveComponent(binding->index); + return 0; }); /*********************************** Ndk::NodeComponent **********************************/ @@ -199,4 +172,45 @@ namespace Ndk } instance.SetGlobal("ComponentType"); } + + LuaBinding::ComponentBinding* LuaBinding::QueryComponentIndex(Nz::LuaInstance& instance, int argIndex) + { + switch (instance.GetType(argIndex)) + { + case Nz::LuaType_Number: + { + ComponentIndex componentIndex = instance.Check(&argIndex); + if (componentIndex > m_componentBinding.size()) + { + instance.Error("Invalid component index"); + return nullptr; + } + + ComponentBinding& binding = m_componentBinding[componentIndex]; + if (binding.name.IsEmpty()) + { + instance.Error("Invalid component index"); + return nullptr; + } + + return &binding; + } + + case Nz::LuaType_String: + { + const char* key = instance.CheckString(argIndex); + auto it = m_componentBindingByName.find(key); + if (it == m_componentBindingByName.end()) + { + instance.Error("Invalid component name"); + return nullptr; + } + + return &m_componentBinding[it->second]; + } + } + + instance.Error("Invalid component index at #" + Nz::String::Number(argIndex)); + return nullptr; + } }