123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236 |
- #include "textspatch.h"
- #include "LotroDat/LotroDat.h"
- #include "LotroDat/Database.h"
- #include <QUrlQuery>
- #include <QSet>
- #include <QThread>
- TextsPatch::TextsPatch(LotroDatManager *mgr, QObject *parent) : Patch("TextsPatch", mgr, parent)
- {
- connect(lotro_mgr_, &LotroDatManager::operationStarted, this, &TextsPatch::onLotroDatManagerOperationStarted);
- connect(lotro_mgr_, &LotroDatManager::operationFinished, this, &TextsPatch::onLotroDatManagerOperationFinished);
- connect(lotro_mgr_, &LotroDatManager::statusChanged, this, &TextsPatch::onLotroDatManagerStatusChanged);
- }
- void TextsPatch::checkForUpdates()
- {
- QUrlQuery query; // query for building GET-request aka patch-version
- foreach (QString db_name, databases_names) {
- query.addQueryItem(db_name, "100");
- }
- QUrl target_url;
- target_url.setUrl(Settings::getValue("Network/patch_updates_url").toString());
- target_url.setQuery(query);
- QByteArray target_array;
- Downloader downloader;
- downloader.setUrl(target_url);
- downloader.targetBytearray = &target_array;
- downloader.start();
- downloader.waitForDownloaded();
- if (target_array.isEmpty()) {
- qWarning() << *this << "Cannot check for updates, target_array is empty!";
- emit errorOccured(E_CHECKFORUPDATES, this, "");
- emit operationFinished(E_CHECKFORUPDATES, this);
- return;
- }
- QStringList patch_info = QString(target_array).split('|');
- if (patch_info.size() != databases_names.size()) {
- qCritical() << __FUNCTION__ << "Incorrect patches number! Data: " << patch_info;
- emit errorOccured(E_CHECKFORUPDATES, this, "");
- emit operationFinished(E_CHECKFORUPDATES, this);
- return;
- }
- for (int i = 0; i < databases_names.size(); ++i) {
- QStringList patch_data = patch_info[i].split(":::");
- if (patch_data.size() != 3) {
- qCritical() << __FUNCTION__ << "Incorrect patch entry size! Entry: " << patch_data;
- emit errorOccured(E_CHECKFORUPDATES, this, "");
- emit operationFinished(E_CHECKFORUPDATES, this);
- return;
- }
- QString patch_filename = Settings::getValue("General/PatchDownloadDir").toString() + "/" + QUrl(patch_data[0]).fileName();
- Settings::setValue("PatchDatabases/" + databases_names[i] + "/url", patch_data[0]);
- Settings::setValue("PatchDatabases/" + databases_names[i] + "/hashsum", patch_data[1]);
- Settings::setValue("PatchDatabases/" + databases_names[i] + "/datetime", patch_data[2]);
- Settings::setValue("PatchDatabases/" + databases_names[i] + "/path", patch_filename);
- }
- emit operationFinished(E_CHECKFORUPDATES, this);
- }
- void TextsPatch::download()
- {
- foreach (QString db_name, databases_names) {
- QString settings_prefix = "PatchDatabases/" + db_name;
- QString target_filename = QApplication::applicationDirPath() + "/" + Settings::getValue(settings_prefix + "/path").toString();
- qDebug() << patch_name_ << ": Checking if file " << target_filename << " matches its hashsum";
- if (FileSystem::fileHash(target_filename) == Settings::getValue(settings_prefix + "/hashsum").toString()) {
- qInfo() << *this << ": file " << target_filename << " is up-to-date, no need to download";
- continue;
- }
- FileSystem::createFilePath(target_filename);
- QFile* target_file = new QFile(target_filename);
- if (!target_file->open(QIODevice::ReadWrite | QIODevice::Truncate)) {
- qWarning() << *this << "Cannot open file " << target_filename;
- continue;
- }
- qInfo() << *this << ": beginning download of file " << target_filename;
- Downloader* downloader = new Downloader();
- downloader->setUrl(Settings::getValue(settings_prefix + "/url").toUrl());
- downloader->targetFile = target_file;
- connect(downloader, &Downloader::progressChanged, this, &TextsPatch::onDownloaderProgressChanged);
- connect(downloader, &Downloader::downloadFinished, this, &TextsPatch::onDownloaderFinished);
- downloaders_.insert(downloader);
- downloader->start();
- }
- if (downloaders_.empty()) {
- emit operationFinished(E_DOWNLOAD, this);
- }
- }
- void TextsPatch::install()
- {
- foreach (QString db_name, databases_names) {
- ++elapsed_patches_to_install_;
- QMetaObject::invokeMethod(lotro_mgr_, "installPatch", Qt::QueuedConnection,
- Q_ARG(QString, getPatchName() + "_" + db_name),
- Q_ARG(QString, Settings::getValue("PatchDatabases/" + db_name + "/path").toString())
- );
- }
- }
- void TextsPatch::activate()
- {
- emit operationFinished(E_ACTIVATE, this);
- return;
- QString operation_name = Settings::getValue("Components/fonts").toBool() ? "enableCategory" : "disableCategory";
- ++elapsed_patches_to_install_;
- QMetaObject::invokeMethod(lotro_mgr_, operation_name.toLocal8Bit().constData(), Qt::QueuedConnection,
- Q_ARG(QString, getPatchName() + "_fonts"),
- Q_ARG(LotroDatManager::Category, LotroDatManager::Category::E_FONTS_COMMON)
- );
- operation_name = Settings::getValue("Components/texts_main").toBool() ? "enableCategory" : "disableCategory";
- ++elapsed_patches_to_install_;
- QMetaObject::invokeMethod(lotro_mgr_, operation_name.toLocal8Bit().constData(), Qt::QueuedConnection,
- Q_ARG(QString, getPatchName() + "_texts_main"),
- Q_ARG(LotroDatManager::Category, LotroDatManager::Category::E_TEXTS_COMMON)
- );
- operation_name = Settings::getValue("Components/texts_items").toBool() ? "enableCategory" : "disableCategory";
- ++elapsed_patches_to_install_;
- QMetaObject::invokeMethod(lotro_mgr_, operation_name.toLocal8Bit().constData(), Qt::QueuedConnection,
- Q_ARG(QString, getPatchName() + "_texts_items"),
- Q_ARG(LotroDatManager::Category, LotroDatManager::Category::E_TEXTS_ITEMS)
- );
- operation_name = Settings::getValue("Components/texts_emotes").toBool() ? "enableCategory" : "disableCategory";
- ++elapsed_patches_to_install_;
- QMetaObject::invokeMethod(lotro_mgr_, operation_name.toLocal8Bit().constData(), Qt::QueuedConnection,
- Q_ARG(QString, getPatchName() + "_texts_emotes"),
- Q_ARG(LotroDatManager::Category, LotroDatManager::Category::E_TEXTS_EMOTES)
- );
- operation_name = Settings::getValue("Components/videos").toBool() ? "enableCategory" : "disableCategory";
- ++elapsed_patches_to_install_;
- QMetaObject::invokeMethod(lotro_mgr_, operation_name.toLocal8Bit().constData(), Qt::QueuedConnection,
- Q_ARG(QString, getPatchName() + "_texts_videos_refs"),
- Q_ARG(LotroDatManager::Category, LotroDatManager::Category::E_TEXTS_VIDEOS_REFS)
- );
- }
- void TextsPatch::onDownloaderProgressChanged(Downloader *, Downloader::Status)
- {
- Downloader::Status all_downloads_status;
- foreach (Downloader* downloader, downloaders_) {
- all_downloads_status = all_downloads_status + downloader->getDownloadStatus();
- }
- emit progressChanged(OperationProgress(all_downloads_status), this);
- }
- void TextsPatch::onDownloaderFinished(Downloader *ptr)
- {
- ptr->targetFile->close();
- ptr->targetFile->deleteLater();
- Downloader::Status all_downloads_status;
- for (const Downloader* downloader : downloaders_) {
- all_downloads_status = all_downloads_status + downloader->getDownloadStatus();
- }
- if (!all_downloads_status.running) {
- for (Downloader* downloader : downloaders_) {
- downloader->deleteLater();
- }
- downloaders_.clear();
- emit operationFinished(E_DOWNLOAD, this);
- }
- }
- void TextsPatch::onLotroDatManagerOperationFinished(QString operation_name, QVector<QVariant> args, bool successful)
- {
- if (args.size() == 0 || !args[0].toString().contains(getPatchName())) {
- return; // This means, that message from LotroManager is addressed to another patchset
- }
- if (operation_name.contains("installPatch")) {
- if (!successful) {
- qCritical() << *this << "Database " + args[0].toString() + " (" + args[1].toString() + ") was not installed due to Legacy core error.";
- emit errorOccured(E_INSTALL, this, "Database " + args[0].toString() + " (" + args[1].toString() + ") was not installed due to Legacy core error.");
- }
- --elapsed_patches_to_install_;
- if (!elapsed_patches_to_install_) {
- is_being_patched_by_lotro_dat_manager_ = false;
- emit operationFinished(E_INSTALL, this);
- }
- }
- if (operation_name.contains("enableCategory") || operation_name.contains("disableCategory")) {
- if (!successful) {
- qCritical() << *this << "Error in patch " + args[0].toString() + ": activating category " + QString::number(args[1].toInt()) + " failed.";
- emit errorOccured(E_ACTIVATE, this, "Error in patch " + args[0].toString() + ": activating category " + QString::number(args[1].toInt()) + " failed.");
- }
- --elapsed_patches_to_install_;
- if (!elapsed_patches_to_install_) {
- is_being_patched_by_lotro_dat_manager_ = false;
- emit operationFinished(E_ACTIVATE, this);
- }
- }
- }
- void TextsPatch::onLotroDatManagerOperationStarted(QString operation_name, QVector<QVariant> args)
- {
- if (args.size() == 0 || !args[0].toString().contains(getPatchName())) {
- return;
- }
- is_being_patched_by_lotro_dat_manager_ = true;
- }
- void TextsPatch::onLotroDatManagerStatusChanged(LotroDatManager::Status status)
- {
- if (!is_being_patched_by_lotro_dat_manager_) {
- return;
- }
- emit progressChanged(OperationProgress(status), this);
- }
|