DatFile.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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_STATE {
  43. CLOSED = 1,
  44. SUCCESS_OPENED = 2,
  45. SUCCESS_SUPERBLOCK = 3,
  46. SUCCESS_DIRECTORIES = 4,
  47. SUCCESS_DICTIONARY = 5,
  48. READY = 6,
  49. UPDATED = 7
  50. };
  51. enum LOCALE : unsigned {
  52. ORIGINAL = 0,
  53. PATCHED = 1
  54. };
  55. class DatFile {
  56. friend class SubDirectory;
  57. public:
  58. DatFile();
  59. bool InitDatFile(const std::string &filename, int dat_id);
  60. DAT_STATE DatFileState() const;
  61. virtual ~DatFile();
  62. bool ExtractFile(long long file_id, const std::string &path = "");
  63. bool ExtractFile(long long file_id, Database *db);
  64. int ExtractAllFilesByType(FILE_TYPE type, std::string path = "");
  65. int ExtractAllFilesByType(FILE_TYPE type, Database *db);
  66. bool PatchFile(const char *filename, YAML::Node options);
  67. bool PatchFile(const SubfileData &data);
  68. bool PatchAllDatabase(Database *db);
  69. void WriteUnorderedDictionary(std::string path) const;
  70. long long files_number() const;
  71. BinaryData GetFileData(const Subfile *file, long long offset = 0);
  72. bool CommitChanges();
  73. bool CloseDatFile();
  74. private:
  75. // INIT SECTION
  76. void OpenDatFile(const char *dat_name);
  77. void ReadSuperBlock();
  78. void MakeDirectories();
  79. void MakeDictionary();
  80. // READ-WRITE SECTION
  81. void ReadData(BinaryData &data, long long size, long long offset = 0, long long data_offset = 0);
  82. void WriteData(const BinaryData &data, long long size, long long offset = 0, long long data_offset = 0);
  83. // PATCH SECTION
  84. void ApplyFilePatch(Subfile *file, const BinaryData &data);
  85. std::vector<std::pair<long long, long long> > GetFragmentationJournal();
  86. void UpdateHeader();
  87. void UpdateSubdirectories();
  88. void UpdateFragmentationJournal(const std::vector<std::pair<long long, long long> > &journal);
  89. // LOCALE MANAGING SECTION
  90. private:
  91. void InitLocale(LOCALE locale, const char* dict_name);
  92. void SaveLocale(LOCALE locale, const char* filename);
  93. void CommitLocales();
  94. public:
  95. void SetLocale(LOCALE locale);
  96. bool CheckIfUpdatedByGame();
  97. void RepairPatches(Database *db);
  98. LOCALE current_locale();
  99. private:
  100. std::unordered_map<long long, Subfile*>* GetLocaleDictReference(LOCALE locale);
  101. private:
  102. LOCALE current_locale_;
  103. std::string filename_;
  104. std::unordered_map<long long, Subfile*> orig_dict_;
  105. std::unordered_map<long long, Subfile*> patch_dict_;
  106. std::unordered_map<long long, Subfile*> pending_patch_;
  107. private:
  108. FILE *file_handler_;
  109. SubDirectory *root_directory_;
  110. std::unordered_set<long long> patched_list;
  111. std::unordered_map<long long, Subfile *> dictionary_;
  112. long long constant1_;
  113. long long constant2_;
  114. long long file_size_;
  115. long long version1_;
  116. long long version2_;
  117. long long root_directory_offset_;
  118. long long fragmentation_journal_offset_;
  119. DAT_STATE dat_state_;
  120. int dat_id_;
  121. };
  122. }
  123. }
  124. #endif //LOTRO_DAT_PATCHER_DATFILE_H