extractor_example.cpp 16 KB

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