diff --git a/src/base_media_base.h b/src/base_media_base.h --- a/src/base_media_base.h +++ b/src/base_media_base.h @@ -188,7 +188,9 @@ public: static Tbase_set *GetAvailableSets(); - static bool SetSet(const std::string &name); + static bool SetSet(const Tbase_set *set); + static bool SetSetByName(const std::string &name); + static bool SetSetByShortname(uint32_t shortname); static void GetSetsList(std::back_insert_iterator &output_iterator); static int GetNumSets(); static int GetIndexOfUsedSet(); @@ -251,6 +253,7 @@ public: /** Values loaded from config file. */ struct Ini { std::string name; + uint32_t shortname; ///< unique key for base set }; static inline Ini ini_data; diff --git a/src/base_media_func.h b/src/base_media_func.h --- a/src/base_media_func.h +++ b/src/base_media_func.h @@ -228,23 +228,56 @@ bool BaseMedia::AddFile(const /** * Set the set to be used. + * @param set the set to use + * @return true if it could be loaded + */ +template +/* static */ bool BaseMedia::SetSet(const Tbase_set *set) +{ + if (set == nullptr) { + if (!BaseMedia::DetermineBestSet()) return false; + } else { + BaseMedia::used_set = set; + } + CheckExternalFiles(); + return true; +} + +/** + * Set the set to be used. * @param name of the set to use * @return true if it could be loaded */ template -/* static */ bool BaseMedia::SetSet(const std::string &name) +/* static */ bool BaseMedia::SetSetByName(const std::string &name) { if (name.empty()) { - if (!BaseMedia::DetermineBestSet()) return false; - CheckExternalFiles(); - return true; + return SetSet(nullptr); } for (const Tbase_set *s = BaseMedia::available_sets; s != nullptr; s = s->next) { if (name == s->name) { - BaseMedia::used_set = s; - CheckExternalFiles(); - return true; + return SetSet(s); + } + } + return false; +} + +/** + * Set the set to be used. + * @param shortname of the set to use + * @return true if it could be loaded + */ +template +/* static */ bool BaseMedia::SetSetByShortname(uint32_t shortname) +{ + if (shortname == 0) { + return SetSet(nullptr); + } + + for (const Tbase_set *s = BaseMedia::available_sets; s != nullptr; s = s->next) { + if (shortname == s->shortname) { + return SetSet(s); } } return false; @@ -376,7 +409,9 @@ template 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); \ - template bool repl_type::SetSet(const std::string &name); \ + template bool repl_type::SetSet(const set_type *set); \ + template bool repl_type::SetSetByName(const std::string &name); \ + template bool repl_type::SetSetByShortname(uint32_t shortname); \ template void repl_type::GetSetsList(std::back_insert_iterator &output_iterator); \ template int repl_type::GetNumSets(); \ template int repl_type::GetIndexOfUsedSet(); \ diff --git a/src/music_gui.cpp b/src/music_gui.cpp --- a/src/music_gui.cpp +++ b/src/music_gui.cpp @@ -173,7 +173,7 @@ void MusicSystem::ChangePlaylist(Playlis */ void MusicSystem::ChangeMusicSet(const std::string &set_name) { - BaseMusic::SetSet(set_name); + BaseMusic::SetSetByName(set_name); BaseMusic::ini_set = set_name; this->BuildPlaylists(); diff --git a/src/openttd.cpp b/src/openttd.cpp --- a/src/openttd.cpp +++ b/src/openttd.cpp @@ -702,10 +702,13 @@ int openttd_main(int argc, char *argv[]) BaseGraphics::FindSets(); bool valid_graphics_set; if (!graphics_set.empty()) { - valid_graphics_set = BaseGraphics::SetSet(graphics_set); + valid_graphics_set = BaseGraphics::SetSetByName(graphics_set); + } else if (BaseGraphics::ini_data.shortname != 0) { + graphics_set = BaseGraphics::ini_data.name; + valid_graphics_set = BaseGraphics::SetSetByShortname(BaseGraphics::ini_data.shortname); } else if (!BaseGraphics::ini_data.name.empty()) { graphics_set = BaseGraphics::ini_data.name; - valid_graphics_set = BaseGraphics::SetSet(BaseGraphics::ini_data.name); + valid_graphics_set = BaseGraphics::SetSetByName(BaseGraphics::ini_data.name); } else { valid_graphics_set = true; BaseGraphics::SetSet(nullptr); // ignore error, continue to bootstrap GUI @@ -769,7 +772,7 @@ int openttd_main(int argc, char *argv[]) BaseSounds::FindSets(); if (sounds_set.empty() && !BaseSounds::ini_set.empty()) sounds_set = BaseSounds::ini_set; - if (!BaseSounds::SetSet(sounds_set)) { + if (!BaseSounds::SetSetByName(sounds_set)) { if (sounds_set.empty() || !BaseSounds::SetSet({})) { UserError("Failed to find a sounds set. Please acquire a sounds set for OpenTTD. See section 1.4 of README.md."); } else { @@ -781,7 +784,7 @@ int openttd_main(int argc, char *argv[]) BaseMusic::FindSets(); if (music_set.empty() && !BaseMusic::ini_set.empty()) music_set = BaseMusic::ini_set; - if (!BaseMusic::SetSet(music_set)) { + if (!BaseMusic::SetSetByName(music_set)) { if (music_set.empty() || !BaseMusic::SetSet({})) { UserError("Failed to find a music set. Please acquire a music set for OpenTTD. See section 1.4 of README.md."); } else { diff --git a/src/settings.cpp b/src/settings.cpp --- a/src/settings.cpp +++ b/src/settings.cpp @@ -995,6 +995,10 @@ static void GraphicsSetLoadConfig(IniFil 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; + + if (const IniItem *item = group->GetItem("shortname"); item != nullptr && item->value && item->value->size() == 8) { + BaseGraphics::ini_data.shortname = BSWAP32(std::strtoul(item->value->c_str(), nullptr, 16)); + } } } @@ -1182,6 +1186,7 @@ static void GraphicsSetSaveConfig(IniFil group.Clear(); group.GetOrCreateItem("name").SetValue(used_set->name); + group.GetOrCreateItem("shortname").SetValue(fmt::format("{:08X}", BSWAP32(used_set->shortname))); } /* Save a GRF configuration to the given group name */ diff --git a/src/settings_gui.cpp b/src/settings_gui.cpp --- a/src/settings_gui.cpp +++ b/src/settings_gui.cpp @@ -693,7 +693,7 @@ struct GameOptionsWindow : Window { case WID_GO_BASE_GRF_DROPDOWN: if (_game_mode == GM_MENU) { auto* set = BaseGraphics::GetSet(index); - BaseGraphics::SetSet(set->name); + BaseGraphics::SetSet(set); this->reload = true; this->InvalidateData(); } @@ -703,7 +703,7 @@ struct GameOptionsWindow : Window { if (_game_mode == GM_MENU) { auto* set = BaseSounds::GetSet(index); BaseSounds::ini_set = set->name; - BaseSounds::SetSet(set->name); + BaseSounds::SetSet(set); this->reload = true; this->InvalidateData(); }