soundspatch.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. #include "soundspatch.h"
  2. #include "LotroDat/LotroDat.h"
  3. #include "LotroDat/Database.h"
  4. #include <QUrlQuery>
  5. #include <QSet>
  6. #include <QThread>
  7. SoundsPatch::SoundsPatch(LotroDatManager *mgr, QObject *parent) : Patch("SoundsPatch", mgr, parent)
  8. {
  9. connect(lotro_mgr_, &LotroDatManager::operationStarted, this, &SoundsPatch::onLotroDatManagerOperationStarted);
  10. connect(lotro_mgr_, &LotroDatManager::operationFinished, this, &SoundsPatch::onLotroDatManagerOperationFinished);
  11. connect(lotro_mgr_, &LotroDatManager::statusChanged, this, &SoundsPatch::onLotroDatManagerStatusChanged);
  12. }
  13. void SoundsPatch::checkForUpdates()
  14. {
  15. QUrlQuery query; // query for building GET-request aka patch-version
  16. foreach (QString db_name, databases_names) {
  17. query.addQueryItem(db_name, "100");
  18. }
  19. QUrl target_url;
  20. target_url.setUrl(Settings::getValue("Network/patch_updates_url").toString());
  21. target_url.setQuery(query);
  22. QByteArray target_array;
  23. Downloader downloader;
  24. downloader.setUrl(target_url);
  25. downloader.targetBytearray = &target_array;
  26. downloader.start();
  27. downloader.waitForDownloaded();
  28. if (target_array.isEmpty()) {
  29. qWarning() << *this << "Cannot check for updates, target_array is empty!";
  30. emit errorOccured(E_CHECKFORUPDATES, this, "");
  31. emit operationFinished(E_CHECKFORUPDATES, this);
  32. return;
  33. }
  34. QStringList patch_info = QString(target_array).split('|');
  35. if (patch_info.size() != databases_names.size()) {
  36. qCritical() << __FUNCTION__ << "Incorrect patches number! Data: " << patch_info;
  37. emit errorOccured(E_CHECKFORUPDATES, this, "");
  38. emit operationFinished(E_CHECKFORUPDATES, this);
  39. return;
  40. }
  41. for (int i = 0; i < databases_names.size(); ++i) {
  42. QStringList patch_data = patch_info[i].split(":::");
  43. if (patch_data.size() != 3) {
  44. qCritical() << __FUNCTION__ << "Incorrect patch entry size! Entry: " << patch_data;
  45. emit errorOccured(E_CHECKFORUPDATES, this, "");
  46. emit operationFinished(E_CHECKFORUPDATES, this);
  47. return;
  48. }
  49. QString patch_filename = Settings::getValue("General/PatchDownloadDir").toString() + "/" + QUrl(patch_data[0]).fileName();
  50. Settings::setValue("PatchDatabases/" + databases_names[i] + "/url", patch_data[0]);
  51. Settings::setValue("PatchDatabases/" + databases_names[i] + "/hashsum", patch_data[1]);
  52. Settings::setValue("PatchDatabases/" + databases_names[i] + "/datetime", patch_data[2]);
  53. Settings::setValue("PatchDatabases/" + databases_names[i] + "/path", patch_filename);
  54. }
  55. emit operationFinished(E_CHECKFORUPDATES, this);
  56. }
  57. void SoundsPatch::download()
  58. {
  59. foreach (QString db_name, databases_names) {
  60. QString settings_prefix = "PatchDatabases/" + db_name;
  61. QString target_filename = QApplication::applicationDirPath() + "/" + Settings::getValue(settings_prefix + "/path").toString();
  62. qDebug() << patch_name_ << ": Checking if file " << target_filename << " matches its hashsum";
  63. if (FileSystem::fileHash(target_filename) == Settings::getValue(settings_prefix + "/hashsum").toString()) {
  64. qInfo() << *this << ": file " << target_filename << " is up-to-date, no need to download";
  65. continue;
  66. }
  67. FileSystem::createFilePath(target_filename);
  68. QFile* target_file = new QFile(target_filename);
  69. if (!target_file->open(QIODevice::ReadWrite | QIODevice::Truncate)) {
  70. qWarning() << *this << "Cannot open file " << target_filename;
  71. continue;
  72. }
  73. qInfo() << *this << ": beginning download of file " << target_filename;
  74. Downloader* downloader = new Downloader();
  75. downloader->setUrl(Settings::getValue(settings_prefix + "/url").toUrl());
  76. downloader->targetFile = target_file;
  77. connect(downloader, &Downloader::progressChanged, this, &SoundsPatch::onDownloaderProgressChanged);
  78. connect(downloader, &Downloader::downloadFinished, this, &SoundsPatch::onDownloaderFinished);
  79. downloaders_.insert(downloader);
  80. downloader->start();
  81. }
  82. if (downloaders_.empty()) {
  83. emit operationFinished(E_DOWNLOAD, this);
  84. }
  85. }
  86. void SoundsPatch::install()
  87. {
  88. emit operationFinished(E_INSTALL, this);
  89. return;
  90. foreach (QString db_name, QStringList({"sound"})) {
  91. ++elapsed_patches_to_install_;
  92. QMetaObject::invokeMethod(lotro_mgr_, "installPatch", Qt::QueuedConnection,
  93. Q_ARG(QString, getPatchName() + "_" + db_name),
  94. Q_ARG(QString, Settings::getValue("PatchDatabases/" + db_name + "/path").toString())
  95. );
  96. }
  97. }
  98. void SoundsPatch::activate()
  99. {
  100. emit operationFinished(E_ACTIVATE, this);
  101. return;
  102. QString operation_name = Settings::getValue("Components/sounds").toBool() ? "enableCategory" : "disableCategory";
  103. ++elapsed_patches_to_install_;
  104. QMetaObject::invokeMethod(lotro_mgr_, operation_name.toLocal8Bit().constData(), Qt::QueuedConnection,
  105. Q_ARG(QString, getPatchName() + "_sounds"),
  106. Q_ARG(LotroDatManager::Category, LotroDatManager::Category::E_AUDIOS_COMMON)
  107. );
  108. }
  109. void SoundsPatch::onDownloaderProgressChanged(Downloader *, Downloader::Status)
  110. {
  111. Downloader::Status all_downloads_status;
  112. foreach (Downloader* downloader, downloaders_) {
  113. all_downloads_status = all_downloads_status + downloader->getDownloadStatus();
  114. }
  115. emit progressChanged(OperationProgress(all_downloads_status), this);
  116. }
  117. void SoundsPatch::onDownloaderFinished(Downloader *ptr)
  118. {
  119. ptr->targetFile->close();
  120. ptr->targetFile->deleteLater();
  121. Downloader::Status all_downloads_status;
  122. for (const Downloader* downloader : downloaders_) {
  123. all_downloads_status = all_downloads_status + downloader->getDownloadStatus();
  124. }
  125. if (!all_downloads_status.running) {
  126. for (Downloader* downloader : downloaders_) {
  127. downloader->deleteLater();
  128. }
  129. downloaders_.clear();
  130. emit operationFinished(E_DOWNLOAD, this);
  131. }
  132. }
  133. void SoundsPatch::onLotroDatManagerOperationFinished(QString operation_name, QVector<QVariant> args, bool successful)
  134. {
  135. if (args.size() == 0 || !args[0].toString().contains(getPatchName())) {
  136. return; // This means, that message from LotroManager is addressed to another patchset
  137. }
  138. if (operation_name.contains("installPatch")) {
  139. if (!successful) {
  140. qCritical() << *this << "Database " + args[0].toString() + " (" + args[1].toString() + ") was not installed due to Legacy core error.";
  141. emit errorOccured(E_INSTALL, this, "Database " + args[0].toString() + " (" + args[1].toString() + ") was not installed due to Legacy core error.");
  142. }
  143. --elapsed_patches_to_install_;
  144. if (!elapsed_patches_to_install_) {
  145. is_being_patched_by_lotro_dat_manager_ = false;
  146. emit operationFinished(E_INSTALL, this);
  147. }
  148. }
  149. if (operation_name.contains("enableCategory") || operation_name.contains("disableCategory")) {
  150. if (!successful) {
  151. qCritical() << *this << "Error in patch " + args[0].toString() + ": activating category " + QString::number(args[1].toInt()) + " failed.";
  152. emit errorOccured(E_ACTIVATE, this, "Error in patch " + args[0].toString() + ": activating category " + QString::number(args[1].toInt()) + " failed.");
  153. }
  154. --elapsed_patches_to_install_;
  155. if (!elapsed_patches_to_install_) {
  156. is_being_patched_by_lotro_dat_manager_ = false;
  157. emit operationFinished(E_ACTIVATE, this);
  158. }
  159. }
  160. }
  161. void SoundsPatch::onLotroDatManagerOperationStarted(QString operation_name, QVector<QVariant> args)
  162. {
  163. if (args.size() == 0 || !args[0].toString().contains(getPatchName())) {
  164. return;
  165. }
  166. is_being_patched_by_lotro_dat_manager_ = true;
  167. }
  168. void SoundsPatch::onLotroDatManagerStatusChanged(LotroDatManager::Status status)
  169. {
  170. if (!is_being_patched_by_lotro_dat_manager_) {
  171. return;
  172. }
  173. emit progressChanged(OperationProgress(status), this);
  174. }