DatFile.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. DAT_RESULT ExtractFile(long long file_id, const std::string &path = "");
  81. DAT_RESULT ExtractFile(long long file_id, Database *db);
  82. int ExtractAllFilesByType(FILE_TYPE type, std::string path = "");
  83. int ExtractAllFilesByType(FILE_TYPE type, Database *db);
  84. DAT_RESULT PatchFile(const char *filename, YAML::Node options);
  85. DAT_RESULT PatchFile(const SubfileData &data, bool rewrite_original = false);
  86. DAT_RESULT PatchAllDatabase(Database *db);
  87. DAT_RESULT WriteUnorderedDictionary(std::string path) const;
  88. long long files_number() const;
  89. const std::string& filename() const;
  90. BinaryData GetFileData(const Subfile *file, long long offset = 0);
  91. DAT_RESULT CommitChanges();
  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. // READ-WRITE SECTION
  100. DAT_RESULT ReadData(BinaryData &data, long long size, long long offset = 0, long long data_offset = 0);
  101. DAT_RESULT WriteData(const BinaryData &data, long long size, long long offset = 0, long long data_offset = 0);
  102. // PATCH SECTION
  103. DAT_RESULT ApplyFilePatch(Subfile *file, const BinaryData &data, bool rewrite_original = false);
  104. std::vector<std::pair<long long, long long> > GetFragmentationJournal();
  105. DAT_RESULT UpdateHeader();
  106. DAT_RESULT UpdateSubdirectories();
  107. DAT_RESULT UpdateFragmentationJournal(const std::vector<std::pair<long long, long long> > &journal);
  108. // LOCALE MANAGING SECTION
  109. private:
  110. DAT_RESULT InitLocales();
  111. DAT_RESULT CommitLocales();
  112. public:
  113. DAT_RESULT SetLocale(LOCALE locale);
  114. DAT_RESULT RepairDatFile();
  115. bool CorrectSubfile(Subfile *file);
  116. bool CheckIfUpdatedByGame();
  117. DAT_RESULT RepairPatches(Database *db);
  118. DAT_RESULT FinishRepairingPatches();
  119. LOCALE current_locale();
  120. DAT_RESULT EnableCategory(int category);
  121. DAT_RESULT DisableCategory(int category);
  122. const std::set<long long>& GetInactiveCategoriesList();
  123. public:
  124. bool CheckIfNotPatched();
  125. bool CheckIfPatchedByOldLauncher();
  126. private:
  127. std::map<long long, Subfile*>* GetLocaleDictReference(LOCALE locale);
  128. private:
  129. LOCALE current_locale_;
  130. std::string filename_;
  131. std::map<long long, Subfile*> orig_dict_;
  132. std::map<long long, Subfile*> patch_dict_;
  133. std::set<long long> pending_patch_;
  134. std::set<long long> inactive_categories;
  135. private:
  136. FILE *file_handler_;
  137. SubDirectory *root_directory_;
  138. std::unordered_set<long long> patched_list;
  139. std::unordered_map<long long, Subfile *> dictionary_;
  140. long long constant1_;
  141. long long constant2_;
  142. long long file_size_;
  143. long long version1_;
  144. long long version2_;
  145. long long root_directory_offset_;
  146. long long fragmentation_journal_offset_;
  147. DAT_STATE dat_state_;
  148. int dat_id_;
  149. };
  150. }
  151. }
  152. #endif //LOTRO_DAT_PATCHER_DATFILE_H