123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111 |
- #include <QFile>
- #include <QDir>
- #include <QDirIterator>
- #include <QCoreApplication>
- #include <QDebug>
- #include <QThread>
- #include <QProcess>
- #include <QApplication>
- #include <QMessageBox>
- void logMessageHandler(QtMsgType type, const QMessageLogContext &context, const QString &msg)
- {
- QString message_type;
- switch (type) {
- case QtDebugMsg:
- message_type = "[LEGACY_FORWARD_HELPER][DEBG]";
- break;
- case QtInfoMsg:
- message_type = "[LEGACY_FORWARD_HELPER][INFO]";
- break;
- case QtWarningMsg:
- message_type = "[LEGACY_FORWARD_HELPER][WARN]";
- break;
- case QtCriticalMsg:
- message_type = "[LEGACY_FORWARD_HELPER][CRIT]";
- break;
- case QtFatalMsg:
- message_type = "[LEGACY_FORWARD_HELPER][FATL]";
- break;
- default:
- message_type = "[LEGACY_FORWARD_HELPER][UNKN]";
- break;
- }
- QString filename = context.category;
- QString function = context.function;
- QString line_number = QString::number(context.line);
- QString s = QString("%1: %2 ||| %3:%4 %5\n")
- .arg(message_type)
- .arg(msg)
- .arg(filename)
- .arg(line_number)
- .arg(function);
- #ifndef QT_DEBUG
- QFile log_file("legacy_log.txt");
- QFileInfo log_file_info(log_file);
- if (log_file_info.size() >= 10 * 1024 * 1024) {
- QFile::remove("legacy_log.old.txt");
- QFile::copy("legacy_log.txt", "legacy_log.old.txt");
- QFile::remove("legacy_log.txt");
- }
- log_file.open(QIODevice::ReadWrite | QIODevice::Append);
- QTextStream stream(&log_file);
- stream << s;
- log_file.close();
- #endif
- fprintf(stderr, "%s", qUtf8Printable(s));
- fflush(stderr);
- }
- int main(int argc, char** argv) {
- if (argc <= 1 || (argc > 1 && std::string(argv[1]) != "-prelaunched")) {
- QApplication a(argc, argv);
- QMessageBox msgBox;
- msgBox.setIcon(QMessageBox::Warning);
- #ifdef DEBUG
- msgBox.setText("ЭТО ТЕСТОВАЯ ВЕРСИЯ ЛАУНЧЕРА! НЕ ДЛЯ РАСПРОСТРАНЕНИЯ!");
- msgBox.exec();
- long long cur_time = time(0);
- if (cur_time >= DEBUG_EXPIRE)
- return 0;
- #else
- msgBox.setText("Запуск Наследия невозможен без лаунчера. Используйте лаунчер Наследия для запуска приложения!");
- msgBox.exec();
- a.exit(0);
- return 0;
- #endif
- }
- QCoreApplication a(argc, argv);
- qInstallMessageHandler(logMessageHandler);
- QThread::currentThread()->sleep(1); // Sleep 2 seconds to ensure that application-caller finished its work
- const QString app_path = QCoreApplication::applicationDirPath();
- const QDir::Filters filters = (QDir::Files | QDir::Dirs | QDir::NoDotAndDotDot);
- QDirIterator it(app_path, filters, QDirIterator::Subdirectories);
- while (it.hasNext()) {
- QString path = it.next();
- if (QFileInfo(path).isFile() && path.endsWith(".forward")) {
- QString path_stripped = path.left(path.length() - QString(".forward").length());
- QFile::remove(path_stripped);
- if (QFile::copy(path, path_stripped)) {
- QFile::remove(path);
- }
- qInfo() << "Renaming file " << path << " to file " << path_stripped;
- }
- }
- QProcess process;
- process.setProgram("./Legacy.exe");
- process.setArguments({"-prelaunched"});
- process.startDetached();
- process.waitForFinished(-1);
- return 0;
- }
|