DatFile.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. 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. status_ = std::make_unique<DatStatus>(this);
  44. }
  45. DatFile::~DatFile() {
  46. if (Initialized())
  47. Deinitialize();
  48. }
  49. LOTRO_DAT::DatLocaleManager &DatFile::GetLocaleManager() {
  50. return *localeManager_;
  51. }
  52. DatExporter &DatFile::GetExporter() {
  53. return *exporter_;
  54. }
  55. LOTRO_DAT::DatPatcher &DatFile::GetPatcher() {
  56. return *patcher_;
  57. }
  58. DatBackupManager &DatFile::GetBackupManager() {
  59. return *backupManager_;
  60. }
  61. DatIO &DatFile::GetIO() {
  62. return *io_;
  63. }
  64. DatFileSystem &DatFile::GetFileSystem() {
  65. return *fileSystem_;
  66. }
  67. DatStatus &DatFile::GetStatusModule() {
  68. return *status_;
  69. }
  70. DatOperationResult<> DatFile::Initialise(const std::string &filename, long long dat_id) {
  71. status_->SetStatus(DatStatus::E_INITIALISING);
  72. dat_id_ = dat_id;
  73. if (initialized_ && io_->GetFilename().result == SUCCESS && io_->GetFilename().value == filename)
  74. return DatOperationResult<>();
  75. initialized_ = false;
  76. auto operation = io_->Init(filename);
  77. if (operation.result != SUCCESS) {
  78. Deinitialize();
  79. status_->ClearAll();
  80. return DatOperationResult<>(ERROR, "DATINIT: Error, cannot initialize dat due to internal IO error");
  81. }
  82. operation = fileSystem_->Init();
  83. if (operation.result != SUCCESS) {
  84. Deinitialize();
  85. status_->ClearAll();
  86. return DatOperationResult<>(ERROR, "DATINIT: Error, cannot initialize dat due to filesystem parsing error");
  87. }
  88. operation = localeManager_->Init();
  89. if (operation.result != SUCCESS) {
  90. Deinitialize();
  91. status_->ClearAll();
  92. return DatOperationResult<>(ERROR, "DATINIT: Error, cannot initialize dat due to locale manager initialisation error");
  93. }
  94. initialized_ = true;
  95. status_->ClearAll();
  96. return DatOperationResult<>();
  97. }
  98. DatOperationResult<> DatFile::GatherInformation(const std::string &output_filename) {
  99. FILE *out = fopen(output_filename.c_str(), "w");
  100. if (!out)
  101. return DatOperationResult<>(ERROR, "GATHERDATINFO: Cannot open file " + output_filename);
  102. status_->SetStatus(DatStatus::E_GATHERING_INFO);
  103. fprintf(out, "########################################################################################\n"
  104. "# LOTRO Dat library version: %8s #\n"
  105. "# Author: Gi1dor (e1.gildor@gmail.com) #\n"
  106. "# Is part of LOTRO Legacy project (http://translate.lotros.ru/) #\n"
  107. "# Last version is available on http://git.gi1dor.ru/LotRO_Legacy/Universal_dat_library #\n"
  108. "########################################################################################\n\n\n",
  109. VERSION);
  110. io_->PrintInformaion(out);
  111. fileSystem_->PrintInformaion(out);
  112. localeManager_->PrintInformaion(out);
  113. fclose(out);
  114. status_->ClearAll();
  115. return DatOperationResult<>(SUCCESS);
  116. }
  117. DatOperationResult<> DatFile::Deinitialize() {
  118. status_->SetStatus(DatStatus::E_COMMITING);
  119. auto operation = localeManager_->DeInit();
  120. if (operation.result != SUCCESS) {
  121. status_->ClearAll();
  122. return DatOperationResult<>(ERROR, "DATDEINIT: Error, cannot deinitialize. msg: " + operation.msg);
  123. }
  124. operation = fileSystem_->DeInit();
  125. if (operation.result != SUCCESS) {
  126. status_->ClearAll();
  127. return DatOperationResult<>(ERROR, "DATDEINIT: Error, cannot deinitialize. msg: " + operation.msg);
  128. }
  129. operation = io_->DeInit();
  130. if (operation.result != SUCCESS) {
  131. status_->ClearAll();
  132. return DatOperationResult<>(ERROR, "DATDEINIT: Error, cannot deinitialize. msg: " + operation.msg);
  133. }
  134. initialized_ = false;
  135. status_->ClearAll();
  136. return DatOperationResult<>();
  137. }
  138. bool DatFile::Initialized() {
  139. return initialized_;
  140. }
  141. long long DatFile::GetDatID() {
  142. return dat_id_;
  143. }
  144. }
  145. }