From 97df27e41f85a9f0d1d422f19ad31b5396a506f8 Mon Sep 17 00:00:00 2001 From: frosch Date: Mon, 2 Oct 2023 13:29:32 +0200 Subject: [PATCH] Codechange: Store base graphics settings in a separate section in openttd.cfg. --- src/base_media_base.h | 16 +++++++++---- src/base_media_func.h | 1 - src/openttd.cpp | 23 +++++++++++------- src/settings.cpp | 36 ++++++++++++++++++++++++++++ src/settings_gui.cpp | 34 ++++++++++---------------- src/table/settings/misc_settings.ini | 7 ------ 6 files changed, 76 insertions(+), 41 deletions(-) diff --git a/src/base_media_base.h b/src/base_media_base.h index 2bd8b58265..79266177a5 100644 --- a/src/base_media_base.h +++ b/src/base_media_base.h @@ -170,9 +170,6 @@ protected: */ static const char *GetExtension(); public: - /** The set as saved in the config file. */ - static std::string ini_set; - /** * Determine the graphics pack that has to be used. * The one with the most correct files wins. @@ -207,7 +204,6 @@ public: static bool HasSet(const ContentInfo *ci, bool md5sum); }; -template /* static */ std::string BaseMedia::ini_set; template /* static */ const Tbase_set *BaseMedia::used_set; template /* static */ Tbase_set *BaseMedia::available_sets; template /* static */ Tbase_set *BaseMedia::duplicate_sets; @@ -252,6 +248,12 @@ struct GraphicsSet : BaseSet { /** All data/functions related with replacing the base graphics. */ class BaseGraphics : public BaseMedia { public: + /** Values loaded from config file. */ + struct Ini { + std::string name; + }; + static inline Ini ini_data; + }; /** All data of a sounds set. */ @@ -261,6 +263,9 @@ struct SoundsSet : BaseSet { /** All data/functions related with replacing the base sounds */ class BaseSounds : public BaseMedia { public: + /** The set as saved in the config file. */ + static inline std::string ini_set; + }; /** Maximum number of songs in the 'class' playlists. */ @@ -307,6 +312,9 @@ struct MusicSet : BaseSet { /** All data/functions related with replacing the base music */ class BaseMusic : public BaseMedia { public: + /** The set as saved in the config file. */ + static inline std::string ini_set; + }; #endif /* BASE_MEDIA_BASE_H */ diff --git a/src/base_media_func.h b/src/base_media_func.h index 56af002cba..0403a65f29 100644 --- a/src/base_media_func.h +++ b/src/base_media_func.h @@ -373,7 +373,6 @@ template * @param set_type the type of the BaseSet to instantiate */ #define INSTANTIATE_BASE_MEDIA_METHODS(repl_type, set_type) \ - template std::string repl_type::ini_set; \ template const char *repl_type::GetExtension(); \ template bool repl_type::AddFile(const std::string &filename, size_t pathlength, const std::string &tar_filename); \ template bool repl_type::HasSet(const struct ContentInfo *ci, bool md5sum); \ diff --git a/src/openttd.cpp b/src/openttd.cpp index b1808316c5..d906e98efc 100644 --- a/src/openttd.cpp +++ b/src/openttd.cpp @@ -700,15 +700,22 @@ int openttd_main(int argc, char *argv[]) InitWindowSystem(); BaseGraphics::FindSets(); - if (graphics_set.empty() && !BaseGraphics::ini_set.empty()) graphics_set = BaseGraphics::ini_set; - if (!BaseGraphics::SetSet(graphics_set)) { - if (!graphics_set.empty()) { - BaseGraphics::SetSet({}); + bool valid_graphics_set; + if (!graphics_set.empty()) { + valid_graphics_set = BaseGraphics::SetSet(graphics_set); + } else if (!BaseGraphics::ini_data.name.empty()) { + graphics_set = BaseGraphics::ini_data.name; + valid_graphics_set = BaseGraphics::SetSet(BaseGraphics::ini_data.name); + } else { + valid_graphics_set = true; + BaseGraphics::SetSet(nullptr); // ignore error, continue to bootstrap GUI + } + if (!valid_graphics_set) { + BaseGraphics::SetSet(nullptr); - ErrorMessageData msg(STR_CONFIG_ERROR, STR_CONFIG_ERROR_INVALID_BASE_GRAPHICS_NOT_FOUND); - msg.SetDParamStr(0, graphics_set); - ScheduleErrorMessage(msg); - } + ErrorMessageData msg(STR_CONFIG_ERROR, STR_CONFIG_ERROR_INVALID_BASE_GRAPHICS_NOT_FOUND); + msg.SetDParamStr(0, graphics_set); + ScheduleErrorMessage(msg); } /* Initialize game palette */ diff --git a/src/settings.cpp b/src/settings.cpp index a9d79b53f4..0dec3a5288 100644 --- a/src/settings.cpp +++ b/src/settings.cpp @@ -42,6 +42,7 @@ #include "ai/ai_config.hpp" #include "game/game_config.hpp" #include "newgrf_config.h" +#include "base_media_base.h" #include "fios.h" #include "fileio_func.h" #include "settings_cmd.h" @@ -981,6 +982,22 @@ static bool DecodeHexText(const char *pos, uint8_t *dest, size_t dest_size) return *pos == '|'; } +/** + * Load BaseGraphics set selection and configuration. + */ +static void GraphicsSetLoadConfig(IniFile &ini) +{ + if (const IniGroup *group = ini.GetGroup("misc"); group != nullptr) { + /* Load old setting first. */ + if (const IniItem *item = group->GetItem("graphicsset"); item != nullptr && item->value) BaseGraphics::ini_data.name = *item->value; + } + + if (const IniGroup *group = ini.GetGroup("graphicsset"); group != nullptr) { + /* Load new settings. */ + if (const IniItem *item = group->GetItem("name"); item != nullptr && item->value) BaseGraphics::ini_data.name = *item->value; + } +} + /** * Load a GRF configuration * @param ini The configuration to read from. @@ -1153,6 +1170,20 @@ static void SaveVersionInConfig(IniFile &ini) group.GetOrCreateItem("ini_version").SetValue(std::to_string(INIFILE_VERSION)); } +/** + * Save BaseGraphics set selection and configuration. + */ +static void GraphicsSetSaveConfig(IniFile &ini) +{ + const GraphicsSet *used_set = BaseGraphics::GetUsedSet(); + if (used_set == nullptr) return; + + IniGroup &group = ini.GetOrCreateGroup("graphicsset"); + group.Clear(); + + group.GetOrCreateItem("name").SetValue(used_set->name); +} + /* Save a GRF configuration to the given group name */ static void GRFSaveConfig(IniFile &ini, const char *grpname, const GRFConfig *list) { @@ -1285,6 +1316,10 @@ void LoadFromConfig(bool startup) IniFileVersion generic_version = LoadVersionFromConfig(generic_ini); + if (startup) { + GraphicsSetLoadConfig(generic_ini); + } + /* Before the split of private/secrets, we have to look in the generic for these settings. */ if (generic_version < IFV_PRIVATE_SECRETS) { HandleSettingDescs(generic_ini, generic_ini, generic_ini, IniLoadSettings, IniLoadSettingList, startup); @@ -1416,6 +1451,7 @@ void SaveToConfig() } HandleSettingDescs(generic_ini, private_ini, secrets_ini, IniSaveSettings, IniSaveSettingList); + GraphicsSetSaveConfig(generic_ini); GRFSaveConfig(generic_ini, "newgrf", _grfconfig_newgame); GRFSaveConfig(generic_ini, "newgrf-static", _grfconfig_static); AISaveConfig(generic_ini, "ai_players"); diff --git a/src/settings_gui.cpp b/src/settings_gui.cpp index 847d75ac47..f79d9eeace 100644 --- a/src/settings_gui.cpp +++ b/src/settings_gui.cpp @@ -649,25 +649,6 @@ struct GameOptionsWindow : Window { } } - /** - * Set the base media set. - * @param index the index of the media set - * @tparam T class of media set - */ - template - void SetMediaSet(int index) - { - if (_game_mode == GM_MENU) { - auto name = T::GetSet(index)->name; - - T::ini_set = name; - - T::SetSet(name); - this->reload = true; - this->InvalidateData(); - } - } - void OnDropdownSelect(int widget, int index) override { switch (widget) { @@ -710,11 +691,22 @@ struct GameOptionsWindow : Window { } case WID_GO_BASE_GRF_DROPDOWN: - this->SetMediaSet(index); + if (_game_mode == GM_MENU) { + auto* set = BaseGraphics::GetSet(index); + BaseGraphics::SetSet(set->name); + this->reload = true; + this->InvalidateData(); + } break; case WID_GO_BASE_SFX_DROPDOWN: - this->SetMediaSet(index); + if (_game_mode == GM_MENU) { + auto* set = BaseSounds::GetSet(index); + BaseSounds::ini_set = set->name; + BaseSounds::SetSet(set->name); + this->reload = true; + this->InvalidateData(); + } break; case WID_GO_BASE_MUSIC_DROPDOWN: diff --git a/src/table/settings/misc_settings.ini b/src/table/settings/misc_settings.ini index dd3a58e24e..7e9db80c83 100644 --- a/src/table/settings/misc_settings.ini +++ b/src/table/settings/misc_settings.ini @@ -94,13 +94,6 @@ max = 2 full = _support8bppmodes cat = SC_BASIC -[SDTG_SSTR] -name = ""graphicsset"" -type = SLE_STRQ -var = BaseGraphics::ini_set -def = nullptr -cat = SC_BASIC - [SDTG_SSTR] name = ""soundsset"" type = SLE_STRQ