18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-11-27 21:45:16 +01:00

fix(scripting): typed scenery handles, sandbox keys from eu07.ini only, GCC 13 build

- the scenery API (eu07.events) hands out a full userdata of its own locked
  metatable for each kind of object (event, track, isolated, vehicle,
  memcell), checked with luaL_checkudata: a handle of another kind, a light
  userdata or any other value is a Lua error, not an object taken for
  another. nil stays "no object". one object has one handle while the
  script holds it, so handles compare and key tables as before. event
  handlers get their handles built inside lua_cpcall, and nil for no
  activator
- memcell_values keeps its text as a std::string: the text of a number was
  read from a stack slot popped before use
- newproxy is gone from the sandboxed libraries
- a config block of a scenery no longer changes what only eu07.ini sets
  (lua.fulllibraries, execonexit, extcam.cmd/rec, screenshotsdir,
  network.server/client, zmq.address, motiontelemetry, uart, feedbackmode,
  feedbackport, python/luascreens/wasm.enabled): kept before its keys are
  read, put back before FinalizeConfig(), each attempt logged
- open_file_library() iterates luaL_Reg instead of a std::pair GCC 13
  deduces as a pair of an array and a function
This commit is contained in:
jerrrrycho
2026-11-16 08:54:00 +01:00
parent fde6d10f32
commit 3ae8d4e72e
13 changed files with 571 additions and 35 deletions

View File

@@ -18,6 +18,9 @@ lua::lua()
// lua.fulllibraries in eu07.ini opens the rest when the scenery loads its first script (interpret())
lua_sandbox::open_safe_libraries(state);
lua_sandbox::open_clock_library(state);
// the events, tracks, isolated circuits, vehicles and memcells a script holds: a userdata of a metatable of each
// kind, which the functions check
lua_sandbox::open_handles(state);
static constexpr luaL_Reg api[] =
{
@@ -100,14 +103,33 @@ void lua::unref(lua_State *L, const int ref)
luaL_unref(L, LUA_REGISTRYINDEX, ref);
}
namespace {
struct event_call {
int handler;
basic_event *event;
TDynamicObject *activator;
};
// calls the handler of the event (event_call) with the handles of the event and its activator (nil when there is none)
int call_handler(lua_State *L)
{
auto const &call = *static_cast<event_call const *>(lua_touserdata(L, 1));
lua_rawgeti(L, LUA_REGISTRYINDEX, call.handler);
lua_sandbox::push_handle(L, lua_sandbox::handle_kind::event, call.event);
lua_sandbox::push_handle(L, lua_sandbox::handle_kind::vehicle, call.activator);
lua_call(L, 2, 0);
return 0;
}
} // namespace
/// Executes the callback function of an event created with `scriptapi_event_create`.
void lua::dispatch_event(lua_State *L, const int handler, basic_event *event, const TDynamicObject *activator)
{
lua_rawgeti(L, LUA_REGISTRYINDEX, handler);
lua_pushlightuserdata(L, event);
lua_pushlightuserdata(L, const_cast<TDynamicObject *>(activator));
// in protected mode: a handler that fails is logged, and the simulation goes on
if (lua_pcall(L, 2, 0, 0) != 0) {
event_call call{handler, event, const_cast<TDynamicObject *>(activator)};
// in protected mode, the handles made as well: a handler that fails is logged, and the simulation goes on
if (lua_cpcall(L, call_handler, &call) != 0) {
auto const *error = lua_tostring(L, -1);
ErrorLog("lua: event " + (event ? event->m_name : std::string("?")) + ": " + (error ? error : "unknown error"), logtype::lua);
lua_pop(L, 1);
@@ -133,12 +155,17 @@ memcell_values lua::get_memcell_values(lua_State *L, int idx)
// For negative indices, we need to account for the extra item that is the key name/value extracted from the table.
if (idx < 0)
idx--;
// no text in the table is an empty one: the memcell and the vehicle take the text as a string
luaL_checktype(L, idx, LUA_TTABLE);
// no text in the table is an empty one: the memcell and the vehicle take the text as a string. copied: the text of
// a number is made on the stack, and gone with it
memcell_values mc{"", 0.0, 0.0};
lua_pushstring(L, "str");
lua_gettable(L, idx);
if (lua_isstring(L, -1))
mc.str = lua_tostring(L, -1);
if (lua_isstring(L, -1)) {
std::size_t length = 0;
auto const *text = lua_tolstring(L, -1, &length);
mc.str.assign(text, length);
}
lua_pop(L, 1);
lua_pushstring(L, "num1");
lua_gettable(L, idx);
@@ -167,7 +194,7 @@ int lua::scriptapi_event_create(lua_State *L)
event->m_delayrandom = randomdelay;
if (simulation::Events.insert(event))
{
lua_pushlightuserdata(L, event);
lua_sandbox::push_handle(L, lua_sandbox::handle_kind::event, event);
return 1;
}
return 0;
@@ -179,7 +206,7 @@ int lua::scriptapi_event_find(lua_State *L)
basic_event *foundevent = simulation::Events.FindEvent(name);
if (foundevent)
{
lua_pushlightuserdata(L, foundevent);
lua_sandbox::push_handle(L, lua_sandbox::handle_kind::event, foundevent);
return 1;
}
ErrorLog("lua: missing event: " + name);
@@ -196,7 +223,7 @@ int lua::scriptapi_event_exists(lua_State *L)
int lua::scriptapi_event_getname(lua_State *L)
{
auto *event = static_cast<basic_event *>(lua_touserdata(L, 1));
auto *event = lua_sandbox::check_handle<basic_event>(L, 1, lua_sandbox::handle_kind::event);
if (event)
{
lua_pushstring(L, event->m_name.c_str());
@@ -207,8 +234,8 @@ int lua::scriptapi_event_getname(lua_State *L)
int lua::scriptapi_event_dispatch(lua_State *L)
{
auto *event = static_cast<basic_event *>(lua_touserdata(L, 1));
auto *activator = static_cast<TDynamicObject *>(lua_touserdata(L, 2));
auto *event = lua_sandbox::check_handle<basic_event>(L, 1, lua_sandbox::handle_kind::event);
auto *activator = lua_sandbox::check_handle<TDynamicObject>(L, 2, lua_sandbox::handle_kind::vehicle);
double delay = lua_tonumber(L, 3);
if (event)
simulation::Events.AddToQuery(event, activator, delay);
@@ -218,7 +245,7 @@ int lua::scriptapi_event_dispatch(lua_State *L)
int lua::scriptapi_event_dispatch_n(lua_State *L)
{
std::string name = luaL_checkstring(L, 1);
auto *activator = static_cast<TDynamicObject *>(lua_touserdata(L, 2));
auto *activator = lua_sandbox::check_handle<TDynamicObject>(L, 2, lua_sandbox::handle_kind::vehicle);
double delay = lua_tonumber(L, 3);
basic_event *foundevent = simulation::Events.FindEvent(name);
if (foundevent)
@@ -234,7 +261,7 @@ int lua::scriptapi_track_find(lua_State *L)
TTrack *track = simulation::Paths.find(name);
if (track)
{
lua_pushlightuserdata(L, track);
lua_sandbox::push_handle(L, lua_sandbox::handle_kind::track, track);
return 1;
}
ErrorLog("lua: missing track: " + name);
@@ -243,7 +270,7 @@ int lua::scriptapi_track_find(lua_State *L)
int lua::scriptapi_track_isoccupied(lua_State *L)
{
auto *track = static_cast<TTrack *>(lua_touserdata(L, 1));
auto *track = lua_sandbox::check_handle<TTrack>(L, 1, lua_sandbox::handle_kind::track);
if (track)
{
lua_pushboolean(L, !track->IsEmpty());
@@ -271,7 +298,7 @@ int lua::scriptapi_isolated_find(lua_State *L)
TIsolated *isolated = TIsolated::Find(name);
if (isolated)
{
lua_pushlightuserdata(L, isolated);
lua_sandbox::push_handle(L, lua_sandbox::handle_kind::isolated, isolated);
return 1;
}
ErrorLog("lua: missing isolated: " + name);
@@ -280,7 +307,7 @@ int lua::scriptapi_isolated_find(lua_State *L)
int lua::scriptapi_isolated_isoccupied(lua_State *L)
{
auto *isolated = static_cast<TIsolated *>(lua_touserdata(L, 1));
auto *isolated = lua_sandbox::check_handle<TIsolated>(L, 1, lua_sandbox::handle_kind::isolated);
if (isolated)
{
lua_pushboolean(L, isolated->Busy());
@@ -304,7 +331,7 @@ int lua::scriptapi_isolated_isoccupied_n(lua_State *L)
int lua::scriptapi_train_getname(lua_State *L)
{
auto *dyn = static_cast<TDynamicObject *>(lua_touserdata(L, 1));
auto *dyn = lua_sandbox::check_handle<TDynamicObject>(L, 1, lua_sandbox::handle_kind::vehicle);
if (dyn && dyn->Mechanik)
{
lua_pushstring(L, dyn->Mechanik->TrainName().c_str());
@@ -315,7 +342,7 @@ int lua::scriptapi_train_getname(lua_State *L)
int lua::scriptapi_dynobj_putvalues(lua_State *L)
{
auto *dyn = static_cast<TDynamicObject *>(lua_touserdata(L, 1));
auto *dyn = lua_sandbox::check_handle<TDynamicObject>(L, 1, lua_sandbox::handle_kind::vehicle);
auto [str, num1, num2] = get_memcell_values(L, 2);
if (!dyn)
return 0;
@@ -333,7 +360,7 @@ int lua::scriptapi_memcell_find(lua_State *L)
TMemCell *mc = simulation::Memory.find(str);
if (mc)
{
lua_pushlightuserdata(L, mc);
lua_sandbox::push_handle(L, lua_sandbox::handle_kind::memcell, mc);
return 1;
}
ErrorLog("lua: missing memcell: " + str);
@@ -342,7 +369,7 @@ int lua::scriptapi_memcell_find(lua_State *L)
int lua::scriptapi_memcell_read(lua_State *L)
{
auto *mc = static_cast<TMemCell *>(lua_touserdata(L, 1));
auto *mc = lua_sandbox::check_handle<TMemCell>(L, 1, lua_sandbox::handle_kind::memcell);
push_memcell_values(L, mc);
return 1;
}
@@ -359,7 +386,7 @@ int lua::scriptapi_memcell_read_n(lua_State *L)
int lua::scriptapi_memcell_update(lua_State *L)
{
auto *mc = static_cast<TMemCell *>(lua_touserdata(L, 1));
auto *mc = lua_sandbox::check_handle<TMemCell>(L, 1, lua_sandbox::handle_kind::memcell);
auto [str, num1, num2] = get_memcell_values(L, 2);
if (mc)
mc->UpdateValues(str, num1, num2,