123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251 |
- //
- // Created by Иван_Архипов on 01.11.2017.
- //
- #include "SubFile.h"
- #include "BinaryData.h"
- #include "DatFile.h"
- #include "SubfileData.h"
- #include "EasyLogging++/easylogging++.h"
- #include "DatOperationResult.h"
- #include "Subfiles/TextSubFile.h"
- #include "Subfiles/DdsSubFile.h"
- #include "Subfiles/FontSubFile.h"
- #include "Subfiles/JpgSubFile.h"
- #include "Subfiles/OggSubFile.h"
- #include "Subfiles/WavSubFile.h"
- #include "Subfiles/UnknownSubFile.h"
- #include <algorithm>
- #include <SubFile.h>
- const long long MAXSIZE = 50ll * 1024ll * 1024ll; // Setting maximal file size 50 MB; Files with size more than 50 mb
- // will be recognized as incorrect and passed.
- // This should be done because of not completely correct implementation
- // of Subfiles and Subdirectories search in DatFile.
- namespace LOTRO_DAT {
- SubFile::SubFile() : category(0), dat_(nullptr), unknown1_(0), file_id_(0), file_offset_(0), file_size_(0),
- timestamp_(0), version_(0), block_size_(0), unknown2_(0) {
- }
- SubFile::SubFile(DatFile &dat, const BinaryData &header) : dat_(&dat) {
- category = 0;
- unknown1_ = header.ToNumber<4>(0); // unknown1
- file_id_ = header.ToNumber<4>(4); // file_id
- file_offset_ = header.ToNumber<4>(8); // file_offset
- file_size_ = header.ToNumber<4>(12); // block_size
- timestamp_ = header.ToNumber<4>(16); // timestamp
- version_ = header.ToNumber<4>(20); // version
- block_size_ = header.ToNumber<4>(24); // block_size
- unknown2_ = header.ToNumber<4>(28); // unknown2
- if (file_size_ > MAXSIZE) {
- LOG(ERROR) << "Bad SubFile::SubFile() - File size of file " << file_id << " with offset " << file_offset
- << " is too much... Maybe it's incorrect..?";
- file_id_ = -1;
- return;
- }
- }
- SubFile::SubFile(DatFile &dat, long long dictionary_offset, long long unknown1, long long file_id,
- long long file_offset, long long file_size, long long timestamp, long long version,
- long long block_size, long long unknown2) :
- category(0), dat_(&dat), dictionary_offset_(dictionary_offset), unknown1_(unknown1), file_id_(file_id),
- file_offset_(file_offset), file_size_(file_size), timestamp_(timestamp), version_(version),
- block_size_(block_size), unknown2_(unknown2) {
- if (file_size_ > MAXSIZE) {
- LOG(ERROR) << "Bad SubFile::SubFile() - File size of file " << file_id << " with offset " << file_offset
- << " is too much... Maybe it's incorrect..?";
- file_id_ = -1;
- return;
- }
- }
- /// Typical getters of private fields, if there's need for getting information about SubFile from outside class.
- long long SubFile::dictionary_offset() const {
- return dictionary_offset_;
- }
- long long SubFile::unknown1() const {
- return unknown1_;
- }
- long long SubFile::file_id() const {
- return file_id_;
- }
- long long SubFile::file_offset() const {
- return file_offset_;
- }
- long long SubFile::file_size() const {
- return file_size_;
- }
- long long SubFile::timestamp() const {
- return timestamp_;
- }
- long long SubFile::version() const {
- return version_;
- }
- long long SubFile::block_size() const {
- return block_size_;
- }
- long long SubFile::unknown2() const {
- return unknown2_;
- }
- /// bool Subfile::FileType(...);
- /// Virtual function, can (and should) be redefined in child class, otherwise an exception will be thrown while exporting/importing file.
- /// Returns enum FILE_TYPE value, which is declared in DatFile.h
- FILE_TYPE SubFile::FileType() const {
- LOG(ERROR) << "INCORRECT IMPLEMENTATION!";
- return UNKNOWN;
- }
- /// std::string Subfile::Extension()
- /// Virtual function, can (and should) be redefined in child class, otherwise an exception will be thrown while exporting/importing file.
- /// Returns std::string with extension, beggined with '.', ex. ".jpg", ".txt" and so on;
- std::string SubFile::Extension() const {
- LOG(ERROR) << "INCORRECT IMPLEMENTATION!";
- return ".subfile";
- }
- /// bool Subfile::PrepareForExport(...);
- /// Virtual function, can be redefined in child class, otherwise an exception will be thrown while exporting file.
- /// Takes constant BinaryData& file_data, which contains all file data in .dat file, except first 8 bytes before file_id.
- /// Takes references to export_size - amount of exported files/strings, and content of exported data such as:
- /// 1) field binary_data - exported as RAW data
- /// 2) field text_data - UTF-16 text, exporting in UTF-8
- /// 3) field options - YAML field, which consists of some parameters of file such as file_id, extension and so on.
- /// Returns true if preparation was success. Otherwise returns false;
- SubfileData SubFile::PrepareForExport(const BinaryData &) {
- LOG(ERROR) << "INCORRECT IMPLEMENTATION!";
- return SubfileData();
- }
- /// BinaryData Subfile::PrepareForImport(...);
- /// Virtual function, can be redefined in child class, otherwise an exception will be thrown while importing file.
- /// Takes constant BinaryData& file_data, which contains all file data in .dat file, including first 8 bytes befire file_id.
- /// 1) const field binary_data - exported as RAW data
- /// 2) const field text_data - UTF-16 text, exporting in UTF-8
- /// 3) const field options - YAML field, which consists of some parameters of file such as file_id, extension and so on.
- /// Returns BinaryData - bytes array, prepared for writing in .dat file
- BinaryData SubFile::MakeForImport(const BinaryData &, const SubfileData &) {
- LOG(ERROR) << "INCORRECT IMPLEMENTATION!";
- return BinaryData(0);
- }
- BinaryData SubFile::MakeHeaderData() const {
- BinaryData header = BinaryData::FromNumber<4>(unknown1_)
- + BinaryData::FromNumber<4>(file_id_)
- + BinaryData::FromNumber<4>(file_offset_)
- + BinaryData::FromNumber<4>(file_size_)
- + BinaryData::FromNumber<4>(timestamp_)
- + BinaryData::FromNumber<4>(version_)
- + BinaryData::FromNumber<4>(block_size_)
- + BinaryData::FromNumber<4>(unknown2_);
- return header;
- }
- bool SubFile::operator==(const SubFile &b) const {
- return unknown1_ == b.unknown1_
- && file_id_ == b.file_id_
- && file_offset_ == b.file_offset_
- && file_size_ == b.file_size_
- && timestamp_ == b.timestamp_
- && version_ == b.version_
- && block_size_ == b.block_size_
- && unknown2_ == b.unknown2_;
- }
- bool SubFile::operator!=(const SubFile &b) const {
- return !(*this == b);
- }
- std::shared_ptr<SubFile> SubFile::MakeSubfile(DatFile &dat, SubFile preinit_file) {
- FILE_TYPE type = SubFile::GetSubfileType(dat, preinit_file);
- switch (type) {
- case TEXT:
- return std::dynamic_pointer_cast<SubFile>(std::make_shared<TextSubFile>(preinit_file));
- case JPG:
- return std::dynamic_pointer_cast<SubFile>(std::make_shared<JpgSubFile>(preinit_file));
- case DDS:
- return std::dynamic_pointer_cast<SubFile>(std::make_shared<DdsSubFile>(preinit_file));
- case WAV:
- return std::dynamic_pointer_cast<SubFile>(std::make_shared<WavSubFile>(preinit_file));
- case OGG:
- return std::dynamic_pointer_cast<SubFile>(std::make_shared<OggSubFile>(preinit_file));
- case FONT:
- return std::dynamic_pointer_cast<SubFile>(std::make_shared<FontSubFile>(preinit_file));
- case UNKNOWN:
- return std::dynamic_pointer_cast<SubFile>(std::make_shared<UnknownSubFile>(preinit_file));
- }
- LOG(ERROR) << "Incorrect file type..";
- return std::dynamic_pointer_cast<SubFile>(std::make_shared<UnknownSubFile>(preinit_file));
- }
- FILE_TYPE SubFile::GetSubfileType(DatFile &dat, SubFile preinit_file) {
- // Text check based on file_id
- if (((unsigned long long) preinit_file.file_id_ >> 24ull) == 0x25ull)
- return TEXT;
- // Font check based on file_id
- if (((unsigned long long) preinit_file.file_id_ >> 24ull) == 0x42ull)
- return FONT;
- BinaryData header(64);
- dat.getIO().ReadData(header, 64, (unsigned) preinit_file.file_offset_ + 8);
- if (header.Empty()) {
- LOG(ERROR) << "Unable to read file header. file_id = " << preinit_file.file_id_ << ", offset = "
- << preinit_file.file_offset_;
- return UNKNOWN;
- }
- // jpeg / dds check
- if (((unsigned long long) preinit_file.file_id_ >> 24ull) == 0x41ull) {
- long long soi = header.ToNumber<2>(24);
- long long marker = header.ToNumber<2>(26);
- //auto markerSize = header.ToNumber<short>(28);
- //auto four = header.ToNumber<int>(30);
- if ((soi == 0xD8FFll && marker == 0xE0FFll) || marker == 0xE1FFll)
- return JPG;
- return DDS;
- }
- // Ogg and Wav check
- if (header[8] == 0x4F && header[9] == 0x67 && header[10] == 0x67 && header[11] == 0x53)
- return OGG;
- if (header[8] == 0x52 && header[9] == 0x49 && header[10] == 0x46 && header[11] == 0x46)
- return WAV;
- return UNKNOWN;
- }
- };
|