inserter.cpp 4.5 KB

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