From bbbef96087c371c4250d2d37a8df610faf594702 Mon Sep 17 00:00:00 2001 From: uramer Date: Tue, 31 Jan 2023 19:50:33 +0100 Subject: [PATCH] Switch to loadInternalLib --- apps/openmw/mwlua/uibindings.cpp | 2 +- .../openmw_test_suite/lua/test_ui_content.cpp | 8 +++----- components/CMakeLists.txt | 2 +- components/lua_ui/content.cpp | 20 ++++--------------- components/lua_ui/content.hpp | 4 +++- components/lua_ui/content.lua | 12 +++++++---- 6 files changed, 20 insertions(+), 28 deletions(-) diff --git a/apps/openmw/mwlua/uibindings.cpp b/apps/openmw/mwlua/uibindings.cpp index e113178dd4..04282f74ca 100644 --- a/apps/openmw/mwlua/uibindings.cpp +++ b/apps/openmw/mwlua/uibindings.cpp @@ -141,7 +141,7 @@ namespace MWLua luaManager->addAction([wm, obj = obj.as()] { wm->setConsoleSelectedObject(obj.ptr()); }); } }; - api["content"] = LuaUi::Content::makeFactory(context.mLua->sol()); + api["content"] = LuaUi::Content::loadConstructor(context.mLua); api["create"] = [context](const sol::table& layout) { auto element = LuaUi::Element::make(layout); context.mLuaManager->addAction(std::make_unique(UiAction::CREATE, element, context.mLua)); diff --git a/apps/openmw_test_suite/lua/test_ui_content.cpp b/apps/openmw_test_suite/lua/test_ui_content.cpp index cceb7b342a..10b67638fd 100644 --- a/apps/openmw_test_suite/lua/test_ui_content.cpp +++ b/apps/openmw_test_suite/lua/test_ui_content.cpp @@ -11,13 +11,11 @@ namespace struct LuaUiContentTest : Test { LuaUtil::LuaState mLuaState{ nullptr, nullptr }; - sol::state_view mSol; sol::protected_function mNew; LuaUiContentTest() - : mSol(mLuaState.sol()) - , mNew(LuaUi::Content::makeFactory(mSol)) { - mSol.open_libraries(sol::lib::base, sol::lib::table); + mLuaState.addInternalLibSearchPath("resources/lua_libs"); + mNew = LuaUi::Content::loadConstructor(&mLuaState); } LuaUi::Content::View makeContent(sol::table source) @@ -28,7 +26,7 @@ namespace return LuaUi::Content::View(result.get()); } - sol::table makeTable() { return sol::table(mSol, sol::create); } + sol::table makeTable() { return sol::table(mLuaState.sol(), sol::create); } sol::table makeTable(std::string name) { diff --git a/components/CMakeLists.txt b/components/CMakeLists.txt index c738019e14..0a61e9d5db 100644 --- a/components/CMakeLists.txt +++ b/components/CMakeLists.txt @@ -272,7 +272,7 @@ add_component_dir (lua_ui properties widget element util layers content alignment resources adapter text textedit window image container flex ) -list (APPEND OPENMW_FILES "lua_ui/content.lua") +copy_resource_file("lua_ui/content.lua" "${OPENMW_RESOURCES_ROOT}" "resources/lua_libs/content.lua") if(WIN32) diff --git a/components/lua_ui/content.cpp b/components/lua_ui/content.cpp index ba9d8b6805..01dfe9faf6 100644 --- a/components/lua_ui/content.cpp +++ b/components/lua_ui/content.cpp @@ -2,23 +2,11 @@ namespace LuaUi::Content { - namespace + sol::protected_function loadConstructor(LuaUtil::LuaState* state) { - sol::table loadMetatable(sol::state_view sol) - { - std::string scriptBody = -#include "content.lua" - ; - auto result = sol.safe_script(scriptBody); - if (result.get_type() != sol::type::table) - throw std::logic_error("Expected a meta table"); - return result.get(); - } - } - - sol::protected_function makeFactory(sol::state_view sol) - { - sol::table metatable = loadMetatable(sol); + sol::function loader = state->loadInternalLib("content"); + sol::set_environment(state->newInternalLibEnvironment(), loader); + sol::table metatable = loader().get(); if (metatable["new"].get_type() != sol::type::function) throw std::logic_error("Expected function"); return metatable["new"].get(); diff --git a/components/lua_ui/content.hpp b/components/lua_ui/content.hpp index 8b641037ef..85c8a3a8fc 100644 --- a/components/lua_ui/content.hpp +++ b/components/lua_ui/content.hpp @@ -6,9 +6,11 @@ #include +#include + namespace LuaUi::Content { - sol::protected_function makeFactory(sol::state_view); + sol::protected_function loadConstructor(LuaUtil::LuaState* state); class View { diff --git a/components/lua_ui/content.lua b/components/lua_ui/content.lua index 650a15ecc6..7fe1d2cff1 100644 --- a/components/lua_ui/content.lua +++ b/components/lua_ui/content.lua @@ -1,4 +1,3 @@ -R"( local M = {} M.__Content = true M.new = function(source) @@ -23,6 +22,7 @@ local function validateIndex(self, index) error('Invalid Content index: ' .. tostring(index)) end end + local function getIndexFromKey(self, key) local index = key if type(key) == 'string' then @@ -34,6 +34,7 @@ local function getIndexFromKey(self, key) validateIndex(self, index) return index end + local methods = { insert = function(self, index, value) validateIndex(self, index) @@ -78,6 +79,7 @@ local function nameAt(self, index) local v = rawget(self, index) return v and type(v.name) == 'string' and v.name end + local function remove(self, index) local oldName = nameAt(self, index) if oldName then @@ -95,6 +97,7 @@ local function remove(self, index) end rawset(self, #self, nil) end + local function assign(self, index, value) local oldName = nameAt(self, index) if oldName then @@ -105,6 +108,7 @@ local function assign(self, index, value) self.__nameIndex[value.name] = index end end + M.__newindex = function(self, key, value) local index = getIndexFromKey(self, key) if value == nil then @@ -126,14 +130,14 @@ local function next(self, index) return nil, nil end end + M.__pairs = function(self) return next, self, 1 end M.__ipairs = M.__pairs M.__metatable = {} -assert(not pcall(function() setmetatable(M.new({}), {}) end), 'Metatable is not protected') -assert(getmetatable(M.new) ~= M, 'Metatable is not protected') +assert(not pcall(function() setmetatable(M.new {}, {}) end), 'Metatable is not protected') +assert(getmetatable(M.new {}) ~= M, 'Metatable is not protected') return M -)" \ No newline at end of file