mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-11-28 01:07:07 +01:00
feat(scripting): file functions for the scenery Lua scripts
The sandbox took io away from the scenery scripts; this gives them files
back the way the Lua screens have them. The screens have
eu07.read_file(path); the scenery state now gets it too, together with a
write function and one to read the written files back:
- eu07.read_file(path): a file of the scenery folder (scenery/), or nil.
The path rule of `lua <file>` and the screens (relative, no '..', no
drive, no leading separator), a regular file of at most 16 MB.
- eu07.write_file(path, text): writes the file in the scenery's data
folder, replacing it, and makes the folders of path. true, or nil and
the reason.
- eu07.read_data_file(path): a file of the data folder, or nil.
The data folder is per scenery and among the player's files, next to the
user eu07.ini and eu07_odometers.txt (user_config_path):
%APPDATA%/MaSzyna/scenery_data/<scenery without .scn>/, ~/.config/MaSzyna/
on Linux. Nothing is written into the game folder; with no user folder,
writes are refused. The log names the folder when the scenery loads its
first script.
Writes are limited to 1 MB a file, 256 files and 16 MB a data folder, and
to relative paths of at most 200 bytes and 8 folders, of portable names:
no '.', '..', empty names, :<>"|?* or control characters, no trailing dot
or space, no Windows device names (con, nul, com1, lpt1...). The parent
folder is checked with canonical paths, so a link in the data folder
doesn't lead a write out of it.
The code is in scripting/luasandbox.{h,cpp} (read_file, which
read_script_file now uses, is_safe_write_path, write_data_file,
scenery_data_folder, open_file_library); lua.cpp only opens the functions
and sets the folders in interpret(). The screens keep their own
read_screen_file, which #356 changes. Documented in the "Scenery scripts"
section of script_screens_doc.md; 8 new cases in test_lua_sandbox.cpp.
This commit is contained in:
@@ -6,6 +6,7 @@
|
||||
#include "vehicle/Driver.h"
|
||||
#include "simulation/simulation.h"
|
||||
#include "scripting/luasandbox.h"
|
||||
#include "utilities/utilities.h"
|
||||
|
||||
lua::lua()
|
||||
{
|
||||
@@ -45,6 +46,9 @@ lua::lua()
|
||||
{nullptr, nullptr}
|
||||
};
|
||||
luaL_register(state, "eu07.events", api);
|
||||
lua_pop(state, 1);
|
||||
// eu07.read_file, eu07.write_file and eu07.read_data_file, the files of the scenery in place of io
|
||||
lua_sandbox::open_file_library(state, &files);
|
||||
}
|
||||
|
||||
lua::~lua()
|
||||
@@ -67,6 +71,15 @@ void lua::interpret(const std::string &folder, const std::string &file)
|
||||
full_libraries = true;
|
||||
WriteLog("lua: lua.fulllibraries is on, the scenery scripts run with all libraries (io, os, ffi, require...)", logtype::lua);
|
||||
}
|
||||
// the scripts read the files of the scenery folder, and write theirs into a folder of the scenery among the
|
||||
// player's files (%APPDATA%/MaSzyna, ~/.config/MaSzyna), never into the game
|
||||
files.scenery = folder;
|
||||
if (auto data = lua_sandbox::scenery_data_folder(user_config_path("scenery_data"), Global.SceneryFile); data != files.data) {
|
||||
files.data = std::move(data);
|
||||
auto const name = files.data.u8string();
|
||||
if (!name.empty())
|
||||
WriteLog("lua: the scenery scripts write their files (eu07.write_file) to \"" + std::string(name.begin(), name.end()) + "\"", logtype::lua);
|
||||
}
|
||||
// a relative path inside the scenery folder, a file of text, run in protected mode: a script that fails stops
|
||||
// there, and the scenery goes on loading
|
||||
if (auto const error = lua_sandbox::run_script_file(state, folder, file)) {
|
||||
|
||||
Reference in New Issue
Block a user