patchinstaller.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  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 (category != -1 && !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. download_video_total_videos = database->CountRows();
  132. download_video_finished_videos = 0;
  133. if (!Settings::getValue("Components/videos").toBool()) {
  134. Settings::setValue("DatabaseNeedInstall/video", false);
  135. return;
  136. }
  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. download_video_finished_videos++;
  141. continue;
  142. }
  143. const QString filename = QString::fromStdString(file.options["name"].as<std::string>());
  144. const QString url = QString::fromStdString(file.options["url"].as<std::string>());
  145. const QString hash = QString::fromStdString(file.options["hash"].as<std::string>());
  146. const QString folder = QString::fromStdString(file.options["folder"].as<std::string>());
  147. const QString full_filename = Settings::getValue("Lotro/game_path").toString() + "/" + folder + "/" + filename;
  148. FileSystem::createFilePath(full_filename);
  149. if (FileSystem::fileExists(full_filename) && FileSystem::fileHash(full_filename) == hash) {
  150. download_video_finished_videos++;
  151. continue;
  152. }
  153. QFile* target_file = new QFile(full_filename);
  154. target_file->open(QIODevice::WriteOnly);
  155. Downloader* video_downloader = new Downloader(this);
  156. connect(video_downloader, &Downloader::progressChanged, this, &PatchInstaller::onDownloaderProgressChanged);
  157. video_downloader->setUrl(url);
  158. video_downloader->targetFile = target_file;
  159. video_downloader->start();
  160. video_downloader->waitForDownloaded();
  161. video_downloader->targetFile->close();
  162. video_downloader->targetFile->deleteLater();
  163. video_downloader->targetFile = nullptr;
  164. video_downloader->deleteLater();
  165. download_video_finished_videos++;
  166. }
  167. Settings::setValue("DatabaseNeedInstall/video", false);
  168. }
  169. // ############## PUBLIC SLOTS ############## //
  170. void PatchInstaller::init()
  171. {
  172. emit started();
  173. qDebug() << __FUNCTION__ << "Starting initialisation of LotroDatManager";
  174. qRegisterMetaType<PatchInstaller::Status>();
  175. QString game_folder = Settings::getValue("Lotro/game_path").toString();
  176. QString locale_prefix = Settings::getValue("Lotro/original_locale").toString();
  177. QString client_local_filepath = game_folder + "/client_local_" + locale_prefix + ".dat";
  178. QString client_general_filepath = game_folder + "/client_general.dat";
  179. if (!FileSystem::fileExists(client_local_filepath) || !FileSystem::fileExists(client_general_filepath)) {
  180. qCritical() << __FUNCTION__ << "DatFiles do not exist!" << client_local_filepath << " " << client_general_filepath;
  181. emit finished();
  182. return;
  183. }
  184. // Updating file permissions to be sure, that they're not in read-only mode
  185. if (!QFile::setPermissions(client_local_filepath, QFileDevice::Permission(0x6666))) {
  186. qDebug() << __FUNCTION__ << "Unable to update permissions on client_local_* file!";
  187. }
  188. if (!QFile::setPermissions(client_general_filepath, QFileDevice::Permission(0x6666))) {
  189. qDebug() << __FUNCTION__ << "Unable to update permissions on client_general* file!";
  190. }
  191. // Initialising client_local_*.dat file and client_general.dat
  192. client_local_file_ = new LOTRO_DAT::DatFile(1);
  193. client_general_file_ = new LOTRO_DAT::DatFile(2);
  194. auto client_local_init_res = client_local_file_->Init(client_local_filepath.toStdString());
  195. auto client_general_init_res = client_general_file_->Init(client_general_filepath.toStdString());
  196. if (!client_local_init_res || !client_general_init_res) {
  197. client_local_file_->Deinit();
  198. client_general_file_->Deinit();
  199. qCritical() << __FUNCTION__ << "Finished LotroDatManager initialisation - error: DatFile initialisation error!";
  200. emit finished();
  201. return;
  202. }
  203. qDebug() << "LotroDatManager initialisation successfull! Dat files: "
  204. << QString::fromStdString(client_general_file_->GetFilename())
  205. << QString::fromStdString(client_local_file_->GetFilename());
  206. emit finished();
  207. }
  208. void PatchInstaller::startGame(bool freeze_updates) {
  209. // if freeze_updates is set to True, original game
  210. // launcher will be replaced with special program,
  211. // which controls lotro startup and prevents from updates
  212. emit started();
  213. QString game_folder = Settings::getValue("Lotro/game_path").toString();
  214. if (game_folder == "none") {
  215. qCritical() << __FUNCTION__ << "Starting game FAILED - game folder wasnt set!";
  216. emit finished();
  217. return;
  218. }
  219. if (!FileSystem::fileExists(QApplication::applicationDirPath() + "/Launcher.exe")) {
  220. qCritical() << __FUNCTION__ << "Starting game FAILED - no game launcher in legacy directory found!";
  221. emit finished();
  222. return;
  223. }
  224. if (freeze_updates) {
  225. QFile::remove(game_folder + "/lotro_ru.exe");
  226. if (!QFile::copy(QApplication::applicationDirPath() + "/LotroLauncher.exe", game_folder + "/lotro_ru.exe")) {
  227. qCritical() << __FUNCTION__ << "Starting game FAILED - cannot copy LotroLauncher to lotro_ru.exe!!";
  228. emit finished();
  229. return;
  230. }
  231. QFile::remove(game_folder + "/LotroLauncher.exe");
  232. if (!QFile::copy(QApplication::applicationDirPath() + "/Launcher.exe", game_folder + "/LotroLauncher.exe")) {
  233. qCritical() << __FUNCTION__ << "Starting game FAILED - cannot copy GameLauncher to LotroLauncher!!";
  234. emit finished();
  235. return;
  236. }
  237. QFile file(game_folder + "/legacy_path.txt");
  238. file.open(QIODevice::WriteOnly);
  239. QTextStream out(&file);
  240. out << QApplication::applicationDirPath() + "/LegacyLauncher.exe";
  241. file.close();
  242. } else {
  243. QFile::remove(game_folder + "/LotroLauncher.exe");
  244. if (!QFile::copy(QApplication::applicationDirPath() + "/LotroLauncher.exe", game_folder + "/LotroLauncher.exe")) {
  245. qCritical() << __FUNCTION__ << "Starting game FAILED - cannot copy LotroLauncher from working dir to LotroLauncher in lotro dir!!";
  246. emit finished();
  247. return;
  248. }
  249. }
  250. QStringList args;
  251. if (freeze_updates) {
  252. args << "gamelaunch" << "-disablePatch";
  253. }
  254. if (Settings::getValue("Lotro/skip_raw_download").toBool()) {
  255. args << "-skiprawdownload";
  256. }
  257. if (Settings::getValue("Lotro/no_splash_screen").toBool()) {
  258. args << "-nosplashscreen";
  259. }
  260. client_general_file_->Deinit();
  261. client_local_file_->Deinit();
  262. QString username = Settings::getValue("Account/username").toString();
  263. QString password = Settings::getValue("Account/password").toString();
  264. if (!username.isEmpty() && !password.isEmpty()) {
  265. args << "-username" << username << "-password" << password;
  266. }
  267. qDebug() << __FUNCTION__ << "Starting game with arguments: " << args;
  268. QFile f(Settings::getValue("Lotro/game_path").toString() + "/LotroLauncher.exe");
  269. QProcess process;
  270. if (FileSystem::fileExists(f.fileName())) {
  271. if (f.fileName().contains(" ")) {
  272. f.setFileName("\"" + f.fileName() + "\"");
  273. }
  274. process.startDetached(f.fileName(), args);
  275. process.waitForFinished(-1);
  276. QApplication::quit();
  277. }
  278. emit finished();
  279. }
  280. void PatchInstaller::startPatchInstallationChain() {
  281. emit started();
  282. qInfo() << "PatchInstaller: Starting installation chain...";
  283. const QVector<QString> patches = {"text", "font", "image", "loadscreen", "texture", "sound", "video", "micro"};
  284. QMap<QString, LOTRO_DAT::Database*> patch_databases;
  285. current_status.total_parts = 0;
  286. current_status.finished_parts = 0;
  287. for (const QString& patch: patches) {
  288. if (!Settings::getValue("DatabaseNeedInstall/" + patch).toBool()) {
  289. continue;
  290. }
  291. const QString patch_hashsum = Settings::getValue("PatchDatabases/" + patch + "/hashsum").toString();
  292. const QString patch_filename = Settings::getValue("PatchDatabases/" + patch + "/path").toString();
  293. const QString real_file_hashsum = FileSystem::fileHash(patch_filename);
  294. if (!FileSystem::fileExists(patch_filename) || real_file_hashsum != patch_hashsum) {
  295. qCritical() << "PatchInstallation: Incorrect patch file: " << patch_filename << ", hashsum: " << real_file_hashsum << ", expected: " << patch_hashsum;
  296. continue;
  297. }
  298. LOTRO_DAT::Database* db = new LOTRO_DAT::Database();
  299. if (!db->InitDatabase(patch_filename.toStdString())) {
  300. qCritical() << "PatchInstallation: failed to initialize db " << patch_filename;
  301. continue;
  302. }
  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*, Downloader::Status progress) {
  318. emit videosDownloadProgressChanged(download_video_finished_videos, download_video_total_videos, progress);
  319. }