diff --git a/script_screens_doc.md b/script_screens_doc.md index 09a4c2638..4cdc5badd 100644 --- a/script_screens_doc.md +++ b/script_screens_doc.md @@ -216,7 +216,7 @@ and works with memcells, tracks and vehicles through the table `eu07.events` (`e `memcell_update`, `writelog` and the rest, see `scripting/lua.cpp`). It shares the sandbox of the Lua screens: - the file is a path relative to the scenery folder (`scenery/`), and stays inside it: no `..`, no drive, no leading - `/`. It is text, of at most 16 MB; precompiled code is not loaded; + `/`. `/` and `\` both separate folders, on Linux too. It is text, of at most 16 MB; precompiled code is not loaded; - the script gets the base library, `table`, `string`, `math` and `bit`, and of `os` only the clock: `os.time`, `os.clock`, `os.difftime` and `os.date` (with the C89 conversions only, `%Y`, `%d`, `%H` and so on). It gets no `io`, the rest of `os`, `ffi`, `debug`, `jit`, `package`, `require`, `dofile`, `load`, `loadfile` or `loadstring`; diff --git a/scripting/luasandbox.cpp b/scripting/luasandbox.cpp index 8d80cdebf..ea5f98a88 100644 --- a/scripting/luasandbox.cpp +++ b/scripting/luasandbox.cpp @@ -12,6 +12,7 @@ http://mozilla.org/MPL/2.0/. #include "scripting/scriptscreenrules.h" +#include #include #include #include @@ -59,6 +60,13 @@ int api_date(lua_State *L) } // namespace +std::string with_forward_slashes(std::string_view const Path) +{ + std::string result{Path}; + std::ranges::replace(result, '\\', '/'); + return result; +} + void open_safe_libraries(lua_State *L) { open_library(L, luaopen_base, ""); @@ -121,7 +129,7 @@ std::optional read_script_file(std::string const &Folder, std::stri } // a folder or a device (CON, NUL, COM1...) isn't a regular file. in the code page of the system, as the game opens // its files - std::filesystem::path const path{Folder + std::string{Path}}; + std::filesystem::path const path{Folder + with_forward_slashes(Path)}; std::error_code error; if (false == std::filesystem::is_regular_file(path, error)) { diff --git a/scripting/luasandbox.h b/scripting/luasandbox.h index abdd21933..167060026 100644 --- a/scripting/luasandbox.h +++ b/scripting/luasandbox.h @@ -38,8 +38,13 @@ void open_clock_library(lua_State *L); // stack bool load_text_chunk(lua_State *L, std::string_view Code, std::string const &Name); +// Path with each '\' turned into '/', as the game reads the paths in its data (replace_slashes): a scenery made on +// Windows may name its folders either way, and on Linux only '/' separates them +std::string with_forward_slashes(std::string_view Path); + // the file Path of Folder, when Path is a relative path inside Folder (no '..', no drive, no leading separator) and -// names a regular file of at most max_script_file bytes. nothing otherwise +// names a regular file of at most max_script_file bytes. nothing otherwise. '\' separates folders in Path as '/' does, +// on every system std::optional read_script_file(std::string const &Folder, std::string_view Path); // reads the script Path of Folder (as read_script_file), loads it as text and runs it in protected mode. nothing when diff --git a/tests/unit/test_lua_sandbox.cpp b/tests/unit/test_lua_sandbox.cpp index f23cb0406..6768e27ed 100644 --- a/tests/unit/test_lua_sandbox.cpp +++ b/tests/unit/test_lua_sandbox.cpp @@ -148,8 +148,11 @@ TEST_CASE("lua sandbox: a scenery reads its scripts only from its own folder") { CHECK(lua_sandbox::read_script_file(folder, "test.lua") == std::optional{"return 2"}); CHECK(lua_sandbox::read_script_file(folder, "scripts/module.lua") == std::optional{"return 3"}); + // '\' separates the folders on Linux too, as in the rest of the scenery's paths + CHECK(lua_sandbox::with_forward_slashes("a\\b/c\\d.lua") == "a/b/c/d.lua"); CHECK(lua_sandbox::read_script_file(folder, "scripts\\module.lua") == std::optional{"return 3"}); for (auto const &path : {std::string{"../outside.lua"}, std::string{"scripts/../../outside.lua"}, std::string{"..\\outside.lua"}, + std::string{"scripts\\..\\..\\outside.lua"}, std::string{"scripts/..\\../outside.lua"}, root.path("outside.lua"), std::string{"/outside.lua"}, std::string{"\\outside.lua"}, std::string{"C:outside.lua"}, std::string{"missing.lua"}, std::string{"folder.lua"}, std::string{"scripts"}, std::string{}}) { CAPTURE(path);