SubFile.cpp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. //
  2. // Created by Иван_Архипов on 01.11.2017.
  3. //
  4. #include "SubFile.h"
  5. #include "BinaryData.h"
  6. #include "DatFile.h"
  7. #include "SubfileData.h"
  8. #include "EasyLogging++/easylogging++.h"
  9. #include "DatOperationResult.h"
  10. #include "Subfiles/TextSubFile.h"
  11. #include "Subfiles/DdsSubFile.h"
  12. #include "Subfiles/FontSubFile.h"
  13. #include "Subfiles/JpgSubFile.h"
  14. #include "Subfiles/OggSubFile.h"
  15. #include "Subfiles/WavSubFile.h"
  16. #include "Subfiles/UnknownSubFile.h"
  17. #include <algorithm>
  18. #include <SubFile.h>
  19. const long long MAXSIZE = 50ll * 1024ll * 1024ll; // Setting maximal file size 50 MB; Files with size more than 50 mb
  20. // will be recognized as incorrect and passed.
  21. // This should be done because of not completely correct implementation
  22. // of Subfiles and Subdirectories search in DatFile.
  23. namespace LOTRO_DAT {
  24. SubFile::SubFile() : category(0), dat_(nullptr), unknown1_(0), file_id_(0), file_offset_(0), file_size_(0),
  25. timestamp_(0), version_(0), block_size_(0), unknown2_(0) {
  26. }
  27. SubFile::SubFile(DatFile &dat, const BinaryData &header) : dat_(&dat) {
  28. category = 0;
  29. unknown1_ = header.ToNumber<4>(0); // unknown1
  30. file_id_ = header.ToNumber<4>(4); // file_id
  31. file_offset_ = header.ToNumber<4>(8); // file_offset
  32. file_size_ = header.ToNumber<4>(12); // block_size
  33. timestamp_ = header.ToNumber<4>(16); // timestamp
  34. version_ = header.ToNumber<4>(20); // version
  35. block_size_ = header.ToNumber<4>(24); // block_size
  36. unknown2_ = header.ToNumber<4>(28); // unknown2
  37. if (file_size_ > MAXSIZE) {
  38. LOG(ERROR) << "Bad SubFile::SubFile() - File size of file " << file_id << " with offset " << file_offset
  39. << " is too much... Maybe it's incorrect..?";
  40. file_id_ = -1;
  41. return;
  42. }
  43. }
  44. SubFile::SubFile(DatFile &dat, long long dictionary_offset, long long unknown1, long long file_id,
  45. long long file_offset, long long file_size, long long timestamp, long long version,
  46. long long block_size, long long unknown2) :
  47. category(0), dat_(&dat), dictionary_offset_(dictionary_offset), unknown1_(unknown1), file_id_(file_id),
  48. file_offset_(file_offset), file_size_(file_size), timestamp_(timestamp), version_(version),
  49. block_size_(block_size), unknown2_(unknown2) {
  50. if (file_size_ > MAXSIZE) {
  51. LOG(ERROR) << "Bad SubFile::SubFile() - File size of file " << file_id << " with offset " << file_offset
  52. << " is too much... Maybe it's incorrect..?";
  53. file_id_ = -1;
  54. return;
  55. }
  56. }
  57. /// Typical getters of private fields, if there's need for getting information about SubFile from outside class.
  58. long long SubFile::dictionary_offset() const {
  59. return dictionary_offset_;
  60. }
  61. long long SubFile::unknown1() const {
  62. return unknown1_;
  63. }
  64. long long SubFile::file_id() const {
  65. return file_id_;
  66. }
  67. long long SubFile::file_offset() const {
  68. return file_offset_;
  69. }
  70. long long SubFile::file_size() const {
  71. return file_size_;
  72. }
  73. long long SubFile::timestamp() const {
  74. return timestamp_;
  75. }
  76. long long SubFile::version() const {
  77. return version_;
  78. }
  79. long long SubFile::block_size() const {
  80. return block_size_;
  81. }
  82. long long SubFile::unknown2() const {
  83. return unknown2_;
  84. }
  85. /// bool Subfile::FileType(...);
  86. /// Virtual function, can (and should) be redefined in child class, otherwise an exception will be thrown while exporting/importing file.
  87. /// Returns enum FILE_TYPE value, which is declared in DatFile.h
  88. FILE_TYPE SubFile::FileType() const {
  89. LOG(ERROR) << "INCORRECT IMPLEMENTATION!";
  90. return UNKNOWN;
  91. }
  92. /// std::string Subfile::Extension()
  93. /// Virtual function, can (and should) be redefined in child class, otherwise an exception will be thrown while exporting/importing file.
  94. /// Returns std::string with extension, beggined with '.', ex. ".jpg", ".txt" and so on;
  95. std::string SubFile::Extension() const {
  96. LOG(ERROR) << "INCORRECT IMPLEMENTATION!";
  97. return ".subfile";
  98. }
  99. /// bool Subfile::PrepareForExport(...);
  100. /// Virtual function, can be redefined in child class, otherwise an exception will be thrown while exporting file.
  101. /// Takes constant BinaryData& file_data, which contains all file data in .dat file, except first 8 bytes before file_id.
  102. /// Takes references to export_size - amount of exported files/strings, and content of exported data such as:
  103. /// 1) field binary_data - exported as RAW data
  104. /// 2) field text_data - UTF-16 text, exporting in UTF-8
  105. /// 3) field options - YAML field, which consists of some parameters of file such as file_id, extension and so on.
  106. /// Returns true if preparation was success. Otherwise returns false;
  107. SubfileData SubFile::PrepareForExport(const BinaryData &) {
  108. LOG(ERROR) << "INCORRECT IMPLEMENTATION!";
  109. return SubfileData();
  110. }
  111. /// BinaryData Subfile::PrepareForImport(...);
  112. /// Virtual function, can be redefined in child class, otherwise an exception will be thrown while importing file.
  113. /// Takes constant BinaryData& file_data, which contains all file data in .dat file, including first 8 bytes befire file_id.
  114. /// 1) const field binary_data - exported as RAW data
  115. /// 2) const field text_data - UTF-16 text, exporting in UTF-8
  116. /// 3) const field options - YAML field, which consists of some parameters of file such as file_id, extension and so on.
  117. /// Returns BinaryData - bytes array, prepared for writing in .dat file
  118. BinaryData SubFile::MakeForImport(const BinaryData &, const SubfileData &) {
  119. LOG(ERROR) << "INCORRECT IMPLEMENTATION!";
  120. return BinaryData(0);
  121. }
  122. BinaryData SubFile::MakeHeaderData() const {
  123. BinaryData header = BinaryData::FromNumber<4>(unknown1_)
  124. + BinaryData::FromNumber<4>(file_id_)
  125. + BinaryData::FromNumber<4>(file_offset_)
  126. + BinaryData::FromNumber<4>(file_size_)
  127. + BinaryData::FromNumber<4>(timestamp_)
  128. + BinaryData::FromNumber<4>(version_)
  129. + BinaryData::FromNumber<4>(block_size_)
  130. + BinaryData::FromNumber<4>(unknown2_);
  131. return header;
  132. }
  133. bool SubFile::operator==(const SubFile &b) const {
  134. return unknown1_ == b.unknown1_
  135. && file_id_ == b.file_id_
  136. && file_offset_ == b.file_offset_
  137. && file_size_ == b.file_size_
  138. && timestamp_ == b.timestamp_
  139. && version_ == b.version_
  140. && block_size_ == b.block_size_
  141. && unknown2_ == b.unknown2_;
  142. }
  143. bool SubFile::operator!=(const SubFile &b) const {
  144. return !(*this == b);
  145. }
  146. std::shared_ptr<SubFile> SubFile::MakeSubfile(DatFile &dat, SubFile preinit_file) {
  147. FILE_TYPE type = SubFile::GetSubfileType(dat, preinit_file);
  148. switch (type) {
  149. case TEXT:
  150. return std::dynamic_pointer_cast<SubFile>(std::make_shared<TextSubFile>(preinit_file));
  151. case JPG:
  152. return std::dynamic_pointer_cast<SubFile>(std::make_shared<JpgSubFile>(preinit_file));
  153. case DDS:
  154. return std::dynamic_pointer_cast<SubFile>(std::make_shared<DdsSubFile>(preinit_file));
  155. case WAV:
  156. return std::dynamic_pointer_cast<SubFile>(std::make_shared<WavSubFile>(preinit_file));
  157. case OGG:
  158. return std::dynamic_pointer_cast<SubFile>(std::make_shared<OggSubFile>(preinit_file));
  159. case FONT:
  160. return std::dynamic_pointer_cast<SubFile>(std::make_shared<FontSubFile>(preinit_file));
  161. case UNKNOWN:
  162. return std::dynamic_pointer_cast<SubFile>(std::make_shared<UnknownSubFile>(preinit_file));
  163. }
  164. LOG(ERROR) << "Incorrect file type..";
  165. return std::dynamic_pointer_cast<SubFile>(std::make_shared<UnknownSubFile>(preinit_file));
  166. }
  167. FILE_TYPE SubFile::GetSubfileType(DatFile &dat, SubFile preinit_file) {
  168. // Text check based on file_id
  169. if (((unsigned long long) preinit_file.file_id_ >> 24ull) == 0x25ull)
  170. return TEXT;
  171. // Font check based on file_id
  172. if (((unsigned long long) preinit_file.file_id_ >> 24ull) == 0x42ull)
  173. return FONT;
  174. BinaryData header(64);
  175. dat.GetIO().ReadData(header, 64, (unsigned) preinit_file.file_offset_ + 8);
  176. if (header.Empty()) {
  177. LOG(ERROR) << "Unable to read file header. file_id = " << preinit_file.file_id_ << ", offset = "
  178. << preinit_file.file_offset_;
  179. return UNKNOWN;
  180. }
  181. // jpeg / dds check
  182. if (((unsigned long long) preinit_file.file_id_ >> 24ull) == 0x41ull) {
  183. long long soi = header.ToNumber<2>(24);
  184. long long marker = header.ToNumber<2>(26);
  185. //auto markerSize = header.ToNumber<short>(28);
  186. //auto four = header.ToNumber<int>(30);
  187. if ((soi == 0xD8FFll && marker == 0xE0FFll) || marker == 0xE1FFll)
  188. return JPG;
  189. return DDS;
  190. }
  191. // Ogg and Wav check
  192. if (header[8] == 0x4F && header[9] == 0x67 && header[10] == 0x67 && header[11] == 0x53)
  193. return OGG;
  194. if (header[8] == 0x52 && header[9] == 0x49 && header[10] == 0x46 && header[11] == 0x46)
  195. return WAV;
  196. return UNKNOWN;
  197. }
  198. };