summaryrefslogtreecommitdiff
path: root/src/profile.cpp
blob: c6a2b43d3eecb3e72f3c67631f2cb698e26baa66 (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
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
/*
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 "profile.h"

#include "windows_error.h"
#include "modinfo.h"
#include "safewritefile.h"
#include <utility.h>
#include <util.h>
#include <error_report.h>
#include <appconfig.h>
#include <iplugingame.h>
#include <report.h>
#include <bsainvalidation.h>
#include <dataarchives.h>

#include <QMessageBox>
#include <QApplication>
#include <QSettings>
#include <QTemporaryFile>

#include <functional>
#include <stdexcept>

#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <shlobj.h>

using namespace MOBase;
using namespace MOShared;


Profile::Profile()
  : m_ModListWriter(std::bind(&Profile::writeModlistNow, this))
  , m_GamePlugin(qApp->property("managed_game").value<IPluginGame*>())
{
}

void Profile::touchFile(QString fileName)
{
  QFile modList(m_Directory.filePath(fileName));
  if (!modList.open(QIODevice::ReadWrite)) {
    throw std::runtime_error(QObject::tr("failed to create %1").arg(m_Directory.filePath(fileName)).toUtf8().constData());
  }
}

Profile::Profile(const QString &name, IPluginGame *gamePlugin, bool useDefaultSettings)
  : m_ModListWriter(std::bind(&Profile::writeModlistNow, this))
  , m_GamePlugin(gamePlugin)
{
  QString profilesDir = qApp->property("dataPath").toString() + "/" + ToQString(AppConfig::profilesPath());
  QDir profileBase(profilesDir);

  QString fixedName = name;
  if (!fixDirectoryName(fixedName)) {
    throw MyException(tr("invalid profile name %1").arg(name));
  }

  if (!profileBase.exists() || !profileBase.mkdir(fixedName)) {
    throw MyException(tr("failed to create %1").arg(fixedName).toUtf8().constData());
  }
  QString fullPath = profilesDir + "/" + fixedName;
  m_Directory = QDir(fullPath);

  try {
    // create files. Needs to happen after m_Directory was set!
    touchFile("modlist.txt");
    touchFile("archives.txt");

    IPluginGame::ProfileSettings settings = IPluginGame::CONFIGURATION
                                          | IPluginGame::MODS
                                          | IPluginGame::SAVEGAMES;

    if (useDefaultSettings) {
      settings |= IPluginGame::PREFER_DEFAULTS;
    }

    gamePlugin->initializeProfile(fullPath, settings);
  } catch (...) {
    // clean up in case of an error
    shellDelete(QStringList(profileBase.absoluteFilePath(fixedName)));
    throw;
  }
  refreshModStatus();
}


Profile::Profile(const QDir &directory, IPluginGame *gamePlugin)
  : m_Directory(directory)
  , m_GamePlugin(gamePlugin)
  , m_ModListWriter(std::bind(&Profile::writeModlistNow, this))
{
  assert(gamePlugin != nullptr);

  if (!QFile::exists(m_Directory.filePath("modlist.txt"))) {
    qWarning("missing modlist.txt in %s", qPrintable(directory.path()));
    touchFile(m_Directory.filePath("modlist.txt"));
  }

  IPluginGame::ProfileSettings settings = IPluginGame::CONFIGURATION
                                        | IPluginGame::MODS
                                        | IPluginGame::SAVEGAMES;
  gamePlugin->initializeProfile(directory, settings);

  if (!QFile::exists(getIniFileName())) {
    reportError(QObject::tr("\"%1\" is missing or inaccessible").arg(getIniFileName()));
  }
  refreshModStatus();
}


Profile::Profile(const Profile &reference)
  : m_Directory(reference.m_Directory)
  , m_ModListWriter(std::bind(&Profile::writeModlistNow, this))
{
  refreshModStatus();
}


Profile::~Profile()
{
  m_ModListWriter.writeImmediately(true);
}

bool Profile::exists() const
{
  return m_Directory.exists();
}

void Profile::writeModlistNow()
{
  if (!m_Directory.exists()) return;

  try {
    QString fileName = getModlistFileName();
    SafeWriteFile file(fileName);

    file->write(QString("# This file was automatically generated by Mod Organizer.\r\n").toUtf8());
    if (m_ModStatus.empty()) {
      return;
    }

    for (int i = m_ModStatus.size() - 1; i >= 0; --i) {
      // the priority order was inverted on load so it has to be inverted again
      unsigned int index = m_ModIndexByPriority[i];
      if (index != UINT_MAX) {
        ModInfo::Ptr modInfo = ModInfo::getByIndex(index);
        std::vector<ModInfo::EFlag> flags = modInfo->getFlags();
        if ((modInfo->getFixedPriority() == INT_MIN)) {
          if (std::find(flags.begin(), flags.end(), ModInfo::FLAG_FOREIGN) != flags.end()) {
            file->write("*");
          } else if (m_ModStatus[index].m_Enabled) {
            file->write("+");
          } else {
            file->write("-");
          }
          file->write(modInfo->name().toUtf8());
          file->write("\r\n");
        }
      }
    }

    if (file.commitIfDifferent(m_LastModlistHash)) {
      qDebug("%s saved", QDir::toNativeSeparators(fileName).toUtf8().constData());
    }
  } catch (const std::exception &e) {
    reportError(tr("failed to write mod list: %1").arg(e.what()));
    return;
  }
}


void Profile::createTweakedIniFile()
{
  QString tweakedIni = m_Directory.absoluteFilePath("initweaks.ini");

  if (QFile::exists(tweakedIni) && !shellDeleteQuiet(tweakedIni)) {
    reportError(tr("failed to update tweaked ini file, wrong settings may be used: %1").arg(windowsErrorString(::GetLastError())));
    return;
  }

  for (unsigned int i = 0; i < m_ModStatus.size(); ++i) {
    unsigned int idx = modIndexByPriority(i);
    if ((idx != UINT_MAX) && m_ModStatus[idx].m_Enabled) {
      ModInfo::Ptr modInfo = ModInfo::getByIndex(idx);
      mergeTweaks(modInfo, tweakedIni);
    }
  }

  mergeTweak(getProfileTweaks(), tweakedIni);

  bool error = false;
  if (!::WritePrivateProfileStringW(L"Archive", L"bInvalidateOlderFiles", L"1", ToWString(tweakedIni).c_str())) {
    error = true;
  }

  if (localSavesEnabled()) {
    if (!::WritePrivateProfileStringW(L"General", L"bUseMyGamesDirectory", L"0", ToWString(tweakedIni).c_str())) {
      error = true;
    }

    if (!::WritePrivateProfileStringW(L"General", L"SLocalSavePath",
                                       AppConfig::localSavePlaceholder(),
                                       ToWString(tweakedIni).c_str())) {
      error = true;
    }
  }

  if (error) {
    reportError(tr("failed to create tweaked ini: %1").arg(getCurrentErrorStringA().c_str()));
  }
  qDebug("%s saved", qPrintable(QDir::toNativeSeparators(tweakedIni)));
}


void Profile::refreshModStatus()
{
  QFile file(getModlistFileName());
  if (!file.open(QIODevice::ReadOnly)) {
    throw MyException(tr("\"%1\" is missing or inaccessible").arg(getModlistFileName()));
  }

  bool modStatusModified = false;
  m_ModStatus.clear();
  m_ModStatus.resize(ModInfo::getNumMods());

  std::set<QString> namesRead;

  // load mods from file and update enabled state and priority for them
  int index = 0;
  while (!file.atEnd()) {
    QByteArray line = file.readLine().trimmed();
    bool enabled = true;
    QString modName;
    if (line.length() == 0) {
      // empty line
      continue;
    } else if (line.at(0) == '#') {
      // comment line
      continue;
    } else if (line.at(0) == '-') {
      enabled = false;
      modName = QString::fromUtf8(line.mid(1).trimmed().constData());
    } else if ((line.at(0) == '+')
               || (line.at(0) == '*')) {
      modName = QString::fromUtf8(line.mid(1).trimmed().constData());
    } else {
      modName = QString::fromUtf8(line.trimmed().constData());
    }
    if (modName.size() > 0) {
      QString lookupName = modName;
      if (namesRead.find(lookupName) != namesRead.end()) {
        continue;
      } else {
        namesRead.insert(lookupName);
      }
      unsigned int modIndex = ModInfo::getIndex(lookupName);
      if (modIndex != UINT_MAX) {
        ModInfo::Ptr info = ModInfo::getByIndex(modIndex);
        if ((modIndex < m_ModStatus.size())
            && (info->getFixedPriority() == INT_MIN)) {
          m_ModStatus[modIndex].m_Enabled = enabled;
          if (m_ModStatus[modIndex].m_Priority == -1) {
            if (static_cast<size_t>(index) >= m_ModStatus.size()) {
              throw MyException(tr("invalid index %1").arg(index));
            }
            m_ModStatus[modIndex].m_Priority = index++;
          }
        } else {
          qWarning("no mod state for \"%s\" (profile \"%s\")",
                   qPrintable(modName), m_Directory.path().toUtf8().constData());
          // need to rewrite the modlist to fix this
          modStatusModified = true;
        }
      } else {
        qDebug("mod \"%s\" (profile \"%s\") not found",
               qPrintable(modName), m_Directory.path().toUtf8().constData());
        // need to rewrite the modlist to fix this
        modStatusModified = true;
      }
    }
  }

  int numKnownMods = index;

  int topInsert = 0;

  // invert priority order to match that of the pluginlist. Also
  // give priorities to mods not referenced in the profile
  for (size_t i = 0; i < m_ModStatus.size(); ++i) {
    ModInfo::Ptr modInfo = ModInfo::getByIndex(i);
    if (modInfo->alwaysEnabled()) {
      m_ModStatus[i].m_Enabled = true;
    }

    if (modInfo->getFixedPriority() == INT_MAX) {
      continue;
    }

    if (m_ModStatus[i].m_Priority != -1) {
      m_ModStatus[i].m_Priority = numKnownMods - m_ModStatus[i].m_Priority - 1;
    } else {
      if (static_cast<size_t>(index) >= m_ModStatus.size()) {
        throw MyException(tr("invalid index %1").arg(index));
      }
      if (modInfo->hasFlag(ModInfo::FLAG_FOREIGN)) {
        m_ModStatus[i].m_Priority = --topInsert;
      } else {
        m_ModStatus[i].m_Priority = index++;
      }
      // also, mark the mod-list as changed
      modStatusModified = true;
    }
  }
  // to support insertion of new mods at the top we may now have mods with negative priority. shift them all up
  // to align priority with 0
  if (topInsert < 0) {
    int offset = topInsert * -1;
    for (size_t i = 0; i < m_ModStatus.size(); ++i) {
      ModInfo::Ptr modInfo = ModInfo::getByIndex(i);
      if (modInfo->getFixedPriority() == INT_MAX) {
        continue;
      }

      m_ModStatus[i].m_Priority += offset;
    }
  }

  file.close();
  updateIndices();
  if (modStatusModified) {
    m_ModListWriter.write();
  }
}


void Profile::dumpModStatus() const
{
  for (unsigned int i = 0; i < m_ModStatus.size(); ++i) {
    ModInfo::Ptr info = ModInfo::getByIndex(i);
    qWarning("%d: %s - %d (%s)", i, info->name().toUtf8().constData(), m_ModStatus[i].m_Priority,
             m_ModStatus[i].m_Enabled ? "enabled" : "disabled");
  }
}


void Profile::updateIndices()
{
  m_NumRegularMods = 0;
  m_ModIndexByPriority.clear();
  m_ModIndexByPriority.resize(m_ModStatus.size(), UINT_MAX);
  for (unsigned int i = 0; i < m_ModStatus.size(); ++i) {
    int priority = m_ModStatus[i].m_Priority;
    if (priority < 0) {
      // don't assign this to mapping at all, it's probably the overwrite mod
      continue;
    } else if (priority >= static_cast<int>(m_ModIndexByPriority.size())) {
      qCritical("invalid priority %d for mod", priority);
      continue;
    } else {
      ++m_NumRegularMods;
      m_ModIndexByPriority.at(priority) = i;
    }
  }
}


std::vector<std::tuple<QString, QString, int> > Profile::getActiveMods()
{
  std::vector<std::tuple<QString, QString, int> > result;
  for (std::vector<unsigned int>::const_iterator iter = m_ModIndexByPriority.begin();
       iter != m_ModIndexByPriority.end(); ++iter) {
    if ((*iter != UINT_MAX) && m_ModStatus[*iter].m_Enabled) {
      ModInfo::Ptr modInfo = ModInfo::getByIndex(*iter);
      result.push_back(std::make_tuple(modInfo->internalName(), modInfo->absolutePath(), m_ModStatus[*iter].m_Priority));
    }
  }

  unsigned int overwriteIndex = ModInfo::findMod([](ModInfo::Ptr mod) -> bool {
    std::vector<ModInfo::EFlag> flags = mod->getFlags();
    return std::find(flags.begin(), flags.end(), ModInfo::FLAG_OVERWRITE) != flags.end(); });

  if (overwriteIndex != UINT_MAX) {
    ModInfo::Ptr overwriteInfo = ModInfo::getByIndex(overwriteIndex);
    result.push_back(std::make_tuple(overwriteInfo->name(), overwriteInfo->absolutePath(), UINT_MAX));
  } else {
    reportError(tr("Overwrite directory couldn't be parsed"));
  }
  return result;
}


unsigned int Profile::modIndexByPriority(unsigned int priority) const
{
  if (priority >= m_ModStatus.size()) {
    throw MyException(tr("invalid priority %1").arg(priority));
  }

  return m_ModIndexByPriority[priority];
}


void Profile::setModEnabled(unsigned int index, bool enabled)
{
  if (index >= m_ModStatus.size()) {
    throw MyException(tr("invalid index %1").arg(index));
  }

  ModInfo::Ptr modInfo = ModInfo::getByIndex(index);
  // we could quit in the following case, this shouldn't be a change anyway,
  // but at least this allows the situation to be fixed in case of an error
  if (modInfo->alwaysEnabled()) {
    enabled = true;
  }

  if (enabled != m_ModStatus[index].m_Enabled) {
    m_ModStatus[index].m_Enabled = enabled;
    emit modStatusChanged(index);
  }
}

bool Profile::modEnabled(unsigned int index) const
{
  if (index >= m_ModStatus.size()) {
    throw MyException(tr("invalid index %1").arg(index));
  }

  return m_ModStatus[index].m_Enabled;
}


int Profile::getModPriority(unsigned int index) const
{
  if (index >= m_ModStatus.size()) {
    throw MyException(tr("invalid index %1").arg(index));
  }

  return m_ModStatus[index].m_Priority;
}


void Profile::setModPriority(unsigned int index, int &newPriority)
{
  if (m_ModStatus.at(index).m_Overwrite) {
    // can't change priority of the overwrite
    return;
  }

  int newPriorityTemp = (std::max)(0, (std::min<int>)(m_ModStatus.size() - 1, newPriority));

  // don't try to place below overwrite
  while ((m_ModIndexByPriority.at(newPriorityTemp) >= m_ModStatus.size()) ||
         m_ModStatus.at(m_ModIndexByPriority.at(newPriorityTemp)).m_Overwrite) {
    --newPriorityTemp;
  }

  int oldPriority = m_ModStatus.at(index).m_Priority;
  if (newPriorityTemp > oldPriority) {
    // priority is higher than the old, so the gap we left is in lower priorities
    for (int i = oldPriority + 1; i <= newPriorityTemp; ++i) {
      --m_ModStatus.at(m_ModIndexByPriority.at(i)).m_Priority;
    }
  } else {
    for (int i = newPriorityTemp; i < oldPriority; ++i) {
      ++m_ModStatus.at(m_ModIndexByPriority.at(i)).m_Priority;
    }
    ++newPriority;
  }

  m_ModStatus.at(index).m_Priority = newPriorityTemp;

  updateIndices();
  m_ModListWriter.write();
}

Profile *Profile::createPtrFrom(const QString &name, const Profile &reference, MOBase::IPluginGame *gamePlugin)
{
  QString profileDirectory = qApp->property("dataPath").toString() + "/" + QString::fromStdWString(AppConfig::profilesPath()) + "/" + name;
  reference.copyFilesTo(profileDirectory);
  return new Profile(QDir(profileDirectory), gamePlugin);
}

void Profile::copyFilesTo(QString &target) const
{
  copyDir(m_Directory.absolutePath(), target, false);
}

std::vector<std::wstring> Profile::splitDZString(const wchar_t *buffer) const
{
  std::vector<std::wstring> result;
  const wchar_t *pos = buffer;
  size_t length = wcslen(pos);
  while (length != 0U) {
    result.push_back(pos);
    pos += length + 1;
    length = wcslen(pos);
  }
  return result;
}

void Profile::mergeTweak(const QString &tweakName, const QString &tweakedIni) const
{
  static const int bufferSize = 32768;

  std::wstring tweakNameW  = ToWString(tweakName);
  std::wstring tweakedIniW = ToWString(tweakedIni);
  QScopedArrayPointer<wchar_t> buffer(new wchar_t[bufferSize]);

  // retrieve a list of sections
  DWORD size = ::GetPrivateProfileSectionNamesW(
        buffer.data(), bufferSize, tweakNameW.c_str());

  if (size == bufferSize - 2) {
    // unfortunately there is no good way to find the required size
    // of the buffer
    throw MyException(QString("Buffer too small. Please report this as a bug. "
                        "For now you might want to split up %1").arg(tweakName));
  }

  std::vector<std::wstring> sections = splitDZString(buffer.data());

  // now iterate over all sections and retrieve a list of keys in each
  for (std::vector<std::wstring>::iterator iter = sections.begin();
       iter != sections.end(); ++iter) {
    // retrieve the names of all keys
    size = ::GetPrivateProfileStringW(iter->c_str(), nullptr, nullptr, buffer.data(),
                                      bufferSize, tweakNameW.c_str());
    if (size == bufferSize - 2) {
      throw MyException(QString("Buffer too small. Please report this as a bug. "
                          "For now you might want to split up %1").arg(tweakName));
    }

    std::vector<std::wstring> keys = splitDZString(buffer.data());

    for (std::vector<std::wstring>::iterator keyIter = keys.begin();
         keyIter != keys.end(); ++keyIter) {
       //TODO this treats everything as strings but how could I differentiate the type?
      ::GetPrivateProfileStringW(iter->c_str(), keyIter->c_str(),
                                 nullptr, buffer.data(), bufferSize, ToWString(tweakName).c_str());
      ::WritePrivateProfileStringW(iter->c_str(), keyIter->c_str(),
                                   buffer.data(), tweakedIniW.c_str());
    }
  }
}

void Profile::mergeTweaks(ModInfo::Ptr modInfo, const QString &tweakedIni) const
{
  std::vector<QString> iniTweaks = modInfo->getIniTweaks();
  for (std::vector<QString>::iterator iter = iniTweaks.begin();
       iter != iniTweaks.end(); ++iter) {
    mergeTweak(*iter, tweakedIni);
  }
}


bool Profile::invalidationActive(bool *supported) const
{
  BSAInvalidation *invalidation = m_GamePlugin->feature<BSAInvalidation>();
  DataArchives *dataArchives = m_GamePlugin->feature<DataArchives>();

  if ((invalidation != nullptr) && (dataArchives != nullptr)) {
    if (supported != nullptr) {
      *supported = true;
    }

    for (const QString &archive : dataArchives->archives(this)) {
      if (invalidation->isInvalidationBSA(archive)) {
        return true;
      }
    }
    return false;
  } else {
    *supported = false;
  }
  return false;
}


void Profile::deactivateInvalidation()
{
  BSAInvalidation *invalidation = m_GamePlugin->feature<BSAInvalidation>();

  if (invalidation != nullptr) {
    invalidation->deactivate(this);
  }
}


void Profile::activateInvalidation()
{
  BSAInvalidation *invalidation = m_GamePlugin->feature<BSAInvalidation>();

  if (invalidation != nullptr) {
    invalidation->activate(this);
  }
}


bool Profile::localSavesEnabled() const
{
  return m_Directory.exists("saves");
}


bool Profile::enableLocalSaves(bool enable)
{
  if (enable) {
    if (m_Directory.exists("_saves")) {
      m_Directory.rename("_saves", "saves");
    } else {
      m_Directory.mkdir("saves");
    }
  } else {
    QMessageBox::StandardButton res = QMessageBox::question(QApplication::activeModalWidget(), tr("Delete savegames?"),
                                                            tr("Do you want to delete local savegames? (If you select \"No\", the save games "
                                                               "will show up again if you re-enable local savegames)"),
                                                            QMessageBox::Yes | QMessageBox::No | QMessageBox::Cancel,
                                                            QMessageBox::Cancel);
    if (res == QMessageBox::Yes) {
      shellDelete(QStringList(m_Directory.absoluteFilePath("saves")));
    } else if (res == QMessageBox::No) {
      m_Directory.rename("saves", "_saves");
    } else {
      return false;
    }
  }

  // default: assume success
  return true;
}


QString Profile::getModlistFileName() const
{
  return QDir::cleanPath(m_Directory.absoluteFilePath("modlist.txt"));
}

QString Profile::getPluginsFileName() const
{
  return QDir::cleanPath(m_Directory.absoluteFilePath("plugins.txt"));
}

QString Profile::getLoadOrderFileName() const
{
  return QDir::cleanPath(m_Directory.absoluteFilePath("loadorder.txt"));
}

QString Profile::getLockedOrderFileName() const
{
  return QDir::cleanPath(m_Directory.absoluteFilePath("lockedorder.txt"));
}

QString Profile::getArchivesFileName() const
{
  return QDir::cleanPath(m_Directory.absoluteFilePath("archives.txt"));
}

QString Profile::getDeleterFileName() const
{
  return QDir::cleanPath(m_Directory.absoluteFilePath("hide_plugins.txt"));
}

QString Profile::getIniFileName() const
{
  std::wstring primaryIniFile = *(GameInfo::instance().getIniFileNames().begin());
  return m_Directory.absoluteFilePath(ToQString(primaryIniFile));
}

QString Profile::getProfileTweaks() const
{
  return QDir::cleanPath(m_Directory.absoluteFilePath(ToQString(AppConfig::profileTweakIni())));
}

QString Profile::absolutePath() const
{
  return QDir::cleanPath(m_Directory.absolutePath());
}

void Profile::rename(const QString &newName)
{
  QDir profileDir(qApp->property("dataPath").toString() + "/" + QString::fromStdWString(AppConfig::profilesPath()));
  profileDir.rename(name(), newName);
  m_Directory = profileDir.absoluteFilePath(newName);
}