Subfile.cpp 6.0 KB

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