patchinstaller.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. #include "patchinstaller.h"
  2. #include "models/filesystem.h"
  3. #include "models/settings.h"
  4. #include <QDebug>
  5. #include <QProcess>
  6. QString getComponentNameFromId(int id) {
  7. switch (id) {
  8. case 100: return "texts_main";
  9. case 101: return "texts_items";
  10. case 102: return "texts_emotes";
  11. case 200: return "maps";
  12. case 201: return "loadscreens";
  13. case 202: return "textures";
  14. case 300: return "sounds";
  15. case 301: return "videos";
  16. }
  17. return "none";
  18. }
  19. PatchInstaller::PatchInstaller(QObject *parent) : QObject(parent) {
  20. }
  21. bool PatchInstaller::initialised() {
  22. return client_general_file_->Initialized() && client_local_file_->Initialized();
  23. }
  24. PatchInstaller::~PatchInstaller() {
  25. deinit();
  26. }
  27. // ############## PRIVATE ############## //
  28. bool PatchInstaller::datPathIsRelevant() {
  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. QString client_local_current_path = QString::fromStdString(client_local_file_->GetFilename());
  34. QString client_general_current_path = QString::fromStdString(client_general_file_->GetFilename());
  35. return QFileInfo(client_local_filepath) != QFileInfo(client_local_current_path)
  36. || QFileInfo(client_general_filepath) != QFileInfo(client_general_current_path);
  37. }
  38. void PatchInstaller::deinit() {
  39. emit started();
  40. client_local_file_->Deinit();
  41. client_general_file_->Deinit();
  42. emit finished();
  43. }
  44. void PatchInstaller::installPatch(QString patch_name, LOTRO_DAT::Database* database) {
  45. if (!Settings::getValue("DatabaseNeedInstall/" + patch_name).toBool()) {
  46. return;
  47. }
  48. if (patch_name == "loadscreen") {
  49. installLoadscreens(database);
  50. return;
  51. }
  52. if (patch_name == "video") {
  53. installVideos(database);
  54. return;
  55. }
  56. if (patch_name == "micro" && !Settings::getValue("Components/micropatch").toBool()) {
  57. Settings::setValue("DatabaseNeedInstall/micropatch", false);
  58. return;
  59. }
  60. LOTRO_DAT::SubfileData file;
  61. qDebug() << "Total files in database " << database->CountRows();
  62. qDebug() << "Patching all files from database..." << database;
  63. while (!(file = database->GetNextFile()).Empty()) {
  64. current_status.finished_parts++;
  65. if (current_status.finished_parts * 100 / current_status.total_parts !=
  66. (current_status.finished_parts - 1) * 100 * 10 / current_status.total_parts) {
  67. // emitting if changed at least on 0.1%
  68. emit progressChanged(current_status);
  69. }
  70. if (!file.options["fid"]) {
  71. continue;
  72. }
  73. const int category = file.options["cat"] ? file.options["cat"].as<int>() : -1;
  74. QString component_name = getComponentNameFromId(category);
  75. if (!Settings::getValue("Components/" + component_name).toBool()) {
  76. continue;
  77. }
  78. const int dat_id = file.options["did"] ? file.options["did"].as<int>() : 0;
  79. if (dat_id == E_CLIENT_LOCAL) {
  80. client_local_file_->PatchFile(file);
  81. } else if (dat_id == E_CLIENT_GENERAL) {
  82. client_general_file_->PatchFile(file);
  83. } else {
  84. qWarning() << "Unknown dat id parameter for file " << file.options["fid"].as<long long>() << " (dat id value = " << dat_id << "), SKIPPING!";
  85. }
  86. }
  87. Settings::setValue("DatabaseNeedInstall/" + patch_name, false);
  88. return;
  89. }
  90. void PatchInstaller::installLoadscreens(LOTRO_DAT::Database* database) {
  91. if (!Settings::getValue("Components/loadscreens").toBool()) {
  92. current_status.finished_parts += database->CountRows();
  93. emit progressChanged(current_status);
  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. current_status.finished_parts++;
  120. if (current_status.finished_parts * 100 / current_status.total_parts !=
  121. (current_status.finished_parts - 1) * 100 * 10 / current_status.total_parts) {
  122. // emitting if changed at least on 0.1%
  123. emit progressChanged(current_status);
  124. }
  125. }
  126. Settings::setValue("DatabaseNeedInstall/loadscreen", false);
  127. }
  128. void PatchInstaller::installVideos(LOTRO_DAT::Database* database) {
  129. current_status.finished_parts += database->CountRows();
  130. emit progressChanged(current_status);
  131. if (!Settings::getValue("Components/videos").toBool()) {
  132. Settings::setValue("DatabaseNeedInstall/video", false);
  133. return;
  134. }
  135. video_downloaders_status.clear();
  136. QVector<Downloader*> video_downloaders;
  137. LOTRO_DAT::SubfileData file;
  138. while (!(file = database->GetNextFile()).Empty()) {
  139. if (!file.options["name"] || !file.options["url"] || !file.options["hash"] || !file.options["folder"]) {
  140. continue;
  141. }
  142. const QString filename = QString::fromStdString(file.options["name"].as<std::string>());
  143. const QString url = QString::fromStdString(file.options["url"].as<std::string>());
  144. const QString hash = QString::fromStdString(file.options["hash"].as<std::string>());
  145. const QString folder = QString::fromStdString(file.options["folder"].as<std::string>());
  146. const QString full_filename = Settings::getValue("Lotro/game_path").toString() + "/" + folder + "/" + filename;
  147. FileSystem::createFilePath(full_filename);
  148. if (FileSystem::fileExists(full_filename) && FileSystem::fileHash(full_filename) == hash) {
  149. continue;
  150. }
  151. QFile target_file(full_filename);
  152. target_file.open(QIODevice::WriteOnly);
  153. Downloader* video_downloader = new Downloader(this);
  154. video_downloaders_status[video_downloader] = Downloader::Status();
  155. video_downloader->setUrl(url);
  156. video_downloader->targetFile = &target_file;
  157. video_downloader->start();
  158. video_downloaders.push_back(video_downloader);
  159. }
  160. for (Downloader* downloader: video_downloaders) {
  161. downloader->waitForDownloaded();
  162. downloader->targetFile->close();
  163. downloader->targetFile = nullptr;
  164. downloader->deleteLater();
  165. }
  166. Settings::setValue("DatabaseNeedInstall/video", false);
  167. }
  168. // ############## PUBLIC SLOTS ############## //
  169. void PatchInstaller::init()
  170. {
  171. emit started();
  172. qDebug() << __FUNCTION__ << "Starting initialisation of LotroDatManager";
  173. qRegisterMetaType<PatchInstaller::Status>();
  174. QString game_folder = Settings::getValue("Lotro/game_path").toString();
  175. QString locale_prefix = Settings::getValue("Lotro/original_locale").toString();
  176. QString client_local_filepath = game_folder + "/client_local_" + locale_prefix + ".dat";
  177. QString client_general_filepath = game_folder + "/client_general.dat";
  178. if (!FileSystem::fileExists(client_local_filepath) || !FileSystem::fileExists(client_general_filepath)) {
  179. qCritical() << __FUNCTION__ << "DatFiles do not exist!" << client_local_filepath << " " << client_general_filepath;
  180. emit finished();
  181. return;
  182. }
  183. // Updating file permissions to be sure, that they're not in read-only mode
  184. if (!QFile::setPermissions(client_local_filepath, QFileDevice::Permission(0x6666))) {
  185. qDebug() << __FUNCTION__ << "Unable to update permissions on client_local_* file!";
  186. }
  187. if (!QFile::setPermissions(client_general_filepath, QFileDevice::Permission(0x6666))) {
  188. qDebug() << __FUNCTION__ << "Unable to update permissions on client_general* file!";
  189. }
  190. // Initialising client_local_*.dat file and client_general.dat
  191. client_local_file_ = new LOTRO_DAT::DatFile(1);
  192. client_general_file_ = new LOTRO_DAT::DatFile(2);
  193. auto client_local_init_res = client_local_file_->Init(client_local_filepath.toStdString());
  194. auto client_general_init_res = client_general_file_->Init(client_general_filepath.toStdString());
  195. if (!client_local_init_res || !client_general_init_res) {
  196. client_local_file_->Deinit();
  197. client_general_file_->Deinit();
  198. qCritical() << __FUNCTION__ << "Finished LotroDatManager initialisation - error: DatFile initialisation error!";
  199. emit finished();
  200. return;
  201. }
  202. qDebug() << "LotroDatManager initialisation successfull! Dat files: "
  203. << QString::fromStdString(client_general_file_->GetFilename())
  204. << QString::fromStdString(client_local_file_->GetFilename());
  205. emit finished();
  206. }
  207. void PatchInstaller::startGame(bool freeze_updates) {
  208. // if freeze_updates is set to True, original game
  209. // launcher will be replaced with special program,
  210. // which controls lotro startup and prevents from updates
  211. emit started();
  212. QString game_folder = Settings::getValue("Lotro/game_path").toString();
  213. if (game_folder == "none") {
  214. qCritical() << __FUNCTION__ << "Starting game FAILED - game folder wasnt set!";
  215. emit finished();
  216. return;
  217. }
  218. if (!FileSystem::fileExists(QApplication::applicationDirPath() + "/Launcher.exe")) {
  219. qCritical() << __FUNCTION__ << "Starting game FAILED - no game launcher in legacy directory found!";
  220. emit finished();
  221. return;
  222. }
  223. if (freeze_updates) {
  224. QFile::remove(game_folder + "/lotro_ru.exe");
  225. if (!QFile::copy(QApplication::applicationDirPath() + "/LotroLauncher.exe", game_folder + "/lotro_ru.exe")) {
  226. qCritical() << __FUNCTION__ << "Starting game FAILED - cannot copy LotroLauncher to lotro_ru.exe!!";
  227. emit finished();
  228. return;
  229. }
  230. QFile::remove(game_folder + "/LotroLauncher.exe");
  231. if (!QFile::copy(QApplication::applicationDirPath() + "/Launcher.exe", game_folder + "/LotroLauncher.exe")) {
  232. qCritical() << __FUNCTION__ << "Starting game FAILED - cannot copy GameLauncher to LotroLauncher!!";
  233. emit finished();
  234. return;
  235. }
  236. QFile file(game_folder + "/legacy_path.txt");
  237. file.open(QIODevice::WriteOnly);
  238. QTextStream out(&file);
  239. out << QApplication::applicationDirPath() + "/LegacyLauncher.exe";
  240. file.close();
  241. } else {
  242. QFile::remove(game_folder + "/LotroLauncher.exe");
  243. if (!QFile::copy(QApplication::applicationDirPath() + "/LotroLauncher.exe", game_folder + "/LotroLauncher.exe")) {
  244. qCritical() << __FUNCTION__ << "Starting game FAILED - cannot copy LotroLauncher from working dir to LotroLauncher in lotro dir!!";
  245. emit finished();
  246. return;
  247. }
  248. }
  249. QStringList args;
  250. if (freeze_updates) {
  251. args << "gamelaunch" << "-disablePatch";
  252. }
  253. if (Settings::getValue("Lotro/skip_raw_download").toBool()) {
  254. args << "-skiprawdownload";
  255. }
  256. if (Settings::getValue("Lotro/no_splash_screen").toBool()) {
  257. args << "-nosplashscreen";
  258. }
  259. client_general_file_->Deinit();
  260. client_local_file_->Deinit();
  261. QString username = Settings::getValue("Account/username").toString();
  262. QString password = Settings::getValue("Account/password").toString();
  263. if (!username.isEmpty() && !password.isEmpty()) {
  264. args << "-username" << username << "-password" << password;
  265. }
  266. qDebug() << __FUNCTION__ << "Starting game with arguments: " << args;
  267. QFile f(Settings::getValue("Lotro/game_path").toString() + "/LotroLauncher.exe");
  268. QProcess process;
  269. if (FileSystem::fileExists(f.fileName())) {
  270. if (f.fileName().contains(" ")) {
  271. f.setFileName("\"" + f.fileName() + "\"");
  272. }
  273. process.startDetached(f.fileName(), args);
  274. process.waitForFinished(-1);
  275. QApplication::quit();
  276. }
  277. emit finished();
  278. }
  279. void PatchInstaller::startPatchInstallationChain() {
  280. emit started();
  281. qInfo() << "PatchInstaller: Starting installation chain...";
  282. const QVector<QString> patches = {"text", "font", "image", "loadscreen", "texture", "sound", "video", "micro"};
  283. QMap<QString, LOTRO_DAT::Database*> patch_databases;
  284. current_status.total_parts = 0;
  285. current_status.finished_parts = 0;
  286. for (const QString& patch: patches) {
  287. if (!Settings::getValue("DatabaseNeedInstall/" + patch).toBool()) {
  288. continue;
  289. }
  290. const QString patch_hashsum = Settings::getValue("PatchDatabases/" + patch + "/hashsum").toString();
  291. const QString patch_filename = Settings::getValue("PatchDatabases/" + patch + "/path").toString();
  292. const QString real_file_hashsum = FileSystem::fileHash(patch_filename);
  293. if (!FileSystem::fileExists(patch_filename) || real_file_hashsum != patch_hashsum) {
  294. qCritical() << "PatchInstallation: Incorrect patch file: " << patch_filename << ", hashsum: " << real_file_hashsum << ", expected: " << patch_hashsum;
  295. continue;
  296. }
  297. LOTRO_DAT::Database* db = new LOTRO_DAT::Database();
  298. if (!db->InitDatabase(patch_filename.toStdString())) {
  299. qCritical() << "PatchInstallation: failed to initialize db " << patch_filename;
  300. continue;
  301. }
  302. LOTRO_DAT::SubfileData data = db->GetNextFile();
  303. patch_databases[patch] = db;
  304. current_status.total_parts += db->CountRows();
  305. }
  306. emit progressChanged(current_status);
  307. for (const QString patch_name: patch_databases.keys()) {
  308. qInfo() << "PatchInstaller: Installing patch " << patch_name;
  309. installPatch(patch_name, patch_databases[patch_name]);
  310. patch_databases[patch_name]->CloseDatabase();
  311. delete patch_databases[patch_name];
  312. }
  313. qInfo() << "PatchInstaller: Finished installation chain...";
  314. emit finished();
  315. }
  316. // ############## PRIVATE SLOTS ############## //
  317. void PatchInstaller::onDownloaderProgressChanged(Downloader* context, Downloader::Status progress) {
  318. video_downloaders_status[context] = progress;
  319. Downloader::Status cumulative_status;
  320. for (const Downloader::Status& status: video_downloaders_status.values()) {
  321. cumulative_status = cumulative_status + status;
  322. }
  323. emit videosDownloadProgressChanged(cumulative_status);
  324. }