mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-12-02 16:36:53 +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:
@@ -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); });
|
||||
|
||||
|
||||
Reference in New Issue
Block a user