Skip to content

Commit

Permalink
Take font prefix into account when changing game language (fixes #865)
Browse files Browse the repository at this point in the history
  • Loading branch information
Xottab-DUTY committed Feb 12, 2024
1 parent e438e06 commit e66e812
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 13 deletions.
5 changes: 3 additions & 2 deletions src/xrEngine/GameFont.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "xr_level_controller.h"
#include "xrCore/Text/StringConversion.hpp"
#include "Render.h"
#include "StringTable/StringTable.h"

extern ENGINE_API bool g_bRendering;
ENGINE_API Fvector2 g_current_font_scale = {1.0f, 1.0f};
Expand Down Expand Up @@ -50,11 +51,11 @@ void CGameFont::Initialize(pcstr cShader, pcstr cTextureName)
{
string_path cTexture;

pcstr _lang = pSettings->r_string("string_table", "font_prefix");
pcstr _lang = StringTable().GetCurrentFontPrefix().c_str();
bool is_di = strstr(cTextureName, "ui_font_hud_01") || strstr(cTextureName, "ui_font_hud_02") ||
strstr(cTextureName, "ui_font_console_02");
if (_lang && !is_di)
strconcat(sizeof(cTexture), cTexture, cTextureName, _lang);
strconcat(cTexture, cTextureName, _lang);
else
xr_strcpy(cTexture, sizeof(cTexture), cTextureName);

Expand Down
41 changes: 38 additions & 3 deletions src/xrEngine/StringTable/StringTable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,18 +128,48 @@ void CStringTable::FillLanguageToken()

void CStringTable::SetLanguage()
{
cpcstr defined_language = pSettings->r_string("string_table", "language");
cpcstr defined_prefix = pSettings->r_string("string_table", "font_prefix");

if (LanguageID != std::numeric_limits<u32>::max())
{
pData->m_sLanguage = languagesToken.at(LanguageID).name;

if (0 == xr_strcmp(pData->m_sLanguage, defined_language))
pData->m_fontPrefix = defined_prefix;
else
{
pData->m_fontPrefix = nullptr;

constexpr std::tuple<pcstr, pcstr> known_prefixes[] =
{
{ "fra", "_west" },
{ "ger", "_west" },
{ "ita", "_west" },
{ "spa", "_west" },
{ "pol", "_cent" },
{ "cze", "_cent" },
};
for (const auto [language, prefix] : known_prefixes)
{
if (0 == xr_strcmp(pData->m_sLanguage, language))
pData->m_fontPrefix = prefix;
}
}
}
else
{
pData->m_sLanguage = pSettings->r_string("string_table", "language");
auto it = std::find_if(languagesToken.begin(), languagesToken.end(), [](const xr_token& token) {
pData->m_sLanguage = defined_language;
pData->m_fontPrefix = defined_prefix;

const auto it = std::find_if(languagesToken.begin(), languagesToken.end(), [](const xr_token& token)
{
return token.name && token.name == pData->m_sLanguage;
});

R_ASSERT3(it != languagesToken.end(), "Check localization.ltx! Current language: ", pData->m_sLanguage.c_str());
if (it != languagesToken.end())
LanguageID = (*it).id;
LanguageID = it->id;
}
}

Expand All @@ -148,6 +178,11 @@ shared_str CStringTable::GetCurrentLanguage() const
return pData ? pData->m_sLanguage : nullptr;
}

shared_str CStringTable::GetCurrentFontPrefix() const
{
return pData ? pData->m_fontPrefix : nullptr;
}

xr_token* CStringTable::GetLanguagesToken() const { return languagesToken.data(); }

void CStringTable::Load(LPCSTR xml_file_full)
Expand Down
2 changes: 2 additions & 0 deletions src/xrEngine/StringTable/StringTable.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ using STRING_TABLE_MAP = xr_map<STRING_ID, STRING_VALUE>;

struct STRING_TABLE_DATA
{
shared_str m_fontPrefix;
shared_str m_sLanguage;
STRING_TABLE_MAP m_StringTable;
};
Expand All @@ -35,6 +36,7 @@ class ENGINE_API CStringTable final
static BOOL m_bWriteErrorsToLog;

shared_str GetCurrentLanguage() const;
shared_str GetCurrentFontPrefix() const;
xr_token* GetLanguagesToken() const;
static u32 LanguageID;

Expand Down
3 changes: 1 addition & 2 deletions src/xrGame/console_commands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -214,8 +214,7 @@ class CCC_GameLanguage : public CCC_Token
CCC_Token::Execute(args);
StringTable().ReloadLanguage();

if (g_pGamePersistent && g_pGamePersistent->IsMainMenuActive())
MainMenu()->OnUIReset();
Device.seqUIReset.Process();

if (!g_pGameLevel)
return;
Expand Down
10 changes: 6 additions & 4 deletions src/xrUICore/FontManager/FontManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

CFontManager::CFontManager()
{
Device.seqDeviceReset.Add(this, REG_PRIORITY_HIGH);

m_all_fonts.push_back(&pFontMedium); // used cpp
m_all_fonts.push_back(&pFontDI); // used cpp
m_all_fonts.push_back(&pFontArial14); // used xml
Expand Down Expand Up @@ -84,7 +82,6 @@ void CFontManager::InitializeFont(CGameFont*& F, LPCSTR section, u32 flags)

CFontManager::~CFontManager()
{
Device.seqDeviceReset.Remove(this);
FONTS_VEC_IT it = m_all_fonts.begin();
FONTS_VEC_IT it_e = m_all_fonts.end();
for (; it != it_e; ++it)
Expand All @@ -98,4 +95,9 @@ void CFontManager::Render()
for (; it != it_e; ++it)
(**it)->OnRender();
}
void CFontManager::OnDeviceReset() { InitializeFonts(); }

void CFontManager::OnUIReset()
{
// XXX: memory leak, font aren't being deallocated
InitializeFonts();
}
4 changes: 2 additions & 2 deletions src/xrUICore/FontManager/FontManager.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once

struct XRUICORE_API CFontManager : public pureDeviceReset
struct XRUICORE_API CFontManager : public CUIResetNotifier
{
CFontManager();
~CFontManager();
Expand Down Expand Up @@ -28,5 +28,5 @@ struct XRUICORE_API CFontManager : public pureDeviceReset
void InitializeFont(CGameFont*& F, LPCSTR section, u32 flags = 0);
LPCSTR GetFontTexName(LPCSTR section);

virtual void OnDeviceReset();
void OnUIReset() override;
};

0 comments on commit e66e812

Please sign in to comment.