mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-12-02 17:46:48 +01:00
fix(editor): a save writing some tracks of a line keeps the records of the others, a plan edit refused in part changes nothing
- record_written() erased every record of the line and added only the pieces written in this save, while the scenery writes only the tracks changed since. After a gizmo move and Ctrl+Z on one track of a line, the next save kept a record of that track alone: on reopening the line owned one track, and the next Plan edit left the others as duplicates. The records of the tracks the line still has and this save didn't write now stay. - model_run() kept each command before trying the next: with the radius of one vertex carried out and the cant of the next refused, the first stayed changed under "Refused, nothing changed". Several commands are now all tried on a copy first. - The fingerprint of the scenario index made a string of every file name before its .scn filter, which throws on Windows outside of the code page, on a detached thread (the class of GE#560). The extension is looked at first, as in the index itself; scenery_text::name_of() and has_extension() are shared by both. - A Del deferred by the check of the scenarios reads Shift as it was at the key press, not at the click on "go on". - open_project() writes no project file for a map with nothing to migrate (#461): the session test expects that.
This commit is contained in:
@@ -367,6 +367,18 @@ void editor_mode::model_run(std::vector<layout::command> Commands, model_context
|
||||
{
|
||||
std::vector<TTrack *> created;
|
||||
auto &session{model().session};
|
||||
// more than one: all of them tried on a copy first, one after another, so one refused leaves none of them carried out. Run one by one,
|
||||
// the radius of the first vertex stayed changed when the cant of the second was refused, under "Refused, nothing changed"
|
||||
if (Commands.size() > 1)
|
||||
{
|
||||
layout::model copy{session.network()};
|
||||
for (auto const &command : Commands)
|
||||
if (auto const outcome{copy.execute(command)}; false == outcome.done)
|
||||
{
|
||||
model_told(outcome, true);
|
||||
return;
|
||||
}
|
||||
}
|
||||
while (false == Commands.empty())
|
||||
{
|
||||
auto const &command{Commands.front()};
|
||||
@@ -916,13 +928,18 @@ bool editor_mode::model_delete(TTrack *Track)
|
||||
}
|
||||
if (Track->name().empty() || Track->name() == "none")
|
||||
return false;
|
||||
model_check_users({Track->name()}, [this, Track]() {
|
||||
// Shift as it was at the key press, not as it is at the click on "go on": Shift+Del of a double switch takes it all away
|
||||
auto const shift{Global.shiftState};
|
||||
model_check_users({Track->name()}, [this, Track, shift]() {
|
||||
if (Track->m_editorremoved)
|
||||
return;
|
||||
m_node = Track;
|
||||
ui()->set_node(m_node);
|
||||
m_model_delete_confirmed = true;
|
||||
auto const now{Global.shiftState};
|
||||
Global.shiftState = shift;
|
||||
delete_selected_track();
|
||||
Global.shiftState = now;
|
||||
m_model_delete_confirmed = false;
|
||||
});
|
||||
return true;
|
||||
|
||||
@@ -15,6 +15,9 @@ http://mozilla.org/MPL/2.0/.
|
||||
#include <algorithm>
|
||||
#include <fstream>
|
||||
#include <iterator>
|
||||
#include <set>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace project
|
||||
{
|
||||
@@ -172,7 +175,18 @@ void session::record_written(layout::line_id const Line, std::vector<written_pie
|
||||
entry.text = piece.text;
|
||||
written.pieces.push_back(std::move(entry));
|
||||
}
|
||||
// the scenery writes only the tracks changed since it was saved: the records of the other tracks the line still has stay as they
|
||||
// were, or after a track moved and moved back the line would keep a record of that one track alone, and own only it when opened
|
||||
std::set<std::string> current;
|
||||
for (auto const &piece : generate(Line).pieces)
|
||||
current.insert(piece.name);
|
||||
auto const written_now = [&Pieces](std::string const &Name) { return std::ranges::any_of(Pieces, [&Name](written_piece const &Piece) { return Piece.name == Name; }); };
|
||||
std::vector<generated_node> kept;
|
||||
for (auto const &node : m_project.generated)
|
||||
if (node.kind == "track" && node.line == Line && current.contains(node.name) && false == written_now(node.name))
|
||||
kept.push_back(node);
|
||||
record_generated(m_project, written, File);
|
||||
m_project.generated.insert(m_project.generated.end(), kept.begin(), kept.end());
|
||||
}
|
||||
|
||||
void session::mark_hand_changed(layout::line_id const Line)
|
||||
|
||||
@@ -31,26 +31,7 @@ namespace
|
||||
|
||||
using scenery_text::statement;
|
||||
|
||||
// the name of the file as the strings of the game hold it, none where the code page can't (Windows): generic_string() throws there,
|
||||
// and the game couldn't open such a file by its name either
|
||||
std::optional<std::string> name_of(std::filesystem::path const &Path)
|
||||
{
|
||||
try
|
||||
{
|
||||
return Path.filename().generic_string();
|
||||
}
|
||||
catch (std::system_error const &)
|
||||
{
|
||||
return std::nullopt;
|
||||
}
|
||||
}
|
||||
|
||||
// the file has the extension, in any case, without its name being made a string
|
||||
bool has_extension(std::filesystem::path const &Path, std::u8string_view const Extension)
|
||||
{
|
||||
auto const extension{Path.extension().u8string()};
|
||||
return extension.size() == Extension.size() && std::ranges::equal(extension, Extension, [](char8_t const A, char8_t const B) { return std::tolower(static_cast<unsigned char>(A)) == std::tolower(static_cast<unsigned char>(B)); });
|
||||
}
|
||||
|
||||
// the files the files include, by a quick look at their words, each file looked at once for all the scenarios: a scenario whose includes
|
||||
// don't reach the file of the map doesn't use it, and isn't read as a whole
|
||||
@@ -266,9 +247,9 @@ scenario_index index_scenarios(std::filesystem::path const &Folder, std::string
|
||||
std::error_code error;
|
||||
for (std::filesystem::directory_iterator file{Folder, std::filesystem::directory_options::skip_permission_denied, error}, end; file != end; file.increment(error))
|
||||
{
|
||||
if (false == has_extension(file->path(), u8".scn") || false == file->is_regular_file(error))
|
||||
if (false == scenery_text::has_extension(file->path(), u8".scn") || false == file->is_regular_file(error))
|
||||
continue;
|
||||
if (auto name{name_of(file->path())})
|
||||
if (auto name{scenery_text::name_of(file->path())})
|
||||
scenarios.push_back(std::move(*name));
|
||||
}
|
||||
std::ranges::sort(scenarios, [](std::string const &A, std::string const &B) { return scenery_text::lowered(A) < scenery_text::lowered(B); });
|
||||
|
||||
@@ -49,11 +49,14 @@ index_fingerprint fingerprint_of(std::filesystem::path const &Folder, std::strin
|
||||
std::error_code error;
|
||||
for (std::filesystem::directory_iterator file{Folder, std::filesystem::directory_options::skip_permission_denied, error}, end; file != end; file.increment(error))
|
||||
{
|
||||
auto const name{file->path().filename().generic_string()};
|
||||
if (false == file->is_regular_file(error) || false == scenery_text::lowered(name).ends_with(".scn"))
|
||||
// the extension looked at before the name is made a string, which throws on Windows outside of the code page
|
||||
if (false == scenery_text::has_extension(file->path(), u8".scn") || false == file->is_regular_file(error))
|
||||
continue;
|
||||
auto name{scenery_text::name_of(file->path())};
|
||||
if (false == name.has_value())
|
||||
continue;
|
||||
index_fingerprint::file entry;
|
||||
entry.name = name;
|
||||
entry.name = std::move(*name);
|
||||
entry.size = file->file_size(error);
|
||||
entry.time = std::chrono::duration_cast<std::chrono::seconds>(file->last_write_time(error).time_since_epoch()).count();
|
||||
result.files.push_back(std::move(entry));
|
||||
|
||||
@@ -13,10 +13,12 @@ http://mozilla.org/MPL/2.0/.
|
||||
|
||||
#include <algorithm>
|
||||
#include <array>
|
||||
#include <cctype>
|
||||
#include <cstdlib>
|
||||
#include <fstream>
|
||||
#include <iterator>
|
||||
#include <set>
|
||||
#include <system_error>
|
||||
#include <utility>
|
||||
|
||||
namespace scenery_text
|
||||
@@ -436,6 +438,24 @@ std::optional<std::string> node_text(std::string_view const Text, std::string_vi
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
std::optional<std::string> name_of(std::filesystem::path const &Path)
|
||||
{
|
||||
try
|
||||
{
|
||||
return Path.filename().generic_string();
|
||||
}
|
||||
catch (std::system_error const &)
|
||||
{
|
||||
return std::nullopt;
|
||||
}
|
||||
}
|
||||
|
||||
bool has_extension(std::filesystem::path const &Path, std::u8string_view const Extension)
|
||||
{
|
||||
auto const extension{Path.extension().u8string()};
|
||||
return extension.size() == Extension.size() && std::ranges::equal(extension, Extension, [](char8_t const A, char8_t const B) { return std::tolower(static_cast<unsigned char>(A)) == std::tolower(static_cast<unsigned char>(B)); });
|
||||
}
|
||||
|
||||
std::optional<std::string> file_text(std::filesystem::path const &Path)
|
||||
{
|
||||
std::ifstream file{Path, std::ios::binary};
|
||||
|
||||
@@ -101,6 +101,12 @@ struct scenery
|
||||
// the scenery from the file Root, relative to the folder, with all its includes
|
||||
scenery read(std::filesystem::path const &Folder, std::string const &Root, read_options const &Options = {});
|
||||
|
||||
// the name of the file as the strings of the game hold it, none where the code page can't (Windows): generic_string() throws there,
|
||||
// and the game couldn't open such a file by its name either
|
||||
std::optional<std::string> name_of(std::filesystem::path const &Path);
|
||||
// the file has the extension, in any case, without its name being made a string
|
||||
bool has_extension(std::filesystem::path const &Path, std::u8string_view const Extension);
|
||||
|
||||
// the text of the file, also where it's named in another case, as the game finds it (FileCased); none when it can't be read
|
||||
std::optional<std::string> file_text(std::filesystem::path const &Path);
|
||||
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
#include <chrono>
|
||||
#include <fstream>
|
||||
#include <iterator>
|
||||
#include <set>
|
||||
#include <string>
|
||||
#include <thread>
|
||||
|
||||
@@ -63,8 +64,9 @@ TEST_CASE("project session: the map opened gets its project, a command tried cha
|
||||
project::session session;
|
||||
auto const opened{session.open(folder.root(), "mapa.scn")};
|
||||
REQUIRE(opened.done);
|
||||
CHECK(opened.migrated);
|
||||
CHECK(std::filesystem::exists(folder.root() / "mapa.eu07proj"));
|
||||
// nothing to migrate: no file yet, it's written as the scenery is saved
|
||||
CHECK_FALSE(opened.migrated);
|
||||
CHECK_FALSE(std::filesystem::exists(folder.root() / "mapa.eu07proj"));
|
||||
CHECK(session.file() == folder.root() / "mapa.eu07proj");
|
||||
CHECK(session.network().lines().empty());
|
||||
|
||||
@@ -164,6 +166,34 @@ TEST_CASE("project session: each command a step the editor takes back and brings
|
||||
CHECK(session.undoable() == 2);
|
||||
}
|
||||
|
||||
TEST_CASE("project session: a save writing one track of a line keeps the records of its other tracks") {
|
||||
test::scratch_folder folder{"project_session_partial"};
|
||||
folder.write("mapa.scn", "// a new scenery\n");
|
||||
project::session session;
|
||||
REQUIRE(session.open(folder.root(), "mapa.scn").done);
|
||||
REQUIRE(session.execute(three_curves()).outcome.done);
|
||||
write_tracks(session, folder, "mapa.scn", {line_id{1}});
|
||||
auto const of_line = [&session]() {
|
||||
std::set<std::string> names;
|
||||
for (auto const &node : session.project().generated)
|
||||
if (node.line == line_id{1})
|
||||
names.insert(node.name);
|
||||
return names;
|
||||
};
|
||||
auto const all{of_line()};
|
||||
REQUIRE(all.size() > 1);
|
||||
// a track moved and moved back (Ctrl+Z): the scenery writes that one alone
|
||||
auto const piece{session.generate(line_id{1}).pieces.front()};
|
||||
session.record_written(line_id{1}, {{piece.name, piece.text}}, "mapa.scn");
|
||||
CHECK(of_line() == all);
|
||||
std::string error;
|
||||
REQUIRE(session.save(error));
|
||||
auto const opened{session.open(folder.root(), "mapa.scn")};
|
||||
REQUIRE(opened.done);
|
||||
CHECK(opened.hand_changes.empty());
|
||||
CHECK(of_line() == all);
|
||||
}
|
||||
|
||||
TEST_CASE("project session: the tracks written recorded, one changed in the file marks its line, written over only when the author decides") {
|
||||
test::scratch_folder folder{"project_session_hand"};
|
||||
folder.write("mapa.scn", "// a new scenery\n");
|
||||
|
||||
Reference in New Issue
Block a user