From a25abe6d5d3cf809c152d0597529981c454e223e Mon Sep 17 00:00:00 2001 From: jerrrrycho Date: Thu, 26 Nov 2026 22:25:59 +0100 Subject: [PATCH] fix(editor): stray switch drives told once a frame, by the same switch lookup the deletion uses (#509) * fix(editor): tell the stray switch drives once a frame, by the test the deletion uses stray_drives() was built anew by each caller in a frame (the notice at the start, the red marks in the view, the list in the switch tool). It now keeps its list for the frame and builds it again after a step of the undo history. A drive whose switch shares its name with another switch was listed as having no switch once the later of the two was deleted: the name went with the deleted track, and only the deletion looked for the other track of the name. Both now use switch_drives::standing_switches, which falls back to the tracks of the scenery when the name finds none. The switches each drive template throws were read once per file until a restart. switch_drives::template_switches reads them again when the templates change (a rescan or a save in the editor), and a file that could not be read is tried again in a later frame. Refs #330 * test(editor): the switch lookup of the drives and the cache of the drive templates Refs #330 * test(editor): include for std::ranges::find Refs #330 --- CMakeLists.txt | 1 + application/editormode.h | 16 ++- application/editormodeswitchdrive.cpp | 78 +++++++---- editor/editorSwitchDrives.cpp | 68 +++++++++ editor/editorSwitchDrives.hpp | 76 ++++++++++ tests/unit/CMakeLists.txt | 2 + tests/unit/test_editor_switch_drives.cpp | 171 +++++++++++++++++++++++ 7 files changed, 382 insertions(+), 30 deletions(-) create mode 100644 editor/editorSwitchDrives.cpp create mode 100644 editor/editorSwitchDrives.hpp create mode 100644 tests/unit/test_editor_switch_drives.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 34be53811..6f5885352 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -192,6 +192,7 @@ set(SOURCES "editor/editorModelSets.cpp" "editor/editorTrackPresets.cpp" "editor/editorIncludeInfo.cpp" +"editor/editorSwitchDrives.cpp" "editor/editorTerrain.cpp" "editor/editorTrack.cpp" "editor/editorTrackTrace.cpp" diff --git a/application/editormode.h b/application/editormode.h index d25840e18..5542e265c 100644 --- a/application/editormode.h +++ b/application/editormode.h @@ -39,6 +39,7 @@ http://mozilla.org/MPL/2.0/. #include "editor/editorAlignmentStore.hpp" #include "editor/editorSwitchStore.hpp" #include "editor/editorSwitchCurve.hpp" +#include "editor/editorSwitchDrives.hpp" #include "editor/editorKilometrage.hpp" #include "editor/editorSpeed.hpp" #include "editor/editorRoad.hpp" @@ -2679,7 +2680,7 @@ class editor_mode : public application_mode, private editor_track::observer, pri // "a".."d" for one of a double switch, whose switches are named a..d. none for a template which isn't a drive. the ones not // described for the editor have no family (-1) std::vector const &drive_switches(std::string const &File, std::map, std::less<>> const &Variants); - std::map, std::less<>> m_drive_templates; // read once for each file + switch_drives::template_switches m_drive_templates; // read once for each file, again when the templates change // a drive set to a switch the scenery hasn't got: left by a switch deleted or renamed by hand or in an earlier editor, its events // throw nothing struct stray_drive @@ -2698,10 +2699,19 @@ class editor_mode : public application_mode, private editor_track::observer, pri std::size_t instances{0}; }; std::optional m_drives_named; + // the drives whose switch isn't in the scenery, told once a frame for the list in the switch tool and the marks in the view, and again + // after a step made, undone or redone in the frame + struct drives_stray + { + std::vector drives; + std::optional frame; // none: to be told again + std::size_t history{0}; // the revision of the undo history + }; + drives_stray m_drives_stray; // the include of a drive or a template changed: what's kept of the drives standing is read again void drives_changed(); - // the drives whose switch isn't in the scenery, checked against the tracks each time - std::vector stray_drives(bool Wait = false); + // the drives whose switch isn't in the scenery, checked against the tracks once a frame. the list holds until the next call + std::vector const &stray_drives(bool Wait = false); void render_stray_drives(); void draw_stray_drives(); scene::layer_handle output_layer(std::string const &Part) override; diff --git a/application/editormodeswitchdrive.cpp b/application/editormodeswitchdrive.cpp index 931350af1..e9e6a6313 100644 --- a/application/editormodeswitchdrive.cpp +++ b/application/editormodeswitchdrive.cpp @@ -13,6 +13,7 @@ http://mozilla.org/MPL/2.0/. #include "application/editorprojection.h" #include "editor/editorFormat.hpp" #include "editor/editorIncludeInfo.hpp" +#include "editor/editorSwitchDrives.hpp" #include "world/Segment.h" #include "world/Track.h" @@ -35,15 +36,14 @@ http://mozilla.org/MPL/2.0/. #include #include #include +#include namespace { std::string drive_file(std::string Text) { - std::replace(Text.begin(), Text.end(), '\\', '/'); - std::transform(Text.begin(), Text.end(), Text.begin(), [](unsigned char Character) { return static_cast(std::tolower(Character)); }); - return Text; + return switch_drives::game_name(std::move(Text)); } // the track the events of a drive find by the name: the game reads the names of the tracks lower case, the parameters of an include as @@ -54,6 +54,30 @@ TTrack *track_named(std::string const &Name) return track != nullptr ? track : simulation::Paths.find(drive_file(Name)); } +// whether the track a name finds is removed, none when it finds no track +std::optional named_removed(std::string const &Name) +{ + auto const *track{track_named(Name)}; + return track != nullptr ? std::optional{track->m_editorremoved} : std::nullopt; +} + +// the names of all the tracks of the scenery not removed +std::vector standing_names() +{ + std::vector names; + for (auto const *track : simulation::Paths.sequence()) + if (track != nullptr && false == track->m_editorremoved) + names.push_back(track->name()); + return names; +} + +// the switches standing, told by the names as the events of the drives find them in the game; with no track found by a name, a track of +// the name left by the deletion of the other of two is looked for in the whole scenery +switch_drives::standing_switches switches_standing() +{ + return switch_drives::standing_switches{named_removed, standing_names}; +} + // a template by the name of its file, with no folder: told to the user, who picks the drives by their names std::string file_name(std::string const &File) { @@ -307,10 +331,8 @@ std::vector const &editor_mode::drive_switches(std::string const &F static std::vector const single{std::string{}}; if (Variants.contains(File)) return single; - auto const [found, fresh]{m_drive_templates.try_emplace(File)}; - if (fresh) - editor_includes::switched_tracks(File, found->second); - return found->second; + // a template changed is read again with the next revision of the templates, one which couldn't be read in the next frame + return m_drive_templates.of(File, EditorIncludes.revision(), ImGui::GetFrameCount(), editor_includes::switched_tracks); } std::map, std::less<>> editor_mode::drive_variants() const @@ -330,7 +352,7 @@ void editor_mode::drives_changed() m_drives_named.reset(); } -std::vector editor_mode::stray_drives(bool const Wait) +std::vector const &editor_mode::stray_drives(bool const Wait) { scan_switch_drives(Wait); auto const instances{static_cast(scene::Layers.instance_count())}; @@ -348,31 +370,38 @@ std::vector editor_mode::stray_drives(bool const Wait) drive.switches.push_back(standing.values[0] + suffix); kept->drives.push_back(std::move(drive)); } + m_drives_stray.frame.reset(); } - // a switch is told by the name, as the events of the drive find it in the game; a drive of a double switch is stray with all of its - // switches gone, while one stands it still throws it - std::vector stray; + // the list in the switch tool and the marks in the view ask in each frame: told once, and again after a step in the frame + auto &stray{m_drives_stray}; + auto const frame{ImGui::GetFrameCount()}; + auto const history{history_revision()}; + if (stray.frame == frame && stray.history == history) + return stray.drives; + stray.frame = frame; + stray.history = history; + stray.drives.clear(); + // a switch is told by the name, as the events of the drive find it in the game, by the same test the deletion of a switch takes its + // drive away by; a drive of a double switch is stray with all of its switches gone, while one stands it still throws it + auto switches{switches_standing()}; for (auto const &drive : kept->drives) { auto const &included{scene::Layers.instance(drive.instance)}; // a gate, a derailer or a turntable of the same kind of template throws a track of another kind: only a track gone counts - if (included.removed || included.dead || std::any_of(drive.switches.begin(), drive.switches.end(), [](std::string const &Name) { - auto const *track{track_named(Name)}; - return track != nullptr && false == track->m_editorremoved; - })) + if (included.removed || included.dead || switches.any_stands(drive.switches)) continue; std::string missing; for (auto const &name : drive.switches) missing += (missing.empty() ? "" : ", ") + name; - stray.push_back(drive); - stray.back().name = std::move(missing); + stray.drives.push_back(drive); + stray.drives.back().name = std::move(missing); } - return stray; + return stray.drives; } void editor_mode::render_stray_drives() { - auto const stray{stray_drives()}; + auto const &stray{stray_drives()}; if (stray.empty()) return; ImGui::PushID("straydrives"); @@ -400,7 +429,7 @@ void editor_mode::render_stray_drives() // a red mark by each drive with no switch in sight, so it's seen in the view and over the photo, not only in the list void editor_mode::draw_stray_drives() { - auto const stray{stray_drives()}; + auto const &stray{stray_drives()}; if (stray.empty()) return; // the ones far off would only crowd the view of the whole line @@ -528,13 +557,8 @@ void editor_mode::drives_deleted(std::vector const &Switches) auto const named = [&](std::string const &Name) { return std::find(deleted.begin(), deleted.end(), drive_file(Name)) != deleted.end(); }; // an older scenery may have two tracks of a name, the name finding the later; the earlier one, left by the deletion of the other, // still has the drive and the ballast, so it's looked for too - auto const stands = [](std::string const &Name) { - if (auto const *track{track_named(Name)}; track != nullptr) - return false == track->m_editorremoved; - auto const name{drive_file(Name)}; - auto const &tracks{simulation::Paths.sequence()}; - return std::any_of(tracks.begin(), tracks.end(), [&](TTrack const *Track) { return Track != nullptr && false == Track->m_editorremoved && drive_file(Track->name()) == name; }); - }; + auto switches{switches_standing()}; + auto const stands = [&switches](std::string const &Name) { return switches.stands(Name); }; auto const take_away = [&](scene::instance_handle const Instance) { if (false == scene::Layers.removable(Instance)) return; diff --git a/editor/editorSwitchDrives.cpp b/editor/editorSwitchDrives.cpp new file mode 100644 index 000000000..8a6bbecfe --- /dev/null +++ b/editor/editorSwitchDrives.cpp @@ -0,0 +1,68 @@ +/* +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 "editor/editorSwitchDrives.hpp" + +#include +#include +#include + +namespace switch_drives +{ + +std::string game_name(std::string Name) +{ + std::ranges::replace(Name, '\\', '/'); + std::ranges::transform(Name, Name.begin(), [](unsigned char const Character) { return static_cast(std::tolower(Character)); }); + return Name; +} + +standing_switches::standing_switches(finder Find, lister Names) : m_find{std::move(Find)}, m_names{std::move(Names)} {} + +bool standing_switches::stands(std::string const &Name) +{ + if (auto const removed{m_find(Name)}; removed.has_value()) + return false == *removed; + if (false == m_standing.has_value()) + { + m_standing.emplace(); + for (auto const &name : m_names()) + m_standing->insert(game_name(name)); + } + return m_standing->contains(game_name(Name)); +} + +bool standing_switches::any_stands(std::vector const &Names) +{ + return std::ranges::any_of(Names, [this](std::string const &Name) { return stands(Name); }); +} + +std::vector const &template_switches::of(std::string const &File, int const Revision, int const Frame, reader const &Read) +{ + if (m_revision != Revision) + { + m_files.clear(); + m_revision = Revision; + } + auto const [found, fresh]{m_files.try_emplace(File)}; + auto &entry{found->second}; + // read once; one that couldn't be read again in each frame at most, as each include of it asks + if (false == fresh && (false == entry.failed.has_value() || *entry.failed == Frame)) + return entry.suffixes; + entry.failed.reset(); + if (false == Read(File, entry.suffixes)) + { + entry.suffixes.clear(); + entry.failed = Frame; + } + return entry.suffixes; +} + +} // namespace switch_drives diff --git a/editor/editorSwitchDrives.hpp b/editor/editorSwitchDrives.hpp new file mode 100644 index 000000000..d21df9bec --- /dev/null +++ b/editor/editorSwitchDrives.hpp @@ -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 +#include +#include +#include +#include +#include + +// what the switch tool of the editor tells of the drives of the switches, with no track and no window of its own +namespace switch_drives +{ + +// a name as the game finds a track by it: '/' for '\', lower case +std::string game_name(std::string Name); + +// whether the switches a drive throws stand in the scenery, told by their names as the events of the drive find them in the game +class standing_switches +{ + public: + // nullopt when the name finds no track, else whether the track it finds is removed + using finder = std::function(std::string const &Name)>; + // the names of all the tracks of the scenery not removed, as they're written + using lister = std::function()>; + + standing_switches(finder Find, lister Names); + + // the track the name finds stands while it isn't removed. an older scenery may have two tracks of a name, the name finding the later + // one; deleted, it takes the name away, and the earlier one left still stands, so with no track found by the name any track of this + // name not removed counts + bool stands(std::string const &Name); + // a drive of a double switch throws four of them: it still throws one while one of them stands + bool any_stands(std::vector const &Names); + + private: + finder m_find; + lister m_names; + // the game names of the tracks not removed, read once, at the first name finding none + std::optional> m_standing; +}; + +// the switches each template of a drive throws, read once from each file while the templates found stay the same: a template changed +// on the disk is found again by a rescan or by its save in the editor, either changing the revision of the templates. a file which +// can't be read is read again, in a later frame, so a template missing for a moment isn't taken for one that isn't a drive until the +// next rescan +class template_switches +{ + public: + // what follows the first parameter in the names of the switches File throws, as editor_includes::switched_tracks() tells it. + // returns: false if the file can't be read + using reader = std::function &Suffixes)>; + + // the switches File throws, none for a template which isn't a drive or can't be read. Revision: of the templates found, Frame: the + // frame asked in. the reference holds until a call with another revision + std::vector const &of(std::string const &File, int Revision, int Frame, reader const &Read); + + private: + struct entry + { + std::vector suffixes; + std::optional failed; // the frame the file couldn't be read in + }; + std::map> m_files; + std::optional m_revision; +}; + +} // namespace switch_drives diff --git a/tests/unit/CMakeLists.txt b/tests/unit/CMakeLists.txt index 180285912..ac5dabdfd 100644 --- a/tests/unit/CMakeLists.txt +++ b/tests/unit/CMakeLists.txt @@ -34,6 +34,8 @@ add_executable(${UNIT_TESTS_NAME} "${CMAKE_SOURCE_DIR}/editor/editorFouling.cpp" # and the http client the editor fetches its aids with "${CMAKE_SOURCE_DIR}/editor/editorHttp.cpp" + # and what the switch tool tells of the drives of the switches + "${CMAKE_SOURCE_DIR}/editor/editorSwitchDrives.cpp" # the camera, for the order of its rotations (test_camera.cpp) "${CMAKE_SOURCE_DIR}/vehicle/Camera.cpp" ${UNIT_TEST_CASES}) diff --git a/tests/unit/test_editor_switch_drives.cpp b/tests/unit/test_editor_switch_drives.cpp new file mode 100644 index 000000000..aeff9c7e2 --- /dev/null +++ b/tests/unit/test_editor_switch_drives.cpp @@ -0,0 +1,171 @@ +#include "stdafx.h" +#include "doctest.h" +#include "editor/editorSwitchDrives.hpp" + +#include +#include +#include +#include +#include + +namespace { + +// a track of the scenery: its name as written, and whether the editor removed it +struct track +{ + std::string name; + bool removed{false}; +}; + +// the scenery as the game finds its tracks: Paths finds a name as written or lower case, the later of two tracks of a name; a track +// deleted takes its name away (editor_track::retire()), Retired lists those names +struct scenery +{ + std::vector tracks; + std::vector retired; + int lookups{0}; // of the whole sequence + + std::optional find(std::string const &Name) const + { + auto const name{switch_drives::game_name(Name)}; + if (std::ranges::find(retired, name) != retired.end()) + return std::nullopt; + std::optional found; + for (auto const &entry : tracks) + if (switch_drives::game_name(entry.name) == name) + found = entry.removed; // the later one + return found; + } + + std::vector standing() + { + ++lookups; + std::vector names; + for (auto const &entry : tracks) + if (false == entry.removed) + names.push_back(entry.name); + return names; + } + + switch_drives::standing_switches lookup() + { + return switch_drives::standing_switches{[this](std::string const &Name) { return find(Name); }, [this] { return standing(); }}; + } + + // deletes the later track of the name, as the editor does + void erase_later(std::string const &Name) + { + auto const name{switch_drives::game_name(Name)}; + for (auto entry{tracks.rbegin()}; entry != tracks.rend(); ++entry) + if (switch_drives::game_name(entry->name) == name && false == entry->removed) + { + entry->removed = true; + retired.push_back(name); + return; + } + } +}; + +} // namespace + +TEST_CASE("switch drives: a name is found as the game finds it") +{ + CHECK(switch_drives::game_name("Zwr\\12A") == "zwr/12a"); + CHECK(switch_drives::game_name("zwr_1") == "zwr_1"); + + scenery line{{{"Zwr1"}, {"zwr2", true}}, {}}; + auto switches{line.lookup()}; + CHECK(switches.stands("Zwr1")); + CHECK(switches.stands("ZWR1")); + // one the name finds, removed, doesn't stand + CHECK_FALSE(switches.stands("zwr2")); + CHECK_FALSE(switches.stands("zwr3")); +} + +TEST_CASE("switch drives: two switches of one name, the later deleted, leave the drive throwing the earlier") +{ + // an older scenery has two tracks of the name; the drive set to it throws the one the game finds + scenery line{{{"zwr7"}, {"other"}, {"Zwr7"}}, {}}; + line.erase_later("zwr7"); + REQUIRE_FALSE(line.find("zwr7").has_value()); // the name went with the track deleted + { + auto switches{line.lookup()}; + // the earlier one still stands: the drive isn't stray, and a hint to delete it would delete a working drive + CHECK(switches.stands("zwr7")); + CHECK(switches.stands("ZWR7")); + CHECK(switches.any_stands({"zwr7"})); + CHECK_FALSE(switches.stands("zwr8")); + // the tracks of the scenery are read once, however many names find none + CHECK(line.lookups == 1); + } + // both deleted: the drive throws nothing now + line.erase_later("zwr7"); + auto switches{line.lookup()}; + CHECK_FALSE(switches.stands("zwr7")); + CHECK_FALSE(switches.any_stands({"zwr7"})); +} + +TEST_CASE("switch drives: the drive of a double switch stands while one of its four switches does") +{ + scenery line{{{"dkw1a"}, {"dkw1b"}, {"dkw1c"}, {"dkw1d"}}, {}}; + std::vector const names{"dkw1a", "dkw1b", "dkw1c", "dkw1d"}; + for (auto const *name : {"dkw1a", "dkw1b", "dkw1c"}) + { + line.erase_later(name); + CHECK(line.lookup().any_stands(names)); + } + line.erase_later("dkw1d"); + CHECK_FALSE(line.lookup().any_stands(names)); +} + +TEST_CASE("switch drives: the switches of a template are read once while the templates stay the same") +{ + switch_drives::template_switches templates; + std::map reads; + std::map> files{{"napedy/naped_l.inc", {""}}, {"napedy/dkw.inc", {"a", "b", "c", "d"}}, {"podsypka.inc", {}}}; + auto const read = [&reads, &files](std::string const &File, std::vector &Suffixes) { + ++reads[File]; + auto const found{files.find(File)}; + if (found == files.end()) + return false; + Suffixes = found->second; + return true; + }; + + SUBCASE("read once in a revision") + { + CHECK(templates.of("napedy/naped_l.inc", 1, 10, read) == std::vector{""}); + CHECK(templates.of("napedy/naped_l.inc", 1, 11, read) == std::vector{""}); + CHECK(templates.of("napedy/dkw.inc", 1, 12, read).size() == 4); + CHECK(templates.of("podsypka.inc", 1, 12, read).empty()); + CHECK(templates.of("podsypka.inc", 1, 13, read).empty()); + CHECK(reads["napedy/naped_l.inc"] == 1); + CHECK(reads["napedy/dkw.inc"] == 1); + CHECK(reads["podsypka.inc"] == 1); + } + + SUBCASE("a template changed is read again in the next revision") + { + CHECK(templates.of("napedy/naped_l.inc", 1, 10, read) == std::vector{""}); + // saved in the editor, or changed on the disk and rescanned: no drive any more + files["napedy/naped_l.inc"] = {}; + CHECK(templates.of("napedy/naped_l.inc", 1, 11, read) == std::vector{""}); + CHECK(templates.of("napedy/naped_l.inc", 2, 11, read).empty()); + CHECK(reads["napedy/naped_l.inc"] == 2); + } + + SUBCASE("a file that can't be read is tried again in a later frame") + { + CHECK(templates.of("napedy/nowy.inc", 1, 10, read).empty()); + // each include of it asks in the same frame: read once + CHECK(templates.of("napedy/nowy.inc", 1, 10, read).empty()); + CHECK(reads["napedy/nowy.inc"] == 1); + CHECK(templates.of("napedy/nowy.inc", 1, 11, read).empty()); + CHECK(reads["napedy/nowy.inc"] == 2); + // there now, with the same revision: no rescan needed + files["napedy/nowy.inc"] = {""}; + CHECK(templates.of("napedy/nowy.inc", 1, 12, read) == std::vector{""}); + CHECK(templates.of("napedy/nowy.inc", 1, 13, read) == std::vector{""}); + CHECK(reads["napedy/nowy.inc"] == 3); + } +}