diff --git a/ChangeLog.md b/ChangeLog.md index 7e108fe9b..db85ecb5d 100644 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -192,7 +192,7 @@ Nazara Engine: - Added VertexMapper::GetVertexCount() - Added VertexMapper::HasComponentOfType() - Fixed SimpleTextDrawer bounds computation - +- Added LuaState::Load methods which allows to load (compile) lua code to function without executing it. Nazara Development Kit: - ⚠️ Components no longer need to be copyable by assignation diff --git a/include/Nazara/Lua/LuaState.hpp b/include/Nazara/Lua/LuaState.hpp index 14f0898c2..5774933a4 100644 --- a/include/Nazara/Lua/LuaState.hpp +++ b/include/Nazara/Lua/LuaState.hpp @@ -112,6 +112,11 @@ namespace Nz bool IsOfType(int index, const String& tname) const; bool IsValid(int index) const; + bool Load(const String& code); + bool LoadFromFile(const String& filePath); + bool LoadFromMemory(const void* data, std::size_t size); + bool LoadFromStream(Stream& stream); + long long Length(int index) const; std::size_t LengthRaw(int index) const; diff --git a/src/Nazara/Lua/LuaState.cpp b/src/Nazara/Lua/LuaState.cpp index 67bf8efa0..e9b18eaef 100644 --- a/src/Nazara/Lua/LuaState.cpp +++ b/src/Nazara/Lua/LuaState.cpp @@ -355,39 +355,18 @@ namespace Nz if (code.IsEmpty()) return true; - if (luaL_loadstring(m_state, code.GetConstBuffer()) != 0) - { - m_lastError = lua_tostring(m_state, -1); - lua_pop(m_state, 1); - + if (!Load(code)) return false; - } - return Run(0, LUA_MULTRET); + return Call(0); } bool LuaState::ExecuteFromFile(const String& filePath) { - File file(filePath); - if (!file.Open(OpenMode_ReadOnly | OpenMode_Text)) - { - NazaraError("Failed to open file"); + if (!LoadFromFile(filePath)) return false; - } - std::size_t length = static_cast(file.GetSize()); - - String source(length, '\0'); - - if (file.Read(&source[0], length) != length) - { - NazaraError("Failed to read file"); - return false; - } - - file.Close(); - - return Execute(source); + return Call(0); } bool LuaState::ExecuteFromMemory(const void* data, std::size_t size) @@ -398,18 +377,10 @@ namespace Nz bool LuaState::ExecuteFromStream(Stream& stream) { - StreamData data; - data.stream = &stream; - - if (lua_load(m_state, StreamReader, &data, "C++", nullptr) != 0) - { - m_lastError = lua_tostring(m_state, -1); - lua_pop(m_state, 1); - + if (!LoadFromStream(stream)) return false; - } - return Run(0, LUA_MULTRET); + return Call(0); } int LuaState::GetAbsIndex(int index) const @@ -545,6 +516,65 @@ namespace Nz return lua_isnoneornil(m_state, index) == 0; } + bool LuaState::Load(const String& code) + { + if (luaL_loadstring(m_state, code.GetConstBuffer()) != 0) + { + m_lastError = lua_tostring(m_state, -1); + lua_pop(m_state, 1); + + return false; + } + + return true; + } + + bool LuaState::LoadFromFile(const String& filePath) + { + File file(filePath); + if (!file.Open(OpenMode_ReadOnly | OpenMode_Text)) + { + NazaraError("Failed to open file"); + return false; + } + + std::size_t length = static_cast(file.GetSize()); + + String source(length, '\0'); + + if (file.Read(&source[0], length) != length) + { + NazaraError("Failed to read file"); + return false; + } + + file.Close(); + + return Load(source); + } + + bool LuaState::LoadFromMemory(const void* data, std::size_t size) + { + MemoryView stream(data, size); + return LoadFromStream(stream); + } + + bool LuaState::LoadFromStream(Stream& stream) + { + StreamData data; + data.stream = &stream; + + if (lua_load(m_state, StreamReader, &data, "C++", nullptr) != 0) + { + m_lastError = lua_tostring(m_state, -1); + lua_pop(m_state, 1); + + return false; + } + + return true; + } + long long LuaState::Length(int index) const { return luaL_len(m_state, index);