#include "settings.h" #include "common/make_array.h" #include "common/string_util.h" #include "host_display.h" #include "host_interface.h" #include #include #include #include Settings g_settings; SettingsInterface::~SettingsInterface() = default; const char* SettingInfo::StringDefaultValue() const { return default_value ? default_value : ""; } bool SettingInfo::BooleanDefaultValue() const { return default_value ? StringUtil::FromChars(default_value).value_or(false) : false; } s32 SettingInfo::IntegerDefaultValue() const { return default_value ? StringUtil::FromChars(default_value).value_or(0) : 0; } s32 SettingInfo::IntegerMinValue() const { static constexpr s32 fallback_value = std::numeric_limits::min(); return min_value ? StringUtil::FromChars(min_value).value_or(fallback_value) : fallback_value; } s32 SettingInfo::IntegerMaxValue() const { static constexpr s32 fallback_value = std::numeric_limits::max(); return max_value ? StringUtil::FromChars(max_value).value_or(fallback_value) : fallback_value; } s32 SettingInfo::IntegerStepValue() const { static constexpr s32 fallback_value = 1; return step_value ? StringUtil::FromChars(step_value).value_or(fallback_value) : fallback_value; } float SettingInfo::FloatDefaultValue() const { return default_value ? StringUtil::FromChars(default_value).value_or(0.0f) : 0.0f; } float SettingInfo::FloatMinValue() const { static constexpr float fallback_value = std::numeric_limits::min(); return min_value ? StringUtil::FromChars(min_value).value_or(fallback_value) : fallback_value; } float SettingInfo::FloatMaxValue() const { static constexpr float fallback_value = std::numeric_limits::max(); return max_value ? StringUtil::FromChars(max_value).value_or(fallback_value) : fallback_value; } float SettingInfo::FloatStepValue() const { static constexpr float fallback_value = 0.1f; return step_value ? StringUtil::FromChars(step_value).value_or(fallback_value) : fallback_value; } Settings::Settings() = default; bool Settings::HasAnyPerGameMemoryCards() const { return std::any_of(memory_card_types.begin(), memory_card_types.end(), [](MemoryCardType t) { return (t == MemoryCardType::PerGame || t == MemoryCardType::PerGameTitle); }); } std::array Settings::GeneratePortLabels() const { static constexpr std::array, static_cast(MultitapMode::Count)> multitap_enabled_on_port = {{{false, false}, {true, false}, {false, true}, {true, true}}}; std::array labels; u32 logical_port = 0; for (u32 physical_port = 0; physical_port < NUM_MULTITAPS; physical_port++) { if (multitap_enabled_on_port[static_cast(multitap_mode)][physical_port]) { for (u32 i = 0; i < 4; i++) { labels[logical_port] = TinyString::FromFormat("Port %u%c", physical_port + 1u, 'A' + i); logical_port++; } } else { labels[logical_port] = TinyString::FromFormat("Port %u", physical_port + 1u); logical_port++; } } return labels; } void Settings::CPUOverclockPercentToFraction(u32 percent, u32* numerator, u32* denominator) { const u32 percent_gcd = std::gcd(percent, 100); *numerator = percent / percent_gcd; *denominator = 100u / percent_gcd; } u32 Settings::CPUOverclockFractionToPercent(u32 numerator, u32 denominator) { return (numerator * 100u) / denominator; } void Settings::SetCPUOverclockPercent(u32 percent) { CPUOverclockPercentToFraction(percent, &cpu_overclock_numerator, &cpu_overclock_denominator); } u32 Settings::GetCPUOverclockPercent() const { return CPUOverclockFractionToPercent(cpu_overclock_numerator, cpu_overclock_denominator); } void Settings::UpdateOverclockActive() { cpu_overclock_active = (cpu_overclock_enable && (cpu_overclock_numerator != 1 || cpu_overclock_denominator != 1)); } void Settings::Load(SettingsInterface& si) { region = ParseConsoleRegionName( si.GetStringValue("Console", "Region", Settings::GetConsoleRegionName(Settings::DEFAULT_CONSOLE_REGION)).c_str()) .value_or(DEFAULT_CONSOLE_REGION); enable_8mb_ram = si.GetBoolValue("Console", "Enable8MBRAM", false); apply_game_settings = si.GetBoolValue("Main", "ApplyGameSettings", true); runahead_frames = static_cast(si.GetIntValue("Main", "RunaheadFrameCount", 0)); audio_fast_hook = si.GetBoolValue("Audio", "FastHook", true); cpu_execution_mode = ParseCPUExecutionMode( si.GetStringValue("CPU", "ExecutionMode", GetCPUExecutionModeName(DEFAULT_CPU_EXECUTION_MODE)).c_str()) .value_or(DEFAULT_CPU_EXECUTION_MODE); cpu_recompiler_block_linking = si.GetBoolValue("CPU", "RecompilerBlockLinking", true); cpu_recompiler_icache = si.GetBoolValue("CPU", "RecompilerICache", false); cpu_fastmem_mode = ParseCPUFastmemMode( si.GetStringValue("CPU", "FastmemMode", GetCPUFastmemModeName(DEFAULT_CPU_FASTMEM_MODE)).c_str()) .value_or(DEFAULT_CPU_FASTMEM_MODE); cpu_fastmem_rewrite = si.GetBoolValue("CPU", "FastmemRewrite", false); gpu_renderer = ParseRendererName(si.GetStringValue("GPU", "Renderer", GetRendererName(DEFAULT_GPU_RENDERER)).c_str()) .value_or(DEFAULT_GPU_RENDERER); gpu_resolution_scale = static_cast(si.GetIntValue("GPU", "ResolutionScale", 1)); gpu_use_thread = si.GetBoolValue("GPU", "UseThread", true); gpu_use_software_renderer_for_readbacks = si.GetBoolValue("GPU", "UseSoftwareRendererForReadbacks", false); gpu_true_color = si.GetBoolValue("GPU", "TrueColor", false); gpu_scaled_dithering = si.GetBoolValue("GPU", "ScaledDithering", false); gpu_texture_filter = ParseTextureFilterName( si.GetStringValue("GPU", "TextureFilter", GetTextureFilterName(DEFAULT_GPU_TEXTURE_FILTER)).c_str()) .value_or(DEFAULT_GPU_TEXTURE_FILTER); gpu_downsample_mode = ParseDownsampleModeName( si.GetStringValue("GPU", "DownsampleMode", GetDownsampleModeName(DEFAULT_GPU_DOWNSAMPLE_MODE)).c_str()) .value_or(DEFAULT_GPU_DOWNSAMPLE_MODE); gpu_disable_interlacing = si.GetBoolValue("GPU", "DisableInterlacing", true); gpu_force_ntsc_timings = si.GetBoolValue("GPU", "ForceNTSCTimings", false); gpu_widescreen_hack = si.GetBoolValue("GPU", "WidescreenHack", false); gpu_24bit_chroma_smoothing = si.GetBoolValue("GPU", "ChromaSmoothing24Bit", false); gpu_pgxp_enable = si.GetBoolValue("GPU", "PGXPEnable", false); gpu_pgxp_culling = si.GetBoolValue("GPU", "PGXPCulling", true); gpu_pgxp_texture_correction = si.GetBoolValue("GPU", "PGXPTextureCorrection", true); gpu_pgxp_color_correction = si.GetBoolValue("GPU", "PGXPColorCorrection", false); gpu_pgxp_vertex_cache = si.GetBoolValue("GPU", "PGXPVertexCache", false); gpu_pgxp_cpu = si.GetBoolValue("GPU", "PGXPCPU", false); gpu_pgxp_preserve_proj_fp = si.GetBoolValue("GPU", "PGXPPreserveProjFP", false); gpu_pgxp_tolerance = si.GetFloatValue("GPU", "PGXPTolerance", -1.0f); gpu_pgxp_depth_buffer = si.GetBoolValue("GPU", "PGXPDepthBuffer", false); SetPGXPDepthClearThreshold(si.GetFloatValue("GPU", "PGXPDepthClearThreshold", DEFAULT_GPU_PGXP_DEPTH_THRESHOLD)); display_crop_mode = ParseDisplayCropMode( si.GetStringValue("Display", "CropMode", GetDisplayCropModeName(DEFAULT_DISPLAY_CROP_MODE)).c_str()) .value_or(DEFAULT_DISPLAY_CROP_MODE); display_aspect_ratio = ParseDisplayAspectRatio( si.GetStringValue("Display", "AspectRatio", GetDisplayAspectRatioName(DEFAULT_DISPLAY_ASPECT_RATIO)).c_str()) .value_or(DEFAULT_DISPLAY_ASPECT_RATIO); display_aspect_ratio_custom_numerator = static_cast( std::clamp(si.GetIntValue("Display", "CustomAspectRatioNumerator", 4), 1, std::numeric_limits::max())); display_aspect_ratio_custom_denominator = static_cast( std::clamp(si.GetIntValue("Display", "CustomAspectRatioDenominator", 3), 1, std::numeric_limits::max())); display_force_4_3_for_24bit = si.GetBoolValue("Display", "Force4_3For24Bit", false); display_active_start_offset = static_cast(si.GetIntValue("Display", "ActiveStartOffset", 0)); display_active_end_offset = static_cast(si.GetIntValue("Display", "ActiveEndOffset", 0)); display_line_start_offset = static_cast(si.GetIntValue("Display", "LineStartOffset", 0)); display_line_end_offset = static_cast(si.GetIntValue("Display", "LineEndOffset", 0)); display_show_osd_messages = si.GetBoolValue("Display", "ShowOSDMessages", true); cdrom_readahead_sectors = static_cast(si.GetIntValue("CDROM", "ReadaheadSectors", DEFAULT_CDROM_READAHEAD_SECTORS)); cdrom_region_check = si.GetBoolValue("CDROM", "RegionCheck", false); cdrom_load_image_to_ram = si.GetBoolValue("CDROM", "LoadImageToRAM", false); cdrom_precache_chd = si.GetBoolValue("CDROM", "PreCacheCHD", false); cdrom_mute_cd_audio = si.GetBoolValue("CDROM", "MuteCDAudio", false); cdrom_read_speedup = si.GetIntValue("CDROM", "ReadSpeedup", 1); cdrom_seek_speedup = si.GetIntValue("CDROM", "SeekSpeedup", 1); use_old_mdec_routines = si.GetBoolValue("Hacks", "UseOldMDECRoutines", false); bios_patch_fast_boot = si.GetBoolValue("BIOS", "PatchFastBoot", DEFAULT_FAST_BOOT_VALUE); controller_analog_combo = si.GetIntValue("Controller", "AnalogCombo", 1); controller_enable_rumble = si.GetBoolValue("Controller", "EnableRumble", true); controller_show_crosshair = si.GetBoolValue("Controller", "ShowCrosshair", true); memory_card_types[0] = ParseMemoryCardTypeName( si.GetStringValue("MemoryCards", "Card1Type", GetMemoryCardTypeName(DEFAULT_MEMORY_CARD_1_TYPE)).c_str()) .value_or(DEFAULT_MEMORY_CARD_1_TYPE); memory_card_types[1] = ParseMemoryCardTypeName( si.GetStringValue("MemoryCards", "Card2Type", GetMemoryCardTypeName(DEFAULT_MEMORY_CARD_2_TYPE)).c_str()) .value_or(DEFAULT_MEMORY_CARD_2_TYPE); memory_card_use_playlist_title = si.GetBoolValue("MemoryCards", "UsePlaylistTitle", true); multitap_mode = ParseMultitapModeName( si.GetStringValue("ControllerPorts", "MultitapMode", GetMultitapModeName(DEFAULT_MULTITAP_MODE)).c_str()) .value_or(DEFAULT_MULTITAP_MODE); log_level = ParseLogLevelName(si.GetStringValue("Logging", "LogLevel", GetLogLevelName(DEFAULT_LOG_LEVEL)).c_str()) .value_or(DEFAULT_LOG_LEVEL); texture_replacements.enable_vram_write_replacements = si.GetBoolValue("TextureReplacements", "EnableVRAMWriteReplacements", false); texture_replacements.preload_textures = si.GetBoolValue("TextureReplacements", "PreloadTextures", false); } static std::array(LogLevel::Count)> s_log_level_names = { {"None", "Error", "Warning", "Perf", "Info", "Verbose", "Dev", "Profile", "Debug", "Trace"}}; std::optional Settings::ParseLogLevelName(const char* str) { int index = 0; for (const char* name : s_log_level_names) { if (StringUtil::Strcasecmp(name, str) == 0) return static_cast(index); index++; } return std::nullopt; } const char* Settings::GetLogLevelName(LogLevel level) { return s_log_level_names[static_cast(level)]; } static std::array s_console_region_names = {{"Auto", "NTSC-J", "NTSC-U", "PAL"}}; static std::array s_console_region_display_names = { {TRANSLATABLE("ConsoleRegion", "Auto-Detect"), TRANSLATABLE("ConsoleRegion", "NTSC-J (Japan)"), TRANSLATABLE("ConsoleRegion", "NTSC-U/C (US, Canada)"), TRANSLATABLE("ConsoleRegion", "PAL (Europe, Australia)")}}; std::optional Settings::ParseConsoleRegionName(const char* str) { int index = 0; for (const char* name : s_console_region_names) { if (StringUtil::Strcasecmp(name, str) == 0) return static_cast(index); index++; } return std::nullopt; } const char* Settings::GetConsoleRegionName(ConsoleRegion region) { return s_console_region_names[static_cast(region)]; } const char* Settings::GetConsoleRegionDisplayName(ConsoleRegion region) { return s_console_region_display_names[static_cast(region)]; } static std::array s_disc_region_names = {{"NTSC-J", "NTSC-U", "PAL", "Other"}}; static std::array s_disc_region_display_names = { {TRANSLATABLE("DiscRegion", "NTSC-J (Japan)"), TRANSLATABLE("DiscRegion", "NTSC-U/C (US, Canada)"), TRANSLATABLE("DiscRegion", "PAL (Europe, Australia)"), TRANSLATABLE("DiscRegion", "Other")}}; std::optional Settings::ParseDiscRegionName(const char* str) { int index = 0; for (const char* name : s_disc_region_names) { if (StringUtil::Strcasecmp(name, str) == 0) return static_cast(index); index++; } return std::nullopt; } const char* Settings::GetDiscRegionName(DiscRegion region) { return s_disc_region_names[static_cast(region)]; } const char* Settings::GetDiscRegionDisplayName(DiscRegion region) { return s_disc_region_display_names[static_cast(region)]; } static std::array s_cpu_execution_mode_names = {{"Interpreter", "CachedInterpreter", "Recompiler"}}; static std::array s_cpu_execution_mode_display_names = { {TRANSLATABLE("CPUExecutionMode", "Interpreter (Slowest)"), TRANSLATABLE("CPUExecutionMode", "Cached Interpreter (Faster)"), TRANSLATABLE("CPUExecutionMode", "Recompiler (Fastest)")}}; std::optional Settings::ParseCPUExecutionMode(const char* str) { u8 index = 0; for (const char* name : s_cpu_execution_mode_names) { if (StringUtil::Strcasecmp(name, str) == 0) return static_cast(index); index++; } return std::nullopt; } const char* Settings::GetCPUExecutionModeName(CPUExecutionMode mode) { return s_cpu_execution_mode_names[static_cast(mode)]; } const char* Settings::GetCPUExecutionModeDisplayName(CPUExecutionMode mode) { return s_cpu_execution_mode_display_names[static_cast(mode)]; } static std::array(CPUFastmemMode::Count)> s_cpu_fastmem_mode_names = { {"Disabled", "MMap", "LUT"}}; static std::array(CPUFastmemMode::Count)> s_cpu_fastmem_mode_display_names = { {TRANSLATABLE("CPUFastmemMode", "Disabled (Slowest)"), TRANSLATABLE("CPUFastmemMode", "MMap (Hardware, Fastest, 64-Bit Only)"), TRANSLATABLE("CPUFastmemMode", "LUT (Faster)")}}; std::optional Settings::ParseCPUFastmemMode(const char* str) { u8 index = 0; for (const char* name : s_cpu_fastmem_mode_names) { if (StringUtil::Strcasecmp(name, str) == 0) return static_cast(index); index++; } return std::nullopt; } const char* Settings::GetCPUFastmemModeName(CPUFastmemMode mode) { return s_cpu_fastmem_mode_names[static_cast(mode)]; } const char* Settings::GetCPUFastmemModeDisplayName(CPUFastmemMode mode) { return s_cpu_fastmem_mode_display_names[static_cast(mode)]; } static constexpr auto s_gpu_renderer_names = make_array( #ifdef _WIN32 "D3D11", "D3D12", #endif "Vulkan", "OpenGL", "Software"); static constexpr auto s_gpu_renderer_display_names = make_array( #ifdef _WIN32 TRANSLATABLE("GPURenderer", "Hardware (D3D11)"), TRANSLATABLE("GPURenderer", "Hardware (D3D12)"), #endif TRANSLATABLE("GPURenderer", "Hardware (Vulkan)"), TRANSLATABLE("GPURenderer", "Hardware (OpenGL)"), TRANSLATABLE("GPURenderer", "Software")); std::optional Settings::ParseRendererName(const char* str) { int index = 0; for (const char* name : s_gpu_renderer_names) { if (StringUtil::Strcasecmp(name, str) == 0) return static_cast(index); index++; } return std::nullopt; } const char* Settings::GetRendererName(GPURenderer renderer) { return s_gpu_renderer_names[static_cast(renderer)]; } const char* Settings::GetRendererDisplayName(GPURenderer renderer) { return s_gpu_renderer_display_names[static_cast(renderer)]; } static constexpr auto s_texture_filter_names = make_array("Nearest", "Bilinear", "BilinearBinAlpha", "JINC2", "JINC2BinAlpha", "xBR", "xBRBinAlpha"); static constexpr auto s_texture_filter_display_names = make_array(TRANSLATABLE("GPUTextureFilter", "Nearest-Neighbor"), TRANSLATABLE("GPUTextureFilter", "Bilinear"), TRANSLATABLE("GPUTextureFilter", "Bilinear (No Edge Blending)"), TRANSLATABLE("GPUTextureFilter", "JINC2"), TRANSLATABLE("GPUTextureFilter", "JINC2 (No Edge Blending)"), TRANSLATABLE("GPUTextureFilter", "xBR"), TRANSLATABLE("GPUTextureFilter", "xBR (No Edge Blending)")); std::optional Settings::ParseTextureFilterName(const char* str) { int index = 0; for (const char* name : s_texture_filter_names) { if (StringUtil::Strcasecmp(name, str) == 0) return static_cast(index); index++; } return std::nullopt; } const char* Settings::GetTextureFilterName(GPUTextureFilter filter) { return s_texture_filter_names[static_cast(filter)]; } const char* Settings::GetTextureFilterDisplayName(GPUTextureFilter filter) { return s_texture_filter_display_names[static_cast(filter)]; } static constexpr auto s_downsample_mode_names = make_array("Disabled", "Box", "Adaptive"); std::optional Settings::ParseDownsampleModeName(const char* str) { int index = 0; for (const char* name : s_downsample_mode_names) { if (StringUtil::Strcasecmp(name, str) == 0) return static_cast(index); index++; } return std::nullopt; } const char* Settings::GetDownsampleModeName(GPUDownsampleMode mode) { return s_downsample_mode_names[static_cast(mode)]; } static std::array s_display_crop_mode_names = {{"None", "Overscan", "Borders"}}; static std::array s_display_crop_mode_display_names = { {TRANSLATABLE("DisplayCropMode", "None"), TRANSLATABLE("DisplayCropMode", "Only Overscan Area"), TRANSLATABLE("DisplayCropMode", "All Borders")}}; std::optional Settings::ParseDisplayCropMode(const char* str) { int index = 0; for (const char* name : s_display_crop_mode_names) { if (StringUtil::Strcasecmp(name, str) == 0) return static_cast(index); index++; } return std::nullopt; } const char* Settings::GetDisplayCropModeName(DisplayCropMode crop_mode) { return s_display_crop_mode_names[static_cast(crop_mode)]; } const char* Settings::GetDisplayCropModeDisplayName(DisplayCropMode crop_mode) { return s_display_crop_mode_display_names[static_cast(crop_mode)]; } static std::array(DisplayAspectRatio::Count)> s_display_aspect_ratio_names = { {TRANSLATABLE("DisplayAspectRatio", "Auto (Game Native)"), TRANSLATABLE("DisplayAspectRatio", "Auto (Match Window)"), TRANSLATABLE("DisplayAspectRatio", "Custom"), "4:3", "16:9", "19:9", "20:9", "PAR 1:1", "Native"}}; static constexpr std::array(DisplayAspectRatio::Count)> s_display_aspect_ratio_values = { {-1.0f, -1.0f, -1.0f, 4.0f / 3.0f, 16.0f / 9.0f, 19.0f / 9.0f, 20.0f / 9.0f, -1.0f, -1.0f}}; std::optional Settings::ParseDisplayAspectRatio(const char* str) { int index = 0; for (const char* name : s_display_aspect_ratio_names) { if (StringUtil::Strcasecmp(name, str) == 0) return static_cast(index); index++; } return std::nullopt; } const char* Settings::GetDisplayAspectRatioName(DisplayAspectRatio ar) { return s_display_aspect_ratio_names[static_cast(ar)]; } float Settings::GetDisplayAspectRatioValue() const { switch (display_aspect_ratio) { case DisplayAspectRatio::MatchWindow: { const HostDisplay* display = g_host_interface->GetDisplay(); if (!display) return s_display_aspect_ratio_values[static_cast(DEFAULT_DISPLAY_ASPECT_RATIO)]; const u32 width = display->GetWindowWidth(); const u32 height = display->GetWindowHeight(); return static_cast(width) / static_cast(height); } case DisplayAspectRatio::Custom: { return static_cast(display_aspect_ratio_custom_numerator) / static_cast(display_aspect_ratio_custom_denominator); } default: { return s_display_aspect_ratio_values[static_cast(display_aspect_ratio)]; } } } static std::array s_controller_type_names = { {"None", "DigitalController", "AnalogController", "AnalogJoystick", "NamcoGunCon", "PlayStationMouse", "NeGcon"}}; std::optional Settings::ParseControllerTypeName(const char* str) { int index = 0; for (const char* name : s_controller_type_names) { if (StringUtil::Strcasecmp(name, str) == 0) return static_cast(index); index++; } return std::nullopt; } const char* Settings::GetControllerTypeName(ControllerType type) { return s_controller_type_names[static_cast(type)]; } static std::array s_memory_card_type_names = { {"None", "Shared", "PerGame", "PerGameTitle", "PerGameFileTitle", "NonPersistent", "Libretro"}}; std::optional Settings::ParseMemoryCardTypeName(const char* str) { int index = 0; for (const char* name : s_memory_card_type_names) { if (StringUtil::Strcasecmp(name, str) == 0) return static_cast(index); index++; } return std::nullopt; } const char* Settings::GetMemoryCardTypeName(MemoryCardType type) { return s_memory_card_type_names[static_cast(type)]; } static std::array s_multitap_enable_mode_names = {{"Disabled", "Port1Only", "Port2Only", "BothPorts"}}; std::optional Settings::ParseMultitapModeName(const char* str) { u32 index = 0; for (const char* name : s_multitap_enable_mode_names) { if (StringUtil::Strcasecmp(name, str) == 0) return static_cast(index); index++; } return std::nullopt; } const char* Settings::GetMultitapModeName(MultitapMode mode) { return s_multitap_enable_mode_names[static_cast(mode)]; }