mirror of
https://github.com/Artikash/Textractor.git
synced 2025-01-10 01:33:51 +08:00
better error handling for extensions
This commit is contained in:
parent
cf90539d09
commit
64bfb4596e
@ -7,8 +7,12 @@
|
|||||||
#include <QMimeData>
|
#include <QMimeData>
|
||||||
#include <QUrl>
|
#include <QUrl>
|
||||||
#include <QLabel>
|
#include <QLabel>
|
||||||
|
#include <QMessageBox>
|
||||||
|
|
||||||
extern const char* EXTENSIONS;
|
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;
|
extern const char* EXTEN_WINDOW_INSTRUCTIONS;
|
||||||
|
|
||||||
namespace
|
namespace
|
||||||
@ -22,15 +26,19 @@ namespace
|
|||||||
concurrency::reader_writer_lock extenMutex;
|
concurrency::reader_writer_lock extenMutex;
|
||||||
std::vector<Extension> extensions;
|
std::vector<Extension> extensions;
|
||||||
|
|
||||||
void Load(QString extenName)
|
bool Load(QString extenName)
|
||||||
{
|
{
|
||||||
if (extenName == ITH_DLL) return;
|
|
||||||
// Extension is dll and exports "OnNewSentence"
|
// 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);
|
if (auto callback = (decltype(Extension::callback))GetProcAddress(LoadLibraryOnce(S(extenName)), "OnNewSentence"))
|
||||||
extensions.push_back({ S(extenName), callback });
|
{
|
||||||
|
std::scoped_lock writeLock(extenMutex);
|
||||||
|
extensions.push_back({ S(extenName), callback });
|
||||||
|
return true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Unload(int index)
|
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)
|
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
|
// 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)
|
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()));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -24,7 +24,6 @@ public:
|
|||||||
private:
|
private:
|
||||||
inline static constexpr auto EXTEN_SAVE_FILE = u8"SavedExtensions.txt";
|
inline static constexpr auto EXTEN_SAVE_FILE = u8"SavedExtensions.txt";
|
||||||
|
|
||||||
void Add(QFileInfo extenFile);
|
|
||||||
void Sync();
|
void Sync();
|
||||||
bool eventFilter(QObject* target, QEvent* event) override;
|
bool eventFilter(QObject* target, QEvent* event) override;
|
||||||
void keyPressEvent(QKeyEvent* event) override;
|
void keyPressEvent(QKeyEvent* event) override;
|
||||||
|
@ -3,6 +3,7 @@
|
|||||||
#include "qtcommon.h"
|
#include "qtcommon.h"
|
||||||
#include "extenwindow.h"
|
#include "extenwindow.h"
|
||||||
#include "host/host.h"
|
#include "host/host.h"
|
||||||
|
#include <QSettings>
|
||||||
|
|
||||||
namespace Ui
|
namespace Ui
|
||||||
{
|
{
|
||||||
|
@ -5,12 +5,9 @@
|
|||||||
#include <QVector>
|
#include <QVector>
|
||||||
#include <QHash>
|
#include <QHash>
|
||||||
#include <QMainWindow>
|
#include <QMainWindow>
|
||||||
#include <QDialog>
|
|
||||||
#include <QFile>
|
#include <QFile>
|
||||||
#include <QFileInfo>
|
#include <QFileInfo>
|
||||||
#include <QDir>
|
#include <QDir>
|
||||||
#include <QRegularExpression>
|
|
||||||
#include <QSettings>
|
|
||||||
|
|
||||||
struct QTextFile : QFile { QTextFile(QString name, QIODevice::OpenMode mode) : QFile(name) { open(mode | QIODevice::Text); } };
|
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() }; }
|
inline std::wstring S(const QString& S) { return { S.toStdWString() }; }
|
||||||
|
4
text.cpp
4
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
|
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)
|
(Does not work if running as administrator)
|
||||||
Drag and drop within the list to reorder
|
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)";
|
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?";
|
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* 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)";
|
extern const char* SEARCH_PATTERN = u8"Search pattern (hex byte array)";
|
||||||
|
Loading…
x
Reference in New Issue
Block a user