From 6808d60cc163ee4fe95bb55a91f63d2d84b75af9 Mon Sep 17 00:00:00 2001 From: Lynix Date: Thu, 31 Mar 2016 22:51:10 +0200 Subject: [PATCH] Sdk: Remake LuaAPI internal Former-commit-id: 297c30dbeca443d934edfd8d889cd7a6c725d0e1 --- SDK/include/NDK/LuaAPI.hpp | 18 +- SDK/src/NDK/LuaAPI.cpp | 31 +++ SDK/src/NDK/LuaAPI_Utility.cpp | 97 --------- SDK/src/NDK/LuaBinding.cpp | 76 +++++++ SDK/src/NDK/LuaBinding.hpp | 114 ++++++++++ ...{LuaAPI_Audio.cpp => LuaBinding_Audio.cpp} | 16 +- .../{LuaAPI_Core.cpp => LuaBinding_Core.cpp} | 45 ++-- ...I_Graphics.cpp => LuaBinding_Graphics.cpp} | 13 +- .../{LuaAPI_Math.cpp => LuaBinding_Math.cpp} | 26 +-- ...API_Network.cpp => LuaBinding_Network.cpp} | 47 ++-- ...I_Renderer.cpp => LuaBinding_Renderer.cpp} | 10 +- SDK/src/NDK/LuaBinding_SDK.cpp | 173 +++++++++++++++ ...{LuaAPI_SDK.cpp => LuaBinding_Utility.cpp} | 204 +++++++----------- build/scripts/tools/ndk.lua | 3 +- build/scripts/tools/ndk_server.lua | 9 +- 15 files changed, 553 insertions(+), 329 deletions(-) create mode 100644 SDK/src/NDK/LuaAPI.cpp delete mode 100644 SDK/src/NDK/LuaAPI_Utility.cpp create mode 100644 SDK/src/NDK/LuaBinding.cpp create mode 100644 SDK/src/NDK/LuaBinding.hpp rename SDK/src/NDK/{LuaAPI_Audio.cpp => LuaBinding_Audio.cpp} (96%) rename SDK/src/NDK/{LuaAPI_Core.cpp => LuaBinding_Core.cpp} (92%) rename SDK/src/NDK/{LuaAPI_Graphics.cpp => LuaBinding_Graphics.cpp} (82%) rename SDK/src/NDK/{LuaAPI_Math.cpp => LuaBinding_Math.cpp} (96%) rename SDK/src/NDK/{LuaAPI_Network.cpp => LuaBinding_Network.cpp} (82%) rename SDK/src/NDK/{LuaAPI_Renderer.cpp => LuaBinding_Renderer.cpp} (64%) create mode 100644 SDK/src/NDK/LuaBinding_SDK.cpp rename SDK/src/NDK/{LuaAPI_SDK.cpp => LuaBinding_Utility.cpp} (56%) diff --git a/SDK/include/NDK/LuaAPI.hpp b/SDK/include/NDK/LuaAPI.hpp index 2f3000034..277948bd5 100644 --- a/SDK/include/NDK/LuaAPI.hpp +++ b/SDK/include/NDK/LuaAPI.hpp @@ -16,20 +16,22 @@ namespace Nz namespace Ndk { + class LuaBinding; + class NDK_API LuaAPI { public: LuaAPI() = delete; ~LuaAPI() = delete; - static void Register_Audio(Nz::LuaInstance& instance); - static void Register_Core(Nz::LuaInstance& instance); - static void Register_Graphics(Nz::LuaInstance& instance); - static void Register_Math(Nz::LuaInstance& instance); - static void Register_Network(Nz::LuaInstance& instance); - static void Register_Renderer(Nz::LuaInstance& instance); - static void Register_SDK(Nz::LuaInstance& instance); - static void Register_Utility(Nz::LuaInstance& instance); + static bool Initialize(); + + static void RegisterClasses(Nz::LuaInstance& instance); + + static void Uninitialize(); + + private: + static LuaBinding* s_binding; }; } diff --git a/SDK/src/NDK/LuaAPI.cpp b/SDK/src/NDK/LuaAPI.cpp new file mode 100644 index 000000000..a1b84bf5d --- /dev/null +++ b/SDK/src/NDK/LuaAPI.cpp @@ -0,0 +1,31 @@ +// This file was automatically generated on 26 May 2014 at 01:05:31 + +#include +#include + +namespace Ndk +{ + bool LuaAPI::Initialize() + { + s_binding = new LuaBinding; + return true; + } + + void LuaAPI::RegisterClasses(Nz::LuaInstance& instance) + { + if (!s_binding && !Initialize()) + { + NazaraError("Failed to initialize binding"); + return; + } + + s_binding->RegisterClasses(instance); + } + + void LuaAPI::Uninitialize() + { + delete s_binding; + } + + LuaBinding* LuaAPI::s_binding = nullptr; +} diff --git a/SDK/src/NDK/LuaAPI_Utility.cpp b/SDK/src/NDK/LuaAPI_Utility.cpp deleted file mode 100644 index 0f2659e1e..000000000 --- a/SDK/src/NDK/LuaAPI_Utility.cpp +++ /dev/null @@ -1,97 +0,0 @@ -// This file is part of the "Nazara Development Kit" -// For conditions of distribution and use, see copyright notice in Prerequesites.hpp - - -#include -#include -#include - -namespace Ndk -{ - void LuaAPI::Register_Utility(Nz::LuaInstance& instance) - { - /*********************************** Nz::AbstractImage **********************************/ - Nz::LuaClass abstractImage("AbstractImage"); - - abstractImage.SetMethod("GetBytesPerPixel", &Nz::AbstractImage::GetBytesPerPixel); - abstractImage.SetMethod("GetDepth", &Nz::AbstractImage::GetDepth, static_cast(0)); - abstractImage.SetMethod("GetFormat", &Nz::AbstractImage::GetFormat); - abstractImage.SetMethod("GetHeight", &Nz::AbstractImage::GetHeight, static_cast(0)); - abstractImage.SetMethod("GetLevelCount", &Nz::AbstractImage::GetLevelCount); - abstractImage.SetMethod("GetMaxLevel", &Nz::AbstractImage::GetMaxLevel); - abstractImage.SetMethod("GetSize", &Nz::AbstractImage::GetSize, static_cast(0)); - abstractImage.SetMethod("GetType", &Nz::AbstractImage::GetType); - abstractImage.SetMethod("GetWidth", &Nz::AbstractImage::GetWidth, static_cast(0)); - abstractImage.SetMethod("IsCompressed", &Nz::AbstractImage::IsCompressed); - abstractImage.SetMethod("IsCubemap", &Nz::AbstractImage::IsCubemap); - - abstractImage.SetMethod("GetMemoryUsage", [] (Nz::LuaInstance& lua, Nz::AbstractImage* abstractImage) -> int - { - unsigned int argCount = std::min(lua.GetStackTop(), 1U); - switch (argCount) - { - case 0: - return lua.Push(abstractImage->GetMemoryUsage()); - - case 1: - { - int index = 1; - Nz::UInt8 level(lua.Check(&index)); - - return lua.Push(abstractImage->GetMemoryUsage(level)); - } - } - - lua.Error("No matching overload for method GetMemoryUsage"); - return 0; - }); - - abstractImage.SetMethod("Update", [] (Nz::LuaInstance& lua, Nz::AbstractImage* abstractImage) -> int - { - unsigned int argCount = std::min(lua.GetStackTop(), 6U); - int argIndex = 1; - - std::size_t bufferSize = 0; - const Nz::UInt8* pixels = reinterpret_cast(lua.CheckString(argIndex++, &bufferSize)); - - if (argCount < 2 || lua.IsOfType(2, Nz::LuaType_Number)) - { - // bool Update(const UInt8* pixels, unsigned int srcWidth = 0, unsigned int srcHeight = 0, UInt8 level = 0) - unsigned int srcWidth = lua.Check(&argIndex, 0); - unsigned int srcHeight = lua.Check(&argIndex, 0); - Nz::UInt8 level = lua.Check(&argIndex, 0); - - ///TODO: Buffer checks (Nz::ByteBufferView ?) - return lua.Push(abstractImage->Update(pixels, srcWidth, srcHeight, level)); - } - /* Disabled until Box and Rect have been ported - else if (lua.IsOfType(2, "Box")) - { - // bool Update(const UInt8* pixels, const Boxui& box, unsigned int srcWidth = 0, unsigned int srcHeight = 0, UInt8 level = 0) - Nz::Boxui box = lua.Check(&argIndex); - unsigned int srcWidth = lua.Check(&argIndex, 0); - unsigned int srcHeight = lua.Check(&argIndex, 0); - Nz::UInt8 level = lua.Check(&argIndex, 0); - - ///TODO: Buffer checks (Nz::ByteBufferView ?) - return lua.Push(abstractImage->Update(pixels, srcWidth, srcHeight, level)); - } - else if (lua.IsOfType(2, "Rect")) - { - // bool Update(const UInt8* pixels, const Rectui& rect, unsigned int z = 0, unsigned int srcWidth = 0, unsigned int srcHeight = 0, UInt8 level = 0) - Nz::Rectui box = lua.Check(&argIndex); - unsigned int srcWidth = lua.Check(&argIndex, 0); - unsigned int srcHeight = lua.Check(&argIndex, 0); - Nz::UInt8 level = lua.Check(&argIndex, 0); - - ///TODO: Buffer checks (Nz::ByteBufferView ?) - return lua.Push(abstractImage->Update(pixels, srcWidth, srcHeight, level)); - }*/ - - lua.Error("No matching overload for method Update"); - return 0; - }); - - abstractImage.Register(instance); - } -} \ No newline at end of file diff --git a/SDK/src/NDK/LuaBinding.cpp b/SDK/src/NDK/LuaBinding.cpp new file mode 100644 index 000000000..0c2766ca7 --- /dev/null +++ b/SDK/src/NDK/LuaBinding.cpp @@ -0,0 +1,76 @@ +// This file was automatically generated on 26 May 2014 at 01:05:31 + +#include + +namespace Ndk +{ + LuaBinding::LuaBinding() : + // Core + clockClass("Clock"), + directoryClass("Directory"), + fileClass("File"), + streamClass("Stream"), + + // Math + eulerAnglesClass("EulerAngles"), + quaternionClass("Quaternion"), + vector2dClass("Vector2"), + vector3dClass("Vector3"), + + // Network + abstractSocketClass("AbstractSocket"), + ipAddressClass("IpAddress"), + + // Utility + abstractImage("AbstractImage"), + nodeClass("Node"), + + // SDK + nodeComponent("NodeComponent"), + entityClass("Entity"), + worldClass("World") + + #ifndef NDK_SERVER + , + + // Audio + soundBuffer("SoundBuffer"), + soundEmitter("SoundEmitter"), + soundClass("Sound"), + + // Graphics + instancedRenderable("InstancedRenderable"), + modelClass("Model"), + + // SDK + graphicsComponent("GraphicsComponent") + #endif + { + BindCore(); + BindMath(); + BindNetwork(); + BindSDK(); + BindUtility(); + + #ifndef NDK_SERVER + BindAudio(); + BindGraphics(); + BindRenderer(); + #endif + } + + void LuaBinding::RegisterClasses(Nz::LuaInstance& instance) + { + RegisterCore(instance); + RegisterMath(instance); + RegisterNetwork(instance); + RegisterSDK(instance); + RegisterUtility(instance); + + #ifndef NDK_SERVER + RegisterAudio(instance); + RegisterGraphics(instance); + RegisterRenderer(instance); + #endif + } +} diff --git a/SDK/src/NDK/LuaBinding.hpp b/SDK/src/NDK/LuaBinding.hpp new file mode 100644 index 000000000..c8dda3c2f --- /dev/null +++ b/SDK/src/NDK/LuaBinding.hpp @@ -0,0 +1,114 @@ +// Copyright (C) 2015 Jérôme Leclercq +// This file is part of the "Nazara Development Kit" +// For conditions of distribution and use, see copyright notice in Prerequesites.hpp + +#pragma once + +#ifndef NDK_LUABINDING_HPP +#define NDK_LUABINDING_HPP + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#ifndef NDK_SERVER +#include +#include +#include +#endif + +namespace Ndk +{ + class LuaBinding + { + public: + LuaBinding(); + ~LuaBinding() = default; + + void RegisterClasses(Nz::LuaInstance& instance); + + private: + void BindCore(); + void BindMath(); + void BindNetwork(); + void BindSDK(); + void BindUtility(); + + template void EnableComponentBinding(); + + void RegisterCore(Nz::LuaInstance& instance); + void RegisterMath(Nz::LuaInstance& instance); + void RegisterNetwork(Nz::LuaInstance& instance); + void RegisterSDK(Nz::LuaInstance& instance); + void RegisterUtility(Nz::LuaInstance& instance); + + #ifndef NDK_SERVER + void BindAudio(); + void BindGraphics(); + void BindRenderer(); + + void RegisterAudio(Nz::LuaInstance& instance); + void RegisterGraphics(Nz::LuaInstance& instance); + void RegisterRenderer(Nz::LuaInstance& instance); + #endif + + // Core + Nz::LuaClass clockClass; + Nz::LuaClass directoryClass; + Nz::LuaClass fileClass; + Nz::LuaClass streamClass; + + // Math + Nz::LuaClass eulerAnglesClass; + Nz::LuaClass quaternionClass; + Nz::LuaClass vector2dClass; + Nz::LuaClass vector3dClass; + + // Network + Nz::LuaClass abstractSocketClass; + Nz::LuaClass ipAddressClass; + + // Utility + Nz::LuaClass abstractImage; + Nz::LuaClass nodeClass; + + // SDK + Nz::LuaClass entityClass; + Nz::LuaClass nodeComponent; + Nz::LuaClass worldClass; + + using AddComponentFunc = int(*)(Nz::LuaInstance&, EntityHandle&); + using GetComponentFunc = int(*)(Nz::LuaInstance&, BaseComponent&); + + struct ComponentBinding + { + AddComponentFunc adder; + GetComponentFunc getter; + bool valid = false; + }; + + std::vector m_componentBinding; + + #ifndef NDK_SERVER + // Audio + Nz::LuaClass soundClass; + Nz::LuaClass soundBuffer; + Nz::LuaClass soundEmitter; + + // Graphics + Nz::LuaClass instancedRenderable; + Nz::LuaClass modelClass; + + // SDK + Nz::LuaClass graphicsComponent; + #endif + }; +} + +#endif // NDK_LUABINDING_HPP diff --git a/SDK/src/NDK/LuaAPI_Audio.cpp b/SDK/src/NDK/LuaBinding_Audio.cpp similarity index 96% rename from SDK/src/NDK/LuaAPI_Audio.cpp rename to SDK/src/NDK/LuaBinding_Audio.cpp index abe5638d4..a894595b3 100644 --- a/SDK/src/NDK/LuaAPI_Audio.cpp +++ b/SDK/src/NDK/LuaBinding_Audio.cpp @@ -1,16 +1,13 @@ // This file was automatically generated on 26 May 2014 at 01:05:31 +#include #include -#include -#include namespace Ndk { - void LuaAPI::Register_Audio(Nz::LuaInstance& instance) + void LuaBinding::BindAudio() { /*********************************** Nz::SoundBuffer **********************************/ - Nz::LuaClass soundBuffer("SoundBuffer"); - soundBuffer.SetConstructor([] (Nz::LuaInstance& lua) -> Nz::SoundBufferRef* { return new Nz::SoundBufferRef(new Nz::SoundBuffer); @@ -65,8 +62,6 @@ namespace Ndk return 1; }); - soundBuffer.Register(instance); - /*********************************** Nz::SoundEmitter **********************************/ Nz::LuaClass soundEmitter("SoundEmitter"); @@ -93,8 +88,6 @@ namespace Ndk soundEmitter.SetMethod("SetVolume", &Nz::SoundEmitter::SetVolume); soundEmitter.SetMethod("Stop", &Nz::SoundEmitter::Stop); - soundEmitter.Register(instance); - /*********************************** Nz::Sound **********************************/ Nz::LuaClass soundClass("Sound"); soundClass.Inherit(soundEmitter); @@ -122,7 +115,12 @@ namespace Ndk lua.PushString(stream); return 1; }); + } + void LuaBinding::RegisterAudio(Nz::LuaInstance& instance) + { soundClass.Register(instance); + soundBuffer.Register(instance); + soundEmitter.Register(instance); } } diff --git a/SDK/src/NDK/LuaAPI_Core.cpp b/SDK/src/NDK/LuaBinding_Core.cpp similarity index 92% rename from SDK/src/NDK/LuaAPI_Core.cpp rename to SDK/src/NDK/LuaBinding_Core.cpp index 7529e4eae..8ed8682ce 100644 --- a/SDK/src/NDK/LuaAPI_Core.cpp +++ b/SDK/src/NDK/LuaBinding_Core.cpp @@ -1,18 +1,13 @@ // This file was automatically generated on 26 May 2014 at 01:05:31 +#include #include -#include -#include - -#include namespace Ndk { - void LuaAPI::Register_Core(Nz::LuaInstance& instance) + void LuaBinding::BindCore() { /*********************************** Nz::Clock **********************************/ - Nz::LuaClass clockClass("Clock"); - clockClass.SetConstructor([](Nz::LuaInstance& lua) -> Nz::Clock* { int argIndex = 1; @@ -39,12 +34,7 @@ namespace Ndk return 1; }); - - clockClass.Register(instance); - /********************************* Nz::Directory ********************************/ - Nz::LuaClass directoryClass("Directory"); - directoryClass.SetConstructor([](Nz::LuaInstance& lua) -> Nz::Directory* { unsigned int argCount = std::min(lua.GetStackTop(), 1U); @@ -93,12 +83,7 @@ namespace Ndk return 1; }); - - directoryClass.Register(instance); - /*********************************** Nz::Stream ***********************************/ - Nz::LuaClass streamClass("Stream"); - streamClass.SetMethod("EnableTextMode", &Nz::Stream::EnableTextMode); streamClass.SetMethod("Flush", &Nz::Stream::Flush); streamClass.SetMethod("GetCursorPos", &Nz::Stream::GetCursorPos); @@ -139,10 +124,7 @@ namespace Ndk return 1; }); - streamClass.Register(instance); - /*********************************** Nz::File ***********************************/ - Nz::LuaClass fileClass("File"); fileClass.Inherit(streamClass); fileClass.SetConstructor([](Nz::LuaInstance& lua) -> Nz::File* @@ -242,8 +224,15 @@ namespace Ndk lua.PushString(stream); return 1; }); + } + void LuaBinding::RegisterCore(Nz::LuaInstance& instance) + { + // Classes + clockClass.Register(instance); + directoryClass.Register(instance); fileClass.Register(instance); + streamClass.Register(instance); // Enums @@ -251,9 +240,9 @@ namespace Ndk static_assert(Nz::CursorPosition_Max + 1 == 3, "Nz::CursorPosition has been updated but change was not reflected to Lua binding"); instance.PushTable(0, 3); { - instance.SetField("AtBegin", Nz::CursorPosition_AtBegin); + instance.SetField("AtBegin", Nz::CursorPosition_AtBegin); instance.SetField("AtCurrent", Nz::CursorPosition_AtCurrent); - instance.SetField("AtEnd", Nz::CursorPosition_AtEnd); + instance.SetField("AtEnd", Nz::CursorPosition_AtEnd); } instance.SetGlobal("CursorPosition"); @@ -277,13 +266,13 @@ namespace Ndk static_assert(Nz::OpenMode_Max + 1 == 2 * (64), "Nz::OpenModeFlags has been updated but change was not reflected to Lua binding"); instance.PushTable(0, 8); { - instance.SetField("Append", Nz::OpenMode_Append); - instance.SetField("NotOpen", Nz::OpenMode_NotOpen); - instance.SetField("Lock", Nz::OpenMode_Lock); - instance.SetField("ReadOnly", Nz::OpenMode_ReadOnly); + instance.SetField("Append", Nz::OpenMode_Append); + instance.SetField("NotOpen", Nz::OpenMode_NotOpen); + instance.SetField("Lock", Nz::OpenMode_Lock); + instance.SetField("ReadOnly", Nz::OpenMode_ReadOnly); instance.SetField("ReadWrite", Nz::OpenMode_ReadWrite); - instance.SetField("Text", Nz::OpenMode_Text); - instance.SetField("Truncate", Nz::OpenMode_Truncate); + instance.SetField("Text", Nz::OpenMode_Text); + instance.SetField("Truncate", Nz::OpenMode_Truncate); instance.SetField("WriteOnly", Nz::OpenMode_WriteOnly); } instance.SetGlobal("OpenMode"); diff --git a/SDK/src/NDK/LuaAPI_Graphics.cpp b/SDK/src/NDK/LuaBinding_Graphics.cpp similarity index 82% rename from SDK/src/NDK/LuaAPI_Graphics.cpp rename to SDK/src/NDK/LuaBinding_Graphics.cpp index 5440b05a3..e3bbaec66 100644 --- a/SDK/src/NDK/LuaAPI_Graphics.cpp +++ b/SDK/src/NDK/LuaBinding_Graphics.cpp @@ -1,18 +1,16 @@ // This file is part of the "Nazara Development Kit" // For conditions of distribution and use, see copyright notice in Prerequesites.hpp - +#include #include -#include -#include namespace Ndk { - void LuaAPI::Register_Graphics(Nz::LuaInstance& instance) + void LuaBinding::BindGraphics() { - Nz::LuaClass instancedRenderable("InstancedRenderable"); + /*********************************** Nz::InstancedRenderable ***********************************/ - Nz::LuaClass modelClass("Model"); + /*********************************** Nz::Model ***********************************/ modelClass.Inherit(instancedRenderable, [] (Nz::ModelRef* model) -> Nz::InstancedRenderableRef* { return reinterpret_cast(model); //TODO: Make a ObjectRefCast @@ -39,7 +37,10 @@ namespace Ndk //modelClass.SetMethod("SetSequence", &Nz::Model::SetSequence); modelClass.SetMethod("SetSkin", &Nz::Model::SetSkin); modelClass.SetMethod("SetSkinCount", &Nz::Model::SetSkinCount); + } + void LuaBinding::RegisterGraphics(Nz::LuaInstance& instance) + { instancedRenderable.Register(instance); modelClass.Register(instance); } diff --git a/SDK/src/NDK/LuaAPI_Math.cpp b/SDK/src/NDK/LuaBinding_Math.cpp similarity index 96% rename from SDK/src/NDK/LuaAPI_Math.cpp rename to SDK/src/NDK/LuaBinding_Math.cpp index 5887b7e83..5d7bc5a6b 100644 --- a/SDK/src/NDK/LuaAPI_Math.cpp +++ b/SDK/src/NDK/LuaBinding_Math.cpp @@ -1,16 +1,14 @@ // This file was automatically generated on 26 May 2014 at 01:05:31 +#include #include -#include -#include +#include namespace Ndk { - void LuaAPI::Register_Math(Nz::LuaInstance& instance) + void LuaBinding::BindMath() { /*********************************** Nz::EulerAngles **********************************/ - Nz::LuaClass eulerAnglesClass("EulerAngles"); - eulerAnglesClass.SetConstructor([] (Nz::LuaInstance& lua) -> Nz::EulerAnglesd* { unsigned int argCount = std::min(lua.GetStackTop(), 3U); @@ -147,11 +145,7 @@ namespace Ndk return false; }); - eulerAnglesClass.Register(instance); - /*********************************** Nz::Quaternion **********************************/ - Nz::LuaClass quaternionClass("Quaternion"); - quaternionClass.SetConstructor([] (Nz::LuaInstance& lua) -> Nz::Quaterniond* { unsigned int argCount = std::min(lua.GetStackTop(), 4U); @@ -243,11 +237,7 @@ namespace Ndk return false; }); - quaternionClass.Register(instance); - /*********************************** Nz::Vector2 **********************************/ - Nz::LuaClass vector2dClass("Vector2"); - vector2dClass.SetConstructor([](Nz::LuaInstance& lua) -> Nz::Vector2d* { unsigned int argCount = std::min(lua.GetStackTop(), 2U); @@ -354,11 +344,7 @@ namespace Ndk return false; }); - vector2dClass.Register(instance); - /*********************************** Nz::Vector3 **********************************/ - Nz::LuaClass vector3dClass("Vector3"); - vector3dClass.SetConstructor([] (Nz::LuaInstance& lua) -> Nz::Vector3d* { unsigned int argCount = std::min(lua.GetStackTop(), 3U); @@ -484,7 +470,13 @@ namespace Ndk return false; }); + } + void LuaBinding::RegisterMath(Nz::LuaInstance& instance) + { + eulerAnglesClass.Register(instance); + quaternionClass.Register(instance); + vector2dClass.Register(instance); vector3dClass.Register(instance); } } diff --git a/SDK/src/NDK/LuaAPI_Network.cpp b/SDK/src/NDK/LuaBinding_Network.cpp similarity index 82% rename from SDK/src/NDK/LuaAPI_Network.cpp rename to SDK/src/NDK/LuaBinding_Network.cpp index e5a36ac7d..14f0b12ca 100644 --- a/SDK/src/NDK/LuaAPI_Network.cpp +++ b/SDK/src/NDK/LuaBinding_Network.cpp @@ -1,28 +1,22 @@ // This file was automatically generated on 26 May 2014 at 01:05:31 +#include #include -#include -#include namespace Ndk { - void LuaAPI::Register_Network(Nz::LuaInstance& instance) + void LuaBinding::BindNetwork() { /*********************************** Nz::AbstractSocket **********************************/ - Nz::LuaClass abstractSocketClass("AbstractSocket"); - - abstractSocketClass.SetMethod("Close", &Nz::AbstractSocket::Close); - abstractSocketClass.SetMethod("EnableBlocking", &Nz::AbstractSocket::EnableBlocking); - abstractSocketClass.SetMethod("GetLastError", &Nz::AbstractSocket::GetLastError); - abstractSocketClass.SetMethod("GetState", &Nz::AbstractSocket::GetState); - abstractSocketClass.SetMethod("GetType", &Nz::AbstractSocket::GetType); - abstractSocketClass.SetMethod("IsBlockingEnabled", &Nz::AbstractSocket::IsBlockingEnabled); + abstractSocketClass.SetMethod("Close", &Nz::AbstractSocket::Close); + abstractSocketClass.SetMethod("EnableBlocking", &Nz::AbstractSocket::EnableBlocking); + abstractSocketClass.SetMethod("GetLastError", &Nz::AbstractSocket::GetLastError); + abstractSocketClass.SetMethod("GetState", &Nz::AbstractSocket::GetState); + abstractSocketClass.SetMethod("GetType", &Nz::AbstractSocket::GetType); + abstractSocketClass.SetMethod("IsBlockingEnabled", &Nz::AbstractSocket::IsBlockingEnabled); abstractSocketClass.SetMethod("QueryAvailableBytes", &Nz::AbstractSocket::QueryAvailableBytes); - abstractSocketClass.Register(instance); - - Nz::LuaClass ipAddressClass("IpAddress"); - + /*********************************** Nz::IpAddress **********************************/ ipAddressClass.SetConstructor([] (Nz::LuaInstance& lua) -> Nz::IpAddress* { unsigned int argCount = std::min(lua.GetStackTop(), 9U); @@ -43,18 +37,18 @@ namespace Ndk case 8: case 9: return new Nz::IpAddress(lua.Check(&argIndex), lua.Check(&argIndex), lua.Check(&argIndex), lua.Check(&argIndex), - lua.Check(&argIndex), lua.Check(&argIndex), lua.Check(&argIndex), lua.Check(&argIndex), lua.Check(&argIndex, 0)); + lua.Check(&argIndex), lua.Check(&argIndex), lua.Check(&argIndex), lua.Check(&argIndex), lua.Check(&argIndex, 0)); } return nullptr; }); - ipAddressClass.SetMethod("GetPort", &Nz::IpAddress::GetPort); + ipAddressClass.SetMethod("GetPort", &Nz::IpAddress::GetPort); ipAddressClass.SetMethod("GetProtocol", &Nz::IpAddress::GetProtocol); - ipAddressClass.SetMethod("IsLoopback", &Nz::IpAddress::IsLoopback); - ipAddressClass.SetMethod("IsValid", &Nz::IpAddress::IsValid); - ipAddressClass.SetMethod("ToUInt32", &Nz::IpAddress::ToUInt32); - ipAddressClass.SetMethod("__tostring", &Nz::IpAddress::ToString); + ipAddressClass.SetMethod("IsLoopback", &Nz::IpAddress::IsLoopback); + ipAddressClass.SetMethod("IsValid", &Nz::IpAddress::IsValid); + ipAddressClass.SetMethod("ToUInt32", &Nz::IpAddress::ToUInt32); + ipAddressClass.SetMethod("__tostring", &Nz::IpAddress::ToString); ipAddressClass.SetStaticMethod("ResolveAddress", [] (Nz::LuaInstance& instance) -> int { @@ -96,10 +90,10 @@ namespace Ndk { instance.PushInteger(index++); instance.PushTable(0, 4); - instance.SetField("Address", std::move(info.address)); + instance.SetField("Address", std::move(info.address)); instance.SetField("CanonicalName", std::move(info.canonicalName)); - instance.SetField("Protocol", std::move(info.protocol)); - instance.SetField("SocketType", std::move(info.socketType)); + instance.SetField("Protocol", std::move(info.protocol)); + instance.SetField("SocketType", std::move(info.socketType)); instance.SetTable(); } @@ -112,7 +106,12 @@ namespace Ndk return 2; } }); + } + void LuaBinding::RegisterNetwork(Nz::LuaInstance& instance) + { + // Classes + abstractSocketClass.Register(instance); ipAddressClass.Register(instance); // Enums diff --git a/SDK/src/NDK/LuaAPI_Renderer.cpp b/SDK/src/NDK/LuaBinding_Renderer.cpp similarity index 64% rename from SDK/src/NDK/LuaAPI_Renderer.cpp rename to SDK/src/NDK/LuaBinding_Renderer.cpp index cbbd24b92..78b9fe561 100644 --- a/SDK/src/NDK/LuaAPI_Renderer.cpp +++ b/SDK/src/NDK/LuaBinding_Renderer.cpp @@ -2,14 +2,16 @@ // This file is part of the "Nazara Development Kit" // For conditions of distribution and use, see copyright notice in Prerequesites.hpp - +#include #include -#include -#include namespace Ndk { - void LuaAPI::Register_Renderer(Nz::LuaInstance& instance) + void LuaBinding::BindRenderer() + { + } + + void LuaBinding::RegisterRenderer(Nz::LuaInstance& instance) { } } \ No newline at end of file diff --git a/SDK/src/NDK/LuaBinding_SDK.cpp b/SDK/src/NDK/LuaBinding_SDK.cpp new file mode 100644 index 000000000..bb8ec3475 --- /dev/null +++ b/SDK/src/NDK/LuaBinding_SDK.cpp @@ -0,0 +1,173 @@ +// Copyright (C) 2016 Jérôme Leclercq, Arnaud Cadot +// This file is part of the "Nazara Development Kit" +// For conditions of distribution and use, see copyright notice in Prerequesites.hpp + +#include +#include + +namespace Ndk +{ + namespace + { + int AddNilComponent(Nz::LuaInstance& lua, EntityHandle&) + { + lua.PushNil(); + return 1; + } + + template + int AddComponentOfType(Nz::LuaInstance& lua, EntityHandle& handle) + { + static_assert(std::is_base_of::value, "ComponentType must inherit BaseComponent"); + + T& component = handle->AddComponent(); + lua.Push(component.CreateHandle()); + return 1; + } + + int PushNilComponent(Nz::LuaInstance& lua, BaseComponent&) + { + lua.PushNil(); + return 1; + } + + template + int PushComponentOfType(Nz::LuaInstance& lua, BaseComponent& component) + { + static_assert(std::is_base_of::value, "ComponentType must inherit BaseComponent"); + + T& rightComponent = static_cast(component); + lua.Push(rightComponent.CreateHandle()); + return 1; + } + } + + void LuaBinding::BindSDK() + { + /*********************************** Ndk::NodeComponent **********************************/ + nodeComponent.Inherit(nodeClass, [] (NodeComponentHandle* handle) -> Nz::Node* + { + return handle->GetObject(); + }); + + /*********************************** Ndk::Entity **********************************/ + entityClass.SetMethod("Enable", &Entity::Enable); + entityClass.SetMethod("GetId", &Entity::GetId); + entityClass.SetMethod("GetWorld", &Entity::GetWorld); + entityClass.SetMethod("Kill", &Entity::Kill); + entityClass.SetMethod("IsEnabled", &Entity::IsEnabled); + entityClass.SetMethod("IsValid", &Entity::IsValid); + entityClass.SetMethod("RemoveComponent", (void(Entity::*)(ComponentIndex)) &Entity::RemoveComponent); + entityClass.SetMethod("RemoveAllComponents", &Entity::RemoveAllComponents); + entityClass.SetMethod("__tostring", &EntityHandle::ToString); + + entityClass.SetMethod("AddComponent", [this] (Nz::LuaInstance& lua, EntityHandle& handle) -> int + { + int index = 1; + ComponentIndex componentIndex = lua.Check(&index); + + if (componentIndex > m_componentBinding.size()) + { + lua.Error("Invalid component index"); + return 0; + } + + ComponentBinding& binding = m_componentBinding[componentIndex]; + if (!binding.valid) + { + lua.Error("This component is not available to the LuaAPI"); + return 0; + } + + return binding.adder(lua, handle); + }); + + entityClass.SetMethod("GetComponent", [this] (Nz::LuaInstance& lua, EntityHandle& handle) -> int + { + int index = 1; + ComponentIndex componentIndex = lua.Check(&index); + + if (!handle->HasComponent(componentIndex)) + { + lua.PushNil(); + return 1; + } + + if (componentIndex > m_componentBinding.size()) + { + lua.Error("Invalid component index"); + return 0; + } + + ComponentBinding& binding = m_componentBinding[componentIndex]; + if (!binding.valid) + { + lua.Error("This component is not available to the LuaAPI"); + return 0; + } + + return binding.getter(lua, handle->GetComponent(componentIndex)); + }); + + /*********************************** Ndk::World **********************************/ + worldClass.SetMethod("CreateEntity", &World::CreateEntity); + worldClass.SetMethod("CreateEntities", &World::CreateEntities); + worldClass.SetMethod("Clear", &World::Clear); + + + #ifndef NDK_SERVER + /*********************************** Ndk::GraphicsComponent **********************************/ + graphicsComponent.SetMethod("Attach", &GraphicsComponent::Attach, 0); + #endif + + + // Components functions + m_componentBinding.resize(BaseComponent::GetMaxComponentIndex() + 1); + + EnableComponentBinding(); + + #ifndef NDK_SERVER + EnableComponentBinding(); + #endif + } + + template + void LuaBinding::EnableComponentBinding() + { + ComponentBinding binding; + binding.adder = &AddComponentOfType; + binding.getter = &PushComponentOfType; + binding.valid = true; + + NazaraAssert(T::componentIndex < m_componentBinding.size(), "Component index is over component binding size"); + + m_componentBinding[T::componentIndex] = std::move(binding); + } + + void LuaBinding::RegisterSDK(Nz::LuaInstance& instance) + { + // Classes + entityClass.Register(instance); + nodeComponent.Register(instance); + worldClass.Register(instance); + + #ifndef NDK_SERVER + graphicsComponent.Register(instance); + #endif + + // Enums + + // ComponentType (fake enumeration to expose component indexes) + instance.PushTable(); + { + #ifndef NDK_SERVER + instance.PushInteger(GraphicsComponent::componentIndex); + instance.SetField("Graphics"); + #endif + + instance.PushInteger(NodeComponent::componentIndex); + instance.SetField("Node"); + } + instance.SetGlobal("ComponentType"); + } +} \ No newline at end of file diff --git a/SDK/src/NDK/LuaAPI_SDK.cpp b/SDK/src/NDK/LuaBinding_Utility.cpp similarity index 56% rename from SDK/src/NDK/LuaAPI_SDK.cpp rename to SDK/src/NDK/LuaBinding_Utility.cpp index 50bd66005..65447a0b3 100644 --- a/SDK/src/NDK/LuaAPI_SDK.cpp +++ b/SDK/src/NDK/LuaBinding_Utility.cpp @@ -1,94 +1,94 @@ -// Copyright (C) 2016 Jérôme Leclercq, Arnaud Cadot // This file is part of the "Nazara Development Kit" // For conditions of distribution and use, see copyright notice in Prerequesites.hpp - +#include #include -#include -#include -#include -#include -#include namespace Ndk { - namespace + void LuaBinding::BindUtility() { - int AddNilComponent(Nz::LuaInstance& lua, EntityHandle&) + /*********************************** Nz::AbstractImage **********************************/ + abstractImage.SetMethod("GetBytesPerPixel", &Nz::AbstractImage::GetBytesPerPixel); + abstractImage.SetMethod("GetDepth", &Nz::AbstractImage::GetDepth, static_cast(0)); + abstractImage.SetMethod("GetFormat", &Nz::AbstractImage::GetFormat); + abstractImage.SetMethod("GetHeight", &Nz::AbstractImage::GetHeight, static_cast(0)); + abstractImage.SetMethod("GetLevelCount", &Nz::AbstractImage::GetLevelCount); + abstractImage.SetMethod("GetMaxLevel", &Nz::AbstractImage::GetMaxLevel); + abstractImage.SetMethod("GetSize", &Nz::AbstractImage::GetSize, static_cast(0)); + abstractImage.SetMethod("GetType", &Nz::AbstractImage::GetType); + abstractImage.SetMethod("GetWidth", &Nz::AbstractImage::GetWidth, static_cast(0)); + abstractImage.SetMethod("IsCompressed", &Nz::AbstractImage::IsCompressed); + abstractImage.SetMethod("IsCubemap", &Nz::AbstractImage::IsCubemap); + + abstractImage.SetMethod("GetMemoryUsage", [] (Nz::LuaInstance& lua, Nz::AbstractImage* abstractImage) -> int { - lua.PushNil(); - return 1; - } - - template - int AddComponentOfType(Nz::LuaInstance& lua, EntityHandle& handle) - { - static_assert(std::is_base_of::value, "ComponentType must inherit BaseComponent"); - - T& component = handle->AddComponent(); - lua.Push(component.CreateHandle()); - return 1; - } - - int AddComponent(Nz::LuaInstance& lua, EntityHandle& handle, ComponentIndex index) - { - std::vector componentAdder(BaseComponent::GetMaxComponentIndex(), AddNilComponent); - componentAdder[GraphicsComponent::componentIndex] = &AddComponentOfType; - componentAdder[NodeComponent::componentIndex] = &AddComponentOfType; - - if (index > componentAdder.size()) + unsigned int argCount = std::min(lua.GetStackTop(), 1U); + switch (argCount) { - lua.Error("Invalid component index"); - return 0; + case 0: + return lua.Push(abstractImage->GetMemoryUsage()); + + case 1: + { + int index = 1; + Nz::UInt8 level(lua.Check(&index)); + + return lua.Push(abstractImage->GetMemoryUsage(level)); + } } - return componentAdder[index](lua, handle); - } + lua.Error("No matching overload for method GetMemoryUsage"); + return 0; + }); - int PushNilComponent(Nz::LuaInstance& lua, BaseComponent&) + abstractImage.SetMethod("Update", [] (Nz::LuaInstance& lua, Nz::AbstractImage* abstractImage) -> int { - lua.PushNil(); - return 1; - } + unsigned int argCount = std::min(lua.GetStackTop(), 6U); + int argIndex = 1; - template - int PushComponentOfType(Nz::LuaInstance& lua, BaseComponent& component) - { - static_assert(std::is_base_of::value, "ComponentType must inherit BaseComponent"); + std::size_t bufferSize = 0; + const Nz::UInt8* pixels = reinterpret_cast(lua.CheckString(argIndex++, &bufferSize)); - T& rightComponent = static_cast(component); - lua.Push(rightComponent.CreateHandle()); - return 1; - } - - int PushComponent(Nz::LuaInstance& lua, BaseComponent& component) - { - std::vector componentConverter(BaseComponent::GetMaxComponentIndex(), PushNilComponent); - componentConverter[GraphicsComponent::componentIndex] = &PushComponentOfType; - componentConverter[NodeComponent::componentIndex] = &PushComponentOfType; - - ComponentIndex index = component.GetIndex(); - - if (index > componentConverter.size()) + if (argCount < 2 || lua.IsOfType(2, Nz::LuaType_Number)) { - lua.Error("Invalid component index"); - return 0; + // bool Update(const UInt8* pixels, unsigned int srcWidth = 0, unsigned int srcHeight = 0, UInt8 level = 0) + unsigned int srcWidth = lua.Check(&argIndex, 0); + unsigned int srcHeight = lua.Check(&argIndex, 0); + Nz::UInt8 level = lua.Check(&argIndex, 0); + + ///TODO: Buffer checks (Nz::ByteBufferView ?) + return lua.Push(abstractImage->Update(pixels, srcWidth, srcHeight, level)); } + /* Disabled until Box and Rect have been ported + else if (lua.IsOfType(2, "Box")) + { + // bool Update(const UInt8* pixels, const Boxui& box, unsigned int srcWidth = 0, unsigned int srcHeight = 0, UInt8 level = 0) + Nz::Boxui box = lua.Check(&argIndex); + unsigned int srcWidth = lua.Check(&argIndex, 0); + unsigned int srcHeight = lua.Check(&argIndex, 0); + Nz::UInt8 level = lua.Check(&argIndex, 0); - return componentConverter[index](lua, component); - } - } + ///TODO: Buffer checks (Nz::ByteBufferView ?) + return lua.Push(abstractImage->Update(pixels, srcWidth, srcHeight, level)); + } + else if (lua.IsOfType(2, "Rect")) + { + // bool Update(const UInt8* pixels, const Rectui& rect, unsigned int z = 0, unsigned int srcWidth = 0, unsigned int srcHeight = 0, UInt8 level = 0) + Nz::Rectui box = lua.Check(&argIndex); + unsigned int srcWidth = lua.Check(&argIndex, 0); + unsigned int srcHeight = lua.Check(&argIndex, 0); + Nz::UInt8 level = lua.Check(&argIndex, 0); - void LuaAPI::Register_SDK(Nz::LuaInstance& instance) - { - Nz::LuaClass graphicsComponent("GraphicsComponent"); + ///TODO: Buffer checks (Nz::ByteBufferView ?) + return lua.Push(abstractImage->Update(pixels, srcWidth, srcHeight, level)); + }*/ - graphicsComponent.SetMethod("Attach", &GraphicsComponent::Attach, 0); - - graphicsComponent.Register(instance); - - Nz::LuaClass nodeClass("Node"); //< TODO: Move to Utility + lua.Error("No matching overload for method Update"); + return 0; + }); + /*********************************** Nz::Node **********************************/ nodeClass.SetMethod("GetBackward", &Nz::Node::GetBackward); //nodeClass.SetMethod("GetChilds", &Nz::Node::GetChilds); nodeClass.SetMethod("GetDown", &Nz::Node::GetDown); @@ -245,69 +245,11 @@ namespace Ndk lua.Error("No matching overload for method SetInitialScale"); return 0; }); + } + void LuaBinding::RegisterUtility(Nz::LuaInstance& instance) + { + abstractImage.Register(instance); nodeClass.Register(instance); - - Nz::LuaClass nodeComponent("NodeComponent"); - nodeComponent.Inherit(nodeClass, [] (NodeComponentHandle* handle) -> Nz::Node* - { - return handle->GetObject(); - }); - - nodeComponent.Register(instance); - - Nz::LuaClass entityClass("Entity"); - - entityClass.SetMethod("Enable", &Entity::Enable); - entityClass.SetMethod("GetId", &Entity::GetId); - entityClass.SetMethod("GetWorld", &Entity::GetWorld); - entityClass.SetMethod("Kill", &Entity::Kill); - entityClass.SetMethod("IsEnabled", &Entity::IsEnabled); - entityClass.SetMethod("IsValid", &Entity::IsValid); - entityClass.SetMethod("RemoveComponent", (void(Entity::*)(ComponentIndex)) &Entity::RemoveComponent); - entityClass.SetMethod("RemoveAllComponents", &Entity::RemoveAllComponents); - entityClass.SetMethod("__tostring", &EntityHandle::ToString); - - entityClass.SetMethod("AddComponent", [] (Nz::LuaInstance& lua, EntityHandle& handle) -> int - { - int index = 1; - ComponentIndex componentIndex = lua.Check(&index); - - return AddComponent(lua, handle, componentIndex); - }); - - entityClass.SetMethod("GetComponent", [] (Nz::LuaInstance& lua, EntityHandle& handle) -> int - { - int index = 1; - ComponentIndex componentIndex = lua.Check(&index); - - if (!handle->HasComponent(componentIndex)) - { - lua.PushNil(); - return 1; - } - - return PushComponent(lua, handle->GetComponent(componentIndex)); - }); - - entityClass.Register(instance); - - Nz::LuaClass worldClass("World"); - - worldClass.SetMethod("CreateEntity", &World::CreateEntity); - worldClass.SetMethod("CreateEntities", &World::CreateEntities); - worldClass.SetMethod("Clear", &World::Clear); - - worldClass.Register(instance); - - instance.PushTable(0, 2); - { - instance.PushInteger(GraphicsComponent::componentIndex); - instance.SetField("Graphics"); - - instance.PushInteger(NodeComponent::componentIndex); - instance.SetField("Node"); - } - instance.SetGlobal("ComponentType"); } } \ No newline at end of file diff --git a/build/scripts/tools/ndk.lua b/build/scripts/tools/ndk.lua index ae0c99881..33c79c3fb 100644 --- a/build/scripts/tools/ndk.lua +++ b/build/scripts/tools/ndk.lua @@ -8,7 +8,8 @@ TOOL.Defines = { } TOOL.Includes = { - "../SDK/include" + "../SDK/include", + "../SDK/src" } TOOL.Files = { diff --git a/build/scripts/tools/ndk_server.lua b/build/scripts/tools/ndk_server.lua index 1f8102a41..6b6d32941 100644 --- a/build/scripts/tools/ndk_server.lua +++ b/build/scripts/tools/ndk_server.lua @@ -9,7 +9,8 @@ TOOL.Defines = { } TOOL.Includes = { - "../SDK/include" + "../SDK/include", + "../SDK/src" } TOOL.Files = { @@ -29,9 +30,9 @@ TOOL.FilesExclusion = { "../SDK/**/ListenerComponent.*", "../SDK/**/ListenerSystem.*", "../SDK/**/RenderSystem.*", - "../SDK/**/LuaAPI_Audio.*", - "../SDK/**/LuaAPI_Graphics.*", - "../SDK/**/LuaAPI_Renderer.*", + "../SDK/**/LuaBinding_Audio.*", + "../SDK/**/LuaBinding_Graphics.*", + "../SDK/**/LuaBinding_Renderer.*" } TOOL.Libraries = {