#include "classes.h" #include "data.h" #include "log.h" #include "manager.h" #include "options_config.h" #include #include #include #include #include #include #include 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}; // The pieces std::getline cuts text into at separator, which drops an empty last piece. static std::vector text_split(std::string_view text, char separator) { std::vector pieces; while (!text.empty()) { size_t end = text.find(separator); pieces.push_back(text.substr(0, end)); text = end == text.npos ? std::string_view() : text.substr(end + 1); } return pieces; } // The text without the whitespace around it. static std::string_view text_trim(std::string_view text) { size_t start = text.find_first_not_of(WHITESPACE); if (start == text.npos) return {}; return text.substr(start, text.find_last_not_of(WHITESPACE) + 1 - start); } // 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) { KeyValueMap map; for (std::string_view line : text_split(text, '\n')) { std::vector pair = text_split(text_trim(line), '='); if (pair.size() != 2) continue; 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; } // 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) { 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 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 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 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 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 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(); 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); }