summaryrefslogtreecommitdiff
path: root/src/profile.h
blob: 31a1fa1748d5ec2fbd570fc7d8704e270c87bc3b (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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
/*
Copyright (C) 2012 Sebastian Herbord. All rights reserved.

This file is part of Mod Organizer.

Mod Organizer is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

Mod Organizer is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Mod Organizer.  If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef PROFILE_H
#define PROFILE_H

#include "executableinfo.h"
#include "modinfo.h"
#include <delayedfilewriter.h>
#include <iprofile.h>

#include <QByteArray>
#include <QDir>
#include <QList>
#include <QObject>
#include <QSettings>
#include <QString>

#include <boost/shared_ptr.hpp>

#include <string>
#include <tuple>
#include <vector>

namespace MOBase
{
class IPluginGame;
}

class GameFeatures;

/**
 * @brief represents a profile
 **/
class Profile : public QObject, public MOBase::IProfile
{

  Q_OBJECT

public:
  using Ptr = boost::shared_ptr<Profile>;

public:
  // the minimum and maximum priority achievable by mods
  //
  static constexpr int MinimumPriority = 0;
  static constexpr int MaximumPriority = std::numeric_limits<int>::max();

public:
  /**
   * @brief constructor
   *
   * This constructor is used to create a new profile so it is to be assumed a profile
   * by this name does not yet exist
   * @param name name of the new profile
   * @param filter save game filter. Defaults to &lt;no filter&gt;.
   **/
  Profile(const QString& name, MOBase::IPluginGame const* gamePlugin,
          GameFeatures const& features, bool useDefaultSettings);

  /**
   * @brief constructor
   *
   * This constructor is used to open an existing profile though it will also try to
   *repair the profile if important files are missing (including the directory itself)
   *so technically, invoking this should always produce a working profile
   * @param directory directory to read the profile from
   **/
  Profile(const QDir& directory, MOBase::IPluginGame const* gamePlugin,
          GameFeatures const& features);

  Profile(const Profile& reference);

  ~Profile();

  /**
   * Determines the default settings for the profile based on the current state of the
   *profile's files.  This function should remain backwards compatible as much as
   *possible.
   **/

  void findProfileSettings(void);

  /**
   * @return true if this profile (still) exists on disc
   */
  bool exists() const;

  /**
   * @param name of the new profile
   * @param reference profile to copy from
   **/
  static Profile* createPtrFrom(const QString& name, const Profile& reference,
                                MOBase::IPluginGame const* gamePlugin);

  static void renameModInAllProfiles(const QString& oldName, const QString& newName);

  void writeModlist();

  void writeModlistNow(bool onlyIfPending = false);

  void cancelModlistWrite();

  /**
   * @brief test if this profile uses archive invalidation
   *
   * @param supported if this is not null, the parameter will be set to false if
   *invalidation is not supported in this profile
   * @return true if archive invalidation is active
   * @note currently, invalidation is not supported if the relevant entry in the ini
   *file does not exist
   **/
  bool invalidationActive(bool* supported) const;

  /**
   * @brief deactivate archive invalidation if it was active
   **/
  void deactivateInvalidation();

  /**
   * @brief activate archive invalidation
   **/
  void activateInvalidation();

  /**
   * @return true if this profile uses local save games
   */
  virtual bool localSavesEnabled() const override;

  /**
   * @brief enables or disables the use of local save games for this profile
   * when disabling the user will be asked if he wants to remove the save games
   * in the profile
   * @param enable if true, local saves are enabled, otherewise they are disabled
   */
  bool enableLocalSaves(bool enable);

  /**
   * @return true if this profile uses local ini files
   */
  virtual bool localSettingsEnabled() const override;

  /**
   * @brief enables or disables the use of local ini files for this profile
   * disabling this does not delete existing ini files but the global ones will be used
   * @param enable
   */
  bool enableLocalSettings(bool enable);

  /**
   * @return name of the profile (this is identical to its directory name)
   **/
  virtual QString name() const override { return m_Directory.dirName(); }

  /**
   * @return the path of the plugins file in this profile
   * @todo is this required? can the functionality using this function be moved to the
   *Profile-class?
   **/
  QString getPluginsFileName() const;

  /**
   * @return the path of the loadorder file in this profile
   **/
  QString getLoadOrderFileName() const;

  /**
   * @return the path of the file containing locked mod indices
   */
  QString getLockedOrderFileName() const;

  /**
   * @return the path of the modlist file in this profile
   */
  QString getModlistFileName() const;

  /**
   * @return path of the archives file in this profile
   */
  QString getArchivesFileName() const;

  /**
   * @return the path of the ini file in this profile
   * @todo since the game can contain multiple ini files (i.e. skyrim.ini
   *skyrimprefs.ini) the concept of this function is somewhat broken
   **/
  QString getIniFileName() const;

  /**
   * @brief Retrieve the absolute file to the corresponding file.
   *
   * @param iniFile INI file to retrieve a path for. This can either be the
   *     name of a file or a path to the absolute file outside of the profile.
   *
   * @return the absolute path for the given INI file for this profile.
   */
  QString absoluteIniFilePath(QString iniFile) const override;

  /**
   * @return the path of the tweak ini in this profile
   */
  QString getProfileTweaks() const;

  /**
   * @return path to this profile
   **/
  virtual QString absolutePath() const override;

  /**
   * @return path to this profile's save games
   **/
  QString savePath() const;

  /**
   * @brief rename profile
   * @param newName new name of profile
   */
  void rename(const QString& newName);

  /**
   * @brief create the ini file to be used by the game
   *
   * the tweaked ini file constructed by this file is a merger
   * of the game-ini of this profile with ini tweaks applied */
  void createTweakedIniFile();

  /**
   * @brief re-read the modlist.txt and update the mod status from it
   **/
  void refreshModStatus();

  /**
   * @brief retrieve a list of mods that are enabled in this profile
   *
   * @return list of active mods sorted by priority (ascending). "first" is the mod
   *name, "second" is its path
   **/
  std::vector<std::tuple<QString, QString, int>> getActiveMods();

  /**
   * @brief retrieve a mod of the indexes ordered by priority
   *
   * @return map of indexes by priority
   **/
  const std::map<int, unsigned int>& getAllIndexesByPriority()
  {
    return m_ModIndexByPriority;
  }

  /**
   * retrieve the number of mods for which this object has status information.
   * This is usually the same as ModInfo::getNumMods() except between
   * calls to ModInfo::updateFromDisc() and the Profile::refreshModStatus()
   *
   * @return number of mods for which the profile has status information
   **/
  size_t numMods() const { return m_ModStatus.size(); }

  /**
   * @brief enable or disable a mod
   *
   * @param index index of the mod to enable/disable
   * @param enabled true if the mod is to be enabled, false if it is to be disabled
   **/
  void setModEnabled(unsigned int index, bool enabled);

  /**
   * @brief enable or disable multiple mods at once
   * This is an abbreviated process and should be immediately followed by a full refresh
   * to maintain data consistency.
   *
   * @param modsToEnable list of mod indicies to enable
   * @param modsToDisable list of mod indicies to disable
   **/
  void setModsEnabled(const QList<unsigned int>& modsToEnable,
                      const QList<unsigned int>& modsToDisable);

  // set the priority of a mod, and the priority of other mods in the range
  // [old priority, new priority] such that no gaps are possible
  //
  // the priority is clamped in the range of valid priority (>= 0, and lower than
  // the number of "regular" mods), you should use MinimumPriority or MaximumPriority
  // to send a mod to the "top" or "bottom" of the priority list
  //
  // the function returns true if the priority was changed, or false if the mod
  // was already at the given priority (or if the priority of the mod cannot be
  // set)
  //
  bool setModPriority(unsigned int index, int& newPriority);

  /**
   * @brief determine if a mod is enabled
   *
   * @param index index of the mod to look up
   * @return true if the mod is enabled, false otherwise
   **/
  bool modEnabled(unsigned int index) const;

  /**
   * @brief query the priority of a mod
   *
   * @param index index of the mod to look up
   * @return priority of the specified mod
   **/
  int getModPriority(unsigned int index) const;

  void dumpModStatus() const;

  QVariant setting(const QString& section, const QString& name,
                   const QVariant& fallback = {}) const;

  void storeSetting(const QString& section, const QString& name,
                    const QVariant& value = {});

  void removeSetting(const QString& section, const QString& name);

  QVariantMap settingsByGroup(const QString& section) const;
  void storeSettingsByGroup(const QString& section, const QVariantMap& values);

  QList<QVariantMap> settingsByArray(const QString& prefix) const;
  void storeSettingsByArray(const QString& prefix, const QList<QVariantMap>& values);

  bool forcedLibrariesEnabled(const QString& executable) const;
  void setForcedLibrariesEnabled(const QString& executable, bool enabled);
  QList<MOBase::ExecutableForcedLoadSetting>
  determineForcedLibraries(const QString& executable) const;
  void storeForcedLibraries(const QString& executable,
                            const QList<MOBase::ExecutableForcedLoadSetting>& values);
  void removeForcedLibraries(const QString& executable);

  void debugDump() const;

  Profile& operator=(const Profile& reference) = delete;

signals:

  /**
   * @brief emitted whenever the status (enabled/disabled) of a mod changed
   *
   * @param index index of the mod that changed
   **/
  void modStatusChanged(unsigned int index);

  /**
   * @brief emitted whenever the status (enabled/disabled) of multiple mods change
   *
   * @param index list of indices of the mods that changed
   **/
  void modStatusChanged(QList<unsigned int> index);

protected slots:

  // should only be called by DelayedFileWriter, use writeModlist() and
  // writeModlistNow() instead
  void doWriteModlist();

private:
  class ModStatus
  {
    friend class Profile;

  public:
    ModStatus() : m_Enabled(false), m_Priority(-1) {}

  private:
    bool m_Enabled;
    int m_Priority;
  };

private:
  void updateIndices();

  void copyFilesTo(QString& target) const;

  std::vector<std::wstring> splitDZString(const wchar_t* buffer) const;
  void mergeTweak(const QString& tweakName, const QString& tweakedIni) const;
  void mergeTweaks(ModInfo::Ptr modInfo, const QString& tweakedIni) const;
  void touchFile(QString fileName);

  static void renameModInList(QFile& modList, const QString& oldName,
                              const QString& newName);

private:
  QDir m_Directory;

  QSettings* m_Settings;

  const GameFeatures& m_GameFeatures;
  const MOBase::IPluginGame* m_GamePlugin;

  std::vector<ModStatus> m_ModStatus;
  std::map<int, unsigned int> m_ModIndexByPriority;

  // "regular" here means mods with modifiable priority (i.e. not backups
  // or overwrite)
  std::size_t m_NumRegularMods;

  MOBase::DelayedFileWriter m_ModListWriter;
};

#endif  // PROFILE_H