main.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. #include <QFile>
  2. #include <QDir>
  3. #include <QDirIterator>
  4. #include <QCoreApplication>
  5. #include <QDebug>
  6. #include <QThread>
  7. #include <QProcess>
  8. #include <QApplication>
  9. #include <QMessageBox>
  10. void logMessageHandler(QtMsgType type, const QMessageLogContext &context, const QString &msg)
  11. {
  12. QString message_type;
  13. switch (type) {
  14. case QtDebugMsg:
  15. message_type = "[LEGACY_FORWARD_HELPER][DEBG]";
  16. break;
  17. case QtInfoMsg:
  18. message_type = "[LEGACY_FORWARD_HELPER][INFO]";
  19. break;
  20. case QtWarningMsg:
  21. message_type = "[LEGACY_FORWARD_HELPER][WARN]";
  22. break;
  23. case QtCriticalMsg:
  24. message_type = "[LEGACY_FORWARD_HELPER][CRIT]";
  25. break;
  26. case QtFatalMsg:
  27. message_type = "[LEGACY_FORWARD_HELPER][FATL]";
  28. break;
  29. default:
  30. message_type = "[LEGACY_FORWARD_HELPER][UNKN]";
  31. break;
  32. }
  33. QString filename = context.category;
  34. QString function = context.function;
  35. QString line_number = QString::number(context.line);
  36. QString s = QString("%1: %2 ||| %3:%4 %5\n")
  37. .arg(message_type)
  38. .arg(msg)
  39. .arg(filename)
  40. .arg(line_number)
  41. .arg(function);
  42. #ifndef QT_DEBUG
  43. QFile log_file("legacy_log.txt");
  44. QFileInfo log_file_info(log_file);
  45. if (log_file_info.size() >= 10 * 1024 * 1024) {
  46. QFile::remove("legacy_log.old.txt");
  47. QFile::copy("legacy_log.txt", "legacy_log.old.txt");
  48. QFile::remove("legacy_log.txt");
  49. }
  50. log_file.open(QIODevice::ReadWrite | QIODevice::Append);
  51. QTextStream stream(&log_file);
  52. stream << s;
  53. log_file.close();
  54. #endif
  55. fprintf(stderr, "%s", qUtf8Printable(s));
  56. fflush(stderr);
  57. }
  58. int main(int argc, char** argv) {
  59. if (argc <= 1 || (argc > 1 && std::string(argv[1]) != "-prelaunched")) {
  60. QApplication a(argc, argv);
  61. QMessageBox msgBox;
  62. msgBox.setIcon(QMessageBox::Warning);
  63. #ifdef DEBUG
  64. msgBox.setText("ЭТО ТЕСТОВАЯ ВЕРСИЯ ЛАУНЧЕРА! НЕ ДЛЯ РАСПРОСТРАНЕНИЯ!");
  65. msgBox.exec();
  66. long long cur_time = time(0);
  67. if (cur_time >= DEBUG_EXPIRE)
  68. return 0;
  69. #else
  70. msgBox.setText("Запуск Наследия невозможен без лаунчера. Используйте лаунчер Наследия для запуска приложения!");
  71. msgBox.exec();
  72. a.exit(0);
  73. return 0;
  74. #endif
  75. }
  76. QCoreApplication a(argc, argv);
  77. qInstallMessageHandler(logMessageHandler);
  78. QThread::currentThread()->sleep(1); // Sleep 2 seconds to ensure that application-caller finished its work
  79. const QString app_path = QCoreApplication::applicationDirPath();
  80. const QDir::Filters filters = (QDir::Files | QDir::Dirs | QDir::NoDotAndDotDot);
  81. QDirIterator it(app_path, filters, QDirIterator::Subdirectories);
  82. while (it.hasNext()) {
  83. QString path = it.next();
  84. if (QFileInfo(path).isFile() && path.endsWith(".forward")) {
  85. QString path_stripped = path.left(path.length() - QString(".forward").length());
  86. QFile::remove(path_stripped);
  87. if (QFile::copy(path, path_stripped)) {
  88. QFile::remove(path);
  89. }
  90. qInfo() << "Renaming file " << path << " to file " << path_stripped;
  91. }
  92. }
  93. QProcess process;
  94. process.setProgram("./Legacy.exe");
  95. process.setArguments({"-prelaunched"});
  96. process.startDetached();
  97. process.waitForFinished(-1);
  98. return 0;
  99. }