From 64bfb4596ec7fce7036a008adaa8976a62d60f64 Mon Sep 17 00:00:00 2001 From: Akash Mozumdar Date: Tue, 4 Jun 2019 23:14:46 -0400 Subject: [PATCH] better error handling for extensions --- GUI/extenwindow.cpp | 39 +++++++++++++++++++++++++-------------- GUI/extenwindow.h | 1 - GUI/mainwindow.h | 1 + GUI/qtcommon.h | 3 --- text.cpp | 4 ++++ 5 files changed, 30 insertions(+), 18 deletions(-) diff --git a/GUI/extenwindow.cpp b/GUI/extenwindow.cpp index 0c6450d..28e4fb3 100644 --- a/GUI/extenwindow.cpp +++ b/GUI/extenwindow.cpp @@ -7,8 +7,12 @@ #include #include #include +#include extern const char* EXTENSIONS; +extern const char* INVALID_EXTENSION; +extern const char* CONFIRM_EXTENSION_OVERWRITE; +extern const char* EXTENSION_WRITE_ERROR; extern const char* EXTEN_WINDOW_INSTRUCTIONS; namespace @@ -22,15 +26,19 @@ namespace concurrency::reader_writer_lock extenMutex; std::vector extensions; - void Load(QString extenName) + bool Load(QString extenName) { - if (extenName == ITH_DLL) return; // Extension is dll and exports "OnNewSentence" - if (auto callback = (decltype(Extension::callback))GetProcAddress(LoadLibraryOnce(S(extenName)), "OnNewSentence")) + if (QTextFile(extenName + ".dll", QIODevice::ReadOnly).readAll().contains("OnNewSentence")) { - std::scoped_lock writeLock(extenMutex); - extensions.push_back({ S(extenName), callback }); + if (auto callback = (decltype(Extension::callback))GetProcAddress(LoadLibraryOnce(S(extenName)), "OnNewSentence")) + { + std::scoped_lock writeLock(extenMutex); + extensions.push_back({ S(extenName), callback }); + return true; + } } + return false; } void Unload(int index) @@ -95,14 +103,6 @@ void ExtenWindow::Sync() } } -void ExtenWindow::Add(QFileInfo extenFile) -{ - if (extenFile.suffix() != "dll") return; - QFile::copy(extenFile.absoluteFilePath(), extenFile.fileName()); - Load(extenFile.completeBaseName()); - Sync(); -} - bool ExtenWindow::eventFilter(QObject* target, QEvent* event) { // See https://stackoverflow.com/questions/1224432/how-do-i-respond-to-an-internal-drag-and-drop-operation-using-a-qlistwidget/1528215 @@ -132,5 +132,16 @@ void ExtenWindow::dragEnterEvent(QDragEnterEvent* event) void ExtenWindow::dropEvent(QDropEvent* event) { - for (auto file : event->mimeData()->urls()) Add(file.toLocalFile()); + for (auto file : event->mimeData()->urls()) + { + QFileInfo extenFile = file.toLocalFile(); + if (extenFile.suffix() != "dll") continue; + if (QFile::exists(extenFile.fileName()) && extenFile.absolutePath() != QDir::currentPath()) + { + if (QMessageBox::question(this, EXTENSIONS, CONFIRM_EXTENSION_OVERWRITE) == QMessageBox::Yes) QFile::remove(extenFile.fileName()); + if (!QFile::copy(extenFile.absoluteFilePath(), extenFile.fileName())) QMessageBox::warning(this, EXTENSIONS, EXTENSION_WRITE_ERROR); + } + if (Load(extenFile.completeBaseName())) Sync(); + else QMessageBox::information(this, EXTENSIONS, QString(INVALID_EXTENSION).arg(extenFile.fileName())); + } } diff --git a/GUI/extenwindow.h b/GUI/extenwindow.h index cc2d1eb..3bd1c3c 100644 --- a/GUI/extenwindow.h +++ b/GUI/extenwindow.h @@ -24,7 +24,6 @@ public: private: inline static constexpr auto EXTEN_SAVE_FILE = u8"SavedExtensions.txt"; - void Add(QFileInfo extenFile); void Sync(); bool eventFilter(QObject* target, QEvent* event) override; void keyPressEvent(QKeyEvent* event) override; diff --git a/GUI/mainwindow.h b/GUI/mainwindow.h index cee7ec2..c271d61 100644 --- a/GUI/mainwindow.h +++ b/GUI/mainwindow.h @@ -3,6 +3,7 @@ #include "qtcommon.h" #include "extenwindow.h" #include "host/host.h" +#include namespace Ui { diff --git a/GUI/qtcommon.h b/GUI/qtcommon.h index 83594b2..5d6f42c 100644 --- a/GUI/qtcommon.h +++ b/GUI/qtcommon.h @@ -5,12 +5,9 @@ #include #include #include -#include #include #include #include -#include -#include struct QTextFile : QFile { QTextFile(QString name, QIODevice::OpenMode mode) : QFile(name) { open(mode | QIODevice::Text); } }; inline std::wstring S(const QString& S) { return { S.toStdWString() }; } diff --git a/text.cpp b/text.cpp index 785d0d7..63bdca4 100644 --- a/text.cpp +++ b/text.cpp @@ -41,7 +41,11 @@ const char* SAVE_SETTINGS = u8"Save settings"; const char* EXTEN_WINDOW_INSTRUCTIONS = u8R"(Drag and drop extension (.dll) files here from your computer to add them (Does not work if running as administrator) Drag and drop within the list to reorder +(Extensions are used from top to bottom: order DOES matter) Press delete with an extension selected to remove it)"; +const char* INVALID_EXTENSION = u8"%1 is an invalid extension"; +const char* CONFIRM_EXTENSION_OVERWRITE = u8"Another version of this extension already exists, do you want to delete and overwrite it?"; +const char* EXTENSION_WRITE_ERROR = u8"Failed to save extension"; const char* USE_JP_LOCALE = u8"Emulate japanese locale?"; extern const char* HOOH_SEARCH_UNSTABLE_WARNING = u8"Searching for hooks is unstable! Be prepared for your game to crash!"; extern const char* SEARCH_PATTERN = u8"Search pattern (hex byte array)";