123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259 |
- #include "lotrodatmanager.h"
- #include "models/filesystem.h"
- #include "models/settings.h"
- #include <QDebug>
- #include <QProcess>
- Q_DECLARE_METATYPE(LotroDatManager::Category)
- LotroDatManager::LotroDatManager(QObject *parent) : QObject(parent), client_local_file_(1), client_general_file_(2) {
- }
- LotroDatManager::~LotroDatManager() {
- deinitializeManager();
- }
- bool LotroDatManager::initialised() {
- return client_general_file_.Initialized() && client_local_file_.Initialized();
- }
- bool LotroDatManager::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 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 (!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_.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();
- 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_.GetFilename())
- << QString::fromStdString(client_local_file_.GetFilename());
- emit operationFinished("initializeManager", {}, true);
- }
- void LotroDatManager::deinitializeManager() {
- emit operationStarted("deinitializeManager");
- client_local_file_.Deinit();
- client_general_file_.Deinit();
- emit operationFinished("deinitializeManager");
- }
- void LotroDatManager::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
- emit operationStarted("startGame");
- 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", {}, "GameFolderNotSet");
- emit operationFinished("startGame", {}, false);
- return;
- }
- if (!FileSystem::fileExists(QApplication::applicationDirPath() + "/Launcher.exe")) {
- qDebug() << __FUNCTION__ << "Starting game FAILED - no game launcher in legacy directory found!";
- emit errorOccured("startGame", {}, "NoGameLauncherInLegacyDir");
- emit operationFinished("startGame", {}, false);
- return;
- }
- if (freeze_updates) {
- 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", {}, "LauncherCopyFailed");
- emit operationFinished("startGame", {}, 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", {}, "NoAccessToGameLauncher");
- emit operationFinished("startGame", {}, 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", {}, "NoAccessToGameLauncher");
- emit operationFinished("startGame", {}, false);
- 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();
- }
- emit operationFinished("startGame");
- }
- void LotroDatManager::installPatch(QString patch_name, QString database_path, RESOURCE_FILE_TYPE dat_file) {
- emit operationStarted("installPatch", {patch_name, database_path});
- LOTRO_DAT::Database db;
- if (!db.InitDatabase(database_path.toStdString())) {
- qDebug() << __FUNCTION__ << "Error database " << database_path << ": failed to open!";
- return;
- }
- size_t patched_files_num = 0;
- LOTRO_DAT::SubfileData file;
- int i = 0;
- const int total_files = db.CountRows();
- qDebug() << "Patching all files from database...";
- while (!(file = db.GetNextFile()).Empty()) {
- if (i * 100 / total_files != (i - 1) * 100 / total_files) {
- qDebug() << "Completed " << i * 100 / total_files << "%";
- }
- ++i;
- if (!file.options["fid"]) {
- continue;
- }
- if (dat_file == E_CLIENT_LOCAL) {
- client_local_file_.PatchFile(file);
- } else if (dat_file == E_CLIENT_GENERAL) {
- client_general_file_.PatchFile(file);
- }
- ++patched_files_num;
- }
- db.CloseDatabase();
- emit operationFinished("installPatch", {patch_name, database_path}, true);
- }
- void LotroDatManager::createBackup()
- {
- // emit operationStarted("createBackup");
- // QString locale_prefix = Settings::getValue("Lotro/original_locale").toString();
- // QString client_local_backup_path = QApplication::applicationDirPath() + Settings::getValue("Backup/path").toString() + "client_local_" + locale_prefix + ".dat";
- // QString client_general_backup_path = QApplication::applicationDirPath() + Settings::getValue("Backup/path").toString() + "client_general.dat";
- // auto loc_res = client_local_file_.GetBackupManager().CreateBackup(client_local_backup_path.toStdString());
- // auto gen_res = client_general_file_.GetBackupManager().CreateBackup(client_general_backup_path.toStdString());
- // bool operations_result = (loc_res.result == LOTRO_DAT::SUCCESS && gen_res.result == LOTRO_DAT::SUCCESS);
- // emit operationFinished("createBackup", {client_local_backup_path, client_general_backup_path}, operations_result);
- }
- void LotroDatManager::restoreFromBackup()
- {
- // emit operationStarted("restoreFromBackup");
- // QString locale_prefix = Settings::getValue("Lotro/original_locale").toString();
- // QString client_local_backup_path = QApplication::applicationDirPath() + Settings::getValue("Backup/path").toString() + "client_local_" + locale_prefix + ".dat";
- // QString client_general_backup_path = QApplication::applicationDirPath() + Settings::getValue("Backup/path").toString() + "client_general.dat";
- // auto loc_res = client_local_file_.GetBackupManager().RestoreFromBackup(client_local_backup_path.toStdString());
- // auto gen_res = client_general_file_.GetBackupManager().RestoreFromBackup(client_general_backup_path.toStdString());
- // bool operations_result = (loc_res.result == LOTRO_DAT::SUCCESS && gen_res.result == LOTRO_DAT::SUCCESS);
- // emit operationFinished("restoreFromBackup", {}, operations_result);
- }
- void LotroDatManager::removeBackup()
- {
- // emit operationStarted("removeBackup");
- // QString locale_prefix = Settings::getValue("Lotro/original_locale").toString();
- // QString client_local_backup_path = QApplication::applicationDirPath() + Settings::getValue("Backup/path").toString() + "client_local_" + locale_prefix + ".dat";
- // QString client_general_backup_path = QApplication::applicationDirPath() + Settings::getValue("Backup/path").toString() + "client_general.dat";
- // auto loc_res = client_local_file_.GetBackupManager().RemoveBackup(client_local_backup_path.toStdString());
- // auto gen_res = client_general_file_.GetBackupManager().RemoveBackup(client_general_backup_path.toStdString());
- // bool operations_result = (loc_res.result == LOTRO_DAT::SUCCESS && gen_res.result == LOTRO_DAT::SUCCESS);
- // emit operationFinished("removeBackup", {}, operations_result);
- }
|