Skip to content

Commit

Permalink
Fix OpenTTD#12648: Ensure all uses of std::filesystem::path use nativ…
Browse files Browse the repository at this point in the history
…e encoding.

std::filesystem::path does not expect UTF-8 strings, so they must be converted to native format first (and back to utf-8 after.)
  • Loading branch information
PeterN committed May 9, 2024
1 parent ec61951 commit 613cc82
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/fileio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -489,7 +489,7 @@ bool TarScanner::AddFile(const std::string &filename, size_t, [[maybe_unused]] c

_tar_list[this->subdir][filename] = std::string{};

std::string filename_base = std::filesystem::path(filename).filename().string();
std::string filename_base = FS2OTTD(std::filesystem::path(OTTD2FS(filename)).filename());
SimplifyFileName(filename_base);

TarHeader th;
Expand Down
2 changes: 1 addition & 1 deletion src/game/game_text.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ void ReconsiderGameScriptLanguage()
{
if (_current_data == nullptr) return;

std::string language = _current_language->file.stem().string();
std::string language = FS2OTTD(_current_language->file.stem());
for (auto &p : _current_data->compiled_strings) {
if (p.language == language) {
_current_data->cur_language = &p;
Expand Down
4 changes: 2 additions & 2 deletions src/openttd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -578,7 +578,7 @@ int openttd_main(std::span<char * const> arguments)
if (mgo.opt != nullptr) {
_file_to_saveload.name = mgo.opt;

std::string extension = std::filesystem::path(_file_to_saveload.name).extension().string();
std::string extension = FS2OTTD(std::filesystem::path(OTTD2FS(_file_to_saveload.name)).extension());
auto [ft, _] = FiosGetSavegameListCallback(SLO_LOAD, _file_to_saveload.name, extension);
if (ft == FIOS_TYPE_INVALID) {
std::tie(ft, _) = FiosGetScenarioListCallback(SLO_LOAD, _file_to_saveload.name, extension);
Expand Down Expand Up @@ -612,7 +612,7 @@ int openttd_main(std::span<char * const> arguments)
return ret;
}

std::string extension = std::filesystem::path(_file_to_saveload.name).extension().string();
std::string extension = FS2OTTD(std::filesystem::path(OTTD2FS(_file_to_saveload.name)).extension());
auto [_, title] = FiosGetSavegameListCallback(SLO_LOAD, mgo.opt, extension);

_load_check_data.Clear();
Expand Down
2 changes: 1 addition & 1 deletion src/signature.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ static bool _ValidateSignatureFile(const std::string &filename)
return false;
}

std::string dirname = std::filesystem::path(filename).parent_path().string();
std::string dirname = FS2OTTD(std::filesystem::path(OTTD2FS(filename)).parent_path());

for (auto &signature : signatures["files"]) {
const std::string sig_filename = dirname + PATHSEPCHAR + signature["filename"].get<std::string>();
Expand Down
16 changes: 8 additions & 8 deletions src/strings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1904,7 +1904,7 @@ bool ReadLanguagePack(const LanguageMetadata *lang)
{
/* Current language pack */
size_t len = 0;
std::unique_ptr<LanguagePack, LanguagePackDeleter> lang_pack(reinterpret_cast<LanguagePack *>(ReadFileToMem(lang->file.string(), len, 1U << 20).release()));
std::unique_ptr<LanguagePack, LanguagePackDeleter> lang_pack(reinterpret_cast<LanguagePack *>(ReadFileToMem(FS2OTTD(lang->file), len, 1U << 20).release()));
if (!lang_pack) return false;

/* End of read data (+ terminating zero added in ReadFileToMem()) */
Expand Down Expand Up @@ -1953,7 +1953,7 @@ bool ReadLanguagePack(const LanguageMetadata *lang)

_current_language = lang;
_current_text_dir = (TextDirection)_current_language->text_dir;
_config_language_file = _current_language->file.filename().string();
_config_language_file = FS2OTTD(_current_language->file.filename());
SetCurrentGrfLangID(_current_language->newgrflangid);

#ifdef _WIN32
Expand Down Expand Up @@ -2079,13 +2079,13 @@ static void FillLanguageList(const std::string &path)
if (dir_entry.path().extension() != ".lng") continue;

LanguageMetadata lmd;
lmd.file = FS2OTTD(dir_entry.path());
lmd.file = dir_entry.path();

/* Check whether the file is of the correct version */
if (!GetLanguageFileHeader(lmd.file.string(), &lmd)) {
Debug(misc, 3, "{} is not a valid language file", lmd.file);
if (!GetLanguageFileHeader(FS2OTTD(lmd.file), &lmd)) {
Debug(misc, 3, "{} is not a valid language file", FS2OTTD(lmd.file));
} else if (GetLanguage(lmd.newgrflangid) != nullptr) {
Debug(misc, 3, "{}'s language ID is already known", lmd.file);
Debug(misc, 3, "{}'s language ID is already known", FS2OTTD(lmd.file));
} else {
_languages.push_back(lmd);
}
Expand Down Expand Up @@ -2119,7 +2119,7 @@ void InitializeLanguagePacks()
/* We are trying to find a default language. The priority is by
* configuration file, local environment and last, if nothing found,
* English. */
if (_config_language_file == lng.file.filename()) {
if (_config_language_file == FS2OTTD(lng.file.filename())) {
chosen_language = &lng;
break;
}
Expand All @@ -2139,7 +2139,7 @@ void InitializeLanguagePacks()
chosen_language = (language_fallback != nullptr) ? language_fallback : en_GB_fallback;
}

if (!ReadLanguagePack(chosen_language)) UserError("Can't read language pack '{}'", chosen_language->file);
if (!ReadLanguagePack(chosen_language)) UserError("Can't read language pack '{}'", FS2OTTD(chosen_language->file));
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/survey.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ void SurveyConfiguration(nlohmann::json &survey)
{
survey["network"] = _networking ? (_network_server ? "server" : "client") : "no";
if (_current_language != nullptr) {
survey["language"]["filename"] = _current_language->file.filename().string();
survey["language"]["filename"] = FS2OTTD(_current_language->file.filename());
survey["language"]["name"] = _current_language->name;
survey["language"]["isocode"] = _current_language->isocode;
}
Expand Down

0 comments on commit 613cc82

Please sign in to comment.