Migrate remaining C strings in config handling

This commit is contained in:
Gymnasiast 2023-01-07 15:27:29 +01:00
parent 18d9e72173
commit b6d422ff95
No known key found for this signature in database
GPG Key ID: DBFFF47AB2CA3EDD
11 changed files with 48 additions and 80 deletions

View File

@ -617,9 +617,9 @@ namespace ThemeManager
ActiveAvailableThemeIndex = 1;
bool configValid = false;
if (!String::IsNullOrEmpty(gConfigInterface.CurrentThemePreset))
if (!gConfigInterface.CurrentThemePreset.empty())
{
if (LoadThemeByConfigName(gConfigInterface.CurrentThemePreset))
if (LoadThemeByConfigName(gConfigInterface.CurrentThemePreset.c_str()))
{
configValid = true;
}
@ -627,7 +627,7 @@ namespace ThemeManager
if (!configValid)
{
String::DiscardDuplicate(&gConfigInterface.CurrentThemePreset, ThemeManagerGetAvailableThemeConfigName(1));
gConfigInterface.CurrentThemePreset = ThemeManagerGetAvailableThemeConfigName(1);
}
}
@ -709,7 +709,7 @@ void ThemeManagerSetActiveAvailableTheme(size_t index)
}
}
ThemeManager::ActiveAvailableThemeIndex = index;
String::DiscardDuplicate(&gConfigInterface.CurrentThemePreset, ThemeManagerGetAvailableThemeConfigName(index));
gConfigInterface.CurrentThemePreset = ThemeManagerGetAvailableThemeConfigName(index);
ColourSchemeUpdateAll();
}
@ -805,7 +805,7 @@ void ThemeRename(const utf8* name)
if (Path::Equals(newPath, ThemeManager::AvailableThemes[i].Path))
{
ThemeManager::ActiveAvailableThemeIndex = i;
String::DiscardDuplicate(&gConfigInterface.CurrentThemePreset, ThemeManagerGetAvailableThemeConfigName(1));
gConfigInterface.CurrentThemePreset = ThemeManagerGetAvailableThemeConfigName(1);
break;
}
}
@ -830,7 +830,7 @@ void ThemeDuplicate(const utf8* name)
if (Path::Equals(newPath, ThemeManager::AvailableThemes[i].Path))
{
ThemeManager::ActiveAvailableThemeIndex = i;
String::DiscardDuplicate(&gConfigInterface.CurrentThemePreset, ThemeManagerGetAvailableThemeConfigName(i));
gConfigInterface.CurrentThemePreset = ThemeManagerGetAvailableThemeConfigName(i);
break;
}
}
@ -841,7 +841,7 @@ void ThemeDelete()
File::Delete(ThemeManager::CurrentThemePath);
ThemeManager::LoadTheme(const_cast<UITheme*>(&PredefinedThemeRCT2));
ThemeManager::ActiveAvailableThemeIndex = 1;
String::DiscardDuplicate(&gConfigInterface.CurrentThemePreset, ThemeManagerGetAvailableThemeConfigName(1));
gConfigInterface.CurrentThemePreset = ThemeManagerGetAvailableThemeConfigName(1);
}
void ThemeManagerInitialise()

View File

@ -164,9 +164,7 @@ public:
CurrencyDescriptors[EnumValue(CurrencyType::Custom)].symbol_unicode, std::string(text).c_str(),
CURRENCY_SYMBOL_MAX_SIZE);
safe_strcpy(
gConfigGeneral.CustomCurrencySymbol, CurrencyDescriptors[EnumValue(CurrencyType::Custom)].symbol_unicode,
CURRENCY_SYMBOL_MAX_SIZE);
gConfigGeneral.CustomCurrencySymbol = CurrencyDescriptors[EnumValue(CurrencyType::Custom)].symbol_unicode;
ConfigSaveDefault();
window_invalidate_all();

View File

@ -199,7 +199,6 @@ namespace OpenRCT2
gfx_unload_g2();
gfx_unload_g1();
Audio::Close();
ConfigRelease();
Instance = nullptr;
}

View File

@ -147,7 +147,7 @@ namespace Config
model->CustomCurrencyRate = reader->GetInt32("custom_currency_rate", 10);
model->CustomCurrencyAffix = reader->GetEnum<CurrencyAffix>(
"custom_currency_affix", CurrencyAffix::Suffix, Enum_CurrencySymbolAffix);
model->CustomCurrencySymbol = reader->GetCString("custom_currency_symbol", "Ctm");
model->CustomCurrencySymbol = reader->GetString("custom_currency_symbol", "Ctm");
model->EdgeScrolling = reader->GetBoolean("edge_scrolling", true);
model->EdgeScrollingSpeed = reader->GetInt32("edge_scrolling_speed", 12);
model->FullscreenMode = reader->GetInt32("fullscreen_mode", 0);
@ -325,8 +325,8 @@ namespace Config
model->ToolbarShowChat = reader->GetBoolean("toolbar_show_chat", false);
model->ToolbarShowZoom = reader->GetBoolean("toolbar_show_zoom", true);
model->ConsoleSmallFont = reader->GetBoolean("console_small_font", false);
model->CurrentThemePreset = reader->GetCString("current_theme", "*RCT2");
model->CurrentTitleSequencePreset = reader->GetCString("current_title_sequence", "*OPENRCT2");
model->CurrentThemePreset = reader->GetString("current_theme", "*RCT2");
model->CurrentTitleSequencePreset = reader->GetString("current_title_sequence", "*OPENRCT2");
model->RandomTitleSequence = reader->GetBoolean("random_title_sequence", false);
model->ObjectSelectionFilterFlags = reader->GetInt32("object_selection_filter_flags", 0x3FFF);
model->ScenarioselectLastTab = reader->GetInt32("scenarioselect_last_tab", 0);
@ -511,8 +511,8 @@ namespace Config
if (reader->ReadSection("font"))
{
auto model = &gConfigFonts;
model->FileName = reader->GetCString("file_name", nullptr);
model->FontName = reader->GetCString("font_name", nullptr);
model->FileName = reader->GetString("file_name", "");
model->FontName = reader->GetString("font_name", "");
model->OffsetX = reader->GetInt32("x_offset", false);
model->OffsetY = reader->GetInt32("y_offset", true);
model->SizeTiny = reader->GetInt32("size_tiny", true);
@ -775,7 +775,6 @@ PluginConfiguration gConfigPlugin;
void ConfigSetDefaults()
{
ConfigRelease();
Config::SetDefaults();
}
@ -786,7 +785,6 @@ bool ConfigOpen(u8string_view path)
return false;
}
ConfigRelease();
auto result = Config::ReadFile(path);
if (result)
{
@ -800,15 +798,6 @@ bool ConfigSave(u8string_view path)
return Config::WriteFile(path);
}
void ConfigRelease()
{
SafeFree(gConfigGeneral.CustomCurrencySymbol);
SafeFree(gConfigInterface.CurrentThemePreset);
SafeFree(gConfigInterface.CurrentTitleSequencePreset);
SafeFree(gConfigFonts.FileName);
SafeFree(gConfigFonts.FontName);
}
u8string ConfigGetDefaultPath()
{
auto env = GetContext()->GetPlatformEnvironment();

View File

@ -77,7 +77,7 @@ struct GeneralConfiguration
CurrencyType CurrencyFormat;
int32_t CustomCurrencyRate;
CurrencyAffix CustomCurrencyAffix;
utf8* CustomCurrencySymbol;
u8string CustomCurrencySymbol;
// Controls
bool EdgeScrolling;
@ -130,8 +130,8 @@ struct InterfaceConfiguration
bool ToolbarShowZoom;
bool ConsoleSmallFont;
bool RandomTitleSequence;
utf8* CurrentThemePreset;
utf8* CurrentTitleSequencePreset;
u8string CurrentThemePreset;
u8string CurrentTitleSequencePreset;
int32_t ObjectSelectionFilterFlags;
int32_t ScenarioselectLastTab;
bool ListRideVehiclesSeparately;
@ -139,7 +139,7 @@ struct InterfaceConfiguration
struct SoundConfiguration
{
std::string Device;
u8string Device;
bool MasterSoundEnabled;
uint8_t MasterVolume;
TitleMusicKind TitleMusic;
@ -152,21 +152,21 @@ struct SoundConfiguration
struct NetworkConfiguration
{
std::string PlayerName;
u8string PlayerName;
int32_t DefaultPort;
std::string ListenAddress;
std::string DefaultPassword;
u8string ListenAddress;
u8string DefaultPassword;
bool StayConnected;
bool Advertise;
std::string AdvertiseAddress;
u8string AdvertiseAddress;
int32_t Maxplayers;
std::string ServerName;
std::string ServerDescription;
std::string ServerGreeting;
std::string MasterServerUrl;
std::string ProviderName;
std::string ProviderEmail;
std::string ProviderWebsite;
u8string ServerName;
u8string ServerDescription;
u8string ServerGreeting;
u8string MasterServerUrl;
u8string ProviderName;
u8string ProviderEmail;
u8string ProviderWebsite;
bool KnownKeysOnly;
bool LogChat;
bool LogServerActions;
@ -198,8 +198,8 @@ struct NotificationConfiguration
struct FontConfiguration
{
utf8* FileName;
utf8* FontName;
u8string FileName;
u8string FontName;
int32_t OffsetX;
int32_t OffsetY;
int32_t SizeTiny;
@ -217,7 +217,7 @@ struct FontConfiguration
struct PluginConfiguration
{
bool EnableHotReloading;
std::string AllowedHosts;
u8string AllowedHosts;
};
enum class Sort : int32_t
@ -268,7 +268,6 @@ bool ConfigOpen(u8string_view path);
bool ConfigSave(u8string_view path);
u8string ConfigGetDefaultPath();
void ConfigSetDefaults();
void ConfigRelease();
bool ConfigSaveDefault();
bool ConfigFindOrBrowseInstallDirectory();

View File

@ -427,17 +427,6 @@ public:
}
};
utf8* IIniReader::GetCString(const std::string& name, const utf8* defaultValue) const
{
std::string szValue;
if (!TryGetString(name, &szValue))
{
return String::Duplicate(defaultValue);
}
return String::Duplicate(szValue.c_str());
}
std::unique_ptr<IIniReader> CreateIniReader(OpenRCT2::IStream* stream)
{
return std::make_unique<IniReader>(stream);

View File

@ -45,8 +45,6 @@ struct IIniReader
return configEnum.GetValue(szValue, defaultValue);
}
utf8* GetCString(const std::string& name, const utf8* defaultValue) const;
};
[[nodiscard]] std::unique_ptr<IIniReader> CreateIniReader(OpenRCT2::IStream* stream);

View File

@ -128,12 +128,12 @@ static bool LoadFont(LocalisationService& localisationService, TTFFontSetDescrip
static bool LoadCustomConfigFont(LocalisationService& localisationService)
{
static TTFFontSetDescriptor TTFFontCustom = { {
{ gConfigFonts.FileName, gConfigFonts.FontName, gConfigFonts.SizeTiny, gConfigFonts.OffsetX, gConfigFonts.OffsetY,
gConfigFonts.HeightTiny, gConfigFonts.HintingThreshold, nullptr },
{ gConfigFonts.FileName, gConfigFonts.FontName, gConfigFonts.SizeSmall, gConfigFonts.OffsetX, gConfigFonts.OffsetY,
gConfigFonts.HeightSmall, gConfigFonts.HintingThreshold, nullptr },
{ gConfigFonts.FileName, gConfigFonts.FontName, gConfigFonts.SizeMedium, gConfigFonts.OffsetX, gConfigFonts.OffsetY,
gConfigFonts.HeightMedium, gConfigFonts.HintingThreshold, nullptr },
{ gConfigFonts.FileName.c_str(), gConfigFonts.FontName.c_str(), gConfigFonts.SizeTiny, gConfigFonts.OffsetX,
gConfigFonts.OffsetY, gConfigFonts.HeightTiny, gConfigFonts.HintingThreshold, nullptr },
{ gConfigFonts.FileName.c_str(), gConfigFonts.FontName.c_str(), gConfigFonts.SizeSmall, gConfigFonts.OffsetX,
gConfigFonts.OffsetY, gConfigFonts.HeightSmall, gConfigFonts.HintingThreshold, nullptr },
{ gConfigFonts.FileName.c_str(), gConfigFonts.FontName.c_str(), gConfigFonts.SizeMedium, gConfigFonts.OffsetX,
gConfigFonts.OffsetY, gConfigFonts.HeightMedium, gConfigFonts.HintingThreshold, nullptr },
} };
ttf_dispose();
@ -153,7 +153,7 @@ void TryLoadFonts(LocalisationService& localisationService)
if (fontFamily != FAMILY_OPENRCT2_SPRITE)
{
if (!String::IsNullOrEmpty(gConfigFonts.FileName))
if (!gConfigFonts.FileName.empty())
{
if (LoadCustomConfigFont(localisationService))
{

View File

@ -40,10 +40,10 @@ void currency_load_custom_currency_config()
{
CurrencyDescriptors[EnumValue(CurrencyType::Custom)].rate = gConfigGeneral.CustomCurrencyRate;
CurrencyDescriptors[EnumValue(CurrencyType::Custom)].affix_unicode = gConfigGeneral.CustomCurrencyAffix;
if (gConfigGeneral.CustomCurrencySymbol != nullptr)
if (!gConfigGeneral.CustomCurrencySymbol.empty())
{
safe_strcpy(
CurrencyDescriptors[EnumValue(CurrencyType::Custom)].symbol_unicode, gConfigGeneral.CustomCurrencySymbol,
CurrencyDescriptors[EnumValue(CurrencyType::Custom)].symbol_unicode, gConfigGeneral.CustomCurrencySymbol.c_str(),
CURRENCY_SYMBOL_MAX_SIZE);
}
}

View File

@ -201,8 +201,7 @@ void TitleScreen::ChangePresetSequence(size_t preset)
}
const utf8* configId = title_sequence_manager_get_config_id(preset);
SafeFree(gConfigInterface.CurrentTitleSequencePreset);
gConfigInterface.CurrentTitleSequencePreset = _strdup(configId);
gConfigInterface.CurrentTitleSequencePreset = configId;
if (!_previewingSequence)
_currentSequence = preset;
@ -319,8 +318,7 @@ bool TitleScreen::TryLoadSequence(bool loadPreview)
{
// Forcefully change the preset to a preset that works.
const utf8* configId = title_sequence_manager_get_config_id(targetSequence);
SafeFree(gConfigInterface.CurrentTitleSequencePreset);
gConfigInterface.CurrentTitleSequencePreset = _strdup(configId);
gConfigInterface.CurrentTitleSequencePreset = configId;
}
_currentSequence = targetSequence;
gfx_invalidate_screen();
@ -397,7 +395,7 @@ void title_set_hide_version_info(bool value)
size_t title_get_config_sequence()
{
return title_sequence_manager_get_index_for_config_id(gConfigInterface.CurrentTitleSequencePreset);
return title_sequence_manager_get_index_for_config_id(gConfigInterface.CurrentTitleSequencePreset.c_str());
}
size_t title_get_current_sequence()

View File

@ -35,7 +35,7 @@ TEST_F(IniReaderTest, create_empty)
auto ir = CreateIniReader(&ms);
ASSERT_NE(ir, nullptr);
ASSERT_EQ(ir->GetBoolean("nobody", true), true);
ASSERT_EQ(ir->GetCString("expects", nullptr), nullptr);
ASSERT_EQ(ir->GetString("expects", ""), "");
ASSERT_EQ(ir->GetEnum<int32_t>("spanish", 12345, Enum_Currency), 12345);
ASSERT_EQ(ir->GetFloat("inquisition", 1.234f), 1.234f);
ASSERT_EQ(ir->GetInt32("universal_answer", 42), 42);
@ -64,9 +64,8 @@ TEST_F(IniReaderTest, read_prepared)
// values from different sections
ASSERT_EQ(ir->GetInt32("one", 42), 42);
ASSERT_EQ(ir->GetBoolean("boolval", false), true);
const utf8* str = ir->GetCString("path", nullptr);
ASSERT_STREQ(str, u8"C:'\\some/dir\\here/神鷹暢遊");
Memory::Free(str);
const auto& str = ir->GetString("path", "");
ASSERT_STREQ(str.c_str(), u8"C:'\\some/dir\\here/神鷹暢遊");
// go back a section
ASSERT_EQ(ir->ReadSection("int"), true);
ASSERT_EQ(ir->GetInt32("one", 42), 1);
@ -107,9 +106,8 @@ TEST_F(IniReaderTest, read_untrimmed)
// there should only be data from the last section
ASSERT_EQ(ir->ReadSection("section"), true);
ASSERT_EQ(ir->GetBoolean("one", false), true);
const utf8* str = ir->GetCString("str", nullptr);
ASSERT_STREQ(str, " xxx ");
Memory::Free(str);
const auto& str = ir->GetString("str", "");
ASSERT_STREQ(str.c_str(), " xxx ");
ASSERT_EQ(ir->GetString("str", "yyy"), " xxx ");
ASSERT_EQ(ir->GetString("nosuchthing", " yyy "), " yyy ");
}