utils.cpp 5.6 KB

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