Subfile.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. //
  2. // Created by Иван_Архипов on 01.11.2017.
  3. //
  4. #include "Subfile.h"
  5. #include "BinaryData.h"
  6. #include "DatFile.h"
  7. #include "DatException.h"
  8. #include "Database.h"
  9. #include <iostream>
  10. const long long MAXSIZE = 50ll * 1024ll * 1024ll; // Setting maximal file size 50 MB; Files with size more than 50 mb
  11. // will be recognized as incorrect and passed.
  12. LOTRO_DAT::Subfile::Subfile() {
  13. ext_ = UNKNOWN;
  14. compressed_ = false;
  15. }
  16. LOTRO_DAT::Subfile::Subfile(DatFile *dat, long long unknown0, long long unknown1, long long file_id, long long file_offset, long long size1,
  17. long long timestamp, long long version, long long size2) :
  18. dat_(dat), unknown0_(unknown0), unknown1_(unknown1), file_id_(file_id), file_offset_(file_offset + 8),
  19. size1_(size1), timestamp_(timestamp), version_(version), size2_(size2) {
  20. if (size1_ > MAXSIZE)
  21. throw DatException("Bad Subfile::Subfile() - File size is too much... Maybe it's incorrect..?", SUBFILE_EXCEPTION);
  22. ext_ = GetExtension();
  23. BinaryData header(20);
  24. dat_->ReadData(header, 10, file_offset_);
  25. compressed_ = header.CheckCompression();
  26. }
  27. long long LOTRO_DAT::Subfile::unknown0() const {
  28. return unknown0_;
  29. }
  30. long long LOTRO_DAT::Subfile::unknown1() const {
  31. return unknown1_;
  32. }
  33. long long LOTRO_DAT::Subfile::file_id() const {
  34. return file_id_;
  35. }
  36. long long LOTRO_DAT::Subfile::file_offset() const {
  37. return file_offset_;
  38. }
  39. long long LOTRO_DAT::Subfile::size1() const {
  40. return size1_;
  41. }
  42. long long LOTRO_DAT::Subfile::timestamp() const {
  43. return timestamp_;
  44. }
  45. long long LOTRO_DAT::Subfile::version() const {
  46. return version_;
  47. }
  48. long long LOTRO_DAT::Subfile::size2() const {
  49. return size2_;
  50. }
  51. LOTRO_DAT::EXTENSION LOTRO_DAT::Subfile::GetExtension() const {
  52. // Text check based on file_id
  53. if ((file_id_ >> 24ll) == 0x25ll)
  54. return TXT;
  55. // Font check based on file_id
  56. if ((file_id_ >> 24ll) == 0x42ll)
  57. return FONT;
  58. BinaryData header(64);
  59. try {
  60. dat_->ReadData(header, 64, (unsigned) file_offset_);
  61. } catch (DatException &e) {
  62. if (e.type() == READ_EXCEPTION) {
  63. std::string err =
  64. "Bad Subfile::getExtension() - unable to read header of file with id = " + std::to_string(file_id()) +
  65. " and offset = " + std::to_string(file_offset());
  66. throw DatException(err.c_str(), SUBFILE_EXCEPTION);
  67. } else
  68. throw e;
  69. }
  70. // jpeg / dds check
  71. if ((file_id_ >> 24ll) == 0x41ll) {
  72. long long soi = header.ToNumber<2>(24);
  73. long long marker = header.ToNumber<2>(26);
  74. //auto markerSize = header.ToNumber<short>(28);
  75. //auto four = header.ToNumber<int>(30);
  76. if (soi == 0xD8FFll && marker == 0xE0FFll || marker == 0xE1FFll)
  77. return JPG;
  78. return DDS;
  79. }
  80. // Ogg and Wav check
  81. if (header[8] == 0x4F && header[9] == 0x67 && header[10] == 0x67 && header[11] == 0x53)
  82. return OGG;
  83. if (header[8] == 0x52 && header[9] == 0x49 && header[10] == 0x46 && header[11] == 0x46)
  84. return WAV;
  85. return UNKNOWN;
  86. }
  87. LOTRO_DAT::EXTENSION LOTRO_DAT::Subfile::ext() const {
  88. return ext_;
  89. }
  90. std::string LOTRO_DAT::Subfile::ExtensionToString(LOTRO_DAT::EXTENSION ext) const {
  91. switch (ext)
  92. {
  93. case LOTRO_DAT::TXT:
  94. return ".txt";
  95. case LOTRO_DAT::JPG:
  96. return ".jpg";
  97. case LOTRO_DAT::DDS:
  98. return ".dds";
  99. case LOTRO_DAT::WAV:
  100. return ".wav";
  101. case LOTRO_DAT::OGG:
  102. return ".ogg";
  103. case LOTRO_DAT::FONT:
  104. return ".fontbin";
  105. case LOTRO_DAT::UNKNOWN:
  106. return ".unk";
  107. default:
  108. return "";
  109. }
  110. }
  111. bool LOTRO_DAT::Subfile::ExportFile(const char *filename, Database *db) const {
  112. try {
  113. BinaryData data;
  114. switch (ext_)
  115. {
  116. case LOTRO_DAT::TXT:
  117. return ExportFileAsTXT(filename, db);
  118. case LOTRO_DAT::JPG:
  119. data = PrepareAsJPG(); break;
  120. case LOTRO_DAT::DDS:
  121. data = PrepareAsDDS(); break;
  122. case LOTRO_DAT::WAV:
  123. data = PrepareAsWAV(); break;
  124. case LOTRO_DAT::OGG:
  125. data = PrepareAsOGG(); break;
  126. case LOTRO_DAT::FONT:
  127. data = PrepareAsFont(); break;
  128. case LOTRO_DAT::UNKNOWN:
  129. data = PrepareAsUnk(); break;
  130. default:
  131. break;
  132. }
  133. if (db == nullptr) {
  134. return data.WriteToFile(std::string(filename) + std::string(ExtensionToString(ext_)));
  135. } else {
  136. db->PushBinaryFile(file_id_, data);
  137. return true;
  138. }
  139. }
  140. catch (DatException &e) {
  141. fprintf(stderr, "Caught exception while unpacking the file with id %lld and offset %lld. Continuing without this file...\n", file_id(), file_offset());
  142. fprintf(stderr, "%s\n", e.what());
  143. return false;
  144. }
  145. }
  146. bool LOTRO_DAT::Subfile::ExportFileAsTXT(const char *filename, Database *db) const {
  147. throw DatException("ExportAsTXT function is not implemented yet", EXPORT_EXCEPTION);
  148. }
  149. const LOTRO_DAT::BinaryData LOTRO_DAT::Subfile::PrepareAsJPG() const {
  150. BinaryData data((unsigned) size1_ - 24);
  151. dat_->ReadData(data, (unsigned) size1_ - 24, (unsigned) file_offset_ + 24);
  152. return data;
  153. }
  154. const LOTRO_DAT::BinaryData LOTRO_DAT::Subfile::PrepareAsDDS() const {
  155. BinaryData data((unsigned) size1_);
  156. dat_->ReadData(data, (unsigned) size1_, file_offset_);
  157. if (compressed_)
  158. data.DecompressData(4);
  159. BinaryData ddsData(data.size() - 24 + 128);
  160. for (int i = 0; i < 128; i++)
  161. ddsData[i] = 0;
  162. memcpy(ddsData.data() + 128, data.data() + 24, data.size() - 24);
  163. ddsData[0] = 0x44; // D
  164. ddsData[1] = 0x44; // D
  165. ddsData[2] = 0x53; // S
  166. ddsData[3] = 0x20;
  167. ddsData[4] = 0x7C;
  168. ddsData[8] = 7;
  169. ddsData[9] = 0x10;
  170. // width, height
  171. ddsData[12] = data[12];
  172. ddsData[13] = data[13];
  173. ddsData[14] = data[14];
  174. ddsData[15] = data[15];
  175. ddsData[16] = data[8];
  176. ddsData[17] = data[9];
  177. ddsData[18] = data[10];
  178. ddsData[19] = data[11];
  179. long long compression = data.ToNumber<4>(0x10);
  180. switch (compression) {
  181. case 20: // 14 00 00 00 - 888 (R8G8B8)
  182. ddsData[0x4C] = 0x20; // ?
  183. ddsData[0x50] = 0x40; // compressed or not
  184. ddsData[0x58] = 0x18; // bytes per pixel
  185. ddsData[0x5E] = 0xFF;
  186. ddsData[0x61] = 0xFF;
  187. ddsData[0x64] = 0xFF;
  188. break;
  189. case 21: // 15 00 00 00 - 8888 (R8G8B8A8)
  190. ddsData[0x4C] = 0x20; // ?
  191. ddsData[0x50] = 0x40; // compressed or not
  192. ddsData[0x58] = 0x20; // bytes per pixel
  193. ddsData[0x5E] = 0xFF;
  194. ddsData[0x61] = 0xFF;
  195. ddsData[0x64] = 0xFF;
  196. ddsData[0x6B] = 0xFF;
  197. break;
  198. case 28: // 1C 00 00 00 - 332 (?)
  199. ddsData[0x4C] = 0x20; // ?
  200. ddsData[0x50] = 0x40; // compressed or not
  201. ddsData[0x58] = 0x08; // bytes per pixel
  202. ddsData[0x5E] = 0xFF;
  203. ddsData[0x61] = 0xFF;
  204. ddsData[0x64] = 0xFF;
  205. break;
  206. case 827611204: // 44 58 54 31 - DXT1
  207. ddsData[76] = 32;
  208. ddsData[80] = 4;
  209. ddsData[84] = 68;
  210. ddsData[85] = 88;
  211. ddsData[86] = 84;
  212. ddsData[87] = 49;
  213. break;
  214. case 861165636: // 44 58 54 33 - DXT3
  215. ddsData[22] = 1;
  216. ddsData[76] = 32;
  217. ddsData[80] = 4;
  218. ddsData[84] = 68;
  219. ddsData[85] = 88;
  220. ddsData[86] = 84;
  221. ddsData[87] = 51;
  222. ddsData[108] = 8;
  223. ddsData[109] = 16;
  224. ddsData[110] = 64;
  225. break;
  226. case 894720068: // 44 58 54 35 - DXT5
  227. ddsData[10] = 8;
  228. ddsData[22] = 1;
  229. ddsData[28] = 1;
  230. ddsData[76] = 32;
  231. ddsData[80] = 4;
  232. ddsData[84] = 68;
  233. ddsData[85] = 88;
  234. ddsData[86] = 84;
  235. ddsData[87] = 53;
  236. ddsData[88] = 32;
  237. ddsData[94] = 255;
  238. ddsData[97] = 255;
  239. ddsData[100] = 255;
  240. ddsData[107] = 255;
  241. ddsData[109] = 16;
  242. break;
  243. }
  244. return ddsData;
  245. }
  246. const LOTRO_DAT::BinaryData LOTRO_DAT::Subfile::PrepareAsOGG() const {
  247. BinaryData data((unsigned) size1_ - 8);
  248. dat_->ReadData(data, (unsigned) size1_ - 8, (unsigned) file_offset_ + 8);
  249. return data;
  250. }
  251. const LOTRO_DAT::BinaryData LOTRO_DAT::Subfile::PrepareAsFont() const {
  252. throw DatException("Bad Subfile::ExportAsFont() - this function hasn't been implemented yet", EXPORT_EXCEPTION);
  253. }
  254. const LOTRO_DAT::BinaryData LOTRO_DAT::Subfile::PrepareAsWAV() const {
  255. BinaryData data((unsigned) size1_ - 8);
  256. dat_->ReadData(data, (unsigned) size1_ - 8, (unsigned) file_offset_ + 8);
  257. return data;
  258. }
  259. const LOTRO_DAT::BinaryData LOTRO_DAT::Subfile::PrepareAsUnk() const {
  260. throw DatException("Bad Subfile::ExportAsUnk() - this function hasn't been implemented yet", EXPORT_EXCEPTION);
  261. }