Subfile.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. //
  2. // Created by Иван_Архипов on 01.11.2017.
  3. //
  4. #include "Subfile.h"
  5. #include "BinaryData.h"
  6. #include "DatFile.h"
  7. #include "Common/DatException.h"
  8. #include "SubfileData.h"
  9. #include <algorithm>
  10. const long long MAXSIZE = 50ll * 1024ll * 1024ll; // Setting maximal file size 50 MB; Files with size more than 50 mb
  11. // will be recognized as incorrect and passed.
  12. // This should be done because of not completely correct implementation
  13. // of Subfiles and Subdirectories search in DatFile.
  14. namespace LOTRO_DAT {
  15. Subfile::Subfile() = default;
  16. Subfile::Subfile(DatFile *dat, const BinaryData &header) {
  17. dat_ = dat;
  18. fragments_count_ = header.ToNumber<4>(0); // fragments_count
  19. unknown1_ = header.ToNumber<4>(4); // unknown1
  20. file_id_ = header.ToNumber<4>(8); // file_id
  21. file_offset_ = header.ToNumber<4>(12); // file_offset
  22. file_size_ = header.ToNumber<4>(16); // block_size
  23. timestamp_ = header.ToNumber<4>(20); // timestamp
  24. version_ = header.ToNumber<4>(24); // version
  25. block_size_ = header.ToNumber<4>(28); // block_size
  26. }
  27. Subfile::Subfile(DatFile *dat, long long dictionary_offset, long long fragments_count, long long unknown1, long long file_id,
  28. long long file_offset,
  29. long long file_size, long long timestamp, long long version, long long block_size) :
  30. dat_(dat), dictionary_offset_(dictionary_offset), fragments_count_(fragments_count), unknown1_(unknown1), file_id_(file_id),
  31. file_offset_(file_offset),
  32. file_size_(file_size), timestamp_(timestamp), version_(version), block_size_(block_size) {
  33. if (file_size_ > MAXSIZE)
  34. throw DatException("Bad Subfile::Subfile() - File size is too much... Maybe it's incorrect..?",
  35. SUBFILE_EXCEPTION);
  36. }
  37. /// Typical getters of private fields, if there's need for getting information about SubFile from outside class.
  38. long long Subfile::dictionary_offset() const {
  39. return dictionary_offset_;
  40. }
  41. long long Subfile::fragments_count() const {
  42. return fragments_count_;
  43. }
  44. long long Subfile::unknown1() const {
  45. return unknown1_;
  46. }
  47. long long Subfile::file_id() const {
  48. return file_id_;
  49. }
  50. long long Subfile::file_offset() const {
  51. return file_offset_;
  52. }
  53. long long Subfile::file_size() const {
  54. return file_size_;
  55. }
  56. long long Subfile::timestamp() const {
  57. return timestamp_;
  58. }
  59. long long Subfile::version() const {
  60. return version_;
  61. }
  62. long long Subfile::block_size() const {
  63. return block_size_;
  64. }
  65. /// bool Subfile::FileType(...);
  66. /// Virtual function, can (and should) be redefined in child class, otherwise an exception will be thrown while exporting/importing file.
  67. /// Returns enum FILE_TYPE value, which is declared in DatFile.h
  68. FILE_TYPE Subfile::FileType() const {
  69. throw DatException("Bad Subfile::FileType() - function is not implemented for this type.", SUBFILE_EXCEPTION);
  70. }
  71. /// std::string Subfile::Extension()
  72. /// Virtual function, can (and should) be redefined in child class, otherwise an exception will be thrown while exporting/importing file.
  73. /// Returns std::string with extension, beggined with '.', ex. ".jpg", ".txt" and so on;
  74. std::string Subfile::Extension() const {
  75. throw DatException("Bad Subfile::Extension() - function is not implemented for this type.", SUBFILE_EXCEPTION);
  76. }
  77. /// bool Subfile::PrepareForExport(...);
  78. /// Virtual function, can be redefined in child class, otherwise an exception will be thrown while exporting file.
  79. /// Takes constant BinaryData& file_data, which contains all file data in .dat file, except first 8 bytes before file_id.
  80. /// Takes references to export_size - amount of exported files/strings, and content of exported data such as:
  81. /// 1) field binary_data - exported as RAW data
  82. /// 2) field text_data - UTF-16 text, exporting in UTF-8
  83. /// 3) field options - YAML field, which consists of some parameters of file such as file_id, extension and so on.
  84. /// Returns true if preparation was success. Otherwise returns false;
  85. SubfileData Subfile::PrepareForExport(const BinaryData &file_data) {
  86. throw DatException("Bad Subfile::PrepareForExport() - function is not implemented for this type.", EXPORT_EXCEPTION);
  87. }
  88. /// BinaryData Subfile::PrepareForImport(...);
  89. /// Virtual function, can be redefined in child class, otherwise an exception will be thrown while importing file.
  90. /// Takes constant BinaryData& file_data, which contains all file data in .dat file, including first 8 bytes befire file_id.
  91. /// 1) const field binary_data - exported as RAW data
  92. /// 2) const field text_data - UTF-16 text, exporting in UTF-8
  93. /// 3) const field options - YAML field, which consists of some parameters of file such as file_id, extension and so on.
  94. /// Returns BinaryData - bytes array, prepared for writing in .dat file
  95. BinaryData Subfile::MakeForImport(const BinaryData &old_data, const SubfileData &data) {
  96. throw DatException("Bad Subfile::MakeForImport() - function is not implemented for this type.", IMPORT_EXCEPTION);
  97. }
  98. BinaryData Subfile::MakeHeaderData() const {
  99. BinaryData header;
  100. BinaryData data(4);
  101. data.FromNumber<4>(fragments_count_);
  102. header = header + data;
  103. data.FromNumber<4>(unknown1_);
  104. header = header + data;
  105. data.FromNumber<4>(file_id_);
  106. header = header + data;
  107. data.FromNumber<4>(file_offset_);
  108. header = header + data;
  109. data.FromNumber<4>(file_size_);
  110. header = header + data;
  111. data.FromNumber<4>(timestamp_);
  112. header = header + data;
  113. data.FromNumber<4>(version_);
  114. header = header + data;
  115. data.FromNumber<4>(block_size_);
  116. header = header + data;
  117. return header;
  118. }
  119. };