DatFile.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. //
  2. // Created by Иван_Архипов on 31.10.2017.
  3. //
  4. #ifndef LOTRO_DAT_PATCHER_DATFILE_H
  5. #define LOTRO_DAT_PATCHER_DATFILE_H
  6. #ifdef LOTRO_DAT_EXPORTS
  7. #define LOTRO_DAT_API __declspec(dllexport)
  8. #else
  9. #define LOTRO_DAT_API __declspec(dllimport)
  10. #endif
  11. #include <fstream>
  12. #include <map>
  13. #include <unordered_map>
  14. #include <set>
  15. #include <vector>
  16. #include <yaml-cpp/node/node.h>
  17. #include <unordered_set>
  18. #include "Database.h"
  19. // Dat file names definitions
  20. extern "C++"
  21. {
  22. namespace LOTRO_DAT {
  23. class BinaryData;
  24. class DatException;
  25. class SubDirectory;
  26. class Subfile;
  27. class SubfileData;
  28. enum FILE_TYPE : int {
  29. TEXT,
  30. JPG,
  31. DDS,
  32. WAV,
  33. OGG,
  34. FONT,
  35. UNKNOWN
  36. };
  37. enum DAT_RESULT : int {
  38. //----BASE----///
  39. FAILED = 0,
  40. SUCCESS = 1,
  41. //----TRUE/FALSE----//
  42. FALSE = 0,
  43. TRUE = 1,
  44. //----WARNINGS----//
  45. CORRUPTED_FILE_WARNING = 2,
  46. //----ERRORS----//
  47. INCORRECT_STATE_ERROR = -1,
  48. NO_FILE_ERROR = -2,
  49. WRITE_TO_FILE_ERROR = -3,
  50. INCORRECT_DAT_ID = -4,
  51. INCORRECT_SUPERBLOCK_ERROR = -5,
  52. INIT_ERROR = -6,
  53. DUBLICATE_PATCH_FILES_ERROR = -8,
  54. INCORRECT_PATCH_FILE = -9,
  55. DAT_PATCH_FILE_ERROR = -10,
  56. DAT_READ_ERROR = -11,
  57. DAT_WRITE_ERROR = -12,
  58. CRITICAL_DAT_ERROR = -14
  59. };
  60. enum DAT_STATE {
  61. CLOSED = 1,
  62. SUCCESS_OPENED = 2,
  63. SUCCESS_SUPERBLOCK = 3,
  64. SUCCESS_DIRECTORIES = 4,
  65. SUCCESS_DICTIONARY = 5,
  66. READY = 6,
  67. UPDATED = 7
  68. };
  69. enum LOCALE : unsigned {
  70. ORIGINAL = 0,
  71. PATCHED = 1
  72. };
  73. class DatFile {
  74. friend class SubDirectory;
  75. public:
  76. DatFile();
  77. DAT_RESULT InitDatFile(const std::string &filename, int dat_id);
  78. DAT_STATE DatFileState() const;
  79. ~DatFile();
  80. // EXTRACT BASE SECTION
  81. DAT_RESULT ExtractFile(long long file_id, const std::string &path = "");
  82. DAT_RESULT ExtractFile(long long file_id, Database *db);
  83. int ExtractAllFilesByType(FILE_TYPE type, std::string path = "");
  84. int ExtractAllFilesByType(FILE_TYPE type, Database *db);
  85. // PATCH BASE SECTION
  86. //DAT_RESULT PatchFile(const char *filename, YAML::Node options);
  87. DAT_RESULT PatchFile(const SubfileData &data);
  88. DAT_RESULT PatchAllDatabase(Database *db);
  89. long long files_number() const;
  90. const std::string& filename() const;
  91. BinaryData GetFileData(const Subfile *file, long long offset = 0);
  92. DAT_RESULT CloseDatFile();
  93. private:
  94. // INIT SECTION
  95. DAT_RESULT OpenDatFile(const char *dat_name);
  96. DAT_RESULT ReadSuperBlock();
  97. DAT_RESULT MakeDirectories();
  98. DAT_RESULT MakeDictionary();
  99. DAT_RESULT ClearFragmentationJournal();
  100. // READ-WRITE SECTION
  101. DAT_RESULT ReadData(BinaryData &data, long long size, long long offset = 0, long long data_offset = 0);
  102. DAT_RESULT WriteData(const BinaryData &data, long long size, long long offset = 0, long long data_offset = 0);
  103. // PATCH SECTION
  104. DAT_RESULT ApplyFilePatch(Subfile *file, BinaryData &data);
  105. private:
  106. long long free_buffered_size_;
  107. const long long MAX_BUFFERED_SIZE = 50 * 1024 * 1024; // 50 megabytes;
  108. const long long MIN_BUFFERED_SIZE = 5 * 1024 * 1024; // 5 megabytes;
  109. void AddBufferedSize();
  110. // COMMIT UPDATE SECTION
  111. DAT_RESULT UpdateHeader();
  112. // LOCALE MANAGING SECTION
  113. private:
  114. DAT_RESULT InitLocales();
  115. DAT_RESULT CommitLocales();
  116. DAT_RESULT CommitDirectories();
  117. public:
  118. DAT_RESULT SetLocale(LOCALE locale);
  119. LOCALE current_locale();
  120. // CATEGORY MANAGEMENT SECTION
  121. DAT_RESULT EnableCategory(int category);
  122. DAT_RESULT DisableCategory(int category);
  123. const std::set<long long>& GetInactiveCategoriesList();
  124. // SOME PRIOR TOOLS
  125. DAT_RESULT RepairDatFile();
  126. bool CorrectSubfile(Subfile *file);
  127. bool CheckIfUpdatedByGame();
  128. DAT_RESULT RepairPatches(Database *db);
  129. DAT_RESULT FinishRepairingPatches();
  130. DAT_RESULT WriteUnorderedDictionary(std::string path) const;
  131. bool CheckIfNotPatched();
  132. bool CheckIfPatchedByOldLauncher();
  133. private:
  134. std::map<long long, Subfile*>* GetLocaleDictReference(LOCALE locale);
  135. private:
  136. LOCALE current_locale_;
  137. std::string filename_;
  138. std::map<long long, Subfile*> orig_dict_;
  139. std::map<long long, Subfile*> patch_dict_;
  140. std::set<long long> pending_patch_;
  141. std::set<long long> inactive_categories;
  142. private:
  143. FILE *file_handler_;
  144. SubDirectory *root_directory_;
  145. std::unordered_set<long long> pending_dictionary_;
  146. std::unordered_map<long long, Subfile *> dictionary_;
  147. long long constant1_;
  148. long long constant2_;
  149. long long file_size_;
  150. long long version1_;
  151. long long version2_;
  152. long long fragmentation_journal_size_;
  153. long long fragmentation_journal_end_;
  154. long long root_directory_offset_;
  155. long long fragmentation_journal_offset_;
  156. DAT_STATE dat_state_;
  157. int dat_id_;
  158. };
  159. }
  160. }
  161. #endif //LOTRO_DAT_PATCHER_DATFILE_H