DatFile.h 5.0 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. #include "DatFileIO.h"
  20. // Dat file names definitions
  21. extern "C++"
  22. {
  23. namespace LOTRO_DAT {
  24. class BinaryData;
  25. class DatException;
  26. class SubDirectory;
  27. class SubFile;
  28. class SubfileData;
  29. enum FILE_TYPE : int {
  30. TEXT,
  31. JPG,
  32. DDS,
  33. WAV,
  34. OGG,
  35. FONT,
  36. UNKNOWN
  37. };
  38. enum DAT_RESULT : int {
  39. //----BASE----///
  40. FAILED = 0,
  41. SUCCESS = 1,
  42. //----WARNINGS----//
  43. CORRUPTED_FILE_WARNING = 2,
  44. //----ERRORS----//
  45. INCORRECT_STATE_ERROR = -1,
  46. NO_FILE_ERROR = -2,
  47. WRITE_TO_FILE_ERROR = -3,
  48. INCORRECT_DAT_ID = -4,
  49. INCORRECT_SUPERBLOCK_ERROR = -5,
  50. INIT_ERROR = -6,
  51. DUBLICATE_PATCH_FILES_ERROR = -8,
  52. INCORRECT_PATCH_FILE = -9,
  53. DAT_PATCH_FILE_ERROR = -10,
  54. DAT_READ_ERROR = -11,
  55. DAT_WRITE_ERROR = -12,
  56. CRITICAL_DAT_ERROR = -14,
  57. NO_BACKUP_ERROR = -15,
  58. REMOVE_FILE_ERROR = -16
  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. explicit DatFile(const DatFile &other) = delete;
  78. DatFile& operator=(const DatFile &other) = delete;
  79. DAT_RESULT InitDatFile(const std::string &filename, int dat_id);
  80. DAT_STATE DatFileState() const;
  81. DAT_RESULT PerformDictionaryCheck();
  82. DAT_RESULT ModifyFragmentationJournal();
  83. ~DatFile();
  84. // EXTRACT BASE SECTION
  85. DAT_RESULT ExtractFile(long long file_id, const std::string &path = "");
  86. DAT_RESULT ExtractFile(long long file_id, Database *db);
  87. int ExtractAllFilesByType(FILE_TYPE type, std::string path = "");
  88. int ExtractAllFilesByType(FILE_TYPE type, Database *db);
  89. // PATCH BASE SECTION
  90. //DAT_RESULT PatchFile(const char *filename, YAML::Node options);
  91. DAT_RESULT PatchFile(const SubfileData &data);
  92. DAT_RESULT PatchAllDatabase(Database *db);
  93. long long files_number() const;
  94. const std::string& filename() const;
  95. BinaryData GetFileData(const std::shared_ptr<SubFile>& file, long long offset = 0);
  96. DAT_RESULT CloseDatFile();
  97. private:
  98. DAT_RESULT MakeDictionary();
  99. // PATCH SECTION
  100. DAT_RESULT ApplyFilePatch(std::shared_ptr<SubFile> file, BinaryData &data);
  101. private:
  102. long long free_buffered_size_;
  103. const unsigned MAX_BUFFERED_SIZE = 10 * 1024 * 1024; // 50 megabytes;
  104. const unsigned MIN_BUFFERED_SIZE = 1 * 1024 * 1024; // 5 megabytes;
  105. void AddBufferedSize();
  106. // LOCALE MANAGING SECTION
  107. private:
  108. DAT_RESULT InitLocales();
  109. DAT_RESULT CommitLocales();
  110. DAT_RESULT CommitDirectories();
  111. public:
  112. DAT_RESULT SetLocale(LOCALE locale);
  113. LOCALE current_locale();
  114. // CATEGORY MANAGEMENT SECTION
  115. DAT_RESULT EnableCategory(int category);
  116. DAT_RESULT DisableCategory(int category);
  117. const std::set<long long>& GetInactiveCategoriesList();
  118. // SOME PRIOR TOOLS
  119. DAT_RESULT RepairDatFile();
  120. bool CorrectSubfile(std::shared_ptr<SubFile> file);
  121. bool CheckIfUpdatedByGame();
  122. DAT_RESULT WriteUnorderedDictionary(std::string path) const;
  123. bool CheckIfNotPatched();
  124. bool CheckIfPatchedByOldLauncher();
  125. bool CheckIfBackupExists(const std::string &backup_datname);
  126. DAT_RESULT CreateBackup(const std::string &backup_datname);
  127. DAT_RESULT RestoreFromBackup(const std::string &backup_datname);
  128. DAT_RESULT RemoveBackup(const std::string &backup_datname);
  129. private:
  130. std::map<long long, std::shared_ptr<SubFile>>& GetLocaleDictReference(LOCALE locale);
  131. private:
  132. LOCALE current_locale_;
  133. std::string filename_;
  134. std::map<long long, std::shared_ptr<SubFile>> orig_dict_;
  135. std::map<long long, std::shared_ptr<SubFile>> patch_dict_;
  136. std::set<long long> pending_patch_;
  137. std::set<long long> inactive_categories;
  138. private:
  139. DatFileIO io;
  140. std::set<long long> pending_dictionary_;
  141. std::map<long long, std::shared_ptr<SubFile> > dictionary_;
  142. DAT_STATE dat_state_;
  143. int dat_id_;
  144. bool dat_without_patches_;
  145. };
  146. }
  147. }
  148. #endif //LOTRO_DAT_PATCHER_DATFILE_H