mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-11-27 21:45:16 +01:00
- Lua: once the instruction budget of a call is spent, the count hook runs at every instruction and raises again, so pcall, nested pcall and coroutine.resume can't swallow the limit; xpcall is rebuilt on pcall so its handler runs after the error left f, while the hook works - Lua: the metatables of eu07.image/font/sprite are locked (__metatable = false); __gc destroys only an object of its own type, once - Lua: read_png checks the size with stbi_info before decoding - Lua: string.rep makes no string over 1 MiB - Lua and WASM: at most 64 commands per call, names up to 64 bytes, 32 log lines per call cut at 1024 bytes; the drops are logged once - fonts: a glyph wider than 4096 is skipped; more than 4096 glyphs, 8192 columns or an image side over 4096 refuse the font; cut_glyph reads only the columns inside the image - the script path from the .mmd (cab screens, destination signs, make: textures) must stay inside the game folder; the files a screen reads, its script and its module must be regular files of 16 MiB at most Tests for each in test_script_screen_host.cpp, test_script_screen_drawing.cpp and test_script_screens.cpp; the test stubs can capture the log.
130 lines
4.5 KiB
C++
130 lines
4.5 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
|
|
|
|
// the inside of the script screens: what the queue (scripting/scriptscreens.cpp) asks of a language runtime
|
|
// (scripting/luaprogram.cpp, scripting/wasmprogram.cpp), and the helpers they share
|
|
|
|
#include "scripting/scriptscreens.h"
|
|
|
|
#include <cstddef>
|
|
#include <cstdint>
|
|
#include <memory>
|
|
#include <optional>
|
|
#include <string>
|
|
#include <string_view>
|
|
#include <utility>
|
|
#include <vector>
|
|
|
|
namespace script_screens
|
|
{
|
|
|
|
// the largest side of a screen image, in pixels
|
|
constexpr int max_image_side{4096};
|
|
// the most instructions one call into a screen may run, so that a script stuck in a loop can't hang the worker
|
|
constexpr std::int64_t instruction_limit{200'000'000};
|
|
// what one call into a screen may send and log; the rest is dropped, which the log tells once per screen. the longest
|
|
// name of a command the engine knows is under 40 characters
|
|
constexpr std::size_t max_commands_per_call{64};
|
|
constexpr std::size_t max_command_name{64};
|
|
constexpr int max_log_lines_per_call{32};
|
|
constexpr std::size_t max_log_line{1024}; // bytes of the text, a longer one is cut
|
|
// the largest file a screen reads: its script or module, a file below its folder
|
|
constexpr std::uintmax_t max_screen_file{16 * 1024 * 1024};
|
|
|
|
struct pending_command
|
|
{
|
|
std::string name;
|
|
std::int32_t param1{0};
|
|
std::int32_t param2{0};
|
|
std::uint32_t vehicle{0};
|
|
};
|
|
|
|
// one refresh of a screen: what it gets and what it gives back
|
|
struct render_call
|
|
{
|
|
dictionary_source const &input;
|
|
std::uint32_t vehicle{0}; // the target of the commands
|
|
std::vector<pending_command> commands; // sent during the call
|
|
// the image drawn, RGBA, from the top row down; empty when the screen didn't change
|
|
std::vector<std::uint8_t> pixels;
|
|
int width{0};
|
|
int height{0};
|
|
// the post-processing still to run on the image (image:led_post, image:resize of the Lua screens)
|
|
std::vector<post_step> post;
|
|
};
|
|
|
|
// a running script: a Lua state, a WebAssembly instance. made, called and destroyed on the worker thread, or by the
|
|
// queue once the worker stopped
|
|
class program
|
|
{
|
|
public:
|
|
program() = default;
|
|
virtual ~program() = default;
|
|
program(program const &) = delete;
|
|
program &operator=(program const &) = delete;
|
|
// runs a refresh. false when the screen can't run (any more); the reason is in the log
|
|
virtual bool render(render_call &Call) = 0;
|
|
};
|
|
|
|
// a language: started and stopped with the queue
|
|
class runtime
|
|
{
|
|
public:
|
|
runtime() = default;
|
|
virtual ~runtime() = default;
|
|
runtime(runtime const &) = delete;
|
|
runtime &operator=(runtime const &) = delete;
|
|
// loads the script of a screen and runs its setup. nullptr when it fails; the reason is in the log
|
|
virtual std::unique_ptr<program> load(std::string const &Script, script_screen_kind Kind) = 0;
|
|
// called on the worker thread when it starts and before it ends
|
|
virtual void attach_thread() {}
|
|
virtual void detach_thread() {}
|
|
};
|
|
|
|
std::unique_ptr<runtime> make_lua_runtime();
|
|
std::unique_ptr<runtime> make_wasm_runtime();
|
|
|
|
// the text as UTF-8: names of sceneries and vehicles may come in windows-1250
|
|
std::string utf8_text(std::string const &Text);
|
|
// a file of the screen's folder or below; nothing for a path leading outside, or no such file
|
|
std::optional<std::string> read_screen_file(std::string const &Folder, std::string_view Path);
|
|
// the file, when it is a regular file of at most max_screen_file bytes: not a folder, a device or anything larger
|
|
std::optional<std::string> read_regular_file(std::string const &Path);
|
|
|
|
// what a screen sends and logs, held to the limits of one call. Name: how the log names the screen
|
|
class output_limits
|
|
{
|
|
public:
|
|
explicit output_limits(std::string Name) : m_name{std::move(Name)} {}
|
|
|
|
// a new call into the screen
|
|
void start_call()
|
|
{
|
|
m_log_lines = 0;
|
|
}
|
|
// true when the command may go with the call; a command over the limits is dropped
|
|
bool take_command(render_call const &Call, std::string_view Name);
|
|
// the line to log, cut to max_log_line; nothing when the call logged enough
|
|
std::optional<std::string> take_log(std::string_view Text);
|
|
|
|
private:
|
|
std::string m_name;
|
|
int m_log_lines{0};
|
|
bool m_commands_dropped{false}; // logged once
|
|
bool m_logs_dropped{false}; // logged once
|
|
};
|
|
// seconds of a steady clock, the same for every screen, for animations that don't depend on how often the screen is
|
|
// refreshed
|
|
double screen_time();
|
|
bool valid_image_side(int Side);
|
|
|
|
} // namespace script_screens
|