18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-11-27 22:55:16 +01:00
Files
maszyna/scripting/lua.h
jerrrrycho fde6d10f32 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.
2026-11-13 18:55:19 +01:00

57 lines
2.2 KiB
C++

#pragma once
#include <lua.hpp>
#include <stdafx.h>
#include "scripting/luasandbox.h"
class basic_event;
class TMemCell;
class TDynamicObject;
struct memcell_values { const char *str; double num1; double num2; };
class lua
{
lua_State *state;
bool full_libraries = false; // lua.fulllibraries: all of LuaJIT's libraries opened
// what eu07.read_file reads (the scenery folder) and eu07.write_file writes (the scenery's data folder), known
// when the scenery loads its scripts (interpret())
lua_sandbox::file_folders files;
static int atpanic(lua_State *s);
static int scriptapi_event_create(lua_State *L);
static int scriptapi_event_find(lua_State *L);
static int scriptapi_event_exists(lua_State *L);
static int scriptapi_event_getname(lua_State *L);
static int scriptapi_event_dispatch(lua_State *L);
static int scriptapi_event_dispatch_n(lua_State *L);
static int scriptapi_track_find(lua_State *L);
static int scriptapi_track_isoccupied(lua_State *L);
static int scriptapi_track_isoccupied_n(lua_State *L);
static int scriptapi_isolated_find(lua_State *L);
static int scriptapi_isolated_isoccupied(lua_State *L);
static int scriptapi_isolated_isoccupied_n(lua_State *L);
static int scriptapi_train_getname(lua_State *L);
static int scriptapi_dynobj_putvalues(lua_State *L);
static int scriptapi_memcell_find(lua_State *L);
static int scriptapi_memcell_read(lua_State *L);
static int scriptapi_memcell_read_n(lua_State *L);
static int scriptapi_memcell_update(lua_State *L);
static int scriptapi_memcell_update_n(lua_State *L);
static int scriptapi_random(lua_State *L);
static int scriptapi_writelog(lua_State *L);
static int scriptapi_writeerrorlog(lua_State *L);
public:
lua();
~lua();
std::string get_error() const;
// runs the script file, a relative path inside folder (the scenery folder), sandboxed (scripting/luasandbox.h).
// a script that can't be read or fails is logged
void interpret(const std::string &folder, const std::string &file);
static void unref(lua_State *L, int ref);
static void dispatch_event(lua_State *L, int handler, basic_event *event, const TDynamicObject *activator);
static void push_memcell_values(lua_State *L, const TMemCell *mc);
static memcell_values get_memcell_values(lua_State *L, int idx);
};