better error handling for extensions

This commit is contained in:
Akash Mozumdar 2019-06-04 23:14:46 -04:00
parent cf90539d09
commit 64bfb4596e
5 changed files with 30 additions and 18 deletions

View File

@ -7,8 +7,12 @@
#include <QMimeData>
#include <QUrl>
#include <QLabel>
#include <QMessageBox>
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<Extension> 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()));
}
}

View File

@ -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;

View File

@ -3,6 +3,7 @@
#include "qtcommon.h"
#include "extenwindow.h"
#include "host/host.h"
#include <QSettings>
namespace Ui
{

View File

@ -5,12 +5,9 @@
#include <QVector>
#include <QHash>
#include <QMainWindow>
#include <QDialog>
#include <QFile>
#include <QFileInfo>
#include <QDir>
#include <QRegularExpression>
#include <QSettings>
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() }; }

View File

@ -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)";