diff --git a/editor/project/projectFile.cpp b/editor/project/projectFile.cpp index 3e5a4f68e..6f7e8c508 100644 --- a/editor/project/projectFile.cpp +++ b/editor/project/projectFile.cpp @@ -15,6 +15,7 @@ http://mozilla.org/MPL/2.0/. #include #include #include +#include #include #include #include @@ -30,6 +31,13 @@ namespace using layout::line_id; using layout::node_id; using layout::turnout_id; + +// the path for a message, as UTF-8: generic_string() throws on Windows for a name the code page can't hold +std::string shown(std::filesystem::path const &Path) +{ + auto const text{Path.generic_u8string()}; + return std::string(text.begin(), text.end()); +} using layout::vertex_id; // the shortest form which reads back to the same double @@ -170,6 +178,14 @@ class record fail(std::format("{} {} is out of range", What, value)); return static_cast(value); } + // the identifier handed out last, of the next one written: they start at 1, so 0 would wrap around + std::uint32_t next_id(std::string_view const What) + { + auto const value{id(What)}; + if (false == failed() && value == 0u) + fail(std::format("{} 0: the identifiers start at 1", What)); + return value > 0u ? value - 1u : 0u; + } // one of the names, the index of it template std::size_t choice(std::string_view const What, std::array const &Names) @@ -598,13 +614,13 @@ read_result read(std::string_view const Text) { layout::network::handed_out last; words.expect("node"); - last.node = words.id("the next node") - 1u; + last.node = words.next_id("the next node"); words.expect("line"); - last.line = words.id("the next line") - 1u; + last.line = words.next_id("the next line"); words.expect("vertex"); - last.vertex = words.id("the next vertex") - 1u; + last.vertex = words.next_id("the next vertex"); words.expect("turnout"); - last.turnout = words.id("the next turnout") - 1u; + last.turnout = words.next_id("the next turnout"); words.finish(); next = last; } @@ -881,7 +897,7 @@ read_result load(std::filesystem::path const &Path) if (false == file.is_open()) { read_result result; - result.error = "there's no project file " + Path.generic_string(); + result.error = "there's no project file " + shown(Path); return result; } std::string const text{std::istreambuf_iterator(file), std::istreambuf_iterator()}; @@ -897,13 +913,18 @@ bool save(document const &Project, std::filesystem::path const &Path, std::strin std::ofstream file{temporary, std::ios::binary | std::ios::trunc}; if (false == file.is_open()) { - Error = "can't write " + temporary.generic_string(); + Error = "can't write " + shown(temporary); return false; } file.write(text.data(), static_cast(text.size())); - if (false == file.good()) + // closed before it's looked at: the last of the text is written by the flush of the close, and a file cut short there must not + // take the place of the project + file.close(); + if (file.fail()) { - Error = "can't write " + temporary.generic_string(); + Error = "can't write " + shown(temporary); + std::error_code ignored; + std::filesystem::remove(temporary, ignored); return false; } } @@ -911,7 +932,7 @@ bool save(document const &Project, std::filesystem::path const &Path, std::strin std::filesystem::rename(temporary, Path, error); if (error) { - Error = "can't put " + temporary.generic_string() + " in the place of the project file: " + error.message(); + Error = "can't put " + shown(temporary) + " in the place of the project file: " + error.message(); std::filesystem::remove(temporary, error); return false; } diff --git a/editor/project/projectMigration.cpp b/editor/project/projectMigration.cpp index 6b7d81913..96e8da49d 100644 --- a/editor/project/projectMigration.cpp +++ b/editor/project/projectMigration.cpp @@ -491,9 +491,8 @@ std::vector marked_files(std::filesystem::path const &Folder, std:: // the templates (*.inc) are a part of the layer including them, their own lines are the descriptions (//$e) if (scenery_text::lowered(file.name).ends_with(".inc")) continue; - std::ifstream input{Folder / file.name, std::ios::binary}; - std::string const text{std::istreambuf_iterator(input), std::istreambuf_iterator()}; - auto lines{marked_lines(text)}; + auto const text{scenery_text::file_text(Folder / file.name)}; + auto lines{marked_lines(text.value_or(std::string{}))}; if (lines.empty()) continue; result.push_back({file.name, file.context, std::move(lines)}); @@ -505,13 +504,20 @@ opened open_project(std::filesystem::path const &Folder, std::string const &Map, { opened result; auto const path{path_of(Folder / Map)}; - if (std::filesystem::exists(path)) + if (std::error_code error; std::filesystem::exists(path, error)) { result.project = load(path); return result; } auto made{migrate(marked_files(Folder, Map), Areas)}; result.notes = std::move(made.notes); + // no //$ lines: no file is written next to the map, which would be one more stray file in a working copy of the data. The project, + // empty, is written as the editor saves the scenery once it has something + if (made.project.network.lines().empty() && made.project.network.nodes().empty()) + { + result.project.project = std::move(made.project); + return result; + } std::string error; if (false == save(made.project, path, error)) { diff --git a/editor/project/projectMigration.hpp b/editor/project/projectMigration.hpp index 660724423..1de3c08e7 100644 --- a/editor/project/projectMigration.hpp +++ b/editor/project/projectMigration.hpp @@ -61,7 +61,7 @@ std::vector marked_files(std::filesystem::path const &Folder, std:: struct opened { read_result project; - bool migrated{false}; // there was no project file: it was made of the //$ lines and written + bool migrated{false}; // there was no project file: it was made of the //$ lines and written; with none of them, nothing is written std::vector notes; }; // the project of the map: its file read, or made of the //$ lines of the files of the map where there's none yet and written next to it; diff --git a/editor/project/scenarioIndex.cpp b/editor/project/scenarioIndex.cpp index 364c8a8d0..9017c982a 100644 --- a/editor/project/scenarioIndex.cpp +++ b/editor/project/scenarioIndex.cpp @@ -13,9 +13,13 @@ http://mozilla.org/MPL/2.0/. #include #include +#include +#include #include #include #include +#include +#include #include #include @@ -27,6 +31,27 @@ 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 class include_graph @@ -62,9 +87,8 @@ class include_graph auto found{m_includes.find(Key)}; if (found != m_includes.end()) return found->second; - std::ifstream file{m_folder / Name, std::ios::binary}; - std::string const text{std::istreambuf_iterator(file), std::istreambuf_iterator()}; - return m_includes.emplace(Key, scenery_text::includes_in(text)).first->second; + auto const text{scenery_text::file_text(m_folder / Name)}; + return m_includes.emplace(Key, scenery_text::includes_in(text.value_or(std::string{}))).first->second; } std::filesystem::path m_folder; @@ -242,9 +266,10 @@ 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)) { - auto const name{file->path().filename().generic_string()}; - if (file->is_regular_file(error) && scenery_text::lowered(name).ends_with(".scn")) - scenarios.push_back(name); + if (false == has_extension(file->path(), u8".scn") || false == file->is_regular_file(error)) + continue; + if (auto name{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/sceneryText.cpp b/editor/project/sceneryText.cpp index d30cec374..ae341799c 100644 --- a/editor/project/sceneryText.cpp +++ b/editor/project/sceneryText.cpp @@ -9,6 +9,7 @@ http://mozilla.org/MPL/2.0/. #include "stdafx.h" #include "editor/project/sceneryText.hpp" +#include "utilities/utilities.h" #include #include @@ -93,13 +94,6 @@ std::string normalized(std::string_view const Name) return result; } -std::optional file_text(std::filesystem::path const &Path) -{ - std::ifstream file{Path, std::ios::binary}; - if (false == file.is_open()) - return std::nullopt; - return std::string{std::istreambuf_iterator(file), std::istreambuf_iterator()}; -} // (p1), (p2)... put in place by the parameters of the include, a missing one as none (cParser::substituteParameters()) void substitute(std::string &Text, std::vector const &Parameters) @@ -442,6 +436,22 @@ std::optional node_text(std::string_view const Text, std::string_vi return std::nullopt; } +std::optional file_text(std::filesystem::path const &Path) +{ + std::ifstream file{Path, std::ios::binary}; + if (false == file.is_open()) + { + // named in another case, as the game finds it on Linux + auto const cased{FileCased(Path)}; + if (cased.empty()) + return std::nullopt; + file.open(cased, std::ios::binary); + if (false == file.is_open()) + return std::nullopt; + } + return std::string{std::istreambuf_iterator(file), std::istreambuf_iterator()}; +} + bool same_file(std::string_view const A, std::string_view const B) { return normalized(A) == normalized(B); diff --git a/editor/project/sceneryText.hpp b/editor/project/sceneryText.hpp index 30d672a16..80ba0c69a 100644 --- a/editor/project/sceneryText.hpp +++ b/editor/project/sceneryText.hpp @@ -101,6 +101,9 @@ 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 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); + // the same names of files, the case and the slashes aside bool same_file(std::string_view const A, std::string_view const B); diff --git a/tests/unit/test_project_file.cpp b/tests/unit/test_project_file.cpp index d118a3214..7894b4f89 100644 --- a/tests/unit/test_project_file.cpp +++ b/tests/unit/test_project_file.cpp @@ -269,6 +269,10 @@ TEST_CASE("project file: a newer schema is refused cleanly, and a file which isn REQUIRE(lower.project.has_value()); CHECK(lower.project->network.handed() == layout::network::handed_out{5, 3, 2}); CHECK_FALSE(lower.notes.empty()); + // next 0: the identifiers start at 1, so it isn't taken as the last one handed out wrapped round to 0xffffffff + auto const zero{with("next node 6 line 4 vertex 3 turnout 1", "next node 0 line 4 vertex 3 turnout 1")}; + CHECK_FALSE(zero.project.has_value()); + CHECK(zero.error.find("the next node 0") != std::string::npos); } TEST_CASE("project file: the identifiers aren't handed out again after deletions and a save and a load") { diff --git a/tests/unit/test_project_migration.cpp b/tests/unit/test_project_migration.cpp index 24d7c564c..881b05dff 100644 --- a/tests/unit/test_project_migration.cpp +++ b/tests/unit/test_project_migration.cpp @@ -166,6 +166,16 @@ TEST_CASE("project migration: the golden //$ lines of data/records made into a p CHECK(station->name == editor_records::read_station(files[7].lines.at('s')[1])->name); } +TEST_CASE("project migration: a map with no //$ lines gets an empty project and no file next to it") { + test::scratch_folder folder{"project_migration_none"}; + std::ofstream{folder.root() / "pusta.scm", std::ios::binary} << "node -1 0 t1 track normal 100 1.435 0.2 25 20 0 flat vis 0 0 0 0 0 0 0 0 0 0 100 0 endtrack\n"; + auto const opened{project::open_project(folder.root(), "pusta.scm")}; + REQUIRE_MESSAGE(opened.project.project.has_value(), opened.project.error); + CHECK(opened.project.project->network.lines().empty()); + CHECK_FALSE(opened.migrated); + CHECK_FALSE(std::filesystem::exists(folder.root() / "pusta.eu07proj")); +} + TEST_CASE("project migration: a map opened for the first time gets its project of the //$ lines of its files; the files stay as they were") { test::scratch_folder folder{"project_migration"}; auto const source{std::filesystem::path{EU07_UNIT_TESTS_DATA} / "project" / "migration"};