From 78ac425a560c0f0460255b5a96b9a611bd15bb2d Mon Sep 17 00:00:00 2001 From: chreden <4263940+chreden@users.noreply.github.com> Date: Sun, 21 Jul 2024 11:15:01 +0100 Subject: [PATCH] Set package path and reload --- trview.app/Lua/Lua.cpp | 24 ++++++++++++++++++------ trview.app/Lua/Lua.h | 2 ++ trview.app/Plugins/Plugin.cpp | 12 +++++++++++- trview.app/Plugins/Plugin.h | 2 ++ 4 files changed, 33 insertions(+), 7 deletions(-) diff --git a/trview.app/Lua/Lua.cpp b/trview.app/Lua/Lua.cpp index dff58ca5b..b79d45e73 100644 --- a/trview.app/Lua/Lua.cpp +++ b/trview.app/Lua/Lua.cpp @@ -71,12 +71,7 @@ namespace trview Lua::Lua(const IRoute::Source& route_source, const IRandomizerRoute::Source& randomizer_route_source, const IWaypoint::Source& waypoint_source, const std::shared_ptr& dialogs, const std::shared_ptr& files) : _route_source(route_source), _randomizer_route_source(randomizer_route_source), _waypoint_source(waypoint_source), _dialogs(dialogs), _files(files) { - L = luaL_newstate(); - for (const auto& lib : loadedlibs) - { - luaL_requiref(L, lib.name, lib.func, 1); - lua_pop(L, 1); - } + create_state(); } Lua::~Lua() @@ -121,6 +116,7 @@ namespace trview void Lua::initialise(IApplication* application) { + create_state(); ILua** userdata = static_cast(lua_newuserdata(L, sizeof(this))); *userdata = this; lua_pushcclosure(L, print, 1); @@ -138,6 +134,22 @@ namespace trview _directory = directory; } + void Lua::create_state() + { + if (L) + { + lua_close(L); + L = nullptr; + } + + L = luaL_newstate(); + for (const auto& lib : loadedlibs) + { + luaL_requiref(L, lib.name, lib.func, 1); + lua_pop(L, 1); + } + } + namespace lua { int push_string(lua_State* L, const std::string& text) diff --git a/trview.app/Lua/Lua.h b/trview.app/Lua/Lua.h index 59edac23a..5ca681556 100644 --- a/trview.app/Lua/Lua.h +++ b/trview.app/Lua/Lua.h @@ -29,6 +29,8 @@ namespace trview void initialise(IApplication* application) override; void set_directory(const std::string& directory) override; private: + void create_state(); + lua_State* L{ nullptr }; IRoute::Source _route_source; IRandomizerRoute::Source _randomizer_route_source; diff --git a/trview.app/Plugins/Plugin.cpp b/trview.app/Plugins/Plugin.cpp index f87bc8934..6dd520354 100644 --- a/trview.app/Plugins/Plugin.cpp +++ b/trview.app/Plugins/Plugin.cpp @@ -1,4 +1,6 @@ #include "Plugin.h" +#include +#include namespace trview { @@ -50,6 +52,7 @@ namespace trview void Plugin::initialise(IApplication* application) { _lua->initialise(application); + set_package_path(); load_script(); } @@ -86,7 +89,7 @@ namespace trview void Plugin::reload() { load(); - load_script(); + initialise(_application); } void Plugin::load() @@ -130,4 +133,11 @@ namespace trview { _lua->execute("if render_ui ~= nil then render_ui() end"); } + + void Plugin::set_package_path() + { + std::string escaped = _path; + std::ranges::replace(escaped, '\\', '/'); + _lua->execute(std::format("package.path = \"{}/?.lua\"", escaped)); + } } diff --git a/trview.app/Plugins/Plugin.h b/trview.app/Plugins/Plugin.h index 1564e19b2..33e9ee0f8 100644 --- a/trview.app/Plugins/Plugin.h +++ b/trview.app/Plugins/Plugin.h @@ -32,6 +32,7 @@ namespace trview void load(); void load_script(); void register_print(); + void set_package_path(); std::shared_ptr _files; std::unique_ptr _lua; @@ -42,5 +43,6 @@ namespace trview std::string _script; std::string _messages; TokenStore _token_store; + IApplication* _application; }; }