18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-11-19 06:26:01 +01:00

Merge branch 'master' into feat/renderers-cleanup

This commit is contained in:
2026-11-10 17:09:09 +01:00
140 changed files with 13065 additions and 622 deletions

View File

@@ -11,6 +11,7 @@ http://mozilla.org/MPL/2.0/.
#include "simulation/simulation.h"
#include "simulation/simulationtime.h"
#include "simulation/simulationenvironment.h"
#include "simulation/simulationtest.h"
#include "utilities/Globals.h"
#include "world/Event.h"
@@ -121,6 +122,8 @@ state_manager::init_scripting_interface() {
simulation::Memory.insert( memorycell );
simulation::Region->insert( memorycell );
}
// the cell a test scenery reports its result to
simulation::test().init_result_cell();
}
// legacy method, calculates changes in simulation state over specified time

View File

@@ -15,6 +15,7 @@ http://mozilla.org/MPL/2.0/.
#include "simulation/simulationtime.h"
#include "simulation/simulationsounds.h"
#include "simulation/simulationenvironment.h"
#include "simulation/simulationtest.h"
#include "scene/scenenodegroups.h"
#include "scene/scenelayers.h"
#include "scene/sceneterrain.h"
@@ -385,6 +386,10 @@ state_serializer::deserialize_config( cParser &Input, scene::scratch_data &Scrat
// config parameters (re)definition
Global.ConfigParse( Input );
if( Global.test.enabled ) {
// a scenery doesn't get to turn on what a test keeps off
simulation::apply_test_mode_settings();
}
}
void

View File

@@ -0,0 +1,270 @@
/*
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/.
*/
#include "stdafx.h"
#include "simulation/simulationtest.h"
#include "application/application.h"
#include "scene/scene.h"
#include "simulation/simulation.h"
#include "utilities/Globals.h"
#include "utilities/Logs.h"
#include "utilities/utilities.h"
#include "vehicle/DynObj.h"
#include "world/Event.h"
#include "world/MemCell.h"
#ifdef _WIN32
#include "Console.h"
#endif
#include <algorithm>
#include <chrono>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <format>
#include <memory>
namespace {
bool is_moving(TDynamicObject const *Vehicle)
{
return std::abs(Vehicle->GetVelocity()) > 0.01;
}
// [s] of real time since the start of the simulator
double elapsed_wall_time()
{
return std::chrono::duration<double>(std::chrono::steady_clock::now() - Global.startTimestamp).count();
}
} // namespace
namespace simulation {
test_manager &test()
{
static test_manager instance;
return instance;
}
void apply_test_mode_settings()
{
// nobody watches nor listens: no player vehicle, nothing drawn, no sound, no window in the way
Global.editor_session = false;
Global.local_start_vehicle = "ghostview";
Global.GfxRenderer = "null";
Global.NvRenderer = false;
Global.bSoundEnabled = false;
Global.python_enabled = false;
Global.bInactivePause = false;
Global.bFullScreen = false;
Global.fullscreen_windowed = false;
Global.VSync = false;
Global.iMultisampling = 0;
Global.gfx_usegles = false;
Global.minframetime = std::chrono::duration<float>(0.0f);
Global.extra_viewports.clear();
Global.headtrack_conf.joy.clear();
// a test runs on its own: no network, and no input from outside (eu07/maszyna#541)
Global.network_servers.clear();
Global.network_client.reset();
Global.InputGamepad = false;
Global.InputMouse = false;
Global.uart_conf.enable = false;
Global.motiontelemetry_conf.enable = false;
#ifdef WITH_ZMQ
Global.zmq_address.clear();
#endif
// nor anything to the outside: no keyboard leds, LPT or PoKeys desk (feedbackmode), no messages to a
// multiplayer server (multiplayer, which also holds some events back for it), no program run at the end
Global.iFeedbackMode = 0;
#ifdef _WIN32
Console::ModeSet(Global.iFeedbackMode, Global.iFeedbackPort);
#endif
Global.iMultiplayer = 0;
Global.exec_on_exit.clear();
// and the same way every time, unless given another seed (-seed, or randomseed in the settings)
if (!Global.random_seed)
Global.random_seed = seed_of("test");
// the real time limit, which the scenery may set too
test().watch_wall_clock(wall_clock_limit(Global.test.wall_timeout, Global.test.timeout));
}
void test_manager::init_result_cell() const
{
auto cell { std::make_unique<TMemCell>(scene::node_data{0, -1, std::string(result_cell), "memcell"}) };
// neither of the reported values, so that a cell nobody wrote to says nothing
cell->UpdateValues("", -1.0, 0.0, basic_event::flags::value1 | basic_event::flags::value2);
simulation::Region->insert(cell.get());
// the table owns its cells
simulation::Memory.insert(cell.release());
}
void test_manager::start()
{
if (m_started)
return;
m_started = true;
{
std::scoped_lock lock { m_lock };
m_progress = test_progress {};
}
if (simulation::Vehicles.sequence().empty())
{
// most likely the scenery wasn't found, or it isn't a test scenery
finish(test_result::error, "the scenery has no vehicles");
return;
}
for (auto const &name : Global.test.vehicles)
{
if (simulation::Vehicles.find(name) == nullptr)
{
finish(test_result::error, std::format("the scenery has no vehicle {} (test.vehicle)", name));
return;
}
}
// a pause set by the scenery or the settings would never be lifted, nobody is there to press the key
Global.iPause = 0;
auto tested { std::string("every vehicle") };
if (false == Global.test.vehicles.empty())
{
tested = "the consists of";
for (auto const &name : Global.test.vehicles)
tested += " " + name;
}
auto const walllimit { m_walllimit.load() };
WriteLog(std::format("test: started, testing {}, timeout {:.0f} s, real time limit {}, standstill {:.0f} s{}{}{}", tested, Global.test.timeout,
walllimit > 0.0 ? std::format("{:.0f} s", walllimit) : std::string("none"), Global.test.standstill,
Global.test.pass_on_standstill ? ", standstill passes" : "",
Global.test.fail_if_stop_overran ? "" : ", signals passed at danger allowed",
Global.test.no_movement_before_pass ? "" : ", pass without waiting for a stop"));
}
void test_manager::update(double const Deltatime)
{
if (false == m_started || m_finished)
return;
test_frame frame { Deltatime, std::nullopt, {}, tested_trains_at_rest() };
if (auto const *cell = simulation::Memory.find(std::string(result_cell)))
{
frame.reported = cell->Value1();
frame.reason = cell->Text();
}
auto const limits { test_limits { Global.test.timeout, Global.test.standstill, Global.test.pass_on_standstill, Global.test.no_movement_before_pass } };
std::optional<test_verdict> verdict;
auto passrequested { false };
{
std::scoped_lock lock { m_lock };
auto const requestedbefore { m_progress.passrequested };
verdict = evaluate_frame(m_progress, frame, limits);
passrequested = m_progress.passrequested && false == requestedbefore;
}
if (verdict.has_value())
{
finish(verdict->result, verdict->reason);
return;
}
if (passrequested)
{
WriteLog(std::format("test: pass requested at {:.1f} s{}, waiting for the tested trains to stop", m_progress.time,
frame.reason.empty() ? std::string() : std::format(" ({})", frame.reason)));
}
}
bool test_manager::tested_trains_at_rest() const
{
if (Global.test.vehicles.empty())
return std::ranges::none_of(simulation::Vehicles.sequence(), is_moving);
// the consists as they are now: a train the tested one couples to joins it, one it leaves behind doesn't
return std::ranges::none_of(Global.test.vehicles, [](std::string const &Name) {
auto *vehicle { simulation::Vehicles.find(Name) };
return vehicle != nullptr && vehicle->find_vehicle(coupling::coupler, is_moving) != nullptr;
});
}
void test_manager::signal_passed_at_danger(std::string_view const Vehicle, std::string_view const Signal)
{
auto const reason { std::format("{} passed signal {} at danger", Vehicle, Signal) };
if (false == Global.test.enabled)
{
WriteLog(reason, logtype::generic);
return;
}
if (false == Global.test.fail_if_stop_overran)
{
WriteLog(std::format("test: {} at {:.1f} s, allowed by test.fail_if_stop_overran", reason, m_progress.time));
return;
}
finish(test_result::signal_passed_at_danger, reason);
}
void test_manager::finish(test_result const Result, std::string_view const Reason)
{
if (conclude(Result, Reason))
Application.queue_quit(true);
}
bool test_manager::conclude(test_result const Result, std::string_view const Reason)
{
std::scoped_lock lock { m_lock };
if (m_finished || false == Global.test.enabled)
return false;
m_result = Result;
m_finished = true;
auto const line { std::format("test: {} (exit code {}) after {:.1f} s of simulation time: {}", to_string(Result), static_cast<int>(Result), m_progress.time, Reason) };
if (Result == test_result::pass)
WriteLog(line);
else
ErrorLog(line);
// the process has no console of its own; whoever started it with a redirected output gets the verdict there too
std::fprintf(stdout, "%s\n", line.c_str());
std::fflush(stdout);
return true;
}
int test_manager::exit_code() const
{
std::scoped_lock lock { m_lock };
return static_cast<int>(m_finished ? m_result : test_result::error);
}
void test_manager::watch_wall_clock(double const Limit)
{
m_walllimit = Limit;
if (Limit <= 0.0 || m_watchdog.joinable())
return;
m_watchdog = std::jthread([this](std::stop_token const &Stop) {
while (false == Stop.stop_requested())
{
std::this_thread::sleep_for(std::chrono::milliseconds(100));
check_wall_clock(elapsed_wall_time());
}
});
}
void test_manager::check_wall_clock(double const Elapsed)
{
auto const limit { m_walllimit.load() };
if (limit <= 0.0 || Elapsed < limit)
return;
// the run hangs, or is too slow: it ends here, whatever the main thread is doing. a run whose test has ended
// but which doesn't close ends with its result
if (false == conclude(test_result::timeout, std::format("still running after {:.0f} s of real time (test.wall_timeout)", limit)))
ErrorLog(std::format("test: the simulator was still running {:.0f} s of real time after the start, ended", limit));
// what was logged last gets written, unless the log is held up by a hung thread
StopLogService();
std::this_thread::sleep_for(std::chrono::milliseconds(250));
std::_Exit(exit_code());
}
} // namespace simulation

View File

@@ -0,0 +1,76 @@
/*
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 "simulation/simulationtestrules.h"
#include <atomic>
#include <mutex>
#include <string_view>
#include <thread>
// integration test mode (eu07 --test -s <scenery>, eu07/maszyna#541): the scenery runs unattended in a tight
// fixed-step loop and the simulator quits with an exit code saying how the test went
namespace simulation {
class test_manager {
public:
// name of the memory cell a scenery reports its result to
static constexpr std::string_view result_cell { "__test.result" };
// fixed simulation time step of one frame in test mode [s]: 3 physics steps of 10 ms
static constexpr double frame_step { 0.03 };
// creates the result cell; called with the other scripting interface cells, before the scenery is read
void init_result_cell() const;
// the simulation is ready: start the clocks
void start();
// checks the end conditions after a simulation frame of Deltatime seconds
void update(double Deltatime);
// reported by a driver whose train went past a signal showing stop
void signal_passed_at_danger(std::string_view Vehicle, std::string_view Signal);
// ends the test with given result and closes the application; the first call wins
void finish(test_result Result, std::string_view Reason);
// ends the test with given result, without closing the application: for the ways out that close it anyway
// (a bad command line, a failed start, an exception). returns false if the test had already ended
bool conclude(test_result Result, std::string_view Reason);
// (re)sets the real time limit of the run [s] since the start of the simulator, 0 = none. the first limit
// starts the watchdog, which ends a run still going at the limit with the timeout code, whatever it's doing
void watch_wall_clock(double Limit);
bool finished() const { return m_finished; }
// exit code of the process: the result, or error while the test hasn't finished
int exit_code() const;
private:
// the watchdog's check, at Elapsed seconds of real time since the start of the simulator
void check_wall_clock(double Elapsed);
// every vehicle of the tested trains stands: the consists of test.vehicle, or every vehicle without it
bool tested_trains_at_rest() const;
// the watchdog may end the test from its own thread
mutable std::mutex m_lock;
bool m_started { false };
std::atomic<bool> m_finished { false };
test_result m_result { test_result::error };
test_progress m_progress;
std::atomic<double> m_walllimit { 0.0 }; // [s] since the start of the simulator, 0 = none
std::jthread m_watchdog;
};
// the test of the run
test_manager &test();
// the settings of the test mode, over whatever the settings files and the scenery say: applied once the command
// line is read, and again after each config block of the scenery, which could turn the sound, the network or the
// program run at the end back on
void apply_test_mode_settings();
} // namespace simulation

View File

@@ -0,0 +1,162 @@
/*
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/.
*/
#include "stdafx.h"
#include "simulation/simulationtestrules.h"
#include <charconv>
#include <cmath>
#include <format>
namespace {
// the cell holds what the scenery wrote, so the values are whole numbers; the margin only keeps a stray fraction out
bool holds(double const Value, double const Expected)
{
return std::abs(Value - Expected) < 1e-6;
}
// the text of the result cell, as said after the result
std::string reason_suffix(std::string_view const Reason)
{
return Reason.empty() ? std::string() : std::format(" ({})", Reason);
}
// options of the simulator which take a value, see eu07_application::init_settings()
bool takes_value(std::string_view const Option)
{
return Option == "-s" || Option == "-v" || Option == "-seed" || Option == "-ini";
}
// a number of seconds given to Option, at Index of Arguments; the index is moved past the value
std::optional<double> option_seconds(std::span<char const *const> const Arguments, std::size_t &Index)
{
if (Index + 1 >= Arguments.size())
return std::nullopt;
++Index;
return simulation::parse_seconds(Arguments[Index]);
}
} // namespace
namespace simulation {
std::string_view to_string(test_result const Result)
{
using enum test_result;
switch (Result)
{
case pass: return "pass";
case fail: return "fail";
case signal_passed_at_danger: return "signal passed at danger";
case timeout: return "timeout";
case standstill: return "standstill";
case error: return "error";
}
return "unknown";
}
std::optional<double> parse_seconds(std::string_view const Text)
{
auto const *const last{Text.data() + Text.size()};
double value{0.0};
if (auto const [parsed, status]{std::from_chars(Text.data(), last, value)}; status != std::errc{} || parsed != last || false == std::isfinite(value) || value < 0.0)
return std::nullopt;
return value;
}
test_command_line parse_test_command_line(std::span<char const *const> const Arguments)
{
test_command_line result;
for (std::size_t i = 1; i < Arguments.size(); ++i)
{
std::string_view const option{Arguments[i]};
if (option == "--test")
{
result.enabled = true;
}
else if (option == "--test-timeout" || option == "--test-wall-timeout")
{
auto const seconds{option_seconds(Arguments, i)};
if (false == seconds.has_value())
{
// a typo would otherwise turn the limit off
if (result.error.empty())
result.error = std::format("{} needs a number of seconds", option);
continue;
}
if (option == "--test-timeout")
result.timeout = seconds;
else
result.wall_timeout = seconds;
}
else if (takes_value(option))
{
++i;
}
}
return result;
}
std::string describe_exception(std::exception_ptr const &Exception)
{
if (nullptr == Exception)
return "the simulator was terminated";
try
{
std::rethrow_exception(Exception);
}
catch (std::exception const &Error)
{
return std::format("critical error: {}", Error.what());
}
catch (...)
{
return "critical error: an exception of unknown type";
}
}
double wall_clock_limit(std::optional<double> const Given, double const Simulationtimeout)
{
if (Given.has_value())
return *Given;
return Simulationtimeout > 0.0 ? Simulationtimeout + wall_timeout_loading_allowance : 0.0;
}
std::optional<test_verdict> evaluate_frame(test_progress &Progress, test_frame const &Frame, test_limits const &Limits)
{
using enum test_result;
Progress.time += Frame.deltatime;
if (Frame.reported.has_value())
{
auto const value{*Frame.reported};
if (holds(value, 0.0))
return test_verdict{fail, std::format("__test.result set to 0{}", reason_suffix(Frame.reason))};
if (holds(value, 1.0))
{
if (false == Limits.no_movement_before_pass)
return test_verdict{pass, std::format("__test.result set to 1{}", reason_suffix(Frame.reason))};
Progress.passrequested = true;
}
}
Progress.resttime = Frame.atrest ? Progress.resttime + Frame.deltatime : 0.0;
if (Progress.passrequested && Progress.resttime >= pass_settle_time)
return test_verdict{pass, "__test.result set to 1 and the tested trains stopped"};
if (Limits.standstill > 0.0 && Progress.resttime >= Limits.standstill)
return test_verdict{Limits.pass_on_standstill ? pass : standstill, std::format("the tested trains stood still for {:.0f} s", Limits.standstill)};
if (Limits.timeout > 0.0 && Progress.time >= Limits.timeout)
return test_verdict{timeout, std::format("no result after {:.0f} s", Limits.timeout)};
return std::nullopt;
}
} // namespace simulation

View File

@@ -0,0 +1,98 @@
/*
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 <exception>
#include <optional>
#include <span>
#include <string>
#include <string_view>
// the rules of the test mode (eu07 --test, eu07/maszyna#541) which need nothing of the running simulation: the exit
// codes, the command line, the end conditions and the real time limit. kept apart so the unit tests can check them
namespace simulation {
// exit codes of a test run; public, scripts and CI read them. none of them is a code a process commonly ends with
// when it goes wrong on its own: 1 (killed with TerminateProcess, or a plain failure of most programs), 3 (abort()
// on Windows), 126-127 (a shell that can't run the program) or 128 + a signal number (killed, on Linux)
enum class test_result : int {
pass = 0, // __test.result set to 1, then the tested trains came to rest (or test.pass_on_standstill)
fail = 10, // __test.result set to 0
signal_passed_at_danger = 11, // a train went past a stop signal (SetVelocity 0 0), unless test.fail_if_stop_overran no
timeout = 12, // test.timeout seconds of simulation time, or test.wall_timeout seconds of real time went by
standstill = 13, // the tested trains stood still for test.standstill seconds without a result
error = 14, // the run could not start (a bad command line, the scenery failed to load) or the simulator threw
};
std::string_view to_string(test_result Result);
// how a test ended, and why
struct test_verdict {
test_result result{test_result::error};
std::string reason;
};
// the test mode options of the command line
struct test_command_line {
bool enabled{false}; // --test
std::optional<double> timeout; // --test-timeout <seconds of simulation time>
std::optional<double> wall_timeout; // --test-wall-timeout <seconds of real time>
std::string error; // what's wrong with them; empty if nothing
};
// seconds given on the command line: a number, not negative. nothing for anything else
std::optional<double> parse_seconds(std::string_view Text);
// picks the test mode options out of a command line (the first argument is the program). the values of the other
// options are skipped, so a scenery named like an option isn't taken for one; options it doesn't know are left alone
test_command_line parse_test_command_line(std::span<char const *const> Arguments);
// what ended a test run that the simulator didn't end itself: the exception caught (or being handled when the
// program was terminated), or nothing
std::string describe_exception(std::exception_ptr const &Exception);
// time allowed for loading the scenery, on top of the simulation time, when the real time limit isn't given [s]
inline constexpr double wall_timeout_loading_allowance{600.0};
// the real time limit of a test run [s], 0 = none: the one given, else the simulation time limit plus time for the
// loading, so a simulation that runs at least as fast as real time is never cut short. no simulation time limit
// (test.timeout 0) means none in real time either, unless given
double wall_clock_limit(std::optional<double> Given, double Simulationtimeout);
// the limits and switches the end conditions go by
struct test_limits {
double timeout{3600.0}; // [s] of simulation time; 0 = never
double standstill{300.0}; // [s] of the tested trains standing without a result; 0 = never
bool pass_on_standstill{false};
bool no_movement_before_pass{true};
};
// what a frame of the simulation brought
struct test_frame {
double deltatime{0.0}; // [s] of simulation time
std::optional<double> reported; // value of the result cell, if there is one
std::string_view reason; // text of the result cell
bool atrest{false}; // every vehicle of the tested trains stands (test.vehicle; every vehicle without it)
};
// the state of a test between frames
struct test_progress {
double time{0.0}; // simulation time since the start [s]
double resttime{0.0}; // how long the tested trains have been standing [s]
bool passrequested{false}; // __test.result was set to 1, the pass waits for the tested trains to stop
};
// how long the tested trains have to stand before a requested pass is granted [s]
inline constexpr double pass_settle_time{1.0};
// checks the end conditions after a frame and moves the progress on. returns the verdict once the test is over
std::optional<test_verdict> evaluate_frame(test_progress &Progress, test_frame const &Frame, test_limits const &Limits);
} // namespace simulation