1
0
mirror of https://github.com/OpenRCT2/OpenRCT2 synced 2026-01-15 11:03:00 +01:00

Remove use of gCurrentLanguage

This commit is contained in:
Ted John
2018-04-26 23:48:25 +01:00
parent 7b28078da2
commit 35df72e61f
15 changed files with 69 additions and 53 deletions

View File

@@ -40,6 +40,7 @@
#include <openrct2/localisation/Date.h>
#include <openrct2/localisation/Language.h>
#include <openrct2/localisation/Localisation.h>
#include <openrct2/localisation/LocalisationService.h>
#include <openrct2/platform/platform.h>
#include <openrct2/sprites.h>
#include <openrct2/util/Util.h>
@@ -1150,7 +1151,7 @@ static void window_options_mousedown(rct_window *w, rct_widgetindex widgetIndex,
gDropdownItemsArgs[i - 1] = (uintptr_t)LanguagesDescriptors[i].native_name;
}
window_options_show_dropdown(w, widget, LANGUAGE_COUNT - 1);
dropdown_set_checked(gCurrentLanguage - 1, true);
dropdown_set_checked(LocalisationService_GetCurrentLanguage() - 1, true);
break;
case WIDX_DATE_FORMAT_DROPDOWN:
for (size_t i = 0; i < 4; i++) {
@@ -1389,8 +1390,8 @@ static void window_options_dropdown(rct_window *w, rct_widgetindex widgetIndex,
break;
case WIDX_LANGUAGE_DROPDOWN:
{
sint32 fallbackLanguage = gCurrentLanguage;
if (dropdownIndex != gCurrentLanguage - 1) {
auto fallbackLanguage = LocalisationService_GetCurrentLanguage();
if (dropdownIndex != LocalisationService_GetCurrentLanguage() - 1) {
if (!language_open(dropdownIndex + 1))
{
// Failed to open language file, try to recover by falling
@@ -1635,7 +1636,7 @@ static void window_options_invalidate(rct_window *w)
case WINDOW_OPTIONS_PAGE_CULTURE:
// Language
set_format_arg(0, char*, LanguagesDescriptors[gCurrentLanguage].native_name);
set_format_arg(0, char*, LanguagesDescriptors[LocalisationService_GetCurrentLanguage()].native_name);
// Currency: pounds, dollars, etc. (10 total)
window_options_culture_widgets[WIDX_CURRENCY].text = CurrencyDescriptors[gConfigGeneral.currency_format].stringId;

View File

@@ -124,7 +124,8 @@ namespace OpenRCT2
std::shared_ptr<IUiContext> uiContext)
: _env(env),
_audioContext(audioContext),
_uiContext(uiContext)
_uiContext(uiContext),
_localisationService(std::make_shared<LocalisationService>(env))
{
Instance = this;
}
@@ -395,14 +396,14 @@ namespace OpenRCT2
// TODO Ideally we want to delay this until we show the title so that we can
// still open the game window and draw a progress screen for the creation
// of the object cache.
_objectRepository->LoadOrConstruct();
_objectRepository->LoadOrConstruct(_localisationService->GetCurrentLanguage());
// TODO Like objects, this can take a while if there are a lot of track designs
// its also really something really we might want to do in the background
// as its not required until the player wants to place a new ride.
_trackDesignRepository->Scan();
_trackDesignRepository->Scan(_localisationService->GetCurrentLanguage());
_scenarioRepository->Scan();
_scenarioRepository->Scan(_localisationService->GetCurrentLanguage());
TitleSequenceManager::Scan();
if (!gOpenRCT2Headless)

View File

@@ -400,11 +400,7 @@ static exitcode_t HandleCommandScanObjects([[maybe_unused]] CommandLineArgEnumer
auto context = std::unique_ptr<OpenRCT2::IContext>(OpenRCT2::CreateContext());
auto env = context->GetPlatformEnvironment();
auto objectRepository = CreateObjectRepository(env);
// HACK: set gCurrentLanguage otherwise it be wrong for the index file
gCurrentLanguage = gConfigGeneral.language;
objectRepository->Construct();
objectRepository->Construct(gConfigGeneral.language);
return EXITCODE_OK;
}

View File

@@ -107,11 +107,11 @@ public:
* Queries and directories and loads the index header. If the index is up to date,
* the items are loaded from the index and returned, otherwise the index is rebuilt.
*/
std::vector<TItem> LoadOrBuild() const
std::vector<TItem> LoadOrBuild(sint32 language) const
{
std::vector<TItem> items;
auto scanResult = Scan();
auto readIndexResult = ReadIndexFile(scanResult.Stats);
auto readIndexResult = ReadIndexFile(language, scanResult.Stats);
if (std::get<0>(readIndexResult))
{
// Index was loaded
@@ -120,15 +120,15 @@ public:
else
{
// Index was not loaded
items = Build(scanResult);
items = Build(language, scanResult);
}
return items;
}
std::vector<TItem> Rebuild() const
std::vector<TItem> Rebuild(sint32 language) const
{
auto scanResult = Scan();
auto items = Build(scanResult);
auto items = Build(language, scanResult);
return items;
}
@@ -208,7 +208,7 @@ private:
}
}
std::vector<TItem> Build(const ScanResult &scanResult) const
std::vector<TItem> Build(sint32 language, const ScanResult &scanResult) const
{
std::vector<TItem> allItems;
Console::WriteLine("Building %s (%zu items)", _name.c_str(), scanResult.Files.size());
@@ -262,7 +262,7 @@ private:
allItems.insert(allItems.end(), itr.begin(), itr.end());
}
WriteIndexFile(scanResult.Stats, allItems);
WriteIndexFile(language, scanResult.Stats, allItems);
}
auto endTime = std::chrono::high_resolution_clock::now();
@@ -272,7 +272,7 @@ private:
return allItems;
}
std::tuple<bool, std::vector<TItem>> ReadIndexFile(const DirectoryStats &stats) const
std::tuple<bool, std::vector<TItem>> ReadIndexFile(sint32 language, const DirectoryStats &stats) const
{
bool loadedItems = false;
std::vector<TItem> items;
@@ -289,7 +289,7 @@ private:
header.MagicNumber == _magicNumber &&
header.VersionA == FILE_INDEX_VERSION &&
header.VersionB == _version &&
header.LanguageId == gCurrentLanguage &&
header.LanguageId == language &&
header.Stats.TotalFiles == stats.TotalFiles &&
header.Stats.TotalFileSize == stats.TotalFileSize &&
header.Stats.FileDateModifiedChecksum == stats.FileDateModifiedChecksum &&
@@ -318,7 +318,7 @@ private:
return std::make_tuple(loadedItems, items);
}
void WriteIndexFile(const DirectoryStats &stats, const std::vector<TItem> &items) const
void WriteIndexFile(sint32 language, const DirectoryStats &stats, const std::vector<TItem> &items) const
{
try
{
@@ -331,7 +331,7 @@ private:
header.MagicNumber = _magicNumber;
header.VersionA = FILE_INDEX_VERSION;
header.VersionB = _version;
header.LanguageId = gCurrentLanguage;
header.LanguageId = language;
header.Stats = stats;
header.NumItems = (uint32)items.size();
fs.WriteValue(header);

View File

@@ -148,7 +148,8 @@ static bool LoadCustomConfigFont(LocalisationService& localisationService)
void TryLoadFonts(LocalisationService& localisationService)
{
#ifndef NO_TTF
TTFontFamily const * fontFamily = LanguagesDescriptors[gCurrentLanguage].font_family;
auto currentLanguage = localisationService.GetCurrentLanguage();
TTFontFamily const * fontFamily = LanguagesDescriptors[currentLanguage].font_family;
if (fontFamily != FAMILY_OPENRCT2_SPRITE)
{

View File

@@ -86,8 +86,6 @@ struct language_descriptor {
extern const language_descriptor LanguagesDescriptors[LANGUAGE_COUNT];
extern sint32 gCurrentLanguage;
extern const utf8 BlackUpArrowString[];
extern const utf8 BlackDownArrowString[];
extern const utf8 BlackLeftArrowString[];

View File

@@ -40,6 +40,11 @@ LocalisationService::LocalisationService(const std::shared_ptr<IPlatformEnvironm
}
}
// Define implementation here to avoid including LanguagePack.h in header
LocalisationService::~LocalisationService()
{
}
const char * LocalisationService::GetString(rct_string_id id) const
{
const char * result = nullptr;
@@ -69,7 +74,7 @@ std::string LocalisationService::GetLanguagePath(uint32 languageId) const
{
auto locale = std::string(LanguagesDescriptors[languageId].locale);
auto languageDirectory = _env->GetDirectoryPath(DIRBASE::OPENRCT2, DIRID::LANGUAGE);
auto languagePath = Path::Combine(languageDirectory, locale + "txt");
auto languagePath = Path::Combine(languageDirectory, locale + ".txt");
return languagePath;
}
@@ -98,8 +103,10 @@ void LocalisationService::OpenLanguage(sint32 id, IObjectManager& objectManager)
// Objects and their localised strings need to be refreshed
objectManager.ResetObjects();
}
throw std::runtime_error("Unable to open language " + std::to_string(id));
else
{
throw std::runtime_error("Unable to open language " + std::to_string(id));
}
}
void LocalisationService::CloseLanguages()

View File

@@ -46,6 +46,7 @@ namespace OpenRCT2::Localisation
void UseTrueTypeFont(bool value) { _useTrueTypeFont = value; }
LocalisationService(const std::shared_ptr<IPlatformEnvironment>& env);
~LocalisationService();
const char * GetString(rct_string_id id) const;
std::tuple<rct_string_id, rct_string_id, rct_string_id> GetLocalisedScenarioStrings(const std::string& scenarioFilename) const;

View File

@@ -43,6 +43,7 @@
#include "../config/Config.h"
#include "../localisation/Localisation.h"
#include "../localisation/LocalisationService.h"
#include "../object/Object.h"
#include "ObjectList.h"
#include "../platform/platform.h"
@@ -225,17 +226,17 @@ public:
ClearItems();
}
void LoadOrConstruct() override
void LoadOrConstruct(sint32 language) override
{
ClearItems();
auto items = _fileIndex.LoadOrBuild();
auto items = _fileIndex.LoadOrBuild(language);
AddItems(items);
SortItems();
}
void Construct() override
void Construct(sint32 language) override
{
auto items = _fileIndex.Rebuild();
auto items = _fileIndex.Rebuild(language);
AddItems(items);
SortItems();
}
@@ -665,8 +666,9 @@ const rct_object_entry * object_list_find(rct_object_entry * entry)
void object_list_load()
{
auto context = GetContext();
IObjectRepository * objectRepository = context->GetObjectRepository();
objectRepository->LoadOrConstruct();
const auto localisationService = context->GetLocalisationService();
auto objectRepository = context->GetObjectRepository();
objectRepository->LoadOrConstruct(localisationService->GetCurrentLanguage());
IObjectManager * objectManager = context->GetObjectManager();
objectManager->UnloadAll();

View File

@@ -29,6 +29,11 @@ namespace OpenRCT2
interface IPlatformEnvironment;
}
namespace OpenRCT2::Localisation
{
class LocalisationService;
}
struct rct_drawpixelinfo;
struct ObjectRepositoryItem
@@ -59,8 +64,8 @@ interface IObjectRepository
{
virtual ~IObjectRepository() = default;
virtual void LoadOrConstruct() abstract;
virtual void Construct() abstract;
virtual void LoadOrConstruct(sint32 language) abstract;
virtual void Construct(sint32 language) abstract;
virtual size_t GetNumObjects() const abstract;
virtual const ObjectRepositoryItem * GetObjects() const abstract;
virtual const ObjectRepositoryItem * FindObject(const utf8 * name) const abstract;

View File

@@ -19,6 +19,7 @@
#include "../core/String.hpp"
#include "../localisation/Language.h"
#include "../localisation/LanguagePack.h"
#include "../localisation/LocalisationService.h"
#include "Object.h"
#include "StringTable.h"
@@ -111,7 +112,8 @@ void StringTable::SetString(uint8 id, uint8 language, const std::string &text)
void StringTable::Sort()
{
std::sort(_strings.begin(), _strings.end(), [](const StringTableEntry &a, const StringTableEntry &b) -> bool
auto targetLanguage = LocalisationService_GetCurrentLanguage();
std::sort(_strings.begin(), _strings.end(), [targetLanguage](const StringTableEntry &a, const StringTableEntry &b) -> bool
{
if (a.Id == b.Id)
{
@@ -120,11 +122,11 @@ void StringTable::Sort()
return String::Compare(a.Text, b.Text, true) < 0;
}
if (a.LanguageId == gCurrentLanguage)
if (a.LanguageId == targetLanguage)
{
return true;
}
if (b.LanguageId == gCurrentLanguage)
if (b.LanguageId == targetLanguage)
{
return false;
}

View File

@@ -26,6 +26,7 @@
#include "../core/FileStream.hpp"
#include "../core/Path.hpp"
#include "../core/String.hpp"
#include "../localisation/LocalisationService.h"
#include "../object/ObjectRepository.h"
#include "../object/RideObject.h"
#include "../PlatformEnvironment.h"
@@ -284,10 +285,10 @@ public:
return refs;
}
void Scan() override
void Scan(sint32 language) override
{
_items.clear();
auto trackDesigns = _fileIndex.LoadOrBuild();
auto trackDesigns = _fileIndex.LoadOrBuild(language);
for (const auto &td : trackDesigns)
{
_items.push_back(td);
@@ -404,7 +405,7 @@ ITrackDesignRepository * CreateTrackDesignRepository(std::shared_ptr<IPlatformEn
void track_repository_scan()
{
ITrackDesignRepository * repo = GetContext()->GetTrackDesignRepository();
repo->Scan();
repo->Scan(LocalisationService_GetCurrentLanguage());
}
bool track_repository_delete(const utf8 * path)

View File

@@ -46,7 +46,7 @@ interface ITrackDesignRepository
virtual std::vector<track_design_file_ref> GetItemsForRideGroup(uint8 rideType,
const RideGroup * rideGroup) const abstract;
virtual void Scan() abstract;
virtual void Scan(sint32 language) abstract;
virtual bool Delete(const std::string &path) abstract;
virtual std::string Rename(const std::string &path, const std::string &newName) abstract;
virtual std::string Install(const std::string &path) abstract;

View File

@@ -33,6 +33,7 @@
#include "../config/Config.h"
#include "../localisation/Localisation.h"
#include "../localisation/LocalisationService.h"
#include "../platform/platform.h"
#include "Scenario.h"
#include "../Game.h"
@@ -339,13 +340,13 @@ public:
ClearHighscores();
}
void Scan() override
void Scan(sint32 language) override
{
ImportMegaPark();
// Reload scenarios from index
_scenarios.clear();
auto scenarios = _fileIndex.LoadOrBuild();
auto scenarios = _fileIndex.LoadOrBuild(language);
for (auto scenario : scenarios)
{
AddScenario(scenario);
@@ -415,11 +416,11 @@ public:
return nullptr;
}
bool TryRecordHighscore(const utf8 * scenarioFileName, money32 companyValue, const utf8 * name) override
bool TryRecordHighscore(sint32 language, const utf8 * scenarioFileName, money32 companyValue, const utf8 * name) override
{
// Scan the scenarios so we have a fresh list to query. This is to prevent the issue of scenario completions
// not getting recorded, see #4951.
Scan();
Scan(language);
scenario_index_entry * scenario = GetByFilename(scenarioFileName);
if (scenario != nullptr)
@@ -743,7 +744,7 @@ IScenarioRepository * GetScenarioRepository()
void scenario_repository_scan()
{
IScenarioRepository * repo = GetScenarioRepository();
repo->Scan();
repo->Scan(LocalisationService_GetCurrentLanguage());
}
size_t scenario_repository_get_count()
@@ -761,6 +762,6 @@ const scenario_index_entry *scenario_repository_get_by_index(size_t index)
bool scenario_repository_try_record_highscore(const utf8 * scenarioFileName, money32 companyValue, const utf8 * name)
{
IScenarioRepository * repo = GetScenarioRepository();
return repo->TryRecordHighscore(scenarioFileName, companyValue, name);
return repo->TryRecordHighscore(LocalisationService_GetCurrentLanguage(), scenarioFileName, companyValue, name);
}

View File

@@ -64,7 +64,7 @@ interface IScenarioRepository
/**
* Scans the scenario directories and grabs the metadata for all the scenarios.
*/
virtual void Scan() abstract;
virtual void Scan(sint32 language) abstract;
virtual size_t GetCount() const abstract;
virtual const scenario_index_entry * GetByIndex(size_t index) const abstract;
@@ -75,7 +75,7 @@ interface IScenarioRepository
virtual const scenario_index_entry * GetByInternalName(const utf8 * name) const abstract;
virtual const scenario_index_entry * GetByPath(const utf8 * path) const abstract;
virtual bool TryRecordHighscore(const utf8 * scenarioFileName, money32 companyValue, const utf8 * name) abstract;
virtual bool TryRecordHighscore(sint32 language, const utf8 * scenarioFileName, money32 companyValue, const utf8 * name) abstract;
};
IScenarioRepository * CreateScenarioRepository(std::shared_ptr<OpenRCT2::IPlatformEnvironment> env);