DatFile.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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.1.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. el::Loggers::addFlag(el::LoggingFlag::LogDetailedCrashReason);
  26. defaultConf.setToDefault();
  27. defaultConf.setGlobally(el::ConfigurationType::Format, "%datetime %level : %msg (function: %func)");
  28. defaultConf.setGlobally(el::ConfigurationType::ToFile, "true");
  29. defaultConf.setGlobally(el::ConfigurationType::Filename, "dat_library.log");
  30. defaultConf.setGlobally(el::ConfigurationType::ToStandardOutput, "false");
  31. defaultConf.setGlobally(el::ConfigurationType::PerformanceTracking, "true");
  32. defaultConf.setGlobally(el::ConfigurationType::MaxLogFileSize, "‭20971520‬"); // 20MB
  33. #ifndef NDEBUG
  34. defaultConf.set(el::Level::Debug, el::ConfigurationType::Enabled, "true");
  35. defaultConf.set(el::Level::Debug, el::ConfigurationType::Filename, "dat_library_debug.log");
  36. #endif
  37. el::Loggers::reconfigureAllLoggers(defaultConf);
  38. LOG(INFO) << "==================================================================";
  39. LOG(INFO) << "Starting new DatFile class instance";
  40. io_ = std::make_unique<DatIO>(this);
  41. fileSystem_ = std::make_unique<DatFileSystem>(this);
  42. localeManager_ = std::make_unique<DatLocaleManager>(this);
  43. exporter_ = std::make_unique<DatExporter>(this);
  44. patcher_ = std::make_unique<DatPatcher>(this);
  45. backupManager_ = std::make_unique<DatBackupManager>(this);
  46. status_ = std::make_unique<DatStatus>(this);
  47. }
  48. DatFile::~DatFile() {
  49. if (Initialized())
  50. Deinitialize();
  51. }
  52. LOTRO_DAT::DatLocaleManager &DatFile::GetLocaleManager() {
  53. return *localeManager_;
  54. }
  55. DatExporter &DatFile::GetExporter() {
  56. return *exporter_;
  57. }
  58. LOTRO_DAT::DatPatcher &DatFile::GetPatcher() {
  59. return *patcher_;
  60. }
  61. DatBackupManager &DatFile::GetBackupManager() {
  62. return *backupManager_;
  63. }
  64. DatIO &DatFile::GetIO() {
  65. return *io_;
  66. }
  67. DatFileSystem &DatFile::GetFileSystem() {
  68. return *fileSystem_;
  69. }
  70. DatStatus &DatFile::GetStatusModule() {
  71. return *status_;
  72. }
  73. DatOperationResult<> DatFile::Initialise(const std::string &filename, long long dat_id) {
  74. status_->SetStatus(DatStatus::E_INITIALISING);
  75. dat_id_ = dat_id;
  76. if (initialized_ && io_->GetFilename().result == SUCCESS && io_->GetFilename().value == filename)
  77. return DatOperationResult<>();
  78. initialized_ = false;
  79. auto operation = io_->Init(filename);
  80. if (operation.result != SUCCESS) {
  81. Deinitialize();
  82. status_->ClearAll();
  83. return DatOperationResult<>(ERROR, "DATINIT: Error, cannot initialize dat due to internal IO error");
  84. }
  85. operation = fileSystem_->Init();
  86. if (operation.result != SUCCESS) {
  87. Deinitialize();
  88. status_->ClearAll();
  89. return DatOperationResult<>(ERROR, "DATINIT: Error, cannot initialize dat due to filesystem parsing error");
  90. }
  91. operation = localeManager_->Init();
  92. if (operation.result != SUCCESS) {
  93. Deinitialize();
  94. status_->ClearAll();
  95. return DatOperationResult<>(ERROR, "DATINIT: Error, cannot initialize dat due to locale manager initialisation error");
  96. }
  97. initialized_ = true;
  98. status_->ClearAll();
  99. return DatOperationResult<>();
  100. }
  101. DatOperationResult<> DatFile::GatherInformation(const std::string &output_filename) {
  102. FILE *out = fopen(output_filename.c_str(), "w");
  103. if (!out)
  104. return DatOperationResult<>(ERROR, "GATHERDATINFO: Cannot open file " + output_filename);
  105. status_->SetStatus(DatStatus::E_GATHERING_INFO);
  106. fprintf(out, "########################################################################################\n"
  107. "# LOTRO Dat library version: %8s #\n"
  108. "# Author: Gi1dor (e1.gildor@gmail.com) #\n"
  109. "# Is part of LOTRO Legacy project (http://translate.lotros.ru/) #\n"
  110. "# Last version is available on http://git.gi1dor.ru/LotRO_Legacy/Universal_dat_library #\n"
  111. "########################################################################################\n\n\n",
  112. VERSION);
  113. io_->PrintInformaion(out);
  114. fileSystem_->PrintInformaion(out);
  115. localeManager_->PrintInformaion(out);
  116. fclose(out);
  117. status_->ClearAll();
  118. return DatOperationResult<>(SUCCESS);
  119. }
  120. DatOperationResult<> DatFile::Deinitialize() {
  121. status_->SetStatus(DatStatus::E_COMMITING);
  122. auto operation = localeManager_->DeInit();
  123. if (operation.result != SUCCESS) {
  124. status_->ClearAll();
  125. return DatOperationResult<>(ERROR, "DATDEINIT: Error, cannot deinitialize. msg: " + operation.msg);
  126. }
  127. operation = fileSystem_->DeInit();
  128. if (operation.result != SUCCESS) {
  129. status_->ClearAll();
  130. return DatOperationResult<>(ERROR, "DATDEINIT: Error, cannot deinitialize. msg: " + operation.msg);
  131. }
  132. operation = io_->DeInit();
  133. if (operation.result != SUCCESS) {
  134. status_->ClearAll();
  135. return DatOperationResult<>(ERROR, "DATDEINIT: Error, cannot deinitialize. msg: " + operation.msg);
  136. }
  137. initialized_ = false;
  138. status_->ClearAll();
  139. return DatOperationResult<>();
  140. }
  141. bool DatFile::Initialized() {
  142. return initialized_;
  143. }
  144. long long DatFile::GetDatID() {
  145. return dat_id_;
  146. }
  147. }
  148. }