mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-11-27 22:55:16 +01:00
The scenery scripts (lua <file> in the .scn) ran in a state with all of
LuaJIT's libraries (io, os, ffi, debug, require, load*), loaded with
luaL_dofile from any path, '..' and absolute ones included, and their
errors in event handlers went through an unprotected lua_call.
- scripting/luasandbox.{h,cpp}: what the Lua states share: the safe
library set (moved from lua_program::open_libraries), loading a text-only
chunk (moved from load_chunk), reading a script from a folder through the
screens' path check (regular file, at most 16 MB), running it under pcall,
and a clock-only os table (time, clock, difftime, date with the C89
conversions; MSVC's strftime ends the program on an unknown one).
- lua.cpp: the scenery state gets the safe set and the clock; interpret()
takes the scenery folder and the file and logs a refused or failing script
with its file name; event handlers run under pcall and are logged with the
event name; the scriptapi functions check their string argument
(luaL_checkstring) instead of making a std::string from NULL; a memcell
table without str updates the text to "" instead of from NULL.
- luaprogram.cpp: calls the shared helpers, no change for the screens.
- eu07.ini lua.fulllibraries (default no) gives trusted sceneries all
libraries back. Documented in script_screens_doc.md.
- tests/unit/test_lua_sandbox.cpp: 8 cases for the shared sandbox.
53 lines
2.0 KiB
C++
53 lines
2.0 KiB
C++
#pragma once
|
|
#include <lua.hpp>
|
|
#include <stdafx.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
|
|
|
|
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);
|
|
};
|