textspatch.cpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. #include "textspatch.h"
  2. #include "LotroDat/LotroDat.h"
  3. #include "LotroDat/Database.h"
  4. #include <QUrlQuery>
  5. #include <QSet>
  6. #include <QThread>
  7. TextsPatch::TextsPatch(LotroDatManager *mgr, QObject *parent) : Patch("TextsPatch", mgr, parent)
  8. {
  9. connect(lotro_mgr_, &LotroDatManager::operationStarted, this, &TextsPatch::onLotroDatManagerOperationStarted);
  10. connect(lotro_mgr_, &LotroDatManager::operationFinished, this, &TextsPatch::onLotroDatManagerOperationFinished);
  11. connect(lotro_mgr_, &LotroDatManager::statusChanged, this, &TextsPatch::onLotroDatManagerStatusChanged);
  12. }
  13. void TextsPatch::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 TextsPatch::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, &TextsPatch::onDownloaderProgressChanged);
  78. connect(downloader, &Downloader::downloadFinished, this, &TextsPatch::onDownloaderFinished);
  79. downloaders_.insert(downloader);
  80. downloader->start();
  81. }
  82. if (downloaders_.empty()) {
  83. emit operationFinished(E_DOWNLOAD, this);
  84. }
  85. }
  86. void TextsPatch::install()
  87. {
  88. foreach (QString db_name, databases_names) {
  89. ++elapsed_patches_to_install_;
  90. QMetaObject::invokeMethod(lotro_mgr_, "installPatch", Qt::QueuedConnection,
  91. Q_ARG(QString, getPatchName() + "_" + db_name),
  92. Q_ARG(QString, Settings::getValue("PatchDatabases/" + db_name + "/path").toString())
  93. );
  94. }
  95. }
  96. void TextsPatch::activate()
  97. {
  98. emit operationFinished(E_ACTIVATE, this);
  99. return;
  100. QString operation_name = Settings::getValue("Components/fonts").toBool() ? "enableCategory" : "disableCategory";
  101. ++elapsed_patches_to_install_;
  102. QMetaObject::invokeMethod(lotro_mgr_, operation_name.toLocal8Bit().constData(), Qt::QueuedConnection,
  103. Q_ARG(QString, getPatchName() + "_fonts"),
  104. Q_ARG(LotroDatManager::Category, LotroDatManager::Category::E_FONTS_COMMON)
  105. );
  106. operation_name = Settings::getValue("Components/texts_main").toBool() ? "enableCategory" : "disableCategory";
  107. ++elapsed_patches_to_install_;
  108. QMetaObject::invokeMethod(lotro_mgr_, operation_name.toLocal8Bit().constData(), Qt::QueuedConnection,
  109. Q_ARG(QString, getPatchName() + "_texts_main"),
  110. Q_ARG(LotroDatManager::Category, LotroDatManager::Category::E_TEXTS_COMMON)
  111. );
  112. operation_name = Settings::getValue("Components/texts_items").toBool() ? "enableCategory" : "disableCategory";
  113. ++elapsed_patches_to_install_;
  114. QMetaObject::invokeMethod(lotro_mgr_, operation_name.toLocal8Bit().constData(), Qt::QueuedConnection,
  115. Q_ARG(QString, getPatchName() + "_texts_items"),
  116. Q_ARG(LotroDatManager::Category, LotroDatManager::Category::E_TEXTS_ITEMS)
  117. );
  118. operation_name = Settings::getValue("Components/texts_emotes").toBool() ? "enableCategory" : "disableCategory";
  119. ++elapsed_patches_to_install_;
  120. QMetaObject::invokeMethod(lotro_mgr_, operation_name.toLocal8Bit().constData(), Qt::QueuedConnection,
  121. Q_ARG(QString, getPatchName() + "_texts_emotes"),
  122. Q_ARG(LotroDatManager::Category, LotroDatManager::Category::E_TEXTS_EMOTES)
  123. );
  124. operation_name = Settings::getValue("Components/videos").toBool() ? "enableCategory" : "disableCategory";
  125. ++elapsed_patches_to_install_;
  126. QMetaObject::invokeMethod(lotro_mgr_, operation_name.toLocal8Bit().constData(), Qt::QueuedConnection,
  127. Q_ARG(QString, getPatchName() + "_texts_videos_refs"),
  128. Q_ARG(LotroDatManager::Category, LotroDatManager::Category::E_TEXTS_VIDEOS_REFS)
  129. );
  130. }
  131. void TextsPatch::onDownloaderProgressChanged(Downloader *, Downloader::Status)
  132. {
  133. Downloader::Status all_downloads_status;
  134. foreach (Downloader* downloader, downloaders_) {
  135. all_downloads_status = all_downloads_status + downloader->getDownloadStatus();
  136. }
  137. emit progressChanged(OperationProgress(all_downloads_status), this);
  138. }
  139. void TextsPatch::onDownloaderFinished(Downloader *ptr)
  140. {
  141. ptr->targetFile->close();
  142. ptr->targetFile->deleteLater();
  143. Downloader::Status all_downloads_status;
  144. for (const Downloader* downloader : downloaders_) {
  145. all_downloads_status = all_downloads_status + downloader->getDownloadStatus();
  146. }
  147. if (!all_downloads_status.running) {
  148. for (Downloader* downloader : downloaders_) {
  149. downloader->deleteLater();
  150. }
  151. downloaders_.clear();
  152. emit operationFinished(E_DOWNLOAD, this);
  153. }
  154. }
  155. void TextsPatch::onLotroDatManagerOperationFinished(QString operation_name, QVector<QVariant> args, bool successful)
  156. {
  157. if (args.size() == 0 || !args[0].toString().contains(getPatchName())) {
  158. return; // This means, that message from LotroManager is addressed to another patchset
  159. }
  160. if (operation_name.contains("installPatch")) {
  161. if (!successful) {
  162. qCritical() << *this << "Database " + args[0].toString() + " (" + args[1].toString() + ") was not installed due to Legacy core error.";
  163. emit errorOccured(E_INSTALL, this, "Database " + args[0].toString() + " (" + args[1].toString() + ") was not installed due to Legacy core error.");
  164. }
  165. --elapsed_patches_to_install_;
  166. if (!elapsed_patches_to_install_) {
  167. is_being_patched_by_lotro_dat_manager_ = false;
  168. emit operationFinished(E_INSTALL, this);
  169. }
  170. }
  171. if (operation_name.contains("enableCategory") || operation_name.contains("disableCategory")) {
  172. if (!successful) {
  173. qCritical() << *this << "Error in patch " + args[0].toString() + ": activating category " + QString::number(args[1].toInt()) + " failed.";
  174. emit errorOccured(E_ACTIVATE, this, "Error in patch " + args[0].toString() + ": activating category " + QString::number(args[1].toInt()) + " failed.");
  175. }
  176. --elapsed_patches_to_install_;
  177. if (!elapsed_patches_to_install_) {
  178. is_being_patched_by_lotro_dat_manager_ = false;
  179. emit operationFinished(E_ACTIVATE, this);
  180. }
  181. }
  182. }
  183. void TextsPatch::onLotroDatManagerOperationStarted(QString operation_name, QVector<QVariant> args)
  184. {
  185. if (args.size() == 0 || !args[0].toString().contains(getPatchName())) {
  186. return;
  187. }
  188. is_being_patched_by_lotro_dat_manager_ = true;
  189. }
  190. void TextsPatch::onLotroDatManagerStatusChanged(LotroDatManager::Status status)
  191. {
  192. if (!is_being_patched_by_lotro_dat_manager_) {
  193. return;
  194. }
  195. emit progressChanged(OperationProgress(status), this);
  196. }