123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423 |
- #include "patchinstaller.h"
- #include "models/filesystem.h"
- #include "models/settings.h"
- #include <QDebug>
- #include <QProcess>
- #include <QApplication>
- #include <QSqlQuery>
- QString getComponentNameFromId(int id) {
- switch (id) {
- case 100: return "texts_main";
- case 101: return "texts_items";
- case 102: return "texts_emotes";
- case 200: return "maps";
- case 201: return "loadscreens";
- case 202: return "textures";
- case 300: return "sounds";
- case 301: return "videos";
- }
- return "none";
- }
- PatchInstaller::PatchInstaller(QObject *parent)
- : QObject(parent)
- , orig_files_db(QSqlDatabase::addDatabase("QSQLITE")) {
- }
- bool PatchInstaller::initialised() {
- return client_general_file_ && client_local_file_
- && client_general_file_->Initialized() && client_local_file_->Initialized();
- }
- PatchInstaller::~PatchInstaller() {
- deinit();
- }
- // ############## PRIVATE ############## //
- bool PatchInstaller::datPathIsRelevant() {
- QString game_folder = Settings::getValue("Lotro/game_path").toString();
- QString locale_prefix = Settings::getValue("Lotro/original_locale").toString();
- QString client_local_filepath = game_folder + "/client_local_" + locale_prefix + ".dat";
- QString client_general_filepath = game_folder + "/client_general.dat";
- QString client_local_current_path = QString::fromStdString(client_local_file_->GetFilename());
- QString client_general_current_path = QString::fromStdString(client_general_file_->GetFilename());
- return QFileInfo(client_local_filepath) != QFileInfo(client_local_current_path)
- || QFileInfo(client_general_filepath) != QFileInfo(client_general_current_path);
- }
- void PatchInstaller::deinit() {
- orig_files_db.close();
- if (client_local_file_)
- client_local_file_->Deinit();
- if (client_general_file_)
- client_general_file_->Deinit();
- }
- void PatchInstaller::installPatch(QString patch_name, LOTRO_DAT::Database* database) {
- if (!Settings::getValue("DatabaseNeedInstall/" + patch_name).toBool()) {
- return;
- }
- if (patch_name == "loadscreen") {
- installLoadscreens(database);
- return;
- }
- if (patch_name == "video") {
- installVideos(database);
- return;
- }
- if (patch_name == "micro" && !Settings::getValue("Components/micropatch").toBool()) {
- Settings::setValue("DatabaseNeedInstall/micropatch", false);
- return;
- }
- LOTRO_DAT::SubfileData file;
- qDebug() << "Total files in database " << database->CountRows();
- qDebug() << "Patching all files from database..." << database;
- while (!(file = database->GetNextFile()).Empty()) {
- current_status.finished_parts++;
- if (current_status.finished_parts * 100 / current_status.total_parts !=
- (current_status.finished_parts - 1) * 100 * 10 / current_status.total_parts) {
- // emitting if changed at least on 0.1%
- emit progressChanged(current_status);
- }
- if (!file.options["fid"]) {
- continue;
- }
- const int category = file.options["cat"] ? file.options["cat"].as<int>() : -1;
- QString component_name = getComponentNameFromId(category);
- if (category != -1 && !Settings::getValue("Components/" + component_name).toBool()) {
- continue;
- }
- const int dat_id = file.options["did"] ? file.options["did"].as<int>() : 0;
- const int file_id = file.options["fid"].as<int>();
- int file_version = -1;
- if (dat_id == E_CLIENT_LOCAL) {
- client_local_file_->PatchFile(file);
- file_version = client_local_file_->GetFileVersion(file_id);
- } else if (dat_id == E_CLIENT_GENERAL) {
- client_general_file_->PatchFile(file);
- file_version = client_local_file_->GetFileVersion(file_id);
- } else {
- qWarning() << "Unknown dat id parameter for file " << file.options["fid"].as<long long>() << " (dat id value = " << dat_id << "), SKIPPING!";
- }
- if (file_version != 721359) {
- const QString query = "INSERT INTO `patch_data` (`file_id`, `options`) VALUES ('"
- + QString::number(file_id) + "', '" + QString::number(file_version)
- + "') ON CONFLICT(`file_id`) DO UPDATE SET `options`='" + QString::number(file_version) + "';";
- qDebug() << "EXECUTING: " << query;
- orig_files_db.exec(query);
- }
- }
- Settings::setValue("DatabaseNeedInstall/" + patch_name, false);
- return;
- }
- void PatchInstaller::installLoadscreens(LOTRO_DAT::Database* database) {
- if (!Settings::getValue("Components/loadscreens").toBool()) {
- current_status.finished_parts += database->CountRows();
- emit progressChanged(current_status);
- Settings::setValue("DatabaseNeedInstall/loadscreen", false);
- return;
- }
- QString locale_prefix = Settings::getValue("Lotro/original_locale").toString();
- const QStringList loadscreens_filenames = {
- locale_prefix == "English" ? "lotro_ad_pregame.jpg" : "lotro_ad_pregame_" + locale_prefix + ".jpg",
- "lotro_generic_teleport_screen_01.jpg",
- "lotro_generic_teleport_screen_02.jpg",
- "lotro_generic_teleport_screen_03.jpg",
- "lotro_generic_teleport_screen_04.jpg",
- "lotro_generic_teleport_screen_05.jpg",
- "lotro_generic_teleport_screen_06.jpg",
- "lotro_generic_teleport_screen_07.jpg",
- "lotro_generic_teleport_screen_08.jpg",
- "lotro_generic_teleport_screen_09.jpg",
- "lotro_generic_teleport_screen_10.jpg"
- };
- LOTRO_DAT::SubfileData data;
- QString logo_path = Settings::getValue("Lotro/game_path").toString() + "/raw/" + (locale_prefix == "English" ? "en" : locale_prefix) + "/logo/";
- for (size_t i = 0; i < qMin(size_t(loadscreens_filenames.size()), database->CountRows()); ++i) {
- data = database->GetNextFile();
- QFile::remove(logo_path + loadscreens_filenames[i]);
- if (!data.binary_data.WriteToFile((logo_path + loadscreens_filenames[i]).toLocal8Bit())) {
- qWarning() << "InstallLoadscreens: Cannot write to file " << logo_path + loadscreens_filenames[i];
- }
- current_status.finished_parts++;
- if (current_status.finished_parts * 100 / current_status.total_parts !=
- (current_status.finished_parts - 1) * 100 * 10 / current_status.total_parts) {
- // emitting if changed at least on 0.1%
- emit progressChanged(current_status);
- }
- }
- Settings::setValue("DatabaseNeedInstall/loadscreen", false);
- }
- void PatchInstaller::installVideos(LOTRO_DAT::Database* database) {
- current_status.finished_parts += database->CountRows();
- emit progressChanged(current_status);
- download_video_total_videos = database->CountRows();
- download_video_finished_videos = 0;
- if (!Settings::getValue("Components/videos").toBool()) {
- Settings::setValue("DatabaseNeedInstall/video", false);
- return;
- }
- LOTRO_DAT::SubfileData file;
- while (!(file = database->GetNextFile()).Empty()) {
- if (!file.options["name"] || !file.options["url"] || !file.options["hash"] || !file.options["folder"]) {
- download_video_finished_videos++;
- continue;
- }
- const QString filename = QString::fromStdString(file.options["name"].as<std::string>());
- const QString url = QString::fromStdString(file.options["url"].as<std::string>());
- const QString hash = QString::fromStdString(file.options["hash"].as<std::string>());
- const QString folder = QString::fromStdString(file.options["folder"].as<std::string>());
- const QString full_filename = Settings::getValue("Lotro/game_path").toString() + "/" + folder + "/" + filename;
- FileSystem::createFilePath(full_filename);
- if (FileSystem::fileExists(full_filename) && FileSystem::fileHash(full_filename) == hash) {
- download_video_finished_videos++;
- continue;
- }
- QFile* target_file = new QFile(full_filename);
- target_file->open(QIODevice::WriteOnly);
- Downloader* video_downloader = new Downloader(this);
- connect(video_downloader, &Downloader::progressChanged, this, &PatchInstaller::onDownloaderProgressChanged);
- video_downloader->setUrl(url);
- video_downloader->targetFile = target_file;
- video_downloader->start();
- video_downloader->waitForDownloaded();
- video_downloader->targetFile->close();
- video_downloader->targetFile->deleteLater();
- video_downloader->targetFile = nullptr;
- video_downloader->deleteLater();
- download_video_finished_videos++;
- }
- Settings::setValue("DatabaseNeedInstall/video", false);
- }
- // ############## PUBLIC SLOTS ############## //
- void PatchInstaller::init()
- {
- qDebug() << __FUNCTION__ << "Starting initialisation of LotroDatManager";
- qRegisterMetaType<PatchInstaller::Status>();
- QString game_folder = Settings::getValue("Lotro/game_path").toString();
- QString locale_prefix = Settings::getValue("Lotro/original_locale").toString();
- QString client_local_filepath = game_folder + "/client_local_" + locale_prefix + ".dat";
- QString client_general_filepath = game_folder + "/client_general.dat";
- if (!FileSystem::fileExists(client_local_filepath) || !FileSystem::fileExists(client_general_filepath)) {
- qCritical() << __FUNCTION__ << "DatFiles do not exist!" << client_local_filepath << " " << client_general_filepath;
- return;
- }
- // Updating file permissions to be sure, that they're not in read-only mode
- if (!QFile::setPermissions(client_local_filepath, QFileDevice::Permission(0x6666))) {
- qDebug() << __FUNCTION__ << "Unable to update permissions on client_local_* file!";
- }
- if (!QFile::setPermissions(client_general_filepath, QFileDevice::Permission(0x6666))) {
- qDebug() << __FUNCTION__ << "Unable to update permissions on client_general* file!";
- }
- // Initialising client_local_*.dat file and client_general.dat
- client_local_file_ = new LOTRO_DAT::DatFile(1);
- client_general_file_ = new LOTRO_DAT::DatFile(2);
- auto client_local_init_res = client_local_file_->Init(client_local_filepath.toStdString());
- auto client_general_init_res = client_general_file_->Init(client_general_filepath.toStdString());
- if (!client_local_init_res || !client_general_init_res) {
- client_local_file_->Deinit();
- client_general_file_->Deinit();
- qCritical() << __FUNCTION__ << "Finished LotroDatManager initialisation - error: DatFile initialisation error!";
- return;
- }
- // Initializing db for original files backup
- QString database_path = game_folder + "/LotroLegacy/orig_files.db";
- if (!FileSystem::fileExists(database_path)) {
- FileSystem::createFilePath(database_path);
- }
- orig_files_db.setDatabaseName(database_path);
- if (!orig_files_db.open()) {
- qCritical() << "Initializing PatchInstaller - Cannot open backup database!!!!!!! " << database_path;
- }
- orig_files_db.exec(create_table_query);
- orig_files_db.exec("PRAGMA synchronous = OFF");
- orig_files_db.exec("PRAGMA count_changes = OFF");
- orig_files_db.exec("PRAGMA journal_mode = MEMORY");
- orig_files_db.exec("PRAGMA temp_store = MEMORY");
- orig_files_db.exec("PRAGMA encoding = \"UTF-8\";");
- qDebug() << "LotroDatManager initialisation successfull! Dat files: "
- << QString::fromStdString(client_general_file_->GetFilename())
- << QString::fromStdString(client_local_file_->GetFilename());
- }
- void PatchInstaller::startGame(bool freeze_updates) {
- // if freeze_updates is set to True, original game
- // launcher will be replaced with special program,
- // which controls lotro startup and prevents from updates
- QString game_folder = Settings::getValue("Lotro/game_path").toString();
- if (game_folder == "none") {
- qCritical() << __FUNCTION__ << "Starting game FAILED - game folder wasnt set!";
- return;
- }
- if (!FileSystem::fileExists(QApplication::applicationDirPath() + "/Launcher.exe")) {
- qCritical() << __FUNCTION__ << "Starting game FAILED - no game launcher in legacy directory found!";
- return;
- }
- if (freeze_updates) {
- QFile::remove(game_folder + "/lotro_ru.exe");
- if (!QFile::copy(QApplication::applicationDirPath() + "/LotroLauncher.exe", game_folder + "/lotro_ru.exe")) {
- qCritical() << __FUNCTION__ << "Starting game FAILED - cannot copy LotroLauncher to lotro_ru.exe!!";
- return;
- }
- QFile::remove(game_folder + "/LotroLauncher.exe");
- if (!QFile::copy(QApplication::applicationDirPath() + "/Launcher.exe", game_folder + "/LotroLauncher.exe")) {
- qCritical() << __FUNCTION__ << "Starting game FAILED - cannot copy GameLauncher to LotroLauncher!!";
- return;
- }
- QFile file(game_folder + "/legacy_path.txt");
- file.open(QIODevice::WriteOnly);
- QTextStream out(&file);
- out << QApplication::applicationDirPath() + "/LegacyLauncher.exe";
- file.close();
- } else {
- QFile::remove(game_folder + "/LotroLauncher.exe");
- if (!QFile::copy(QApplication::applicationDirPath() + "/LotroLauncher.exe", game_folder + "/LotroLauncher.exe")) {
- qCritical() << __FUNCTION__ << "Starting game FAILED - cannot copy LotroLauncher from working dir to LotroLauncher in lotro dir!!";
- return;
- }
- }
- QStringList args;
- if (freeze_updates) {
- args << "gamelaunch" << "-disablePatch";
- }
- if (Settings::getValue("Lotro/skip_raw_download").toBool()) {
- args << "-skiprawdownload";
- }
- if (Settings::getValue("Lotro/no_splash_screen").toBool()) {
- args << "-nosplashscreen";
- }
- client_general_file_->Deinit();
- client_local_file_->Deinit();
- QString username = Settings::getValue("Account/username").toString();
- QString password = Settings::getValue("Account/password").toString();
- if (!username.isEmpty() && !password.isEmpty()) {
- args << "-username" << username << "-password" << password;
- }
- qDebug() << __FUNCTION__ << "Starting game with arguments: " << args;
- QFile f(Settings::getValue("Lotro/game_path").toString() + "/LotroLauncher.exe");
- QProcess process;
- if (FileSystem::fileExists(f.fileName())) {
- if (f.fileName().contains(" ")) {
- f.setFileName("\"" + f.fileName() + "\"");
- }
- process.startDetached(f.fileName(), args);
- process.waitForFinished(-1);
- QApplication::quit();
- }
- }
- void PatchInstaller::startPatchInstallationChain() {
- emit started();
- qInfo() << "PatchInstaller: Starting installation chain...";
- const QVector<QString> patches = {"text", "font", "image", "loadscreen", "texture", "sound", "video", "micro"};
- QMap<QString, LOTRO_DAT::Database*> patch_databases;
- current_status.total_parts = 0;
- current_status.finished_parts = 0;
- for (const QString& patch: patches) {
- if (!Settings::getValue("DatabaseNeedInstall/" + patch).toBool()) {
- continue;
- }
- const QString patch_hashsum = Settings::getValue("PatchDatabases/" + patch + "/hashsum").toString();
- const QString patch_filename = Settings::getValue("PatchDatabases/" + patch + "/path").toString();
- const QString real_file_hashsum = FileSystem::fileHash(patch_filename);
- if (!FileSystem::fileExists(patch_filename) || real_file_hashsum != patch_hashsum) {
- qCritical() << "PatchInstallation: Incorrect patch file: " << patch_filename << ", hashsum: " << real_file_hashsum << ", expected: " << patch_hashsum;
- continue;
- }
- LOTRO_DAT::Database* db = new LOTRO_DAT::Database();
- if (!db->InitDatabase(patch_filename.toStdString())) {
- qCritical() << "PatchInstallation: failed to initialize db " << patch_filename;
- continue;
- }
- patch_databases[patch] = db;
- current_status.total_parts += db->CountRows();
- }
- emit progressChanged(current_status);
- for (const QString patch_name: patch_databases.keys()) {
- qInfo() << "PatchInstaller: Installing patch " << patch_name;
- installPatch(patch_name, patch_databases[patch_name]);
- patch_databases[patch_name]->CloseDatabase();
- delete patch_databases[patch_name];
- }
- qInfo() << "PatchInstaller: Finished installation chain...";
- emit finished();
- }
- // ############## PRIVATE SLOTS ############## //
- void PatchInstaller::onDownloaderProgressChanged(Downloader*, Downloader::Status progress) {
- emit videosDownloadProgressChanged(download_video_finished_videos, download_video_total_videos, progress);
- }
|