DatFile.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. //
  2. // Created by Иван_Архипов on 31.10.2017.
  3. //
  4. #include "DatFile.h"
  5. #include "BinaryData.h"
  6. #include "DatException.h"
  7. #include "SubDirectory.h"
  8. #include "Subfile.h"
  9. #include <locale>
  10. extern "C++"
  11. {
  12. namespace LOTRO_DAT {
  13. DatFile::DatFile() {
  14. dat_state_ = CLOSED;
  15. }
  16. DatFile::DatFile(const char *filename) {
  17. dat_state_ = CLOSED;
  18. OpenDatFile(filename);
  19. ReadSuperBlock();
  20. MakeDirectories();
  21. MakeDictionary();
  22. if (dat_state_ == SUCCESS_DICTIONARY)
  23. dat_state_ = READY;
  24. else
  25. throw DatException("Bad DatFile initialization! Not all init states were successfully passed!",
  26. INIT_EXCEPTION);
  27. }
  28. DatFile::DatFile(const std::string &filename) {
  29. dat_state_ = CLOSED;
  30. OpenDatFile(filename.c_str());
  31. ReadSuperBlock();
  32. MakeDirectories();
  33. MakeDictionary();
  34. if (dat_state_ == SUCCESS_DICTIONARY)
  35. dat_state_ = READY;
  36. else
  37. throw DatException("Bad DatFile initialization! Not all init states were successfully passed!",
  38. INIT_EXCEPTION);
  39. }
  40. DatFile::~DatFile() {
  41. if (file_handler_ != nullptr)
  42. fclose(file_handler_);
  43. delete file_handler_;
  44. delete root_directory_;
  45. }
  46. void DatFile::OpenDatFile(const char *dat_name) {
  47. if (dat_state_ != CLOSED)
  48. throw DatException("Bad initialisation of DatFile - current DatFile isn't in correct state!",
  49. INIT_EXCEPTION);
  50. fopen_s(&file_handler_, dat_name, "r+b");
  51. if (file_handler_ == nullptr) {
  52. std::string err = "Bad DatFile::OpenDatFile. Unable to open file ";
  53. err += dat_name;
  54. throw DatException(err.c_str(), INIT_EXCEPTION);
  55. }
  56. _fseeki64(file_handler_, 0, SEEK_END);
  57. file_size_ = _ftelli64(file_handler_);
  58. _fseeki64(file_handler_, 0, SEEK_SET);
  59. dat_state_ = SUCCESS_OPENED;
  60. }
  61. void DatFile::ReadSuperBlock() {
  62. if (dat_state_ != SUCCESS_OPENED)
  63. throw DatException("Bad DatFile::ReadSuperBlock() - DatFile isn't in valid state!", INIT_EXCEPTION);
  64. BinaryData data(1024);
  65. ReadData(data, 1024);
  66. constant1_ = data.ToNumber<4>(0x100);
  67. constant2_ = data.ToNumber<4>(0x140);
  68. version1_ = data.ToNumber<4>(0x14C);
  69. version2_ = data.ToNumber<4>(0x150);
  70. root_directory_offset_ = data.ToNumber<4>(0x160);
  71. auto size1 = data.ToNumber<4>(0x148);
  72. if (constant1_ != 0x4C5000)
  73. throw DatException(
  74. "Bad DatFile::ReadSuperBlock - variable at position 0x100 is not equal to .dat file constant!",
  75. INIT_EXCEPTION);
  76. if (constant2_ != 0x5442)
  77. throw DatException(
  78. "Bad DatFile::ReadSuperBlock - variable at position 0x140 is not equal to .dat file constant!",
  79. INIT_EXCEPTION);
  80. if (file_size_ != size1)
  81. throw DatException(
  82. "Bad DatFile::ReadSuperBlock - variable at 0x148 position is not equal to .dat file size!",
  83. INIT_EXCEPTION);
  84. dat_state_ = SUCCESS_SUPERBLOCK;
  85. }
  86. void DatFile::MakeDirectories() {
  87. if (dat_state_ != SUCCESS_SUPERBLOCK)
  88. throw DatException("Bad DatFile::MakeDirectories() - DatFile isn't in valid state!", INIT_EXCEPTION);
  89. root_directory_ = new SubDirectory((unsigned) root_directory_offset_, this);
  90. dat_state_ = SUCCESS_DIRECTORIES;
  91. }
  92. void DatFile::MakeDictionary() {
  93. if (dat_state_ != SUCCESS_DIRECTORIES)
  94. throw DatException("Bad DatFile::MakeDictionary() - DatFile isn't in valid state!", INIT_EXCEPTION);
  95. root_directory_->MakeDictionary(dictionary_);
  96. dat_state_ = SUCCESS_DICTIONARY;
  97. }
  98. void DatFile::ReadData(BinaryData &data, long long size, long long offset, long long data_offset) {
  99. if (dat_state_ == CLOSED)
  100. throw DatException("Bad DatFile::ReadData() - DatFile isn't in valid state!", READ_EXCEPTION);
  101. if (data_offset + size > data.size()) {
  102. std::string err = "Bad DatFile::ReadData - trying to read more than BinaryData size\n";
  103. err += std::string("Reading ") + std::to_string(size) + std::string(" bytes from ")
  104. + std::to_string(offset) + std::string(" position in dat file.");
  105. throw DatException(err.c_str(), READ_EXCEPTION);
  106. }
  107. if (offset + size > file_size()) {
  108. std::string err = "Bad DatFile::ReadData - trying to read more than DatFile size elapsed\n";
  109. err += std::string("Reading ") + std::to_string(size) + std::string(" bytes from ")
  110. + std::to_string(offset) + std::string(" position in dat file.");
  111. throw DatException(err.c_str(), READ_EXCEPTION);
  112. }
  113. _fseeki64(file_handler_, offset, SEEK_SET);
  114. fread(data.data() + data_offset, size, 1, file_handler_);
  115. data.CheckCompression();
  116. }
  117. void DatFile::WriteData(const BinaryData &data, long long size, long long offset, long long data_offset) {
  118. if (dat_state_ != READY)
  119. throw DatException("Bad DatFile::WriteData() - DatFile isn't in valid state!", WRITE_EXCEPTION);
  120. _fseeki64(file_handler_, offset, SEEK_SET);
  121. if (data_offset + size > data.size())
  122. throw DatException("Bad DatFile::WriteData - trying to write more than BinaryData size", WRITE_EXCEPTION);
  123. fwrite(data.data() + data_offset, size, 1, file_handler_);
  124. }
  125. long long DatFile::constant1() const {
  126. return constant1_;
  127. }
  128. long long DatFile::constant2() const {
  129. return constant2_;
  130. }
  131. long long DatFile::file_size() const {
  132. return file_size_;
  133. }
  134. long long DatFile::version1() const {
  135. return version1_;
  136. }
  137. long long DatFile::version2() const {
  138. return version2_;
  139. }
  140. const std::unordered_map<long long, Subfile *> &DatFile::dictionary() {
  141. return dictionary_;
  142. }
  143. }
  144. }