patchinstaller.cpp 20 KB

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