lotrodatmanager.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  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. Q_DECLARE_METATYPE(LotroDatManager::Status)
  9. void debug::ClientLocalDatStatusChangedHandler(DatStatus::ProgressInfo info) {
  10. if (info.status == DatStatus::DAT_STATUS::E_FREE) {
  11. std::cout << "CLIENT_LOCAL: operation finished" << std::endl;
  12. return;
  13. }
  14. if (info.status == DatStatus::DAT_STATUS::E_BACKUP_CREATING) {
  15. std::cout << "CLIENT_LOCAL: creating backup " << info.percentage << "% "
  16. << "(" << info.finished_parts << "/" << info.total_parts << ")" << std::endl;
  17. return;
  18. }
  19. if (info.status == DatStatus::DAT_STATUS::E_BACKUP_REMOVING) {
  20. std::cout << "CLIENT_LOCAL: removing backup " << info.percentage << "% "
  21. << "(" << info.finished_parts << "/" << info.total_parts << ")" << std::endl;
  22. return;
  23. }
  24. if (info.status == DatStatus::DAT_STATUS::E_BACKUP_RESTORING) {
  25. std::cout << "CLIENT_LOCAL: restoring backup " << info.percentage << "% "
  26. << "(" << info.finished_parts << "/" << info.total_parts << ")" << std::endl;
  27. return;
  28. }
  29. if (info.status == DatStatus::DAT_STATUS::E_COMMITING) {
  30. std::cout << "CLIENT_LOCAL: applying locales " << info.percentage << "% "
  31. << "(" << info.finished_parts << "/" << info.total_parts << ")" << std::endl;
  32. return;
  33. }
  34. if (info.status == DatStatus::DAT_STATUS::E_EXTRACTING) {
  35. std::cout << "CLIENT_LOCAL: extracting data " << info.percentage << "% "
  36. << "(" << info.finished_parts << "/" << info.total_parts << ")" << std::endl;
  37. return;
  38. }
  39. if (info.status == DatStatus::DAT_STATUS::E_GATHERING_INFO) {
  40. std::cout << "CLIENT_LOCAL: gathering info " << info.percentage << "% "
  41. << "(" << info.finished_parts << "/" << info.total_parts << ")" << std::endl;
  42. return;
  43. }
  44. if (info.status == DatStatus::DAT_STATUS::E_INITIALISING) {
  45. std::cout << "CLIENT_LOCAL: initialising " << info.percentage << "% "
  46. << "(" << info.finished_parts << "/" << info.total_parts << ")" << std::endl;
  47. return;
  48. }
  49. if (info.status == DatStatus::DAT_STATUS::E_PATCHING) {
  50. std::cout << "CLIENT_LOCAL: applying patch " << info.percentage << "% "
  51. << "(" << info.finished_parts << "/" << info.total_parts << ")" << std::endl;
  52. return;
  53. }
  54. }
  55. void debug::ClientGeneralDatStatusChangedHandler(DatStatus::ProgressInfo info) {
  56. if (info.status == DatStatus::DAT_STATUS::E_FREE) {
  57. std::cout << "CLIENT_GENERAL: operation finished" << std::endl;
  58. return;
  59. }
  60. if (info.status == DatStatus::DAT_STATUS::E_BACKUP_CREATING) {
  61. std::cout << "CLIENT_GENERAL: creating backup " << info.percentage << "% "
  62. << "(" << info.finished_parts << "/" << info.total_parts << ")" << std::endl;
  63. return;
  64. }
  65. if (info.status == DatStatus::DAT_STATUS::E_BACKUP_REMOVING) {
  66. std::cout << "CLIENT_GENERAL: removing backup " << info.percentage << "% "
  67. << "(" << info.finished_parts << "/" << info.total_parts << ")" << std::endl;
  68. return;
  69. }
  70. if (info.status == DatStatus::DAT_STATUS::E_BACKUP_RESTORING) {
  71. std::cout << "CLIENT_GENERAL: restoring backup " << info.percentage << "% "
  72. << "(" << info.finished_parts << "/" << info.total_parts << ")" << std::endl;
  73. return;
  74. }
  75. if (info.status == DatStatus::DAT_STATUS::E_COMMITING) {
  76. std::cout << "CLIENT_GENERAL: applying locales " << info.percentage << "% "
  77. << "(" << info.finished_parts << "/" << info.total_parts << ")" << std::endl;
  78. return;
  79. }
  80. if (info.status == DatStatus::DAT_STATUS::E_EXTRACTING) {
  81. std::cout << "CLIENT_GENERAL: extracting data " << info.percentage << "% "
  82. << "(" << info.finished_parts << "/" << info.total_parts << ")" << std::endl;
  83. return;
  84. }
  85. if (info.status == DatStatus::DAT_STATUS::E_GATHERING_INFO) {
  86. std::cout << "CLIENT_GENERAL: gathering info " << info.percentage << "% "
  87. << "(" << info.finished_parts << "/" << info.total_parts << ")" << std::endl;
  88. return;
  89. }
  90. if (info.status == DatStatus::DAT_STATUS::E_INITIALISING) {
  91. std::cout << "CLIENT_GENERAL: initialising " << info.percentage << "% "
  92. << "(" << info.finished_parts << "/" << info.total_parts << ")" << std::endl;
  93. return;
  94. }
  95. if (info.status == DatStatus::DAT_STATUS::E_PATCHING) {
  96. std::cout << "CLIENT_GENERAL: applying patch " << info.percentage << "% "
  97. << "(" << info.finished_parts << "/" << info.total_parts << ")" << std::endl;
  98. return;
  99. }
  100. }
  101. LotroDatManager::LotroDatManager(QObject *parent) :
  102. QObject(parent) {
  103. qRegisterMetaType<Status>("Status");
  104. qRegisterMetaType<LOTRO_DAT::DatLocaleManager::LOCALE>("LOCALE");
  105. client_local_file_.GetStatusModule().AddStatusChangedCallbackFunction(client_local_status_updated_callback_);
  106. client_general_file_.GetStatusModule().AddStatusChangedCallbackFunction(client_general_status_updated_callback_);
  107. }
  108. LotroDatManager::~LotroDatManager()
  109. {
  110. deinitializeManager();
  111. }
  112. bool LotroDatManager::Initialised()
  113. {
  114. return client_general_file_.Initialized() && client_local_file_.Initialized();
  115. }
  116. bool LotroDatManager::NotPatched()
  117. {
  118. return !client_local_file_.GetStatusModule().CheckIfNotPatched() && !client_local_file_.GetStatusModule().CheckIfNotPatched();
  119. }
  120. void LotroDatManager::initializeManager()
  121. {
  122. emit operationStarted("initializeManager");
  123. qDebug() << __FUNCTION__ << "Starting initialisation of LotroDatManager";
  124. QString game_folder = Settings::getValue("Lotro/game_path").toString();
  125. QString locale_prefix = Settings::getValue("Lotro/original_locale").toString();
  126. QString client_local_filepath = game_folder + "/client_local_" + locale_prefix + ".dat";
  127. QString client_general_filepath = game_folder + "/client_general.dat";
  128. if (game_folder == "none") {
  129. qDebug() << __FUNCTION__ << "Finished initialisation LotroDatManager - error: .dat files not found!";
  130. emit errorOccured("initializeManager", {}, "FolderNotDefined");
  131. emit operationFinished("initializeManager", {}, false);
  132. return;
  133. }
  134. if (!FileSystem::fileExists(client_local_filepath) || !FileSystem::fileExists(client_general_filepath)) {
  135. emit errorOccured("initializeManager", {}, "DatFilesNotFound");
  136. emit operationFinished("initializeManager", {}, false);
  137. return;
  138. }
  139. // Updating file permissions to be sure, that they're not in read-only mode
  140. if (!QFile::setPermissions(client_local_filepath, QFileDevice::Permission(0x6666))) {
  141. qDebug() << __FUNCTION__ << "Unable to update permissions on client_local_* file!";
  142. }
  143. if (!QFile::setPermissions(client_general_filepath, QFileDevice::Permission(0x6666))) {
  144. qDebug() << __FUNCTION__ << "Unable to update permissions on client_general* file!";
  145. }
  146. // Initialising client_local_*.dat file and client_general.dat
  147. auto client_local_init_res = client_local_file_.Initialise(client_local_filepath.toStdString(), 0);
  148. auto client_general_init_res = client_general_file_.Initialise(client_general_filepath.toStdString(), 1);
  149. if (client_local_init_res.result != LOTRO_DAT::SUCCESS
  150. || client_general_init_res.result != LOTRO_DAT::SUCCESS)
  151. {
  152. client_local_file_.Deinitialize();
  153. client_general_file_.Deinitialize();
  154. qDebug() << __FUNCTION__ << "Finished LotroDatManager initialisation - error: DatFile initialisation error!";
  155. emit errorOccured("initializeManager", {}, "DatInitError");
  156. emit operationFinished("initializeManager", {}, false);
  157. return;
  158. }
  159. qDebug() << "LotroDatManager initialisation successfull! Dat files: "
  160. << QString::fromStdString(client_general_file_.GetIO().GetFilename().value)
  161. << QString::fromStdString(client_local_file_.GetIO().GetFilename().value);
  162. emit operationFinished("initializeManager", {}, true);
  163. }
  164. void LotroDatManager::deinitializeManager()
  165. {
  166. emit operationStarted("deinitializeManager");
  167. client_local_file_.Deinitialize();
  168. client_general_file_.Deinitialize();
  169. emit operationFinished("deinitializeManager");
  170. }
  171. void LotroDatManager::startGame(LOTRO_DAT::DatLocaleManager::LOCALE locale)
  172. {
  173. emit operationStarted("startGame", {locale});
  174. client_general_file_.GetLocaleManager().SetLocale(locale);
  175. client_local_file_.GetLocaleManager().SetLocale(locale);
  176. QString game_folder = Settings::getValue("Lotro/game_path").toString();
  177. if (game_folder == "none") {
  178. qDebug() << __FUNCTION__ << "Starting game FAILED - game folder wasnt set!";
  179. emit errorOccured("startGame", {locale}, "GameFolderNotSet");
  180. emit operationFinished("startGame", {locale}, false);
  181. return;
  182. }
  183. if (!FileSystem::fileExists(QApplication::applicationDirPath() + "/Launcher.exe")) {
  184. qDebug() << __FUNCTION__ << "Starting game FAILED - no game launcher in legacy directory found!";
  185. emit errorOccured("startGame", {locale}, "NoGameLauncherInLegacyDir");
  186. emit operationFinished("startGame", {locale}, false);
  187. return;
  188. }
  189. if (locale == LOTRO_DAT::DatLocaleManager::PATCHED) {
  190. QFile::remove(game_folder + "/lotro_ru.exe");
  191. if (!QFile::copy(QApplication::applicationDirPath() + "/LotroLauncher.exe", game_folder + "/lotro_ru.exe")) {
  192. qDebug() << __FUNCTION__ << "Starting game FAILED - cannot copy LotroLauncher to lotro_ru.exe!!";
  193. emit errorOccured("startGame", {locale}, "LauncherCopyFailed");
  194. emit operationFinished("startGame", {locale}, false);
  195. return;
  196. }
  197. QFile::remove(game_folder + "/LotroLauncher.exe");
  198. if (!QFile::copy(QApplication::applicationDirPath() + "/Launcher.exe", game_folder + "/LotroLauncher.exe")) {
  199. qDebug() << __FUNCTION__ << "Starting game FAILED - cannot copy GameLauncher to LotroLauncher!!";
  200. emit errorOccured("startGame", {locale}, "NoAccessToGameLauncher");
  201. emit operationFinished("startGame", {locale}, false);
  202. return;
  203. }
  204. QFile file(game_folder + "/legacy_path.txt");
  205. file.open(QIODevice::WriteOnly);
  206. QTextStream out(&file);
  207. out << QApplication::applicationDirPath() + "/LegacyLauncher.exe";
  208. file.close();
  209. } else {
  210. QFile::remove(game_folder + "/LotroLauncher.exe");
  211. if (!QFile::copy(QApplication::applicationDirPath() + "/LotroLauncher.exe", game_folder + "/LotroLauncher.exe")) {
  212. qDebug() << __FUNCTION__ << "Starting game FAILED - cannot copy LotroLauncher from working dir to LotroLauncher in lotro dir!!";
  213. emit errorOccured("startGame", {locale}, "NoAccessToGameLauncher");
  214. emit operationFinished("startGame", {locale}, false);
  215. return;
  216. }
  217. }
  218. if (!startLotroLauncherWithParameters(locale)) {
  219. emit errorOccured("startGame", {locale}, "StartLotroLauncherWithParametersFailed");
  220. emit operationFinished("startGame", {locale}, false);
  221. return;
  222. }
  223. emit operationFinished("startGame", {locale}, true);
  224. }
  225. void LotroDatManager::installPatch(QString patch_name, QString database_path)
  226. {
  227. emit operationStarted("installPatch", {patch_name, database_path});
  228. LOTRO_DAT::Database db;
  229. if (!db.InitDatabase(database_path.toStdString())) {
  230. emit errorOccured("installPatch", {patch_name, database_path}, "ErrorInitDatabase");
  231. emit operationFinished("installPatch", {patch_name, database_path}, false);
  232. return;
  233. }
  234. if (client_local_file_.GetPatcher().PatchAllDatabase(&db).result != LOTRO_DAT::SUCCESS) {
  235. db.CloseDatabase();
  236. emit errorOccured("installPatch", {patch_name, database_path}, "ErrorPatchClientLocal");
  237. emit operationFinished("installPatch", {patch_name, database_path}, false);
  238. return;
  239. }
  240. if (client_general_file_.GetPatcher().PatchAllDatabase(&db).result != LOTRO_DAT::SUCCESS) {
  241. db.CloseDatabase();
  242. emit errorOccured("installPatch", {patch_name, database_path});
  243. emit operationFinished("installPatch", {patch_name, database_path}, false);
  244. return;
  245. }
  246. db.CloseDatabase();
  247. emit operationFinished("installPatch", {patch_name, database_path}, true);
  248. }
  249. void LotroDatManager::enableCategory(QString patch_name, LotroDatManager::Category category)
  250. {
  251. emit operationStarted("enableCategory", {patch_name, category});
  252. if (client_local_file_.GetLocaleManager().EnableCategory(category).result != LOTRO_DAT::SUCCESS) {
  253. emit errorOccured("enableCategory", {patch_name, category}, "ErrorEnableCategoryClientLocal");
  254. emit operationFinished("enableCategory", {patch_name, category}, false);
  255. return;
  256. }
  257. client_local_file_.GetFileSystem().CommitDirectories();
  258. client_local_file_.GetLocaleManager().CommitLocales();
  259. if (client_general_file_.GetLocaleManager().EnableCategory(category).result != LOTRO_DAT::SUCCESS) {
  260. emit errorOccured("enableCategory", {patch_name, category}, "ErrorEnableCategoryClientGeneral");
  261. emit operationFinished("enableCategory", {patch_name, category}, false);
  262. return;
  263. }
  264. client_general_file_.GetFileSystem().CommitDirectories();
  265. client_general_file_.GetLocaleManager().CommitLocales();
  266. emit operationFinished("enableCategory", {patch_name, category});
  267. }
  268. void LotroDatManager::disableCategory(QString patch_name, LotroDatManager::Category category)
  269. {
  270. emit operationStarted("disableCategory", {patch_name, category});
  271. if (client_local_file_.GetLocaleManager().DisableCategory(category).result != LOTRO_DAT::SUCCESS) {
  272. emit errorOccured("disableCategory", {patch_name, category}, "ErrorDisableCategoryClientLocal");
  273. emit operationFinished("disableCategory", {patch_name, category}, false);
  274. return;
  275. }
  276. if (client_general_file_.GetLocaleManager().DisableCategory(category).result != LOTRO_DAT::SUCCESS) {
  277. emit errorOccured("disableCategory", {patch_name, category}, "ErrorDisableCategoryClientGeneral");
  278. emit operationFinished("disableCategory", {patch_name, category}, false);
  279. return;
  280. }
  281. emit operationFinished("disableCategory", {patch_name, category}, true);
  282. }
  283. void LotroDatManager::createBackup()
  284. {
  285. // TODO: Error handling
  286. emit operationStarted("createBackup");
  287. QString locale_prefix = Settings::getValue("Lotro/original_locale").toString();
  288. client_local_file_.GetBackupManager().CreateBackup((QApplication::applicationDirPath() + "/backup/client_local_" + locale_prefix + ".dat").toStdString());
  289. client_general_file_.GetBackupManager().CreateBackup((QApplication::applicationDirPath() + "/backup/client_general.dat").toStdString());
  290. emit operationFinished("createBackup");
  291. }
  292. void LotroDatManager::restoreFromBackup()
  293. {
  294. // TODO: Error handling
  295. emit operationStarted("restoreFromBackup");
  296. QString locale_prefix = Settings::getValue("Lotro/original_locale").toString();
  297. client_local_file_.GetBackupManager().RestoreFromBackup((QApplication::applicationDirPath() + "/backup/client_local_" + locale_prefix + ".dat").toStdString());
  298. client_general_file_.GetBackupManager().RestoreFromBackup((QApplication::applicationDirPath() + "/backup/client_general.dat").toStdString());
  299. emit operationFinished("restoreFromBackup");
  300. }
  301. void LotroDatManager::removeBackup()
  302. {
  303. // TODO: Error handling
  304. emit operationStarted("removeBackup");
  305. QString locale_prefix = Settings::getValue("Lotro/original_locale").toString();
  306. client_local_file_.GetBackupManager().RemoveBackup((QApplication::applicationDirPath() + "/backup/client_local_" + locale_prefix + ".dat").toStdString());
  307. client_general_file_.GetBackupManager().RemoveBackup((QApplication::applicationDirPath() + "/backup/client_general.dat").toStdString());
  308. emit operationFinished("removeBackup");
  309. }
  310. bool LotroDatManager::startLotroLauncherWithParameters(LOTRO_DAT::DatLocaleManager::LOCALE locale)
  311. {
  312. QStringList args;
  313. if (Settings::getValue("Lotro/skip_raw_download").toString() == "True") {
  314. args << "-skiprawdownload";
  315. }
  316. if (Settings::getValue("Lotro/no_splash_screen").toString() == "True") {
  317. args << "-nosplashscreen";
  318. }
  319. if (locale == LOTRO_DAT::DatLocaleManager::PATCHED) {
  320. args << "gamelaunch" << "-disablePatch";
  321. }
  322. client_general_file_.Deinitialize();
  323. client_local_file_.Deinitialize();
  324. QString username = Settings::getValue("Account/username").toString();
  325. QString password = Settings::getValue("Account/password").toString();
  326. if (!username.isEmpty() && !password.isEmpty()) {
  327. args << "-username" << username << "-password" << password;
  328. }
  329. qDebug() << __FUNCTION__ << "Starting game with arguments: " << args;
  330. QFile f(Settings::getValue("Lotro/game_path").toString() + "/LotroLauncher.exe");
  331. QProcess process;
  332. if (FileSystem::fileExists(f.fileName())) {
  333. if (f.fileName().contains(" ")) {
  334. f.setFileName("\"" + f.fileName() + "\"");
  335. }
  336. process.startDetached(f.fileName(), args);
  337. process.waitForFinished(-1);
  338. QApplication::quit();
  339. return true;
  340. } else {
  341. return false;
  342. }
  343. }