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:
shweetandClaude Opus 5.5 committed 2026-11-19 23:04:54 -05:00
1 parent d6e1ca7c6c
commit 780767f5e1
161 files changed
+8646 -21919

No files matched your search

+299 -128
View File
@@ -1,149 +1,320 @@
#include "classes.h"
#include "data.h"
#include "log.h"
#include "options_config.h"
#include <string_view>
#include <SDL3/SDL_filesystem.h>
#include <SDL3/SDL_iostream.h>
#include <SDL3/SDL_stdinc.h>
#include <algorithm>
#include <cstdio>
#include <optional>
#include <vector>
constexpr float DEFAULT_VOLUME = 0.3f;
constexpr float DEFAULT_MAP_OPACITY = 0.3f;
constexpr int DEFAULT_ONLINE_PLAYER_VOLUME = 6;
constexpr int DEFAULT_ONLINE_PLAYER_OPACITY = 10;
constexpr int DEFAULT_ONLINE_INPUT_DELAY = 3;
constexpr int DEFAULT_MAX_SCALE = 99;
constexpr int DEFAULT_MAX_RENDER_SCALE = 2;
constexpr int DEFAULT_WINDOW_X = 8;
constexpr int DEFAULT_WINDOW_Y = 32;
// An MSVC string keeps up to 15 characters in its own buffer, and a pointer to the heap there past that.
constexpr uint32_t STRING_INLINE_CAPACITY = 15;
constexpr const char* CONFIG_INI = "config.ini";
constexpr const char* OPTIONS_INI = "options.ini";
constexpr std::string_view LEGACY_SAVE_FOLDER = "Binding of Isaac Afterbirth+";
constexpr std::string_view SAVE_FOLDER = "Binding of Isaac Repentance+";
constexpr std::string_view WHITESPACE = " \t\f\v\r\n";
constexpr int LINE_LENGTH = 256;
constexpr int ONLINE_INPUT_DELAY_MAX = 5;
constexpr int ONLINE_LEVEL_MAX = 10;
constexpr WindowSetting LOAD_WINDOW_SETTINGS[] = {WINDOW_VSYNC, WINDOW_CURSOR, WINDOW_POSITION, WINDOW_FULLSCREEN};
// A node of the MSVC std::map<std::string, std::string> an .ini file is parsed into.
struct StringMapNode
// The pieces std::getline cuts text into at separator, which drops an empty last piece.
static std::vector<std::string_view> text_split(std::string_view text, char separator)
{
StringMapNode* left;
StringMapNode* parent;
StringMapNode* right;
char color;
bool isNil;
alignas(void*) std_string key;
std_string value;
};
LAYOUT32(offsetof(StringMapNode, value) == 0x28 && sizeof(StringMapNode) == 0x40);
std::vector<std::string_view> pieces;
static char* string_text(std_string& text)
{
return text._capacity <= STRING_INLINE_CAPACITY ? text._buf : *(char**)text._buf;
}
static std::string_view string_view_get(std_string& text)
{
return {string_text(text), text._size};
}
// The value stored under key, or null when the map has none.
static std_string* keyvalue_find(std_map* map, std::string_view key)
{
auto head = (StringMapNode*)map->_head;
StringMapNode* bound = head;
for (StringMapNode* node = head->parent; !node->isNil;)
while (!text.empty())
{
if (string_view_get(node->key) < key) node = node->right;
else
{
bound = node;
node = node->left;
}
size_t end = text.find(separator);
pieces.push_back(text.substr(0, end));
text = end == text.npos ? std::string_view() : text.substr(end + 1);
}
bool isFound = !bound->isNil && !(key < string_view_get(bound->key));
return isFound ? &bound->value : nullptr;
return pieces;
}
// 006841d0 A key's value as a bool, true for "true", "True" or a nonzero number, and the default when missing or empty.
// Its this is not an OptionsConfig but the key-value map an .ini file was parsed into.
uint8_t OptionsConfig::read_bool(uint8_t* key, uint8_t isDefault)
// The text without the whitespace around it.
static std::string_view text_trim(std::string_view text)
{
std_string* value = keyvalue_find((std_map*)this, (char*)key);
if (!value || value->_size == 0) return isDefault;
size_t start = text.find_first_not_of(WHITESPACE);
if (start == text.npos) return {};
char* text = string_text(*value);
return !strcmp(text, "true") || !strcmp(text, "True") || atoi(text) != 0;
return text.substr(start, text.find_last_not_of(WHITESPACE) + 1 - start);
}
// 00924000 Puts every option options.ini holds back to its default, the window at the size it has now.
void OptionsConfig::reset_defaults()
// 00683d00 Reads key=value lines into a map, trimmed, skipping lines without exactly one '=' or with no key.
// A later line with the same key wins.
KeyValueMap keyvalue_map_parse(std::string_view text)
{
int windowSize[2];
KeyValueMap map;
_musicVolume = DEFAULT_VOLUME;
_sfxVolume = DEFAULT_VOLUME;
_mapOpacity = DEFAULT_MAP_OPACITY;
_brightness = 0;
_contrast = 1;
_exposure = 1;
_gamma = 1;
_hudOffset = 1;
for (std::string_view line : text_split(text, '\n'))
{
std::vector<std::string_view> pair = text_split(text_trim(line), '=');
if (pair.size() != 2) continue;
_announcerVoiceMode = ANNOUNCER_VOICE_MODE_RANDOM;
_cameraStyle = CAMERA_STYLE_ACTIVE_CAM_ON;
_consoleFont = CONSOLE_FONT_DEFAULT;
_eosCrossplay = 0;
_historyHudMode = EXTRA_HUD_STYLE_OFF;
_onlineColorSet = 0;
_onlineHud = 0;
_onlineInputDelay = DEFAULT_ONLINE_INPUT_DELAY;
_onlinePlayerOpacity = DEFAULT_ONLINE_PLAYER_OPACITY;
_onlinePlayerVolume = DEFAULT_ONLINE_PLAYER_VOLUME;
_popupsMode = 1;
_touchMode = 1;
_acceptedDataCollectionDisclaimer = false;
_acceptedPublicBeta_VERSION = false;
_ascentVoiceover = true;
_bossHpOnBottom = true;
_controlHotplugEnabled = true;
_enableAimLock = true;
_enableBulletVisibility = false;
_enableChargeBars = false;
_enableDebugConsole = false;
_enableEpicOverlay = true;
_enableFadedConsoleDisplay = false;
_enableFoundHUD = false;
_enableMods = true;
_enableMouseControl = false;
_enablePauseOnFocusLost = true;
_enableRumble = true;
_enableSaveCommandHistory = true;
_enableSteamCloud = true;
_enableVSync = true;
_filterEnabled = false;
_isExclusiveFullscreen = false;
_isFullscreen = false;
_itemInfoDisplayEnabled = false;
_onlineChatEnabled = true;
_onlineFilterEnabled = true;
_streamerMode = false;
_tryImportSave = false;
glfw_get_window_size(windowSize, 0);
_maxRenderScale = DEFAULT_MAX_RENDER_SCALE;
_maxScale = DEFAULT_MAX_SCALE;
_windowHeight = windowSize[1];
_windowPosX = DEFAULT_WINDOW_X;
_windowPosY = DEFAULT_WINDOW_Y;
_windowWidth = windowSize[0];
_optionsIniFilePath._size = 0;
string_text(_optionsIniFilePath)[0] = '\0';
std::string_view key = text_trim(pair[0]);
if (!key.empty()) map.insert_or_assign(std::string(key), std::string(text_trim(pair[1])));
}
return map;
}
// 00925c60 Switches fullscreen on or off, going back to the saved window size, position and cursor when off.
void OptionsConfig::set_fullscreen(uint8_t isFullscreen)
// 00684590 A key's value, or an empty one when the map lacks it.
std::string keyvalue_map_get(KeyValueMap& map, std::string_view key)
{
_isFullscreen = isFullscreen;
KAGE_Graphics::set_fullscreen(isFullscreen, _isExclusiveFullscreen);
if (_isFullscreen) return;
int windowSize[] = {_windowWidth, _windowHeight};
KAGE_Graphics::set_window_size(windowSize);
glfw_set_window_pos(_windowPosX, _windowPosY);
glfwSetInputMode(g_glfwWindow, _enableMouseControl ? GLFW_CURSOR_HIDDEN : GLFW_CURSOR_NORMAL);
auto found = map.find(key);
return found == map.end() ? std::string() : found->second;
}
// 00684130 A key's value as an int, or the fallback when it is missing or empty.
int keyvalue_map_read_int(KeyValueMap& map, std::string_view key, int fallback)
{
std::string value = keyvalue_map_get(map, key);
return value.empty() ? fallback : std::atoi(value.c_str());
}
// 006843a0 A key's value as a float, or the fallback when it is missing or empty.
float keyvalue_map_read_float(KeyValueMap& map, std::string_view key, float fallback)
{
std::string value = keyvalue_map_get(map, key);
return value.empty() ? fallback : (float)std::atof(value.c_str());
}
// 006841d0 Whether a key reads "true", "True" or a nonzero number, or the fallback when it is missing or empty.
bool is_keyvalue_map_true(KeyValueMap& map, std::string_view key, bool isFallback)
{
std::string value = keyvalue_map_get(map, key);
if (value.empty()) return isFallback;
return value == "true" || value == "True" || std::atoi(value.c_str()) != 0;
}
// The .ini file at path read up to its first NUL and parsed, or nothing when it cannot be read.
static std::optional<KeyValueMap> ini_load(const char* path)
{
char* text = (char*)SDL_LoadFile(path, nullptr);
if (!text) return std::nullopt;
KeyValueMap map = keyvalue_map_parse(text);
SDL_free(text);
return map;
}
// Reads an option, keeping its current value when the key is missing or empty.
static void option_read(KeyValueMap& map, const char* key, bool& value)
{
value = is_keyvalue_map_true(map, key, value);
}
// Floats, which atof reads.
static void option_read(KeyValueMap& map, const char* key, float& value)
{
value = keyvalue_map_read_float(map, key, value);
}
// Ints and the enums options.ini stores as ints.
template <typename T> static void option_read(KeyValueMap& map, const char* key, T& value)
{
value = (T)keyvalue_map_read_int(map, key, value);
}
// Adds a key=value line, floats with 4 decimals.
static void line_write(std::string& text, const char* key, float value)
{
char line[LINE_LENGTH];
text.append(line, std::snprintf(line, sizeof(line), "%s=%.4f\r\n", key, value));
}
// Ints, bools and enums, all written as numbers.
static void line_write(std::string& text, const char* key, int value)
{
char line[LINE_LENGTH];
text.append(line, std::snprintf(line, sizeof(line), "%s=%d\r\n", key, value));
}
// Calls visit with each option options.ini holds under its own key, in the order Save writes them.
template <typename Visit> static void options_visit(OptionsConfig& options, Visit visit)
{
visit("SFXVolume", options.sfxVolume);
visit("MapOpacity", options.mapOpacity);
visit("Fullscreen", options.isFullscreen);
visit("Filter", options.isFilterEnabled);
visit("Exposure", options.exposure);
visit("Gamma", options.gamma);
visit("ControllerHotplug", options.isControllerHotplugEnabled);
visit("PopUps", options.popupsMode);
visit("CameraStyle", options.cameraStyle);
visit("ShowRecentItems", options.historyHudMode);
visit("HudOffset", options.hudOffset);
visit("TryImportSave", options.isTryImportSave);
visit("FoundHUD", options.isFoundHudEnabled);
visit("EnableMods", options.isModsEnabled);
visit("RumbleEnabled", options.isRumbleEnabled);
visit("ChargeBars", options.isChargeBarsEnabled);
visit("BulletVisibility", options.isBulletVisibilityEnabled);
visit("TouchMode", options.touchMode);
visit("AimLock", options.isAimLockEnabled);
visit("JacobEsauControls", options.jacobEsauControls);
visit("AscentVoiceOver", options.isAscentVoiceover);
visit("OnlineHud", options.onlineHud);
visit("StreamerMode", options.isStreamerMode);
visit("OnlinePlayerVolume", options.onlinePlayerVolume);
visit("OnlinePlayerOpacity", options.onlinePlayerOpacity);
visit("OnlineChatEnabled", options.isOnlineChatEnabled);
visit("OnlineChatFilterEnabled", options.isOnlineFilterEnabled);
visit("MultiplayerColorSet", options.onlineColorSet);
visit("OnlineInputDelay", options.onlineInputDelay);
visit("ItemInfoDisplayEnabled", options.isItemInfoDisplayEnabled);
visit("AcceptedPublicBeta_v1.9.7.17", options.isPublicBetaAccepted);
visit("AcceptedDataCollectionDisclaimer", options.isDataCollectionDisclaimerAccepted);
visit("EnableDebugConsole", options.isDebugConsoleEnabled);
visit("MaxScale", options.maxScale);
visit("MaxRenderScale", options.maxRenderScale);
visit("VSync", options.isVsyncEnabled);
visit("PauseOnFocusLost", options.isPauseOnFocusLostEnabled);
visit("SteamCloud", options.isSteamCloudEnabled);
visit("MouseControl", options.isMouseControlEnabled);
visit("BossHpOnBottom", options.isBossHpOnBottom);
visit("AnnouncerVoiceMode", options.announcerVoiceMode);
visit("ConsoleFont", options.consoleFont);
visit("FadedConsoleDisplay", options.isFadedConsoleDisplayEnabled);
visit("SaveCommandHistory", options.isSaveCommandHistoryEnabled);
visit("WindowWidth", options.windowWidth);
visit("WindowHeight", options.windowHeight);
visit("WindowPosX", options.windowPosX);
visit("WindowPosY", options.windowPosY);
visit("UseExclusiveFullscreen", options.isExclusiveFullscreen);
visit("EnableEpicOverlay", options.isEpicOverlayEnabled);
visit("EosCrossplay", options.eosCrossplay);
}
// Where options.ini lives on Linux: in the save folder SDL keeps under the user's data directory.
std::string options_path()
{
char* folder = SDL_GetPrefPath("", SAVE_FOLDER.data());
std::string path = std::string(folder ? folder : "") + OPTIONS_INI;
SDL_free(folder);
return path;
}
// 00924000 Puts every option options.ini holds back to its default, keeping config.ini's switches and taking the
// window's current size.
// The original leaves jacobEsauControls as it was, unset before the first load. This resets it to its default 1.
void OptionsConfig::defaults_reset()
{
RenderConfig kept = render;
*this = {};
render = kept;
window_size_load(*this);
}
// 00924180 Reads config.ini's effect switches, each on unless the file turns it off.
void OptionsConfig::config_ini_load()
{
render = {};
std::optional<KeyValueMap> map = ini_load(resource_path(CONFIG_INI).c_str());
if (!map) return;
option_read(*map, "EnableColorCorrection", render.isColorCorrectionEnabled);
option_read(*map, "EnableCaustics", render.isCausticsEnabled);
option_read(*map, "EnableShockwave", render.isShockwaveEnabled);
option_read(*map, "EnableLighting", render.isLightingEnabled);
option_read(*map, "EnableFilter", render.isFilterEnabled);
option_read(*map, "EnablePixelation", render.isPixelationEnabled);
option_read(*map, "EnableBloom", render.isBloomEnabled);
option_read(*map, "EnableWaterSurface", render.isWaterSurfaceEnabled);
option_read(*map, "EnableInterpolation", render.isInterpolationEnabled);
option_read(*map, "EnableColorModifier", render.isColorModifierEnabled);
render.isWaterOverlayEnabled = render.isWaterSurfaceEnabled;
}
// 00924440 Reads options.ini at path, falling back to Afterbirth+'s and then to writing the defaults, and applies the
// sound and window options.
// Afterbirth+'s options keep everything but its gamma and HUD offset. Brightness and contrast are read, never saved.
// AscentVoiceOver is read as a number, so "true" turns it off.
void OptionsConfig::Load(std::string_view path)
{
defaults_reset();
optionsIniFilePath = path;
std::optional<KeyValueMap> map = ini_load(optionsIniFilePath.c_str());
bool isLegacy = !map;
if (isLegacy)
{
std::string legacyPath = optionsIniFilePath;
size_t folder = legacyPath.rfind(SAVE_FOLDER);
if (folder != legacyPath.npos) legacyPath.replace(folder, SAVE_FOLDER.size(), LEGACY_SAVE_FOLDER);
map = ini_load(legacyPath.c_str());
}
if (!map) return Save();
KeyValueMap& values = *map;
g_manager->validate_language(keyvalue_map_read_int(values, "Language", 0));
SetMusicVolume(keyvalue_map_read_float(values, "MusicVolume", musicVolume));
if (!is_keyvalue_map_true(values, "MusicEnabled", true)) g_manager->_musicManager._isEnabled = false;
options_visit(*this, [&](const char* key, auto& value) { option_read(values, key, value); });
option_read(values, "Brightness", brightness);
option_read(values, "Contrast", contrast);
isAscentVoiceover = keyvalue_map_read_int(values, "AscentVoiceOver", 1) != 0;
g_manager->_sfxManager.ClearVolumeModifier();
onlinePlayerVolume = std::clamp(onlinePlayerVolume, 0, ONLINE_LEVEL_MAX);
onlinePlayerOpacity = std::clamp(onlinePlayerOpacity, 0, ONLINE_LEVEL_MAX);
onlineInputDelay = std::clamp(onlineInputDelay, 1, ONLINE_INPUT_DELAY_MAX);
if (windowPosX < WINDOW_POSITION_MIN || windowPosX > WINDOW_POSITION_MAX) windowPosX = DEFAULT_WINDOW_X;
if (windowPosY < WINDOW_POSITION_MIN || windowPosY > WINDOW_POSITION_MAX) windowPosY = DEFAULT_WINDOW_Y;
for (WindowSetting setting : LOAD_WINDOW_SETTINGS) window_apply(*this, setting);
if (!isLegacy) return;
gamma = 1;
hudOffset = 1;
}
// 00924d10 Writes every option to options.ini, with the language and whether music plays.
void OptionsConfig::Save()
{
std::string text = "[Options]\r\n";
line_write(text, "Language", g_manager->_stringTable._language);
line_write(text, "MusicVolume", musicVolume);
line_write(text, "MusicEnabled", g_manager->_musicManager._isEnabled);
options_visit(*this, [&](const char* key, auto value) { line_write(text, key, value); });
text += "\r\n";
if (SDL_SaveFile(optionsIniFilePath.c_str(), text.data(), text.size())) return;
std::string message = "[warn] could not opn " + optionsIniFilePath + " for writing. options will not be saved.\n";
KAGE_LogMessage(LOG_INFO, (uint8_t*)message.data());
}
// 00925c00 Sets the music volume, turning music off at 0, and moves the playing channel straight to it.
void OptionsConfig::SetMusicVolume(float volume)
{
MusicManager& music = g_manager->_musicManager;
MusicChannel& channel = music._channels[music._currentChannel];
musicVolume = volume;
music._isEnabled = volume > 0;
channel._volume = musicVolume;
channel._targetVolume = musicVolume;
}
// 00925c60 Switches fullscreen on or off, back to the saved window size, position and cursor when off.
void OptionsConfig::SetFullScreen(bool isEnabled)
{
isFullscreen = isEnabled;
window_apply(*this, WINDOW_FULLSCREEN);
}
// 00925ce0 Turns vsync on or off. The platform keeps it off for the session on a display that does not run at a
// multiple of 60Hz.
void OptionsConfig::vsync_set(bool isEnabled)
{
isVsyncEnabled = isEnabled;
window_apply(*this, WINDOW_VSYNC);
}