Explorar el Código

Minor fixes in lotromanager, removed busy status due to using Qt queued connections in differend thread

Ivan Arkhipov hace 5 años
padre
commit
a7d4db3ba4

+ 22 - 122
src/Legacy-advanced/models/lotromanager.cpp

@@ -1,4 +1,4 @@
-#include "lotromgr.h"
+#include "lotromanager.h"
 #include "filesystem.h"
 
 #include <QtConcurrent/QtConcurrent>
@@ -9,32 +9,25 @@
 #include <iostream>
 #include <fstream>
 
-LotroMgr::LotroMgr(QSettings* app_settings_, QObject *parent) : app_settings(app_settings_),
-    QObject(parent) {
+LotroManager::LotroManager(QSettings* app_settings_, QObject *parent) :
+    QObject(parent), app_settings(app_settings_) {
 }
 
-void LotroMgr::initialiseDatFile(QString file_path) {
-    if (!tryToBlockFile())
-        return;
+void LotroManager::initialiseDatFile(QString file_path) {
     emit processStarted("initialiseDatFile", {file_path});
 
     qDebug() << "Initialising file " << file_path;
 
     if (!FileSystem::fileExists(file_path)) {
         emit caughtError(QString("initialiseDatFile"), {QString("Ошибка инициализации"), QString("Файл " + file_path + " несуществует! Невозможно инициализировать файл ресурсов.")});
-        busy = false;
         return;
     }
 
     file.Initialise((file_path).toStdString(), 0);
-    busy = false;
     emit processFinished("initialiseDatFile", {QString("Success")});
 }
 
-void LotroMgr::changeLocale() {
-    if (!tryToBlockFile())
-        return;
-
+void LotroManager::changeLocale() {
     qDebug() << "Changing locale of dat file...";
     // Setting locale, opposite to current
     auto current_locale = file.GetLocaleManager().GetCurrentLocale();
@@ -52,7 +45,6 @@ void LotroMgr::changeLocale() {
     auto new_current_locale = file.GetLocaleManager().GetCurrentLocale();
     QString new_current_locale_name = (new_current_locale == LOTRO_DAT::DatLocaleManager::PATCHED ? "Русифицированная" : "Оригинальная");
 
-    busy = false;
     if (operation.result == LOTRO_DAT::SUCCESS) {
         emit processFinished("changeLocale", {"Success", new_current_locale_name});
     } else {
@@ -61,17 +53,13 @@ void LotroMgr::changeLocale() {
     }
 }
 
-void LotroMgr::getLocaleFileContents(long long file_id, int locale) {
-    if (!tryToBlockFile())
-        return;
-
+void LotroManager::getLocaleFileContents(long long file_id, int locale) {
     emit processStarted("getFileContents", {file_id, locale});
 
     auto getfile_op = file.GetLocaleManager().GetLocaleFile(file_id, (LOTRO_DAT::DatLocaleManager::LOCALE)locale);
     if (!getfile_op.result) {
         emit caughtError("getFileContents", {"Файл не найден!", QString("Не удаётся найти файл с id ") + QString::number(file_id)});
         emit processFinished("getFileContents", {"Error"});
-        busy = false;
         return;
     }
 
@@ -82,27 +70,20 @@ void LotroMgr::getLocaleFileContents(long long file_id, int locale) {
     if (getfile_op.result == LOTRO_DAT::ERROR) {
         emit caughtError("getFileContents", {"Ошибка извлечения!", QString("Обнаружены некорректные данные файла в словаре! Файл ресурсов мог быть повреждён!\nid = ") + QString::number(file_id) + ", locale_id = " + QString::number(locale)});
         emit processFinished("getFileContents", {"Error"});
-        busy = false;
         return;
     }
 
     LOTRO_DAT::SubfileData result = subfile.PrepareForExport(getfiledata_op.value);
-
-    busy = false;
     emit localeFileContentsReceived(locale, result);
     emit processFinished("getFileContents", {"Success", file_id, locale});
 }
 
-void LotroMgr::importFilesFromDatabase(QString database_path) {
-    if (!tryToBlockFile())
-        return;
-
+void LotroManager::importFilesFromDatabase(QString database_path) {
     emit processStarted("importFilesFromDatabase", {database_path});
 
     if (!FileSystem::fileExists(database_path)) {
         emit caughtError(QString("importFilesFromDatabase"), {QString("Ошибка импорта!"), QString("Файл " + database_path + " не существует! Невозможно инициализировать базу данных!")});
         emit processFinished("importFilesFromDatabase", {QString("Error")});
-        busy = false;
         return;
     }
 
@@ -110,13 +91,11 @@ void LotroMgr::importFilesFromDatabase(QString database_path) {
     if (!db.InitDatabase(database_path.toStdString())) {
         emit caughtError("importFilesFromDatabase", {QString("Ошибка импорта!"), QString("Внутренняя ошибка инициализации базы данных!")});
         emit processFinished("importFilesFromDatabase", {QString("Error")});
-        busy = false;
         return;
     }
 
     auto patch_operation = file.GetPatcher().PatchAllDatabase(&db);
 
-    busy = false;
     if (patch_operation.result == LOTRO_DAT::SUCCESS) {
         emit processFinished("importFilesFromDatabase", {QString("Success"), patch_operation.value});
     } else {
@@ -124,16 +103,12 @@ void LotroMgr::importFilesFromDatabase(QString database_path) {
     }
 }
 
-void LotroMgr::importFile(long long file_id, QString file_path) {
-    if (!tryToBlockFile())
-        return;
-
+void LotroManager::importFile(long long file_id, QString file_path) {
     emit processStarted("importFile", {file_id, file_path});
 
     if (!FileSystem::fileExists(file_path)) {
         emit caughtError("importFile", {QString("Ошибка импорта!"), QString("Файл ") + file_path + QString(" не существует!")});
         emit processFinished("importFile", {QString("Error")});
-        busy = false;
         return;
     }
 
@@ -145,7 +120,6 @@ void LotroMgr::importFile(long long file_id, QString file_path) {
     if (getfile_op.result == LOTRO_DAT::ERROR) {
         emit caughtError("importFile", {QString("Ошибка импорта!"), QString("Файл с id ") + QString::number(file_id) + QString(" не существует в ресурсах игры! Невозможно импортировать :/")});
         emit processFinished("importFile", {QString("Error")});
-        busy = false;
         return;
     }
 
@@ -158,7 +132,6 @@ void LotroMgr::importFile(long long file_id, QString file_path) {
         if (!input_stream.is_open()) {
             emit caughtError("importFile", {QString("Ошибка импорта!"), QString("Текстовый файл ") + file_path + QString(" не удаётся открыть!")});
             emit processFinished("importFile", {QString("Error")});
-            busy = false;
             return;
         }
 
@@ -174,7 +147,6 @@ void LotroMgr::importFile(long long file_id, QString file_path) {
         if (!data_file.isOpen()) {
             emit caughtError("importFile", {QString("Ошибка импорта!"), QString("Файл ") + file_path + QString(" не удаётся открыть!")});
             emit processFinished("importFile", {QString("Error")});
-            busy = false;
             return;
         }
 
@@ -185,7 +157,6 @@ void LotroMgr::importFile(long long file_id, QString file_path) {
 
     auto patchfile_op = file.GetPatcher().PatchFile(data);
 
-    busy = false;
     if (patchfile_op.result == LOTRO_DAT::SUCCESS) {
         emit processFinished("importFile", {QString("Success")});
     } else {
@@ -195,17 +166,13 @@ void LotroMgr::importFile(long long file_id, QString file_path) {
     return;
 }
 
-void LotroMgr::importTextFragment(long long file_id, long long fragment_id,
+void LotroManager::importTextFragment(long long file_id, long long fragment_id,
                                QString fragment_contents, QString arguments) {
-    if (!tryToBlockFile())
-        return;
-
     emit processStarted("importTextFragment", {file_id, fragment_id});
 
     if (fragment_contents.contains("DO_NOT_TOUCH")) {
         emit caughtError("importTextFragment", {"Ошибка формата!", QString("Текстовые данные содержат указатели на аргументы DO_NOT_TOUCH! Такого быть не должно! Пользуйтесь порядком перечисления аргументов внизу")});
         emit processFinished("importTextFragment", {"Error"});
-        busy = false;
         return;
     }
 
@@ -213,7 +180,6 @@ void LotroMgr::importTextFragment(long long file_id, long long fragment_id,
     if (!getfile_op.result) {
         emit caughtError("importTextFragment", {"Файл не найден!", QString("Не удаётся найти в ресурсах файл с id ") + QString::number(file_id)});
         emit processFinished("importTextFragment", {"Error"});
-        busy = false;
         return;
     }
 
@@ -224,7 +190,6 @@ void LotroMgr::importTextFragment(long long file_id, long long fragment_id,
     if (getfile_op.result == LOTRO_DAT::ERROR) {
         emit caughtError("importTextFragment", {"Ошибка импорта!", QString("Обнаружены некорректные данные файла в словаре! Файл ресурсов мог быть повреждён!\nid = ") + QString::number(file_id)});
         emit processFinished("importTextFragment", {"Error"});
-        busy = false;
         return;
     }
 
@@ -232,7 +197,6 @@ void LotroMgr::importTextFragment(long long file_id, long long fragment_id,
     if (data.Empty()) {
         emit caughtError("importTextFragment", {"Ошибка импорта!", QString("Не удалось подготовить файл к изменению фрагмента!\nid = ") + QString::number(file_id)});
         emit processFinished("importTextFragment", {"Error"});
-        busy = false;
         return;
     }
 
@@ -240,7 +204,6 @@ void LotroMgr::importTextFragment(long long file_id, long long fragment_id,
     if (beginning == std::u16string::npos) {
         emit caughtError("importTextFragment", {"Ошибка импорта!", QString("Не удалось найти фрагмент в файле!\nid = ") + QString::number(file_id) + "\nfragment_id = " + QString::number(fragment_id)});
         emit processFinished("importTextFragment", {"Error"});
-        busy = false;
         return;
     }
 
@@ -252,7 +215,6 @@ void LotroMgr::importTextFragment(long long file_id, long long fragment_id,
 
     auto patchfile_op = file.GetPatcher().PatchFile(data);
 
-    busy = false;
     if (patchfile_op.result == LOTRO_DAT::SUCCESS) {
         emit processFinished("importTextFragment", {QString("Success")});
     } else {
@@ -261,17 +223,13 @@ void LotroMgr::importTextFragment(long long file_id, long long fragment_id,
     }
 }
 
-void LotroMgr::getTextFragment(long long file_id, long long fragment_id) {
-    if (!tryToBlockFile())
-        return;
-
+void LotroManager::getTextFragment(long long file_id, long long fragment_id) {
     emit processStarted("getTextFragment", {file_id, fragment_id});
 
     auto getfile_op = file.GetFileSystem().GetFile(file_id);
     if (!getfile_op.result) {
         emit caughtError("getTextFragment", {"Файл не найден!", QString("Не удаётся найти в ресурсах файл с id ") + QString::number(file_id)});
         emit processFinished("getTextFragment", {"Error"});
-        busy = false;
         return;
     }
 
@@ -282,7 +240,6 @@ void LotroMgr::getTextFragment(long long file_id, long long fragment_id) {
     if (getfile_op.result == LOTRO_DAT::ERROR) {
         emit caughtError("getTextFragment", {"Ошибка импорта!", QString("Обнаружены некорректные данные файла в словаре! Файл ресурсов мог быть повреждён!\nid = ") + QString::number(file_id)});
         emit processFinished("getTextFragment", {"Error"});
-        busy = false;
         return;
     }
 
@@ -290,7 +247,6 @@ void LotroMgr::getTextFragment(long long file_id, long long fragment_id) {
     if (data.Empty()) {
         emit caughtError("getTextFragment", {"Ошибка импорта!", QString("Не удалось подготовить файл к изменению фрагмента!\nid = ") + QString::number(file_id)});
         emit processFinished("getTextFragment", {"Error"});
-        busy = false;
         return;
     }
 
@@ -298,7 +254,6 @@ void LotroMgr::getTextFragment(long long file_id, long long fragment_id) {
     if (beginning == std::u16string::npos) {
         emit caughtError("importTextFragment", {"Ошибка импорта!", QString("Не удалось найти фрагмент в файле!\nid = ") + QString::number(file_id) + "\nfragment_id = " + QString::number(fragment_id)});
         emit processFinished("importTextFragment", {"Error"});
-        busy = false;
         return;
     }
 
@@ -315,22 +270,16 @@ void LotroMgr::getTextFragment(long long file_id, long long fragment_id) {
     if (splitted_fragment.size() != 3) {
         emit caughtError("importTextFragment", {"Ошибка импорта!", QString("Получены некорректные данные фрагмента!\nДанные:") + QString::fromStdU16String(str)});
         emit processFinished("importTextFragment", {"Error"});
-        busy = false;
         return;
     }
 
-    busy = false;
     emit textFragmentReceived(splitted_fragment.at(1), splitted_fragment.at(2));
     emit processFinished("importTextFragment", {"Success"});
 }
 
-void LotroMgr::createCoreStatusFile(QString output_filename) {
-    if (!tryToBlockFile())
-        return;
-
+void LotroManager::createCoreStatusFile(QString output_filename) {
     emit processStarted("createCoreStatusFile", {output_filename});
     auto gatherinfo_op = file.GatherInformation(output_filename.toStdString());
-    busy = false;
 
     if (gatherinfo_op.result == LOTRO_DAT::SUCCESS) {
         emit processFinished("createCoreStatusFile", {"Success", output_filename});
@@ -340,14 +289,9 @@ void LotroMgr::createCoreStatusFile(QString output_filename) {
     }
 }
 
-void LotroMgr::extractSingleFile(QString output_filename, long long file_id) {
-    if (!tryToBlockFile())
-        return;
-
+void LotroManager::extractSingleFile(QString output_filename, long long file_id) {
     emit processStarted("extractSingleFile", {output_filename, file_id});
     auto extractfile_op = file.GetExporter().ExtractFileById(file_id, output_filename.toStdString());
-    busy = false;
-
     if (extractfile_op.result == LOTRO_DAT::SUCCESS) {
         emit processFinished("extractSingleFile", {"Success", output_filename, file_id});
     } else {
@@ -356,21 +300,16 @@ void LotroMgr::extractSingleFile(QString output_filename, long long file_id) {
     }
 }
 
-void LotroMgr::extractSingleFileToDatabase(QString database_path, long long file_id) {
-    if (!tryToBlockFile())
-        return;
-
+void LotroManager::extractSingleFileToDatabase(QString database_path, long long file_id) {
     emit processStarted("extractSingleFileToDatabase", {database_path, file_id});
     LOTRO_DAT::Database db;
     if (!db.InitDatabase(database_path.toStdString())) {
         emit caughtError("extractSingleFileToDatabase", {"Ошибка экспорта!", QString("Не удаётся создать/открыть базу данных " + database_path)});
         emit processFinished("extractSingleFileToDatabase", {"Error", database_path});
-        busy = false;
         return;
     }
 
     auto extractfile_op = file.GetExporter().ExtractFileById(file_id, &db);
-    busy = false;
 
     if (extractfile_op.result == LOTRO_DAT::SUCCESS) {
         emit processFinished("extractSingleFileToDatabase", {"Success", database_path, file_id});
@@ -380,14 +319,9 @@ void LotroMgr::extractSingleFileToDatabase(QString database_path, long long file
     }
 }
 
-void LotroMgr::extractGrouppedFiles(QString output_foldername, LOTRO_DAT::FILE_TYPE type) {
-    if (!tryToBlockFile())
-        return;
-
+void LotroManager::extractGrouppedFiles(QString output_foldername, LOTRO_DAT::FILE_TYPE type) {
     emit processStarted("extractGrouppedFiles", {output_foldername, type});
     auto extractfile_op = file.GetExporter().ExtractAllFilesByType(type, output_foldername.toStdString());
-    busy = false;
-
     if (extractfile_op.result == LOTRO_DAT::SUCCESS) {
         emit processFinished("extractGrouppedFiles", {"Success", output_foldername, type, extractfile_op.value});
     } else {
@@ -396,22 +330,16 @@ void LotroMgr::extractGrouppedFiles(QString output_foldername, LOTRO_DAT::FILE_T
     }
 }
 
-void LotroMgr::extractGrouppedFilesToDatabase(QString database_path, LOTRO_DAT::FILE_TYPE type) {
-    if (!tryToBlockFile())
-        return;
-
+void LotroManager::extractGrouppedFilesToDatabase(QString database_path, LOTRO_DAT::FILE_TYPE type) {
     emit processStarted(QString("extractGrouppedFilesToDatabase"), {database_path, type});
     LOTRO_DAT::Database db;
     if (!db.InitDatabase(database_path.toStdString())) {
         emit caughtError("extractGrouppedFilesToDatabase", {"Ошибка экспорта!", QString("Не удаётся создать/открыть базу данных " + database_path)});
         emit processFinished("extractGrouppedFilesToDatabase", {"Error", database_path});
-        busy = false;
         return;
     }
 
     auto extractfile_op = file.GetExporter().ExtractAllFilesByType(type, &db);
-    busy = false;
-
     if (extractfile_op.result == LOTRO_DAT::SUCCESS) {
         emit processFinished("extractGrouppedFilesToDatabase", {"Success", database_path, type, extractfile_op.value});
     } else {
@@ -420,10 +348,7 @@ void LotroMgr::extractGrouppedFilesToDatabase(QString database_path, LOTRO_DAT::
     }
 }
 
-void LotroMgr::getUnactiveCategories() {
-    if (!tryToBlockFile())
-        return;
-
+void LotroManager::getUnactiveCategories() {
     emit processStarted("getUnactiveCategories", {});
 
     const std::set<long long>& categories = file.GetLocaleManager().GetInactiveCategories();
@@ -433,14 +358,11 @@ void LotroMgr::getUnactiveCategories() {
         result << QString::number(category);
     }
 
-    busy = false;
     emit unactiveCategoriesReceived(result);
     emit processFinished("getUnactiveCategories", {"Success"});
 }
 
-void LotroMgr::startGame() {
-    if (!tryToBlockFile())
-        return;
+void LotroManager::startGame() {
     emit processStarted("startGame", {});
 
     QStringList args;
@@ -473,21 +395,16 @@ void LotroMgr::startGame() {
         emit caughtError("startGame", {"Ошибка запуска игры!", QString("Не удалось найти файл LotroLauncher в папке: ") + app_settings->value("Local", "folder").toString()});
         emit processFinished("startGame", {"Error"});
     }
-    busy = false;
 }
 
 
-void LotroMgr::getLocaleFileInfo(long long file_id, int locale) {
-    if (!tryToBlockFile())
-        return;
-
+void LotroManager::getLocaleFileInfo(long long file_id, int locale) {
     emit processStarted("getLocaleFileInfo", {file_id, locale});
 
     auto getfile_op = file.GetLocaleManager().GetLocaleFile(file_id, (LOTRO_DAT::DatLocaleManager::LOCALE)locale);
     if (!getfile_op.result) {
         emit caughtError("getLocaleFileInfo", {"Файл не найден!", QString("Не удаётся найти в ресурсах файл с id ") + QString::number(file_id)});
         emit processFinished("getLocaleFileInfo", {"Error"});
-        busy = false;
         return;
     }
 
@@ -505,22 +422,17 @@ void LotroMgr::getLocaleFileInfo(long long file_id, int locale) {
                      "unknown2: " + QString::number(subfile.file_id()) + "\n"
                      "category: " + QString::number(subfile.category) + "\n";
 
-    busy = false;
     emit localeFileInfoReceived(result);
     emit processFinished("getLocaleFileInfo", {"Success"});
 }
 
-void LotroMgr::getFileInfo(long long file_id) {
-    if (!tryToBlockFile())
-        return;
-
+void LotroManager::getFileInfo(long long file_id) {
     emit processStarted(QString("getFileInfo"), {file_id});
 
     auto getfile_op = file.GetFileSystem().GetFile(file_id);
     if (!getfile_op.result) {
         emit caughtError("getFileInfo", {"Файл не найден!", QString("Не удаётся найти файл с id ") + QString::number(file_id)});
         emit processFinished("getFileInfo", {"Error"});
-        busy = false;
         return;
     }
 
@@ -537,31 +449,19 @@ void LotroMgr::getFileInfo(long long file_id) {
                      "block_size: " + QString::number(subfile.block_size()) + "\n"
                      "unknown2: " + QString::number(subfile.file_id()) + "\n";
 
-    busy = false;
     emit localeFileInfoReceived(result);
     emit processFinished("getFileInfo", {"Success"});
 }
 
 
-bool LotroMgr::initialised() {
+bool LotroManager::initialised() {
     return file.Initialized();
 }
 
-int LotroMgr::currentLocale() {
+int LotroManager::currentLocale() {
     return file.GetLocaleManager().GetCurrentLocale();
 }
 
-bool LotroMgr::notPatched() {
+bool LotroManager::notPatched() {
     return file.GetStatusModule().CheckIfNotPatched();
 }
-
-
-bool LotroMgr::tryToBlockFile()
-{
-    if (busy) {
-        emit caughtError("common", {QString("Ошибка инициализации!"), QString("Уже выполняется другой процесс с ресурсами игры! Невозможно инициализировать во время выполнения другого процесса!")});
-        return false;
-    }
-    busy = true;
-}
-

+ 2 - 6
src/Legacy-advanced/models/lotromanager.h

@@ -7,12 +7,12 @@
 
 #include <LotroDat/LotroDat.h>
 
-class LotroMgr : public QObject
+class LotroManager : public QObject
 {
     Q_OBJECT
 
 public:
-    explicit LotroMgr(QSettings* app_settings_, QObject *parent = nullptr);
+    explicit LotroManager(QSettings* app_settings_, QObject *parent = nullptr);
 
     bool initialised();
 
@@ -53,9 +53,6 @@ public slots:
 
     void getFileInfo(long long file_id);
 
-private:
-    bool tryToBlockFile();
-
 signals:
     // general signals. First argument is process_name, second - processed values
     void processStarted(QString, QVector<QVariant>);
@@ -70,7 +67,6 @@ signals:
 
 private:
     LOTRO_DAT::DatFile file;
-    bool busy;
     QSettings* app_settings;
 };