extractor_example.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. //
  2. // Created by Иван_Архипов on 30.10.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. using namespace std;
  14. // Change these variables to true if you want export category to files.
  15. bool exportTextsToFiles = false;
  16. bool exportImagesToFiles = false;
  17. bool exportFontsToFiles = false;
  18. bool exportSoundsToFiles = false;
  19. bool exportTexturesToFiles = false;
  20. bool exportUnknownToFiles = false;
  21. // Change these variables to true if you want export category to databases.
  22. bool exportTextsToDb = false;
  23. bool exportImagesToDb = false;
  24. bool exportFontsToDb = false;
  25. bool exportSoundsToDb = false;
  26. bool exportTexturesToDb = false;
  27. bool exportUnknownToDb = false;
  28. // There is no need to change anything else
  29. void DatStatusChangedHandler(DatStatus::ProgressInfo info) {
  30. if (info.status == DatStatus::DAT_STATUS::E_FREE) {
  31. std::cout << "DatStatus: operation finished" << std::endl;
  32. return;
  33. }
  34. if (info.status == DatStatus::DAT_STATUS::E_BACKUP_CREATING) {
  35. std::cout << "DatStatus: creating backup " << info.percentage << "% "
  36. << "(" << info.finished_parts << "/" << info.total_parts << ")" << std::endl;
  37. return;
  38. }
  39. if (info.status == DatStatus::DAT_STATUS::E_BACKUP_REMOVING) {
  40. std::cout << "DatStatus: removing backup " << info.percentage << "% "
  41. << "(" << info.finished_parts << "/" << info.total_parts << ")" << std::endl;
  42. return;
  43. }
  44. if (info.status == DatStatus::DAT_STATUS::E_BACKUP_RESTORING) {
  45. std::cout << "DatStatus: restoring backup " << info.percentage << "% "
  46. << "(" << info.finished_parts << "/" << info.total_parts << ")" << std::endl;
  47. return;
  48. }
  49. if (info.status == DatStatus::DAT_STATUS::E_COMMITING) {
  50. std::cout << "DatStatus: applying locales " << info.percentage << "% "
  51. << "(" << info.finished_parts << "/" << info.total_parts << ")" << std::endl;
  52. return;
  53. }
  54. if (info.status == DatStatus::DAT_STATUS::E_EXTRACTING) {
  55. std::cout << "DatStatus: extracting data " << info.percentage << "% "
  56. << "(" << info.finished_parts << "/" << info.total_parts << ")" << std::endl;
  57. return;
  58. }
  59. if (info.status == DatStatus::DAT_STATUS::E_GATHERING_INFO) {
  60. std::cout << "DatStatus: gathering info " << info.percentage << "% "
  61. << "(" << info.finished_parts << "/" << info.total_parts << ")" << std::endl;
  62. return;
  63. }
  64. if (info.status == DatStatus::DAT_STATUS::E_INITIALISING) {
  65. std::cout << "DatStatus: initialising " << info.percentage << "% "
  66. << "(" << info.finished_parts << "/" << info.total_parts << ")" << std::endl;
  67. return;
  68. }
  69. if (info.status == DatStatus::DAT_STATUS::E_PATCHING) {
  70. std::cout << "DatStatus: applying patch " << info.percentage << "% "
  71. << "(" << info.finished_parts << "/" << info.total_parts << ")" << std::endl;
  72. return;
  73. }
  74. }
  75. int main() {
  76. std::cout.precision(1);
  77. std::cout << fixed;
  78. std::cout << "Gi1dor's LotRO .dat extractor ver. " << LOTRO_DAT_VERSION << std::endl;
  79. std::cout << "Hello! I'm a basic shell version of .dat file extractor. I can open .dat file directly, "
  80. "if you write path to it (with name of file) in file \"dat_file_path.txt\"\n";
  81. DatFile file;
  82. ifstream in("dat_file_path.txt");
  83. if (!in.fail()) {
  84. std::string filename;
  85. getline(in, filename);
  86. std::cout << "Using .dat file from dat_file_path.txt...\n";
  87. std::cout << "Opening file " << filename << std::endl;
  88. auto operation = file.Initialise(filename, 0);
  89. if (operation.result == false) {
  90. std::cout << "Dat initialisation failed. Error message: " << operation.msg << "\n";
  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 << "Opening file " << filename << std::endl;
  99. auto operation = file.Initialise(filename, 0);
  100. if (operation.result == false) {
  101. std::cout << "Dat initialisation failed. Error message: " << operation.msg << "\nTo try again enter path to .dat file\n";
  102. }
  103. }
  104. std::cout << "Great! File initialised successfully!\n";
  105. file.GetStatusModule().AddStatusChangedCallbackFunction(&DatStatusChangedHandler);
  106. std::cout << "Gathering file information...\n";
  107. file.GatherInformation("dat_info.log");
  108. int cmd = 0;
  109. while (true) {
  110. std::cout << "Please, choose, what should I do. I can extract .dat file to files (press 1), "
  111. "open another .dat file (press 2), or configure, what should I extract. Choose, what to do or exit (press -1)\n";
  112. if (cmd != 3) {
  113. std::cout << "Enter number of command (1-3): ";
  114. std::cin >> cmd;
  115. std::string tmp;
  116. std::getline(std::cin, tmp);
  117. }
  118. if (cmd == -1) {
  119. std::cout << "Exiting. Thanks for using me!\n";
  120. break;
  121. }
  122. if (cmd == 1) {
  123. const clock_t begin_time = clock();
  124. mkdir("Extracted data", 744);
  125. std::time_t result = std::time(nullptr);
  126. char *ttime = std::asctime(std::localtime(&result));
  127. auto *out_time = new char[25];
  128. memcpy(out_time, ttime, 24);
  129. out_time[24] = '\0';
  130. std::string filename = file.GetIO().GetFilename().value;
  131. std::cout << "FILENAME = " << filename << std::endl;
  132. std::string output_dir = std::string("Extracted data\\") + filename.substr(std::max(filename.length() - 16, 0u), 16) + " " + std::string(out_time) + "\\";
  133. std::replace(output_dir.begin(), output_dir.end(), ':', '-');
  134. mkdir(output_dir.c_str(), 744);
  135. std::cout << "Beginning unpacking... Please, wait for some minutes."
  136. "\nMaybe it's a good idea to have a cup of tea, while unpacker is working...\n" << std::flush;
  137. Database output_db;
  138. if (exportImagesToDb) {
  139. output_db.InitDatabase(output_dir + std::string("Images.db"));
  140. int extracted_jpg_files_num = file.GetExporter().ExtractAllFilesByType(JPG, &output_db).value;
  141. std::cout << "Extracted " << extracted_jpg_files_num << " .jpg files to Images.db" << std::endl << std::flush;
  142. output_db.CloseDatabase();
  143. }
  144. if (exportSoundsToDb) {
  145. output_db.InitDatabase(output_dir + std::string("Sounds.db"));
  146. int extracted_wav_files_num = file.GetExporter().ExtractAllFilesByType(WAV, &output_db).value;
  147. int extracted_ogg_files_num = file.GetExporter().ExtractAllFilesByType(OGG, &output_db).value;
  148. std::cout << "Extracted " << extracted_wav_files_num << " .wav files to Sounds.db" << std::endl << std::flush;
  149. std::cout << "Extracted " << extracted_ogg_files_num << " .ogg files to Sounds.db" << std::endl << std::flush;
  150. output_db.CloseDatabase();
  151. }
  152. if (exportTextsToDb) {
  153. output_db.InitDatabase(output_dir + std::string("Texts.db"));
  154. int extracted_text_files_num = file.GetExporter().ExtractAllFilesByType(TEXT, &output_db).value;
  155. std::cout << "Extracted " << extracted_text_files_num << " text files to Texts.db" << std::endl << std::flush;
  156. output_db.CloseDatabase();
  157. }
  158. if (exportFontsToDb) {
  159. output_db.InitDatabase(output_dir + std::string("Fonts.db"));
  160. int extracted_font_files_num = file.GetExporter().ExtractAllFilesByType(FONT, &output_db).value;
  161. std::cout << "Extracted " << extracted_font_files_num << " font files to Fonts.db" << std::endl << std::flush;
  162. output_db.CloseDatabase();
  163. }
  164. if (exportTexturesToDb) {
  165. output_db.InitDatabase(output_dir + std::string("Textures.db"));
  166. int extracted_dds_files_num = file.GetExporter().ExtractAllFilesByType(DDS, &output_db).value;
  167. std::cout << "Extracted " << extracted_dds_files_num << " .dds files to Textures.db" << std::endl << std::flush;
  168. output_db.CloseDatabase();
  169. }
  170. if (exportImagesToFiles) {
  171. mkdir((output_dir + "jpg").c_str(), 744);
  172. int extracted_jpg_files_num = file.GetExporter().ExtractAllFilesByType(JPG, output_dir + "jpg\\").value;
  173. std::cout << "Extracted " << extracted_jpg_files_num << " .jpg files to directory" << std::endl << std::flush;
  174. }
  175. if (exportTexturesToFiles) {
  176. mkdir((output_dir + "dds").c_str(), 744);
  177. int extracted_dds_files_num = file.GetExporter().ExtractAllFilesByType(DDS, output_dir + "dds\\").value;
  178. std::cout << "Extracted " << extracted_dds_files_num << " .dds files to directory" << std::endl << std::flush;
  179. }
  180. if (exportSoundsToFiles) {
  181. mkdir((output_dir + "wav").c_str(), 744);
  182. int extracted_wav_files_num = file.GetExporter().ExtractAllFilesByType(WAV, output_dir + "wav\\").value;
  183. std::cout << "Extracted " << extracted_wav_files_num << " .wav files to directory" << std::endl << std::flush;
  184. mkdir((output_dir + "ogg").c_str(), 744);
  185. int extracted_ogg_files_num = file.GetExporter().ExtractAllFilesByType(OGG, output_dir + "ogg\\").value;
  186. std::cout << "Extracted " << extracted_ogg_files_num << " .ogg files to directory" << std::endl << std::flush;
  187. }
  188. if (exportFontsToFiles) {
  189. mkdir((output_dir + "fonts").c_str(), 744);
  190. int extracted_font_files_num = file.GetExporter().ExtractAllFilesByType(FONT, output_dir + "fonts\\").value;
  191. std::cout << "Extracted " << extracted_font_files_num << " font files to directory" << std::endl << std::flush;
  192. }
  193. if (exportUnknownToFiles) {
  194. mkdir((output_dir + "unknown").c_str(), 744);
  195. int extracted_unknown_files_num = file.GetExporter().ExtractAllFilesByType(UNKNOWN, output_dir + "unknown\\").value;
  196. std::cout << "Extracted " << extracted_unknown_files_num << " unknown files to directory" << std::endl << std::flush;
  197. }
  198. if (exportTextsToFiles) {
  199. mkdir((output_dir + "texts").c_str(), 744);
  200. int extracted_text_files_num = file.GetExporter().ExtractAllFilesByType(TEXT, output_dir + "texts\\").value;
  201. std::cout << "Extracted " << extracted_text_files_num << " text files to directory" << std::endl << std::flush;
  202. }
  203. fprintf(stdout, "Spent %f seconds on running unpacker! Thank you for your patience!\n",
  204. float(clock() - begin_time) / CLOCKS_PER_SEC);
  205. }
  206. if (cmd == 2) {
  207. std::cout << "Closing file...\n";
  208. file.Deinitialize();
  209. while (!file.Initialized()) {
  210. std::cout << "Please, tell, where the .dat file is\n";
  211. std::cout << "Enter path to file (including filename): ";
  212. std::string filename;
  213. std::getline(std::cin, filename);
  214. std::cout << "Opening file " << filename << std::endl;
  215. auto operation = file.Initialise(filename, 0);
  216. if (operation.result == false) {
  217. std::cout << "Some error caused while opening the file... "
  218. "Could you enter .dat filename once more?" << std::endl;
  219. std::cout << "Error message: " << operation.msg << std::endl;
  220. file.Deinitialize();
  221. }
  222. }
  223. }
  224. if (cmd == 3) {
  225. std::cout << "Current parameters:\n";
  226. std::cout << "(01) Export texts to files - " << (exportTextsToFiles ? "true\n" : "false\n");
  227. std::cout << "(02) Export texts to database - " << (exportTextsToDb ? "true\n" : "false\n");
  228. std::cout << "(11) Export images to files - " << (exportImagesToFiles ? "true\n" : "false\n");
  229. std::cout << "(12) Export images to database - " << (exportImagesToDb ? "true\n" : "false\n");
  230. std::cout << "(21) Export fonts to files - " << (exportFontsToFiles ? "true\n" : "false\n");
  231. std::cout << "(22) Export fonts to database - " << (exportFontsToDb ? "true\n" : "false\n");
  232. std::cout << "(31) Export sounds to files - " << (exportSoundsToFiles ? "true\n" : "false\n");
  233. std::cout << "(32) Export sounds to database - " << (exportSoundsToDb ? "true\n" : "false\n");
  234. std::cout << "(41) Export textures to files - " << (exportTexturesToFiles ? "true\n" : "false\n");
  235. std::cout << "(42) Export textures to database - " << (exportTexturesToDb ? "true\n" : "false\n");
  236. std::cout << "(51) Export unknown files to files - " << (exportUnknownToFiles ? "true\n" : "false\n");
  237. std::cout << "(52) Export unknown files to database - " << (exportUnknownToDb ? "true\n" : "false\n");
  238. std::cout << "Enter number of parameter to change (or -1 to exit this menu): ";
  239. int number = 0;
  240. std::cin >> number;
  241. std::string tmp;
  242. std::getline(std::cin, tmp);
  243. if (number == -1) {
  244. std::cout << "Returning to main menu..\n";
  245. cmd = 0;
  246. } else {
  247. switch (number) {
  248. case 01:
  249. exportTextsToFiles = !exportTextsToFiles;
  250. break;
  251. case 02:
  252. exportTextsToDb = !exportTextsToDb;
  253. break;
  254. case 11:
  255. exportImagesToFiles = !exportImagesToFiles;
  256. break;
  257. case 12:
  258. exportImagesToDb = !exportImagesToDb;
  259. break;
  260. case 21:
  261. exportFontsToFiles = !exportFontsToFiles;
  262. break;
  263. case 22:
  264. exportFontsToDb = !exportFontsToDb;
  265. break;
  266. case 31:
  267. exportSoundsToFiles = !exportSoundsToFiles;
  268. break;
  269. case 32:
  270. exportSoundsToDb = !exportSoundsToDb;
  271. break;
  272. case 41:
  273. exportTexturesToFiles = !exportTexturesToFiles;
  274. break;
  275. case 42:
  276. exportTexturesToDb = !exportTexturesToDb;
  277. break;
  278. case 51:
  279. exportUnknownToFiles = !exportTexturesToFiles;
  280. break;
  281. case 52:
  282. exportUnknownToDb = !exportTexturesToDb;
  283. break;
  284. default:
  285. std::cout << "Incorrect number. Returning to main menu..\n";
  286. }
  287. }
  288. }
  289. }
  290. file.Deinitialize();
  291. system("pause");
  292. return 0;
  293. }