@@ -29,83 +29,69 @@ namespace RTE {
29
29
// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
30
30
31
31
void ModuleMan::Destroy () {
32
- for (const DataModule * dataModule : m_LoadedDataModules) {
32
+ for (const auto &[moduleID, dataModule] : m_LoadedDataModules) {
33
33
delete dataModule;
34
34
}
35
35
Clear ();
36
36
}
37
37
38
38
// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
39
39
40
- bool ModuleMan::IsModDisabled (const std::string &modModule) const {
41
- return (m_DisabledMods.find (modModule) != m_DisabledMods.end ()) ? m_DisabledMods.at (modModule) : false ;
40
+ bool ModuleMan::IsModuleOfficial (const std::string_view &moduleName) const {
41
+ return std::any_of (c_OfficialModules.begin (), c_OfficialModules.end (),
42
+ [&moduleName](const std::string_view &officialModuleName) {
43
+ return officialModuleName == moduleName;
44
+ }
45
+ );
42
46
}
43
47
44
48
// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
45
49
46
- bool ModuleMan::IsModuleOfficial (const std::string &moduleName) const {
47
- for (const std::string officialModuleName : c_OfficialModules) {
48
- // Module name coming from Lua might be with different casing.
49
- if (StringsEqualCaseInsensitive (officialModuleName, moduleName)) {
50
- return true ;
50
+ bool ModuleMan::IsModuleUserdata (const std::string_view &moduleName) const {
51
+ return std::any_of (c_UserdataModules.begin (), c_UserdataModules.end (),
52
+ [&moduleName](const std::pair<std::string_view, std::string_view> &userdataModuleEntry) {
53
+ return userdataModuleEntry.first == moduleName;
51
54
}
52
- }
53
- return false ;
55
+ );
54
56
}
55
57
56
58
// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
57
59
58
- bool ModuleMan::IsModuleUserdata (const std::string &moduleName) const {
59
- auto userdataModuleItr = std::find_if (c_UserdataModules.begin (), c_UserdataModules.end (),
60
- [&moduleName](const auto &userdataModulesEntry) {
61
- return userdataModulesEntry.first == moduleName;
60
+ DataModule * ModuleMan::GetDataModule (int whichModule) {
61
+ if (whichModule < 0 || whichModule >= GetTotalModuleCount ()) {
62
+ return nullptr ;
63
+ }
64
+
65
+ auto loadedModulesItr = std::find_if (m_LoadedDataModules.begin (), m_LoadedDataModules.end (),
66
+ [&whichModule](const auto &loadedModuleEntry) {
67
+ return loadedModuleEntry.first == whichModule;
62
68
}
63
69
);
64
- return userdataModuleItr != c_UserdataModules. end () ;
70
+ return (*loadedModulesItr). second ;
65
71
}
66
72
67
73
// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
68
74
69
- const DataModule * ModuleMan::GetDataModule (int whichModule) {
70
- RTEAssert (whichModule < (int )m_LoadedDataModules.size (), " Tried to access an out of bounds data module number!" );
71
- return m_LoadedDataModules[whichModule];
72
- }
73
-
74
- // ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
75
+ int ModuleMan::GetModuleID (const std::string_view &moduleName) const {
76
+ if (moduleName.empty ()) {
77
+ return -1 ;
78
+ }
75
79
76
- const std::string ModuleMan::GetModuleName (int whichModule) {
77
- RTEAssert (whichModule < (int )m_LoadedDataModules.size (), " Tried to access an out of bounds data module number!" );
78
- return m_LoadedDataModules[whichModule]->GetFileName ();
80
+ auto loadedModulesItr = std::find_if (m_LoadedDataModules.begin (), m_LoadedDataModules.end (),
81
+ [&moduleName](const auto &loadedModuleEntry) {
82
+ return loadedModuleEntry.second ->GetFileName () == moduleName;
83
+ }
84
+ );
85
+ return (loadedModulesItr != m_LoadedDataModules.end ()) ? (*loadedModulesItr).first : -1 ;
79
86
}
80
87
81
88
// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
82
89
83
- int ModuleMan::GetModuleID (std::string moduleName) {
84
- // Lower-case search name so we can match up against the already-lowercase names in m_DataModuleIDs
85
- std::transform (moduleName.begin (), moduleName.end (), moduleName.begin (), ::tolower);
86
-
87
- // First pass
88
- std::map<std::string, size_t >::iterator itr = m_DataModuleIDs.find (moduleName);
89
- if (itr != m_DataModuleIDs.end ()) {
90
- return (*itr).second ;
91
- }
92
-
93
- // Try with or without the .rte on the end before giving up
94
- int dotPos = moduleName.find_last_of (' .' );
95
- // Wasnt, so try adding it
96
- if (dotPos == std::string::npos) {
97
- moduleName = moduleName + System::GetModulePackageExtension ();
98
- // There was ".rte", so try to shave it off the name
99
- } else {
100
- moduleName = moduleName.substr (0 , dotPos);
101
- }
102
-
103
- // Try to find the module again!
104
- itr = m_DataModuleIDs.find (moduleName);
105
- if (itr != m_DataModuleIDs.end ()) {
106
- return (*itr).second ;
90
+ std::string ModuleMan::GetModuleName (int whichModule) {
91
+ if (const DataModule *dataModule = GetDataModule (whichModule)) {
92
+ return dataModule->GetFileName ();
107
93
}
108
- return - 1 ;
94
+ return " " ;
109
95
}
110
96
111
97
// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@@ -132,15 +118,6 @@ namespace RTE {
132
118
return moduleName;
133
119
}
134
120
135
- // ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
136
-
137
- int ModuleMan::GetModuleIDFromPath (const std::string &dataPath) {
138
- if (dataPath.empty ()) {
139
- return -1 ;
140
- }
141
- return GetModuleID (GetModuleNameFromPath (dataPath));
142
- }
143
-
144
121
// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
145
122
146
123
std::string ModuleMan::GetFullModulePath (const std::string &modulePath) const {
@@ -158,73 +135,34 @@ namespace RTE {
158
135
return (pathTopDir == moduleTopDir) ? modulePathGeneric : moduleTopDir + modulePathGeneric;
159
136
}
160
137
161
-
162
138
// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
163
139
164
140
bool ModuleMan::LoadDataModule (const std::string &moduleName, DataModule::DataModuleType moduleType, const ProgressCallback &progressCallback) {
165
141
if (moduleName.empty ()) {
166
142
return false ;
167
143
}
168
- // Make a lowercase-version of the module name so it makes it easier to compare to and find case-agnostically.
169
- std::string lowercaseName = moduleName;
170
- std::transform (lowercaseName.begin (), lowercaseName.end (), lowercaseName.begin (), ::tolower);
171
144
172
- // Make sure we don't add the same module twice.
173
- for (const DataModule *dataModule : m_LoadedDataModules) {
174
- if (dataModule->GetFileName () == moduleName) {
175
- return false ;
176
- }
145
+ if (const DataModule *dataModule = GetDataModule (GetModuleID (moduleName)); dataModule) {
146
+ return true ;
177
147
}
178
148
179
149
// Only instantiate it here, because it needs to be in the lists of this before being created.
180
150
DataModule *newModule = new DataModule ();
151
+ newModule->m_ModuleType = moduleType;
152
+ newModule->m_ModuleID = static_cast <int >(m_LoadedDataModules.size ());
181
153
182
- // Official modules are stacked in the beginning of the vector.
183
- if (official && !userdata) {
184
- // Halt if an official module is being loaded after any non-official ones!
185
- // RTEAssert(m_LoadedDataModules.size() == m_OfficialModuleCount, "Trying to load an official module after a non-official one has been loaded!");
186
-
187
- // Find where the official modules end in the vector.
188
- std::vector<DataModule *>::iterator moduleItr = m_LoadedDataModules.begin ();
189
- size_t newModuleID = 0 ;
190
- for (; newModuleID < m_OfficialModuleCount; ++newModuleID) {
191
- moduleItr++;
192
- }
193
- // Insert into after the last official one.
194
- m_LoadedDataModules.emplace (moduleItr, newModule);
195
- m_DataModuleIDs.try_emplace (lowercaseName, newModuleID);
196
- m_OfficialModuleCount++;
197
- } else {
198
- if (userdata) {
199
- newModule->SetAsUserdata ();
200
- }
201
- m_LoadedDataModules.emplace_back (newModule);
202
- m_DataModuleIDs.try_emplace (lowercaseName, m_LoadedDataModules.size () - 1 );
203
- }
154
+ m_LoadedDataModules.try_emplace (newModule->m_ModuleID , newModule);
204
155
205
156
if (newModule->Create (moduleName, progressCallback) < 0 ) {
206
157
RTEAbort (" Failed to find the " + moduleName + " Data Module!" );
207
158
return false ;
208
159
}
209
- newModule = nullptr ;
210
160
return true ;
211
161
}
212
162
213
-
214
- // ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
215
-
216
- void ModuleMan::LoadOfficialModules () {
217
- for (const std::string &officialModule : c_OfficialModules) {
218
- if (!LoadDataModule (officialModule, DataModule::DataModuleType::Official, LoadingScreen::LoadingSplashProgressReport)) {
219
- RTEAbort (" Failed to load official DataModule \" " + officialModule + " \" !" );
220
- }
221
- }
222
- }
223
-
224
163
// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
225
164
226
165
bool ModuleMan::LoadAllDataModules () {
227
- // Destroy any possible loaded modules.
228
166
Destroy ();
229
167
230
168
LoadOfficialModules ();
@@ -238,33 +176,24 @@ namespace RTE {
238
176
}
239
177
} else {
240
178
FindAndExtractZippedModules ();
241
-
242
- std::vector<std::filesystem::directory_entry> modDirectoryFolders;
243
- const std::string modDirectory = System::GetWorkingDirectory () + System::GetModDirectory ();
244
- std::copy_if (std::filesystem::directory_iterator (modDirectory), std::filesystem::directory_iterator (), std::back_inserter (modDirectoryFolders),
245
- [](auto dirEntry) {
246
- return std::filesystem::is_directory (dirEntry);
247
- }
248
- );
249
- std::sort (modDirectoryFolders.begin (), modDirectoryFolders.end ());
250
-
251
- for (const std::filesystem::directory_entry &directoryEntry : modDirectoryFolders) {
252
- std::string directoryEntryPath = directoryEntry.path ().generic_string ();
253
- if (std::regex_match (directoryEntryPath, std::regex (" .*\.rte" ))) {
254
- std::string moduleName = directoryEntryPath.substr (directoryEntryPath.find_last_of (' /' ) + 1 , std::string::npos);
255
- if (!g_ModuleMan.IsModDisabled (moduleName) && !IsModuleOfficial (moduleName) && !IsModuleUserdata (moduleName)) {
256
- // 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.
257
- LoadDataModule (moduleName, DataModule::DataModuleType::Unofficial, LoadingScreen::LoadingSplashProgressReport);
258
- }
259
- }
260
- }
179
+ LoadUnofficialModules ();
261
180
262
181
// Load userdata modules AFTER all other techs etc are loaded; might be referring to stuff in user mods.
263
182
return LoadUserdataModules ();
264
183
}
265
184
return true ;
266
185
}
267
186
187
+ // ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
188
+
189
+ void ModuleMan::LoadOfficialModules () {
190
+ for (const std::string &officialModule : c_OfficialModules) {
191
+ if (!LoadDataModule (officialModule, DataModule::DataModuleType::Official, LoadingScreen::LoadingSplashProgressReport)) {
192
+ RTEAbort (" Failed to load official DataModule \" " + officialModule + " \" !" );
193
+ }
194
+ }
195
+ }
196
+
268
197
// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
269
198
270
199
bool ModuleMan::LoadUserdataModules () {
@@ -290,17 +219,46 @@ namespace RTE {
290
219
return true ;
291
220
}
292
221
222
+ // ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
293
223
224
+ void ModuleMan::LoadUnofficialModules () {
225
+ std::vector<std::filesystem::directory_entry> modDirectoryFolders;
226
+ const std::string modDirectory = System::GetWorkingDirectory () + System::GetModDirectory ();
227
+ std::copy_if (std::filesystem::directory_iterator (modDirectory), std::filesystem::directory_iterator (), std::back_inserter (modDirectoryFolders),
228
+ [](auto dirEntry) {
229
+ return std::filesystem::is_directory (dirEntry);
230
+ }
231
+ );
232
+ std::sort (modDirectoryFolders.begin (), modDirectoryFolders.end ());
233
+
234
+ for (const std::filesystem::directory_entry &directoryEntry : modDirectoryFolders) {
235
+ std::string directoryEntryPath = directoryEntry.path ().generic_string ();
236
+ if (directoryEntryPath.ends_with (" .rte" )) {
237
+ std::string moduleName = directoryEntryPath.substr (directoryEntryPath.find_last_of (' /' ) + 1 , std::string::npos);
238
+ if (!g_ModuleMan.IsModDisabled (moduleName) && !IsModuleOfficial (moduleName) && !IsModuleUserdata (moduleName)) {
239
+ // 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.
240
+ LoadDataModule (moduleName, DataModule::DataModuleType::Unofficial, LoadingScreen::LoadingSplashProgressReport);
241
+ }
242
+ }
243
+ }
244
+ }
294
245
295
246
// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
296
247
297
248
void ModuleMan::FindAndExtractZippedModules () const {
298
- for (const std::filesystem::directory_entry &directoryEntry : std::filesystem::directory_iterator (System::GetWorkingDirectory () + System::GetModDirectory ())) {
249
+ const std::string modDirectory = System::GetWorkingDirectory () + System::GetModDirectory ();
250
+ for (const std::filesystem::directory_entry &directoryEntry : std::filesystem::directory_iterator (modDirectory)) {
299
251
std::string zippedModulePath = std::filesystem::path (directoryEntry).generic_string ();
300
252
if (zippedModulePath.ends_with (System::GetZippedModulePackageExtension ())) {
301
253
LoadingScreen::LoadingSplashProgressReport (" Extracting Data Module from: " + directoryEntry.path ().filename ().generic_string (), true );
302
254
LoadingScreen::LoadingSplashProgressReport (System::ExtractZippedDataModule (zippedModulePath), true );
303
255
}
304
256
}
305
257
}
258
+
259
+ // ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
260
+
261
+ bool ModuleMan::IsModDisabled (const std::string &modModule) const {
262
+ return (m_DisabledMods.find (modModule) != m_DisabledMods.end ()) ? m_DisabledMods.at (modModule) : false ;
263
+ }
306
264
}
0 commit comments