Subfile.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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() {}
  15. Subfile::Subfile(DatFile *dat, 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), 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::fragments_count() const {
  27. return fragments_count_;
  28. }
  29. long long Subfile::unknown1() const {
  30. return unknown1_;
  31. }
  32. long long Subfile::file_id() const {
  33. return file_id_;
  34. }
  35. long long Subfile::file_offset() const {
  36. return file_offset_;
  37. }
  38. long long Subfile::file_size() const {
  39. return file_size_;
  40. }
  41. long long Subfile::timestamp() const {
  42. return timestamp_;
  43. }
  44. long long Subfile::version() const {
  45. return version_;
  46. }
  47. long long Subfile::block_size() const {
  48. return block_size_;
  49. }
  50. /// bool Subfile::FileType(...);
  51. /// Virtual function, can (and should) be redefined in child class, otherwise an exception will be thrown while exporting/importing file.
  52. /// Returns enum FILE_TYPE value, which is declared in DatFile.h
  53. FILE_TYPE Subfile::FileType() const {
  54. throw DatException("Bad Subfile::FileType() - function is not implemented for this type.", SUBFILE_EXCEPTION);
  55. }
  56. /// std::string Subfile::Extension()
  57. /// Virtual function, can (and should) be redefined in child class, otherwise an exception will be thrown while exporting/importing file.
  58. /// Returns std::string with extension, beggined with '.', ex. ".jpg", ".txt" and so on;
  59. std::string Subfile::Extension() const {
  60. throw DatException("Bad Subfile::Extension() - function is not implemented for this type.", SUBFILE_EXCEPTION);
  61. }
  62. /// bool Subfile::PrepareForExport(...);
  63. /// Virtual function, can be redefined in child class, otherwise an exception will be thrown while exporting file.
  64. /// Takes constant BinaryData& file_data, which contains all file data in .dat file, except first 8 bytes before file_id.
  65. /// Takes references to export_size - amount of exported files/strings, and content of exported data such as:
  66. /// 1) field binary_data - exported as RAW data
  67. /// 2) field text_data - UTF-16 text, exporting in UTF-8
  68. /// 3) field options - YAML field, which consists of some parameters of file such as file_id, extension and so on.
  69. /// Returns true if preparation was success. Otherwise returns false;
  70. bool Subfile::PrepareForExport(const BinaryData &file_data, long long &export_size, std::vector<BinaryData> &binary_data,
  71. std::vector<std::u16string> &text_data, std::vector<YAML::Node> &options) {
  72. throw DatException("Bad Subfile::PrepareForExport() - function is not implemented for this type.", EXPORT_EXCEPTION);
  73. }
  74. /// BinaryData Subfile::PrepareForImport(...);
  75. /// Virtual function, can be redefined in child class, otherwise an exception will be thrown while importing file.
  76. /// Takes constant BinaryData& file_data, which contains all file data in .dat file, including first 8 bytes befire file_id.
  77. /// 1) const field binary_data - exported as RAW data
  78. /// 2) const field text_data - UTF-16 text, exporting in UTF-8
  79. /// 3) const field options - YAML field, which consists of some parameters of file such as file_id, extension and so on.
  80. /// Returns BinaryData - bytes array, prepared for writing in .dat file
  81. BinaryData Subfile::MakeForImport(const BinaryData &old_data, const BinaryData &binary_data, const std::u16string &text_data,
  82. const YAML::Node &options) {
  83. throw DatException("Bad Subfile::MakeForImport() - function is not implemented for this type.", IMPORT_EXCEPTION);
  84. }
  85. };