patcher_example.cpp 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. //
  2. // Created by Иван_Архипов on 23.11.2017.
  3. //
  4. #include <iostream>
  5. #include <ctime>
  6. #include <algorithm>
  7. #ifdef WIN32
  8. #include <direct.h>
  9. #define mkdir(dir, mode) _mkdir(dir)
  10. #endif
  11. #include "LotroDat.h"
  12. using namespace LOTRO_DAT;
  13. void DatStatusChangedHandler(DatStatus::ProgressInfo info) {
  14. if (info.status == DatStatus::DAT_STATUS::E_FREE) {
  15. std::cout << "DatStatus: operation finished" << std::endl;
  16. return;
  17. }
  18. if (info.status == DatStatus::DAT_STATUS::E_BACKUP_CREATING) {
  19. std::cout << "DatStatus: creating backup " << info.percentage << "% "
  20. << "(" << info.finished_parts << "/" << info.total_parts << ")" << std::endl;
  21. return;
  22. }
  23. if (info.status == DatStatus::DAT_STATUS::E_BACKUP_REMOVING) {
  24. std::cout << "DatStatus: removing backup " << info.percentage << "% "
  25. << "(" << info.finished_parts << "/" << info.total_parts << ")" << std::endl;
  26. return;
  27. }
  28. if (info.status == DatStatus::DAT_STATUS::E_BACKUP_RESTORING) {
  29. std::cout << "DatStatus: restoring backup " << info.percentage << "% "
  30. << "(" << info.finished_parts << "/" << info.total_parts << ")" << std::endl;
  31. return;
  32. }
  33. if (info.status == DatStatus::DAT_STATUS::E_COMMITING) {
  34. std::cout << "DatStatus: applying locales " << info.percentage << "% "
  35. << "(" << info.finished_parts << "/" << info.total_parts << ")" << std::endl;
  36. return;
  37. }
  38. if (info.status == DatStatus::DAT_STATUS::E_EXTRACTING) {
  39. std::cout << "DatStatus: extracting data " << info.percentage << "% "
  40. << "(" << info.finished_parts << "/" << info.total_parts << ")" << std::endl;
  41. return;
  42. }
  43. if (info.status == DatStatus::DAT_STATUS::E_GATHERING_INFO) {
  44. std::cout << "DatStatus: gathering info " << info.percentage << "% "
  45. << "(" << info.finished_parts << "/" << info.total_parts << ")" << std::endl;
  46. return;
  47. }
  48. if (info.status == DatStatus::DAT_STATUS::E_INITIALISING) {
  49. std::cout << "DatStatus: initialising " << info.percentage << "% "
  50. << "(" << info.finished_parts << "/" << info.total_parts << ")" << std::endl;
  51. return;
  52. }
  53. if (info.status == DatStatus::DAT_STATUS::E_PATCHING) {
  54. std::cout << "DatStatus: applying patch " << info.percentage << "% "
  55. << "(" << info.finished_parts << "/" << info.total_parts << ")" << std::endl;
  56. return;
  57. }
  58. }
  59. int main() {
  60. std::cout.precision(1);
  61. std::cout << std::fixed;
  62. std::cout << "Gi1dor's LotRO .dat patcher ver. " << LOTRO_DAT_VERSION << std::endl;
  63. freopen("patcher_errors.log", "w", stderr);
  64. setbuf(stdout, nullptr);
  65. setbuf(stderr, nullptr);
  66. setvbuf (stdout, nullptr, _IONBF, BUFSIZ);
  67. setvbuf (stderr, nullptr, _IONBF, BUFSIZ);
  68. std::cout << "Hello! I'm a basic shell version of .dat file patcher. I can open .dat file directly, "
  69. "if you write path to it (with name of file) in file \"dat_file_path.txt\"\n";
  70. DatFile file;
  71. std::ifstream in("dat_file_path.txt");
  72. if (!in.fail()) {
  73. std::string filename;
  74. std::getline(in, filename);
  75. std::string file_id_str;
  76. std::getline(in, file_id_str);
  77. int file_id = 0;
  78. try {
  79. file_id = stoi(file_id_str);
  80. } catch (std::invalid_argument) {
  81. file_id = -1;
  82. std::cout << "Invalid file_id on second line of file dat_file_path.txt!\n\n";
  83. }
  84. if (file_id != -1) {
  85. std::cout << "Using .dat file from dat_file_path.txt...\n";
  86. std::cout << "Opening file " << filename << " with id = " << file_id << std::endl;
  87. auto operation = file.Initialise(filename, file_id);
  88. if (operation.result == false) {
  89. std::cout << "Dat initialisation failed. Error message: " << operation.msg << "\n";
  90. }
  91. }
  92. }
  93. while (!file.Initialized()) {
  94. std::cout << "Please, tell, where the .dat file is\n";
  95. std::cout << "Enter path to file (including filename): ";
  96. std::string filename;
  97. std::getline(std::cin, filename);
  98. std::cout << "Enter file id: ";
  99. std::string file_id_str;
  100. std::getline(std::cin, file_id_str);
  101. int file_id = 0;
  102. try {
  103. file_id = stoi(file_id_str);
  104. } catch (std::invalid_argument) {
  105. std::cout << "Invalid command entered!\n\n";
  106. continue;
  107. }
  108. std::cout << "Opening file " << filename << std::endl;
  109. auto operation = file.Initialise(filename, file_id);
  110. if (operation.result == false) {
  111. std::cout << "Dat initialisation failed. Error message: " << operation.msg << "\nTo try again enter path to .dat file\n";
  112. }
  113. }
  114. std::cout << "Great! File initialised successfully!\n";
  115. file.GetStatusModule().AddStatusChangedCallbackFunction(&DatStatusChangedHandler);
  116. while (true) {
  117. std::cout << "Please, choose, what should I do. I can patch datfile from database to .dat file (enter 1), "
  118. "change locale (enter 2), print current locale (enter 3), enable category (enter 4), disable category (enter 5), "
  119. "print disabled categories (enter 6), create backup (enter 7), remove backup (enter 8), "
  120. "restore .dat file from backup (enter 9), check if backup exists (enter 10) or exit (enter -1)\n";
  121. std::cout << "Enter number of command (1-10): ";
  122. std::string command;
  123. std::getline(std::cin, command);
  124. int cmd = -10;
  125. try {
  126. cmd = stoi(command);
  127. } catch (std::invalid_argument) {
  128. std::cout << "Invalid command entered!\n\n";
  129. continue;
  130. }
  131. if (cmd == -1) {
  132. std::cout << "Exiting. Thanks for using me!\n";
  133. file.Deinitialize();
  134. break;
  135. }
  136. if (cmd == 1) {
  137. std::cout << "You've chosen to patch database! Write name of database file (it should be in the same "
  138. "directory), of enter 0 to return to main dialogue.\n";
  139. while (true) {
  140. std::cout << "Enter name of file or 0: ";
  141. std::string dbname;
  142. std::getline(std::cin, dbname);
  143. if (dbname == std::to_string(0)) {
  144. std::cout << "Okay, returning back...\n\n";
  145. break;
  146. }
  147. Database db;
  148. std::cout << "Opening database... " << dbname << std::endl;
  149. if (!db.InitDatabase(dbname)) {
  150. std::cout << "Unfortunately, I cannot open this database. Could you try again please?\n";
  151. continue;
  152. };
  153. if (db.CountRows() == 0) {
  154. std::cout << "There are no files in database or database doesn't exist. "
  155. "Please, try again!\n";
  156. continue;
  157. }
  158. std::cout << "There are " << db.CountRows() << " files in database." << std::endl;
  159. std::cout << "Successfully opened database! Beginning patching...\n";
  160. const clock_t begin_time = clock();
  161. file.GetPatcher().PatchAllDatabase(&db);
  162. db.CloseDatabase();
  163. fprintf(stdout, "Spent %f seconds on patching! Thank you for your patience!\n",
  164. float(clock() - begin_time) / CLOCKS_PER_SEC);
  165. std::cout << "Great! File was patched successfully!\n\n";
  166. break;
  167. }
  168. }
  169. if (cmd == 2) {
  170. std::cout << "Old locale is " << (file.GetLocaleManager().GetCurrentLocale() == DatLocaleManager::PATCHED ? "RU" : "Original") << std::endl;
  171. std::cout << "Changing locale..." << std::endl;
  172. file.GetLocaleManager().SetLocale(file.GetLocaleManager().GetCurrentLocale() == DatLocaleManager::PATCHED ? DatLocaleManager::ORIGINAL : DatLocaleManager::PATCHED);
  173. std::cout << "New locale is " << (file.GetLocaleManager().GetCurrentLocale() == DatLocaleManager::PATCHED ? "RU" : "Original") << std::endl;
  174. }
  175. if (cmd == 3) {
  176. std::cout << "Current locale is " << (file.GetLocaleManager().GetCurrentLocale() == DatLocaleManager::PATCHED ? "RU" : "Original") << std::endl;
  177. }
  178. if (cmd == 4) {
  179. int category_id = 0;
  180. std::cout << "Enter category id: ";
  181. std::cin >> category_id;
  182. file.GetLocaleManager().EnableCategory(category_id);
  183. std::cout << "Category successfully enabled!" << std::endl;
  184. }
  185. if (cmd == 5) {
  186. int category_id = 0;
  187. std::cout << "Enter category id: ";
  188. std::cin >> category_id;
  189. file.GetLocaleManager().DisableCategory(category_id);
  190. std::cout << "Category successfully disabled!" << std::endl;
  191. }
  192. if (cmd == 6) {
  193. std::cout << "Disabled categories: ";
  194. for (auto i : file.GetLocaleManager().GetInactiveCategories())
  195. std::cout << i << " ";
  196. std::cout << std::endl;
  197. }
  198. if (cmd == 7) {
  199. std::cout << "Creating backup..." << std::endl;
  200. std::cout << "Create backup function returned " << file.GetBackupManager().CreateBackup("cli_local_En.backup").result << std::endl;
  201. }
  202. if (cmd == 8) {
  203. std::cout << "Removing backup..." << std::endl;
  204. std::cout << "Remove backup function returned " << file.GetBackupManager().RemoveBackup("cli_local_En.backup").result << std::endl;
  205. }
  206. if (cmd == 9) {
  207. std::cout << "Restoring file from backup..." << std::endl;
  208. std::cout << "Restore file function returned " << file.GetBackupManager().RestoreFromBackup("cli_local_En.backup").result << std::endl;
  209. }
  210. if (cmd == 10) {
  211. std::cout << "Backup file " << (file.GetBackupManager().CheckIfBackupAvailable("cli_local_En.backup") ? "exists!" : "doesn't exist.") << std::endl;
  212. }
  213. }
  214. //system("pause");
  215. return 0;
  216. }