aboutsummaryrefslogtreecommitdiff
path: root/libs/script_extender_checker_native/src/scriptextenderchecker.cpp
blob: 65276ad949e390ed697cd44ea0dd96d0c2ece1fc (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
#include "scriptextenderchecker.h"

#include <uibase/iplugingame.h>
#include <uibase/pluginrequirements.h>

#include <QCoreApplication>
#include <QDir>
#include <QFile>
#include <QFileInfo>
#include <QMap>
#include <QRegularExpression>
#include <QStringConverter>
#include <QTextStream>

using namespace MOBase;

// Regex patterns matching SKSE/F4SE/etc log formats
static const QRegularExpression RE_NORMAL(
    R"(plugin (?P<pluginPath>.+) \((?P<infoVersion>[\dA-Fa-f]{8}) (?P<name>.*) (?P<version>[\dA-Fa-f]{8})\) (?P<loadStatus>.+?)(?P<errorCode> \d+)?( \(handle \d+\))?\s*$)");

static const QRegularExpression RE_COULDNT_LOAD(
    R"(couldn't load plugin (?P<pluginPath>.+) \(Error (?:code )?(?P<lastError>[-+]?\d+)(?::\s*(?P<seDetails>.*))?\)\s*)");

static const QRegularExpression RE_NOT_PLUGIN(
    R"(plugin (?P<pluginPath>.+) does not appear to be an (?:SK|F4|NV|FO|OB)SE plugin\s*)");

ScriptExtenderChecker::ScriptExtenderChecker() : m_organizer(nullptr) {}

const QMap<QString, ScriptExtenderChecker::GameType>&
ScriptExtenderChecker::supportedGames()
{
  static const QMap<QString, GameType> games = {
      {"Skyrim",
       {LogLocation::Docs, "SKSE/skse.log", "SKSE/skse_editor.log"}},
      {"Skyrim Special Edition",
       {LogLocation::Docs, "SKSE/skse64.log", ""}},
      {"Skyrim VR",
       {LogLocation::Docs, "SKSE/sksevr.log", ""}},
      {"Fallout 4",
       {LogLocation::Docs, "F4SE/f4se.log", ""}},
      {"Oblivion",
       {LogLocation::Install, "obse.log", "obse_editor.log"}},
      {"New Vegas",
       {LogLocation::Install, "nvse.log", "nvse_editor.log"}},
      {"TTW",
       {LogLocation::Install, "nvse.log", "nvse_editor.log"}},
      {"Fallout 3",
       {LogLocation::Install, "fose.log", "fose_editor.log"}},
  };
  return games;
}

bool ScriptExtenderChecker::init(IOrganizer* moInfo)
{
  m_organizer = moInfo;
  m_organizer->onFinishedRun(
      [this](const QString&, unsigned int) { invalidate(); });
  return true;
}

QString ScriptExtenderChecker::name() const
{
  return "Script Extender Plugin Load Checker (Native)";
}

QString ScriptExtenderChecker::localizedName() const
{
  return tr("Script Extender Plugin Load Checker (Native)");
}

QString ScriptExtenderChecker::author() const
{
  return "AnyOldName3";
}

QString ScriptExtenderChecker::description() const
{
  return tr("Checks script extender log to see if any plugins failed to load.");
}

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

std::vector<std::shared_ptr<const IPluginRequirement>>
ScriptExtenderChecker::requirements() const
{
  const auto& games = supportedGames();
  return {PluginRequirementFactory::gameDependency(QStringList(games.keys()))};
}

QList<PluginSetting> ScriptExtenderChecker::settings() const
{
  return {};
}

std::vector<unsigned int> ScriptExtenderChecker::activeProblems() const
{
  if (!listBadPluginMessages().isEmpty()) {
    return {PROBLEM_PLUGIN_LOAD};
  }
  return {};
}

QString ScriptExtenderChecker::shortDescription(unsigned int key) const
{
  return tr("Script extender log reports incompatible plugins.");
}

QString ScriptExtenderChecker::fullDescription(unsigned int key) const
{
  QStringList plugins     = listBadPluginMessages();
  QString pluginListString = "\n  \u2022 " + plugins.join("\n  \u2022 ");
  return tr("You have one or more script extender plugins which failed to "
            "load!\n\n"
            "If you want this notification to go away, here are some steps you "
            "can take:\n"
            "  \u2022 Look for updates to the mod or the specific plugin "
            "included in the mod.\n"
            "  \u2022 Disable the mod containing the plugin.\n"
            "  \u2022 Hide or delete the plugin from the mod.\n\n"
            "To refresh the script extender logs, you will need to run the game "
            "and/or editor again!\n\n"
            "The failed plugins are:%1")
      .arg(pluginListString);
}

bool ScriptExtenderChecker::hasGuidedFix(unsigned int key) const
{
  return false;
}

void ScriptExtenderChecker::startGuidedFix(unsigned int key) const {}

QString ScriptExtenderChecker::resolveOrigin(const QString& pluginPath) const
{
  try {
    QString dataDir =
        m_organizer->managedGame()->dataDirectory().absolutePath();
    QString relativePath = QDir(dataDir).relativeFilePath(pluginPath);
    QStringList origins  = m_organizer->getFileOrigins(relativePath);
    if (!origins.isEmpty()) {
      return origins.first();
    }
  } catch (...) {
  }
  return QString();
}

ScriptExtenderChecker::PluginMessage
ScriptExtenderChecker::parseNormalLine(
    const QRegularExpressionMatch& match) const
{
  PluginMessage msg;
  msg.pluginPath    = match.captured("pluginPath");
  QString name      = match.captured("name");
  QString version   = match.captured("version");
  QString status    = match.captured("loadStatus");
  msg.origin        = resolveOrigin(msg.pluginPath);
  msg.success =
      msg.origin.isEmpty() || status == "loaded correctly" || status == "no version data";

  if (!msg.success) {
    QString trStatus = status;
    // Translate known statuses
    if (status == "disabled, address library needs to be updated")
      trStatus = tr("disabled, address library needs to be updated");
    else if (status == "disabled, fatal error occurred while loading plugin")
      trStatus = tr("disabled, fatal error occurred while loading plugin");
    else if (status == "disabled, bad version data")
      trStatus = tr("disabled, bad version data");
    else if (status == "disabled, no name specified")
      trStatus = tr("disabled, no name specified");
    else if (status == "disabled, unsupported version independence method")
      trStatus = tr("disabled, unsupported version independence method");
    else if (status == "disabled, incompatible with current runtime version")
      trStatus = tr("disabled, incompatible with current runtime version");
    else if (status == "disabled, requires newer script extender")
      trStatus = tr("disabled, requires newer script extender");
    else if (status == "reported as incompatible during query")
      trStatus = tr("reported as incompatible during query");
    else if (status == "reported as incompatible during load")
      trStatus = tr("reported as incompatible during load");
    else if (status ==
             "disabled, fatal error occurred while checking plugin compatibility")
      trStatus = tr("disabled, fatal error occurred while checking plugin "
                     "compatibility");
    else if (status == "disabled, fatal error occurred while querying plugin")
      trStatus = tr("disabled, fatal error occurred while querying plugin");

    msg.message = tr("%1 version %2 (%3, %4) %5.")
                      .arg(name)
                      .arg(version)
                      .arg(QFileInfo(msg.pluginPath).fileName())
                      .arg(msg.origin)
                      .arg(trStatus);
  }
  return msg;
}

ScriptExtenderChecker::PluginMessage
ScriptExtenderChecker::parseCouldntLoadLine(
    const QRegularExpressionMatch& match) const
{
  PluginMessage msg;
  msg.pluginPath = match.captured("pluginPath");
  int lastError  = match.captured("lastError").toInt();
  QString details = match.captured("seDetails").trimmed();
  msg.origin      = resolveOrigin(msg.pluginPath);
  msg.success     = msg.origin.isEmpty();

  if (!msg.success) {
    QString fileName = QFileInfo(msg.pluginPath).fileName();
    if (lastError == 126) {
      msg.message =
          tr("Couldn't load %1 (%2). A dependency DLL could not be found "
             "(code %3). %4")
              .arg(fileName)
              .arg(msg.origin)
              .arg(lastError)
              .arg(details);
    } else if (lastError == 193) {
      msg.message = tr("Couldn't load %1 (%2). A DLL is invalid (code %3).")
                        .arg(fileName)
                        .arg(msg.origin)
                        .arg(lastError);
    } else {
      msg.message =
          tr("Couldn't load %1 (%2). The last error code was %3.")
              .arg(fileName)
              .arg(msg.origin)
              .arg(lastError);
    }
  }
  return msg;
}

ScriptExtenderChecker::PluginMessage
ScriptExtenderChecker::parseNotAPluginLine(
    const QRegularExpressionMatch& match) const
{
  PluginMessage msg;
  msg.pluginPath = match.captured("pluginPath");
  msg.origin     = resolveOrigin(msg.pluginPath);
  msg.success    = msg.origin.isEmpty();

  if (!msg.success) {
    msg.message =
        tr("%1 (%2) does not appear to be a script extender plugin.")
            .arg(QFileInfo(msg.pluginPath).fileName())
            .arg(msg.origin);
  }
  return msg;
}

QList<ScriptExtenderChecker::PluginMessage>
ScriptExtenderChecker::parseLog(const QString& logPath) const
{
  QList<PluginMessage> messages;

  QFile file(logPath);
  if (!file.exists() || !file.open(QIODevice::ReadOnly | QIODevice::Text)) {
    return messages;
  }

  // Script extender logs use cp1252 encoding
  QTextStream stream(&file);
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
  stream.setCodec("Windows-1252");
#else
  stream.setEncoding(QStringConverter::Latin1);
#endif

  while (!stream.atEnd()) {
    QString line = stream.readLine() + "\n";

    QRegularExpressionMatch match = RE_NORMAL.match(line);
    if (match.hasMatch()) {
      messages.append(parseNormalLine(match));
      continue;
    }

    match = RE_COULDNT_LOAD.match(line);
    if (match.hasMatch()) {
      messages.append(parseCouldntLoadLine(match));
      continue;
    }

    match = RE_NOT_PLUGIN.match(line);
    if (match.hasMatch()) {
      messages.append(parseNotAPluginLine(match));
      continue;
    }
  }

  return messages;
}

QStringList ScriptExtenderChecker::listBadPluginMessages() const
{
  const auto& games = supportedGames();
  QString gameName  = m_organizer->managedGame()->gameName();

  if (!games.contains(gameName)) {
    return {};
  }

  const GameType& gameType = games[gameName];

  QString baseDir;
  if (gameType.base == LogLocation::Docs) {
    baseDir = m_organizer->managedGame()->documentsDirectory().absolutePath();
  } else {
    baseDir = m_organizer->managedGame()->gameDirectory().absolutePath();
  }

  QList<PluginMessage> gameMessages;
  QList<PluginMessage> editorMessages;

  if (!gameType.gameSuffix.isEmpty()) {
    gameMessages = parseLog(QDir(baseDir).filePath(gameType.gameSuffix));
  }
  if (!gameType.editorSuffix.isEmpty()) {
    editorMessages = parseLog(QDir(baseDir).filePath(gameType.editorSuffix));
  }

  QStringList result;

  // Report game log failures that aren't successful in editor log
  for (const auto& gameMsg : gameMessages) {
    if (!gameMsg.success) {
      bool editorOk = false;
      for (const auto& editorMsg : editorMessages) {
        if (gameMsg.pluginPath == editorMsg.pluginPath && editorMsg.success) {
          editorOk = true;
          break;
        }
      }
      if (!editorOk && !result.contains(gameMsg.message)) {
        result.append(gameMsg.message);
      }
    }
  }

  // Report editor log failures that aren't successful in game log
  for (const auto& editorMsg : editorMessages) {
    if (!editorMsg.success) {
      bool gameOk = false;
      for (const auto& gameMsg : gameMessages) {
        if (editorMsg.pluginPath == gameMsg.pluginPath && gameMsg.success) {
          gameOk = true;
          break;
        }
      }
      if (!gameOk && !result.contains(editorMsg.message)) {
        result.append(editorMsg.message);
      }
    }
  }

  return result;
}