diff --git a/application/editormodemodel.cpp b/application/editormodemodel.cpp index e70c2644d..e09dab039 100644 --- a/application/editormodemodel.cpp +++ b/application/editormodemodel.cpp @@ -367,6 +367,18 @@ void editor_mode::model_run(std::vector Commands, model_context { std::vector 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; diff --git a/editor/project/projectSession.cpp b/editor/project/projectSession.cpp index 9973efe46..767da1738 100644 --- a/editor/project/projectSession.cpp +++ b/editor/project/projectSession.cpp @@ -15,6 +15,9 @@ http://mozilla.org/MPL/2.0/. #include #include #include +#include +#include +#include namespace project { @@ -172,7 +175,18 @@ void session::record_written(layout::line_id const Line, std::vector 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 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) diff --git a/editor/project/scenarioIndex.cpp b/editor/project/scenarioIndex.cpp index 9017c982a..dcaf58ab6 100644 --- a/editor/project/scenarioIndex.cpp +++ b/editor/project/scenarioIndex.cpp @@ -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 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(A)) == std::tolower(static_cast(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); }); diff --git a/editor/project/scenarioIndexCache.cpp b/editor/project/scenarioIndexCache.cpp index 9cc7df07d..b08bbbef0 100644 --- a/editor/project/scenarioIndexCache.cpp +++ b/editor/project/scenarioIndexCache.cpp @@ -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(file->last_write_time(error).time_since_epoch()).count(); result.files.push_back(std::move(entry)); diff --git a/editor/project/sceneryText.cpp b/editor/project/sceneryText.cpp index ae341799c..81da9d3aa 100644 --- a/editor/project/sceneryText.cpp +++ b/editor/project/sceneryText.cpp @@ -13,10 +13,12 @@ http://mozilla.org/MPL/2.0/. #include #include +#include #include #include #include #include +#include #include namespace scenery_text @@ -436,6 +438,24 @@ std::optional node_text(std::string_view const Text, std::string_vi return std::nullopt; } +std::optional 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(A)) == std::tolower(static_cast(B)); }); +} + std::optional file_text(std::filesystem::path const &Path) { std::ifstream file{Path, std::ios::binary}; diff --git a/editor/project/sceneryText.hpp b/editor/project/sceneryText.hpp index 80ba0c69a..c7be0a252 100644 --- a/editor/project/sceneryText.hpp +++ b/editor/project/sceneryText.hpp @@ -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 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 file_text(std::filesystem::path const &Path); diff --git a/tests/unit/test_project_session.cpp b/tests/unit/test_project_session.cpp index d3eec883d..8463e1c7c 100644 --- a/tests/unit/test_project_session.cpp +++ b/tests/unit/test_project_session.cpp @@ -16,6 +16,7 @@ #include #include #include +#include #include #include @@ -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 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");