patchinstaller.cpp 17 KB

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