GameSettings: Add string key accessors

This commit is contained in:
Connor McLaughlin 2021-01-03 17:26:51 +10:00
parent 2501547acb
commit 6ff73f35bd
2 changed files with 399 additions and 0 deletions

View File

@ -411,6 +411,391 @@ static void StoreIniSection(const Entry& entry, const char* section, CSimpleIniA
ini.SetValue(section, "InputProfileName", entry.input_profile_name.c_str()); ini.SetValue(section, "InputProfileName", entry.input_profile_name.c_str());
} }
static std::optional<std::string> GetEntryValueForKey(const Entry& entry, const std::string_view& key)
{
if (key == "CPUOverclock")
{
if (!entry.cpu_overclock_enable.has_value())
return std::nullopt;
return std::to_string(Settings::CPUOverclockFractionToPercent(entry.cpu_overclock_numerator.value_or(1),
entry.cpu_overclock_denominator.value_or(1)));
}
else if (key == "CDROMReadSpeedup")
{
if (!entry.cdrom_read_speedup.has_value())
return std::nullopt;
else
return std::to_string(entry.cdrom_read_speedup.value());
}
else if (key == "DisplayCropMode")
{
if (!entry.display_crop_mode.has_value())
return std::nullopt;
else
return Settings::GetDisplayCropModeName(entry.display_crop_mode.value());
}
else if (key == "DisplayAspectRatio")
{
if (!entry.display_aspect_ratio.has_value())
return std::nullopt;
else
return Settings::GetDisplayAspectRatioName(entry.display_aspect_ratio.value());
}
else if (key == "GPUDownsampleMode")
{
if (!entry.gpu_downsample_mode.has_value())
return std::nullopt;
else
return Settings::GetDownsampleModeName(entry.gpu_downsample_mode.value());
}
else if (key == "DisplayLinearUpscaling")
{
if (!entry.display_linear_upscaling.has_value())
return std::nullopt;
else
return entry.display_linear_upscaling ? "true" : "false";
}
else if (key == "DisplayIntegerUpscaling")
{
if (!entry.display_integer_upscaling.has_value())
return std::nullopt;
else
return entry.display_integer_upscaling ? "true" : "false";
}
else if (key == "DisplayForce4_3For24Bit")
{
if (!entry.display_force_4_3_for_24bit.has_value())
return std::nullopt;
else
return entry.display_force_4_3_for_24bit ? "true" : "false";
}
else if (key == "GPUResolutionScale")
{
if (!entry.gpu_resolution_scale.has_value())
return std::nullopt;
else
return std::to_string(entry.gpu_resolution_scale.value());
}
else if (key == "GPUMSAA")
{
if (!entry.gpu_multisamples.has_value())
{
return std::nullopt;
}
else
{
return StringUtil::StdStringFromFormat("%u%s", entry.gpu_multisamples.value(),
entry.gpu_multisamples.value_or(false) ? "-ssaa" : "");
}
}
else if (key == "GPUTrueColor")
{
if (!entry.gpu_true_color.has_value())
return std::nullopt;
else
return entry.gpu_true_color ? "true" : "false";
}
else if (key == "GPUScaledDithering")
{
if (!entry.gpu_scaled_dithering.has_value())
return std::nullopt;
else
return entry.gpu_scaled_dithering ? "true" : "false";
}
else if (key == "GPUTextureFilter")
{
if (!entry.gpu_texture_filter.has_value())
return std::nullopt;
else
return entry.gpu_texture_filter ? "true" : "false";
}
else if (key == "GPUForceNTSCTimings")
{
if (!entry.gpu_force_ntsc_timings.has_value())
return std::nullopt;
else
return entry.gpu_force_ntsc_timings ? "true" : "false";
}
else if (key == "GPUWidescreenHack")
{
if (!entry.gpu_widescreen_hack.has_value())
return std::nullopt;
else
return entry.gpu_widescreen_hack ? "true" : "false";
}
else if (key == "GPUPGXP")
{
if (!entry.gpu_pgxp.has_value())
return std::nullopt;
else
return entry.gpu_pgxp ? "true" : "false";
}
else if (key == "GPUPGXPDepthBuffer")
{
if (!entry.gpu_pgxp_depth_buffer.has_value())
return std::nullopt;
else
return entry.gpu_pgxp_depth_buffer ? "true" : "false";
}
else if (key == "Controller1Type")
{
if (!entry.controller_1_type.has_value())
return std::nullopt;
else
return Settings::GetControllerTypeName(entry.controller_1_type.value());
}
else if (key == "Controller2Type")
{
if (!entry.controller_2_type.has_value())
return std::nullopt;
else
return Settings::GetControllerTypeName(entry.controller_2_type.value());
}
else if (key == "MemoryCard1Type")
{
if (!entry.memory_card_1_type.has_value())
return std::nullopt;
else
return Settings::GetMemoryCardTypeName(entry.memory_card_1_type.value());
}
else if (key == "MemoryCard2Type")
{
if (!entry.memory_card_2_type.has_value())
return std::nullopt;
else
return Settings::GetMemoryCardTypeName(entry.memory_card_2_type.value());
}
else if (key == "MemoryCard1SharedPath")
{
if (entry.memory_card_1_shared_path.empty())
return std::nullopt;
else
return entry.memory_card_1_shared_path;
}
else if (key == "MemoryCard2SharedPath")
{
if (entry.memory_card_2_shared_path.empty())
return std::nullopt;
else
return entry.memory_card_2_shared_path;
}
else if (key == "InputProfileName")
{
if (entry.input_profile_name.empty())
return std::nullopt;
else
return entry.input_profile_name;
}
else
{
Log_ErrorPrintf("Unknown key: %s", std::string(key).c_str());
return std::nullopt;
}
}
static void SetEntryValueForKey(Entry& entry, const std::string_view& key, const std::optional<std::string>& value)
{
if (key == "CPUOverclock")
{
if (!value.has_value())
{
entry.cpu_overclock_numerator.reset();
entry.cpu_overclock_denominator.reset();
entry.cpu_overclock_enable.reset();
}
else
{
u32 numerator, denominator;
Settings::CPUOverclockPercentToFraction(StringUtil::FromChars<u32>(value.value()).value_or(100), &numerator,
&denominator);
entry.cpu_overclock_numerator = numerator;
entry.cpu_overclock_denominator = denominator;
entry.cpu_overclock_enable = true;
}
}
else if (key == "CDROMReadSpeedup")
{
if (!value.has_value())
entry.cdrom_read_speedup.reset();
else
entry.cdrom_read_speedup = StringUtil::FromChars<u32>(value.value());
}
else if (key == "DisplayCropMode")
{
if (!value.has_value())
entry.display_crop_mode.reset();
else
entry.display_crop_mode = Settings::ParseDisplayCropMode(value->c_str());
}
else if (key == "DisplayAspectRatio")
{
if (!value.has_value())
entry.display_aspect_ratio.reset();
else
entry.display_aspect_ratio = Settings::ParseDisplayAspectRatio(value->c_str());
}
else if (key == "GPUDownsampleMode")
{
if (!value.has_value())
entry.gpu_downsample_mode.reset();
else
entry.gpu_downsample_mode = Settings::ParseDownsampleModeName(value->c_str());
}
else if (key == "DisplayLinearUpscaling")
{
if (!value.has_value())
entry.display_linear_upscaling.reset();
else
entry.display_linear_upscaling = StringUtil::FromChars<bool>(value.value());
}
else if (key == "DisplayIntegerUpscaling")
{
if (!value.has_value())
entry.display_integer_upscaling.reset();
else
entry.display_integer_upscaling = StringUtil::FromChars<bool>(value.value());
}
else if (key == "DisplayForce4_3For24Bit")
{
if (!value.has_value())
entry.display_force_4_3_for_24bit.reset();
else
entry.display_force_4_3_for_24bit = StringUtil::FromChars<bool>(value.value());
}
else if (key == "GPUResolutionScale")
{
if (!value.has_value())
entry.gpu_resolution_scale.reset();
else
entry.gpu_resolution_scale = StringUtil::FromChars<u32>(value.value());
}
else if (key == "GPUMSAA")
{
if (!value.has_value())
{
entry.gpu_multisamples.reset();
entry.gpu_per_sample_shading.reset();
}
else
{
if (StringUtil::EndsWith(value.value(), "-ssaa"))
{
entry.gpu_multisamples =
StringUtil::FromChars<u32>(std::string_view(value.value()).substr(0, value->length() - 5));
entry.gpu_per_sample_shading = true;
}
else
{
entry.gpu_multisamples = StringUtil::FromChars<u32>(value.value());
entry.gpu_per_sample_shading = false;
}
}
}
else if (key == "GPUTrueColor")
{
if (!value.has_value())
entry.gpu_true_color.reset();
else
entry.gpu_true_color = StringUtil::FromChars<bool>(value.value());
}
else if (key == "GPUScaledDithering")
{
if (!value.has_value())
entry.gpu_scaled_dithering.reset();
else
entry.gpu_scaled_dithering = StringUtil::FromChars<bool>(value.value());
}
else if (key == "GPUTextureFilter")
{
if (!value.has_value())
entry.gpu_texture_filter.reset();
else
entry.gpu_texture_filter = Settings::ParseTextureFilterName(value->c_str());
}
else if (key == "GPUForceNTSCTimings")
{
if (!value.has_value())
entry.gpu_force_ntsc_timings.reset();
else
entry.gpu_force_ntsc_timings = StringUtil::FromChars<bool>(value.value());
}
else if (key == "GPUWidescreenHack")
{
if (!value.has_value())
entry.gpu_widescreen_hack.reset();
else
entry.gpu_widescreen_hack = StringUtil::FromChars<bool>(value.value());
}
else if (key == "GPUPGXP")
{
if (!value.has_value())
entry.gpu_pgxp.reset();
else
entry.gpu_pgxp = StringUtil::FromChars<bool>(value.value());
}
else if (key == "GPUPGXPDepthBuffer")
{
if (!value.has_value())
entry.gpu_pgxp_depth_buffer.reset();
else
entry.gpu_pgxp_depth_buffer = StringUtil::FromChars<bool>(value.value());
}
else if (key == "Controller1Type")
{
if (!value.has_value())
entry.controller_1_type.reset();
else
entry.controller_1_type = Settings::ParseControllerTypeName(value->c_str());
}
else if (key == "Controller2Type")
{
if (!value.has_value())
entry.controller_2_type.reset();
else
entry.controller_2_type = Settings::ParseControllerTypeName(value->c_str());
}
else if (key == "MemoryCard1Type")
{
if (!value.has_value())
entry.memory_card_1_type.reset();
else
entry.memory_card_1_type = Settings::ParseMemoryCardTypeName(value->c_str());
}
else if (key == "MemoryCard2Type")
{
if (!value.has_value())
entry.memory_card_2_type.reset();
else
entry.memory_card_2_type = Settings::ParseMemoryCardTypeName(value->c_str());
}
else if (key == "MemoryCard1SharedPath")
{
if (!value.has_value())
entry.memory_card_1_shared_path.clear();
else
entry.memory_card_1_shared_path = value.value();
}
else if (key == "MemoryCard2SharedPath")
{
if (!value.has_value())
entry.memory_card_1_shared_path.clear();
else
entry.memory_card_1_shared_path = value.value();
}
else if (key == "InputProfileName")
{
if (!value.has_value())
entry.input_profile_name.clear();
else
entry.input_profile_name = value.value();
}
else
{
Log_ErrorPrintf("Unknown key: %s", std::string(key).c_str());
}
}
Database::Database() = default; Database::Database() = default;
Database::~Database() = default; Database::~Database() = default;
@ -504,6 +889,16 @@ void Database::SetEntry(const std::string& code, const std::string& name, const
#endif #endif
std::optional<std::string> Entry::GetValueForKey(const std::string_view& key) const
{
return GetEntryValueForKey(*this, key);
}
void Entry::SetValueForKey(const std::string_view& key, const std::optional<std::string>& value)
{
SetEntryValueForKey(*this, key, value);
}
void Entry::ApplySettings(bool display_osd_messages) const void Entry::ApplySettings(bool display_osd_messages) const
{ {
constexpr float osd_duration = 10.0f; constexpr float osd_duration = 10.0f;

View File

@ -87,6 +87,10 @@ struct Entry
bool SaveToStream(ByteStream* stream) const; bool SaveToStream(ByteStream* stream) const;
void ApplySettings(bool display_osd_messages) const; void ApplySettings(bool display_osd_messages) const;
// Key-based interface, used by Android.
std::optional<std::string> GetValueForKey(const std::string_view& key) const;
void SetValueForKey(const std::string_view& key, const std::optional<std::string>& value);
}; };
#ifndef LIBRETRO #ifndef LIBRETRO