18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-11-27 21:45:16 +01:00
Files
maszyna/editor/editorSwitchDrives.cpp
jerrrrycho a25abe6d5d 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 <algorithm> for std::ranges::find

Refs #330
2026-11-26 22:25:59 +01:00

69 lines
1.9 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/.
*/
#include "stdafx.h"
#include "editor/editorSwitchDrives.hpp"
#include <algorithm>
#include <cctype>
#include <utility>
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<char>(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<std::string> const &Names)
{
return std::ranges::any_of(Names, [this](std::string const &Name) { return stands(Name); });
}
std::vector<std::string> 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