patchinstaller.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  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 != 721359) {
  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. result.has_no_patch_mark = false;
  217. // const QString text_data = QString::fromUtf16(reinterpret_cast<const ushort*>(patch_versions_file.binary_data.data()), patch_versions_file.binary_data.size() / sizeof(ushort));
  218. // qDebug() << "AAAAAAAAA PATCH INFO TEXT DATA " << text_data;
  219. }
  220. // TODO: PATCHED BY OLD LEGACY CHECK
  221. return result;
  222. }
  223. void PatchInstaller::insertPatchesInfoInDatFile(const PatchInstaller::AppliedPatchesInfo& info) {
  224. LOTRO_DAT::SubfileData file_data;
  225. file_data.options["fid"] = _applied_patches_file_id;
  226. file_data.options["ext"] = LOTRO_DAT::StringFromFileType(LOTRO_DAT::TEXT);
  227. QString patches_info_data = "1::::::[HI_FROM_ENDEVIR_V2.0\n";
  228. patches_info_data += "TEXTS:" + info.texts_patch_hashsum + "\n";
  229. patches_info_data += "IMAGES:" + info.images_patch_hashsum + "\n";
  230. patches_info_data += "SOUNDS:" + info.sounds_patch_hashsum + "\n";
  231. patches_info_data += "LOADSCREENS:" + info.loadscreens_patch_hashsum + "\n";
  232. patches_info_data += "TEXTURES:" + info.textures_patch_hashsum + "\n";
  233. patches_info_data += "FONTS:" + info.fonts_patch_hashsum + "\n";
  234. patches_info_data += "VIDEOS:" + info.videos_patch_hashsum + "\n";
  235. patches_info_data += "]";
  236. file_data.text_data = patches_info_data.toStdU16String();
  237. client_local_file_->PatchFile(file_data);
  238. }
  239. // ############## PUBLIC SLOTS ############## //
  240. void PatchInstaller::init()
  241. {
  242. if (client_local_file_->Initialized() || client_general_file_->Initialized()) {
  243. // Firstly - deinitializing existing client_local files.
  244. deinit();
  245. }
  246. qDebug() << __FUNCTION__ << "Starting initialisation of LotroDatManager";
  247. qRegisterMetaType<PatchInstaller::Status>();
  248. QString game_folder = Settings::getValue("Lotro/game_path").toString();
  249. QString locale_prefix = Settings::getValue("Lotro/original_locale").toString();
  250. QString client_local_filepath = game_folder + "/client_local_" + locale_prefix + ".dat";
  251. QString client_general_filepath = game_folder + "/client_general.dat";
  252. // Initialising client_local_*.dat file and client_general.dat
  253. auto client_local_init_res = client_local_file_->Init(client_local_filepath.toStdString());
  254. auto client_general_init_res = client_general_file_->Init(client_general_filepath.toStdString());
  255. if (!client_local_init_res || !client_general_init_res) {
  256. client_local_file_->Deinit();
  257. client_general_file_->Deinit();
  258. qCritical() << __FUNCTION__ << "Finished LotroDatManager initialisation - error: DatFile initialisation error!";
  259. return;
  260. }
  261. // Initializing db for original files backup
  262. QString database_path = game_folder + "/LotroLegacy/orig_files.db";
  263. if (!FileSystem::fileExists(database_path)) {
  264. FileSystem::createFilePath(database_path);
  265. }
  266. orig_files_db.setDatabaseName(database_path);
  267. if (!orig_files_db.open()) {
  268. qCritical() << "Initializing PatchInstaller - Cannot open backup database!!!!!!! " << database_path;
  269. }
  270. orig_files_db.exec(create_table_query);
  271. orig_files_db.exec("PRAGMA synchronous = OFF");
  272. orig_files_db.exec("PRAGMA count_changes = OFF");
  273. orig_files_db.exec("PRAGMA journal_mode = MEMORY");
  274. orig_files_db.exec("PRAGMA temp_store = MEMORY");
  275. orig_files_db.exec("PRAGMA encoding = \"UTF-8\";");
  276. qDebug() << "LotroDatManager initialisation successfull! Dat files: "
  277. << QString::fromStdString(client_general_file_->GetFilename())
  278. << QString::fromStdString(client_local_file_->GetFilename());
  279. _current_applied_patches_info = getAppliedPatchesInfoFromDatFile();
  280. emit successfullyInitialized();
  281. }
  282. void PatchInstaller::startGame(bool freeze_updates) {
  283. // if freeze_updates is set to True, original game
  284. // launcher will be replaced with special program,
  285. // which controls lotro startup and prevents from updates
  286. QString game_folder = Settings::getValue("Lotro/game_path").toString();
  287. if (game_folder == "none") {
  288. qCritical() << __FUNCTION__ << "Starting game FAILED - game folder wasnt set!";
  289. return;
  290. }
  291. if (!FileSystem::fileExists(QApplication::applicationDirPath() + "/Launcher.exe")) {
  292. qCritical() << __FUNCTION__ << "Starting game FAILED - no game launcher in legacy directory found!";
  293. return;
  294. }
  295. if (freeze_updates) {
  296. QFile::remove(game_folder + "/lotro_ru.exe");
  297. if (!QFile::copy(QApplication::applicationDirPath() + "/LotroLauncher.exe", game_folder + "/lotro_ru.exe")) {
  298. qCritical() << __FUNCTION__ << "Starting game FAILED - cannot copy LotroLauncher to lotro_ru.exe!!";
  299. return;
  300. }
  301. QFile::remove(game_folder + "/LotroLauncher.exe");
  302. if (!QFile::copy(QApplication::applicationDirPath() + "/Launcher.exe", game_folder + "/LotroLauncher.exe")) {
  303. qCritical() << __FUNCTION__ << "Starting game FAILED - cannot copy GameLauncher to LotroLauncher!!";
  304. return;
  305. }
  306. QFile file(game_folder + "/legacy_path.txt");
  307. file.open(QIODevice::WriteOnly);
  308. QTextStream out(&file);
  309. out << QApplication::applicationDirPath() + "/LegacyLauncher.exe";
  310. file.close();
  311. } else {
  312. QFile::remove(game_folder + "/LotroLauncher.exe");
  313. if (!QFile::copy(QApplication::applicationDirPath() + "/LotroLauncher.exe", game_folder + "/LotroLauncher.exe")) {
  314. qCritical() << __FUNCTION__ << "Starting game FAILED - cannot copy LotroLauncher from working dir to LotroLauncher in lotro dir!!";
  315. return;
  316. }
  317. }
  318. QStringList args;
  319. if (freeze_updates) {
  320. args << "gamelaunch" << "-disablePatch";
  321. }
  322. if (Settings::getValue("Lotro/skip_raw_download").toBool()) {
  323. args << "-skiprawdownload";
  324. }
  325. if (Settings::getValue("Lotro/no_splash_screen").toBool()) {
  326. args << "-nosplashscreen";
  327. }
  328. client_general_file_->Deinit();
  329. client_local_file_->Deinit();
  330. QString username = Settings::getValue("Account/username").toString();
  331. QString password = Settings::getValue("Account/password").toString();
  332. if (!username.isEmpty() && !password.isEmpty()) {
  333. args << "-username" << username << "-password" << password;
  334. }
  335. qDebug() << __FUNCTION__ << "Starting game with arguments: " << args;
  336. QFile f(Settings::getValue("Lotro/game_path").toString() + "/LotroLauncher.exe");
  337. QProcess process;
  338. if (FileSystem::fileExists(f.fileName())) {
  339. if (f.fileName().contains(" ")) {
  340. f.setFileName("\"" + f.fileName() + "\"");
  341. }
  342. process.startDetached(f.fileName(), args);
  343. process.waitForFinished(-1);
  344. QApplication::quit();
  345. }
  346. }
  347. void PatchInstaller::startPatchInstallationChain() {
  348. emit started();
  349. qInfo() << "PatchInstaller: Starting installation chain...";
  350. const QVector<QString> patches = {"text", "font", "image", "loadscreen", "texture", "sound", "video", "micro"};
  351. QMap<QString, LOTRO_DAT::Database*> patch_databases;
  352. current_status.total_parts = 0;
  353. current_status.finished_parts = 0;
  354. for (const QString& patch: patches) {
  355. if (!Settings::getValue("DatabaseDownload/" + patch).toBool()) {
  356. qDebug() << "Skipping patch " << patch << " because its disabled";
  357. continue;
  358. }
  359. const QString patch_hashsum = Settings::getValue("PatchDatabases/" + patch + "/hashsum").toString();
  360. const QString patch_filename = Settings::getValue("PatchDatabases/" + patch + "/path").toString();
  361. QString hashsum_in_dat_file = "";
  362. if (patch == "text") {
  363. hashsum_in_dat_file = _current_applied_patches_info.texts_patch_hashsum;
  364. } else if (patch == "image") {
  365. hashsum_in_dat_file = _current_applied_patches_info.images_patch_hashsum;
  366. } else if (patch == "sound") {
  367. hashsum_in_dat_file = _current_applied_patches_info.sounds_patch_hashsum;
  368. } else if (patch == "texture") {
  369. hashsum_in_dat_file = _current_applied_patches_info.textures_patch_hashsum;
  370. } else if (patch == "font") {
  371. hashsum_in_dat_file = _current_applied_patches_info.fonts_patch_hashsum;
  372. }
  373. if (patch_hashsum == hashsum_in_dat_file) {
  374. qDebug() << "Skipping patch " << patch << " because its hashsum (" << patch_hashsum << ") is equal to hashsum in dat file (" << hashsum_in_dat_file << ")";
  375. return;
  376. }
  377. const QString real_file_hashsum = FileSystem::fileHash(patch_filename);
  378. if (!FileSystem::fileExists(patch_filename) || real_file_hashsum != patch_hashsum) {
  379. qCritical() << "PatchInstallation: Incorrect patch file: " << patch_filename << ", hashsum: " << real_file_hashsum << ", expected: " << patch_hashsum;
  380. continue;
  381. }
  382. LOTRO_DAT::Database* db = new LOTRO_DAT::Database();
  383. if (!db->InitDatabase(patch_filename.toStdString())) {
  384. qCritical() << "PatchInstallation: failed to initialize db " << patch_filename;
  385. continue;
  386. }
  387. patch_databases[patch] = db;
  388. current_status.total_parts += db->CountRows();
  389. }
  390. emit progressChanged(current_status);
  391. for (const QString patch_name: patch_databases.keys()) {
  392. qInfo() << "PatchInstaller: Installing patch " << patch_name;
  393. installPatch(patch_name, patch_databases[patch_name]);
  394. patch_databases[patch_name]->CloseDatabase();
  395. delete patch_databases[patch_name];
  396. }
  397. qInfo() << "PatchInstaller: Finished installation chain...";
  398. emit finished();
  399. }
  400. // ############## PRIVATE SLOTS ############## //
  401. void PatchInstaller::onDownloaderProgressChanged(Downloader*, Downloader::Status progress) {
  402. emit videosDownloadProgressChanged(download_video_finished_videos, download_video_total_videos, progress);
  403. }