Skip to content

Commit

Permalink
Set package path and reload
Browse files Browse the repository at this point in the history
  • Loading branch information
chreden committed Jul 21, 2024
1 parent 972f341 commit 78ac425
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 7 deletions.
24 changes: 18 additions & 6 deletions trview.app/Lua/Lua.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<IDialogs>& dialogs, const std::shared_ptr<IFiles>& 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()
Expand Down Expand Up @@ -121,6 +116,7 @@ namespace trview

void Lua::initialise(IApplication* application)
{
create_state();
ILua** userdata = static_cast<ILua**>(lua_newuserdata(L, sizeof(this)));
*userdata = this;
lua_pushcclosure(L, print, 1);
Expand All @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions trview.app/Lua/Lua.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
12 changes: 11 additions & 1 deletion trview.app/Plugins/Plugin.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#include "Plugin.h"
#include <algorithm>
#include <format>

namespace trview
{
Expand Down Expand Up @@ -50,6 +52,7 @@ namespace trview
void Plugin::initialise(IApplication* application)
{
_lua->initialise(application);
set_package_path();
load_script();
}

Expand Down Expand Up @@ -86,7 +89,7 @@ namespace trview
void Plugin::reload()
{
load();
load_script();
initialise(_application);
}

void Plugin::load()
Expand Down Expand Up @@ -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));
}
}
2 changes: 2 additions & 0 deletions trview.app/Plugins/Plugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ namespace trview
void load();
void load_script();
void register_print();
void set_package_path();

std::shared_ptr<IFiles> _files;
std::unique_ptr<ILua> _lua;
Expand All @@ -42,5 +43,6 @@ namespace trview
std::string _script;
std::string _messages;
TokenStore _token_store;
IApplication* _application;
};
}

0 comments on commit 78ac425

Please sign in to comment.