123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404 |
- #include "lotrodatmanager.h"
- #include "models/filesystem.h"
- #include "models/settings.h"
- #include <QDebug>
- #include <QProcess>
- Q_DECLARE_METATYPE(LOTRO_DAT::DatLocaleManager::LOCALE)
- Q_DECLARE_METATYPE(LotroDatManager::Category)
- Q_DECLARE_METATYPE(LotroDatManager::Status)
- void debug::ClientLocalDatStatusChangedHandler(DatStatus::ProgressInfo info) {
- if (info.status == DatStatus::DAT_STATUS::E_FREE) {
- std::cout << "CLIENT_LOCAL: operation finished" << std::endl;
- return;
- }
- if (info.status == DatStatus::DAT_STATUS::E_BACKUP_CREATING) {
- std::cout << "CLIENT_LOCAL: creating backup " << info.percentage << "% "
- << "(" << info.finished_parts << "/" << info.total_parts << ")" << std::endl;
- return;
- }
- if (info.status == DatStatus::DAT_STATUS::E_BACKUP_REMOVING) {
- std::cout << "CLIENT_LOCAL: removing backup " << info.percentage << "% "
- << "(" << info.finished_parts << "/" << info.total_parts << ")" << std::endl;
- return;
- }
- if (info.status == DatStatus::DAT_STATUS::E_BACKUP_RESTORING) {
- std::cout << "CLIENT_LOCAL: restoring backup " << info.percentage << "% "
- << "(" << info.finished_parts << "/" << info.total_parts << ")" << std::endl;
- return;
- }
- if (info.status == DatStatus::DAT_STATUS::E_COMMITING) {
- std::cout << "CLIENT_LOCAL: applying locales " << info.percentage << "% "
- << "(" << info.finished_parts << "/" << info.total_parts << ")" << std::endl;
- return;
- }
- if (info.status == DatStatus::DAT_STATUS::E_EXTRACTING) {
- std::cout << "CLIENT_LOCAL: extracting data " << info.percentage << "% "
- << "(" << info.finished_parts << "/" << info.total_parts << ")" << std::endl;
- return;
- }
- if (info.status == DatStatus::DAT_STATUS::E_GATHERING_INFO) {
- std::cout << "CLIENT_LOCAL: gathering info " << info.percentage << "% "
- << "(" << info.finished_parts << "/" << info.total_parts << ")" << std::endl;
- return;
- }
- if (info.status == DatStatus::DAT_STATUS::E_INITIALISING) {
- std::cout << "CLIENT_LOCAL: initialising " << info.percentage << "% "
- << "(" << info.finished_parts << "/" << info.total_parts << ")" << std::endl;
- return;
- }
- if (info.status == DatStatus::DAT_STATUS::E_PATCHING) {
- std::cout << "CLIENT_LOCAL: applying patch " << info.percentage << "% "
- << "(" << info.finished_parts << "/" << info.total_parts << ")" << std::endl;
- return;
- }
- }
- void debug::ClientGeneralDatStatusChangedHandler(DatStatus::ProgressInfo info) {
- if (info.status == DatStatus::DAT_STATUS::E_FREE) {
- std::cout << "CLIENT_GENERAL: operation finished" << std::endl;
- return;
- }
- if (info.status == DatStatus::DAT_STATUS::E_BACKUP_CREATING) {
- std::cout << "CLIENT_GENERAL: creating backup " << info.percentage << "% "
- << "(" << info.finished_parts << "/" << info.total_parts << ")" << std::endl;
- return;
- }
- if (info.status == DatStatus::DAT_STATUS::E_BACKUP_REMOVING) {
- std::cout << "CLIENT_GENERAL: removing backup " << info.percentage << "% "
- << "(" << info.finished_parts << "/" << info.total_parts << ")" << std::endl;
- return;
- }
- if (info.status == DatStatus::DAT_STATUS::E_BACKUP_RESTORING) {
- std::cout << "CLIENT_GENERAL: restoring backup " << info.percentage << "% "
- << "(" << info.finished_parts << "/" << info.total_parts << ")" << std::endl;
- return;
- }
- if (info.status == DatStatus::DAT_STATUS::E_COMMITING) {
- std::cout << "CLIENT_GENERAL: applying locales " << info.percentage << "% "
- << "(" << info.finished_parts << "/" << info.total_parts << ")" << std::endl;
- return;
- }
- if (info.status == DatStatus::DAT_STATUS::E_EXTRACTING) {
- std::cout << "CLIENT_GENERAL: extracting data " << info.percentage << "% "
- << "(" << info.finished_parts << "/" << info.total_parts << ")" << std::endl;
- return;
- }
- if (info.status == DatStatus::DAT_STATUS::E_GATHERING_INFO) {
- std::cout << "CLIENT_GENERAL: gathering info " << info.percentage << "% "
- << "(" << info.finished_parts << "/" << info.total_parts << ")" << std::endl;
- return;
- }
- if (info.status == DatStatus::DAT_STATUS::E_INITIALISING) {
- std::cout << "CLIENT_GENERAL: initialising " << info.percentage << "% "
- << "(" << info.finished_parts << "/" << info.total_parts << ")" << std::endl;
- return;
- }
- if (info.status == DatStatus::DAT_STATUS::E_PATCHING) {
- std::cout << "CLIENT_GENERAL: applying patch " << info.percentage << "% "
- << "(" << info.finished_parts << "/" << info.total_parts << ")" << std::endl;
- return;
- }
- }
- LotroDatManager::LotroDatManager(QObject *parent) :
- QObject(parent) {
- qRegisterMetaType<Status>("Status");
- qRegisterMetaType<LOTRO_DAT::DatLocaleManager::LOCALE>("LOCALE");
- client_local_file_.GetStatusModule().AddStatusChangedCallbackFunction(client_local_status_updated_callback_);
- client_general_file_.GetStatusModule().AddStatusChangedCallbackFunction(client_general_status_updated_callback_);
- }
- LotroDatManager::~LotroDatManager()
- {
- deinitializeManager();
- }
- bool LotroDatManager::Initialised()
- {
- return client_general_file_.Initialized() && client_local_file_.Initialized();
- }
- bool LotroDatManager::NotPatched()
- {
- return !client_local_file_.GetStatusModule().CheckIfNotPatched() && !client_local_file_.GetStatusModule().CheckIfNotPatched();
- }
- void LotroDatManager::initializeManager()
- {
- emit operationStarted("initializeManager");
- qDebug() << __FUNCTION__ << "Starting initialisation of LotroDatManager";
- 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 (game_folder == "none") {
- qDebug() << __FUNCTION__ << "Finished initialisation LotroDatManager - error: .dat files not found!";
- emit errorOccured("initializeManager", {}, "FolderNotDefined");
- emit operationFinished("initializeManager", {}, false);
- return;
- }
- if (!FileSystem::fileExists(client_local_filepath) || !FileSystem::fileExists(client_general_filepath)) {
- emit errorOccured("initializeManager", {}, "DatFilesNotFound");
- emit operationFinished("initializeManager", {}, false);
- 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
- auto client_local_init_res = client_local_file_.Initialise(client_local_filepath.toStdString(), 0);
- auto client_general_init_res = client_general_file_.Initialise(client_general_filepath.toStdString(), 1);
- if (client_local_init_res.result != LOTRO_DAT::SUCCESS
- || client_general_init_res.result != LOTRO_DAT::SUCCESS)
- {
- client_local_file_.Deinitialize();
- client_general_file_.Deinitialize();
- qDebug() << __FUNCTION__ << "Finished LotroDatManager initialisation - error: DatFile initialisation error!";
- emit errorOccured("initializeManager", {}, "DatInitError");
- emit operationFinished("initializeManager", {}, false);
- return;
- }
- qDebug() << "LotroDatManager initialisation successfull! Dat files: "
- << QString::fromStdString(client_general_file_.GetIO().GetFilename().value)
- << QString::fromStdString(client_local_file_.GetIO().GetFilename().value);
- emit operationFinished("initializeManager", {}, true);
- }
- void LotroDatManager::deinitializeManager()
- {
- emit operationStarted("deinitializeManager");
- client_local_file_.Deinitialize();
- client_general_file_.Deinitialize();
- emit operationFinished("deinitializeManager");
- }
- void LotroDatManager::startGame(LOTRO_DAT::DatLocaleManager::LOCALE locale)
- {
- emit operationStarted("startGame", {locale});
- client_general_file_.GetLocaleManager().SetLocale(locale);
- client_local_file_.GetLocaleManager().SetLocale(locale);
- QString game_folder = Settings::getValue("Lotro/game_path").toString();
- if (game_folder == "none") {
- qDebug() << __FUNCTION__ << "Starting game FAILED - game folder wasnt set!";
- emit errorOccured("startGame", {locale}, "GameFolderNotSet");
- emit operationFinished("startGame", {locale}, false);
- return;
- }
- if (!FileSystem::fileExists(QApplication::applicationDirPath() + "/Launcher.exe")) {
- qDebug() << __FUNCTION__ << "Starting game FAILED - no game launcher in legacy directory found!";
- emit errorOccured("startGame", {locale}, "NoGameLauncherInLegacyDir");
- emit operationFinished("startGame", {locale}, false);
- return;
- }
- if (locale == LOTRO_DAT::DatLocaleManager::PATCHED) {
- QFile::remove(game_folder + "/lotro_ru.exe");
- if (!QFile::copy(QApplication::applicationDirPath() + "/LotroLauncher.exe", game_folder + "/lotro_ru.exe")) {
- qDebug() << __FUNCTION__ << "Starting game FAILED - cannot copy LotroLauncher to lotro_ru.exe!!";
- emit errorOccured("startGame", {locale}, "LauncherCopyFailed");
- emit operationFinished("startGame", {locale}, false);
- return;
- }
- QFile::remove(game_folder + "/LotroLauncher.exe");
- if (!QFile::copy(QApplication::applicationDirPath() + "/Launcher.exe", game_folder + "/LotroLauncher.exe")) {
- qDebug() << __FUNCTION__ << "Starting game FAILED - cannot copy GameLauncher to LotroLauncher!!";
- emit errorOccured("startGame", {locale}, "NoAccessToGameLauncher");
- emit operationFinished("startGame", {locale}, false);
- 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")) {
- qDebug() << __FUNCTION__ << "Starting game FAILED - cannot copy LotroLauncher from working dir to LotroLauncher in lotro dir!!";
- emit errorOccured("startGame", {locale}, "NoAccessToGameLauncher");
- emit operationFinished("startGame", {locale}, false);
- return;
- }
- }
- if (!startLotroLauncherWithParameters(locale)) {
- emit errorOccured("startGame", {locale}, "StartLotroLauncherWithParametersFailed");
- emit operationFinished("startGame", {locale}, false);
- return;
- }
- emit operationFinished("startGame", {locale}, true);
- }
- void LotroDatManager::installPatch(QString patch_name, QString database_path)
- {
- emit operationStarted("installPatch", {patch_name, database_path});
- LOTRO_DAT::Database db;
- if (!db.InitDatabase(database_path.toStdString())) {
- emit errorOccured("installPatch", {patch_name, database_path}, "ErrorInitDatabase");
- emit operationFinished("installPatch", {patch_name, database_path}, false);
- return;
- }
- if (client_local_file_.GetPatcher().PatchAllDatabase(&db).result != LOTRO_DAT::SUCCESS) {
- db.CloseDatabase();
- emit errorOccured("installPatch", {patch_name, database_path}, "ErrorPatchClientLocal");
- emit operationFinished("installPatch", {patch_name, database_path}, false);
- return;
- }
- if (client_general_file_.GetPatcher().PatchAllDatabase(&db).result != LOTRO_DAT::SUCCESS) {
- db.CloseDatabase();
- emit errorOccured("installPatch", {patch_name, database_path});
- emit operationFinished("installPatch", {patch_name, database_path}, false);
- return;
- }
- db.CloseDatabase();
- emit operationFinished("installPatch", {patch_name, database_path}, true);
- }
- void LotroDatManager::enableCategory(QString patch_name, LotroDatManager::Category category)
- {
- emit operationStarted("enableCategory", {patch_name, category});
- if (client_local_file_.GetLocaleManager().EnableCategory(category).result != LOTRO_DAT::SUCCESS) {
- emit errorOccured("enableCategory", {patch_name, category}, "ErrorEnableCategoryClientLocal");
- emit operationFinished("enableCategory", {patch_name, category}, false);
- return;
- }
- client_local_file_.GetFileSystem().CommitDirectories();
- client_local_file_.GetLocaleManager().CommitLocales();
- if (client_general_file_.GetLocaleManager().EnableCategory(category).result != LOTRO_DAT::SUCCESS) {
- emit errorOccured("enableCategory", {patch_name, category}, "ErrorEnableCategoryClientGeneral");
- emit operationFinished("enableCategory", {patch_name, category}, false);
- return;
- }
- client_general_file_.GetFileSystem().CommitDirectories();
- client_general_file_.GetLocaleManager().CommitLocales();
- emit operationFinished("enableCategory", {patch_name, category});
- }
- void LotroDatManager::disableCategory(QString patch_name, LotroDatManager::Category category)
- {
- emit operationStarted("disableCategory", {patch_name, category});
- if (client_local_file_.GetLocaleManager().DisableCategory(category).result != LOTRO_DAT::SUCCESS) {
- emit errorOccured("disableCategory", {patch_name, category}, "ErrorDisableCategoryClientLocal");
- emit operationFinished("disableCategory", {patch_name, category}, false);
- return;
- }
- if (client_general_file_.GetLocaleManager().DisableCategory(category).result != LOTRO_DAT::SUCCESS) {
- emit errorOccured("disableCategory", {patch_name, category}, "ErrorDisableCategoryClientGeneral");
- emit operationFinished("disableCategory", {patch_name, category}, false);
- return;
- }
- emit operationFinished("disableCategory", {patch_name, category}, true);
- }
- void LotroDatManager::createBackup()
- {
- // TODO: Error handling
- emit operationStarted("createBackup");
- QString locale_prefix = Settings::getValue("Lotro/original_locale").toString();
- client_local_file_.GetBackupManager().CreateBackup((QApplication::applicationDirPath() + "/backup/client_local_" + locale_prefix + ".dat").toStdString());
- client_general_file_.GetBackupManager().CreateBackup((QApplication::applicationDirPath() + "/backup/client_general.dat").toStdString());
- emit operationFinished("createBackup");
- }
- void LotroDatManager::restoreFromBackup()
- {
- // TODO: Error handling
- emit operationStarted("restoreFromBackup");
- QString locale_prefix = Settings::getValue("Lotro/original_locale").toString();
- client_local_file_.GetBackupManager().RestoreFromBackup((QApplication::applicationDirPath() + "/backup/client_local_" + locale_prefix + ".dat").toStdString());
- client_general_file_.GetBackupManager().RestoreFromBackup((QApplication::applicationDirPath() + "/backup/client_general.dat").toStdString());
- emit operationFinished("restoreFromBackup");
- }
- void LotroDatManager::removeBackup()
- {
- // TODO: Error handling
- emit operationStarted("removeBackup");
- QString locale_prefix = Settings::getValue("Lotro/original_locale").toString();
- client_local_file_.GetBackupManager().RemoveBackup((QApplication::applicationDirPath() + "/backup/client_local_" + locale_prefix + ".dat").toStdString());
- client_general_file_.GetBackupManager().RemoveBackup((QApplication::applicationDirPath() + "/backup/client_general.dat").toStdString());
- emit operationFinished("removeBackup");
- }
- bool LotroDatManager::startLotroLauncherWithParameters(LOTRO_DAT::DatLocaleManager::LOCALE locale)
- {
- QStringList args;
- if (Settings::getValue("Lotro/skip_raw_download").toString() == "True") {
- args << "-skiprawdownload";
- }
- if (Settings::getValue("Lotro/no_splash_screen").toString() == "True") {
- args << "-nosplashscreen";
- }
- if (locale == LOTRO_DAT::DatLocaleManager::PATCHED) {
- args << "gamelaunch" << "-disablePatch";
- }
- client_general_file_.Deinitialize();
- client_local_file_.Deinitialize();
- 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();
- return true;
- } else {
- return false;
- }
- }
|