soundspatch.cpp 8.4 KB

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