Subfile.cpp 7.9 KB

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