DatFile.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. //
  2. // Created by Иван_Архипов on 31.10.2017.
  3. //
  4. #define NOMINMAX
  5. #include <LotroDat.h>
  6. #include <DatFile.h>
  7. #include <DatOperationResult.h>
  8. #include <EasyLogging++/easylogging++.h>
  9. #ifdef WIN32
  10. #define fseek _fseeki64
  11. #define ftell _ftelli64
  12. #endif
  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::MaxLogFileSize, "15728640"); // 15MB
  31. #ifndef NDEBUG
  32. defaultConf.set(el::Level::Debug, el::ConfigurationType::Enabled, "true");
  33. defaultConf.set(el::Level::Debug, el::ConfigurationType::Filename, "dat_library_debug.log");
  34. #elif NDEBUG
  35. defaultConf.set(el::Level::Debug, el::ConfigurationType::Enabled, "false");
  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_->SetDefaultStatus();
  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_->SetDefaultStatus();
  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_->SetDefaultStatus();
  95. return DatOperationResult<>(ERROR, "DATINIT: Error, cannot initialize dat due to locale manager initialisation error");
  96. }
  97. initialized_ = true;
  98. status_->SetDefaultStatus();
  99. LOG(INFO) << "DATINIT: All initialization processes finished successfully";
  100. return DatOperationResult<>();
  101. }
  102. DatOperationResult<> DatFile::GatherInformation(const std::string &output_filename) {
  103. FILE *out = fopen(output_filename.c_str(), "w");
  104. if (!out)
  105. return DatOperationResult<>(ERROR, "GATHERDATINFO: Cannot open file " + output_filename);
  106. status_->SetStatus(DatStatus::E_GATHERING_INFO);
  107. fprintf(out, "########################################################################################\n"
  108. "# LOTRO Dat library version: %8s #\n"
  109. "# Author: Endevir (aka Gi1dor) (me@endevir.ru) #\n"
  110. "# Is part of LOTRO Legacy project (http://translate.lotros.ru/) #\n"
  111. "# Last version is available on http://git.endevir.ru/LotRO_Legacy/LotroDat #\n"
  112. "########################################################################################\n\n\n",
  113. LOTRO_DAT_VERSION);
  114. io_->PrintInformaion(out);
  115. fileSystem_->PrintInformaion(out);
  116. localeManager_->PrintInformaion(out);
  117. fclose(out);
  118. status_->SetDefaultStatus();
  119. return DatOperationResult<>(SUCCESS);
  120. }
  121. DatOperationResult<> DatFile::Deinitialize() {
  122. status_->SetStatus(DatStatus::E_COMMITING);
  123. auto operation = localeManager_->DeInit();
  124. if (operation.result != SUCCESS) {
  125. status_->SetDefaultStatus();
  126. return DatOperationResult<>(ERROR, "DATDEINIT: Error, cannot deinitialize. msg: " + operation.msg);
  127. }
  128. operation = fileSystem_->DeInit();
  129. if (operation.result != SUCCESS) {
  130. status_->SetDefaultStatus();
  131. return DatOperationResult<>(ERROR, "DATDEINIT: Error, cannot deinitialize. msg: " + operation.msg);
  132. }
  133. operation = io_->DeInit();
  134. if (operation.result != SUCCESS) {
  135. status_->SetDefaultStatus();
  136. return DatOperationResult<>(ERROR, "DATDEINIT: Error, cannot deinitialize. msg: " + operation.msg);
  137. }
  138. initialized_ = false;
  139. status_->SetDefaultStatus();
  140. return DatOperationResult<>();
  141. }
  142. bool DatFile::Initialized() {
  143. return initialized_;
  144. }
  145. long long DatFile::GetDatID() {
  146. return dat_id_;
  147. }
  148. }
  149. }