فهرست منبع

Added check if game running & fixed freezing during application starup

Ivan Arkhipov 4 سال پیش
والد
کامیت
6d87d993c4

+ 25 - 12
src/Legacy/legacyapplication.cpp

@@ -7,6 +7,7 @@
 #include <QFontDatabase>
 #include <QMessageLogContext>
 #include <QApplication>
+#include <QtConcurrent/QtConcurrent>
 
 #include "utils.h"
 #include "widgets/mainwindow.h"
@@ -18,6 +19,8 @@ extern Q_CORE_EXPORT int qt_ntfs_permission_lookup;
 
 
 LegacyApplication::LegacyApplication() {
+    qRegisterMetaType<AppErrorStatus>();
+
     qInstallMessageHandler(logMessageHandler);
 
     modules_init_timer_.setInterval(1 * 1000); // one second.
@@ -66,6 +69,15 @@ bool LegacyApplication::init() {
     gui = new MainWindow();
 
     qDebug() << "Starting functional modules initialisation...";
+    connect(this, &LegacyApplication::ErrorStatusChanged, this, [this](AppErrorStatus status){
+        if (status == E_NO_SERVER_CONNECTION) {
+            qWarning() << "LegacyApplication: Cannot connect to server - restarting in 1 second!";
+            modules_init_timer_.start();
+        } else if (status == E_LOTRO_RUNNING) {
+            qWarning() << "LegacyApplication: found running lotro instance!";
+            modules_init_timer_.start();
+        }
+    });
     InitModules();
 
     qDebug() << "Successfully finished the initialization!!!";
@@ -74,18 +86,19 @@ bool LegacyApplication::init() {
 
 void LegacyApplication::InitModules() {
     modules_init_timer_.stop();
-    AppErrorStatus status = CheckAppPrerequesities();
-    emit ErrorStatusChanged(status);
-    qDebug() << "NEW ERROR STATUS: " << status;
-
-    if (status == E_NO_ERRORS) {
-        QMetaObject::invokeMethod(&PatchInstaller::instance(), &PatchInstaller::init, Qt::BlockingQueuedConnection);
-        QMetaObject::invokeMethod(&PatchDownloader::instance(), &PatchDownloader::init, Qt::BlockingQueuedConnection);
-        QMetaObject::invokeMethod(&PatchDownloader::instance(), &PatchDownloader::checkForUpdates, Qt::QueuedConnection);
-    } else if (status == E_NO_SERVER_CONNECTION) {
-        qWarning() << "LegacyApplication: Cannot connect to server - restarting in 1 second!";
-        modules_init_timer_.start();
-    }
+
+    QtConcurrent::run([this]() {
+        emit ModulesInitializationStarted();
+        AppErrorStatus status = CheckAppPrerequesities();
+        emit ErrorStatusChanged(status);
+        qDebug() << "NEW ERROR STATUS: " << status;
+
+        if (status == E_NO_ERRORS) {
+            QMetaObject::invokeMethod(&PatchInstaller::instance(), &PatchInstaller::init, Qt::BlockingQueuedConnection);
+            QMetaObject::invokeMethod(&PatchDownloader::instance(), &PatchDownloader::init, Qt::BlockingQueuedConnection);
+            QMetaObject::invokeMethod(&PatchDownloader::instance(), &PatchDownloader::checkForUpdates, Qt::QueuedConnection);
+        }
+    });
 }
 
 void LegacyApplication::close() {

+ 3 - 0
src/Legacy/legacyapplication.h

@@ -28,6 +28,7 @@ public:
 
 signals:
     void ErrorStatusChanged(AppErrorStatus status);
+    void ModulesInitializationStarted();
 
 public slots:
     void InitModules();
@@ -41,4 +42,6 @@ private:
     QTimer modules_init_timer_;
 };
 
+Q_DECLARE_METATYPE(AppErrorStatus);
+
 #endif // LEGACYAPPLICATION_H

+ 3 - 0
src/Legacy/main.cpp

@@ -8,6 +8,9 @@
 double window_height = 1000;
 double window_width = 648;
 
+//#define DEBUG
+//#define DEBUG_EXPIRE 1609448400 // 01.01.2021 00:00:00
+
 int main(int argc, char *argv[])
 {
     QApplication app(argc, argv);

+ 1 - 0
src/Legacy/models/patchinstaller.cpp

@@ -382,6 +382,7 @@ void PatchInstaller::insertPatchesInfoInDatFile(const PatchInstaller::AppliedPat
 
 void PatchInstaller::init()
 {
+    emit initializationStarted();
     if (client_local_file_->Initialized() || client_general_file_->Initialized()) {
         // Firstly - deinitializing existing client_local files.
         deinit();

+ 2 - 0
src/Legacy/models/patchinstaller.h

@@ -82,6 +82,8 @@ signals:
     void progressChanged(PatchInstaller::Status status);
     void videosDownloadProgressChanged(int finished_videos, int total_videos, Downloader::Status status);
     void finished();
+
+    void initializationStarted();
     void successfullyInitialized();
     void deinitialized();
     void updatedByGameStatusChanged(bool updated_by_game);

+ 18 - 0
src/Legacy/utils.cpp

@@ -10,6 +10,7 @@
 #include <QLockFile>
 #include <QDir>
 #include <QMessageBox>
+#include <QProcess>
 
 #include <LotroDat/datfile.h>
 
@@ -84,7 +85,24 @@ void logMessageHandler(QtMsgType type, const QMessageLogContext &context, const
     fflush(stderr);
 }
 
+bool isRunning(const QString &process) {
+  QProcess tasklist;
+  tasklist.start(
+        "tasklist",
+        QStringList() << "/NH"
+                      << "/FO" << "CSV"
+                      << "/FI" << QString("IMAGENAME eq %1").arg(process));
+  tasklist.waitForFinished();
+  QString output = tasklist.readAllStandardOutput();
+  return output.startsWith(QString("\"%1").arg(process));
+}
+
+
 AppErrorStatus CheckAppPrerequesities() {
+    if (isRunning("LotroLauncher.exe") || isRunning("lotroclient.exe") || isRunning("lotroclient64.exe")) {
+        return E_LOTRO_RUNNING;
+    }
+
     QString game_folder_path = Settings::getValue("Lotro/game_path").toString();
     QString locale_prefix = Settings::getValue("Lotro/original_locale").toString();
 

+ 1 - 0
src/Legacy/utils.h

@@ -14,6 +14,7 @@ bool checkInternetConnection(QUrl url);
 void logMessageHandler(QtMsgType type, const QMessageLogContext &context, const QString &msg);
 
 enum AppErrorStatus : int {
+    E_LOTRO_RUNNING = 64,
     E_PATCHED_BY_OLD_LEGACY = 32,
     E_WRONG_GAME_FOLDER = 16,
     E_DAT_FILES_MISSING = 8,

+ 8 - 2
src/Legacy/widgets/statuswidget.cpp

@@ -47,6 +47,9 @@ StatusWidget::StatusWidget(QWidget *parent)
 
     connect(&PatchInstaller::instance(), &PatchInstaller::videosDownloadProgressChanged, this, &StatusWidget::onPatchInstallerVideoProgressChanged);
 
+    connect(&PatchInstaller::instance(), &PatchInstaller::initializationStarted, this, [this](){
+        setToolTipMessage("Инициализация файлов игры...", E_INFO);
+    });
     connect(&PatchInstaller::instance(), &PatchInstaller::successfullyInitialized, this, &StatusWidget::showComponentsStatus);
     connect(&PatchInstaller::instance(), &PatchInstaller::successfullyInitialized, this, [this](){updateButtonStatus();});
     connect(&PatchInstaller::instance(), &PatchInstaller::updatedByGameStatusChanged, this, [this](bool updated){_need_to_forcely_patch = updated; updateButtonStatus();});
@@ -66,6 +69,7 @@ StatusWidget::StatusWidget(QWidget *parent)
     _components_status_opacity_animation->setDuration(1000);
 
     ui->b_components_status->setGraphicsEffect(_components_status_opacity_effect);
+    setToolTipMessage("Запускаем Наследие...", E_INFO);
 }
 
 StatusWidget::~StatusWidget()
@@ -153,7 +157,9 @@ void StatusWidget::onErrorStatusChanged(AppErrorStatus status)
         _need_to_repair_game_files = false;
         updateButtonStatus();
         return;
-    } else if (status == E_PATCHED_BY_OLD_LEGACY) {
+    } else if (status == E_LOTRO_RUNNING) {
+        init_error_type_message = "Закройте игру Властелин Колец Онлайн, чтобы продолжить работу русификатора.";
+    } if (status == E_PATCHED_BY_OLD_LEGACY) {
         _need_to_repair_game_files = true;
         updateButtonStatus();
         init_error_type_message = "Некоторые файлы игры несовместимы с этой версией Наследия.\nНажмите на кнопку \"Исправить\", чтобы запустить лаунчер игры, который автоматически скачает нужные файлы.";
@@ -177,7 +183,7 @@ void StatusWidget::onErrorStatusChanged(AppErrorStatus status)
     } else if (status == E_DAT_FILE_INCORRECT) {
         _need_to_repair_game_files = true;
         updateButtonStatus();
-        init_error_type_message = "Некоторые файлы игры несовместимы с этой версией Наследия.\nНажмите на кнопку \"Исправить\", чтобы запустить лаунчер игры, который автоматически скачает нужные файлы.";
+        init_error_type_message = "Не удалось открыть файлы игры.\nНажмите на кнопку \"Исправить\", чтобы запустить лаунчер игры, который заново скачает нужные файлы.";
     } else if (status == E_NO_SERVER_CONNECTION) {
         _need_to_repair_game_files = false;
         updateButtonStatus();