1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
|
#pragma once
#include "ArchiveExtractor.h"
#include "FomodDB.h"
#include "stringutil.h"
#include "xml/ModuleConfiguration.h"
#include <QDir>
#include <QString>
#include <functional>
#include <imodinterface.h>
#include <imoinfo.h>
#include <nlohmann/json.hpp>
struct RescanResult {
int totalModsProcessed = 0;
int successfullyScanned = 0;
int missingArchives = 0;
int parseErrors = 0;
std::vector<std::string> failedMods;
};
/**
* Orchestrates rescanning of all mods with stored FOMOD choices to repopulate the database.
* Used when fomod.db is missing or needs to be regenerated from existing installations.
*/
class FomodRescan {
public:
using ProgressCallback = std::function<void(int current, int total, const QString& modName)>;
FomodRescan(MOBase::IOrganizer* organizer, FomodDB* db)
: mOrganizer(organizer), mFomodDb(db) {}
/**
* Scan all mods that have stored FOMOD Plus choices and repopulate the database.
* @param progressCallback Optional callback for progress updates
* @return RescanResult with statistics about the scan
*/
RescanResult scanAllModsWithChoices(const ProgressCallback& progressCallback = nullptr)
{
RescanResult result;
const auto modList = mOrganizer->modList();
if (!modList) {
return result;
}
// First pass: gather all mods with stored choices
std::vector<MOBase::IModInterface*> modsWithChoices;
for (const auto& modName : modList->allMods()) {
auto* mod = modList->getMod(modName);
if (mod && hasStoredChoices(mod)) {
modsWithChoices.push_back(mod);
}
}
result.totalModsProcessed = static_cast<int>(modsWithChoices.size());
// Second pass: process each mod
int current = 0;
for (auto* mod : modsWithChoices) {
current++;
if (progressCallback) {
progressCallback(current, result.totalModsProcessed, mod->name());
}
const auto scanResult = processMod(mod);
switch (scanResult) {
case ScanOutcome::Success:
result.successfullyScanned++;
break;
case ScanOutcome::MissingArchive:
result.missingArchives++;
result.failedMods.push_back(mod->name().toStdString() + " (missing archive)");
break;
case ScanOutcome::ParseError:
result.parseErrors++;
result.failedMods.push_back(mod->name().toStdString() + " (parse error)");
break;
case ScanOutcome::NoFomod:
result.failedMods.push_back(mod->name().toStdString() + " (no FOMOD)");
break;
}
}
// Save the database
mFomodDb->saveToFile();
return result;
}
private:
MOBase::IOrganizer* mOrganizer;
FomodDB* mFomodDb;
enum class ScanOutcome {
Success,
MissingArchive,
ParseError,
NoFomod
};
/**
* Check if a mod has stored FOMOD Plus choices (non-zero pluginSetting).
*/
bool hasStoredChoices(MOBase::IModInterface* mod) const
{
const auto fomodData = mod->pluginSetting(
StringConstants::Plugin::NAME.data(), "fomod", 0);
if (!fomodData.isValid() || fomodData.isNull()) {
return false;
}
// Check if it's actually valid JSON with steps
try {
const auto json = nlohmann::json::parse(fomodData.toString().toStdString());
return json.contains("steps") && json["steps"].is_array() && !json["steps"].empty();
} catch (...) {
return false;
}
}
/**
* Get the stored choices JSON from a mod's pluginSetting.
*/
nlohmann::json getStoredChoices(MOBase::IModInterface* mod) const
{
const auto fomodData = mod->pluginSetting(
StringConstants::Plugin::NAME.data(), "fomod", 0);
try {
return nlohmann::json::parse(fomodData.toString().toStdString());
} catch (...) {
return nlohmann::json();
}
}
/**
* Process a single mod: extract archive, parse FOMOD, create DB entry with selection states.
*/
ScanOutcome processMod(MOBase::IModInterface* mod)
{
// Get the archive path
const auto installationFile = mod->installationFile();
if (installationFile.isEmpty()) {
return ScanOutcome::MissingArchive;
}
const auto downloadsPath = mOrganizer->downloadsPath();
const auto archivePath = QDir(installationFile).isAbsolute()
? installationFile
: downloadsPath + "/" + installationFile;
if (!QFile::exists(archivePath)) {
return ScanOutcome::MissingArchive;
}
// Extract FOMOD data from archive
auto extractionResult = ArchiveExtractor::extractFomodData(archivePath);
if (!extractionResult.success) {
return ScanOutcome::ParseError;
}
// Parse ModuleConfiguration
auto moduleConfig = std::make_unique<ModuleConfiguration>();
try {
if (!moduleConfig->deserialize(extractionResult.moduleConfigPath)) {
return ScanOutcome::ParseError;
}
} catch (...) {
return ScanOutcome::ParseError;
}
// Get the mod's Nexus ID
const int modId = mod->nexusId();
// Create FomodDbEntry using existing logic
auto entry = FomodDB::getEntryFromFomod(
moduleConfig.get(),
extractionResult.pluginPaths,
modId
);
if (!entry || entry->getOptions().empty()) {
return ScanOutcome::NoFomod;
}
// Apply selection states from stored choices
const auto choices = getStoredChoices(mod);
applySelectionsToEntry(*entry, choices);
// Add to database (upsert)
mFomodDb->addEntry(entry, true);
return ScanOutcome::Success;
}
/**
* Apply user selection states to a FomodDbEntry based on stored choices JSON.
*
* Choices JSON format:
* {
* "steps": [{
* "name": "Step Name",
* "groups": [{
* "name": "Group Name",
* "plugins": ["Selected Plugin 1"],
* "deselected": ["Manually Deselected Plugin"]
* }]
* }]
* }
*/
void applySelectionsToEntry(FomodDbEntry& entry, const nlohmann::json& choices)
{
if (!choices.contains("steps") || !choices["steps"].is_array()) {
// No choices data - mark all as Available
for (auto& option : entry.getOptionsMutable()) {
option.selectionState = SelectionState::Available;
}
return;
}
// Build a lookup map for quick matching: stepName/groupName/pluginName -> state
struct PluginState {
bool selected = false;
bool deselected = false;
};
std::map<std::string, PluginState> stateMap;
for (const auto& step : choices["steps"]) {
if (!step.contains("name") || !step.contains("groups")) continue;
const std::string stepName = step["name"];
for (const auto& group : step["groups"]) {
if (!group.contains("name")) continue;
const std::string groupName = group["name"];
// Process selected plugins
if (group.contains("plugins") && group["plugins"].is_array()) {
for (const auto& plugin : group["plugins"]) {
const std::string pluginName = plugin;
const auto key = stepName + "/" + groupName + "/" + pluginName;
stateMap[key].selected = true;
}
}
// Process deselected plugins
if (group.contains("deselected") && group["deselected"].is_array()) {
for (const auto& plugin : group["deselected"]) {
const std::string pluginName = plugin;
const auto key = stepName + "/" + groupName + "/" + pluginName;
stateMap[key].deselected = true;
}
}
}
}
// Apply states to options
for (auto& option : entry.getOptionsMutable()) {
const auto key = option.step + "/" + option.group + "/" + option.name;
if (auto it = stateMap.find(key); it != stateMap.end()) {
if (it->second.selected) {
option.selectionState = SelectionState::Selected;
} else if (it->second.deselected) {
option.selectionState = SelectionState::Deselected;
} else {
option.selectionState = SelectionState::Available;
}
} else {
// Plugin not found in choices - mark as Available
option.selectionState = SelectionState::Available;
}
}
}
};
|