- anm2.h/.cpp take over ANM2, AnimationState, LayerState, AnmCache and the frame data. They read Anm2Ed's JSON documents with simdjson and play them as the game does (interpolation baked at load). src/anm2/ and msvc.h are retired. - font, formatted_text, string_table and rich_text take over the text classes. FormattedText::parse is still bit-exact against its assembly. - options_config and challenge_params take over OptionsConfig, ChallengeParams and the .ini helpers. The tests include Save's exact bytes. - platform.h/.cpp and main.cpp run IsaacMain on SDL3: the window and GL context open, and the run stops at the first built-in shader (FUN_00a13fa0). - source_tree.py: a taken-over parent stays bytes, taken-over methods are not re-emitted as a namespace, and every address in a (a, b, c to d) list on a comment counts as taken over. - log.h: reconstruction_missing() for a taken-over class's methods that are not written yet. 206 functions taken over, boot path 89/615. The full build waits for wave B: item_config, entity_config and mod_manager still use the old APIs. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
197 lines
7.8 KiB
C++
197 lines
7.8 KiB
C++
#include "string_table.h"
|
|
#include "data.h"
|
|
#include <cstdlib>
|
|
|
|
constexpr std::string_view INVALID_LANGUAGE = "StringTable::InvalidLanguage";
|
|
constexpr std::string_view INVALID_CATEGORY = "StringTable::InvalidCategory";
|
|
constexpr std::string_view INVALID_KEY = "StringTable::InvalidKey";
|
|
constexpr std::string_view INVALID_KEY_INDEX = "StringTable::InvalidKeyIndex";
|
|
constexpr std::string_view UNTRANSLATED_STRING = "StringTable::UntranslatedString";
|
|
constexpr std::string_view TABLE_EXTENSION = "sta";
|
|
constexpr char CATEGORY_SEPARATOR = '/';
|
|
// Languages with an id from here up, like the file's "Key" column, get no column.
|
|
constexpr int LANGUAGE_ID_END = 20;
|
|
|
|
// A copy with ASCII capitals lowered, as the game matches names.
|
|
static std::string text_lower(std::string_view text)
|
|
{
|
|
std::string lower(text);
|
|
|
|
for (char& character : lower)
|
|
if (character >= 'A' && character <= 'Z') character += 'a' - 'A';
|
|
return lower;
|
|
}
|
|
|
|
// An attribute's text, or false when the element lacks it.
|
|
static bool attr_find(DataNode node, std::string_view name, std::string_view& value)
|
|
{
|
|
return node["attrs"][name].get(value) == simdjson::SUCCESS;
|
|
}
|
|
|
|
// The first child named tag, as rapidxml's first_node finds it, or false.
|
|
static bool node_first(DataNode node, std::string_view tag, DataNode& found)
|
|
{
|
|
for (DataNode child : node_children(node))
|
|
{
|
|
found = child;
|
|
if (node_tag(child) == tag) return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
// The document's first element named tag, as rapidxml's first_node finds it under the document, or false.
|
|
static bool root_find(Data& data, std::string_view tag, DataNode& root)
|
|
{
|
|
if (node_tag(data.root) == "#document") return node_first(data.root, tag, root);
|
|
root = data.root;
|
|
return node_tag(root) == tag;
|
|
}
|
|
|
|
// The children from the first one named tag on: all of them, or with isNamed only those named tag, as rapidxml's
|
|
// next_sibling walks them.
|
|
template <class Visit>
|
|
static void node_siblings_walk(DataNode node, std::string_view tag, bool isNamed, Visit visit)
|
|
{
|
|
bool isFound = false;
|
|
|
|
for (DataNode child : node_children(node))
|
|
{
|
|
isFound = isFound || node_tag(child) == tag;
|
|
if (isFound && (!isNamed || node_tag(child) == tag)) visit(child);
|
|
}
|
|
}
|
|
|
|
// The column of a language id, or -1 for an unknown language or a column past the language count.
|
|
static int column_find(std::map<int, int>& languages, int language)
|
|
{
|
|
auto found = languages.find(language);
|
|
return found == languages.end() || found->second < 0 || found->second >= (int)languages.size() ? -1 : found->second;
|
|
}
|
|
|
|
// 00a26810 Loads a .sta file and makes its lowest language id current. Anything else empties the table.
|
|
// The extension is what follows the last dot, or all but the first character when there is none, as in the game.
|
|
bool StringTable::Load(std::string_view fileName)
|
|
{
|
|
size_t dot = fileName.rfind('.');
|
|
size_t extension = (dot == std::string_view::npos ? 0 : dot) + 1;
|
|
bool isTable = !fileName.empty() && fileName.substr(extension) == TABLE_EXTENSION;
|
|
bool isLoaded = false;
|
|
|
|
clear();
|
|
if (isTable) isLoaded = load_file(fileName);
|
|
if (isLoaded && !languages.empty()) language = languages.begin()->first;
|
|
return isLoaded;
|
|
}
|
|
|
|
// 00a26f20, 00a27038 Reads the languages, giving each with an id below 20 the next column, then every category's keys
|
|
// and their strings in column order. The first named category becomes the default one, and a repeated category or key
|
|
// replaces the earlier one.
|
|
// A file without the root element reads as empty and one without languages has no translations, where the game crashed.
|
|
bool StringTable::load_file(std::string_view path)
|
|
{
|
|
Data data;
|
|
DataNode languagesNode;
|
|
DataNode root;
|
|
int column = 0;
|
|
|
|
data_load(data, path);
|
|
if (!data.isLoaded) return false;
|
|
if (!root_find(data, "stringtable", root)) return true;
|
|
|
|
if (node_first(root, "languages", languagesNode))
|
|
node_siblings_walk(languagesNode, "language", false, [&](DataNode languageNode) {
|
|
std::string_view id;
|
|
if (!attr_find(languageNode, "id", id)) return;
|
|
|
|
int languageId = std::atoi(std::string(id).c_str());
|
|
if (languageId < LANGUAGE_ID_END) languages[languageId] = column++;
|
|
});
|
|
|
|
node_siblings_walk(root, "category", true, [&](DataNode categoryNode) {
|
|
std::string_view categoryName;
|
|
if (!attr_find(categoryNode, "name", categoryName)) return;
|
|
|
|
std::string name = text_lower(categoryName);
|
|
StringTableCategory& category = categories[name] = {};
|
|
if (defaultCategory.empty()) defaultCategory = name;
|
|
|
|
node_siblings_walk(categoryNode, "key", true, [&](DataNode keyNode) {
|
|
std::string_view keyName;
|
|
if (!attr_find(keyNode, "name", keyName)) return;
|
|
|
|
std::vector<std::string>& translations = category.strings[text_lower(keyName)] = {};
|
|
category.keys.push_back(text_lower(keyName));
|
|
node_siblings_walk(keyNode, "string", true, [&](DataNode stringNode) {
|
|
if (translations.size() < languages.size()) translations.emplace_back(node_text(stringNode));
|
|
});
|
|
});
|
|
});
|
|
return true;
|
|
}
|
|
|
|
// 00a26920 Empties the table: every category, the default one and every language.
|
|
void StringTable::clear()
|
|
{
|
|
categories.clear();
|
|
defaultCategory.clear();
|
|
language = 0;
|
|
languages.clear();
|
|
}
|
|
|
|
// 00a26a30 Translates "category/key", or a key of the default category, into the current language. The key is
|
|
// lowered and its backslashes made slashes first.
|
|
std::string_view StringTable::translate(std::string_view key)
|
|
{
|
|
std::string path = text_lower(key);
|
|
|
|
for (char& character : path)
|
|
if (character == '\\') character = CATEGORY_SEPARATOR;
|
|
|
|
size_t separator = path.find(CATEGORY_SEPARATOR);
|
|
if (separator == std::string::npos) return get_string(defaultCategory, language, path);
|
|
std::string_view view = path;
|
|
return get_string(view.substr(0, separator), language, view.substr(separator + 1));
|
|
}
|
|
|
|
// A string by category and key in a language, or a text naming what was missing.
|
|
static std::string_view string_find(StringTable& table, std::string_view category, int language, std::string_view key,
|
|
bool& isMissing)
|
|
{
|
|
int column = column_find(table.languages, language);
|
|
if (column < 0) return INVALID_LANGUAGE;
|
|
|
|
auto categoryFound = table.categories.find(text_lower(category));
|
|
if (categoryFound == table.categories.end()) return INVALID_CATEGORY;
|
|
|
|
auto& strings = categoryFound->second.strings;
|
|
auto stringFound = strings.find(text_lower(key));
|
|
if (stringFound == strings.end()) return INVALID_KEY;
|
|
if ((size_t)column >= stringFound->second.size()) return UNTRANSLATED_STRING;
|
|
|
|
isMissing = false;
|
|
return stringFound->second[column];
|
|
}
|
|
|
|
// 00a26af0 A string by category and key in a language. A miss names what was missing and sets outIsMissing.
|
|
// A language column at the language count reads past the translations in the game, so it is an invalid language here.
|
|
std::string_view StringTable::get_string(std::string_view category, int language, std::string_view key,
|
|
bool* outIsMissing)
|
|
{
|
|
bool isMissing = true;
|
|
std::string_view text = string_find(*this, category, language, key, isMissing);
|
|
|
|
if (outIsMissing) *outIsMissing = isMissing;
|
|
return text;
|
|
}
|
|
|
|
// 00a26d30 The string at a key's place in its category's file order, in a language.
|
|
std::string_view StringTable::get_string_by_index(std::string_view category, int language, size_t keyIndex)
|
|
{
|
|
if (column_find(languages, language) < 0) return INVALID_LANGUAGE;
|
|
|
|
auto found = categories.find(text_lower(category));
|
|
if (found == categories.end()) return INVALID_CATEGORY;
|
|
if (keyIndex >= found->second.keys.size()) return INVALID_KEY_INDEX;
|
|
return get_string(category, language, found->second.keys[keyIndex]);
|
|
}
|