Fix duplicate mod ids breaking mod selection

Fixes #6780
This commit is contained in:
pentarctagon 2024-02-11 10:39:40 -06:00 committed by Pentarctagon
parent 9146db7d5d
commit bcec952e1e
8 changed files with 18 additions and 17 deletions

View file

@ -548,12 +548,12 @@ void create_engine::set_current_era_index(const std::size_t index, bool force)
dependency_manager_->try_era_by_index(index, force);
}
bool create_engine::toggle_mod(int index, bool force)
bool create_engine::toggle_mod(const std::string& id, bool force)
{
force |= state_.classification().type != campaign_type::type::multiplayer;
bool is_active = dependency_manager_->is_modification_active(index);
dependency_manager_->try_modification_by_index(index, !is_active, force);
bool is_active = dependency_manager_->is_modification_active(id);
dependency_manager_->try_modification_by_id(id, !is_active, force);
state_.classification().active_mods = dependency_manager_->get_modifications();
@ -791,7 +791,6 @@ void create_engine::init_extras(const MP_EXTRA extra_type)
extras.push_back(std::move(new_extras_metadata));
}
else {
//TODO: use a more visible error message.
ERR_CF << "found " << extra_name << " with id=" << extra["id"] << " twice";
}
}

View file

@ -367,7 +367,7 @@ public:
const std::vector<extras_metadata_ptr>& get_const_extras_by_type(const MP_EXTRA extra_type) const;
std::vector<extras_metadata_ptr>& get_extras_by_type(const MP_EXTRA extra_type);
bool toggle_mod(int index, bool force = false);
bool toggle_mod(const std::string& id, bool force = false);
bool generator_assigned() const;
bool generator_has_settings() const;

View file

@ -367,9 +367,8 @@ void manager::try_modifications(const std::vector<std::string>& ids, bool force)
}
}
void manager::try_modification_by_index(int index, bool activate, bool force)
void manager::try_modification_by_id(const std::string& id, bool activate, bool force)
{
std::string id = depinfo_.mandatory_child("modification", index)["id"];
std::vector<std::string> mods_copy = mods_;
if(activate) {

View file

@ -79,11 +79,11 @@ public:
/**
* Tries to enable/disable a specific modification
*
* @param index the index of the modification
* @param id the id of the modification
* @param activate activate or deactivate
* @param force whether to skip dependency check
*/
void try_modification_by_index(int index, bool activate, bool force = false);
void try_modification_by_id(const std::string& id, bool activate, bool force = false);
/**
* Tries to set the selected era

View file

@ -384,6 +384,7 @@ void campaign_selection::pre_show(window& window)
mod_menu_values.emplace_back("label", mod->name, "checkbox", active);
mod_states_.push_back(active);
mod_ids_.emplace_back(mod->id);
}
mods_menu.set_values(mod_menu_values);
@ -485,7 +486,7 @@ void campaign_selection::mod_toggled()
for(unsigned i = 0; i < mod_states_.size(); i++) {
if(mod_states_[i]) {
engine_.toggle_mod(i);
engine_.toggle_mod(mod_ids_[i]);
}
}

View file

@ -67,6 +67,7 @@ public:
, current_difficulty_()
, current_sorting_(RANK)
, currently_sorted_asc_(true)
, mod_ids_()
{
}
@ -133,6 +134,8 @@ private:
std::vector<std::string> last_search_words_;
inline const static std::string missing_campaign_ = "////missing-campaign////";
std::vector<std::string> mod_ids_;
};
} // namespace dialogs

View file

@ -210,13 +210,12 @@ void mp_create_game::pre_show(window& win)
toggle_button& mog_toggle = find_widget<toggle_button>(row_grid, "mod_active_state", false);
const int i = mod_list_->get_item_count() - 1;
if(std::find(activemods.begin(), activemods.end(), mod->id) != activemods.end()) {
create_engine_.active_mods().push_back(mod->id);
mog_toggle.set_value_bool(true);
}
connect_signal_notify_modified(mog_toggle, std::bind(&mp_create_game::on_mod_toggle, this, i, &mog_toggle));
connect_signal_notify_modified(mog_toggle, std::bind(&mp_create_game::on_mod_toggle, this, mod->id, &mog_toggle));
}
// No mods, hide the header
@ -378,7 +377,7 @@ void mp_create_game::pre_show(window& win)
create_engine_.set_current_era_index(cfg["index"].to_int()); }, true);
plugins_context_->set_callback("select_mod", [this](const config& cfg) {
on_mod_toggle(cfg["index"].to_int(), nullptr);
on_mod_toggle(cfg["id"].str(), nullptr);
}, true);
plugins_context_->set_accessor("get_selected", [this](const config&) {
@ -523,14 +522,14 @@ void mp_create_game::on_tab_select()
find_widget<stacked_widget>(get_window(), "pager", false).select_layer(i);
}
void mp_create_game::on_mod_toggle(const int index, toggle_button* sender)
void mp_create_game::on_mod_toggle(const std::string id, toggle_button* sender)
{
if(sender && (sender->get_value_bool() == create_engine_.dependency_manager().is_modification_active(index))) {
if(sender && (sender->get_value_bool() == create_engine_.dependency_manager().is_modification_active(id))) {
ERR_MP << "ignoring on_mod_toggle that is already set";
return;
}
create_engine_.toggle_mod(index);
create_engine_.toggle_mod(id);
sync_with_depcheck();

View file

@ -101,7 +101,7 @@ private:
void on_game_select();
void on_tab_select();
void on_era_select();
void on_mod_toggle(const int index, toggle_button* sender);
void on_mod_toggle(const std::string id, toggle_button* sender);
void on_random_faction_mode_select();
std::vector<std::string> get_active_mods();