Files
isaac-port/src/data.cpp
T
shweetandClaude Opus 5.5 fd8b965350 Curation round 4: popups, Seeds/Random, Game, KeyConfigs, offline NetplayManager, Console, TextureCache, render passes
Six agents took over PopupButtons/PopupQueue/AchievementOverlay, Seeds with the global Random (now seeded from the
clock as in the original), Game (g_game exists from Manager::Init on), KeyConfigs, an offline NetplayManager, Console's
output and printing, TextureCache, the color-mod and sprite shader passes, mod shaders and reload_resource_paths. An
integration pass moved callers to the new APIs (0 errors, 0 warnings with -Wall -Wextra). build/isaac loads the 34
installed mods and stops at ScoreSheet::ScoreSheet in Game's constructor; with the remaining stubs skipped under gdb the
intro cutscene plays through and the main menu's frame loop runs.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-20 01:32:19 -05:00

96 lines
3.5 KiB
C++

#include "data.h"
#include "manager.h"
#include <cctype>
#include <filesystem>
constexpr std::string_view EMPTY_ARRAY = "[]";
// What a missing tag, attribute or text reads as: empty, but C text that atoi can take.
constexpr std::string_view EMPTY_TEXT = "";
constexpr std::string_view JSON_EXTENSION = ".json";
// Where the import tool puts a language archive's files (the original's resources.<code> mount).
constexpr std::string_view LANGUAGE_FOLDER = "lang/";
std::string dataDirectory = "resources/";
std::string languageDirectory;
// A game path lowercased with forward slashes, as the game normalizes every path before a lookup.
static std::string path_normalize(std::string_view path)
{
std::string normalized(path);
for (char& letter : normalized) letter = letter == '\\' ? '/' : (char)std::tolower((unsigned char)letter);
return normalized;
}
// A normalized path under the language's folder when that holds it, under the data directory otherwise.
static std::string mount_resolve(const std::string& normalized)
{
std::error_code error;
std::string localized = languageDirectory + normalized;
if (!languageDirectory.empty() && std::filesystem::exists(localized, error)) return localized;
return dataDirectory + normalized;
}
// Where a game path lives once imported.
std::string resource_path(std::string_view path)
{
return mount_resolve(path_normalize(path));
}
// Where a converted XML document lives: its game path with ".json" appended.
std::string data_path(std::string_view path)
{
return mount_resolve(path_normalize(path) + std::string(JSON_EXTENSION));
}
// 009abbd0 Remounts the resources, with the folder of the string table's language in front once there is a manager.
// The language folder is only mounted when the import tool wrote one, which finds the same files.
// The original also mounts the game folder behind the resources when its file system already knows it. Nothing on
// Linux reads from there.
void reload_resource_paths()
{
languageDirectory.clear();
if (!g_manager) return;
std::string localized = dataDirectory + std::string(LANGUAGE_FOLDER) + std::string(Manager::GetLanguage()) + '/';
std::error_code error;
if (std::filesystem::is_directory(localized, error)) languageDirectory = localized;
}
// Loads a converted document, leaving isLoaded false when it is missing or unreadable.
void data_load(Data& data, std::string_view path)
{
data.isLoaded = data.parser.load(data_path(path)).get(data.root) == simdjson::SUCCESS;
}
std::string_view node_tag(DataNode node)
{
std::string_view tag;
return node["tag"].get(tag) == simdjson::SUCCESS ? tag : EMPTY_TEXT;
}
// An attribute's raw string, or an empty one when the element lacks it.
std::string_view node_attr(DataNode node, std::string_view name)
{
std::string_view value;
return node["attrs"][name].get(value) == simdjson::SUCCESS ? value : EMPTY_TEXT;
}
std::string_view node_text(DataNode node)
{
std::string_view text;
return node["text"].get(text) == simdjson::SUCCESS ? text : EMPTY_TEXT;
}
// The element's children in document order, empty when it has none.
// A default-constructed array points at no document, so a parsed empty one stands in.
simdjson::dom::array node_children(DataNode node)
{
static simdjson::dom::parser emptyParser;
static simdjson::dom::array empty =
emptyParser.parse(EMPTY_ARRAY.data(), EMPTY_ARRAY.size()).get_array().value_unsafe();
simdjson::dom::array children;
return node["children"].get(children) == simdjson::SUCCESS ? children : empty;
}