summaryrefslogtreecommitdiff
path: root/src/directoryrefresher.cpp
blob: bce3f65af7b375e3cace2874bc384977275ef1a0 (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
/*
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/>.
*/

#include "directoryrefresher.h"

#include "iplugingame.h"
#include "utility.h"
#include "report.h"
#include "modinfo.h"
#include "settings.h"
#include "envfs.h"
#include "modinfodialogfwd.h"

#include <QApplication>
#include <QDir>
#include <QString>
#include <QTextCodec>
#include <gameplugins.h>


using namespace MOBase;
using namespace MOShared;


DirectoryRefresher::DirectoryRefresher(std::size_t threadCount)
  : m_DirectoryStructure(nullptr), m_threadCount(threadCount)
{
}

DirectoryRefresher::~DirectoryRefresher()
{
  delete m_DirectoryStructure;
}

DirectoryEntry *DirectoryRefresher::getDirectoryStructure()
{
  QMutexLocker locker(&m_RefreshLock);
  DirectoryEntry *result = m_DirectoryStructure;
  m_DirectoryStructure = nullptr;
  return result;
}

void DirectoryRefresher::setMods(const std::vector<std::tuple<QString, QString, int> > &mods
                                 , const std::set<QString> &managedArchives)
{
  QMutexLocker locker(&m_RefreshLock);

  m_Mods.clear();
  for (auto mod = mods.begin(); mod != mods.end(); ++mod) {
    QString name = std::get<0>(*mod);
    ModInfo::Ptr info = ModInfo::getByIndex(ModInfo::getIndex(name));
    m_Mods.push_back(EntryInfo(name, std::get<1>(*mod), info->stealFiles(), info->archives(), std::get<2>(*mod)));
  }

  m_EnabledArchives = managedArchives;
}

void DirectoryRefresher::cleanStructure(DirectoryEntry *structure)
{
  static const wchar_t *files[] = { L"meta.ini", L"readme.txt" };
  for (int i = 0; i < sizeof(files) / sizeof(wchar_t*); ++i) {
    structure->removeFile(files[i]);
  }

  static const wchar_t *dirs[] = { L"fomod" };
  for (int i = 0; i < sizeof(dirs) / sizeof(wchar_t*); ++i) {
    structure->removeDir(std::wstring(dirs[i]));
  }
}

void DirectoryRefresher::addModBSAToStructure(DirectoryEntry *directoryStructure, const QString &modName,
                                              int priority, const QString &directory, const QStringList &archives)
{
  std::wstring directoryW = ToWString(QDir::toNativeSeparators(directory));
  IPluginGame *game = qApp->property("managed_game").value<IPluginGame*>();

  GamePlugins *gamePlugins = game->feature<GamePlugins>();
  QStringList loadOrder = QStringList();
  gamePlugins->getLoadOrder(loadOrder);

  for (const QString &archive : archives) {
    QFileInfo fileInfo(archive);
    if (m_EnabledArchives.find(fileInfo.fileName()) != m_EnabledArchives.end()) {

      int order = -1;

      for (auto plugin : loadOrder)
      {
        QString name = plugin.left(plugin.size() - 4);
        if (fileInfo.fileName().startsWith(name + " - ", Qt::CaseInsensitive) || fileInfo.fileName().startsWith(name + ".", Qt::CaseInsensitive)) {
          order = loadOrder.indexOf(plugin);
        }
      }

      try {
        IPluginGame *game = qApp->property("managed_game").value<IPluginGame*>();
        directoryStructure->addFromBSA(ToWString(modName), directoryW, ToWString(QDir::toNativeSeparators(fileInfo.absoluteFilePath())), priority, order);
      } catch (const std::exception &e) {
        throw MyException(tr("failed to parse bsa %1: %2").arg(archive, e.what()));
      }
    }
  }
}

void DirectoryRefresher::stealModFilesIntoStructure(
  DirectoryEntry *directoryStructure, const QString &modName,
  int priority, const QString &directory, const QStringList &stealFiles)
{
  std::wstring directoryW = ToWString(QDir::toNativeSeparators(directory));

  // instead of adding all the files of the target directory, we just change the root of the specified
  // files to this mod
  DirectoryStats dummy;
  FilesOrigin &origin = directoryStructure->createOrigin(
    ToWString(modName), directoryW, priority, dummy);

  for (const QString &filename : stealFiles) {
    if (filename.isEmpty()) {
      log::warn("Trying to find file with no name");
      continue;
    }
    QFileInfo fileInfo(filename);
    FileEntry::Ptr file = directoryStructure->findFile(ToWString(fileInfo.fileName()));
    if (file.get() != nullptr) {
      if (file->getOrigin() == 0) {
        // replace data as the origin on this bsa
        file->removeOrigin(0);
      }
      origin.addFile(file->getIndex());
      file->addOrigin(origin.getID(), file->getFileTime(), L"", -1);
    } else {
      QString warnStr = fileInfo.absolutePath();
      if (warnStr.isEmpty())
        warnStr = filename;
      log::warn("file not found: {}", warnStr);
    }
  }
}

void DirectoryRefresher::addModFilesToStructure(
  DirectoryEntry *directoryStructure, const QString &modName,
  int priority, const QString &directory, const QStringList &stealFiles)
{
  TimeThis tt("addModFilesToStructure()");

  std::wstring directoryW = ToWString(QDir::toNativeSeparators(directory));

  if (stealFiles.length() > 0) {
    stealModFilesIntoStructure(
      directoryStructure, modName, priority, directory, stealFiles);
  } else {
    directoryStructure->addFromOrigin(ToWString(modName), directoryW, priority);
  }
}

void DirectoryRefresher::addModToStructure(DirectoryEntry *directoryStructure
  , const QString &modName
  , int priority
  , const QString &directory
  , const QStringList &stealFiles
  , const QStringList &archives)
{
  TimeThis tt("addModToStructure()");

  if (stealFiles.length() > 0) {
    stealModFilesIntoStructure(
      directoryStructure, modName, priority, directory, stealFiles);
  } else {
    std::wstring directoryW = ToWString(QDir::toNativeSeparators(directory));
    directoryStructure->addFromOrigin(ToWString(modName), directoryW, priority);
  }

  if (Settings::instance().archiveParsing()) {
    addModBSAToStructure(directoryStructure, modName, priority, directory, archives);
  }
}

struct ModThread
{
  DirectoryEntry* ds = nullptr;
  std::wstring modName;
  std::wstring path;
  int prio = -1;
  env::Directory* dir = nullptr;

  std::condition_variable cv;
  std::mutex mutex;
  bool ready = false;

  void wakeup()
  {
    ready = true;
    cv.notify_one();
  }

  void run()
  {
    std::unique_lock lock(mutex);
    cv.wait(lock, [&]{ return ready; });

    ds->addFromOrigin(modName, path, prio);

    /*if (Settings::instance().archiveParsing()) {
      addModBSAToStructure(
        directoryStructure,
        entries[i].modName,
        prio,
        entries[i].absolutePath,
        entries[i].archives);
    }*/

    ready = false;
  }
};

void DirectoryRefresher::addMultipleModsFilesToStructure(
  MOShared::DirectoryEntry *directoryStructure,
  const std::vector<EntryInfo>& entries, bool emitProgress)
{
  std::vector<env::Directory> dirs(entries.size());

  {
    TimeThis tt("walk dirs");

    env::ThreadPool<ModThread> threads(m_threadCount);

    for (std::size_t i=0; i<entries.size(); ++i) {
      const auto& e = entries[i];
      const int prio = static_cast<int>(i + 1);

      try
      {
        if (e.stealFiles.length() > 0) {
          stealModFilesIntoStructure(
            directoryStructure, e.modName, prio, e.absolutePath, e.stealFiles);
        } else {
          auto& mt = threads.request();

          mt.ds = directoryStructure;
          mt.modName = entries[i].modName.toStdWString();
          mt.path = QDir::toNativeSeparators(e.absolutePath).toStdWString();
          mt.prio = prio;
          mt.dir = &dirs[i];

          mt.wakeup();
        }
      } catch (const std::exception& ex) {
        emit error(tr("failed to read mod (%1): %2").arg(e.modName, ex.what()));
      }

      if (emitProgress) {
        emit progress((static_cast<int>(i) * 100) / static_cast<int>(entries.size()) + 1);
      }
    }

    threads.join();
  }

  //std::sort(stats.begin(), stats.end(), [](auto&& a, auto&& b){
  //  return (naturalCompare(QString::fromStdString(a.mod), QString::fromStdString(b.mod)) < 0);
  //});

  //static int run = 1;
  //
  //std::ofstream out("c:\\tmp\\data.csv", std::ios::app);

  //out << fmt::format("what,run,{}", DirectoryStats::csvHeader());
  //
  //for (std::size_t i=0; i<entries.size(); ++i) {
  //  out << fmt::format("{},{},{}", stats[i].mod, run, stats[i].toCsv()) << "\n";
  //}
  //
  //out << fmt::format("total,{},{}", run, total.toCsv()) << "\n";
  //
  //++run;
}

namespace MOShared{ void logcounts(std::string w); }

void DirectoryRefresher::refresh()
{
  SetThisThreadName("DirectoryRefresher");

  QMutexLocker locker(&m_RefreshLock);

  //logcounts("before delete");
  delete m_DirectoryStructure;
  //logcounts("after delete");

  m_DirectoryStructure = new DirectoryEntry(L"data", nullptr, 0);

  IPluginGame *game = qApp->property("managed_game").value<IPluginGame*>();

  std::wstring dataDirectory =
    QDir::toNativeSeparators(game->dataDirectory().absolutePath()).toStdWString();

  m_DirectoryStructure->addFromOrigin(L"data", dataDirectory, 0);

  std::sort(m_Mods.begin(), m_Mods.end(), [](auto lhs, auto rhs) {
    return lhs.priority < rhs.priority;
  });

  addMultipleModsFilesToStructure(m_DirectoryStructure, m_Mods, true);

  m_DirectoryStructure->getFileRegister()->sortOrigins();

  emit progress(100);

  cleanStructure(m_DirectoryStructure);

  emit refreshed();

  //logcounts("after refresh");
}