launcher.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. #include "launcher.h"
  2. #include "ui_launcher.h"
  3. GIFAnimationDemoWidget::GIFAnimationDemoWidget( QWidget* parent ) :
  4. QWidget( parent, Qt::Window | Qt::FramelessWindowHint ), ui( new Ui::GIFAnimationDemoWidget ) {
  5. ui->setupUi(this);
  6. setWindowTitle("ВКО: Наследие - система обновлений");
  7. ui->lbHint->setText("Получение списка файлов...");
  8. movie = new QMovie(":/second.gif");
  9. ui->lbMovie->setMovie(movie);
  10. movie->setScaledSize(QSize(ui->lbMovie->width(), ui->lbMovie->height()));
  11. movie->start();
  12. QString response = getFilelist("http://translate.lotros.ru/upload/launcher/filelist.txt");
  13. QFuture<void> future = QtConcurrent::run([=]() {
  14. start(response);
  15. });
  16. }
  17. GIFAnimationDemoWidget::~GIFAnimationDemoWidget() {
  18. delete ui;
  19. }
  20. void GIFAnimationDemoWidget::start(QString filelist) {
  21. ui->lbHint->setText("Проверка и скачивание файлов...");
  22. QStringList templist = filelist.split("\r\n");
  23. foreach(const QString &str, templist){
  24. QString path = str;
  25. QStringList list = path.replace("release/", "").split("|");
  26. path = list[0];
  27. if (path.size() == 0)
  28. continue;
  29. qDebug() << path;
  30. if (fileExists(path)) {
  31. QString hash = fileHash(list[0], QCryptographicHash::Md5); // Hashes match - file is ok. Passing it;
  32. qDebug() << path << " " << hash << " " << list[1];
  33. if (hash == list[1])
  34. continue;
  35. }
  36. QFileInfo fileInfo(path);
  37. QString folder = fileInfo.dir().path();
  38. if (folder == ".") folder = "";
  39. makeFolder(folder);
  40. ui->lbHint->setText("Загрузка " + path);
  41. downloadFile ("http://translate.lotros.ru/upload/launcher/release/" + path, QApplication::applicationDirPath() + "/" + folder);
  42. }
  43. ui->lbHint->setText("Подготовка к запуску ...");
  44. QProcess process;
  45. process.startDetached("Legacy.exe -prelaunched");
  46. process.waitForFinished(-1);
  47. exit(0);
  48. }
  49. QString GIFAnimationDemoWidget::fileHash(const QString &fileName, QCryptographicHash::Algorithm hashAlgorithm) {
  50. QFile file(fileName);
  51. if (file.open(QIODevice::ReadOnly)) {
  52. QByteArray fileData = file.readAll();
  53. QByteArray hashData = QCryptographicHash::hash(fileData, hashAlgorithm);
  54. return hashData.toHex();
  55. }
  56. return QByteArray();
  57. }
  58. bool GIFAnimationDemoWidget::fileExists(QString path) {
  59. QFileInfo check_file(path);
  60. return check_file.exists() && check_file.isFile();
  61. }
  62. void GIFAnimationDemoWidget::makeFolder(QString path) {
  63. QString folder = QApplication::applicationDirPath();
  64. QStringList p = path.split("/");
  65. foreach(const QString &str, p){
  66. folder = folder + "/" + str;
  67. if (!(QDir(folder).exists() == true) ){
  68. QDir().mkdir(folder);
  69. qDebug() << "Создана папка " + folder;
  70. }
  71. }
  72. }
  73. void GIFAnimationDemoWidget::startNewAnimation() {
  74. delete movie;
  75. movie = new QMovie(":/second.gif");
  76. ui->lbMovie->setMovie(movie);
  77. movie->setScaledSize(QSize(ui->lbMovie->width(), ui->lbMovie->height()));
  78. movie->start();
  79. }
  80. QString GIFAnimationDemoWidget::getFilelist(const QString &url) {
  81. QNetworkAccessManager manager;
  82. QNetworkReply* reply = manager.get(QNetworkRequest(url));
  83. QEventLoop loop;
  84. QObject::connect(reply, SIGNAL(finished()), &loop, SLOT(quit()));
  85. loop.exec();
  86. QString content;
  87. if (reply->error() != QNetworkReply::NoError){
  88. content = "error";
  89. // пишем в лог
  90. qDebug() << reply->errorString();
  91. }
  92. reply->deleteLater();
  93. content = reply->readAll();
  94. return content;
  95. }
  96. void GIFAnimationDemoWidget::downloadFile(const QString &url, const QString &aPathInClient) {
  97. QNetworkAccessManager m_NetworkMngr;
  98. QNetworkReply *reply= m_NetworkMngr.get(QNetworkRequest(url));
  99. QEventLoop loop;
  100. QObject::connect(reply, SIGNAL(finished()), &loop, SLOT(quit()));
  101. loop.exec();
  102. QUrl aUrl(url);
  103. QFileInfo fileInfo=aUrl.path();
  104. QFile file(aPathInClient+ "/" + fileInfo.fileName());
  105. file.remove();
  106. file.open(QIODevice::WriteOnly);
  107. file.write(reply->readAll());
  108. }