iw4x-client/src/Components/Modules/Localization.cpp

386 lines
8.2 KiB
C++
Raw Normal View History

2022-02-27 07:53:44 -05:00
#include <STDInclude.hpp>
2017-01-19 16:23:59 -05:00
namespace Components
{
std::recursive_mutex Localization::LocalizeMutex;
Dvar::Var Localization::UseLocalization;
std::unordered_map<std::string, Game::LocalizeEntry*> Localization::LocalizeMap;
2022-11-16 12:25:21 -05:00
std::optional<std::string> Localization::PrefixOverride;
std::function<void(Game::LocalizeEntry*)> Localization::ParseCallback;
2022-11-16 12:25:21 -05:00
void Localization::Set(const std::string& psLocalReference, const std::string& psNewString)
2017-01-19 16:23:59 -05:00
{
2022-11-16 12:25:21 -05:00
std::lock_guard _(LocalizeMutex);
2017-06-02 09:36:20 -04:00
Utils::Memory::Allocator* allocator = Utils::Memory::GetAllocator();
2017-01-19 16:23:59 -05:00
2022-11-16 12:25:21 -05:00
auto key = psLocalReference;
if (PrefixOverride.has_value())
{
key.insert(0, PrefixOverride.value());
}
if (LocalizeMap.contains(key))
2017-01-19 16:23:59 -05:00
{
2022-11-16 12:25:21 -05:00
auto* entry = LocalizeMap[key];
2017-01-19 16:23:59 -05:00
2022-11-16 12:25:21 -05:00
const auto* newStaticValue = allocator->duplicateString(psNewString);
2017-01-19 16:23:59 -05:00
if (!newStaticValue) return;
2022-11-16 12:25:21 -05:00
if (entry->value) allocator->free(entry->value);
2017-01-19 16:23:59 -05:00
entry->value = newStaticValue;
2022-11-16 12:25:21 -05:00
SaveParseOutput(entry);
2017-01-19 16:23:59 -05:00
return;
}
2022-11-16 12:25:21 -05:00
auto* entry = allocator->allocate<Game::LocalizeEntry>();
2017-01-19 16:23:59 -05:00
if (!entry) return;
entry->name = allocator->duplicateString(key);
2017-01-19 16:23:59 -05:00
if (!entry->name)
{
allocator->free(entry);
2017-01-19 16:23:59 -05:00
return;
}
2022-11-16 12:25:21 -05:00
entry->value = allocator->duplicateString(psNewString);
2017-01-19 16:23:59 -05:00
if (!entry->value)
{
allocator->free(entry->name);
allocator->free(entry);
2017-01-19 16:23:59 -05:00
return;
}
2022-11-16 12:25:21 -05:00
SaveParseOutput(entry);
LocalizeMap[key] = entry;
2017-01-19 16:23:59 -05:00
}
const char* Localization::Get(const char* key)
{
2022-11-16 12:25:21 -05:00
if (!UseLocalization.get<bool>()) return key;
2017-01-19 16:23:59 -05:00
Game::LocalizeEntry* entry = nullptr;
2017-01-19 16:23:59 -05:00
{
2022-11-16 12:25:21 -05:00
std::lock_guard _(LocalizeMutex);
2022-11-16 12:25:21 -05:00
if (LocalizeMap.contains(key))
{
2022-11-16 12:25:21 -05:00
entry = LocalizeMap[key];
}
2017-01-19 16:23:59 -05:00
}
if (!entry || !entry->value)
{
entry = Game::DB_FindXAssetHeader(Game::XAssetType::ASSET_TYPE_LOCALIZE_ENTRY, key).localize;
}
if (entry && entry->value)
{
return entry->value;
}
return key;
}
2022-11-16 12:25:21 -05:00
void __stdcall Localization::SetStringStub(const char* psLocalReference, const char* psNewString, [[maybe_unused]] int bSentenceIsEnglish)
2017-01-19 16:23:59 -05:00
{
2022-11-16 12:25:21 -05:00
Set(psLocalReference, psNewString);
}
void Localization::ParseOutput(const std::function<void(Game::LocalizeEntry*)>& callback)
{
ParseCallback = callback;
}
void Localization::SaveParseOutput(Game::LocalizeEntry* asset)
{
if (ParseCallback)
2022-11-16 12:25:21 -05:00
{
ParseCallback(asset);
2022-11-16 12:25:21 -05:00
}
2017-01-19 16:23:59 -05:00
}
2017-02-08 10:25:48 -05:00
void Localization::SetCredits()
{
static const char* staff[] =
{
2022-07-07 09:32:18 -04:00
"Snake",
2017-02-08 10:25:48 -05:00
"/dev/../",
"/dev/console",
"/dev/full",
"/dev/sdb",
"/dev/sr0",
2021-02-07 17:26:47 -05:00
"/dev/tty0",
2017-02-08 10:25:48 -05:00
"/dev/urandom",
2022-07-07 09:32:18 -04:00
"Dss0",
"FutureRave",
"H3X1C",
"Homura",
"Laupetin",
"Louvenarde",
"lsb_release -a",
"quaK",
2017-02-08 10:25:48 -05:00
};
static const char* contributors[] =
{
"a231",
2017-02-08 10:25:48 -05:00
"AmateurHailbut",
"Aoki",
2021-10-16 16:21:32 -04:00
"Chase",
2017-02-08 10:25:48 -05:00
"civil",
"Dasfonia",
"Deity",
2017-02-08 10:25:48 -05:00
"Dizzy",
"HardNougat",
2020-12-22 15:12:23 -05:00
"INeedGames",
2022-07-07 09:32:18 -04:00
"JTAG",
2017-02-08 10:25:48 -05:00
"Killera",
"Lithium",
2020-12-22 15:12:23 -05:00
"OneFourOne",
"RaidMax",
2017-02-08 10:25:48 -05:00
"Revo",
"RezTech",
"Shadow the Hedgehog",
"Slykuiper",
2017-02-08 10:25:48 -05:00
"st0rm",
"VVLNT",
2022-07-07 09:32:18 -04:00
"X3RX35",
2017-02-08 10:25:48 -05:00
};
static const char* specials[] =
{
"NTAuthority",
"aerosoul94",
2017-02-08 13:27:35 -05:00
"ReactIW4",
"IW4Play",
2020-12-22 15:12:23 -05:00
"V2",
2017-07-05 15:08:34 -04:00
"luckyy"
2017-02-08 10:25:48 -05:00
};
std::string credits = "^2The IW4x Team:^7\n";
2022-07-07 09:32:18 -04:00
for (std::size_t i = 0; i < ARRAYSIZE(staff); ++i)
2017-02-08 10:25:48 -05:00
{
credits.append(staff[i]);
credits.append("\n");
}
credits.append("\n^3Contributors:^7\n");
2022-07-07 09:32:18 -04:00
for (std::size_t i = 0; i < ARRAYSIZE(contributors); ++i)
2017-02-08 10:25:48 -05:00
{
credits.append(contributors[i]);
credits.append("\n");
}
credits.append("\n^5Special thanks to:^7\n");
2022-07-07 09:32:18 -04:00
for (std::size_t i = 0; i < ARRAYSIZE(specials); ++i)
2017-02-08 10:25:48 -05:00
{
credits.append(specials[i]);
credits.append("\n");
}
// I have no idea why, but the last 2 lines are invisible!
credits.append("-\n-");
2022-11-16 12:25:21 -05:00
Set("IW4X_CREDITS", credits);
2017-02-08 10:25:48 -05:00
}
2022-08-31 16:21:26 -04:00
const char* Localization::SEH_LocalizeTextMessageStub(const char* pszInputBuffer, const char* pszMessageType, Game::msgLocErrType_t errType)
{
constexpr auto szStringCount = 10;
constexpr auto szStringSize = 1024;
char szInsertBuf[szStringSize];
char szTokenBuf[szStringSize];
static thread_local int iCurrString;
static thread_local char szStrings[szStringCount][szStringSize];
iCurrString = (iCurrString + 1) % szStringCount;
std::memset(szStrings[iCurrString], 0, sizeof(szStrings[0]));
auto* pszString = szStrings[iCurrString];
auto iLen = 0;
auto bLocOn = 1;
auto bInsertEnabled = 1;
auto iInsertLevel = 0;
auto insertIndex = 1;
auto bLocSkipped = 0;
const auto* pszTokenStart = pszInputBuffer;
const auto* pszIn = pszInputBuffer;
auto i = 0;
while (*pszTokenStart)
{
if (*pszIn && *pszIn != '\x14' && *pszIn != '\x15' && *pszIn != '\x16')
{
++pszIn;
continue;
}
if (pszIn > pszTokenStart)
{
auto iTokenLen = pszIn - pszTokenStart;
2022-09-02 04:27:44 -04:00
Game::I_strncpyz_s(szTokenBuf, sizeof(szTokenBuf), pszTokenStart, pszIn - pszTokenStart);
2022-08-31 16:21:26 -04:00
if (bLocOn)
{
if (!Game::SEH_GetLocalizedTokenReference(szTokenBuf, szTokenBuf, pszMessageType, errType))
{
return nullptr;
}
2022-09-02 04:27:44 -04:00
iTokenLen = std::strlen(szTokenBuf);
2022-08-31 16:21:26 -04:00
}
if (iTokenLen + iLen >= szStringSize)
{
Game::Com_Printf(Game::CON_CHANNEL_SYSTEM, "%s too long when translated\n", pszMessageType);
2022-08-31 16:21:26 -04:00
return nullptr;
}
for (i = 0; i < iTokenLen - 2; ++i)
{
if (!std::strncmp(&szTokenBuf[i], "&&", 2) && std::isdigit(szTokenBuf[i + 2]))
{
if (bInsertEnabled)
{
++iInsertLevel;
}
else
{
szTokenBuf[i] = '\x16';
bLocSkipped = 1;
}
}
}
if (iInsertLevel <= 0 || iLen <= 0)
{
2022-09-02 04:27:44 -04:00
Game::I_strcpy(&pszString[iLen], szStringSize - iLen, szTokenBuf);
2022-08-31 16:21:26 -04:00
}
else
{
for (i = 0; i < iLen - 2; ++i)
{
if (!std::strncmp(&pszString[i], "&&", 2) && std::isdigit(pszString[i + 2]))
{
const auto digit = pszString[i + 2] - 48;
if (!digit)
{
Game::Com_Printf(Game::CON_CHANNEL_SYSTEM, "%s cannot have &&0 as conversion format: \"%s\"\n", pszMessageType, pszInputBuffer);
}
if (digit == insertIndex)
{
2022-09-02 04:27:44 -04:00
Game::I_strcpy(szInsertBuf, sizeof(szInsertBuf), &pszString[i + 3]);
2022-08-31 16:21:26 -04:00
pszString[i] = 0;
++insertIndex;
break;
}
}
}
2022-09-02 04:27:44 -04:00
Game::I_strcpy(&pszString[i], szStringSize - i, szTokenBuf);
Game::I_strcpy(&pszString[iTokenLen + i], szStringSize - (iTokenLen + i), szInsertBuf);
2022-08-31 16:21:26 -04:00
iLen -= 3;
--iInsertLevel;
}
iLen += iTokenLen;
}
bInsertEnabled = 1;
if (*pszIn == '\x14')
{
bLocOn = 1;
++pszIn;
}
else if (*pszIn == '\x15')
{
bLocOn = 0;
++pszIn;
}
if (*pszIn == '\x16')
{
bInsertEnabled = 0;
++pszIn;
}
pszTokenStart = pszIn;
}
if (bLocSkipped)
{
for (i = 0; i < iLen; ++i)
{
if (pszString[i] == '\x16')
{
pszString[i] = '%';
}
}
}
return pszString;
}
2017-01-19 16:23:59 -05:00
Localization::Localization()
{
2022-11-16 12:25:21 -05:00
SetCredits();
2017-02-08 10:25:48 -05:00
2022-11-16 12:25:21 -05:00
AssetHandler::OnFind(Game::XAssetType::ASSET_TYPE_LOCALIZE_ENTRY, [](Game::XAssetType, const std::string& name)
2017-01-19 16:23:59 -05:00
{
Game::XAssetHeader header = { nullptr };
2022-11-16 12:25:21 -05:00
std::lock_guard _(LocalizeMutex);
2017-01-19 16:23:59 -05:00
2022-11-16 12:25:21 -05:00
if (const auto itr = LocalizeMap.find(name); itr != LocalizeMap.end())
2017-01-19 16:23:59 -05:00
{
2022-11-16 12:25:21 -05:00
header.localize = itr->second;
2017-01-19 16:23:59 -05:00
}
return header;
});
// Resolving hook
2022-11-16 12:25:21 -05:00
Utils::Hook(0x629B90, Get, HOOK_JUMP).install()->quick();
2017-01-19 16:23:59 -05:00
// Overwrite SetString
2022-11-16 12:25:21 -05:00
Utils::Hook(0x4CE5EE, SetStringStub, HOOK_CALL).install()->quick();
2017-01-19 16:23:59 -05:00
2022-11-16 12:25:21 -05:00
Utils::Hook(0x49D4A0, SEH_LocalizeTextMessageStub, HOOK_JUMP).install()->quick();
2022-08-31 16:21:26 -04:00
Utils::Hook::Nop(0x49D4A5, 1);
2022-11-16 12:25:21 -05:00
UseLocalization = Dvar::Register<bool>("ui_localize", true, Game::DVAR_NONE, "Use localization strings");
// Generate localized entries for custom classes above 10
2018-12-17 08:29:18 -05:00
AssetHandler::OnLoad([](Game::XAssetType type, Game::XAssetHeader asset, const std::string& name, bool* /*restrict*/)
{
if (type != Game::XAssetType::ASSET_TYPE_LOCALIZE_ENTRY) return;
if (name == "CLASS_SLOT1"s)
{
for (int i = 11; i <= NUM_CUSTOM_CLASSES; ++i)
{
std::string key = Utils::String::VA("CLASS_SLOT%i", i);
std::string value = asset.localize->value;
2022-11-07 19:19:17 -05:00
Utils::String::Replace(value, "1", std::to_string(i)); // Pretty ugly, but it should work
2022-11-16 12:25:21 -05:00
Set(key, value);
}
}
});
2017-01-19 16:23:59 -05:00
}
Localization::~Localization()
{
2022-11-16 12:25:21 -05:00
LocalizeMap.clear();
2017-01-19 16:23:59 -05:00
}
}