lotrodatmanager.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. #include "lotrodatmanager.h"
  2. #include "models/filesystem.h"
  3. #include "models/settings.h"
  4. #include <QDebug>
  5. #include <QProcess>
  6. Q_DECLARE_METATYPE(LOTRO_DAT::DatLocaleManager::LOCALE)
  7. Q_DECLARE_METATYPE(LotroDatManager::Category)
  8. LotroDatManager::LotroDatManager(QObject *parent) :
  9. QObject(parent) {
  10. qRegisterMetaType<LOTRO_DAT::DatLocaleManager::LOCALE>();
  11. client_local_file_.GetStatusModule().AddStatusChangedCallbackFunction(file_status_updated_callback_);
  12. }
  13. LotroDatManager::~LotroDatManager()
  14. {
  15. deinitializeManager();
  16. }
  17. bool LotroDatManager::Initialised()
  18. {
  19. return client_general_file_.Initialized() && client_local_file_.Initialized();
  20. }
  21. bool LotroDatManager::NotPatched()
  22. {
  23. return !client_local_file_.GetStatusModule().CheckIfNotPatched() && !client_local_file_.GetStatusModule().CheckIfNotPatched();
  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 (game_folder == "none") {
  34. qDebug() << __FUNCTION__ << "Finished initialisation LotroDatManager - error: .dat files not found!";
  35. emit errorOccured("initializeManager", {}, "FolderNotDefined");
  36. emit operationFinished("initializeManager", {}, false);
  37. return;
  38. }
  39. if (!FileSystem::fileExists(client_local_filepath) || !FileSystem::fileExists(client_general_filepath)) {
  40. emit errorOccured("initializeManager", {}, "DatFilesNotFound");
  41. emit operationFinished("initializeManager", {}, false);
  42. return;
  43. }
  44. // Updating file permissions to be sure, that they're not in read-only mode
  45. if (!QFile::setPermissions(client_local_filepath, QFileDevice::Permission(0x6666))) {
  46. qDebug() << __FUNCTION__ << "Unable to update permissions on client_local_* file!";
  47. }
  48. if (!QFile::setPermissions(client_general_filepath, QFileDevice::Permission(0x6666))) {
  49. qDebug() << __FUNCTION__ << "Unable to update permissions on client_general* file!";
  50. }
  51. // Initialising client_local_*.dat file and client_general.dat
  52. auto client_local_init_res = client_local_file_.Initialise(client_local_filepath.toStdString(), 0);
  53. auto client_general_init_res = client_general_file_.Initialise(client_general_filepath.toStdString(), 1);
  54. if (client_local_init_res.result != LOTRO_DAT::SUCCESS
  55. || client_general_init_res.result != LOTRO_DAT::SUCCESS)
  56. {
  57. client_local_file_.Deinitialize();
  58. client_general_file_.Deinitialize();
  59. qDebug() << __FUNCTION__ << "Finished LotroDatManager initialisation - error: DatFile initialisation error!";
  60. emit errorOccured("initializeManager", {}, "DatInitError");
  61. emit operationFinished("initializeManager", {}, false);
  62. return;
  63. }
  64. emit operationFinished("initializeManager", {}, true);
  65. }
  66. void LotroDatManager::deinitializeManager()
  67. {
  68. emit operationStarted("deinitializeManager");
  69. client_local_file_.Deinitialize();
  70. client_general_file_.Deinitialize();
  71. emit operationFinished("deinitializeManager");
  72. }
  73. void LotroDatManager::startGame(LOTRO_DAT::DatLocaleManager::LOCALE locale)
  74. {
  75. emit operationStarted("startGame", {locale});
  76. client_general_file_.GetLocaleManager().SetLocale(locale);
  77. client_local_file_.GetLocaleManager().SetLocale(locale);
  78. QString game_folder = Settings::getValue("Lotro/game_path").toString();
  79. if (game_folder == "none") {
  80. qDebug() << __FUNCTION__ << "Starting game FAILED - game folder wasnt set!";
  81. emit errorOccured("startGame", {locale}, "GameFolderNotSet");
  82. emit operationFinished("startGame", {locale}, false);
  83. return;
  84. }
  85. if (!FileSystem::fileExists(QApplication::applicationDirPath() + "/Launcher.exe")) {
  86. qDebug() << __FUNCTION__ << "Starting game FAILED - no game launcher in legacy directory found!";
  87. emit errorOccured("startGame", {locale}, "NoGameLauncherInLegacyDir");
  88. emit operationFinished("startGame", {locale}, false);
  89. return;
  90. }
  91. if (locale == LOTRO_DAT::DatLocaleManager::PATCHED) {
  92. QFile::remove(game_folder + "/lotro_ru.exe");
  93. if (!QFile::copy(QApplication::applicationDirPath() + "/LotroLauncher.exe", game_folder + "/lotro_ru.exe")) {
  94. qDebug() << __FUNCTION__ << "Starting game FAILED - cannot copy LotroLauncher to lotro_ru.exe!!";
  95. emit errorOccured("startGame", {locale}, "LauncherCopyFailed");
  96. emit operationFinished("startGame", {locale}, false);
  97. return;
  98. }
  99. QFile::remove(game_folder + "/LotroLauncher.exe");
  100. if (!QFile::copy(QApplication::applicationDirPath() + "/Launcher.exe", game_folder + "/LotroLauncher.exe")) {
  101. qDebug() << __FUNCTION__ << "Starting game FAILED - cannot copy GameLauncher to LotroLauncher!!";
  102. emit errorOccured("startGame", {locale}, "NoAccessToGameLauncher");
  103. emit operationFinished("startGame", {locale}, false);
  104. return;
  105. }
  106. QFile file(game_folder + "/legacy_path.txt");
  107. file.open(QIODevice::WriteOnly);
  108. QTextStream out(&file);
  109. out << QApplication::applicationDirPath() + "/LegacyLauncher.exe";
  110. file.close();
  111. } else {
  112. QFile::remove(game_folder + "/LotroLauncher.exe");
  113. if (!QFile::copy(QApplication::applicationDirPath() + "/LotroLauncher.exe", game_folder + "/LotroLauncher.exe")) {
  114. qDebug() << __FUNCTION__ << "Starting game FAILED - cannot copy LotroLauncher from working dir to LotroLauncher in lotro dir!!";
  115. emit errorOccured("startGame", {locale}, "NoAccessToGameLauncher");
  116. emit operationFinished("startGame", {locale}, false);
  117. return;
  118. }
  119. }
  120. if (!startLotroLauncherWithParameters(locale)) {
  121. emit errorOccured("startGame", {locale}, "StartLotroLauncherWithParametersFailed");
  122. emit operationFinished("startGame", {locale}, false);
  123. return;
  124. }
  125. emit operationFinished("startGame", {locale}, true);
  126. }
  127. void LotroDatManager::installPatch(QString patch_name, QString database_path)
  128. {
  129. emit operationStarted("installPatch", {patch_name, database_path});
  130. LOTRO_DAT::Database db;
  131. if (!db.InitDatabase(database_path.toStdString())) {
  132. emit errorOccured("installPatch", {patch_name, database_path}, "ErrorInitDatabase");
  133. emit operationFinished("installPatch", {patch_name, database_path}, false);
  134. return;
  135. }
  136. if (client_local_file_.GetPatcher().PatchAllDatabase(&db).result != LOTRO_DAT::SUCCESS) {
  137. db.CloseDatabase();
  138. emit errorOccured("installPatch", {patch_name, database_path}, "ErrorPatchClientLocal");
  139. emit operationFinished("installPatch", {patch_name, database_path}, false);
  140. return;
  141. }
  142. if (client_general_file_.GetPatcher().PatchAllDatabase(&db).result != LOTRO_DAT::SUCCESS) {
  143. db.CloseDatabase();
  144. emit errorOccured("installPatch", {patch_name, database_path});
  145. emit operationFinished("installPatch", {patch_name, database_path}, false);
  146. return;
  147. }
  148. db.CloseDatabase();
  149. emit operationFinished("installPatch", {patch_name, database_path}, true);
  150. }
  151. void LotroDatManager::enableCategory(QString patch_name, LotroDatManager::Category category)
  152. {
  153. emit operationStarted("enableCategory", {patch_name, category});
  154. if (client_local_file_.GetLocaleManager().EnableCategory(category).result != LOTRO_DAT::SUCCESS) {
  155. emit errorOccured("enableCategory", {patch_name, category}, "ErrorEnableCategoryClientLocal");
  156. emit operationFinished("enableCategory", {patch_name, category}, false);
  157. return;
  158. }
  159. if (client_general_file_.GetLocaleManager().EnableCategory(category).result != LOTRO_DAT::SUCCESS) {
  160. emit errorOccured("enableCategory", {patch_name, category}, "ErrorEnableCategoryClientGeneral");
  161. emit operationFinished("enableCategory", {patch_name, category}, false);
  162. return;
  163. }
  164. emit operationFinished("enableCategory", {patch_name, category});
  165. }
  166. void LotroDatManager::disableCategory(QString patch_name, LotroDatManager::Category category)
  167. {
  168. emit operationStarted("disableCategory", {patch_name, category});
  169. if (client_local_file_.GetLocaleManager().DisableCategory(category).result != LOTRO_DAT::SUCCESS) {
  170. emit errorOccured("disableCategory", {patch_name, category}, "ErrorDisableCategoryClientLocal");
  171. emit operationFinished("disableCategory", {patch_name, category}, false);
  172. return;
  173. }
  174. if (client_general_file_.GetLocaleManager().DisableCategory(category).result != LOTRO_DAT::SUCCESS) {
  175. emit errorOccured("disableCategory", {patch_name, category}, "ErrorDisableCategoryClientGeneral");
  176. emit operationFinished("disableCategory", {patch_name, category}, false);
  177. return;
  178. }
  179. emit operationFinished("disableCategory", {patch_name, category}, true);
  180. }
  181. void LotroDatManager::createBackup()
  182. {
  183. // TODO: Error handling
  184. emit operationStarted("createBackup");
  185. QString locale_prefix = Settings::getValue("Lotro/original_locale").toString();
  186. client_local_file_.GetBackupManager().CreateBackup((QApplication::applicationDirPath() + "/backup/client_local_" + locale_prefix + ".dat").toStdString());
  187. client_general_file_.GetBackupManager().CreateBackup((QApplication::applicationDirPath() + "/backup/client_general.dat").toStdString());
  188. emit operationFinished("createBackup");
  189. }
  190. void LotroDatManager::restoreFromBackup()
  191. {
  192. // TODO: Error handling
  193. emit operationStarted("restoreFromBackup");
  194. QString locale_prefix = Settings::getValue("Lotro/original_locale").toString();
  195. client_local_file_.GetBackupManager().RestoreFromBackup((QApplication::applicationDirPath() + "/backup/client_local_" + locale_prefix + ".dat").toStdString());
  196. client_general_file_.GetBackupManager().RestoreFromBackup((QApplication::applicationDirPath() + "/backup/client_general.dat").toStdString());
  197. emit operationFinished("restoreFromBackup");
  198. }
  199. void LotroDatManager::removeBackup()
  200. {
  201. // TODO: Error handling
  202. emit operationStarted("removeBackup");
  203. QString locale_prefix = Settings::getValue("Lotro/original_locale").toString();
  204. client_local_file_.GetBackupManager().RemoveBackup((QApplication::applicationDirPath() + "/backup/client_local_" + locale_prefix + ".dat").toStdString());
  205. client_general_file_.GetBackupManager().RemoveBackup((QApplication::applicationDirPath() + "/backup/client_general.dat").toStdString());
  206. emit operationFinished("removeBackup");
  207. }
  208. bool LotroDatManager::startLotroLauncherWithParameters(LOTRO_DAT::DatLocaleManager::LOCALE locale)
  209. {
  210. QStringList args;
  211. if (Settings::getValue("Lotro/skip_raw_download").toString() == "True") {
  212. args << "-skiprawdownload";
  213. }
  214. if (Settings::getValue("Lotro/no_splash_screen").toString() == "True") {
  215. args << "-nosplashscreen";
  216. }
  217. if (locale == LOTRO_DAT::DatLocaleManager::PATCHED) {
  218. args << "gamelaunch" << "-disablePatch";
  219. }
  220. client_general_file_.Deinitialize();
  221. client_local_file_.Deinitialize();
  222. QString username = Settings::getValue("Account/username").toString();
  223. QString password = Settings::getValue("Account/password").toString();
  224. if (!username.isEmpty() && !password.isEmpty()) {
  225. args << "-username" << username << "-password" << password;
  226. }
  227. qDebug() << __FUNCTION__ << "Starting game with arguments: " << args;
  228. QFile f(Settings::getValue("Lotro/game_path").toString() + "/LotroLauncher.exe");
  229. QProcess process;
  230. if (FileSystem::fileExists(f.fileName())) {
  231. if (f.fileName().contains(" ")) {
  232. f.setFileName("\"" + f.fileName() + "\"");
  233. }
  234. process.startDetached(f.fileName(), args);
  235. process.waitForFinished(-1);
  236. QApplication::quit();
  237. return true;
  238. } else {
  239. return false;
  240. }
  241. }