lotrodatmanager.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. #include "lotrodatmanager.h"
  2. #include "models/filesystem.h"
  3. #include "models/settings.h"
  4. #include <QDebug>
  5. #include <QProcess>
  6. Q_DECLARE_METATYPE(LotroDatManager::Category)
  7. LotroDatManager::LotroDatManager(QObject *parent) : QObject(parent), client_local_file_(1), client_general_file_(2) {
  8. }
  9. LotroDatManager::~LotroDatManager() {
  10. deinitializeManager();
  11. }
  12. bool LotroDatManager::initialised() {
  13. return client_general_file_.Initialized() && client_local_file_.Initialized();
  14. }
  15. bool LotroDatManager::datPathIsRelevant() {
  16. QString game_folder = Settings::getValue("Lotro/game_path").toString();
  17. QString locale_prefix = Settings::getValue("Lotro/original_locale").toString();
  18. QString client_local_filepath = game_folder + "/client_local_" + locale_prefix + ".dat";
  19. QString client_general_filepath = game_folder + "/client_general.dat";
  20. QString client_local_current_path = QString::fromStdString(client_local_file_.GetFilename());
  21. QString client_general_current_path = QString::fromStdString(client_general_file_.GetFilename());
  22. return QFileInfo(client_local_filepath) != QFileInfo(client_local_current_path)
  23. || QFileInfo(client_general_filepath) != QFileInfo(client_general_current_path);
  24. }
  25. void LotroDatManager::initializeManager()
  26. {
  27. emit operationStarted("initializeManager");
  28. qDebug() << __FUNCTION__ << "Starting initialisation of LotroDatManager";
  29. QString game_folder = Settings::getValue("Lotro/game_path").toString();
  30. QString locale_prefix = Settings::getValue("Lotro/original_locale").toString();
  31. QString client_local_filepath = game_folder + "/client_local_" + locale_prefix + ".dat";
  32. QString client_general_filepath = game_folder + "/client_general.dat";
  33. if (!FileSystem::fileExists(client_local_filepath) || !FileSystem::fileExists(client_general_filepath)) {
  34. emit errorOccured("initializeManager", {}, "DatFilesNotFound");
  35. emit operationFinished("initializeManager", {}, false);
  36. return;
  37. }
  38. // Updating file permissions to be sure, that they're not in read-only mode
  39. if (!QFile::setPermissions(client_local_filepath, QFileDevice::Permission(0x6666))) {
  40. qDebug() << __FUNCTION__ << "Unable to update permissions on client_local_* file!";
  41. }
  42. if (!QFile::setPermissions(client_general_filepath, QFileDevice::Permission(0x6666))) {
  43. qDebug() << __FUNCTION__ << "Unable to update permissions on client_general* file!";
  44. }
  45. // Initialising client_local_*.dat file and client_general.dat
  46. auto client_local_init_res = client_local_file_.Init(client_local_filepath.toStdString());
  47. auto client_general_init_res = client_general_file_.Init(client_general_filepath.toStdString());
  48. if (!client_local_init_res || !client_general_init_res) {
  49. client_local_file_.Deinit();
  50. client_general_file_.Deinit();
  51. qDebug() << __FUNCTION__ << "Finished LotroDatManager initialisation - error: DatFile initialisation error!";
  52. emit errorOccured("initializeManager", {}, "DatInitError");
  53. emit operationFinished("initializeManager", {}, false);
  54. return;
  55. }
  56. qDebug() << "LotroDatManager initialisation successfull! Dat files: "
  57. << QString::fromStdString(client_general_file_.GetFilename())
  58. << QString::fromStdString(client_local_file_.GetFilename());
  59. emit operationFinished("initializeManager", {}, true);
  60. }
  61. void LotroDatManager::deinitializeManager() {
  62. emit operationStarted("deinitializeManager");
  63. client_local_file_.Deinit();
  64. client_general_file_.Deinit();
  65. emit operationFinished("deinitializeManager");
  66. }
  67. void LotroDatManager::startGame(bool freeze_updates) {
  68. // if freeze_updates is set to True, original game
  69. // launcher will be replaced with special program,
  70. // which controls lotro startup and prevents from updates
  71. emit operationStarted("startGame");
  72. QString game_folder = Settings::getValue("Lotro/game_path").toString();
  73. if (game_folder == "none") {
  74. qDebug() << __FUNCTION__ << "Starting game FAILED - game folder wasnt set!";
  75. emit errorOccured("startGame", {}, "GameFolderNotSet");
  76. emit operationFinished("startGame", {}, false);
  77. return;
  78. }
  79. if (!FileSystem::fileExists(QApplication::applicationDirPath() + "/Launcher.exe")) {
  80. qDebug() << __FUNCTION__ << "Starting game FAILED - no game launcher in legacy directory found!";
  81. emit errorOccured("startGame", {}, "NoGameLauncherInLegacyDir");
  82. emit operationFinished("startGame", {}, false);
  83. return;
  84. }
  85. if (freeze_updates) {
  86. QFile::remove(game_folder + "/lotro_ru.exe");
  87. if (!QFile::copy(QApplication::applicationDirPath() + "/LotroLauncher.exe", game_folder + "/lotro_ru.exe")) {
  88. qDebug() << __FUNCTION__ << "Starting game FAILED - cannot copy LotroLauncher to lotro_ru.exe!!";
  89. emit errorOccured("startGame", {}, "LauncherCopyFailed");
  90. emit operationFinished("startGame", {}, false);
  91. return;
  92. }
  93. QFile::remove(game_folder + "/LotroLauncher.exe");
  94. if (!QFile::copy(QApplication::applicationDirPath() + "/Launcher.exe", game_folder + "/LotroLauncher.exe")) {
  95. qDebug() << __FUNCTION__ << "Starting game FAILED - cannot copy GameLauncher to LotroLauncher!!";
  96. emit errorOccured("startGame", {}, "NoAccessToGameLauncher");
  97. emit operationFinished("startGame", {}, false);
  98. return;
  99. }
  100. QFile file(game_folder + "/legacy_path.txt");
  101. file.open(QIODevice::WriteOnly);
  102. QTextStream out(&file);
  103. out << QApplication::applicationDirPath() + "/LegacyLauncher.exe";
  104. file.close();
  105. } else {
  106. QFile::remove(game_folder + "/LotroLauncher.exe");
  107. if (!QFile::copy(QApplication::applicationDirPath() + "/LotroLauncher.exe", game_folder + "/LotroLauncher.exe")) {
  108. qDebug() << __FUNCTION__ << "Starting game FAILED - cannot copy LotroLauncher from working dir to LotroLauncher in lotro dir!!";
  109. emit errorOccured("startGame", {}, "NoAccessToGameLauncher");
  110. emit operationFinished("startGame", {}, false);
  111. return;
  112. }
  113. }
  114. QStringList args;
  115. if (freeze_updates) {
  116. args << "gamelaunch" << "-disablePatch";
  117. }
  118. if (Settings::getValue("Lotro/skip_raw_download").toBool()) {
  119. args << "-skiprawdownload";
  120. }
  121. if (Settings::getValue("Lotro/no_splash_screen").toBool()) {
  122. args << "-nosplashscreen";
  123. }
  124. client_general_file_.Deinit();
  125. client_local_file_.Deinit();
  126. QString username = Settings::getValue("Account/username").toString();
  127. QString password = Settings::getValue("Account/password").toString();
  128. if (!username.isEmpty() && !password.isEmpty()) {
  129. args << "-username" << username << "-password" << password;
  130. }
  131. qDebug() << __FUNCTION__ << "Starting game with arguments: " << args;
  132. QFile f(Settings::getValue("Lotro/game_path").toString() + "/LotroLauncher.exe");
  133. QProcess process;
  134. if (FileSystem::fileExists(f.fileName())) {
  135. if (f.fileName().contains(" ")) {
  136. f.setFileName("\"" + f.fileName() + "\"");
  137. }
  138. process.startDetached(f.fileName(), args);
  139. process.waitForFinished(-1);
  140. QApplication::quit();
  141. }
  142. emit operationFinished("startGame");
  143. }
  144. void LotroDatManager::installPatch(QString patch_name, QString database_path, RESOURCE_FILE_TYPE dat_file) {
  145. emit operationStarted("installPatch", {patch_name, database_path});
  146. LOTRO_DAT::Database db;
  147. if (!db.InitDatabase(database_path.toStdString())) {
  148. qDebug() << __FUNCTION__ << "Error database " << database_path << ": failed to open!";
  149. return;
  150. }
  151. size_t patched_files_num = 0;
  152. LOTRO_DAT::SubfileData file;
  153. int i = 0;
  154. const int total_files = db.CountRows();
  155. qDebug() << "Patching all files from database...";
  156. while (!(file = db.GetNextFile()).Empty()) {
  157. if (i * 100 / total_files != (i - 1) * 100 / total_files) {
  158. qDebug() << "Completed " << i * 100 / total_files << "%";
  159. }
  160. ++i;
  161. if (!file.options["fid"]) {
  162. continue;
  163. }
  164. if (dat_file == E_CLIENT_LOCAL) {
  165. client_local_file_.PatchFile(file);
  166. } else if (dat_file == E_CLIENT_GENERAL) {
  167. client_general_file_.PatchFile(file);
  168. }
  169. ++patched_files_num;
  170. }
  171. db.CloseDatabase();
  172. emit operationFinished("installPatch", {patch_name, database_path}, true);
  173. }
  174. void LotroDatManager::createBackup()
  175. {
  176. // emit operationStarted("createBackup");
  177. // QString locale_prefix = Settings::getValue("Lotro/original_locale").toString();
  178. // QString client_local_backup_path = QApplication::applicationDirPath() + Settings::getValue("Backup/path").toString() + "client_local_" + locale_prefix + ".dat";
  179. // QString client_general_backup_path = QApplication::applicationDirPath() + Settings::getValue("Backup/path").toString() + "client_general.dat";
  180. // auto loc_res = client_local_file_.GetBackupManager().CreateBackup(client_local_backup_path.toStdString());
  181. // auto gen_res = client_general_file_.GetBackupManager().CreateBackup(client_general_backup_path.toStdString());
  182. // bool operations_result = (loc_res.result == LOTRO_DAT::SUCCESS && gen_res.result == LOTRO_DAT::SUCCESS);
  183. // emit operationFinished("createBackup", {client_local_backup_path, client_general_backup_path}, operations_result);
  184. }
  185. void LotroDatManager::restoreFromBackup()
  186. {
  187. // emit operationStarted("restoreFromBackup");
  188. // QString locale_prefix = Settings::getValue("Lotro/original_locale").toString();
  189. // QString client_local_backup_path = QApplication::applicationDirPath() + Settings::getValue("Backup/path").toString() + "client_local_" + locale_prefix + ".dat";
  190. // QString client_general_backup_path = QApplication::applicationDirPath() + Settings::getValue("Backup/path").toString() + "client_general.dat";
  191. // auto loc_res = client_local_file_.GetBackupManager().RestoreFromBackup(client_local_backup_path.toStdString());
  192. // auto gen_res = client_general_file_.GetBackupManager().RestoreFromBackup(client_general_backup_path.toStdString());
  193. // bool operations_result = (loc_res.result == LOTRO_DAT::SUCCESS && gen_res.result == LOTRO_DAT::SUCCESS);
  194. // emit operationFinished("restoreFromBackup", {}, operations_result);
  195. }
  196. void LotroDatManager::removeBackup()
  197. {
  198. // emit operationStarted("removeBackup");
  199. // QString locale_prefix = Settings::getValue("Lotro/original_locale").toString();
  200. // QString client_local_backup_path = QApplication::applicationDirPath() + Settings::getValue("Backup/path").toString() + "client_local_" + locale_prefix + ".dat";
  201. // QString client_general_backup_path = QApplication::applicationDirPath() + Settings::getValue("Backup/path").toString() + "client_general.dat";
  202. // auto loc_res = client_local_file_.GetBackupManager().RemoveBackup(client_local_backup_path.toStdString());
  203. // auto gen_res = client_general_file_.GetBackupManager().RemoveBackup(client_general_backup_path.toStdString());
  204. // bool operations_result = (loc_res.result == LOTRO_DAT::SUCCESS && gen_res.result == LOTRO_DAT::SUCCESS);
  205. // emit operationFinished("removeBackup", {}, operations_result);
  206. }