summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorisanae <14251494+isanae@users.noreply.github.com>2019-11-25 00:47:45 -0500
committerisanae <14251494+isanae@users.noreply.github.com>2019-11-25 00:47:45 -0500
commit0e45044dbd724e9050bea00511585dc023afe144 (patch)
tree46841f3a56f83670c5ddf9e6df72547e808fa77e
parent48fcf9521f796bc3b6e536545877a9ea2e37360d (diff)
fixed cancel button
debug logs, some cleanup
-rw-r--r--src/loot.cpp47
-rw-r--r--src/lootdialog.cpp92
-rw-r--r--src/lootdialog.h11
3 files changed, 98 insertions, 52 deletions
diff --git a/src/loot.cpp b/src/loot.cpp
index 2faf9c2e..9c8cf8c4 100644
--- a/src/loot.cpp
+++ b/src/loot.cpp
@@ -199,6 +199,7 @@ Loot::~Loot()
}
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) {
@@ -211,6 +212,8 @@ Loot::~Loot()
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();
@@ -271,8 +274,19 @@ bool Loot::start(QWidget* parent, OrganizerCore& core, bool didUpdateMasterList)
core.pluginList()->clearAdditionalInformation();
+ log::debug("starting loot thread");
+
m_thread.reset(QThread::create([&]{
- lootThread();
+ try
+ {
+ lootThread();
+ }
+ catch(...)
+ {
+ log::error("unhandled exception in loot thread");
+ }
+
+ log::debug("finishing loot thread");
emit finished();
}));
@@ -283,7 +297,10 @@ bool Loot::start(QWidget* parent, OrganizerCore& core, bool didUpdateMasterList)
void Loot::cancel()
{
- m_cancel = true;
+ if (!m_cancel) {
+ log::debug("loot received cancel request");
+ m_cancel = true;
+ }
}
bool Loot::result() const
@@ -303,6 +320,8 @@ const Loot::Report& Loot::report() const
void Loot::lootThread()
{
+ ::SetThreadDescription(GetCurrentThread(), L"loot");
+
try {
m_result = false;
@@ -321,10 +340,13 @@ 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;
}
@@ -336,9 +358,13 @@ bool Loot::waitForCompletion()
}
if (m_cancel) {
- // terminate and wait to finish
+ 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;
}
@@ -396,6 +422,12 @@ 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 (;;) {
@@ -440,7 +472,7 @@ void Loot::processMessage(const lootcli::Message& m)
void Loot::processOutputFile()
{
- log::info("parsing json output file at '{}'", m_outPath);
+ log::debug("parsing json output file at '{}'", m_outPath);
QFile outFile(m_outPath);
if (!outFile.open(QIODevice::ReadOnly)) {
@@ -645,20 +677,13 @@ std::vector<QString> Loot::reportStringArray(const QJsonArray& array) const
bool runLoot(QWidget* parent, OrganizerCore& core, bool didUpdateMasterList)
{
- //m_OrganizerCore.currentProfile()->writeModlistNow();
core.savePluginList();
- //Create a backup of the load orders w/ LOOT in name
- //to make sure that any sorting is easily undo-able.
- //Need to figure out how I want to do that.
-
try {
Loot loot;
LootDialog dialog(parent, core, loot);
loot.start(parent, core, didUpdateMasterList);
-
- dialog.setText(QObject::tr("Please wait while LOOT is running"));
dialog.exec();
return dialog.result();
diff --git a/src/lootdialog.cpp b/src/lootdialog.cpp
index 80664415..43929c00 100644
--- a/src/lootdialog.cpp
+++ b/src/lootdialog.cpp
@@ -8,6 +8,29 @@
using namespace MOBase;
+QString progressToString(lootcli::Progress p)
+{
+ using P = lootcli::Progress;
+
+ static const std::map<P, QString> map = {
+ {P::CheckingMasterlistExistence, QObject::tr("Checking masterlist existence")},
+ {P::UpdatingMasterlist, QObject::tr("Updating masterlist")},
+ {P::LoadingLists, QObject::tr("Loading lists")},
+ {P::ReadingPlugins, QObject::tr("Reading plugins")},
+ {P::SortingPlugins, QObject::tr("Sorting plugins")},
+ {P::WritingLoadorder, QObject::tr("Writing loadorder.txt")},
+ {P::ParsingLootMessages, QObject::tr("Parsing loot messages")},
+ {P::Done, QObject::tr("Done")}
+ };
+
+ auto itor = map.find(p);
+ if (itor == map.end()) {
+ return QString("unknown progress %1").arg(static_cast<int>(p));
+ } else {
+ return itor->second;
+ }
+}
+
MarkdownDocument::MarkdownDocument(QObject* parent)
: QObject(parent)
@@ -74,7 +97,11 @@ void LootDialog::setText(const QString& s)
void LootDialog::setProgress(lootcli::Progress p)
{
- setText(progressToString(p));
+ // don't overwrite the "stopping loot" message even if lootcli generates a new
+ // progress message
+ if (!m_cancelling) {
+ setText(progressToString(p));
+ }
if (p == lootcli::Progress::Done) {
ui->progressBar->setRange(0, 1);
@@ -82,24 +109,6 @@ void LootDialog::setProgress(lootcli::Progress p)
}
}
-QString LootDialog::progressToString(lootcli::Progress p)
-{
- using P = lootcli::Progress;
-
- switch (p)
- {
- case P::CheckingMasterlistExistence: return tr("Checking masterlist existence");
- case P::UpdatingMasterlist: return tr("Updating masterlist");
- case P::LoadingLists: return tr("Loading lists");
- case P::ReadingPlugins: return tr("Reading plugins");
- case P::SortingPlugins: return tr("Sorting plugins");
- case P::WritingLoadorder: return tr("Writing loadorder.txt");
- case P::ParsingLootMessages: return tr("Parsing loot messages");
- case P::Done: return tr("Done");
- default: return QString("unknown progress %1").arg(static_cast<int>(p));
- }
-}
-
void LootDialog::addOutput(const QString& s)
{
if (m_core.settings().diagnostics().lootLogLevel() > lootcli::LogLevels::Debug) {
@@ -125,8 +134,12 @@ bool LootDialog::result() const
void LootDialog::cancel()
{
if (!m_finished && !m_cancelling) {
- addLineOutput(tr("Stopping LOOT..."));
+ log::debug("loot dialog: cancelling");
m_loot.cancel();
+
+ setText(tr("Stopping LOOT..."));
+ addLineOutput("stopping loot");
+
ui->buttons->setEnabled(false);
m_cancelling = true;
}
@@ -138,6 +151,22 @@ void LootDialog::openReport()
shell::Open(path);
}
+void LootDialog::accept()
+{
+ // no-op
+}
+
+void LootDialog::reject()
+{
+ if (m_finished) {
+ log::debug("loot dialog reject: loot finished, closing");
+ QDialog::reject();
+ } else {
+ log::debug("loot dialog reject: not finished, cancelling");
+ cancel();
+ }
+}
+
void LootDialog::createUI()
{
ui->setupUi(this);
@@ -169,7 +198,7 @@ void LootDialog::createUI()
new ExpanderWidget(ui->details, ui->detailsPanel);
- connect(ui->buttons, &QDialogButtonBox::clicked, [&](auto* b){ onButton(b); });
+ ui->buttons->setStandardButtons(QDialogButtonBox::Cancel);
resize(650, 450);
}
@@ -177,24 +206,15 @@ void LootDialog::createUI()
void LootDialog::closeEvent(QCloseEvent* e)
{
if (m_finished) {
+ log::debug("loot dialog close event: finished, closing");
QDialog::closeEvent(e);
} else {
+ log::debug("loot dialog close event: not finished, cancelling");
cancel();
e->ignore();
}
}
-void LootDialog::onButton(QAbstractButton* b)
-{
- if (ui->buttons->buttonRole(b) == QDialogButtonBox::RejectRole) {
- if (m_finished) {
- close();
- } else {
- cancel();
- }
- }
-}
-
void LootDialog::addLineOutput(const QString& line)
{
ui->output->appendPlainText(line);
@@ -202,12 +222,16 @@ void LootDialog::addLineOutput(const QString& line)
void LootDialog::onFinished()
{
+ log::debug("loot dialog: loot is finished");
+
m_finished = true;
if (m_cancelling) {
+ log::debug("loot dialog: was cancelling, closing");
close();
} else {
- handleReport();
+ log::debug("loot dialog: showing report");
+ showReport();
ui->openJsonReport->setEnabled(true);
ui->buttons->setStandardButtons(QDialogButtonBox::Close);
}
@@ -224,7 +248,7 @@ void LootDialog::log(log::Levels lv, const QString& s)
}
}
-void LootDialog::handleReport()
+void LootDialog::showReport()
{
const auto& lootReport = m_loot.report();
diff --git a/src/lootdialog.h b/src/lootdialog.h
index df9e546d..fcdeb304 100644
--- a/src/lootdialog.h
+++ b/src/lootdialog.h
@@ -48,16 +48,14 @@ public:
void setText(const QString& s);
void setProgress(lootcli::Progress p);
- QString progressToString(lootcli::Progress p);
-
void addOutput(const QString& s);
-
bool result() const;
-
void cancel();
-
void openReport();
+ void accept() override;
+ void reject() override;
+
private:
std::unique_ptr<Ui::LootDialog> ui;
OrganizerCore& m_core;
@@ -68,11 +66,10 @@ private:
void createUI();
void closeEvent(QCloseEvent* e) override;
- void onButton(QAbstractButton* b);
void addLineOutput(const QString& line);
void onFinished();
void log(MOBase::log::Levels lv, const QString& s);
- void handleReport();
+ void showReport();
};
#endif // MODORGANIZER_LOOTDIALOG_H