#include "loot.h" #include "lootdialog.h" #include "spawn.h" #include "organizercore.h" #include "json.h" #include #include using namespace MOBase; using namespace json; log::Levels levelFromLoot(lootcli::LogLevels level) { using LC = lootcli::LogLevels; switch (level) { case LC::Trace: // fall-through case LC::Debug: return log::Debug; case LC::Info: return log::Info; case LC::Warning: return log::Warning; case LC::Error: return log::Error; default: return log::Info; } } QString Loot::Report::toMarkdown() const { QString s; if (!messages.empty()) { s += "### " + QObject::tr("General messages") + "\n"; for (auto&& m : messages) { s += " - " + m.toMarkdown() + "\n"; } } if (!plugins.empty()) { if (!s.isEmpty()) { s += "\n"; } s += "### " + QObject::tr("Plugins") + "\n"; for (auto&& p : plugins) { const auto ps = p.toMarkdown(); if (!ps.isEmpty()) { s += ps + "\n"; } } } if (s.isEmpty()) { s += "**" + QObject::tr("No messages.") + "**"; } s += stats.toMarkdown(); return s; } QString Loot::Stats::toMarkdown() const { return QString("`stats: %1s, lootcli %2, loot %3`") .arg(QString::number(time / 1000.0, 'f', 2)) .arg(lootcliVersion) .arg(lootVersion); } QString Loot::Plugin::toMarkdown() const { QString s; if (!incompatibilities.empty()) { s += " - **" + QObject::tr("Incompatibilities") + ": "; QString fs; for (auto&& f : incompatibilities) { if (!fs.isEmpty()) { fs += ", "; } fs += f.displayName.isEmpty() ? f.name : f.displayName; } s += fs + "**\n"; } if (!missingMasters.empty()) { s += " - **" + QObject::tr("Missing masters") + ": "; QString ms; for (auto&& m : missingMasters) { if (!ms.isEmpty()) { ms += ", "; } ms += m; } s += ms + "**\n"; } for (auto&& m : messages) { s += " - " + m.toMarkdown() + "\n"; } for (auto&& d : dirty) { s += " - " + d.toMarkdown(false) + "\n"; } if (!s.isEmpty()) { s = "#### " + name + "\n" + s; } return s; } QString Loot::Dirty::toString(bool isClean) const { if (isClean) { return QObject::tr("Verified clean by %1") .arg(cleaningUtility.isEmpty() ? "?" : cleaningUtility); } QString s = cleaningString(); if (!info.isEmpty()) { s += " " + info; } return s; } QString Loot::Dirty::toMarkdown(bool isClean) const { return toString(isClean); } QString Loot::Dirty::cleaningString() const { return QObject::tr("%1 found %2 ITM record(s), %3 deleted reference(s) and %4 deleted navmesh(es).") .arg(cleaningUtility.isEmpty() ? "?" : cleaningUtility) .arg(itm) .arg(deletedReferences) .arg(deletedNavmesh); } QString Loot::Message::toMarkdown() const { QString s; switch (type) { case log::Error: { s += "**" + QObject::tr("Error") + "**: "; break; } case log::Warning: { s += "**" + QObject::tr("Warning") + "**: "; break; } default: { break; } } s += text; return s; } Loot::Loot() : m_thread(nullptr), m_cancel(false), m_result(false) { } Loot::~Loot() { if (m_thread) { m_thread->wait(); } if (!m_outPath.isEmpty() && QFile::exists(m_outPath)) { log::debug("deleting temporary loot report '{}'", m_outPath); const auto r = shell::Delete(m_outPath); if (!r) { log::error( "failed to remove temporary loot json report '{}': {}", m_outPath, r.toString()); } } } bool Loot::start(QWidget* parent, OrganizerCore& core, bool didUpdateMasterList) { log::debug("starting loot"); m_outPath = QDir::temp().absoluteFilePath("lootreport.json"); const auto logLevel = core.settings().diagnostics().lootLogLevel(); QStringList parameters; parameters << "--game" << core.managedGame()->gameShortName() << "--gamePath" << QString("\"%1\"").arg(core.managedGame()->gameDirectory().absolutePath()) << "--pluginListPath" << QString("\"%1/loadorder.txt\"").arg(core.profilePath()) << "--logLevel" << QString::fromStdString(lootcli::logLevelToString(logLevel)) << "--out" << QString("\"%1\"").arg(m_outPath); if (didUpdateMasterList) { parameters << "--skipUpdateMasterlist"; } SECURITY_ATTRIBUTES secAttributes; secAttributes.nLength = sizeof(SECURITY_ATTRIBUTES); secAttributes.bInheritHandle = TRUE; secAttributes.lpSecurityDescriptor = nullptr; env::HandlePtr readPipe, writePipe; { HANDLE read = INVALID_HANDLE_VALUE; HANDLE write = INVALID_HANDLE_VALUE; if (!::CreatePipe(&read, &write, &secAttributes, 0)) { log::error("failed to create stdout reroute"); } readPipe.reset(read); writePipe.reset(write); if (!::SetHandleInformation(read, HANDLE_FLAG_INHERIT, 0)) { log::error("failed to correctly set up the stdout reroute"); } } core.prepareVFS(); spawn::SpawnParameters sp; sp.binary = QFileInfo(qApp->applicationDirPath() + "/loot/lootcli.exe"); sp.arguments = parameters.join(" "); sp.currentDirectory.setPath(qApp->applicationDirPath() + "/loot"); sp.hooked = true; sp.stdOut = writePipe.get(); m_stdout = std::move(readPipe); HANDLE lootHandle = spawn::startBinary(parent, sp); if (lootHandle == INVALID_HANDLE_VALUE) { emit log(log::Levels::Error, tr("failed to start loot")); return false; } m_lootProcess.reset(lootHandle); core.pluginList()->clearAdditionalInformation(); log::debug("starting loot thread"); m_thread.reset(QThread::create([&]{ try { lootThread(); } catch(...) { log::error("unhandled exception in loot thread"); } log::debug("finishing loot thread"); emit finished(); })); m_thread->start(); return true; } void Loot::cancel() { if (!m_cancel) { log::debug("loot received cancel request"); m_cancel = true; } } bool Loot::result() const { return m_result; } const QString& Loot::outPath() const { return m_outPath; } const Loot::Report& Loot::report() const { return m_report; } void Loot::lootThread() { ::SetThreadDescription(GetCurrentThread(), L"loot"); try { m_result = false; if (!waitForCompletion()) { return; } m_result = true; processOutputFile(); } catch (const std::exception &e) { emit log(log::Levels::Error, tr("failed to run loot: %1").arg(e.what())); } } bool Loot::waitForCompletion() { bool terminating = false; log::debug("loot thread waiting for completion on lootcli"); for (;;) { DWORD res = WaitForSingleObject(m_lootProcess.get(), 100); if (res == WAIT_OBJECT_0) { log::debug("lootcli has completed"); // done break; } if (res == WAIT_FAILED) { const auto e = GetLastError(); log::error("failed to wait on loot process, {}", formatSystemMessage(e)); return false; } if (m_cancel) { log::debug("terminating lootcli process"); ::TerminateProcess(m_lootProcess.get(), 1); log::debug("waiting for loocli process to terminate"); WaitForSingleObject(m_lootProcess.get(), INFINITE); log::debug("lootcli terminated"); return false; } processStdout(readFromPipe()); } if (m_cancel) { return false; } processStdout(readFromPipe()); // checking exit code DWORD exitCode = 0; if (!::GetExitCodeProcess(m_lootProcess.get(), &exitCode)) { const auto e = GetLastError(); log::error("failed to get exit code for loot, {}", formatSystemMessage(e)); return false; } if (exitCode != 0UL) { emit log(log::Levels::Error, tr("Loot failed. Exit code was: %1").arg(exitCode)); return false; } return true; } std::string Loot::readFromPipe() { static const int chunkSize = 128; std::string result; char buffer[chunkSize + 1]; buffer[chunkSize] = '\0'; DWORD read = 1; while (read > 0) { if (!::ReadFile(m_stdout.get(), buffer, chunkSize, &read, nullptr)) { break; } if (read > 0) { result.append(buffer, read); if (read < chunkSize) { break; } } } return result; } void Loot::processStdout(const std::string &lootOut) { emit output(QString::fromStdString(lootOut)); m_outputBuffer += lootOut; if (m_outputBuffer.empty()) { return; } log::debug("loot: processing stdout ({} bytes)", m_outputBuffer.size()); std::size_t start = 0; for (;;) { const auto newline = m_outputBuffer.find("\n", start); if (newline == std::string::npos) { break; } const std::string_view line(m_outputBuffer.c_str() + start, newline - start); const auto m = lootcli::parseMessage(line); if (m.type == lootcli::MessageType::None) { log::error("unrecognised loot output: '{}'", line); continue; } processMessage(m); start = newline + 1; } m_outputBuffer.erase(0, start); } void Loot::processMessage(const lootcli::Message& m) { switch (m.type) { case lootcli::MessageType::Log: { emit log(levelFromLoot(m.logLevel), QString::fromStdString(m.log)); break; } case lootcli::MessageType::Progress: { emit progress(m.progress); break; } } } void Loot::processOutputFile() { log::debug("parsing json output file at '{}'", m_outPath); QFile outFile(m_outPath); if (!outFile.open(QIODevice::ReadOnly)) { emit log( MOBase::log::Error, QString("failed to open file, %1 (error %2)") .arg(outFile.errorString()).arg(outFile.error())); return; } QJsonParseError e; const QJsonDocument doc = QJsonDocument::fromJson(outFile.readAll(), &e); if (doc.isNull()) { emit log( MOBase::log::Error, QString("invalid json, %1 (error %2)") .arg(e.errorString()).arg(e.error)); return; } m_report = createReport(doc); } Loot::Report Loot::createReport(const QJsonDocument& doc) const { requireObject(doc, "root"); Report r; const QJsonObject object = doc.object(); r.messages = reportMessages(getOpt(object, "messages")); r.plugins = reportPlugins(getOpt(object, "plugins")); r.stats = reportStats(getWarn(object, "stats")); return r; } std::vector Loot::reportPlugins(const QJsonArray& plugins) const { std::vector v; for (auto pluginValue : plugins) { const auto o = convertWarn(pluginValue, "plugin"); if (o.isEmpty()) { continue; } auto p = reportPlugin(o); if (!p.name.isEmpty()) { v.emplace_back(std::move(p)); } } return v; } Loot::Plugin Loot::reportPlugin(const QJsonObject& plugin) const { Plugin p; p.name = getWarn(plugin, "name"); if (p.name.isEmpty()) { return {}; } if (plugin.contains("incompatibilities")) { p.incompatibilities = reportFiles(getOpt(plugin, "incompatibilities")); } if (plugin.contains("messages")) { p.messages = reportMessages(getOpt(plugin, "messages")); } if (plugin.contains("dirty")) { p.dirty = reportDirty(getOpt(plugin, "dirty")); } if (plugin.contains("clean")) { p.clean = reportDirty(getOpt(plugin, "clean")); } if (plugin.contains("missingMasters")) { p.missingMasters = reportStringArray(getOpt(plugin, "missingMasters")); } p.loadsArchive = getOpt(plugin, "loadsArchive", false); p.isMaster = getOpt(plugin, "isMaster", false); p.isLightMaster = getOpt(plugin, "isLightMaster", false); return p; } Loot::Stats Loot::reportStats(const QJsonObject& stats) const { Stats s; s.time = getWarn(stats, "time"); s.lootcliVersion = getWarn(stats, "lootcliVersion"); s.lootVersion = getWarn(stats, "lootVersion"); return s; } std::vector Loot::reportMessages(const QJsonArray& array) const { std::vector v; for (auto messageValue : array) { const auto o = convertWarn(messageValue, "message"); if (o.isEmpty()) { continue; } Message m; const auto type = getWarn(o, "type"); if (type == "info") { m.type = log::Info; } else if (type == "warn") { m.type = log::Warning; } else if (type == "error") { m.type = log::Error; } else { log::error("unknown message type '{}'", type); m.type = log::Info; } m.text = getWarn(o, "text"); if (!m.text.isEmpty()) { v.emplace_back(std::move(m)); } } return v; } std::vector Loot::reportFiles(const QJsonArray& array) const { std::vector v; for (auto&& fileValue : array) { const auto o = convertWarn(fileValue, "file"); if (o.isEmpty()) { continue; } File f; f.name = getWarn(o, "name"); f.displayName = getOpt(o, "displayName"); if (!f.name.isEmpty()) { v.emplace_back(std::move(f)); } } return v; } std::vector Loot::reportDirty(const QJsonArray& array) const { std::vector v; for (auto&& dirtyValue : array) { const auto o = convertWarn(dirtyValue, "dirty"); Dirty d; d.crc = getWarn(o, "crc"); d.itm = getOpt(o, "itm"); d.deletedReferences = getOpt(o, "deletedReferences"); d.deletedNavmesh = getOpt(o, "deletedNavmesh"); d.cleaningUtility = getOpt(o, "cleaningUtility"); d.info = getOpt(o, "info"); v.emplace_back(std::move(d)); } return v; } std::vector Loot::reportStringArray(const QJsonArray& array) const { std::vector v; for (auto&& sv : array) { auto s = convertWarn(sv, "string"); if (s.isEmpty()) { continue; } v.emplace_back(std::move(s)); } return v; } bool runLoot(QWidget* parent, OrganizerCore& core, bool didUpdateMasterList) { core.savePluginList(); try { Loot loot; LootDialog dialog(parent, core, loot); loot.start(parent, core, didUpdateMasterList); dialog.exec(); return dialog.result(); } catch (const UsvfsConnectorException &e) { log::debug("{}", e.what()); return false; } catch (const std::exception &e) { reportError(QObject::tr("failed to run loot: %1").arg(e.what())); return false; } }