Curation round 2, wave A: anm2 (Anm2Ed JSON), text, config as idiomatic classes; SDL3 platform opens a window
- 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>
This commit is contained in:
1 parent
d6e1ca7c6c
commit
780767f5e1
161 files changed
+8646
-21919
No files matched your search
+170
-75
@@ -1,101 +1,196 @@
|
||||
#include "classes.h"
|
||||
#include "msvc.h"
|
||||
#include <cstring>
|
||||
#include <string>
|
||||
#include "string_table.h"
|
||||
#include "data.h"
|
||||
#include <cstdlib>
|
||||
|
||||
constexpr const char* INVALID_LANGUAGE = "StringTable::InvalidLanguage";
|
||||
constexpr const char* INVALID_CATEGORY = "StringTable::InvalidCategory";
|
||||
constexpr const char* INVALID_KEY = "StringTable::InvalidKey";
|
||||
constexpr const char* UNTRANSLATED_STRING = "StringTable::UntranslatedString";
|
||||
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;
|
||||
|
||||
// Language id to its column in every string's translations.
|
||||
using LanguageNode = MsvcTreeNode<int, int>;
|
||||
// Lowercased category name to its strings.
|
||||
using CategoryNode = MsvcTreeNode<char*, StringTableCategory>;
|
||||
// Lowercased key to its translations, one per language column.
|
||||
using StringNode = MsvcTreeNode<char*, char**>;
|
||||
LAYOUT32(sizeof(LanguageNode) == 0x18 && sizeof(CategoryNode) == 0x28 && sizeof(StringNode) == 0x18);
|
||||
|
||||
static bool is_text_before(const char* text, const char* other)
|
||||
// A copy with ASCII capitals lowered, as the game matches names.
|
||||
static std::string text_lower(std::string_view text)
|
||||
{
|
||||
return std::strcmp(text, other) < 0;
|
||||
}
|
||||
|
||||
// A copy with ASCII capitals lowered, as the game matches names. No text reads as empty, where the game crashed.
|
||||
static std::string text_lower(uint8_t* text)
|
||||
{
|
||||
std::string lower = text ? (char*)text : "";
|
||||
std::string lower(text);
|
||||
|
||||
for (char& character : lower)
|
||||
if (character >= 'A' && character <= 'Z') character += 'a' - 'A';
|
||||
return lower;
|
||||
}
|
||||
|
||||
static void category_destroy(StringTableCategory& category)
|
||||
// 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)
|
||||
{
|
||||
FID_conflict__Tidy(&category._keys);
|
||||
tree_destroy<StringNode>(category._strings);
|
||||
return node["attrs"][name].get(value) == simdjson::SUCCESS;
|
||||
}
|
||||
|
||||
// 00a26920 Empties the table: every category along with its name, the default category and every language.
|
||||
// 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()
|
||||
{
|
||||
auto categories = (CategoryNode*)_categories._head;
|
||||
|
||||
for (CategoryNode* node = categories->_left; node != categories; node = tree_next(node))
|
||||
tracked_alloc(TRACKED_FREE, node->_key, 0);
|
||||
tree_clear<CategoryNode>(_categories, category_destroy);
|
||||
_defaultCategory = nullptr;
|
||||
_language = 0;
|
||||
tree_clear<LanguageNode>(_languages);
|
||||
categories.clear();
|
||||
defaultCategory.clear();
|
||||
language = 0;
|
||||
languages.clear();
|
||||
}
|
||||
|
||||
// 00a26a30 Translates "category/key", or a key of the default category, into the current language.
|
||||
uint8_t* StringTable::translate(uint8_t* key)
|
||||
// 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)
|
||||
{
|
||||
auto lowerKey = (char*)path_join(key, nullptr, true, false);
|
||||
char* separator = lowerKey ? std::strchr(lowerKey, CATEGORY_SEPARATOR) : nullptr;
|
||||
auto category = (uint8_t*)_defaultCategory;
|
||||
auto name = (uint8_t*)lowerKey;
|
||||
uint8_t isMissing = false;
|
||||
std::string path = text_lower(key);
|
||||
|
||||
if (separator)
|
||||
{
|
||||
*separator = '\0';
|
||||
category = (uint8_t*)lowerKey;
|
||||
name = (uint8_t*)separator + 1;
|
||||
}
|
||||
for (char& character : path)
|
||||
if (character == '\\') character = CATEGORY_SEPARATOR;
|
||||
|
||||
uint8_t* text = get_string(category, _language, name, &isMissing);
|
||||
tracked_alloc(TRACKED_FREE, lowerKey, 0);
|
||||
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;
|
||||
}
|
||||
|
||||
// 00a26af0 A string by category and key, both matched lowercased, in a language. A miss names what was missing and
|
||||
// sets outIsMissing. A language column equal to the language count reads past the translations in the game, so it is
|
||||
// taken as an invalid language here.
|
||||
uint8_t* StringTable::get_string(uint8_t* category, int language, uint8_t* key, uint8_t* outIsMissing)
|
||||
// 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)
|
||||
{
|
||||
*outIsMissing = true;
|
||||
if (column_find(languages, language) < 0) return INVALID_LANGUAGE;
|
||||
|
||||
auto languageNode = tree_find<LanguageNode>(_languages, language);
|
||||
if (languageNode->_isNil) return (uint8_t*)INVALID_LANGUAGE;
|
||||
|
||||
int column = languageNode->_value;
|
||||
if (column < 0 || column >= (int)_languages._size) return (uint8_t*)INVALID_LANGUAGE;
|
||||
|
||||
std::string categoryName = text_lower(category);
|
||||
auto categoryNode = tree_find<CategoryNode>(_categories, categoryName.data(), is_text_before);
|
||||
if (categoryNode->_isNil) return (uint8_t*)INVALID_CATEGORY;
|
||||
|
||||
std::string keyName = text_lower(key);
|
||||
auto stringNode = tree_find<StringNode>(categoryNode->_value._strings, keyName.data(), is_text_before);
|
||||
if (stringNode->_isNil) return (uint8_t*)INVALID_KEY;
|
||||
|
||||
char* text = stringNode->_value[column];
|
||||
if (!text) return (uint8_t*)UNTRANSLATED_STRING;
|
||||
|
||||
*outIsMissing = false;
|
||||
return (uint8_t*)text;
|
||||
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]);
|
||||
}
|
||||
Reference in new issue
Block a user