Skip to content

Commit 64f97ed

Browse files
committed
Refactor all the ModuleMan things and last bit of unclusterfucking (followup to 5f75ded)
1 parent bfa9498 commit 64f97ed

File tree

2 files changed

+128
-188
lines changed

2 files changed

+128
-188
lines changed

Managers/ModuleMan.cpp

Lines changed: 81 additions & 123 deletions
Original file line numberDiff line numberDiff line change
@@ -29,83 +29,69 @@ namespace RTE {
2929
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
3030

3131
void ModuleMan::Destroy() {
32-
for (const DataModule *dataModule : m_LoadedDataModules) {
32+
for (const auto &[moduleID, dataModule] : m_LoadedDataModules) {
3333
delete dataModule;
3434
}
3535
Clear();
3636
}
3737

3838
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
3939

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+
);
4246
}
4347

4448
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
4549

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;
5154
}
52-
}
53-
return false;
55+
);
5456
}
5557

5658
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
5759

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;
6268
}
6369
);
64-
return userdataModuleItr != c_UserdataModules.end();
70+
return (*loadedModulesItr).second;
6571
}
6672

6773
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
6874

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+
}
7579

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;
7986
}
8087

8188
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
8289

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();
10793
}
108-
return -1;
94+
return "";
10995
}
11096

11197
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@@ -132,15 +118,6 @@ namespace RTE {
132118
return moduleName;
133119
}
134120

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-
144121
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
145122

146123
std::string ModuleMan::GetFullModulePath(const std::string &modulePath) const {
@@ -158,73 +135,34 @@ namespace RTE {
158135
return (pathTopDir == moduleTopDir) ? modulePathGeneric : moduleTopDir + modulePathGeneric;
159136
}
160137

161-
162138
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
163139

164140
bool ModuleMan::LoadDataModule(const std::string &moduleName, DataModule::DataModuleType moduleType, const ProgressCallback &progressCallback) {
165141
if (moduleName.empty()) {
166142
return false;
167143
}
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);
171144

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;
177147
}
178148

179149
// Only instantiate it here, because it needs to be in the lists of this before being created.
180150
DataModule *newModule = new DataModule();
151+
newModule->m_ModuleType = moduleType;
152+
newModule->m_ModuleID = static_cast<int>(m_LoadedDataModules.size());
181153

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);
204155

205156
if (newModule->Create(moduleName, progressCallback) < 0) {
206157
RTEAbort("Failed to find the " + moduleName + " Data Module!");
207158
return false;
208159
}
209-
newModule = nullptr;
210160
return true;
211161
}
212162

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-
224163
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
225164

226165
bool ModuleMan::LoadAllDataModules() {
227-
// Destroy any possible loaded modules.
228166
Destroy();
229167

230168
LoadOfficialModules();
@@ -238,33 +176,24 @@ namespace RTE {
238176
}
239177
} else {
240178
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();
261180

262181
// Load userdata modules AFTER all other techs etc are loaded; might be referring to stuff in user mods.
263182
return LoadUserdataModules();
264183
}
265184
return true;
266185
}
267186

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+
268197
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
269198

270199
bool ModuleMan::LoadUserdataModules() {
@@ -290,17 +219,46 @@ namespace RTE {
290219
return true;
291220
}
292221

222+
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
293223

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+
}
294245

295246
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
296247

297248
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)) {
299251
std::string zippedModulePath = std::filesystem::path(directoryEntry).generic_string();
300252
if (zippedModulePath.ends_with(System::GetZippedModulePackageExtension())) {
301253
LoadingScreen::LoadingSplashProgressReport("Extracting Data Module from: " + directoryEntry.path().filename().generic_string(), true);
302254
LoadingScreen::LoadingSplashProgressReport(System::ExtractZippedDataModule(zippedModulePath), true);
303255
}
304256
}
305257
}
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+
}
306264
}

0 commit comments

Comments
 (0)