Subfile.cpp 5.5 KB

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