18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-11-27 22:55:16 +01:00
Files
maszyna/scripting/scriptscreenrules.h
jerrrrycho f6e9075a73 fix(scripting): a %b counts the way it walks along the subject
A %bxy at the end of a pattern (or before what always matches) was costed
as one pass over the subject, but matchbalance walks to the end of the
subject from every opening character nothing closes: ("("):rep(1e6):find("%b()")
was let through and ran for hours in C, past the instruction hook.

A %b now multiplies wherever it stands. With the subject at hand it counts
the longest way from an opening character to its closing one, or, when it is
tried once from each position, the ways from all of them over the positions:
a long text of short balanced pairs stays cheap, one that doesn't balance is
refused.
2026-11-18 19:29:25 +01:00

109 lines
5.8 KiB
C++

/*
This Source Code Form is subject to the
terms of the Mozilla Public License, v.
2.0. If a copy of the MPL was not
distributed with this file, You can
obtain one at
http://mozilla.org/MPL/2.0/.
*/
#pragma once
#include <cstdint>
#include <optional>
#include <string>
#include <string_view>
class cParser;
// the parts of the script screens (Lua, WebAssembly) that don't need a runtime: the .mmd entries, the frame
// schedule and the file access rules. kept apart from scripting/scriptscreens.cpp so the unit tests can check them
namespace script_screens
{
// a script screen as the .mmd file describes it:
// luascreen: { script.lua target: submodel frames: 4 parameters: key=value&key=value }
// luadestinationsign: { script.lua frames: 30 instance: name parameters: ... background: texture }
// and the same with wasmscreen:, wasmdestinationsign: and a .wasm module
// every key but the script is optional, and an unknown key is skipped along with its value
struct screen_config
{
std::string script; // the .lua script or .wasm module, relative to the vehicle folder when it has no path of its own
std::string target{"none"}; // cab screens: the submodel the screen is drawn on
std::string parameters; // extra key=value pairs passed to the screen with the simulation state
std::string instancing; // destination signs: "name" or "type", one sign per vehicle or per vehicle type
std::string background; // destination signs: the texture shown while the sign is off
int frames{0}; // the screen's own refresh interval in frames, used when longer than the one set in eu07.ini
void deserialize(cParser &Input);
private:
bool deserialize_mapping(cParser &Input);
};
// how often a screen gets fresh data, counted in rendered frames
class frame_schedule
{
public:
frame_schedule() = default;
// Start: the frame the screen was created on. the first refresh comes Phase frames later
frame_schedule(std::uint64_t Start, int Interval, int Phase);
// the interval of a screen: the one set in eu07.ini, or the screen's own one when that is longer. at least 1
static int effective_interval(int Configured, int Requested);
// a fixed offset in [0, Interval) taken from the screen's name, so that the screens sharing an interval
// don't all refresh on the same frame
static int phase(std::string_view Key, int Interval);
// true when the screen is due on Frame, and then not again until Interval frames later. a screen that
// wasn't asked for a while (a vehicle out of sight) is due on the first frame it is asked again
bool poll(std::uint64_t Frame);
int interval() const
{
return m_interval;
}
private:
int m_interval{1};
std::uint64_t m_next{0};
};
// true for a file a screen may read: a relative path inside the screen's folder, without '..', without a
// drive or a leading separator
bool is_safe_relative_path(std::string_view Path);
// the path the game opens the script of a screen by: a name without a folder is in the vehicle's folder (Folder), a
// path is from the game's folder. nothing for a path that leads outside the game's folder (as for the files a screen
// reads: absolute, with a drive, or with ".."), since the script's folder is the one the screen then reads files from
std::optional<std::string> screen_script_path(std::string const &Folder, std::string const &Script);
// the Lua pattern functions (string.find, match, gmatch, gsub) run in C, where the instruction limit doesn't reach, and
// backtrack: ("a"):rep(1e6):find(".-b") would take hours. a call is refused when the steps it may take at worst, by
// lua_pattern_cost(), are over this: LuaJIT 2.0 goes through the worst patterns that cost as much (a):rep(5770) and
// .-b) in about a tenth of a second
constexpr double max_pattern_cost{1e8};
// the steps a Lua pattern function may take at worst on a subject of Subject bytes, Pattern being m bytes long:
// - a plain search (string.find with plain set, or a pattern without special characters): Subject x m;
// - a pattern: tried from every position of the subject (Subject + 1), or from the first only when Anchored (it starts
// with '^', and the function anchors: find, match, gsub, not gmatch), times (Subject + 1) for each repetition ('*',
// '+', '-', %b, a back reference %1-%9) but one followed by what always matches (nothing, ')', a repetition with '*'
// or '-' or a '?' of what always matches, '.*' / '.-' before '$'), where the match is over: so neither the last
// repetition nor the first %s* of "^%s*(.-)%s*$" multiplies. times 2 for each '?', times m for the work of each step
// (a set is gone through item by item)
// - a %bxy goes along the subject on its own, whatever follows it, so it counts where it ends the pattern as well:
// ("("):rep(1e6):find("%b()") walks to the end of the subject from each of its characters
// a malformed pattern costs nothing here: the function raises its error itself
double lua_pattern_cost(std::string_view Pattern, std::size_t Subject, bool Plain, bool Anchorable);
// as above, on the subject itself: a repetition of a character or a %class right before the final '$' ("%s*$") counts
// as many times as the longest run of it in the subject (plus one), not the whole subject: past that run it fails or
// gets to the end, so the trim "^%s*(.-)%s*$" costs about the subject times its longest run of spaces
// a %bxy counts as many times as the longest way from an opening character to the one that closes it (to the end of
// the subject when nothing does), plus one; when only captures are before it in a pattern tried from every position,
// each opening character is tried once, and it's the ways from all of them over the positions of the subject. so
// "%b()" on a long text of short balanced pairs is cheap, and on one that doesn't balance it's the subject squared
double lua_pattern_cost(std::string_view Pattern, std::string_view Subject, bool Plain, bool Anchorable);
} // namespace script_screens