DatFile.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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. //----WARNINGS----//
  42. CORRUPTED_FILE_WARNING = 2,
  43. //----ERRORS----//
  44. INCORRECT_STATE_ERROR = -1,
  45. NO_FILE_ERROR = -2,
  46. WRITE_TO_FILE_ERROR = -3,
  47. INCORRECT_DAT_ID = -4,
  48. INCORRECT_SUPERBLOCK_ERROR = -5,
  49. INIT_ERROR = -6,
  50. DUBLICATE_PATCH_FILES_ERROR = -8,
  51. INCORRECT_PATCH_FILE = -9,
  52. DAT_PATCH_FILE_ERROR = -10,
  53. DAT_READ_ERROR = -11,
  54. DAT_WRITE_ERROR = -12,
  55. CRITICAL_DAT_ERROR = -14,
  56. NO_BACKUP_ERROR = -15,
  57. REMOVE_FILE_ERROR = -16
  58. };
  59. enum DAT_STATE {
  60. CLOSED = 1,
  61. SUCCESS_OPENED = 2,
  62. SUCCESS_SUPERBLOCK = 3,
  63. SUCCESS_DIRECTORIES = 4,
  64. SUCCESS_DICTIONARY = 5,
  65. READY = 6,
  66. UPDATED = 7
  67. };
  68. enum LOCALE : unsigned {
  69. ORIGINAL = 0,
  70. PATCHED = 1
  71. };
  72. class DatFile {
  73. friend class SubDirectory;
  74. public:
  75. DatFile();
  76. DAT_RESULT InitDatFile(const std::string &filename, int dat_id);
  77. DAT_STATE DatFileState() const;
  78. DAT_RESULT PerformDictionaryCheck();
  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 ModifyFragmentationJournal();
  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 = 10 * 1024 * 1024; // 50 megabytes;
  108. const long long MIN_BUFFERED_SIZE = 1 * 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 WriteUnorderedDictionary(std::string path) const;
  129. bool CheckIfNotPatched();
  130. bool CheckIfPatchedByOldLauncher();
  131. bool CheckIfBackupExists(const std::string &backup_datname);
  132. DAT_RESULT CreateBackup(const std::string &backup_datname);
  133. DAT_RESULT RestoreFromBackup(const std::string &backup_datname);
  134. DAT_RESULT RemoveBackup(const std::string &backup_datname);
  135. private:
  136. std::map<long long, Subfile*>* GetLocaleDictReference(LOCALE locale);
  137. private:
  138. LOCALE current_locale_;
  139. std::string filename_;
  140. std::map<long long, Subfile*> orig_dict_;
  141. std::map<long long, Subfile*> patch_dict_;
  142. std::set<long long> pending_patch_;
  143. std::set<long long> inactive_categories;
  144. private:
  145. FILE *file_handler_;
  146. SubDirectory *root_directory_;
  147. std::set<long long> pending_dictionary_;
  148. std::map<long long, Subfile *> dictionary_;
  149. long long constant1_;
  150. long long constant2_;
  151. long long file_size_;
  152. long long version1_;
  153. long long version2_;
  154. long long fragmentation_journal_size_;
  155. long long fragmentation_journal_end_;
  156. long long root_directory_offset_;
  157. long long fragmentation_journal_offset_;
  158. long long free_dat_size_;
  159. long long actual_dat_size_;
  160. DAT_STATE dat_state_;
  161. int dat_id_;
  162. bool dat_without_patches_;
  163. };
  164. }
  165. }
  166. #endif //LOTRO_DAT_PATCHER_DATFILE_H