h2-mod/src/client/component/localized_strings.cpp

195 lines
4.2 KiB
C++
Raw Normal View History

2021-09-06 18:40:37 -04:00
#include <std_include.hpp>
2021-08-29 14:50:23 -04:00
#include "loader/component_loader.hpp"
2022-07-15 18:29:44 -04:00
2021-08-29 14:50:23 -04:00
#include "localized_strings.hpp"
2022-07-15 18:29:44 -04:00
#include "game_console.hpp"
#include "filesystem.hpp"
#include "console.hpp"
#include "game/game.hpp"
2021-08-29 14:50:23 -04:00
#include <utils/hook.hpp>
#include <utils/string.hpp>
#include <utils/concurrency.hpp>
2022-07-15 18:29:44 -04:00
#include <utils/io.hpp>
2021-08-29 14:50:23 -04:00
namespace localized_strings
{
namespace
{
utils::hook::detour seh_string_ed_get_string_hook;
2022-07-15 18:29:44 -04:00
struct localize_entry
{
std::string value{};
bool volatile_{};
};
using localized_map = std::unordered_map<std::string, localize_entry>;
2021-08-29 14:50:23 -04:00
utils::concurrency::container<localized_map> localized_overrides;
const char* seh_string_ed_get_string(const char* reference)
{
return localized_overrides.access<const char*>([&](const localized_map& map)
{
const auto entry = map.find(reference);
if (entry != map.end())
{
2022-07-15 18:29:44 -04:00
return utils::string::va("%s", entry->second.value.data());
2021-08-29 14:50:23 -04:00
}
return seh_string_ed_get_string_hook.invoke<const char*>(reference);
});
}
2022-07-14 20:43:50 -04:00
game::XAssetHeader db_find_localize_entry_stub(game::XAssetType type, const char* name, int allow_create_default)
{
const auto value = localized_overrides.access<const char*>([&](const localized_map& map)
-> const char*
{
const auto entry = map.find(name);
if (entry != map.end())
{
2022-07-15 18:29:44 -04:00
return utils::string::va("%s", entry->second.value.data());
2022-07-14 20:43:50 -04:00
}
return nullptr;
});
if (value == nullptr)
{
return game::DB_FindXAssetHeader(type, name, allow_create_default);
}
static game::LocalizeEntry entry{};
entry.value = value;
entry.name = name;
return static_cast<game::XAssetHeader>(&entry);
}
2022-07-15 18:29:44 -04:00
bool parse_localized_strings_file(const std::string& data)
{
rapidjson::Document j;
j.Parse(data.data());
if (!j.IsObject())
{
return false;
}
localized_overrides.access([&](localized_map& map)
{
const auto obj = j.GetObj();
for (const auto& [key, value] : obj)
{
if (!key.IsString() || !value.IsString())
{
continue;
}
const auto name = key.GetString();
const auto str = value.GetString();
const auto entry = map.find(name);
if (entry == map.end() || entry->second.volatile_)
{
map[name] = {str, true};
}
}
});
return true;
}
bool try_load_file(const std::string& path, const std::string& language)
{
const auto file = utils::string::va("%s/localizedstrings/%s.json", path.data(), language.data());
if (!utils::io::file_exists(file))
{
return false;
}
console::info("[Localized strings] Parsing %s\n", file);
const auto data = utils::io::read_file(file);
if (!parse_localized_strings_file(data))
{
console::error("[Localized strings] Invalid language json file\n");
return false;
}
return true;
}
void load_localized_strings()
{
bool found = false;
const auto search_paths = filesystem::get_search_paths();
const auto language = game::SEH_GetCurrentLanguageName();
for (const auto& path : search_paths)
{
if (!try_load_file(path, language))
{
if (try_load_file(path, "english"))
{
found = true;
console::warn("[Localized strings] No valid language file found for '%s' in '%s', falling back to 'english'\n",
language, path.data());
}
}
else
{
found = true;
}
}
if (!found)
{
console::warn("[Localized strings] No valid language file found!\n");
}
}
2021-08-29 14:50:23 -04:00
}
2022-07-15 18:29:44 -04:00
void override(const std::string& key, const std::string& value, bool volatile_)
2021-08-29 14:50:23 -04:00
{
localized_overrides.access([&](localized_map& map)
{
2022-07-15 18:29:44 -04:00
map[key] = {value, volatile_};
2021-08-29 14:50:23 -04:00
});
}
2022-07-15 18:29:44 -04:00
void clear()
{
localized_overrides.access([&](localized_map& map)
{
for (auto i = map.begin(); i != map.end();)
{
if (i->second.volatile_)
{
i = map.erase(i);
}
else
{
++i;
}
}
});
load_localized_strings();
}
2021-08-29 14:50:23 -04:00
class component final : public component_interface
{
public:
void post_unpack() override
{
// Change some localized strings
2022-03-18 17:02:44 -04:00
seh_string_ed_get_string_hook.create(0x1405E5FD0, &seh_string_ed_get_string);
2022-07-14 20:43:50 -04:00
utils::hook::call(0x1405E5AB9, db_find_localize_entry_stub);
2021-08-29 14:50:23 -04:00
}
};
}
REGISTER_COMPONENT(localized_strings::component)