patchinstaller.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  1. #include "patchinstaller.h"
  2. #include "models/patchdownloader.h"
  3. #include "models/filesystem.h"
  4. #include "models/settings.h"
  5. #include <QDebug>
  6. #include <QProcess>
  7. #include <QApplication>
  8. #include <QSqlQuery>
  9. QString getComponentNameFromId(int id) {
  10. switch (id) {
  11. case 100: return "texts_main";
  12. case 101: return "texts_items";
  13. case 102: return "texts_emotes";
  14. case 200: return "maps";
  15. case 201: return "loadscreens";
  16. case 202: return "textures";
  17. case 300: return "sounds";
  18. case 301: return "videos";
  19. }
  20. return "none";
  21. }
  22. PatchInstaller::PatchInstaller(QObject *parent)
  23. : QObject(parent)
  24. , orig_files_db(QSqlDatabase::addDatabase("QSQLITE")) {
  25. client_local_file_ = new LOTRO_DAT::DatFile(100);
  26. client_general_file_ = new LOTRO_DAT::DatFile(101);
  27. connect(&PatchDownloader::instance(), &PatchDownloader::finished, this, &PatchInstaller::startPatchInstallationChain);
  28. }
  29. bool PatchInstaller::initialised() {
  30. return client_general_file_->Initialized() && client_local_file_->Initialized();
  31. }
  32. PatchInstaller::~PatchInstaller() {
  33. deinit();
  34. delete client_local_file_;
  35. delete client_general_file_;
  36. }
  37. // ############## PRIVATE ############## //
  38. bool PatchInstaller::datPathIsRelevant() {
  39. QString game_folder = Settings::getValue("Lotro/game_path").toString();
  40. QString locale_prefix = Settings::getValue("Lotro/original_locale").toString();
  41. QString client_local_filepath = game_folder + "/client_local_" + locale_prefix + ".dat";
  42. QString client_general_filepath = game_folder + "/client_general.dat";
  43. QString client_local_current_path = QString::fromStdString(client_local_file_->GetFilename());
  44. QString client_general_current_path = QString::fromStdString(client_general_file_->GetFilename());
  45. return QFileInfo(client_local_filepath) != QFileInfo(client_local_current_path)
  46. || QFileInfo(client_general_filepath) != QFileInfo(client_general_current_path);
  47. }
  48. void PatchInstaller::deinit() {
  49. orig_files_db.close();
  50. client_local_file_->Deinit();
  51. client_general_file_->Deinit();
  52. emit deinitialized();
  53. }
  54. PatchInstaller::AppliedPatchesInfo PatchInstaller::getPatchesInfo() {
  55. return _current_applied_patches_info;
  56. }
  57. void PatchInstaller::installPatch(QString patch_name, LOTRO_DAT::Database* database) {
  58. if (patch_name == "loadscreen") {
  59. installLoadscreens(database);
  60. return;
  61. }
  62. if (patch_name == "video") {
  63. installVideos(database);
  64. return;
  65. }
  66. if (patch_name == "micro" && !Settings::getValue("Components/micropatch").toBool()) {
  67. Settings::setValue("DatabaseNeedInstall/micropatch", false);
  68. return;
  69. }
  70. LOTRO_DAT::SubfileData file;
  71. qDebug() << "Total files in database " << database->CountRows();
  72. qDebug() << "Patching all files from database..." << database;
  73. while (!(file = database->GetNextFile()).Empty()) {
  74. current_status.finished_parts++;
  75. if (current_status.finished_parts * 100 / current_status.total_parts !=
  76. (current_status.finished_parts - 1) * 100 * 10 / current_status.total_parts) {
  77. // emitting if changed at least on 0.1%
  78. emit progressChanged(current_status);
  79. }
  80. if (!file.options["fid"]) {
  81. continue;
  82. }
  83. const int category = file.options["cat"] ? file.options["cat"].as<int>() : -1;
  84. QString component_name = getComponentNameFromId(category);
  85. if (category != -1 && !Settings::getValue("Components/" + component_name).toBool()) {
  86. continue;
  87. }
  88. const int dat_id = file.options["did"] ? file.options["did"].as<int>() : 0;
  89. const int file_id = file.options["fid"].as<int>();
  90. int file_version = -1;
  91. if (dat_id == E_CLIENT_LOCAL) {
  92. client_local_file_->PatchFile(file);
  93. file_version = client_local_file_->GetFileVersion(file_id);
  94. } else if (dat_id == E_CLIENT_GENERAL) {
  95. client_general_file_->PatchFile(file);
  96. file_version = client_local_file_->GetFileVersion(file_id);
  97. } else {
  98. qWarning() << "Unknown dat id parameter for file " << file.options["fid"].as<long long>() << " (dat id value = " << dat_id << "), SKIPPING!";
  99. }
  100. if (file_version != _patch_files_versions) {
  101. const QString query = "INSERT INTO `patch_data` (`file_id`, `options`) VALUES ('"
  102. + QString::number(file_id) + "', '" + QString::number(file_version)
  103. + "') ON CONFLICT(`file_id`) DO UPDATE SET `options`='" + QString::number(file_version) + "';";
  104. qDebug() << "EXECUTING: " << query;
  105. orig_files_db.exec(query);
  106. }
  107. }
  108. _current_applied_patches_info.has_no_patch_mark = false;
  109. const QString hashsum = Settings::getValue("PatchDatabases/" + patch_name + "/hashsum").toString();
  110. if (patch_name == "text") {
  111. _current_applied_patches_info.texts_patch_hashsum = hashsum;
  112. } else if (patch_name == "image") {
  113. _current_applied_patches_info.images_patch_hashsum = hashsum;
  114. } else if (patch_name == "sound") {
  115. _current_applied_patches_info.sounds_patch_hashsum = hashsum;
  116. } else if (patch_name == "texture") {
  117. _current_applied_patches_info.textures_patch_hashsum = hashsum;
  118. } else if (patch_name == "font") {
  119. _current_applied_patches_info.fonts_patch_hashsum = hashsum;
  120. }
  121. insertPatchesInfoInDatFile(_current_applied_patches_info);
  122. Settings::setValue("DatabaseNeedInstall/" + patch_name, false);
  123. return;
  124. }
  125. void PatchInstaller::installLoadscreens(LOTRO_DAT::Database* database) {
  126. if (!Settings::getValue("Components/loadscreens").toBool()) {
  127. current_status.finished_parts += database->CountRows();
  128. emit progressChanged(current_status);
  129. Settings::setValue("DatabaseNeedInstall/loadscreen", false);
  130. return;
  131. }
  132. QString locale_prefix = Settings::getValue("Lotro/original_locale").toString();
  133. const QStringList loadscreens_filenames = {
  134. locale_prefix == "English" ? "lotro_ad_pregame.jpg" : "lotro_ad_pregame_" + locale_prefix + ".jpg",
  135. "lotro_generic_teleport_screen_01.jpg",
  136. "lotro_generic_teleport_screen_02.jpg",
  137. "lotro_generic_teleport_screen_03.jpg",
  138. "lotro_generic_teleport_screen_04.jpg",
  139. "lotro_generic_teleport_screen_05.jpg",
  140. "lotro_generic_teleport_screen_06.jpg",
  141. "lotro_generic_teleport_screen_07.jpg",
  142. "lotro_generic_teleport_screen_08.jpg",
  143. "lotro_generic_teleport_screen_09.jpg",
  144. "lotro_generic_teleport_screen_10.jpg"
  145. };
  146. LOTRO_DAT::SubfileData data;
  147. QString logo_path = Settings::getValue("Lotro/game_path").toString() + "/raw/" + (locale_prefix == "English" ? "en" : locale_prefix) + "/logo/";
  148. for (size_t i = 0; i < qMin(size_t(loadscreens_filenames.size()), database->CountRows()); ++i) {
  149. data = database->GetNextFile();
  150. QFile::remove(logo_path + loadscreens_filenames[i]);
  151. if (!data.binary_data.WriteToFile((logo_path + loadscreens_filenames[i]).toLocal8Bit())) {
  152. qWarning() << "InstallLoadscreens: Cannot write to file " << logo_path + loadscreens_filenames[i];
  153. }
  154. current_status.finished_parts++;
  155. if (current_status.finished_parts * 100 / current_status.total_parts !=
  156. (current_status.finished_parts - 1) * 100 * 10 / current_status.total_parts) {
  157. // emitting if changed at least on 0.1%
  158. emit progressChanged(current_status);
  159. }
  160. }
  161. Settings::setValue("DatabaseNeedInstall/loadscreen", false);
  162. _current_applied_patches_info.has_no_patch_mark = false;
  163. _current_applied_patches_info.loadscreens_patch_hashsum = Settings::getValue("PatchDatabases/loadscreen/hashsum").toString();
  164. insertPatchesInfoInDatFile(_current_applied_patches_info);
  165. }
  166. void PatchInstaller::installVideos(LOTRO_DAT::Database* database) {
  167. current_status.finished_parts += database->CountRows();
  168. emit progressChanged(current_status);
  169. download_video_total_videos = database->CountRows();
  170. download_video_finished_videos = 0;
  171. if (!Settings::getValue("Components/videos").toBool()) {
  172. Settings::setValue("DatabaseNeedInstall/video", false);
  173. return;
  174. }
  175. LOTRO_DAT::SubfileData file;
  176. while (!(file = database->GetNextFile()).Empty()) {
  177. if (!file.options["name"] || !file.options["url"] || !file.options["hash"] || !file.options["folder"]) {
  178. download_video_finished_videos++;
  179. continue;
  180. }
  181. const QString filename = QString::fromStdString(file.options["name"].as<std::string>());
  182. const QString url = QString::fromStdString(file.options["url"].as<std::string>());
  183. const QString hash = QString::fromStdString(file.options["hash"].as<std::string>());
  184. const QString folder = QString::fromStdString(file.options["folder"].as<std::string>());
  185. const QString full_filename = Settings::getValue("Lotro/game_path").toString() + "/" + folder + "/" + filename;
  186. FileSystem::createFilePath(full_filename);
  187. if (FileSystem::fileExists(full_filename) && FileSystem::fileHash(full_filename) == hash) {
  188. download_video_finished_videos++;
  189. continue;
  190. }
  191. QFile* target_file = new QFile(full_filename);
  192. target_file->open(QIODevice::WriteOnly);
  193. Downloader* video_downloader = new Downloader(this);
  194. connect(video_downloader, &Downloader::progressChanged, this, &PatchInstaller::onDownloaderProgressChanged);
  195. video_downloader->setUrl(url);
  196. video_downloader->targetFile = target_file;
  197. video_downloader->start();
  198. video_downloader->waitForDownloaded();
  199. video_downloader->targetFile->close();
  200. video_downloader->targetFile->deleteLater();
  201. video_downloader->targetFile = nullptr;
  202. video_downloader->deleteLater();
  203. download_video_finished_videos++;
  204. }
  205. _current_applied_patches_info.has_no_patch_mark = false;
  206. _current_applied_patches_info.videos_patch_hashsum = Settings::getValue("PatchDatabases/video/hashsum").toString();
  207. insertPatchesInfoInDatFile(_current_applied_patches_info);
  208. Settings::setValue("DatabaseNeedInstall/video", false);
  209. }
  210. PatchInstaller::AppliedPatchesInfo PatchInstaller::getAppliedPatchesInfoFromDatFile() {
  211. PatchInstaller::AppliedPatchesInfo result;
  212. const LOTRO_DAT::SubfileData patch_versions_file = client_local_file_->GetFile(_applied_patches_file_id);
  213. if (patch_versions_file.Empty()) {
  214. result.has_no_patch_mark = true;
  215. } else {
  216. const QString text_data = QString::fromUtf8(reinterpret_cast<const char*>(patch_versions_file.binary_data.data() + 4), patch_versions_file.binary_data.size() - 4);
  217. const QStringList text_data_splitted = text_data.split("\n");
  218. if (text_data_splitted.length() < 0 || text_data_splitted[0] != _patch_mark_header) {
  219. return result;
  220. }
  221. result.has_no_patch_mark = false;
  222. for (const QString& str: text_data_splitted) {
  223. if (str.startsWith("TEXTS:")) {
  224. result.texts_patch_hashsum = QString(str).remove(0, 6);
  225. } else if (str.startsWith("IMAGES:")) {
  226. result.images_patch_hashsum = QString(str).remove(0, 7);
  227. } else if (str.startsWith("SOUNDS:")) {
  228. result.sounds_patch_hashsum = QString(str).remove(0, 7);
  229. } else if (str.startsWith("LOADSCREENS:")) {
  230. result.loadscreens_patch_hashsum = QString(str).remove(0, 12);
  231. } else if (str.startsWith("TEXTURES:")) {
  232. result.textures_patch_hashsum = QString(str).remove(0, 9);
  233. } else if (str.startsWith("FONTS:")) {
  234. result.fonts_patch_hashsum = QString(str).remove(0, 6);
  235. } else if (str.startsWith("VIDEOS:")) {
  236. result.videos_patch_hashsum = QString(str).remove(0, 6);
  237. }
  238. }
  239. }
  240. // TODO: PATCHED BY OLD LEGACY CHECK
  241. return result;
  242. }
  243. void PatchInstaller::insertPatchesInfoInDatFile(const PatchInstaller::AppliedPatchesInfo& info) {
  244. LOTRO_DAT::SubfileData file_data;
  245. file_data.options["fid"] = _applied_patches_file_id;
  246. file_data.options["ext"] = LOTRO_DAT::StringFromFileType(LOTRO_DAT::TEXT);
  247. QString patches_info_data = _patch_mark_header + "\n";
  248. patches_info_data += "TEXTS:" + info.texts_patch_hashsum + "\n";
  249. patches_info_data += "IMAGES:" + info.images_patch_hashsum + "\n";
  250. patches_info_data += "SOUNDS:" + info.sounds_patch_hashsum + "\n";
  251. patches_info_data += "LOADSCREENS:" + info.loadscreens_patch_hashsum + "\n";
  252. patches_info_data += "TEXTURES:" + info.textures_patch_hashsum + "\n";
  253. patches_info_data += "FONTS:" + info.fonts_patch_hashsum + "\n";
  254. patches_info_data += "VIDEOS:" + info.videos_patch_hashsum + "\n";
  255. QByteArray data = QByteArray((char*)&_applied_patches_file_id, 4) + patches_info_data.toUtf8();
  256. file_data.binary_data = LOTRO_DAT::BinaryData(data.data(), data.size());
  257. client_local_file_->PatchFile(file_data, _patch_files_versions, 1, true);
  258. }
  259. // ############## PUBLIC SLOTS ############## //
  260. void PatchInstaller::init()
  261. {
  262. if (client_local_file_->Initialized() || client_general_file_->Initialized()) {
  263. // Firstly - deinitializing existing client_local files.
  264. deinit();
  265. }
  266. qDebug() << __FUNCTION__ << "Starting initialisation of LotroDatManager";
  267. qRegisterMetaType<PatchInstaller::Status>();
  268. QString game_folder = Settings::getValue("Lotro/game_path").toString();
  269. QString locale_prefix = Settings::getValue("Lotro/original_locale").toString();
  270. QString client_local_filepath = game_folder + "/client_local_" + locale_prefix + ".dat";
  271. QString client_general_filepath = game_folder + "/client_general.dat";
  272. // Initialising client_local_*.dat file and client_general.dat
  273. auto client_local_init_res = client_local_file_->Init(client_local_filepath.toStdString());
  274. auto client_general_init_res = client_general_file_->Init(client_general_filepath.toStdString());
  275. if (!client_local_init_res || !client_general_init_res) {
  276. client_local_file_->Deinit();
  277. client_general_file_->Deinit();
  278. qCritical() << __FUNCTION__ << "Finished LotroDatManager initialisation - error: DatFile initialisation error!";
  279. return;
  280. }
  281. // Initializing db for original files backup
  282. QString database_path = game_folder + "/LotroLegacy/orig_files.db";
  283. if (!FileSystem::fileExists(database_path)) {
  284. FileSystem::createFilePath(database_path);
  285. }
  286. orig_files_db.setDatabaseName(database_path);
  287. if (!orig_files_db.open()) {
  288. qCritical() << "Initializing PatchInstaller - Cannot open backup database!!!!!!! " << database_path;
  289. }
  290. orig_files_db.exec(create_table_query);
  291. orig_files_db.exec("PRAGMA synchronous = OFF");
  292. orig_files_db.exec("PRAGMA count_changes = OFF");
  293. orig_files_db.exec("PRAGMA journal_mode = MEMORY");
  294. orig_files_db.exec("PRAGMA temp_store = MEMORY");
  295. orig_files_db.exec("PRAGMA encoding = \"UTF-8\";");
  296. qDebug() << "LotroDatManager initialisation successfull! Dat files: "
  297. << QString::fromStdString(client_general_file_->GetFilename())
  298. << QString::fromStdString(client_local_file_->GetFilename());
  299. _current_applied_patches_info = getAppliedPatchesInfoFromDatFile();
  300. emit successfullyInitialized();
  301. }
  302. void PatchInstaller::startGame(bool freeze_updates) {
  303. // if freeze_updates is set to True, original game
  304. // launcher will be replaced with special program,
  305. // which controls lotro startup and prevents from updates
  306. QString game_folder = Settings::getValue("Lotro/game_path").toString();
  307. if (game_folder == "none") {
  308. qCritical() << __FUNCTION__ << "Starting game FAILED - game folder wasnt set!";
  309. return;
  310. }
  311. if (!FileSystem::fileExists(QApplication::applicationDirPath() + "/Launcher.exe")) {
  312. qCritical() << __FUNCTION__ << "Starting game FAILED - no game launcher in legacy directory found!";
  313. return;
  314. }
  315. if (freeze_updates) {
  316. QFile::remove(game_folder + "/lotro_ru.exe");
  317. if (!QFile::copy(QApplication::applicationDirPath() + "/LotroLauncher.exe", game_folder + "/lotro_ru.exe")) {
  318. qCritical() << __FUNCTION__ << "Starting game FAILED - cannot copy LotroLauncher to lotro_ru.exe!!";
  319. return;
  320. }
  321. QFile::remove(game_folder + "/LotroLauncher.exe");
  322. if (!QFile::copy(QApplication::applicationDirPath() + "/Launcher.exe", game_folder + "/LotroLauncher.exe")) {
  323. qCritical() << __FUNCTION__ << "Starting game FAILED - cannot copy GameLauncher to LotroLauncher!!";
  324. return;
  325. }
  326. QFile file(game_folder + "/legacy_path.txt");
  327. file.open(QIODevice::WriteOnly);
  328. QTextStream out(&file);
  329. out << QApplication::applicationDirPath() + "/LegacyLauncher.exe";
  330. file.close();
  331. } else {
  332. QFile::remove(game_folder + "/LotroLauncher.exe");
  333. if (!QFile::copy(QApplication::applicationDirPath() + "/LotroLauncher.exe", game_folder + "/LotroLauncher.exe")) {
  334. qCritical() << __FUNCTION__ << "Starting game FAILED - cannot copy LotroLauncher from working dir to LotroLauncher in lotro dir!!";
  335. return;
  336. }
  337. }
  338. QStringList args;
  339. if (freeze_updates) {
  340. args << "gamelaunch" << "-disablePatch";
  341. }
  342. if (Settings::getValue("Lotro/skip_raw_download").toBool()) {
  343. args << "-skiprawdownload";
  344. }
  345. if (Settings::getValue("Lotro/no_splash_screen").toBool()) {
  346. args << "-nosplashscreen";
  347. }
  348. client_general_file_->Deinit();
  349. client_local_file_->Deinit();
  350. QString username = Settings::getValue("Account/username").toString();
  351. QString password = Settings::getValue("Account/password").toString();
  352. if (!username.isEmpty() && !password.isEmpty()) {
  353. args << "-username" << username << "-password" << password;
  354. }
  355. qDebug() << __FUNCTION__ << "Starting game with arguments: " << args;
  356. QFile f(Settings::getValue("Lotro/game_path").toString() + "/LotroLauncher.exe");
  357. QProcess process;
  358. if (FileSystem::fileExists(f.fileName())) {
  359. if (f.fileName().contains(" ")) {
  360. f.setFileName("\"" + f.fileName() + "\"");
  361. }
  362. process.startDetached(f.fileName(), args);
  363. process.waitForFinished(-1);
  364. QApplication::quit();
  365. }
  366. }
  367. void PatchInstaller::startPatchInstallationChain() {
  368. emit started();
  369. qInfo() << "PatchInstaller: Starting installation chain...";
  370. const QVector<QString> patches = {"text", "font", "image", "loadscreen", "texture", "sound", "video", "micro"};
  371. QMap<QString, LOTRO_DAT::Database*> patch_databases;
  372. current_status.total_parts = 0;
  373. current_status.finished_parts = 0;
  374. for (const QString& patch: patches) {
  375. if (!Settings::getValue("DatabaseDownload/" + patch).toBool()) {
  376. qDebug() << "Skipping patch " << patch << " because its disabled";
  377. continue;
  378. }
  379. const QString patch_hashsum = Settings::getValue("PatchDatabases/" + patch + "/hashsum").toString();
  380. const QString patch_filename = Settings::getValue("PatchDatabases/" + patch + "/path").toString();
  381. QString hashsum_in_dat_file = "";
  382. if (patch == "text") {
  383. hashsum_in_dat_file = _current_applied_patches_info.texts_patch_hashsum;
  384. } else if (patch == "image") {
  385. hashsum_in_dat_file = _current_applied_patches_info.images_patch_hashsum;
  386. } else if (patch == "sound") {
  387. hashsum_in_dat_file = _current_applied_patches_info.sounds_patch_hashsum;
  388. } else if (patch == "texture") {
  389. hashsum_in_dat_file = _current_applied_patches_info.textures_patch_hashsum;
  390. } else if (patch == "font") {
  391. hashsum_in_dat_file = _current_applied_patches_info.fonts_patch_hashsum;
  392. }
  393. if (patch_hashsum == hashsum_in_dat_file) {
  394. qDebug() << "Skipping patch " << patch << " because its hashsum (" << patch_hashsum << ") is equal to hashsum in dat file (" << hashsum_in_dat_file << ")";
  395. return;
  396. }
  397. const QString real_file_hashsum = FileSystem::fileHash(patch_filename);
  398. if (!FileSystem::fileExists(patch_filename) || real_file_hashsum != patch_hashsum) {
  399. qCritical() << "PatchInstallation: Incorrect patch file: " << patch_filename << ", hashsum: " << real_file_hashsum << ", expected: " << patch_hashsum;
  400. continue;
  401. }
  402. LOTRO_DAT::Database* db = new LOTRO_DAT::Database();
  403. if (!db->InitDatabase(patch_filename.toStdString())) {
  404. qCritical() << "PatchInstallation: failed to initialize db " << patch_filename;
  405. continue;
  406. }
  407. patch_databases[patch] = db;
  408. current_status.total_parts += db->CountRows();
  409. }
  410. emit progressChanged(current_status);
  411. for (const QString patch_name: patch_databases.keys()) {
  412. qInfo() << "PatchInstaller: Installing patch " << patch_name;
  413. installPatch(patch_name, patch_databases[patch_name]);
  414. patch_databases[patch_name]->CloseDatabase();
  415. delete patch_databases[patch_name];
  416. }
  417. qInfo() << "PatchInstaller: Finished installation chain...";
  418. emit finished();
  419. }
  420. // ############## PRIVATE SLOTS ############## //
  421. void PatchInstaller::onDownloaderProgressChanged(Downloader*, Downloader::Status progress) {
  422. emit videosDownloadProgressChanged(download_video_finished_videos, download_video_total_videos, progress);
  423. }