utils.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*
  2. * This file contains minor utility functions
  3. */
  4. #include <QNetworkAccessManager>
  5. #include <QNetworkReply>
  6. #include <QEventLoop>
  7. #include <QFile>
  8. #include <QTimer>
  9. #include <QLockFile>
  10. #include <QDir>
  11. #include <QMessageBox>
  12. #include <LotroDat/datfile.h>
  13. #include "models/settings.h"
  14. #include "utils.h"
  15. bool checkInternetConnection(QUrl url) {
  16. QNetworkAccessManager nam;
  17. QNetworkRequest req(url);
  18. QNetworkReply* reply = nam.get(req);
  19. QEventLoop loop;
  20. QObject::connect(reply, &QNetworkReply::finished, &loop, &QEventLoop::quit);
  21. QTimer request_timeout_timer;
  22. QObject::connect(&request_timeout_timer, &QTimer::timeout, &loop, &QEventLoop::quit);
  23. request_timeout_timer.start(5000); // 5 seconds connection timeout
  24. loop.exec();
  25. return reply->bytesAvailable();
  26. }
  27. void logMessageHandler(QtMsgType type, const QMessageLogContext &context, const QString &msg)
  28. {
  29. QString message_type;
  30. switch (type) {
  31. case QtDebugMsg:
  32. message_type = "[DEBG]";
  33. break;
  34. case QtInfoMsg:
  35. message_type = "[INFO]";
  36. break;
  37. case QtWarningMsg:
  38. message_type = "[WARN]";
  39. break;
  40. case QtCriticalMsg:
  41. message_type = "[CRIT]";
  42. break;
  43. case QtFatalMsg:
  44. message_type = "[FATL]";
  45. break;
  46. default:
  47. message_type = "[UNKN]";
  48. break;
  49. }
  50. QString filename = context.category;
  51. QString function = context.function;
  52. QString line_number = QString::number(context.line);
  53. QString s = QString("%1: %2 ||| %3:%4 %5\n")
  54. .arg(message_type)
  55. .arg(msg)
  56. .arg(filename)
  57. .arg(line_number)
  58. .arg(function);
  59. #ifdef QT_DEBUG
  60. QFile log_file("legacy_v2.log");
  61. log_file.open(QIODevice::ReadWrite | QIODevice::Append);
  62. QTextStream stream(&log_file);
  63. stream << s;
  64. log_file.close();
  65. #endif
  66. fprintf(stderr, "%s", qUtf8Printable(s));
  67. fflush(stderr);
  68. }
  69. bool checkSingleAppInstance() {
  70. QLockFile lockFile(QDir::temp().absoluteFilePath("rulotro.lock"));
  71. if(!lockFile.tryLock(1)){
  72. qDebug() << "Lock file already exists! Some other application is already running...";
  73. QMessageBox msgBox;
  74. msgBox.setIcon(QMessageBox::Warning);
  75. msgBox.setText("Приложение уже запущено.\nРазрешено запускать только один экземпляр приложения.");
  76. msgBox.exec();
  77. return false;
  78. }
  79. return true;
  80. }
  81. AppErrorStatus CheckAppPrerequesities() {
  82. size_t status = E_NO_ERRORS;
  83. QString game_folder_path = Settings::getValue("Lotro/game_path").toString();
  84. QString locale_prefix = Settings::getValue("Lotro/original_locale").toString();
  85. // Checking LotroLauncher.exe availability
  86. QFileInfo lotro_launcher_exe(game_folder_path + "/LotroLauncher.exe");
  87. if (!lotro_launcher_exe.exists()) {
  88. status |= E_WRONG_GAME_FOLDER;
  89. } else {
  90. if (!QFile::setPermissions(lotro_launcher_exe.absoluteFilePath(),
  91. QFileDevice::ReadUser | QFileDevice::WriteUser | QFileDevice::ExeUser)) {
  92. status |= E_WRONG_FILE_PERMISSIONS;
  93. }
  94. }
  95. // Checking DAT files availability & correctness
  96. QString client_local_filepath = game_folder_path + "/client_local_" + locale_prefix + ".dat";
  97. QString client_general_filepath = game_folder_path + "/client_general.dat";
  98. QFileInfo client_local_file(client_local_filepath);
  99. QFileInfo client_general_file(client_general_filepath);
  100. if (!client_general_file.exists() || !client_local_file.exists()) {
  101. status |= E_DAT_FILES_MISSING;
  102. } else {
  103. if (!QFile::setPermissions(client_general_filepath,
  104. QFileDevice::ReadUser |
  105. QFileDevice::WriteUser |
  106. QFileDevice::ExeUser)) {
  107. status |= E_WRONG_FILE_PERMISSIONS;
  108. status |= E_CANNOT_ACCESS_DAT_FILES;
  109. }
  110. if (!QFile::setPermissions(client_local_filepath,
  111. QFileDevice::ReadUser |
  112. QFileDevice::WriteUser |
  113. QFileDevice::ExeUser)) {
  114. status |= E_WRONG_FILE_PERMISSIONS;
  115. status |= E_CANNOT_ACCESS_DAT_FILES;
  116. }
  117. LOTRO_DAT::DatFile file(0);
  118. if (!file.Init(client_local_file.absoluteFilePath().toStdString())) {
  119. status |= E_DAT_FILE_INCORRECT;
  120. } else {
  121. file.Deinit();
  122. }
  123. if (!file.Init(client_general_file.absoluteFilePath().toStdString())) {
  124. status |= E_DAT_FILE_INCORRECT;
  125. } else {
  126. file.Deinit();
  127. }
  128. }
  129. if (!checkInternetConnection(QUrl(Settings::getValue("Network/patch_updates_url").toString()))) {
  130. status |= E_NO_SERVER_CONNECTION;
  131. }
  132. qDebug() << "LegacyApplication: StatusCheck: " << AppErrorStatus(status);
  133. return AppErrorStatus(status);
  134. }
  135. QDebug operator<<(QDebug dbg, const AppErrorStatus &status)
  136. {
  137. bool wrong_game_folder = (status & E_WRONG_GAME_FOLDER);
  138. bool dat_files_missing = (status & E_DAT_FILES_MISSING);
  139. bool wrong_file_permissions = (status & E_WRONG_FILE_PERMISSIONS);
  140. bool cannot_access_dat_files = (status & E_CANNOT_ACCESS_DAT_FILES);
  141. bool dat_file_incorrect = (status & E_DAT_FILE_INCORRECT);
  142. bool no_server_connection = (status & E_NO_SERVER_CONNECTION);
  143. dbg.nospace() << "ErrorStatus ( "
  144. << (wrong_game_folder ? "Wrong game folder! " : "")
  145. << (dat_files_missing ? "Dat files missing! " : "")
  146. << (wrong_file_permissions ? "Wrong file permissions! " : "")
  147. << (cannot_access_dat_files ? "Cannot access dat files! " : "")
  148. << (dat_file_incorrect ? "Incorrect dat file! " : "")
  149. << (no_server_connection ? "No server connection! " : "")
  150. << ")";
  151. return dbg;
  152. }