DatFile.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. //
  2. // Created by Иван_Архипов on 31.10.2017.
  3. //
  4. #define NOMINMAX
  5. #include <DatFile.h>
  6. #include <DatOperationResult.h>
  7. #include <EasyLogging++/easylogging++.h>
  8. #ifdef WIN32
  9. #define fseek _fseeki64
  10. #define ftell _ftelli64
  11. #endif
  12. #define VERSION "7.0.0"
  13. INITIALIZE_EASYLOGGINGPP
  14. extern "C++"
  15. {
  16. namespace LOTRO_DAT {
  17. //------------------------------------------------//
  18. // INIT SECTION
  19. //------------------------------------------------//
  20. DatFile::DatFile() {
  21. el::Configurations defaultConf;
  22. el::Loggers::addFlag(el::LoggingFlag::LogDetailedCrashReason);
  23. el::Loggers::addFlag(el::LoggingFlag::ImmediateFlush);
  24. el::Loggers::addFlag(el::LoggingFlag::StrictLogFileSizeCheck);
  25. defaultConf.setToDefault();
  26. defaultConf.setGlobally(el::ConfigurationType::Format, "%datetime %level : %msg (function: %func)");
  27. defaultConf.setGlobally(el::ConfigurationType::ToFile, "true");
  28. defaultConf.setGlobally(el::ConfigurationType::Filename, "dat_library.log");
  29. defaultConf.setGlobally(el::ConfigurationType::ToStandardOutput, "false");
  30. defaultConf.setGlobally(el::ConfigurationType::PerformanceTracking, "true");
  31. defaultConf.setGlobally(el::ConfigurationType::MaxLogFileSize, "5242880"); // 5MB
  32. defaultConf.set(el::Level::Debug, el::ConfigurationType::Enabled, "false");
  33. defaultConf.set(el::Level::Debug, el::ConfigurationType::Filename, "dat_library_debug.log");
  34. el::Loggers::reconfigureAllLoggers(defaultConf);
  35. LOG(INFO) << "==================================================================";
  36. LOG(INFO) << "Starting new DatFile class instance";
  37. io_ = std::make_unique<DatIO>(this);
  38. fileSystem_ = std::make_unique<DatFileSystem>(this);
  39. localeManager_ = std::make_unique<DatLocaleManager>(this);
  40. exporter_ = std::make_unique<DatExporter>(this);
  41. patcher_ = std::make_unique<DatPatcher>(this);
  42. // backupManager_ = std::make_unique<DatBackupManager>(this);
  43. }
  44. DatFile::~DatFile() {
  45. if (Initialized())
  46. Deinitialize();
  47. }
  48. LOTRO_DAT::DatLocaleManager &DatFile::getLocaleManager() {
  49. return *localeManager_;
  50. }
  51. DatExporter &DatFile::getExporter() {
  52. return *exporter_;
  53. }
  54. LOTRO_DAT::DatPatcher &DatFile::getPatcher() {
  55. return *patcher_;
  56. }
  57. DatBackupManager &DatFile::getBackupManager() {
  58. return *backupManager_;
  59. }
  60. DatIO &DatFile::getIO() {
  61. return *io_;
  62. }
  63. DatFileSystem &DatFile::getFileSystem() {
  64. return *fileSystem_;
  65. }
  66. DatOperationResult<> DatFile::Initialise(const std::string &filename, long long dat_id) {
  67. dat_id_ = dat_id;
  68. if (initialized_ && io_->GetFilename().result == SUCCESS && io_->GetFilename().value == filename)
  69. return DatOperationResult<>();
  70. auto operation = io_->Init(filename);
  71. if (operation.result != SUCCESS)
  72. return DatOperationResult<>(ERROR, "DATINIT: Error, cannot initialize dat");
  73. operation = fileSystem_->Init();
  74. if (operation.result != SUCCESS)
  75. return DatOperationResult<>(ERROR, "DATINIT: Error, cannot initialize dat");
  76. operation = localeManager_->Init();
  77. if (operation.result != SUCCESS)
  78. return DatOperationResult<>(ERROR, "DATINIT: Error, cannot initialize dat");
  79. initialized_ = true;
  80. return DatOperationResult<>();
  81. }
  82. DatOperationResult<> DatFile::GatherInformation(const std::string &output_filename) {
  83. FILE *out = fopen(output_filename.c_str(), "w");
  84. if (!out)
  85. return DatOperationResult<>(ERROR, "GATHERDATINFO: Cannot open file " + output_filename);
  86. fprintf(out, "########################################################################################\n"
  87. "# LOTRO Dat library version: %8s #\n"
  88. "# Author: Gi1dor (e1.gildor@gmail.com) #\n"
  89. "# Is part of LOTRO Legacy project (http://translate.lotros.ru/) #\n"
  90. "# Last version is available on http://git.gi1dor.ru/LotRO_Legacy/Universal_dat_library #\n"
  91. "########################################################################################\n\n\n",
  92. VERSION);
  93. io_->PrintInformaion(out);
  94. fileSystem_->PrintInformaion(out);
  95. localeManager_->PrintInformaion(out);
  96. fclose(out);
  97. return DatOperationResult<>(SUCCESS);
  98. }
  99. DatOperationResult<> DatFile::Deinitialize() {
  100. if (!initialized_)
  101. return DatOperationResult<>();
  102. auto operation = localeManager_->DeInit();
  103. if (operation.result != SUCCESS)
  104. return DatOperationResult<>(ERROR, "DATDEINIT: Error, cannot deinitialize. msg: " + operation.msg);
  105. operation = fileSystem_->DeInit();
  106. if (operation.result != SUCCESS)
  107. return DatOperationResult<>(ERROR, "DATDEINIT: Error, cannot deinitialize. msg: " + operation.msg);
  108. operation = io_->DeInit();
  109. if (operation.result != SUCCESS)
  110. return DatOperationResult<>(ERROR, "DATDEINIT: Error, cannot deinitialize. msg: " + operation.msg);
  111. initialized_ = false;
  112. return DatOperationResult<>();
  113. }
  114. bool DatFile::Initialized() {
  115. return initialized_;
  116. }
  117. }
  118. }