Skip to content

Commit cd2a70b

Browse files
committed
PresetMan followup (followup to 64f97ed)
1 parent e15ad77 commit cd2a70b

File tree

1 file changed

+69
-53
lines changed

1 file changed

+69
-53
lines changed

Managers/PresetMan.cpp

Lines changed: 69 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -26,22 +26,23 @@ namespace RTE {
2626
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2727

2828
bool PresetMan::GetGroups(std::list<std::string> &groupList, int whichModule, const std::string &withType, bool moduleSpace) const {
29-
RTEAssert(whichModule < g_ModuleMan.GetTotalModuleCount(), "Tried to access an out of bounds data module number!");
30-
3129
bool foundAny = false;
32-
auto &loadedModules = g_ModuleMan.GetLoadedDataModules();
3330

3431
if (whichModule < 0) {
35-
for (DataModule *dataModule : loadedModules) {
32+
for (const auto &[moduleName, dataModule] : g_ModuleMan.GetLoadedDataModules()) {
3633
foundAny = dataModule->GetGroupsWithType(groupList, withType) || foundAny;
3734
}
3835
} else {
36+
RTEAssert(whichModule < g_ModuleMan.GetTotalModuleCount(), "Trying to get from an out of bounds DataModule ID in PresetMan::GetGroups!");
37+
3938
if (moduleSpace) {
40-
for (size_t officialModuleID = 0; officialModuleID < g_ModuleMan.GetOfficialModuleCount(); ++officialModuleID) {
41-
foundAny = loadedModules[officialModuleID]->GetGroupsWithType(groupList, withType) || foundAny;
39+
for (const auto &[moduleName, dataModule] : g_ModuleMan.GetLoadedDataModules()) {
40+
if (dataModule->IsOfficial()) {
41+
foundAny = dataModule->GetGroupsWithType(groupList, withType) || foundAny;
42+
}
4243
}
4344
}
44-
foundAny = loadedModules[whichModule]->GetGroupsWithType(groupList, withType) || foundAny;
45+
foundAny = g_ModuleMan.GetDataModule(whichModule)->GetGroupsWithType(groupList, withType) || foundAny;
4546
}
4647
if (!groupList.empty() && (withType == "All" || withType.empty())) {
4748
// DataModule::GetGroupsWithType doesn't sort and filter dupes when getting all groups, so do it here.
@@ -66,7 +67,7 @@ namespace RTE {
6667
reader >> ClassName;
6768
pClass = Entity::ClassInfo::GetClass(ClassName);
6869

69-
auto &loadedModules = g_ModuleMan.GetLoadedDataModules();
70+
DataModule *dataModule = g_ModuleMan.GetDataModule(whichModule);
7071

7172
if (pClass && pClass->IsConcrete()) {
7273
// Instantiate
@@ -78,20 +79,24 @@ namespace RTE {
7879
// Try to read in the preset instance's data from the reader
7980
if (newInstance && newInstance->Create(reader, false) < 0) {
8081
// Abort loading if we can't create entity and it's not in a module that allows ignoring missing items.
81-
if (!g_ModuleMan.GetDataModule(whichModule)->GetIgnoreMissingItems()) {
82+
if (!dataModule->GetIgnoreMissingItems()) {
8283
RTEAbort("Reading of a preset instance \"" + newInstance->GetPresetName() + "\" of class " + newInstance->GetClassName() + " failed in file " + reader.GetCurrentFilePath() + ", shortly before line #" + reader.GetCurrentFileLine());
8384
}
8485
} else if (newInstance) {
8586
// Try to add the instance to the collection
86-
loadedModules[whichModule]->AddEntityPreset(newInstance, reader.GetPresetOverwriting(), entityFilePath);
87+
dataModule->AddEntityPreset(newInstance, reader.GetPresetOverwriting(), entityFilePath);
8788

8889
// Regardless of whether there was a collision or not, use whatever now exists in the instance map of that class and name
89-
pReturnPreset = loadedModules[whichModule]->GetEntityPreset(newInstance->GetClassName(), newInstance->GetPresetName());
90+
pReturnPreset = dataModule->GetEntityPreset(newInstance->GetClassName(), newInstance->GetPresetName());
9091
// If the instance wasn't found in the specific DataModule, try to find it in all the official ones instead
9192
if (!pReturnPreset) {
92-
RTEAssert(g_ModuleMan.GetOfficialModuleCount() <= loadedModules.size(), "More official modules than modules loaded?!");
93-
for (int i = 0; i < g_ModuleMan.GetOfficialModuleCount() && !pReturnPreset; ++i) {
94-
pReturnPreset = loadedModules[i]->GetEntityPreset(newInstance->GetClassName(), newInstance->GetPresetName());
93+
for (const auto &[moduleID, loadedModule] : g_ModuleMan.GetLoadedDataModules()) {
94+
if (loadedModule->IsOfficial()) {
95+
pReturnPreset = loadedModule->GetEntityPreset(newInstance->GetClassName(), newInstance->GetPresetName());
96+
if (pReturnPreset) {
97+
break;
98+
}
99+
}
95100
}
96101
}
97102
}
@@ -111,8 +116,6 @@ namespace RTE {
111116

112117
const Entity *pRetEntity = nullptr;
113118

114-
auto &loadedModules = g_ModuleMan.GetLoadedDataModules();
115-
116119
// Preset name might have "[ModuleName]/" preceding it, detect it here and select proper module!
117120
int slashPos = presetName.find_first_of('/');
118121
if (slashPos != std::string::npos) {
@@ -124,18 +127,25 @@ namespace RTE {
124127
// All modules
125128
if (whichModule < 0) {
126129
// Search all modules
127-
for (int i = 0; i < loadedModules.size() && !pRetEntity; ++i) {
128-
pRetEntity = loadedModules[i]->GetEntityPreset(typeName, presetName);
130+
for (const auto &[moduleID, dataModule] : g_ModuleMan.GetLoadedDataModules()) {
131+
pRetEntity = dataModule->GetEntityPreset(typeName, presetName);
132+
if (pRetEntity) {
133+
break;
134+
}
129135
}
130136
} else {
131137
// Try to get it from the asked for module
132-
pRetEntity = loadedModules[whichModule]->GetEntityPreset(typeName, presetName);
138+
pRetEntity = g_ModuleMan.GetDataModule(whichModule)->GetEntityPreset(typeName, presetName);
133139

134140
// If couldn't find it in there, then try all the official modules!
135141
if (!pRetEntity) {
136-
RTEAssert(g_ModuleMan.GetOfficialModuleCount() <= loadedModules.size(), "More official modules than modules loaded?!");
137-
for (int i = 0; i < g_ModuleMan.GetOfficialModuleCount() && !pRetEntity; ++i) {
138-
pRetEntity = loadedModules[i]->GetEntityPreset(typeName, presetName);
142+
for (const auto &[moduleID, dataModule] : g_ModuleMan.GetLoadedDataModules()) {
143+
if (dataModule->IsOfficial()) {
144+
pRetEntity = dataModule->GetEntityPreset(typeName, presetName);
145+
if (pRetEntity) {
146+
break;
147+
}
148+
}
139149
}
140150
}
141151
}
@@ -170,9 +180,13 @@ namespace RTE {
170180

171181
// If couldn't find it in there, then try all the official modules!
172182
if (pRetPath.empty()) {
173-
RTEAssert(g_ModuleMan.GetOfficialModuleCount() <= loadedModules.size(), "More official modules than modules loaded?!");
174-
for (int i = 0; i < g_ModuleMan.GetOfficialModuleCount() && pRetPath.empty(); ++i) {
175-
pRetPath = loadedModules[i]->GetEntityDataLocation(type, preset);
183+
for (const auto &[moduleID, dataModule] : loadedModules) {
184+
if (dataModule->IsOfficial()) {
185+
pRetPath = dataModule->GetEntityDataLocation(type, preset);
186+
if (!pRetPath.empty()) {
187+
break;
188+
}
189+
}
176190
}
177191
}
178192
}
@@ -183,26 +197,27 @@ namespace RTE {
183197
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
184198

185199
bool PresetMan::GetAllOfType(std::list<Entity *> &entityList, const std::string &typeName, int whichModule, bool moduleSpace) const {
186-
RTEAssert(whichModule < g_ModuleMan.GetTotalModuleCount(), "Trying to get from an out of bounds DataModule ID!");
187-
188200
if (typeName.empty()) {
189201
return false;
190202
}
191203

192204
bool foundAny = false;
193-
auto &loadedModules = g_ModuleMan.GetLoadedDataModules();
194205

195206
if (whichModule < 0) {
196-
for (DataModule *dataModule : loadedModules) {
207+
for (const auto &[moduleName, dataModule] : g_ModuleMan.GetLoadedDataModules()) {
197208
foundAny = dataModule->GetAllOfType(entityList, typeName) || foundAny;
198209
}
199210
} else {
211+
RTEAssert(whichModule < g_ModuleMan.GetTotalModuleCount(), "Trying to get from an out of bounds DataModule ID in PresetMan::GetAllOfType!");
212+
200213
if (moduleSpace) {
201-
for (size_t officialModuleID = 0; officialModuleID < g_ModuleMan.GetOfficialModuleCount(); ++officialModuleID) {
202-
foundAny = loadedModules[officialModuleID]->GetAllOfType(entityList, typeName) || foundAny;
214+
for (const auto &[moduleName, dataModule] : g_ModuleMan.GetLoadedDataModules()) {
215+
if (dataModule->IsOfficial()) {
216+
foundAny = dataModule->GetAllOfType(entityList, typeName) || foundAny;
217+
}
203218
}
204219
}
205-
foundAny = loadedModules[whichModule]->GetAllOfType(entityList, typeName);
220+
foundAny = g_ModuleMan.GetDataModule(whichModule)->GetAllOfType(entityList, typeName);
206221
}
207222
return foundAny;
208223
}
@@ -213,21 +228,22 @@ namespace RTE {
213228
RTEAssert(!groupNames.empty(), "Looking for empty groups in PresetMan::GetAllOfGroups!");
214229

215230
bool foundAny = false;
216-
auto &loadedModules = g_ModuleMan.GetLoadedDataModules();
217231

218232
if (whichModule < 0) {
219-
for (DataModule *dataModule : loadedModules) {
233+
for (const auto &[moduleName, dataModule] : g_ModuleMan.GetLoadedDataModules()) {
220234
foundAny = dataModule->GetAllOfGroups(entityList, groupNames, typeName) || foundAny;
221235
}
222236
} else {
223-
RTEAssert(whichModule < loadedModules.size(), "Trying to get from an out of bounds DataModule ID in PresetMan::GetAllOfGroups!");
237+
RTEAssert(whichModule < g_ModuleMan.GetTotalModuleCount(), "Trying to get from an out of bounds DataModule ID in PresetMan::GetAllOfGroups!");
224238

225239
if (moduleSpace) {
226-
for (size_t officialModuleID = 0; officialModuleID < g_ModuleMan.GetOfficialModuleCount(); ++officialModuleID) {
227-
foundAny = loadedModules[officialModuleID]->GetAllOfGroups(entityList, groupNames, typeName) || foundAny;
240+
for (const auto &[moduleName, dataModule] : g_ModuleMan.GetLoadedDataModules()) {
241+
if (dataModule->IsOfficial()) {
242+
foundAny = dataModule->GetAllOfGroups(entityList, groupNames, typeName) || foundAny;
243+
}
228244
}
229245
}
230-
foundAny = loadedModules[whichModule]->GetAllOfGroups(entityList, groupNames, typeName);
246+
foundAny = g_ModuleMan.GetDataModule(whichModule)->GetAllOfGroups(entityList, groupNames, typeName);
231247
}
232248
return foundAny;
233249
}
@@ -242,23 +258,22 @@ namespace RTE {
242258
}
243259

244260
bool foundAny = false;
245-
auto &loadedModules = g_ModuleMan.GetLoadedDataModules();
246261

247262
if (whichModule < 0) {
248-
for (DataModule *dataModule : loadedModules) {
263+
for (const auto &[moduleID, dataModule] : g_ModuleMan.GetLoadedDataModules()) {
249264
foundAny = dataModule->GetAllNotOfGroups(entityList, groupNames, typeName) || foundAny;
250265
}
251266
} else {
252-
RTEAssert(whichModule < loadedModules.size(), "Trying to get from an out of bounds DataModule ID in PresetMan::GetAllNotOfGroups!");
253-
foundAny = loadedModules[whichModule]->GetAllNotOfGroups(entityList, groupNames, typeName);
267+
RTEAssert(whichModule < g_ModuleMan.GetTotalModuleCount(), "Trying to get from an out of bounds DataModule ID in PresetMan::GetAllNotOfGroups!");
268+
foundAny = g_ModuleMan.GetDataModule(whichModule)->GetAllNotOfGroups(entityList, groupNames, typeName);
254269
}
255270
return foundAny;
256271
}
257272

258273
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
259274

260275
Entity * PresetMan::GetRandomOfGroup(const std::string &groupName, const std::string &typeName, int whichModule, bool moduleSpace) {
261-
RTEAssert(!groupName.empty(), "Looking for empty group!");
276+
RTEAssert(!groupName.empty(), "Looking for empty group in PresetMan::GetRandomOfGroup!");
262277

263278
if (std::list<Entity *> entityList; GetAllOfGroups(entityList, { groupName }, typeName, whichModule, moduleSpace)) {
264279
auto entityItr = entityList.begin();
@@ -272,23 +287,21 @@ namespace RTE {
272287
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
273288

274289
Entity * PresetMan::GetRandomBuyableOfGroupFromTech(const std::string &groupName, const std::string &typeName, int whichModule) {
275-
RTEAssert(!groupName.empty(), "Looking for empty group!");
290+
RTEAssert(!groupName.empty(), "Looking for empty group in PresetMan::GetRandomBuyableOfGroupFromTech!");
276291

277292
bool foundAny = false;
278293
std::list<Entity *> entityList;
279294
std::list<Entity *> tempList;
280295

281-
auto &loadedModules = g_ModuleMan.GetLoadedDataModules();
282-
283296
if (whichModule < 0) {
284-
for (DataModule *dataModule : loadedModules) {
297+
for (const auto &[moduleID, dataModule] : g_ModuleMan.GetLoadedDataModules()) {
285298
if (dataModule->IsFaction()) {
286299
foundAny = dataModule->GetAllOfGroups(tempList, { groupName }, typeName) || foundAny;
287300
}
288301
}
289302
} else {
290-
RTEAssert(whichModule < loadedModules.size(), "Trying to get from an out of bounds DataModule ID!");
291-
foundAny = loadedModules[whichModule]->GetAllOfGroups(tempList, { groupName }, typeName);
303+
RTEAssert(whichModule < g_ModuleMan.GetTotalModuleCount(), "Trying to get from an out of bounds DataModule ID in PresetMan::GetRandomBuyableOfGroupFromTech!");
304+
foundAny = g_ModuleMan.GetDataModule(whichModule)->GetAllOfGroups(tempList, { groupName }, typeName);
292305
}
293306

294307
// Filter found entities, we need only buyables.
@@ -390,7 +403,7 @@ namespace RTE {
390403

391404
void PresetMan::ReloadAllScripts() const {
392405
g_LuaMan.ClearUserModuleCache();
393-
for (const DataModule *dataModule : g_ModuleMan.GetLoadedDataModules()) {
406+
for (const auto &[moduleID, dataModule] : g_ModuleMan.GetLoadedDataModules()) {
394407
dataModule->ReloadAllScripts();
395408
}
396409
g_ConsoleMan.PrintString("SYSTEM: Scripts reloaded!");
@@ -462,15 +475,18 @@ namespace RTE {
462475
bool PresetMan::AddMaterialMapping(uint8_t fromID, uint8_t toID, int whichModule) const {
463476
//RTEAssert(whichModule >= g_ModuleMan.GetOfficialModuleCount() && whichModule < g_ModuleMan.GetTotalModuleCount(), "Tried to make a material mapping in an official or out-of-bounds DataModule!");
464477

465-
return g_ModuleMan.GetLoadedDataModules()[whichModule]->AddMaterialMapping(fromID, toID);
478+
if (DataModule *dataModule = g_ModuleMan.GetDataModule(whichModule); dataModule->IsOfficial()) {
479+
return dataModule->AddMaterialMapping(fromID, toID);
480+
}
481+
return false;
466482
}
467483

468484
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
469485

470486
bool PresetMan::AddEntityPreset(Entity *entityToAdd, int whichModule, bool overwriteSame, const std::string &readFromFile) const {
471487
RTEAssert(whichModule >= 0 && whichModule < g_ModuleMan.GetTotalModuleCount(), "Tried to access an out of bounds data module number!");
472488

473-
return g_ModuleMan.GetLoadedDataModules()[whichModule]->AddEntityPreset(entityToAdd, overwriteSame, readFromFile);
489+
return g_ModuleMan.GetDataModule(whichModule)->AddEntityPreset(entityToAdd, overwriteSame, readFromFile);
474490
}
475491

476492
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@@ -498,7 +514,7 @@ namespace RTE {
498514
}
499515
} else {
500516
// Note that we'll return this instance regardless of whether the adding was successful or not.
501-
g_ModuleMan.GetLoadedDataModules()[whichModule]->AddEntityPreset(newInstance, reader.GetPresetOverwriting(), entityFilePath);
517+
g_ModuleMan.GetDataModule(whichModule)->AddEntityPreset(newInstance, reader.GetPresetOverwriting(), entityFilePath);
502518
return newInstance;
503519
}
504520
}

0 commit comments

Comments
 (0)