summaryrefslogtreecommitdiff
path: root/src/organizercore.h
blob: 3e0cdbba5e31e6df8bc52064477c21e24e18f14d (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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
#ifndef ORGANIZERCORE_H
#define ORGANIZERCORE_H

#include "downloadmanager.h"
#include "envdump.h"
#include "executableinfo.h"
#include "executableslist.h"
#include "guessedvalue.h"
#include "installationmanager.h"
#include "memoizedlock.h"
#include "moddatacontent.h"
#include "modinfo.h"
#include "modlist.h"
#include "moshortcut.h"
#include "pluginlist.h"
#include "processrunner.h"
#include "selfupdater.h"
#include "settings.h"
#include "uilocker.h"
#include "usvfsconnector.h"
#include <boost/signals2.hpp>
#include <delayedfilewriter.h>
#include <imoinfo.h>
#include <iplugindiagnose.h>
#include <log.h>
#include <versioninfo.h>

#include <QDir>
#include <QFileInfo>
#include <QList>
#include <QObject>
#include <QSettings>
#include <QString>
#include <QStringList>
#include <QThread>
#include <QVariant>

class ModListSortProxy;
class PluginListSortProxy;
class Profile;
class IUserInterface;
class GameFeatures;
class PluginContainer;
class DirectoryRefresher;

namespace MOBase
{
template <typename T>
class GuessedValue;
class IModInterface;
class IPluginGame;
}  // namespace MOBase

namespace MOShared
{
class DirectoryEntry;
}

class OrganizerCore : public QObject, public MOBase::IPluginDiagnose
{

  Q_OBJECT
  Q_INTERFACES(MOBase::IPluginDiagnose)

private:
  friend class OrganizerProxy;

  struct SignalCombinerAnd
  {
    using result_type = bool;

    template <typename InputIterator>
    bool operator()(InputIterator first, InputIterator last) const
    {
      while (first != last) {
        if (!(*first)) {
          return false;
        }
        ++first;
      }
      return true;
    }
  };

private:
  using SignalAboutToRunApplication =
      boost::signals2::signal<bool(const QString&, const QDir&, const QString&),
                              SignalCombinerAnd>;
  using SignalFinishedRunApplication =
      boost::signals2::signal<void(const QString&, unsigned int)>;
  using SignalUserInterfaceInitialized = boost::signals2::signal<void(QMainWindow*)>;
  using SignalProfileCreated = boost::signals2::signal<void(MOBase::IProfile*)>;
  using SignalProfileRenamed =
      boost::signals2::signal<void(MOBase::IProfile*, QString const&, QString const&)>;
  using SignalProfileRemoved = boost::signals2::signal<void(QString const&)>;
  using SignalProfileChanged =
      boost::signals2::signal<void(MOBase::IProfile*, MOBase::IProfile*)>;
  using SignalPluginSettingChanged = boost::signals2::signal<void(
      QString const&, const QString& key, const QVariant&, const QVariant&)>;
  using SignalPluginEnabled = boost::signals2::signal<void(const MOBase::IPlugin*)>;

public:
  /**
   * Small holder for the game content returned by the ModDataContent feature (the
   * list of all possible contents, not the per-mod content).
   */
  struct ModDataContentHolder
  {

    using Content = MOBase::ModDataContent::Content;

    /**
     * @return true if the hold list of contents is empty, false otherwise.
     */
    bool empty() const { return m_Contents.empty(); }

    /**
     * @param id ID of the content to retrieve.
     *
     * @return the content with the given ID, or a null pointer if it is not found.
     */
    const Content* findById(int id) const
    {
      auto it = std::find_if(std::begin(m_Contents), std::end(m_Contents),
                             [&id](auto const& content) {
                               return content.id() == id;
                             });
      return it == std::end(m_Contents) ? nullptr : &(*it);
    }

    /**
     * Apply the given function to each content whose ID is in the given set.
     *
     * @param ids The set of content IDs.
     * @param fn The function to apply.
     * @param includeFilter true to also apply the function to filter-only contents,
     * false otherwise.
     */
    template <class Fn>
    void forEachContentIn(std::set<int> const& ids, Fn const& fn,
                          bool includeFilter = false) const
    {
      for (const auto& content : m_Contents) {
        if ((includeFilter || !content.isOnlyForFilter()) &&
            ids.find(content.id()) != ids.end()) {
          fn(content);
        }
      }
    }

    /**
     * @brief Apply fnIn to each content whose ID is in the given set, and fnOut to each
     * content not in the given set, excluding filter-only content (from both cases)
     * unless includeFilter is true.
     *
     * @param ids The set of content IDs.
     * @param fnIn Function to apply to content whose IDs are in ids.
     * @param fnOut Function to apply to content whose IDs are not in ids.
     * @param includeFilter true to also apply the function to filter-only contents,
     * false otherwise.
     */
    template <class FnIn, class FnOut>
    void forEachContentInOrOut(std::set<int> const& ids, FnIn const& fnIn,
                               FnOut const& fnOut, bool includeFilter = false) const
    {
      for (const auto& content : m_Contents) {
        if ((includeFilter || !content.isOnlyForFilter())) {
          if (ids.find(content.id()) != ids.end()) {
            fnIn(content);
          } else {
            fnOut(content);
          }
        }
      }
    }

    /**
     * Apply the given function to each content.
     *
     * @param fn The function to apply.
     * @param includeFilter true to also apply the function to filter-only contents,
     * false otherwise.
     */
    template <class Fn>
    void forEachContent(Fn const& fn, bool includeFilter = false) const
    {
      for (const auto& content : m_Contents) {
        if (includeFilter || !content.isOnlyForFilter()) {
          fn(content);
        }
      }
    }

    ModDataContentHolder& operator=(ModDataContentHolder const&) = delete;
    ModDataContentHolder& operator=(ModDataContentHolder&&)      = default;

  private:
    std::vector<Content> m_Contents;

    /**
     * @brief Construct a ModDataContentHolder without any contents (e.g., if the
     * feature is missing).
     */
    ModDataContentHolder() {}

    /**
     * @brief Construct a ModDataContentHold holding the given list of contents.
     */
    ModDataContentHolder(std::vector<MOBase::ModDataContent::Content> contents)
        : m_Contents(std::move(contents))
    {}

    friend class OrganizerCore;
  };

  // enumeration for the mode when adding refresh callbacks
  //
  enum class RefreshCallbackMode : int
  {
    // run the callbacks immediately if no refresh is running
    RUN_NOW_IF_POSSIBLE = 0,

    // wait for the next refresh if none is running
    FORCE_WAIT_FOR_REFRESH = 1
  };

  // enumeration for the groups where refresh callbacks can be put
  //
  enum class RefreshCallbackGroup : int
  {
    // for callbacks by the core itself, highest priority
    CORE = 0,

    // internal MO2 callbacks
    INTERNAL = 1,

    // external callbacks, typically MO2 plugins
    EXTERNAL = 2
  };

public:
  OrganizerCore(Settings& settings);

  ~OrganizerCore();

  void setUserInterface(IUserInterface* ui);
  void connectPlugins(PluginContainer* container);

  void setManagedGame(MOBase::IPluginGame* game);

  void updateExecutablesList();
  void updateModInfoFromDisc();

  void checkForUpdates();
  void startMOUpdate();

  Settings& settings();
  SelfUpdater* updater() { return &m_Updater; }
  InstallationManager* installationManager();
  MOShared::DirectoryEntry* directoryStructure() { return m_DirectoryStructure; }
  DirectoryRefresher* directoryRefresher() { return m_DirectoryRefresher.get(); }
  ExecutablesList* executablesList() { return &m_ExecutablesList; }
  void setExecutablesList(const ExecutablesList& executablesList)
  {
    m_ExecutablesList = executablesList;
  }

  Profile* currentProfile() const { return m_CurrentProfile.get(); }
  void setCurrentProfile(const QString& profileName);

  std::vector<QString> enabledArchives();

  MOBase::VersionInfo getVersion() const { return m_Updater.getVersion(); }

  // return the plugin container
  //
  PluginContainer& pluginContainer() const;

  // return the game features
  GameFeatures& gameFeatures() const;

  MOBase::IPluginGame const* managedGame() const;

  /**
   * @brief Retrieve the organizer proxy of the currently managed game.
   *
   */
  MOBase::IOrganizer const* managedGameOrganizer() const;

  /**
   * @return the list of contents for the currently managed game, or an empty vector
   *     if the game plugin does not implement the ModDataContent feature.
   */
  const ModDataContentHolder& modDataContents() const { return m_Contents; }

  bool isArchivesInit() const { return m_ArchivesInit; }

  bool saveCurrentLists();

  ProcessRunner processRunner();

  bool beforeRun(const QFileInfo& binary, const QDir& cwd, const QString& arguments,
                 const QString& profileName, const QString& customOverwrite,
                 const QList<MOBase::ExecutableForcedLoadSetting>& forcedLibraries);

  void afterRun(const QFileInfo& binary, DWORD exitCode);

  ProcessRunner::Results
  waitForAllUSVFSProcesses(UILocker::Reasons reason = UILocker::PreventExit);

  void refreshESPList(bool force = false);
  void refreshBSAList();

  void refreshDirectoryStructure();
  void updateModInDirectoryStructure(unsigned int index, ModInfo::Ptr modInfo);
  void updateModsInDirectoryStructure(QMap<unsigned int, ModInfo::Ptr> modInfos);

  void doAfterLogin(const std::function<void()>& function)
  {
    m_PostLoginTasks.append(function);
  }
  void loggedInAction(QWidget* parent, std::function<void()> f);

  bool previewFileWithAlternatives(QWidget* parent, QString filename,
                                   int selectedOrigin = -1);
  bool previewFile(QWidget* parent, const QString& originName, const QString& path);

  void loginSuccessfulUpdate(bool necessary);
  void loginFailedUpdate(const QString& message);

  static bool createAndMakeWritable(const QString& path);
  bool checkPathSymlinks();
  bool bootstrap();
  void createDefaultProfile();
  void createOverwriteDirectories();

  MOBase::DelayedFileWriter& pluginsWriter() { return m_PluginListsWriter; }

  void prepareVFS();

  void updateVFSParams(MOBase::log::Levels logLevel, env::CoreDumpTypes coreDumpType,
                       const QString& coreDumpsPath, std::chrono::seconds spawnDelay,
                       QString executableBlacklist, const QStringList& skipFileSuffixes,
                       const QStringList& skipDirectories);

  void setLogLevel(MOBase::log::Levels level);

  bool cycleDiagnostics();

  static env::CoreDumpTypes getGlobalCoreDumpType();
  static void setGlobalCoreDumpType(env::CoreDumpTypes type);
  static std::wstring getGlobalCoreDumpPath();

public:
  MOBase::IModRepositoryBridge* createNexusBridge() const;
  QString profileName() const;
  QString profilePath() const;
  QString downloadsPath() const;
  QString overwritePath() const;
  QString basePath() const;
  QString modsPath() const;
  MOBase::VersionInfo appVersion() const;
  MOBase::IPluginGame* getGame(const QString& gameName) const;
  MOBase::IModInterface* createMod(MOBase::GuessedValue<QString>& name);
  void modDataChanged(MOBase::IModInterface* mod);
  QVariant pluginSetting(const QString& pluginName, const QString& key) const;
  void setPluginSetting(const QString& pluginName, const QString& key,
                        const QVariant& value);
  QVariant persistent(const QString& pluginName, const QString& key,
                      const QVariant& def) const;
  void setPersistent(const QString& pluginName, const QString& key,
                     const QVariant& value, bool sync);
  static QString pluginDataPath();
  virtual MOBase::IModInterface* installMod(const QString& fileName, int priority,
                                            bool reinstallation,
                                            ModInfo::Ptr currentMod,
                                            const QString& initModName);
  QString resolvePath(const QString& fileName) const;
  QStringList listDirectories(const QString& directoryName) const;
  QStringList findFiles(const QString& path,
                        const std::function<bool(const QString&)>& filter) const;
  QStringList getFileOrigins(const QString& fileName) const;
  QList<MOBase::IOrganizer::FileInfo> findFileInfos(
      const QString& path,
      const std::function<bool(const MOBase::IOrganizer::FileInfo&)>& filter) const;
  DownloadManager* downloadManager();
  PluginList* pluginList();
  ModList* modList();
  void refresh(bool saveChanges = true);

  boost::signals2::connection onAboutToRun(
      const std::function<bool(const QString&, const QDir&, const QString&)>& func);
  boost::signals2::connection
  onFinishedRun(const std::function<void(const QString&, unsigned int)>& func);
  boost::signals2::connection
  onUserInterfaceInitialized(std::function<void(QMainWindow*)> const& func);
  boost::signals2::connection
  onProfileCreated(std::function<void(MOBase::IProfile*)> const& func);
  boost::signals2::connection onProfileRenamed(
      std::function<void(MOBase::IProfile*, QString const&, QString const&)> const&
          func);
  boost::signals2::connection
  onProfileRemoved(std::function<void(QString const&)> const& func);
  boost::signals2::connection onProfileChanged(
      std::function<void(MOBase::IProfile*, MOBase::IProfile*)> const& func);
  boost::signals2::connection onPluginSettingChanged(
      std::function<void(QString const&, const QString& key, const QVariant&,
                         const QVariant&)> const& func);
  boost::signals2::connection
  onPluginEnabled(std::function<void(const MOBase::IPlugin*)> const& func);
  boost::signals2::connection
  onPluginDisabled(std::function<void(const MOBase::IPlugin*)> const& func);

  // add a function to be called after the next refresh is done
  //
  // - group to add the function to
  // - if immediateIfReady is true, the function will be called immediately if no
  //   directory update is running
  boost::signals2::connection onNextRefresh(std::function<void()> const& func,
                                            RefreshCallbackGroup group,
                                            RefreshCallbackMode mode);

public:  // IPluginDiagnose interface
  virtual std::vector<unsigned int> activeProblems() const;
  virtual QString shortDescription(unsigned int key) const;
  virtual QString fullDescription(unsigned int key) const;
  virtual bool hasGuidedFix(unsigned int key) const;
  virtual void startGuidedFix(unsigned int key) const;

public slots:

  void syncOverwrite();

  void savePluginList();

  void refreshLists();

  ModInfo::Ptr installDownload(int downloadIndex, int priority = -1);
  ModInfo::Ptr installArchive(const QString& archivePath, int priority = -1,
                              bool reinstallation     = false,
                              ModInfo::Ptr currentMod = nullptr,
                              const QString& modName  = QString());

  void modPrioritiesChanged(QModelIndexList const& indexes);
  void modStatusChanged(unsigned int index);
  void modStatusChanged(QList<unsigned int> index);
  void requestDownload(const QUrl& url, QNetworkReply* reply);
  void downloadRequestedNXM(const QString& url);

  void userInterfaceInitialized();

  void profileCreated(MOBase::IProfile* profile);
  void profileRenamed(MOBase::IProfile* profile, QString const& oldName,
                      QString const& newName);
  void profileRemoved(QString const& profileName);

  bool nexusApi(bool retry = false);

signals:

  // emitted after a mod has been installed
  //
  void modInstalled(const QString& modName);

  // emitted when the managed game changes
  //
  void managedGameChanged(MOBase::IPluginGame const* gamePlugin);

  // emitted when the profile is changed, before notifying plugins
  //
  // the new profile can be stored but the old one is temporary and
  // should not be
  //
  void profileChanged(Profile* oldProfile, Profile* newProfile);

  // Notify that the directory structure is ready to be used on the main thread
  // Use queued connections
  void directoryStructureReady();

  // Notify of a general UI refresh
  void refreshTriggered();

private:
  std::pair<unsigned int, ModInfo::Ptr> doInstall(const QString& archivePath,
                                                  MOBase::GuessedValue<QString> modName,
                                                  ModInfo::Ptr currentMod, int priority,
                                                  bool reinstallation);

  void saveCurrentProfile();
  void storeSettings();

  void updateModActiveState(int index, bool active);
  void updateModsActiveState(const QList<unsigned int>& modIndices, bool active);

  // clear the conflict caches of all the given mods, and the mods in conflict
  // with the given mods
  //
  void clearCaches(std::vector<unsigned int> const& indices) const;

  bool createDirectory(const QString& path);

  QString oldMO1HookDll() const;

  /**
   * @brief return a descriptor of the mappings real file->virtual file
   */
  std::vector<Mapping> fileMapping(const QString& profile,
                                   const QString& customOverwrite);

  std::vector<Mapping> fileMapping(const QString& dataPath, const QString& relPath,
                                   const MOShared::DirectoryEntry* base,
                                   const MOShared::DirectoryEntry* directoryEntry,
                                   int createDestination);

private slots:

  void onDirectoryRefreshed();
  void downloadRequested(QNetworkReply* reply, QString gameName, int modID,
                         const QString& fileName);
  void removeOrigin(const QString& name);
  void downloadSpeed(const QString& serverName, int bytesPerSecond);
  void loginSuccessful(bool necessary);
  void loginFailed(const QString& message);

private:
  static const unsigned int PROBLEM_MO1SCRIPTEXTENDERWORKAROUND = 1;

private:
  IUserInterface* m_UserInterface;
  PluginContainer* m_PluginContainer;
  QString m_GameName;
  MOBase::IPluginGame* m_GamePlugin;
  ModDataContentHolder m_Contents;

  std::unique_ptr<Profile> m_CurrentProfile;

  Settings& m_Settings;

  SelfUpdater m_Updater;

  SignalAboutToRunApplication m_AboutToRun;
  SignalFinishedRunApplication m_FinishedRun;
  SignalUserInterfaceInitialized m_UserInterfaceInitialized;
  SignalProfileCreated m_ProfileCreated;
  SignalProfileRenamed m_ProfileRenamed;
  SignalProfileRemoved m_ProfileRemoved;
  SignalProfileChanged m_ProfileChanged;
  SignalPluginSettingChanged m_PluginSettingChanged;
  SignalPluginEnabled m_PluginEnabled;
  SignalPluginEnabled m_PluginDisabled;

  boost::signals2::signal<void()> m_OnNextRefreshCallbacks;

  ModList m_ModList;
  PluginList m_PluginList;

  QList<std::function<void()>> m_PostLoginTasks;

  ExecutablesList m_ExecutablesList;
  QStringList m_PendingDownloads;
  QStringList m_DefaultArchives;
  QStringList m_ActiveArchives;

  std::unique_ptr<DirectoryRefresher> m_DirectoryRefresher;
  MOShared::DirectoryEntry* m_DirectoryStructure;
  MOBase::MemoizedLocked<std::shared_ptr<const MOBase::IFileTree>> m_VirtualFileTree;

  DownloadManager m_DownloadManager;
  InstallationManager m_InstallationManager;

  QThread m_RefresherThread;

  std::thread m_StructureDeleter;

  std::atomic<bool> m_DirectoryUpdate;
  bool m_ArchivesInit;

  MOBase::DelayedFileWriter m_PluginListsWriter;
  UsvfsConnector m_USVFS;

  UILocker m_UILocker;
};

#endif  // ORGANIZERCORE_H