patchinstaller.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. #include "patchinstaller.h"
  2. #include "models/filesystem.h"
  3. #include "models/downloader.h"
  4. #include "models/settings.h"
  5. #include <QDebug>
  6. #include <QProcess>
  7. QString getComponentNameFromId(int id) {
  8. switch (id) {
  9. case 100: return "texts_main";
  10. case 101: return "texts_items";
  11. case 102: return "texts_emotes";
  12. case 200: return "maps";
  13. case 201: return "loadscreens";
  14. case 202: return "textures";
  15. case 300: return "sounds";
  16. case 301: return "videos";
  17. }
  18. return "none";
  19. }
  20. PatchInstaller::PatchInstaller(QObject *parent) : QObject(parent) {
  21. }
  22. bool PatchInstaller::initialised() {
  23. return client_general_file_->Initialized() && client_local_file_->Initialized();
  24. }
  25. PatchInstaller::~PatchInstaller() {
  26. deinit();
  27. }
  28. // ############## PRIVATE ############## //
  29. bool PatchInstaller::datPathIsRelevant() {
  30. QString game_folder = Settings::getValue("Lotro/game_path").toString();
  31. QString locale_prefix = Settings::getValue("Lotro/original_locale").toString();
  32. QString client_local_filepath = game_folder + "/client_local_" + locale_prefix + ".dat";
  33. QString client_general_filepath = game_folder + "/client_general.dat";
  34. QString client_local_current_path = QString::fromStdString(client_local_file_->GetFilename());
  35. QString client_general_current_path = QString::fromStdString(client_general_file_->GetFilename());
  36. return QFileInfo(client_local_filepath) != QFileInfo(client_local_current_path)
  37. || QFileInfo(client_general_filepath) != QFileInfo(client_general_current_path);
  38. }
  39. void PatchInstaller::deinit() {
  40. emit operationStarted("deinitializeManager");
  41. client_local_file_->Deinit();
  42. client_general_file_->Deinit();
  43. emit operationFinished("deinitializeManager");
  44. }
  45. void PatchInstaller::installPatch(QString patch_name, LOTRO_DAT::Database& database) {
  46. if (patch_name == "loadscreen") {
  47. installLoadscreens(database);
  48. return;
  49. }
  50. if (patch_name == "video") {
  51. installVideos(database);
  52. return;
  53. }
  54. if (patch_name == "micro" && !Settings::getValue("Components/micropatch").toBool()) {
  55. Settings::setValue("DatabaseNeedInstall/micropatch", false);
  56. return;
  57. }
  58. size_t patched_files_num = 0;
  59. LOTRO_DAT::SubfileData file;
  60. int i = 0;
  61. const int total_files = database.CountRows();
  62. qDebug() << "Patching all files from database...";
  63. while (!(file = database.GetNextFile()).Empty()) {
  64. if (i * 100 / total_files != (i - 1) * 100 / total_files) {
  65. qDebug() << "Completed " << i * 100 / total_files << "%";
  66. }
  67. ++i;
  68. if (!file.options["fid"]) {
  69. continue;
  70. }
  71. const int category = file.options["cat"] ? file.options["cat"].as<int>() : -1;
  72. QString component_name = getComponentNameFromId(category);
  73. if (!Settings::getValue("Components/" + component_name).toBool()) {
  74. continue;
  75. }
  76. const int dat_id = file.options["fid"].as<int>();
  77. if (dat_id == E_CLIENT_LOCAL) {
  78. client_local_file_->PatchFile(file);
  79. } else if (dat_id == E_CLIENT_GENERAL) {
  80. client_general_file_->PatchFile(file);
  81. } else {
  82. qWarning() << "Unknown dat id parameter for file " << file.options["fid"].as<long long>() << " (dat id value = " << dat_id << "), SKIPPING!";
  83. }
  84. ++patched_files_num;
  85. }
  86. Settings::setValue("DatabaseNeedInstall/" + patch_name, false);
  87. return;
  88. }
  89. // TODO
  90. void PatchInstaller::installLoadscreens(LOTRO_DAT::Database& database) {
  91. if (!Settings::getValue("DatabaseNeedInstall/loadscreen").toBool() ||
  92. !Settings::getValue("Components/loadscreens").toBool())
  93. {
  94. Settings::setValue("DatabaseNeedInstall/loadscreen", false);
  95. return;
  96. }
  97. QString locale_prefix = Settings::getValue("Lotro/original_locale").toString();
  98. const QStringList loadscreens_filenames = {
  99. locale_prefix == "English" ? "lotro_ad_pregame.jpg" : "lotro_ad_pregame_" + locale_prefix + ".jpg",
  100. "lotro_generic_teleport_screen_01.jpg",
  101. "lotro_generic_teleport_screen_02.jpg",
  102. "lotro_generic_teleport_screen_03.jpg",
  103. "lotro_generic_teleport_screen_04.jpg",
  104. "lotro_generic_teleport_screen_05.jpg",
  105. "lotro_generic_teleport_screen_06.jpg",
  106. "lotro_generic_teleport_screen_07.jpg",
  107. "lotro_generic_teleport_screen_08.jpg",
  108. "lotro_generic_teleport_screen_09.jpg",
  109. "lotro_generic_teleport_screen_10.jpg"
  110. };
  111. LOTRO_DAT::SubfileData data;
  112. QString logo_path = Settings::getValue("Lotro/game_path").toString() + "/raw/" + (locale_prefix == "English" ? "en" : locale_prefix) + "/logo/";
  113. for (size_t i = 0; i < qMin(size_t(loadscreens_filenames.size()), database.CountRows()); ++i) {
  114. data = database.GetNextFile();
  115. QFile::remove(logo_path + loadscreens_filenames[i]);
  116. if (!data.binary_data.WriteToFile((logo_path + loadscreens_filenames[i]).toLocal8Bit())) {
  117. qWarning() << "InstallLoadscreens: Cannot write to file " << logo_path + loadscreens_filenames[i];
  118. }
  119. // progress.install_finished_parts++;
  120. // emit progressChanged(progress, this);
  121. }
  122. Settings::setValue("DatabaseNeedInstall/loadscreen", false);
  123. }
  124. // TODO
  125. void PatchInstaller::installVideos(LOTRO_DAT::Database& database) {
  126. }
  127. // ############## PUBLIC SLOTS ############## //
  128. void PatchInstaller::init()
  129. {
  130. emit operationStarted("initializeManager");
  131. qDebug() << __FUNCTION__ << "Starting initialisation of LotroDatManager";
  132. QString game_folder = Settings::getValue("Lotro/game_path").toString();
  133. QString locale_prefix = Settings::getValue("Lotro/original_locale").toString();
  134. QString client_local_filepath = game_folder + "/client_local_" + locale_prefix + ".dat";
  135. QString client_general_filepath = game_folder + "/client_general.dat";
  136. if (!FileSystem::fileExists(client_local_filepath) || !FileSystem::fileExists(client_general_filepath)) {
  137. emit errorOccured("initializeManager", {}, "DatFilesNotFound");
  138. emit operationFinished("initializeManager", {}, false);
  139. return;
  140. }
  141. // Updating file permissions to be sure, that they're not in read-only mode
  142. if (!QFile::setPermissions(client_local_filepath, QFileDevice::Permission(0x6666))) {
  143. qDebug() << __FUNCTION__ << "Unable to update permissions on client_local_* file!";
  144. }
  145. if (!QFile::setPermissions(client_general_filepath, QFileDevice::Permission(0x6666))) {
  146. qDebug() << __FUNCTION__ << "Unable to update permissions on client_general* file!";
  147. }
  148. // Initialising client_local_*.dat file and client_general.dat
  149. client_local_file_ = new LOTRO_DAT::DatFile(1);
  150. client_general_file_ = new LOTRO_DAT::DatFile(2);
  151. auto client_local_init_res = client_local_file_->Init(client_local_filepath.toStdString());
  152. auto client_general_init_res = client_general_file_->Init(client_general_filepath.toStdString());
  153. if (!client_local_init_res || !client_general_init_res) {
  154. client_local_file_->Deinit();
  155. client_general_file_->Deinit();
  156. qDebug() << __FUNCTION__ << "Finished LotroDatManager initialisation - error: DatFile initialisation error!";
  157. emit errorOccured("initializeManager", {}, "DatInitError");
  158. emit operationFinished("initializeManager", {}, false);
  159. return;
  160. }
  161. qDebug() << "LotroDatManager initialisation successfull! Dat files: "
  162. << QString::fromStdString(client_general_file_->GetFilename())
  163. << QString::fromStdString(client_local_file_->GetFilename());
  164. emit operationFinished("initializeManager", {}, true);
  165. }
  166. void PatchInstaller::startGame(bool freeze_updates) {
  167. // if freeze_updates is set to True, original game
  168. // launcher will be replaced with special program,
  169. // which controls lotro startup and prevents from updates
  170. emit operationStarted("startGame");
  171. QString game_folder = Settings::getValue("Lotro/game_path").toString();
  172. if (game_folder == "none") {
  173. qDebug() << __FUNCTION__ << "Starting game FAILED - game folder wasnt set!";
  174. emit errorOccured("startGame", {}, "GameFolderNotSet");
  175. emit operationFinished("startGame", {}, false);
  176. return;
  177. }
  178. if (!FileSystem::fileExists(QApplication::applicationDirPath() + "/Launcher.exe")) {
  179. qDebug() << __FUNCTION__ << "Starting game FAILED - no game launcher in legacy directory found!";
  180. emit errorOccured("startGame", {}, "NoGameLauncherInLegacyDir");
  181. emit operationFinished("startGame", {}, false);
  182. return;
  183. }
  184. if (freeze_updates) {
  185. QFile::remove(game_folder + "/lotro_ru.exe");
  186. if (!QFile::copy(QApplication::applicationDirPath() + "/LotroLauncher.exe", game_folder + "/lotro_ru.exe")) {
  187. qDebug() << __FUNCTION__ << "Starting game FAILED - cannot copy LotroLauncher to lotro_ru.exe!!";
  188. emit errorOccured("startGame", {}, "LauncherCopyFailed");
  189. emit operationFinished("startGame", {}, false);
  190. return;
  191. }
  192. QFile::remove(game_folder + "/LotroLauncher.exe");
  193. if (!QFile::copy(QApplication::applicationDirPath() + "/Launcher.exe", game_folder + "/LotroLauncher.exe")) {
  194. qDebug() << __FUNCTION__ << "Starting game FAILED - cannot copy GameLauncher to LotroLauncher!!";
  195. emit errorOccured("startGame", {}, "NoAccessToGameLauncher");
  196. emit operationFinished("startGame", {}, false);
  197. return;
  198. }
  199. QFile file(game_folder + "/legacy_path.txt");
  200. file.open(QIODevice::WriteOnly);
  201. QTextStream out(&file);
  202. out << QApplication::applicationDirPath() + "/LegacyLauncher.exe";
  203. file.close();
  204. } else {
  205. QFile::remove(game_folder + "/LotroLauncher.exe");
  206. if (!QFile::copy(QApplication::applicationDirPath() + "/LotroLauncher.exe", game_folder + "/LotroLauncher.exe")) {
  207. qDebug() << __FUNCTION__ << "Starting game FAILED - cannot copy LotroLauncher from working dir to LotroLauncher in lotro dir!!";
  208. emit errorOccured("startGame", {}, "NoAccessToGameLauncher");
  209. emit operationFinished("startGame", {}, false);
  210. return;
  211. }
  212. }
  213. QStringList args;
  214. if (freeze_updates) {
  215. args << "gamelaunch" << "-disablePatch";
  216. }
  217. if (Settings::getValue("Lotro/skip_raw_download").toBool()) {
  218. args << "-skiprawdownload";
  219. }
  220. if (Settings::getValue("Lotro/no_splash_screen").toBool()) {
  221. args << "-nosplashscreen";
  222. }
  223. client_general_file_->Deinit();
  224. client_local_file_->Deinit();
  225. QString username = Settings::getValue("Account/username").toString();
  226. QString password = Settings::getValue("Account/password").toString();
  227. if (!username.isEmpty() && !password.isEmpty()) {
  228. args << "-username" << username << "-password" << password;
  229. }
  230. qDebug() << __FUNCTION__ << "Starting game with arguments: " << args;
  231. QFile f(Settings::getValue("Lotro/game_path").toString() + "/LotroLauncher.exe");
  232. QProcess process;
  233. if (FileSystem::fileExists(f.fileName())) {
  234. if (f.fileName().contains(" ")) {
  235. f.setFileName("\"" + f.fileName() + "\"");
  236. }
  237. process.startDetached(f.fileName(), args);
  238. process.waitForFinished(-1);
  239. QApplication::quit();
  240. }
  241. emit operationFinished("startGame");
  242. }
  243. // TODO
  244. void PatchInstaller::startPatchInstallationChain() {
  245. }