SubDirectory.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. //
  2. // Created by Иван_Архипов on 07.11.2017.
  3. //
  4. #include "SubDirectory.h"
  5. #include "DatFile.h"
  6. #include "Common/DatException.h"
  7. #include "Subfile.h"
  8. #include "BinaryData.h"
  9. #include "Subfiles/TextSubfile.h"
  10. #include "Subfiles/DdsSubfile.h"
  11. #include "Subfiles/FontSubFile.h"
  12. #include "Subfiles/JpgSubfile.h"
  13. #include "Subfiles/OggSubfile.h"
  14. #include "Subfiles/WavSubfile.h"
  15. #include "Subfiles/UnknownSubfile.h"
  16. namespace LOTRO_DAT {
  17. SubDirectory::SubDirectory() {
  18. offset_ = 0;
  19. }
  20. SubDirectory::~SubDirectory() {
  21. for (Subfile* file : subfiles_)
  22. delete file;
  23. for (SubDirectory *directory : subdirs_)
  24. delete directory;
  25. }
  26. SubDirectory::SubDirectory(long long offset, DatFile *dat, long long max_subdirs) :
  27. dat_(dat), offset_(offset), max_subdirs_(max_subdirs) {
  28. try {
  29. MakeSubDirectories();
  30. } catch (std::exception &e) {
  31. fprintf(stderr, "Caught %s exception.", e.what());
  32. fprintf(stderr, "Unable to initialize directory at offset %lld. Initializing it as empty directory...\n",
  33. offset);
  34. subdirs_.clear();
  35. subfiles_.clear();
  36. return;
  37. }
  38. try {
  39. MakeSubFiles();
  40. } catch (std::exception &e) {
  41. fprintf(stderr, "Caught %s exception.", e.what());
  42. fprintf(stderr, "Unable to initialize directory at offset %lld. Initializing it as empty directory...\n",
  43. offset);
  44. subdirs_.clear();
  45. subfiles_.clear();
  46. return;
  47. }
  48. }
  49. void SubDirectory::MakeSubDirectories() {
  50. BinaryData data(1024);
  51. dat_->ReadData(data, 63 * 8, offset_);
  52. if (data.ToNumber<4>(0) != 0 || data.ToNumber<4>(4) != 0) {
  53. std::string err =
  54. std::string("Bad SubDirectory::MakeSubDirectories - first 8 bytes are not equal to 0 at offset ")
  55. + std::to_string(offset_);
  56. throw DatException(err.c_str(), SUBDIR_EXCEPTION);
  57. }
  58. for (unsigned int i = 8; i < 63 * 8; i += 8) {
  59. if (data.ToNumber<4>(i) == 0 || data.ToNumber<4>(i + 4) == 0)
  60. break;
  61. try {
  62. SubDirectory *subdir = new SubDirectory(data.ToNumber<4>(i + 4), dat_);
  63. subdirs_.push_back(subdir);
  64. } catch (std::exception &e) {
  65. fprintf(stderr, "Caught %s exception.", e.what());
  66. fprintf(stderr, "Making SubDirectory at offset %lld failed, continuing\n", data.ToNumber<4>(i + 4));
  67. }
  68. }
  69. }
  70. void SubDirectory::MakeSubFiles() {
  71. BinaryData data(64 * 32);
  72. dat_->ReadData(data, 64 * 32, offset_ + 63 * 8);
  73. for (unsigned int i = 0; i < 61 * 32; i += 32) {
  74. if (data.ToNumber<4>(i + 8) < 0x32 || data.ToNumber<4>(i + 12) < 0x32)
  75. continue;
  76. BinaryData mfile_id(4);
  77. dat_->ReadData(mfile_id, 4, data.ToNumber<4>(i + 12) + 8);
  78. if (data.ToNumber<4>(i + 8) != mfile_id.ToNumber<4>(0)) {
  79. continue;
  80. }
  81. subfiles_.push_back(
  82. MakeSubfile(
  83. offset_ + 63 * 8 + i,
  84. data.ToNumber<4>(i), // fragments_count
  85. data.ToNumber<4>(i + 4), // unknown1
  86. data.ToNumber<4>(i + 8), // file_id
  87. data.ToNumber<4>(i + 12), // file_offset
  88. data.ToNumber<4>(i + 16), // block_size
  89. data.ToNumber<4>(i + 20), // timestamp
  90. data.ToNumber<4>(i + 24), // version
  91. data.ToNumber<4>(i + 28) // block_size
  92. )
  93. );
  94. }
  95. }
  96. void SubDirectory::MakeDictionary(std::unordered_map<long long, Subfile *> &dict) {
  97. for (Subfile *i : subfiles_) {
  98. if (dict.count(i->file_id()) == 0 || dict[i->file_id()]->timestamp() < i->timestamp())
  99. dict[i->file_id()] = i;
  100. }
  101. for (SubDirectory *i : subdirs_)
  102. i->MakeDictionary(dict);
  103. }
  104. void SubDirectory::UpdateDirectories(std::unordered_map<long long, BinaryData *> &patched_files, std::unordered_map<long long, Subfile*> &dict) {
  105. for (unsigned i = 0; i < subfiles_.size(); i++) {
  106. long long file_id = subfiles_[i]->file_id();
  107. if (patched_files.count(file_id) != 0) {
  108. BinaryData data(32);
  109. dat_->ReadData(data, 32, subfiles_[i]->dictionary_offset());
  110. if (data != *patched_files[file_id])
  111. continue;
  112. BinaryData header = dict[subfiles_[i]->file_id()]->MakeHeaderData();
  113. BinaryData write_data(4);
  114. write_data.FromNumber<4>(dict[file_id]->file_offset());
  115. dat_->WriteData(write_data, 4, subfiles_[i]->dictionary_offset() + 12);
  116. write_data.FromNumber<4>(dict[file_id]->file_size());
  117. dat_->WriteData(write_data, 4, subfiles_[i]->dictionary_offset() + 16);
  118. write_data.FromNumber<4>(dict[file_id]->block_size());
  119. dat_->WriteData(write_data, 4, subfiles_[i]->dictionary_offset() + 28);
  120. }
  121. }
  122. for (SubDirectory *i : subdirs_)
  123. i->UpdateDirectories(patched_files, dict);
  124. }
  125. Subfile *SubDirectory::MakeSubfile(long long dictionary_offset, long long fragments_count, long long unknown1, long long file_id,
  126. long long file_offset, long long file_size, long long timestamp,
  127. long long version, long long block_size) {
  128. FILE_TYPE type = GetSubfileType(file_id, file_offset);
  129. switch (type) {
  130. case TEXT:
  131. return dynamic_cast<Subfile *>(new TextSubfile(dat_, dictionary_offset, fragments_count, unknown1, file_id, file_offset, file_size, timestamp, version, block_size));
  132. case JPG:
  133. return dynamic_cast<Subfile *>(new JpgSubfile(dat_, dictionary_offset, fragments_count, unknown1, file_id, file_offset, file_size, timestamp, version, block_size));
  134. case DDS:
  135. return dynamic_cast<Subfile *>(new DdsSubfile(dat_, dictionary_offset, fragments_count, unknown1, file_id, file_offset, file_size, timestamp, version, block_size));
  136. case WAV:
  137. return dynamic_cast<Subfile *>(new WavSubfile(dat_, dictionary_offset, fragments_count, unknown1, file_id, file_offset, file_size, timestamp, version, block_size));
  138. case OGG:
  139. return dynamic_cast<Subfile *>(new OggSubfile(dat_, dictionary_offset, fragments_count, unknown1, file_id, file_offset, file_size, timestamp, version, block_size));
  140. case FONT:
  141. return dynamic_cast<Subfile *>(new FontSubfile(dat_, dictionary_offset, fragments_count, unknown1, file_id, file_offset, file_size, timestamp, version, block_size));
  142. case UNKNOWN:
  143. return dynamic_cast<Subfile *>(new UnknownSubfile(dat_, dictionary_offset, fragments_count, unknown1, file_id, file_offset, file_size, timestamp, version, block_size));
  144. }
  145. throw DatException("Bad SubDirectory::MakeSubfile() - unable to recognize file_type!", SUBFILE_EXCEPTION);
  146. }
  147. FILE_TYPE SubDirectory::GetSubfileType(long long file_id, long long file_offset) const {
  148. // Text check based on file_id
  149. if ((file_id >> 24ll) == 0x25ll)
  150. return TEXT;
  151. // Font check based on file_id
  152. if ((file_id >> 24ll) == 0x42ll)
  153. return FONT;
  154. BinaryData header(64);
  155. try {
  156. dat_->ReadData(header, 64, (unsigned) file_offset + 8);
  157. }catch (DatException &e) {
  158. if (e.type() == READ_EXCEPTION) {
  159. std::string err =
  160. "Bad Subfile::getExtension() - unable to read header of file with id = " +
  161. std::to_string(file_id) +
  162. " and offset = " + std::to_string(file_offset);
  163. throw DatException(err.c_str(), SUBFILE_EXCEPTION);
  164. } else
  165. throw e;
  166. }
  167. // jpeg / dds check
  168. if ((file_id >> 24ll) == 0x41ll) {
  169. long long soi = header.ToNumber<2>(24);
  170. long long marker = header.ToNumber<2>(26);
  171. //auto markerSize = header.ToNumber<short>(28);
  172. //auto four = header.ToNumber<int>(30);
  173. if ((soi == 0xD8FFll && marker == 0xE0FFll) || marker == 0xE1FFll)
  174. return JPG;
  175. return DDS;
  176. }
  177. // Ogg and Wav check
  178. if (header[8] == 0x4F && header[9] == 0x67 && header[10] == 0x67 && header[11] == 0x53)
  179. return OGG;
  180. if (header[8] == 0x52 && header[9] == 0x49 && header[10] == 0x46 && header[11] == 0x46)
  181. return WAV;
  182. return UNKNOWN;
  183. }
  184. };