inserter.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. #include <iostream>
  2. #include <fstream>
  3. #include <ctime>
  4. #include <algorithm>
  5. #ifdef WIN32
  6. #include <direct.h>
  7. #define mkdir(dir, mode) _mkdir(dir)
  8. #endif
  9. #include "datfile.h"
  10. using namespace LOTRO_DAT;
  11. void InitializeFileLoop(DatFile& file) {
  12. while (!file.Initialized()) {
  13. std::cout << "Please, tell, where the .dat file is\n";
  14. std::cout << "Enter path to file (including filename): ";
  15. std::string filename;
  16. std::getline(std::cin, filename);
  17. // std::cout << "Enter file id: ";
  18. // std::string file_id_str;
  19. // std::getline(std::cin, file_id_str);
  20. // int file_id = 0;
  21. // try {
  22. // file_id = stoi(file_id_str);
  23. // } catch (std::invalid_argument) {
  24. // std::cout << "Invalid dat file id entered!\n\n";
  25. // continue;
  26. // }
  27. std::cout << "Opening file " << filename << std::endl;
  28. bool result = file.Init(filename); // TODO: INIT WITH DAT FILE ID
  29. if (!result) {
  30. std::cout << "Dat initialisation failed.\nPlease enter path to .dat file once more\n";
  31. }
  32. }
  33. }
  34. int main() {
  35. std::cout.precision(1);
  36. std::cout << std::fixed;
  37. std::cout << "Endevir's LotRO .dat patcher ver. 1.0.0" << std::endl;
  38. freopen("patcher_errors.log", "w", stderr);
  39. setbuf(stdout, nullptr);
  40. setbuf(stderr, nullptr);
  41. setvbuf (stdout, nullptr, _IONBF, BUFSIZ);
  42. setvbuf (stderr, nullptr, _IONBF, BUFSIZ);
  43. std::cout << "Hello! I'm a basic shell version of .dat file patcher. I can open .dat file directly, "
  44. "if you write path to it (with name of file) in file \"dat_file_path.txt\"\n";
  45. DatFile file(0);
  46. std::ifstream in("dat_file_path.txt");
  47. if (!in.fail()) {
  48. std::string filename;
  49. std::getline(in, filename);
  50. std::string file_id_str;
  51. std::getline(in, file_id_str);
  52. int file_id = 0;
  53. try {
  54. file_id = stoi(file_id_str);
  55. } catch (std::invalid_argument) {
  56. file_id = -1;
  57. std::cout << "Invalid file_id on second line of file dat_file_path.txt!\n\n";
  58. }
  59. if (file_id != -1) {
  60. std::cout << "Using .dat file from dat_file_path.txt...\n";
  61. std::cout << "Opening file " << filename << " with id = " << file_id << std::endl;
  62. bool result = file.Init(filename);
  63. if (!result) {
  64. std::cout << "Dat initialisation failed.\n";
  65. }
  66. }
  67. }
  68. InitializeFileLoop(file);
  69. std::cout << "Great! File initialised successfully!\n";
  70. while (true) {
  71. std::cout << "Please, choose, what should I do. I can patch datfile from database to .dat file (enter 1) or exit (enter -1)\n";
  72. std::cout << "Enter number of command (1 or -1): ";
  73. std::string command;
  74. std::getline(std::cin, command);
  75. int cmd = -10;
  76. try {
  77. cmd = stoi(command);
  78. } catch (std::invalid_argument) {
  79. std::cout << "Invalid command entered!\n\n";
  80. continue;
  81. }
  82. if (cmd == -1) {
  83. std::cout << "Exiting. Thanks for using me!\n";
  84. file.Deinit();
  85. break;
  86. }
  87. if (cmd == 1) {
  88. std::cout << "You've chosen to patch database! Write name of database file (it should be in the same "
  89. "directory), of enter 0 to return to main dialogue.\n";
  90. while (true) {
  91. std::cout << "Enter name of file or 0: ";
  92. std::string dbname;
  93. std::getline(std::cin, dbname);
  94. if (dbname == std::to_string(0)) {
  95. std::cout << "Okay, returning back...\n\n";
  96. break;
  97. }
  98. Database db;
  99. std::cout << "Opening database... " << dbname << std::endl;
  100. if (!db.InitDatabase(dbname)) {
  101. std::cout << "Unfortunately, I cannot open this database. Could you try again please?\n";
  102. continue;
  103. };
  104. if (db.CountRows() == 0) {
  105. std::cout << "There are no files in database or database doesn't exist. "
  106. "Please, try again!\n";
  107. continue;
  108. }
  109. std::cout << "There are " << db.CountRows() << " files in database." << std::endl;
  110. std::cout << "Successfully opened database! Beginning patching...\n";
  111. const clock_t begin_time = clock();
  112. file.PatchAllFilesFromDatabase(db);
  113. db.CloseDatabase();
  114. fprintf(stdout, "Spent %f seconds on patching! Thank you for your patience!\n",
  115. float(clock() - begin_time) / CLOCKS_PER_SEC);
  116. std::cout << "Great! File was patched successfully!\n\n";
  117. break;
  118. }
  119. }
  120. }
  121. system("pause");
  122. return 0;
  123. }