Subfile.cpp 8.3 KB

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