DatFile.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. //
  2. // Created by Иван_Архипов on 31.10.2017.
  3. //
  4. #include "DatFile.h"
  5. #include "BinaryData.h"
  6. #include "Common/DatException.h"
  7. #include "SubDirectory.h"
  8. #include "Subfile.h"
  9. #include <locale>
  10. extern "C++"
  11. {
  12. namespace LOTRO_DAT {
  13. DatFile::DatFile() {
  14. dat_state_ = CLOSED;
  15. }
  16. DatFile::DatFile(const char *filename, int dat_id) {
  17. dat_id_ = dat_id;
  18. dat_state_ = CLOSED;
  19. OpenDatFile(filename);
  20. ReadSuperBlock();
  21. MakeDirectories();
  22. try {
  23. MakeDictionary();
  24. } catch (...) {
  25. fprintf(stderr, "Unable to make dictionary!! Unable to init DatFile!!!");
  26. return;
  27. }
  28. if (dat_state_ == SUCCESS_DICTIONARY)
  29. dat_state_ = READY;
  30. else
  31. throw DatException("Bad DatFile initialization! Not all init states were successfully passed!",
  32. INIT_EXCEPTION);
  33. }
  34. DatFile::DatFile(const std::string &filename, int dat_id) {
  35. dat_id_ = dat_id;
  36. dat_state_ = CLOSED;
  37. OpenDatFile(filename.c_str());
  38. ReadSuperBlock();
  39. MakeDirectories();
  40. MakeDictionary();
  41. if (dat_state_ == SUCCESS_DICTIONARY)
  42. dat_state_ = READY;
  43. else
  44. throw DatException("Bad DatFile initialization! Not all init states were successfully passed!",
  45. INIT_EXCEPTION);
  46. }
  47. DatFile::~DatFile() {
  48. if (file_handler_ != nullptr)
  49. fclose(file_handler_);
  50. delete file_handler_;
  51. delete root_directory_;
  52. }
  53. /// Extracts file with file_id.
  54. /// If path is undefined then it will be recognised as current working directory
  55. /// Output file path consists of "path + file_id + file_extension";
  56. /// NOTICE: The directory, mentioned in "std::string path" variable SHOULD BE ALREADY CREATED;
  57. /// Otherwise DatException() will be thrown.
  58. /// Returns true, if file was successfully extracted;
  59. /// Throws DatException() if undefined behaviour happened
  60. bool DatFile::ExtractFile(long long file_id, const std::string path) {
  61. if (dat_state_ != READY) {
  62. throw DatException("Bad DatFile::ExtractFile() - invalid DatFile state!", EXPORT_EXCEPTION);
  63. }
  64. return dictionary_[file_id]->ExportFile(path.c_str());
  65. }
  66. /// Extracts file with file_id to database "db".
  67. /// DATABASE SHOULD BE ALREADY CREATED; Otherwise DatException will be called.
  68. /// NOTICE: The directory, mentioned in "std::string path" variable SHOULD BE ALREADY CREATED;
  69. /// Otherwise DatException() will be thrown.
  70. /// Returns true, if file was successfully extracted;
  71. /// Throws DatException() if undefined behaviour happened
  72. bool DatFile::ExtractFile(long long file_id, Database *db) {
  73. if (dat_state_ != READY) {
  74. throw DatException("Bad DatFile::ExtractFile() - invalid DatFile state!", EXPORT_EXCEPTION);
  75. }
  76. return dictionary_[file_id]->ExportFile(db);
  77. }
  78. /// Extracts all files with specific type to "path + type + file_id + extension" files;
  79. /// If path is undefined then it will be recognised as current working directory
  80. /// NOTICE: The directory, mentioned in "std::string path" variable SHOULD BE ALREADY CREATED;
  81. /// Otherwise DatException() will be thrown.
  82. /// Returns number of successfully extracted files
  83. /// Throws DatException() if undefined behaviour happened
  84. int DatFile::ExtractAllFilesByType(FILE_TYPE type, std::string path) {
  85. if (dat_state_ != READY) {
  86. throw DatException("Bad DatFile::ExtractAllFilesByType() - invalid DatFile state!", EXPORT_EXCEPTION);
  87. }
  88. int success = 0;
  89. for (auto i : dictionary_) {
  90. FILE_TYPE ext = i.second->ext();
  91. if (ext == type) {
  92. success += i.second->ExportFile((path + std::to_string(i.second->file_id())).c_str());
  93. }
  94. }
  95. return success;
  96. }
  97. /// Extracts all files with specific type to database "db";
  98. /// DATABASE SHOULD BE ALREADY CREATED; Otherwise DatException will be called.
  99. /// Returns number of successfully extracted files
  100. /// Throws DatException() if undefined behaviour happened
  101. int DatFile::ExtractAllFilesByType(FILE_TYPE type, Database *db) {
  102. if (dat_state_ != READY) {
  103. throw DatException("Bad DatFile::ExtractAllFilesByType() - invalid DatFile state!", EXPORT_EXCEPTION);
  104. }
  105. int success = 0;
  106. for (auto i : dictionary_) {
  107. FILE_TYPE ext = i.second->ext();
  108. if (ext == type) {
  109. success += i.second->ExportFile(db);
  110. }
  111. }
  112. return success;
  113. }
  114. /// Patches .dat with text file with specific file_id, gossip_id and values, equal to values in text database.
  115. /// std::string text contains of text data, surrounded by '[' and ']', where <--DO_NOT_TOUCH!--> represents
  116. /// position of variables in order, described in args_order.
  117. /// std::string args_order contains of numbers, divided by '-'. Ex. "1-2-3-4". There should be not less numbers, than
  118. /// <--DO_NOT_TOUCH!--> pieces in text variable/
  119. /// std::string args contains of numbers, divided by ' '. These numbers are references to variables. There should
  120. /// be not less references, than the biggest number in args_order
  121. /// Returns true if text was succesfully patched.
  122. /// Throws DatException() if undefined behaviour happened;
  123. bool DatFile::PatchTextFile(long long file_id, long long gossip_id, std::string text, std::string args_order,
  124. std::string args) {
  125. }
  126. /// Patches .dat with text file with specific file_id and gossip_id.
  127. /// All text file data is got from database.
  128. /// Returns true if text file was successfully patched.
  129. /// Throws DatException() if undefined behaviour happened;
  130. bool DatFile::PatchTextFile(long long file_id, long long gossip_id, Database *db) {
  131. }
  132. /// Patches .dat with binary file with specific file_id.
  133. /// All file data is got from file in "file_path".
  134. /// Returns true if file was successfully patched.
  135. /// Throws DatException() if undefined behaviour happened;
  136. bool DatFile::PatchBinaryFile(long long file_id, std::string file_path) {
  137. }
  138. /// Patches .dat with binary file with specific file_id.
  139. /// All file data is got from Database "db".
  140. /// Returns true if file was successfully patched.
  141. /// Throws DatException() if undefined behaviour happened;
  142. bool DatFile::PatchBinaryFile(long long file_id, Database *db) {
  143. }
  144. void DatFile::OpenDatFile(const char *dat_name) {
  145. if (dat_state_ != CLOSED)
  146. throw DatException("Bad initialisation of DatFile - current DatFile isn't in correct state!",
  147. INIT_EXCEPTION);
  148. fopen_s(&file_handler_, dat_name, "r+b");
  149. if (file_handler_ == nullptr) {
  150. std::string err = "Bad DatFile::OpenDatFile. Unable to open file ";
  151. err += dat_name;
  152. throw DatException(err.c_str(), INIT_EXCEPTION);
  153. }
  154. _fseeki64(file_handler_, 0, SEEK_END);
  155. file_size_ = _ftelli64(file_handler_);
  156. _fseeki64(file_handler_, 0, SEEK_SET);
  157. dat_state_ = SUCCESS_OPENED;
  158. }
  159. void DatFile::ReadSuperBlock() {
  160. if (dat_state_ != SUCCESS_OPENED)
  161. throw DatException("Bad DatFile::ReadSuperBlock() - DatFile isn't in valid state!", INIT_EXCEPTION);
  162. BinaryData data(1024);
  163. ReadData(data, 1024);
  164. constant1_ = data.ToNumber<4>(0x100);
  165. constant2_ = data.ToNumber<4>(0x140);
  166. version1_ = data.ToNumber<4>(0x14C);
  167. version2_ = data.ToNumber<4>(0x150);
  168. root_directory_offset_ = data.ToNumber<4>(0x160);
  169. auto size1 = data.ToNumber<4>(0x148);
  170. if (constant1_ != 0x4C5000)
  171. throw DatException(
  172. "Bad DatFile::ReadSuperBlock - variable at position 0x100 is not equal to .dat file constant!",
  173. INIT_EXCEPTION);
  174. if (constant2_ != 0x5442)
  175. throw DatException(
  176. "Bad DatFile::ReadSuperBlock - variable at position 0x140 is not equal to .dat file constant!",
  177. INIT_EXCEPTION);
  178. if (file_size_ != size1)
  179. throw DatException(
  180. "Bad DatFile::ReadSuperBlock - variable at 0x148 position is not equal to .dat file size!",
  181. INIT_EXCEPTION);
  182. dat_state_ = SUCCESS_SUPERBLOCK;
  183. }
  184. void DatFile::MakeDirectories() {
  185. if (dat_state_ != SUCCESS_SUPERBLOCK)
  186. throw DatException("Bad DatFile::MakeDirectories() - DatFile isn't in valid state!", INIT_EXCEPTION);
  187. root_directory_ = new SubDirectory((unsigned) root_directory_offset_, this);
  188. dat_state_ = SUCCESS_DIRECTORIES;
  189. }
  190. void DatFile::MakeDictionary() {
  191. if (dat_state_ != SUCCESS_DIRECTORIES)
  192. throw DatException("Bad DatFile::MakeDictionary() - DatFile isn't in valid state!", INIT_EXCEPTION);
  193. try {
  194. root_directory_->MakeDictionary(dictionary_);
  195. } catch (...) {
  196. fprintf(stderr, "Bad DatFile::MakeDictionary() - File is corrupted?\n");
  197. return;
  198. }
  199. dat_state_ = SUCCESS_DICTIONARY;
  200. }
  201. void DatFile::ReadData(BinaryData &data, long long size, long long offset, long long data_offset) {
  202. if (dat_state_ == CLOSED)
  203. throw DatException("Bad DatFile::ReadData() - DatFile isn't in valid state!", READ_EXCEPTION);
  204. if (data_offset + size > data.size()) {
  205. std::string err = "Bad DatFile::ReadData - trying to read more than BinaryData size\n";
  206. err += std::string("Reading ") + std::to_string(size) + std::string(" bytes from ")
  207. + std::to_string(offset) + std::string(" position in dat file.");
  208. throw DatException(err.c_str(), READ_EXCEPTION);
  209. }
  210. if (offset + size > file_size()) {
  211. std::string err = "Bad DatFile::ReadData - trying to read more than DatFile size elapsed\n";
  212. err += std::string("Reading ") + std::to_string(size) + std::string(" bytes from ")
  213. + std::to_string(offset) + std::string(" position in dat file.");
  214. throw DatException(err.c_str(), READ_EXCEPTION);
  215. }
  216. _fseeki64(file_handler_, offset, SEEK_SET);
  217. fread(data.data() + data_offset, size, 1, file_handler_);
  218. data.CheckCompression();
  219. }
  220. void DatFile::WriteData(const BinaryData &data, long long size, long long offset, long long data_offset) {
  221. if (dat_state_ != READY)
  222. throw DatException("Bad DatFile::WriteData() - DatFile isn't in valid state!", WRITE_EXCEPTION);
  223. _fseeki64(file_handler_, offset, SEEK_SET);
  224. if (data_offset + size > data.size())
  225. throw DatException("Bad DatFile::WriteData - trying to write more than BinaryData size", WRITE_EXCEPTION);
  226. fwrite(data.data() + data_offset, size, 1, file_handler_);
  227. }
  228. long long DatFile::constant1() const {
  229. return constant1_;
  230. }
  231. long long DatFile::constant2() const {
  232. return constant2_;
  233. }
  234. long long DatFile::file_size() const {
  235. return file_size_;
  236. }
  237. long long DatFile::version1() const {
  238. return version1_;
  239. }
  240. long long DatFile::version2() const {
  241. return version2_;
  242. }
  243. void DatFile::WriteUnorderedDictionary(std::string path) const {
  244. FILE *f;
  245. fopen_s(&f, (path + "dict.txt").c_str(), "w");
  246. fprintf(f, "file_id offset size size2 extension\n");
  247. for (auto i : dictionary_) {
  248. std::string extension = "unk";
  249. FILE_TYPE ext = i.second->ext();
  250. if (ext == TEXT) extension = "txt";
  251. if (ext == JPG) extension = "jpg";
  252. if (ext == DDS) extension = "dds";
  253. if (ext == WAV) extension = "wav";
  254. if (ext == OGG) extension = "ogg";
  255. if (ext == FONT) extension = "font";
  256. fprintf(f, "%lld %lld %lld %lld %s\n", i.second->file_id(), i.second->file_offset(), i.second->file_size(),
  257. i.second->block_size(), extension.c_str());
  258. }
  259. fclose(f);
  260. }
  261. long long DatFile::files_number() const {
  262. return dictionary_.size();
  263. }
  264. }
  265. }