mirror of https://github.com/OpenTTD/OpenTTD
Codechange: use std::filesystem::path for the language file's path
parent
97dd84d1e0
commit
13cdf5fffa
|
@ -34,6 +34,7 @@
|
||||||
#include "company_base.h"
|
#include "company_base.h"
|
||||||
#include "company_func.h"
|
#include "company_func.h"
|
||||||
#include "3rdparty/fmt/chrono.h"
|
#include "3rdparty/fmt/chrono.h"
|
||||||
|
#include "3rdparty/fmt/std.h"
|
||||||
|
|
||||||
#ifdef WITH_ALLEGRO
|
#ifdef WITH_ALLEGRO
|
||||||
# include <allegro.h>
|
# include <allegro.h>
|
||||||
|
@ -167,7 +168,7 @@ void CrashLog::LogConfiguration(std::back_insert_iterator<std::string> &output_i
|
||||||
BlitterFactory::GetCurrentBlitter() == nullptr ? "none" : BlitterFactory::GetCurrentBlitter()->GetName(),
|
BlitterFactory::GetCurrentBlitter() == nullptr ? "none" : BlitterFactory::GetCurrentBlitter()->GetName(),
|
||||||
BaseGraphics::GetUsedSet() == nullptr ? "none" : BaseGraphics::GetUsedSet()->name,
|
BaseGraphics::GetUsedSet() == nullptr ? "none" : BaseGraphics::GetUsedSet()->name,
|
||||||
BaseGraphics::GetUsedSet() == nullptr ? UINT32_MAX : BaseGraphics::GetUsedSet()->version,
|
BaseGraphics::GetUsedSet() == nullptr ? UINT32_MAX : BaseGraphics::GetUsedSet()->version,
|
||||||
_current_language == nullptr ? "none" : _current_language->file,
|
_current_language == nullptr ? "none" : _current_language->file.filename(),
|
||||||
MusicDriver::GetInstance() == nullptr ? "none" : MusicDriver::GetInstance()->GetName(),
|
MusicDriver::GetInstance() == nullptr ? "none" : MusicDriver::GetInstance()->GetName(),
|
||||||
BaseMusic::GetUsedSet() == nullptr ? "none" : BaseMusic::GetUsedSet()->name,
|
BaseMusic::GetUsedSet() == nullptr ? "none" : BaseMusic::GetUsedSet()->name,
|
||||||
BaseMusic::GetUsedSet() == nullptr ? UINT32_MAX : BaseMusic::GetUsedSet()->version,
|
BaseMusic::GetUsedSet() == nullptr ? UINT32_MAX : BaseMusic::GetUsedSet()->version,
|
||||||
|
|
|
@ -384,19 +384,7 @@ void ReconsiderGameScriptLanguage()
|
||||||
{
|
{
|
||||||
if (_current_data == nullptr) return;
|
if (_current_data == nullptr) return;
|
||||||
|
|
||||||
char temp[MAX_PATH];
|
std::string language = _current_language->file.stem().string();
|
||||||
strecpy(temp, _current_language->file, lastof(temp));
|
|
||||||
|
|
||||||
/* Remove the extension */
|
|
||||||
char *l = strrchr(temp, '.');
|
|
||||||
assert(l != nullptr);
|
|
||||||
*l = '\0';
|
|
||||||
|
|
||||||
/* Skip the path */
|
|
||||||
char *language = strrchr(temp, PATHSEPCHAR);
|
|
||||||
assert(language != nullptr);
|
|
||||||
language++;
|
|
||||||
|
|
||||||
for (auto &p : _current_data->compiled_strings) {
|
for (auto &p : _current_data->compiled_strings) {
|
||||||
if (p.language == language) {
|
if (p.language == language) {
|
||||||
_current_data->cur_language = &p;
|
_current_data->cur_language = &p;
|
||||||
|
|
|
@ -14,6 +14,7 @@
|
||||||
#include <unicode/coll.h>
|
#include <unicode/coll.h>
|
||||||
#endif /* WITH_ICU_I18N */
|
#endif /* WITH_ICU_I18N */
|
||||||
#include "strings_type.h"
|
#include "strings_type.h"
|
||||||
|
#include <filesystem>
|
||||||
|
|
||||||
static const uint8 CASE_GENDER_LEN = 16; ///< The (maximum) length of a case/gender string.
|
static const uint8 CASE_GENDER_LEN = 16; ///< The (maximum) length of a case/gender string.
|
||||||
static const uint8 MAX_NUM_GENDERS = 8; ///< Maximum number of supported genders.
|
static const uint8 MAX_NUM_GENDERS = 8; ///< Maximum number of supported genders.
|
||||||
|
@ -90,7 +91,7 @@ static_assert(sizeof(LanguagePackHeader) % 4 == 0);
|
||||||
|
|
||||||
/** Metadata about a single language. */
|
/** Metadata about a single language. */
|
||||||
struct LanguageMetadata : public LanguagePackHeader {
|
struct LanguageMetadata : public LanguagePackHeader {
|
||||||
char file[MAX_PATH]; ///< Name of the file we read this data from.
|
std::filesystem::path file; ///< Name of the file we read this data from.
|
||||||
};
|
};
|
||||||
|
|
||||||
/** Type for the list of language meta data. */
|
/** Type for the list of language meta data. */
|
||||||
|
|
|
@ -176,13 +176,7 @@ static void SurveyConfiguration(nlohmann::json &survey)
|
||||||
{
|
{
|
||||||
survey["network"] = _networking ? (_network_server ? "server" : "client") : "no";
|
survey["network"] = _networking ? (_network_server ? "server" : "client") : "no";
|
||||||
if (_current_language != nullptr) {
|
if (_current_language != nullptr) {
|
||||||
std::string_view language_basename(_current_language->file);
|
survey["language"]["filename"] = _current_language->file.filename().string();
|
||||||
auto e = language_basename.rfind(PATHSEPCHAR);
|
|
||||||
if (e != std::string::npos) {
|
|
||||||
language_basename = language_basename.substr(e + 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
survey["language"]["filename"] = language_basename;
|
|
||||||
survey["language"]["name"] = _current_language->name;
|
survey["language"]["name"] = _current_language->name;
|
||||||
survey["language"]["isocode"] = _current_language->isocode;
|
survey["language"]["isocode"] = _current_language->isocode;
|
||||||
}
|
}
|
||||||
|
|
|
@ -42,6 +42,7 @@
|
||||||
|
|
||||||
#include "table/strings.h"
|
#include "table/strings.h"
|
||||||
#include "table/control_codes.h"
|
#include "table/control_codes.h"
|
||||||
|
#include "3rdparty/fmt/std.h"
|
||||||
|
|
||||||
#include "strings_internal.h"
|
#include "strings_internal.h"
|
||||||
|
|
||||||
|
@ -1827,7 +1828,7 @@ bool ReadLanguagePack(const LanguageMetadata *lang)
|
||||||
{
|
{
|
||||||
/* Current language pack */
|
/* Current language pack */
|
||||||
size_t len = 0;
|
size_t len = 0;
|
||||||
std::unique_ptr<LanguagePack, LanguagePackDeleter> lang_pack(reinterpret_cast<LanguagePack *>(ReadFileToMem(lang->file, len, 1U << 20).release()));
|
std::unique_ptr<LanguagePack, LanguagePackDeleter> lang_pack(reinterpret_cast<LanguagePack *>(ReadFileToMem(lang->file.string(), len, 1U << 20).release()));
|
||||||
if (!lang_pack) return false;
|
if (!lang_pack) return false;
|
||||||
|
|
||||||
/* End of read data (+ terminating zero added in ReadFileToMem()) */
|
/* End of read data (+ terminating zero added in ReadFileToMem()) */
|
||||||
|
@ -1882,8 +1883,7 @@ bool ReadLanguagePack(const LanguageMetadata *lang)
|
||||||
|
|
||||||
_current_language = lang;
|
_current_language = lang;
|
||||||
_current_text_dir = (TextDirection)_current_language->text_dir;
|
_current_text_dir = (TextDirection)_current_language->text_dir;
|
||||||
const char *c_file = strrchr(_current_language->file, PATHSEPCHAR) + 1;
|
_config_language_file = _current_language->file.filename().string();
|
||||||
_config_language_file = c_file;
|
|
||||||
SetCurrentGrfLangID(_current_language->newgrflangid);
|
SetCurrentGrfLangID(_current_language->newgrflangid);
|
||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
|
@ -2007,9 +2007,9 @@ static bool GetLanguageFileHeader(const char *file, LanguagePackHeader *hdr)
|
||||||
* Gets a list of languages from the given directory.
|
* Gets a list of languages from the given directory.
|
||||||
* @param path the base directory to search in
|
* @param path the base directory to search in
|
||||||
*/
|
*/
|
||||||
static void GetLanguageList(const char *path)
|
static void GetLanguageList(const std::string &path)
|
||||||
{
|
{
|
||||||
DIR *dir = ttd_opendir(path);
|
DIR *dir = ttd_opendir(path.c_str());
|
||||||
if (dir != nullptr) {
|
if (dir != nullptr) {
|
||||||
struct dirent *dirent;
|
struct dirent *dirent;
|
||||||
while ((dirent = readdir(dir)) != nullptr) {
|
while ((dirent = readdir(dir)) != nullptr) {
|
||||||
|
@ -2020,10 +2020,10 @@ static void GetLanguageList(const char *path)
|
||||||
if (extension == nullptr || strcmp(extension, ".lng") != 0) continue;
|
if (extension == nullptr || strcmp(extension, ".lng") != 0) continue;
|
||||||
|
|
||||||
LanguageMetadata lmd;
|
LanguageMetadata lmd;
|
||||||
seprintf(lmd.file, lastof(lmd.file), "%s%s", path, d_name.c_str());
|
lmd.file = path + d_name;
|
||||||
|
|
||||||
/* Check whether the file is of the correct version */
|
/* Check whether the file is of the correct version */
|
||||||
if (!GetLanguageFileHeader(lmd.file, &lmd)) {
|
if (!GetLanguageFileHeader(lmd.file.string().c_str(), &lmd)) {
|
||||||
Debug(misc, 3, "{} is not a valid language file", lmd.file);
|
Debug(misc, 3, "{} is not a valid language file", lmd.file);
|
||||||
} else if (GetLanguage(lmd.newgrflangid) != nullptr) {
|
} 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", lmd.file);
|
||||||
|
@ -2042,8 +2042,7 @@ static void GetLanguageList(const char *path)
|
||||||
void InitializeLanguagePacks()
|
void InitializeLanguagePacks()
|
||||||
{
|
{
|
||||||
for (Searchpath sp : _valid_searchpaths) {
|
for (Searchpath sp : _valid_searchpaths) {
|
||||||
std::string path = FioGetDirectory(sp, LANG_DIR);
|
GetLanguageList(FioGetDirectory(sp, LANG_DIR));
|
||||||
GetLanguageList(path.c_str());
|
|
||||||
}
|
}
|
||||||
if (_languages.size() == 0) UserError("No available language packs (invalid versions?)");
|
if (_languages.size() == 0) UserError("No available language packs (invalid versions?)");
|
||||||
|
|
||||||
|
@ -2060,8 +2059,7 @@ void InitializeLanguagePacks()
|
||||||
/* We are trying to find a default language. The priority is by
|
/* We are trying to find a default language. The priority is by
|
||||||
* configuration file, local environment and last, if nothing found,
|
* configuration file, local environment and last, if nothing found,
|
||||||
* English. */
|
* English. */
|
||||||
const char *lang_file = strrchr(lng.file, PATHSEPCHAR) + 1;
|
if (_config_language_file == lng.file.filename()) {
|
||||||
if (_config_language_file == lang_file) {
|
|
||||||
chosen_language = &lng;
|
chosen_language = &lng;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue