DatFile.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. #define CLIENT_LOCAL_ENGLISH 0
  21. #define CLIENT_GENERAL 1
  22. #define CLIENT_SOUND 2
  23. #define CLIENT_SURFACE 3
  24. #define CLIENT_HIGHRES 4
  25. extern "C++"
  26. {
  27. namespace LOTRO_DAT {
  28. class BinaryData;
  29. class DatException;
  30. class SubDirectory;
  31. class Subfile;
  32. class SubfileData;
  33. enum FILE_TYPE : int {
  34. TEXT,
  35. JPG,
  36. DDS,
  37. WAV,
  38. OGG,
  39. FONT,
  40. UNKNOWN
  41. };
  42. enum DAT_RESULT : int {
  43. //----BASE----///
  44. FAILED = 0,
  45. SUCCESS = 1,
  46. //----TRUE/FALSE----//
  47. FALSE = 0,
  48. TRUE = 1,
  49. INCORRECT_STATE_ERROR = -1,
  50. NO_FILE_ERROR = -2,
  51. WRITE_TO_FILE_ERROR = -3,
  52. INCORRECT_DAT_ID = -4,
  53. INCORRECT_SUPERBLOCK_ERROR = -5,
  54. INIT_ERROR = -6,
  55. DAT_READ_ERROR = -7,
  56. DUBLICATE_PATCH_FILES_ERROR = -8,
  57. INCORRECT_PATCH_FILE = -9,
  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. ~DatFile();
  79. DAT_RESULT ExtractFile(long long file_id, const std::string &path = "");
  80. DAT_RESULT ExtractFile(long long file_id, Database *db);
  81. int ExtractAllFilesByType(FILE_TYPE type, std::string path = "");
  82. int ExtractAllFilesByType(FILE_TYPE type, Database *db);
  83. DAT_RESULT PatchFile(const char *filename, YAML::Node options);
  84. DAT_RESULT PatchFile(const SubfileData &data, bool rewrite_original = false);
  85. DAT_RESULT PatchAllDatabase(Database *db);
  86. DAT_RESULT WriteUnorderedDictionary(std::string path) const;
  87. long long files_number() const;
  88. const std::string& filename() const;
  89. BinaryData GetFileData(const Subfile *file, long long offset = 0);
  90. DAT_RESULT CommitChanges();
  91. DAT_RESULT CloseDatFile();
  92. private:
  93. // INIT SECTION
  94. DAT_RESULT OpenDatFile(const char *dat_name);
  95. DAT_RESULT ReadSuperBlock();
  96. DAT_RESULT MakeDirectories();
  97. DAT_RESULT MakeDictionary();
  98. // READ-WRITE SECTION
  99. DAT_RESULT ReadData(BinaryData &data, long long size, long long offset = 0, long long data_offset = 0);
  100. DAT_RESULT WriteData(const BinaryData &data, long long size, long long offset = 0, long long data_offset = 0);
  101. // PATCH SECTION
  102. DAT_RESULT ApplyFilePatch(Subfile *file, const BinaryData &data, bool rewrite_original = false);
  103. std::vector<std::pair<long long, long long> > GetFragmentationJournal();
  104. DAT_RESULT UpdateHeader();
  105. DAT_RESULT UpdateSubdirectories();
  106. DAT_RESULT UpdateFragmentationJournal(const std::vector<std::pair<long long, long long> > &journal);
  107. // LOCALE MANAGING SECTION
  108. private:
  109. DAT_RESULT InitLocales();
  110. DAT_RESULT CommitLocales();
  111. public:
  112. DAT_RESULT SetLocale(LOCALE locale);
  113. bool CheckIfUpdatedByGame();
  114. DAT_RESULT RepairPatches(Database *db);
  115. DAT_RESULT FinishRepairingPatches();
  116. LOCALE current_locale();
  117. DAT_RESULT EnableCategory(int category);
  118. DAT_RESULT DisableCategory(int category);
  119. const std::set<long long>& GetInactiveCategoriesList();
  120. public:
  121. bool CheckIfNotPatched();
  122. bool CheckIfPatchedByOldLauncher();
  123. private:
  124. std::map<long long, Subfile*>* GetLocaleDictReference(LOCALE locale);
  125. private:
  126. LOCALE current_locale_;
  127. std::string filename_;
  128. std::map<long long, Subfile*> orig_dict_;
  129. std::map<long long, Subfile*> patch_dict_;
  130. std::set<long long> pending_patch_;
  131. std::set<long long> inactive_categories;
  132. private:
  133. FILE *file_handler_;
  134. SubDirectory *root_directory_;
  135. std::unordered_set<long long> patched_list;
  136. std::unordered_map<long long, Subfile *> dictionary_;
  137. long long constant1_;
  138. long long constant2_;
  139. long long file_size_;
  140. long long version1_;
  141. long long version2_;
  142. long long root_directory_offset_;
  143. long long fragmentation_journal_offset_;
  144. DAT_STATE dat_state_;
  145. int dat_id_;
  146. };
  147. }
  148. }
  149. #endif //LOTRO_DAT_PATCHER_DATFILE_H