DatFile.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. 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. auto operation = io_->Init(filename);
  76. if (operation.result != SUCCESS) {
  77. status_->ClearAll();
  78. return DatOperationResult<>(ERROR, "DATINIT: Error, cannot initialize dat");
  79. }
  80. operation = fileSystem_->Init();
  81. if (operation.result != SUCCESS) {
  82. status_->ClearAll();
  83. return DatOperationResult<>(ERROR, "DATINIT: Error, cannot initialize dat");
  84. }
  85. operation = localeManager_->Init();
  86. if (operation.result != SUCCESS) {
  87. status_->ClearAll();
  88. return DatOperationResult<>(ERROR, "DATINIT: Error, cannot initialize dat");
  89. }
  90. initialized_ = true;
  91. status_->ClearAll();
  92. return DatOperationResult<>();
  93. }
  94. DatOperationResult<> DatFile::GatherInformation(const std::string &output_filename) {
  95. FILE *out = fopen(output_filename.c_str(), "w");
  96. if (!out)
  97. return DatOperationResult<>(ERROR, "GATHERDATINFO: Cannot open file " + output_filename);
  98. status_->SetStatus(DatStatus::E_GATHERING_INFO);
  99. fprintf(out, "########################################################################################\n"
  100. "# LOTRO Dat library version: %8s #\n"
  101. "# Author: Gi1dor (e1.gildor@gmail.com) #\n"
  102. "# Is part of LOTRO Legacy project (http://translate.lotros.ru/) #\n"
  103. "# Last version is available on http://git.gi1dor.ru/LotRO_Legacy/Universal_dat_library #\n"
  104. "########################################################################################\n\n\n",
  105. VERSION);
  106. io_->PrintInformaion(out);
  107. fileSystem_->PrintInformaion(out);
  108. localeManager_->PrintInformaion(out);
  109. fclose(out);
  110. status_->ClearAll();
  111. return DatOperationResult<>(SUCCESS);
  112. }
  113. DatOperationResult<> DatFile::Deinitialize() {
  114. if (!initialized_)
  115. return DatOperationResult<>();
  116. status_->SetStatus(DatStatus::E_COMMITING);
  117. auto operation = localeManager_->DeInit();
  118. if (operation.result != SUCCESS) {
  119. status_->ClearAll();
  120. return DatOperationResult<>(ERROR, "DATDEINIT: Error, cannot deinitialize. msg: " + operation.msg);
  121. }
  122. operation = fileSystem_->DeInit();
  123. if (operation.result != SUCCESS) {
  124. status_->ClearAll();
  125. return DatOperationResult<>(ERROR, "DATDEINIT: Error, cannot deinitialize. msg: " + operation.msg);
  126. }
  127. operation = io_->DeInit();
  128. if (operation.result != SUCCESS) {
  129. status_->ClearAll();
  130. return DatOperationResult<>(ERROR, "DATDEINIT: Error, cannot deinitialize. msg: " + operation.msg);
  131. }
  132. initialized_ = false;
  133. status_->ClearAll();
  134. return DatOperationResult<>();
  135. }
  136. bool DatFile::Initialized() {
  137. return initialized_;
  138. }
  139. long long DatFile::GetDatID() {
  140. return dat_id_;
  141. }
  142. }
  143. }