Codechange: use the shortname as unique id to identify the base graphics in openttd.cfg.

pull/615/head
frosch 8 months ago committed by frosch
parent 97df27e41f
commit 0b7ecf6102

@ -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<std::string> &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;

@ -226,25 +226,58 @@ bool BaseMedia<Tbase_set>::AddFile(const std::string &filename, size_t basepath_
return ret;
}
/**
* Set the set to be used.
* @param set the set to use
* @return true if it could be loaded
*/
template <class Tbase_set>
/* static */ bool BaseMedia<Tbase_set>::SetSet(const Tbase_set *set)
{
if (set == nullptr) {
if (!BaseMedia<Tbase_set>::DetermineBestSet()) return false;
} else {
BaseMedia<Tbase_set>::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 <class Tbase_set>
/* static */ bool BaseMedia<Tbase_set>::SetSet(const std::string &name)
/* static */ bool BaseMedia<Tbase_set>::SetSetByName(const std::string &name)
{
if (name.empty()) {
if (!BaseMedia<Tbase_set>::DetermineBestSet()) return false;
CheckExternalFiles();
return true;
return SetSet(nullptr);
}
for (const Tbase_set *s = BaseMedia<Tbase_set>::available_sets; s != nullptr; s = s->next) {
if (name == s->name) {
BaseMedia<Tbase_set>::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 <class Tbase_set>
/* static */ bool BaseMedia<Tbase_set>::SetSetByShortname(uint32_t shortname)
{
if (shortname == 0) {
return SetSet(nullptr);
}
for (const Tbase_set *s = BaseMedia<Tbase_set>::available_sets; s != nullptr; s = s->next) {
if (shortname == s->shortname) {
return SetSet(s);
}
}
return false;
@ -376,7 +409,9 @@ template <class Tbase_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); \
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<std::string> &output_iterator); \
template int repl_type::GetNumSets(); \
template int repl_type::GetIndexOfUsedSet(); \

@ -173,7 +173,7 @@ void MusicSystem::ChangePlaylist(PlaylistChoices pl)
*/
void MusicSystem::ChangeMusicSet(const std::string &set_name)
{
BaseMusic::SetSet(set_name);
BaseMusic::SetSetByName(set_name);
BaseMusic::ini_set = set_name;
this->BuildPlaylists();

@ -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 {

@ -995,6 +995,10 @@ static void GraphicsSetLoadConfig(IniFile &ini)
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(IniFile &ini)
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 */

@ -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();
}

Loading…
Cancel
Save