Skip to content

Commit aa243de

Browse files
committed
Use unordered set for disabled module names
Check modules for enabled instead of disabled
1 parent ca33886 commit aa243de

File tree

5 files changed

+42
-42
lines changed

5 files changed

+42
-42
lines changed

Managers/ModuleMan.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ namespace RTE {
1818

1919
void ModuleMan::Clear() {
2020
m_LoadedDataModules.clear();
21-
m_DisabledMods.clear();
21+
m_DisabledDataModuleNames.clear();
2222
}
2323

2424
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@@ -158,8 +158,6 @@ namespace RTE {
158158
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
159159

160160
bool ModuleMan::LoadAllDataModules() {
161-
Destroy();
162-
163161
LoadOfficialModules();
164162

165163
// If a single module is specified, skip loading all other unofficial modules and load specified module only.
@@ -230,7 +228,7 @@ namespace RTE {
230228
std::string directoryEntryPath = directoryEntry.path().generic_string();
231229
if (directoryEntryPath.ends_with(".rte")) {
232230
std::string moduleName = directoryEntryPath.substr(directoryEntryPath.find_last_of('/') + 1, std::string::npos);
233-
if (!g_ModuleMan.IsModDisabled(moduleName) && !IsModuleOfficial(moduleName) && !IsModuleUserdata(moduleName)) {
231+
if (g_ModuleMan.IsModuleEnabled(moduleName) && !IsModuleOfficial(moduleName) && !IsModuleUserdata(moduleName)) {
234232
// NOTE: LoadDataModule can return false (especially since it may try to load already loaded modules, which is okay) and shouldn't cause stop, so we can ignore its return value here.
235233
LoadDataModule(moduleName, DataModule::DataModuleType::Unofficial, LoadingScreen::LoadingSplashProgressReport);
236234
}
@@ -253,7 +251,11 @@ namespace RTE {
253251

254252
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
255253

256-
bool ModuleMan::IsModDisabled(const std::string &modModule) const {
257-
return (m_DisabledMods.find(modModule) != m_DisabledMods.end()) ? m_DisabledMods.at(modModule) : false;
254+
bool ModuleMan::IsModuleEnabled(const std::string_view &moduleName) const {
255+
return std::none_of(m_DisabledDataModuleNames.begin(), m_DisabledDataModuleNames.end(),
256+
[&moduleName](const std::string_view &disabledModuleName) {
257+
return disabledModuleName == moduleName;
258+
}
259+
);
258260
}
259261
}

Managers/ModuleMan.h

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,21 @@ namespace RTE {
118118
std::string GetFullModulePath(const std::string &modulePath) const;
119119
#pragma endregion
120120

121+
#pragma region DataModule Management
122+
/// <summary>
123+
/// Gets the DataModule names that are disabled and should not be loaded at startup.
124+
/// </summary>
125+
/// <returns>Map of mods which are disabled.</returns>
126+
std::unordered_set<std::string> & GetDisabledDataModuleNames() { return m_DisabledDataModuleNames; }
127+
128+
/// <summary>
129+
/// Gets whether the specified mod is disabled in the settings.
130+
/// </summary>
131+
/// <param name="moduleName">Mod to check.</param>
132+
/// <returns>Whether the module is disabled via settings.</returns>
133+
bool IsModuleEnabled(const std::string_view &moduleName) const;
134+
#pragma endregion
135+
121136
#pragma region Contrete Methods
122137
/// <summary>
123138
/// Reads an entire DataModule and adds it to this. NOTE that official modules can't be loaded after any non-official ones!
@@ -134,18 +149,7 @@ namespace RTE {
134149
bool LoadAllDataModules();
135150
#pragma endregion
136151

137-
/// <summary>
138-
/// Gets the map of mods which are disabled.
139-
/// </summary>
140-
/// <returns>Map of mods which are disabled.</returns>
141-
std::map<std::string, bool> & GetDisabledModsMap() { return m_DisabledMods; }
142152

143-
/// <summary>
144-
/// Gets whether the specified mod is disabled in the settings.
145-
/// </summary>
146-
/// <param name="modModule">Mod to check.</param>
147-
/// <returns>Whether the mod is disabled via settings.</returns>
148-
bool IsModDisabled(const std::string &modModule) const;
149153

150154
private:
151155

@@ -154,10 +158,9 @@ namespace RTE {
154158

155159
std::unordered_map<int, DataModule *> m_LoadedDataModules; //!< Map of all loaded DataModules by their ID. Owned by this.
156160

161+
std::unordered_set<std::string> m_DisabledDataModuleNames; //!< The DataModule names that are disabled and should not be loaded at startup.
157162
std::string m_SingleModuleToLoad; //!< Name of the single module to load after the official modules.
158163

159-
std::map<std::string, bool> m_DisabledMods; //!< Map of the module names that are disabled.
160-
161164
#pragma region Module Loading Breakdown
162165
/// <summary>
163166
/// Loads all the official DataModules individually with LoadDataModule.

Managers/SettingsMan.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ namespace RTE {
289289
} else if (propName == "VisibleAssemblyGroup") {
290290
m_VisibleAssemblyGroupsList.push_back(reader.ReadPropValue());
291291
} else if (propName == "DisableMod") {
292-
g_ModuleMan.m_DisabledMods.try_emplace(reader.ReadPropValue(), true);
292+
g_ModuleMan.m_DisabledDataModuleNames.emplace(reader.ReadPropValue());
293293
} else if (propName == "EnableGlobalScript") {
294294
m_EnabledGlobalScripts.try_emplace(reader.ReadPropValue(), true);
295295
} else if (propName == "MouseSensitivity") {
@@ -472,13 +472,13 @@ namespace RTE {
472472
}
473473
}
474474

475-
if (!g_ModuleMan.m_DisabledMods.empty()) {
475+
if (!g_ModuleMan.m_DisabledDataModuleNames.empty()) {
476476
writer.NewLine(false, 2);
477477
writer.NewDivider(false);
478478
writer.NewLineString("// Disabled Mods", false);
479479
writer.NewLine(false);
480-
for (const auto &[modPath, modDisabled] : g_ModuleMan.m_DisabledMods) {
481-
if (modDisabled) { writer.NewPropertyWithValue("DisableMod", modPath); }
480+
for (const std::string &disabledModuleName : g_ModuleMan.m_DisabledDataModuleNames) {
481+
writer.NewPropertyWithValue("DisableMod", disabledModuleName);
482482
}
483483
}
484484

Menus/ModManagerGUI.cpp

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -58,21 +58,20 @@ namespace RTE {
5858
void ModManagerGUI::PopulateKnownModsList() {
5959
for (const auto &[moduleID, dataModule] : g_ModuleMan.GetLoadedDataModules()) {
6060
if (!dataModule->IsOfficial() && !dataModule->IsUserdata()) {
61-
m_KnownMods.emplace_back(dataModule->GetFileName(), dataModule->GetFriendlyName(), dataModule->GetDescription(), g_ModuleMan.IsModDisabled(dataModule->GetFileName()));
61+
m_KnownMods.emplace_back(dataModule->GetFileName(), dataModule->GetFriendlyName(), dataModule->GetDescription(), g_ModuleMan.IsModuleEnabled(dataModule->GetFileName()));
6262
}
6363
}
6464
// Add missing data from disabled mods settings
65-
for (const auto &[modPath, modDisabled] : g_ModuleMan.GetDisabledModsMap()) {
65+
for (const std::string &disabledModuleName : g_ModuleMan.GetDisabledDataModuleNames()) {
6666
bool found = false;
6767
for (const ModRecord &knowModListEntry : m_KnownMods) {
68-
if (modPath == knowModListEntry.ModulePath) {
68+
if (disabledModuleName == knowModListEntry.ModulePath) {
6969
found = true;
7070
break;
7171
}
7272
}
7373
if (!found) {
74-
ModRecord disabledModRecord = { modPath, "N/A, Module not loaded", "N/A, Module not loaded", modDisabled };
75-
m_KnownMods.emplace_back(disabledModRecord);
74+
m_KnownMods.emplace_back(disabledModuleName, "N/A, Module not loaded", "N/A, Module not loaded", false);
7675
}
7776
}
7877
std::sort(m_KnownMods.begin(), m_KnownMods.end());
@@ -110,21 +109,17 @@ namespace RTE {
110109
void ModManagerGUI::ToggleMod() {
111110
int index = m_ModsListBox->GetSelectedIndex();
112111
if (index > -1) {
113-
std::map<std::string, bool> &disabledModsList = g_ModuleMan.GetDisabledModsMap();
112+
std::unordered_set<std::string> &disabledModsList = g_ModuleMan.GetDisabledDataModuleNames();
114113
GUIListPanel::Item *selectedItem = m_ModsListBox->GetSelected();
115114
ModRecord &modRecord = m_KnownMods.at(selectedItem->m_ExtraIndex);
116115

117-
modRecord.Disabled = !modRecord.Disabled;
118-
if (modRecord.Disabled) {
119-
m_ToggleModButton->SetText("Enable Mod");
120-
if (disabledModsList.find(modRecord.ModulePath) != disabledModsList.end()) {
121-
disabledModsList.at(modRecord.ModulePath) = true;
122-
} else {
123-
disabledModsList.try_emplace(modRecord.ModulePath, true);
124-
}
125-
} else {
116+
modRecord.Enabled = !modRecord.Enabled;
117+
if (modRecord.Enabled) {
126118
m_ToggleModButton->SetText("Disable Mod");
127-
disabledModsList.at(modRecord.ModulePath) = false;
119+
disabledModsList.erase(disabledModsList.find(modRecord.ModulePath));
120+
} else {
121+
m_ToggleModButton->SetText("Enable Mod");
122+
disabledModsList.emplace(modRecord.ModulePath);
128123
}
129124
selectedItem->m_Name = modRecord.GetDisplayString();
130125
m_ModsListBox->SetSelectedIndex(index);
@@ -186,7 +181,7 @@ namespace RTE {
186181
if (guiEvent.GetControl() == m_ModsListBox && (guiEvent.GetMsg() == GUIListBox::Select && m_ModsListBox->GetSelectedIndex() > -1)) {
187182
const ModRecord &modRecord = m_KnownMods.at(m_ModsListBox->GetSelected()->m_ExtraIndex);
188183
m_ModOrScriptDescriptionLabel->SetText(modRecord.Description);
189-
m_ToggleModButton->SetText(modRecord.Disabled ? "Enable Mod" : "Disable Mod");
184+
m_ToggleModButton->SetText(modRecord.Enabled ? "Disable Mod" : "Enable Mod");
190185
} else if (guiEvent.GetControl() == m_ScriptsListBox && (guiEvent.GetMsg() == GUIListBox::Select && m_ScriptsListBox->GetSelectedIndex() > -1)) {
191186
const ScriptRecord &scriptRecord = m_KnownScripts.at(m_ScriptsListBox->GetSelected()->m_ExtraIndex);
192187
m_ModOrScriptDescriptionLabel->SetText(scriptRecord.Description);

Menus/ModManagerGUI.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,13 @@ namespace RTE {
4848
std::string ModulePath; //!< Mod DataModule path.
4949
std::string ModuleName; //!< Mod ModuleName.
5050
std::string Description; //!< Mod description.
51-
bool Disabled; //!< Whether the mod is disabled through the settings file or not.
51+
bool Enabled; //!< Whether the mod is disabled through the settings file or not.
5252

5353
/// <summary>
5454
/// Makes GUI displayable string with mod info.
5555
/// </summary>
5656
/// <returns>String with mod info.</returns>
57-
std::string GetDisplayString() const { return (Disabled ? "- " : "+ ") + ModulePath + " - " + ModuleName; }
57+
std::string GetDisplayString() const { return (Enabled ? "+ " : "- ") + ModulePath + " - " + ModuleName; }
5858

5959
/// <summary>
6060
/// Comparison operator for sorting the KnownMods list alphabetically by path with std::sort.

0 commit comments

Comments
 (0)