aboutsummaryrefslogtreecommitdiff
path: root/libs/installer_fomod_plus/share/xml/ModuleConfiguration.cpp
blob: 64d6b0b1be95f5bd7d360dcdb4f1ca96fc956ac2 (plain)
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
#include "ModuleConfiguration.h"

#include <format>

#include "XmlHelper.h"
#include "XmlParseException.h"
#include "stringutil.h"

#include <QFile>
#include <QString>

using namespace StringConstants::FomodFiles;

static GroupTypeEnum groupTypeFromString(const std::string& groupType)
{
    if (groupType == "SelectAny")
        return SelectAny;
    if (groupType == "SelectAll")
        return SelectAll;
    if (groupType == "SelectExactlyOne")
        return SelectExactlyOne;
    if (groupType == "SelectAtMostOne")
        return SelectAtMostOne;
    if (groupType == "SelectAtLeastOne")
        return SelectAtLeastOne;
    return SelectAny; // is this a sane default? probably
}

PluginTypeEnum pluginTypeFromString(const std::string& typeStr)
{
    if (typeStr == TYPE_REQUIRED)
        return PluginTypeEnum::Required;
    if (typeStr == TYPE_OPTIONAL)
        return PluginTypeEnum::Optional;
    if (typeStr == TYPE_RECOMMENDED)
        return PluginTypeEnum::Recommended;
    if (typeStr == TYPE_NOT_USABLE)
        return PluginTypeEnum::NotUsable;
    if (typeStr == TYPE_COULD_BE_USABLE)
        return PluginTypeEnum::CouldBeUsable;
    return PluginTypeEnum::Optional;
}

template <typename T>
bool deserializeList(pugi::xml_node& node, const char* childName, std::vector<T>& list)
{
    for (pugi::xml_node childNode : node.children(childName)) {
        T item;
        item.deserialize(childNode);
        list.push_back(item);
    }
    return true;
}

/*
 * NOTE: We call 'trim()' on all error-prone fields that rely on user-input. I'm assuming these FOMODs are created with
 * the FOMOD creation tool, so I won't trim things like flags that the tool sets for the user.
 */

bool FileDependency::deserialize(pugi::xml_node& node)
{
    file = node.attribute("file").as_string();
    trim(file);
    // ReSharper disable once CppTooWideScopeInitStatement
    // Do not use 'auto' for these. it will break equality checks
    const std::string stateStr = node.attribute("state").as_string();

    if (stateStr == "Missing")
        state = FileDependencyTypeEnum::Missing;
    else if (stateStr == "Inactive")
        state = FileDependencyTypeEnum::Inactive;
    else if (stateStr == "Active")
        state = FileDependencyTypeEnum::Active;
    return true;
}

bool FlagDependency::deserialize(pugi::xml_node& node)
{
    flag  = node.attribute("flag").as_string();
    value = node.attribute("value").as_string();
    trim({ flag, value });
    return true;
}

bool GameDependency::deserialize(pugi::xml_node& node)
{
    version = node.attribute("version").as_string();
    trim(version);
    return true;
}

bool CompositeDependency::deserialize(pugi::xml_node& node)
{

    // this could EITHER have a dependencies child or the dependencies are here.
    // turns out they could have both.
    pugi::xml_node possibleNode = node;

    // If the dependencies are all right inside, just use the root node as the dependency base.
    // This looks hacky but accommodates both _nested_ dependencies for plugins, and extremely simple ones for step visibility.
    if (node.child("dependencies") && !node.child("fileDependency") && !node.child("flagDependency") &&!node.child("gameDependency")) {
        possibleNode = node.child("dependencies");
    }

    deserializeList(possibleNode, "fileDependency", fileDependencies);
    deserializeList(possibleNode, "flagDependency", flagDependencies);
    deserializeList(possibleNode, "gameDependency", gameDependencies);
    deserializeList(possibleNode, "dependencies", nestedDependencies);

    operatorType = OperatorTypeEnum::AND; // safest default.

    if (const std::string operatorStr = possibleNode.attribute("operator").as_string(); operatorStr == "Or") {
        operatorType = OperatorTypeEnum::OR;
    }

    return true;
}

bool DependencyPattern::deserialize(pugi::xml_node& node)
{
    if (!node)
        return false;
    pugi::xml_node dependenciesNode = node.child("dependencies");
    dependencies.deserialize(dependenciesNode);

    const pugi::xml_node typeNode = node.child("type");
    type                          = pluginTypeFromString(typeNode.attribute("name").as_string());
    return true;
}

bool DependencyPatternList::deserialize(pugi::xml_node& node)
{
    return deserializeList(node, "pattern", patterns);
}

bool DependencyPluginType::deserialize(pugi::xml_node& node)
{
    pugi::xml_node patternsNode          = node.child("patterns");
    const pugi::xml_node defaultTypeNode = node.child("defaultType");
    defaultType                          = pluginTypeFromString(defaultTypeNode.attribute("name").as_string());
    patterns.deserialize(patternsNode);
    return true;
}

bool TypeDescriptor::deserialize(pugi::xml_node& node)
{
    pugi::xml_node dependencyTypeNode = node.child("dependencyType");
    dependencyType.deserialize(dependencyTypeNode);
    const pugi::xml_node typeNode = node.child("type");
    type                          = pluginTypeFromString(typeNode.attribute("name").as_string());
    return true;
}

bool Image::deserialize(pugi::xml_node& node)
{
    path = node.attribute("path").as_string();
    return true;
}

bool HeaderImage::deserialize(pugi::xml_node& node)
{
    path      = node.attribute("path").as_string();
    showImage = node.attribute("showImage").as_bool();
    showFade  = node.attribute("showFade").as_bool();
    height    = node.attribute("height").as_int();
    return true;
}

bool FileList::deserialize(pugi::xml_node& node)
{
    for (pugi::xml_node childNode : node.children()) {
        if (std::string(childNode.name()) == "folder"
            || std::string(childNode.name()) == "file") {
            File file;
            file.deserialize(childNode);
            files.emplace_back(file);
        }
    }
    return true;
}

bool ConditionalFileInstallPattern::deserialize(pugi::xml_node& node)
{
    pugi::xml_node dependenciesNode = node.child("dependencies");
    pugi::xml_node filesNode        = node.child("files");

    dependencies.deserialize(dependenciesNode);
    files.deserialize(filesNode);

    return true;
}

// <flag name="2">On</flag>
bool ConditionFlag::deserialize(pugi::xml_node& node)
{
    name  = node.attribute("name").as_string();
    value = node.child_value(); //
    return true;
}

bool ConditionFlagList::deserialize(pugi::xml_node& node)
{
    return deserializeList(node, "flag", flags);
}

bool File::deserialize(pugi::xml_node& node)
{
    source      = node.attribute("source").as_string();
    // destination = node.attribute("destination").as_string();
    priority    = node.attribute("priority").as_int();
    isFolder    = strcmp(node.name(), "folder") == 0;
    if (auto attr = node.attribute("destination"); attr) {
        destination = attr.as_string();
    } else {
        destination = std::nullopt;
    }
    return true;
}


bool Plugin::deserialize(pugi::xml_node& node)
{
    pugi::xml_node imageNode          = node.child("image");
    pugi::xml_node typeDescriptorNode = node.child("typeDescriptor");
    pugi::xml_node conditionFlagsNode = node.child("conditionFlags");
    pugi::xml_node filesNode          = node.child("files");

    // Description is optional in the schema; guard against null C strings from pugixml.
    if (const pugi::xml_node descNode = node.child("description")) {
        if (const char* rawDesc = descNode.text().as_string()) {
            description = rawDesc;
        }
    }
    description = trim(description); // Find a better way to do this eventually.
    image.deserialize(imageNode);
    typeDescriptor.deserialize(typeDescriptorNode);
    name = node.attribute("name").as_string();
    name = trim(name);
    conditionFlags.deserialize(conditionFlagsNode);
    files.deserialize(filesNode);
    return true;
}

bool PluginList::deserialize(pugi::xml_node& node)
{
    deserializeList(node, "plugin", plugins);
    order = XmlHelper::getOrderType(node.attribute("order").as_string(), OrderTypeEnum::Ascending);

    // Sort the plugins based on the specified order
    std::ranges::sort(plugins, [this](const Plugin& a, const Plugin& b) {
        if (order == OrderTypeEnum::Ascending) {
            return a.name < b.name;
        }
        if (order == OrderTypeEnum::Descending) {
            return a.name > b.name;
        }
        return false; // Default case, no sorting
    });

    return true;
}

bool Group::deserialize(pugi::xml_node& node)
{
    pugi::xml_node pluginsNode = node.child("plugins");
    plugins.deserialize(pluginsNode);
    name = node.attribute("name").as_string();
    type = groupTypeFromString(node.attribute("type").as_string());
    return true;
}

bool GroupList::deserialize(pugi::xml_node& node)
{
    deserializeList(node, "group", groups);
    order = XmlHelper::getOrderType(node.attribute("order").as_string());

    // Sort the groups based on the specified order
    std::ranges::sort(groups, [this](const Group& a, const Group& b) {
        if (order == OrderTypeEnum::Ascending) {
            return a.name < b.name;
        }
        if (order == OrderTypeEnum::Descending) {
            return a.name > b.name;
        }
        return false; // Default case, no sorting
    });
    return true;
}

bool InstallStep::deserialize(pugi::xml_node& node)
{
    pugi::xml_node visibleNode            = node.child("visible");
    pugi::xml_node optionalFileGroupsNode = node.child("optionalFileGroups");
    visible.deserialize(visibleNode);
    optionalFileGroups.deserialize(optionalFileGroupsNode);
    name = node.attribute("name").as_string();
    return true;
}

bool ConditionalFileInstall::deserialize(pugi::xml_node& node)
{
    pugi::xml_node patternsNode = node.child("patterns");
    deserializeList(patternsNode, "pattern", patterns);
    return true;
}

bool StepList::deserialize(pugi::xml_node& node)
{
    deserializeList(node, "installStep", installSteps);
    order = XmlHelper::getOrderType(node.attribute("order").as_string());
    return true;
}

bool ModuleConfiguration::deserialize(const QString& filePath)
{
    QFile file(filePath);
    if (!file.open(QIODevice::ReadOnly)) {
        throw XmlParseException(std::format("Failed to open file: {}", filePath.toStdString()));
    }
    const QByteArray content = file.readAll();
    file.close();

    pugi::xml_document doc;
    if (const pugi::xml_parse_result result = doc.load_buffer(content.constData(), content.size()); !result) {
        throw XmlParseException(std::format("XML parsed with errors: {}", result.description()));
    }

    const pugi::xml_node configNode = doc.child("config");
    if (!configNode) {
        throw XmlParseException("No <config> node found");
    }

    moduleName = configNode.child("moduleName").text().as_string();

    moduleImage                    = HeaderImage();
    pugi::xml_node moduleImageNode = configNode.child("moduleImage");
    moduleImage.deserialize(moduleImageNode);

    pugi::xml_node moduleDependenciesNode = configNode.child("moduleDependencies");
    moduleDependencies.deserialize(moduleDependenciesNode);

    pugi::xml_node requiredInstallFilesNode = configNode.child("requiredInstallFiles");
    requiredInstallFiles.deserialize(requiredInstallFilesNode);

    pugi::xml_node installStepsNode = configNode.child("installSteps");
    installSteps.deserialize(installStepsNode);

    pugi::xml_node conditionalFileInstallsNode = configNode.child("conditionalFileInstalls");
    conditionalFileInstalls.deserialize(conditionalFileInstallsNode);

    return true;
}