mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-12-01 04:47:06 +01:00
fix(editor): file names outside the code page don't crash the project, includes are found in any case, a project file cut short isn't put in place
- The scenario index made a string of the name of every file in the folder, which throws on Windows for a name the ANSI code page can't hold (the class of GE#560). It looks at the extension first and leaves out a .scn the game couldn't open by its name either; the messages of the project file show paths as UTF-8. - The includes are read with the FileCased() fallback of the game (scenery_text::file_text), so on Linux an include written in another case no longer drops the scenario from the index, skips the migration of its file or marks its tracks as changed by hand. - save() closes the file before looking at it: a failed last flush no longer renames a truncated .new over the project. - "next node 0" is refused instead of wrapping to 0xffffffff. - open_project() writes no .eu07proj when there were no //$ lines to migrate, and asks exists() without throwing.
This commit is contained in:
@@ -15,6 +15,7 @@ http://mozilla.org/MPL/2.0/.
|
||||
#include <array>
|
||||
#include <charconv>
|
||||
#include <cstdio>
|
||||
#include <filesystem>
|
||||
#include <format>
|
||||
#include <fstream>
|
||||
#include <iterator>
|
||||
@@ -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<std::uint32_t>(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 Count>
|
||||
std::size_t choice(std::string_view const What, std::array<std::string_view, Count> 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<char>(file), std::istreambuf_iterator<char>()};
|
||||
@@ -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<std::streamsize>(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;
|
||||
}
|
||||
|
||||
@@ -491,9 +491,8 @@ std::vector<marked_file> 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<char>(input), std::istreambuf_iterator<char>()};
|
||||
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))
|
||||
{
|
||||
|
||||
@@ -61,7 +61,7 @@ std::vector<marked_file> 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<std::string> 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;
|
||||
|
||||
@@ -13,9 +13,13 @@ http://mozilla.org/MPL/2.0/.
|
||||
|
||||
#include <algorithm>
|
||||
#include <array>
|
||||
#include <cctype>
|
||||
#include <filesystem>
|
||||
#include <fstream>
|
||||
#include <iterator>
|
||||
#include <map>
|
||||
#include <optional>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <system_error>
|
||||
|
||||
@@ -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<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
|
||||
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<char>(file), std::istreambuf_iterator<char>()};
|
||||
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); });
|
||||
|
||||
|
||||
@@ -9,6 +9,7 @@ http://mozilla.org/MPL/2.0/.
|
||||
|
||||
#include "stdafx.h"
|
||||
#include "editor/project/sceneryText.hpp"
|
||||
#include "utilities/utilities.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <array>
|
||||
@@ -93,13 +94,6 @@ std::string normalized(std::string_view const Name)
|
||||
return result;
|
||||
}
|
||||
|
||||
std::optional<std::string> 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<char>(file), std::istreambuf_iterator<char>()};
|
||||
}
|
||||
|
||||
// (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<std::string> const &Parameters)
|
||||
@@ -442,6 +436,22 @@ std::optional<std::string> node_text(std::string_view const Text, std::string_vi
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
std::optional<std::string> 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<char>(file), std::istreambuf_iterator<char>()};
|
||||
}
|
||||
|
||||
bool same_file(std::string_view const A, std::string_view const B)
|
||||
{
|
||||
return normalized(A) == normalized(B);
|
||||
|
||||
@@ -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<std::string> 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);
|
||||
|
||||
|
||||
@@ -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") {
|
||||
|
||||
@@ -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"};
|
||||
|
||||
Reference in New Issue
Block a user