Skip to content

Commit

Permalink
Fix duplicate mod ids breaking mod selection
Browse files Browse the repository at this point in the history
  • Loading branch information
Pentarctagon committed Feb 11, 2024
1 parent 6828b54 commit 2c9468f
Show file tree
Hide file tree
Showing 8 changed files with 18 additions and 17 deletions.
7 changes: 3 additions & 4 deletions src/game_initialization/create_engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -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";
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/game_initialization/create_engine.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ class create_engine
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;
Expand Down
3 changes: 1 addition & 2 deletions src/game_initialization/depcheck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions src/game_initialization/depcheck.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,11 @@ class manager
/**
* 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
Expand Down
3 changes: 2 additions & 1 deletion src/gui/dialogs/campaign_selection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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]);
}
}

Expand Down
3 changes: 3 additions & 0 deletions src/gui/dialogs/campaign_selection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ class campaign_selection : public modal_dialog
, current_difficulty_()
, current_sorting_(RANK)
, currently_sorted_asc_(true)
, mod_ids_()
{
}

Expand Down Expand Up @@ -133,6 +134,8 @@ class campaign_selection : public modal_dialog
std::vector<std::string> last_search_words_;

inline const static std::string missing_campaign_ = "////missing-campaign////";

std::vector<std::string> mod_ids_;
};

} // namespace dialogs
11 changes: 5 additions & 6 deletions src/gui/dialogs/multiplayer/mp_create_game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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&) {
Expand Down Expand Up @@ -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();

Expand Down
2 changes: 1 addition & 1 deletion src/gui/dialogs/multiplayer/mp_create_game.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ class mp_create_game : public modal_dialog, private plugin_executor
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();
Expand Down

0 comments on commit 2c9468f

Please sign in to comment.