aboutsummaryrefslogtreecommitdiff
path: root/libs/check_fnis/src/checkfnis.cpp
blob: 73f2ac0d76adc73b776c3b799d59bb8347c54704 (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
#include "checkfnis.h"

#include <uibase/iplugingame.h>
#include <uibase/pluginsetting.h>
#include <uibase/questionboxmemory.h>
#include <uibase/report.h>
#include <uibase/scopeguard.h>

#include <QCryptographicHash>
#include <QDialogButtonBox>
#include <QDir>
#include <QFile>
#include <QFileInfo>
#include <QMessageBox>
#include <QStringList>
#include <QVariant>

#include <Qt>       // for Qt::CaseInsensitive
#include <QtDebug>  // for qCritical, qDebug

#include <functional>

using namespace MOBase;

CheckFNIS::CheckFNIS()
    : m_MOInfo(nullptr), m_Active(false),
      m_MatchExpressions(std::vector<QRegularExpression>{
          // MSVC2013 bug. The (std::vector<QRegExp> shouldn't be necessary
          QRegularExpression("\\\\FNIS_.*_List\\.txt$",
                             QRegularExpression::PatternOption::CaseInsensitiveOption),
          QRegularExpression("\\\\FNIS.*Behavior\\.txt$",
                             QRegularExpression::PatternOption::CaseInsensitiveOption),
          QRegularExpression("\\\\PatchList\\.txt$",
                             QRegularExpression::PatternOption::CaseInsensitiveOption),
          QRegularExpression(
              "\\\\skeleton.*\\.hkx$",
              QRegularExpression::PatternOption::CaseInsensitiveOption)}),
      m_SensitiveMatchExpressions(std::vector<QRegularExpression>{
          QRegularExpression("\\\\animations\\\\.*\\.hkx$",
                             QRegularExpression::PatternOption::CaseInsensitiveOption)})
{}

CheckFNIS::~CheckFNIS() {}

bool CheckFNIS::init(IOrganizer* moInfo)
{
  m_MOInfo = moInfo;

  if (!moInfo->onAboutToRun([this](const auto& binary) {
        return fnisCheck(binary);
      })) {
    qCritical("failed to connect to about to run event");
    return false;
  }

  if (!moInfo->onFinishedRun(std::bind(&CheckFNIS::fnisEndCheck, this,
                                       std::placeholders::_1, std::placeholders::_2))) {
    qCritical("failed to connect to finished run event");
    return false;
  }

  return true;
}

QString CheckFNIS::name() const
{
  return "FNIS Checker";
}

QString CheckFNIS::localizedName() const
{
  return tr("FNIS Checker");
}

QString CheckFNIS::author() const
{
  return "Tannin";
}

QString CheckFNIS::description() const
{
  return tr("Checks if FNIS behaviours need to be updated whenever you start the game."
            " This is only relevant for Skyrim and if FNIS is installed.<br>");
}

VersionInfo CheckFNIS::version() const
{
  return VersionInfo(1, 0, 1, VersionInfo::RELEASE_FINAL);
}

std::vector<std::shared_ptr<const MOBase::IPluginRequirement>>
CheckFNIS::requirements() const
{
  return {Requirements::gameDependency("Skyrim")};
}

QList<PluginSetting> CheckFNIS::settings() const
{
  QList<PluginSetting> result;
  result.push_back(PluginSetting("sensitive",
                                 "check changes on non-fnis animations. Makes this "
                                 "more reliable but will cause FNIS to be called more "
                                 "often than necessary.",
                                 QVariant(false)));
  return result;
}

bool CheckFNIS::testFileRelevant(const IOrganizer::FileInfo& fileName) const
{
  if (!fileName.archive.isEmpty()) {
    return false;
  }

  for (auto& expr : m_MatchExpressions) {
    auto match = expr.match(fileName.filePath);
    if (match.hasMatch()) {
      return true;
    }
  }

  if (m_MOInfo->pluginSetting(name(), "sensitive").toBool()) {
    for (auto& expr : m_SensitiveMatchExpressions) {
      auto match = expr.match(fileName.filePath);
      if (match.hasMatch()) {
        return true;
      }
    }
  }

  return false;
}

void CheckFNIS::findRelevantFilesRecursive(const QString& path,
                                           QMap<QString, QString>& fileList) const
{
  // find all relevant files
  QList<IOrganizer::FileInfo> files = m_MOInfo->findFileInfos(
      path, std::bind(&CheckFNIS::testFileRelevant, this, std::placeholders::_1));
  foreach (const IOrganizer::FileInfo& fileInfo, files) {
    QFile file(fileInfo.filePath);
    if (file.open(QIODevice::ReadOnly)) {
      QString hash(
          QCryptographicHash::hash(file.readAll(), QCryptographicHash::Md5).toHex());
      fileList.insert(fileInfo.filePath, hash);
    } else {
      qCritical("failed to open %s", qUtf8Printable(fileInfo.filePath));
    }
  }

  QStringList subDirectories = m_MOInfo->listDirectories(path);
  foreach (const QString& directory, subDirectories) {
    findRelevantFilesRecursive(QDir(path).filePath(directory), fileList);
  }
}

QString CheckFNIS::generateIdentifier() const
{
  QMap<QString, QString> fileList;

  findRelevantFilesRecursive("meshes\\actors", fileList);

  QStringList flattenedList;
  for (auto iter = fileList.begin(); iter != fileList.end(); ++iter) {
    flattenedList.append(iter.key() + "=" + iter.value());
  }

  return QCryptographicHash::hash(flattenedList.join(",").toUtf8(),
                                  QCryptographicHash::Md5)
      .toHex();
}

bool CheckFNIS::appIsFNIS(QString const& application, QString const& fnisApp)
{
  return QString::compare(QDir::fromNativeSeparators(application),
                          QDir::fromNativeSeparators(fnisApp),
                          Qt::CaseInsensitive) == 0;
}

QString CheckFNIS::getFnisPath() const
{
  if (!m_MOInfo->pluginSetting(name(), "enabled").toBool()) {
    return "";
  }

  // Check if it's actually installed (...)
  QStringList fnisBinaryList = m_MOInfo->findFiles(
      "tools/GenerateFNIS_for_Users", [](const QString& fileName) -> bool {
        return fileName.endsWith("GenerateFNISforUsers.exe", Qt::CaseInsensitive);
      });

  if (fnisBinaryList.count() == 0) {
    // fnis seems not to be installed even though this is enabled
    qDebug("fnis not installed");
    return "";
  }

  // As this is looking in the vfs, there can be precisely 0 or 1 instances of FNIS
  return fnisBinaryList.at(0);
}

bool CheckFNIS::fnisCheck(const QString& application)
{

  QString fnisApp(getFnisPath());
  if (fnisApp.isEmpty() || appIsFNIS(application, fnisApp)) {
    return true;
  }

  // prevent this check from being called recursively
  if (m_Active) {
    return true;
  }

  m_Active = true;
  ON_BLOCK_EXIT([&] {
    m_Active = false;
  });

  QString const newHash = generateIdentifier();

  if (newHash == m_MOInfo->persistent(name(), m_MOInfo->profileName(), "").toString()) {
    // Don't need to run fnis as nothing relevant has changed.
    return true;
  }

  QDialogButtonBox::StandardButton res = QuestionBoxMemory::query(
      nullptr, "fnisCheck", QFileInfo(application).fileName(),
      tr("Run FNIS before %1?").arg(application),
      tr("FNIS source data has been changed. You should run GenerateFNIS.exe now."),
      QDialogButtonBox::Yes | QDialogButtonBox::No | QDialogButtonBox::Cancel,
      QDialogButtonBox::Yes);

  if (res == QDialogButtonBox::Yes) {
    HANDLE process = m_MOInfo->startApplication(fnisApp);
    bool cont      = true;
    if (process == INVALID_HANDLE_VALUE) {
      reportError(tr("Failed to start %1").arg(fnisApp));
    } else {
      DWORD exitCodeU;
      if (m_MOInfo->waitForApplication(process, &exitCodeU)) {
        int exitCode = static_cast<int>(exitCodeU);
        if (exitCode != 0) {
          cont = QMessageBox::question(
                     nullptr, tr("Start %1?").arg(application),
                     tr("FNIS reported a %1, do you want to run the application "
                        "anyway?")
                         .arg(exitCode < 0 ? tr("warning") : tr("critical error")),
                     QMessageBox::Yes | QMessageBox::No,
                     QMessageBox::No) == QMessageBox::Yes;
        }
      } else {
        cont = QMessageBox::question(nullptr, tr("Start %1?").arg(application),
                                     tr("Failed to determine FNIS exit code, do you "
                                        "want to run the application "
                                        "anyway?"),
                                     QMessageBox::Yes | QMessageBox::No,
                                     QMessageBox::No) == QMessageBox::Yes;
      }
      if (cont) {
        m_MOInfo->setPersistent(name(), m_MOInfo->profileName(), newHash);
      }
    }
    return cont;
  } else if (res == QDialogButtonBox::No) {
    return true;
  } else {
    // Don't run the app if they pressed cancel
    return false;
  }
}

void CheckFNIS::fnisEndCheck(const QString& application, unsigned int code)
{
  if (appIsFNIS(application, getFnisPath())) {
    bool update  = true;
    int exitCode = static_cast<int>(code);
    if (exitCode != 0) {
      update =
          QMessageBox::question(
              nullptr, tr("Start %1?").arg(application),
              tr("FNIS reported a %1. Do you want to assume it worked?")
                  .arg(exitCode < 0 ? tr("warning") : tr("critical error")),
              QMessageBox::Yes | QMessageBox::No, QMessageBox::No) == QMessageBox::Yes;
    }
    if (update) {
      m_MOInfo->setPersistent(name(), m_MOInfo->profileName(), generateIdentifier());
    }
  }
}

#if QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
Q_EXPORT_PLUGIN2(checkfnis, CheckFNIS)
#endif