DatFile.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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::removeFlag(el::LoggingFlag::CreateLoggerAutomatically);
  23. el::Loggers::addFlag(el::LoggingFlag::NewLineForContainer);
  24. el::Loggers::addFlag(el::LoggingFlag::LogDetailedCrashReason);
  25. el::Loggers::addFlag(el::LoggingFlag::ImmediateFlush);
  26. el::Loggers::addFlag(el::LoggingFlag::StrictLogFileSizeCheck);
  27. el::Loggers::addFlag(el::LoggingFlag::HierarchicalLogging);
  28. defaultConf.setToDefault();
  29. defaultConf.setGlobally(el::ConfigurationType::Format, "%datetime %level : %msg (function: %func)");
  30. defaultConf.setGlobally(el::ConfigurationType::ToFile, "true");
  31. defaultConf.setGlobally(el::ConfigurationType::Filename, "dat_library.log");
  32. defaultConf.setGlobally(el::ConfigurationType::ToStandardOutput, "false");
  33. defaultConf.setGlobally(el::ConfigurationType::PerformanceTracking, "true");
  34. defaultConf.setGlobally(el::ConfigurationType::MaxLogFileSize, "5242880"); // 5MB
  35. defaultConf.set(el::Level::Debug, el::ConfigurationType::Enabled, "false");
  36. defaultConf.set(el::Level::Debug, el::ConfigurationType::Filename, "logs/dat_library_debug.log");
  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. }
  47. LOTRO_DAT::DatLocaleManager &DatFile::getLocaleManager() {
  48. return *localeManager_;
  49. }
  50. DatExporter &DatFile::getExporter() {
  51. return *exporter_;
  52. }
  53. //
  54. // LOTRO_DAT::DatPatcher &DatFile::getPatcher() {
  55. // return *patcher_;
  56. // }
  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. DatOperationResult<> DatFile::Initialise(const std::string &filename, long long dat_id) {
  68. dat_id_ = dat_id;
  69. if (initialized_ && io_->GetFilename().result == SUCCESS && io_->GetFilename().value == filename)
  70. return DatOperationResult<>();
  71. auto operation = io_->Init(filename);
  72. if (operation.result != SUCCESS)
  73. return DatOperationResult<>(ERROR, "DATINIT: Error, cannot initialize. msg: " + operation.msg);
  74. operation = fileSystem_->Init();
  75. if (operation.result != SUCCESS)
  76. return DatOperationResult<>(ERROR, "DATINIT: Error, cannot initialize. msg: " + operation.msg);
  77. operation = localeManager_->Init();
  78. if (operation.result != SUCCESS)
  79. return DatOperationResult<>(ERROR, "DATINIT: Error, cannot initialize. msg: " + operation.msg);
  80. initialized_ = true;
  81. return DatOperationResult<>();
  82. }
  83. DatOperationResult<> DatFile::GatherInformation(const std::string &output_filename) {
  84. FILE *out = fopen(output_filename.c_str(), "w");
  85. if (!out)
  86. return DatOperationResult<>(ERROR, "GATHERDATINFO: Cannot open file " + output_filename);
  87. fprintf(out, "########################################################################################\n"
  88. "# LOTRO Dat library version: %8s #\n"
  89. "# Author: Gi1dor (e1.gildor@gmail.com) #\n"
  90. "# Is part of LOTRO Legacy project (http://translate.lotros.ru/) #\n"
  91. "# Last version is available on http://git.gi1dor.ru/LotRO_Legacy/Universal_dat_library #\n"
  92. "########################################################################################\n\n\n",
  93. VERSION);
  94. io_->PrintInformaion(out);
  95. fileSystem_->PrintInformaion(out);
  96. localeManager_->PrintInformaion(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. return DatOperationResult<>();
  112. }
  113. bool DatFile::Initialized() {
  114. return initialized_;
  115. }
  116. }
  117. }