// // Created by Иван_Архипов on 07.11.2017. // #include "SubDirectory.h" #include "DatFile.h" #include "DatException.h" #include "Subfile.h" #include "BinaryData.h" #include "EasyLogging++/easylogging++.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" namespace LOTRO_DAT { SubDirectory::SubDirectory() { offset_ = 0; } SubDirectory::~SubDirectory() { subfiles_.clear(); subdirs_.clear(); } SubDirectory::SubDirectory(long long offset, DatFile *dat, long long max_subdirs) : dat_(dat), offset_(offset), max_subdirs_(max_subdirs) { LOG(DEBUG) << "Initialising " << offset_ << " SubDirectory"; LOG(DEBUG) << "Processing SubDirectories"; if (!MakeSubDirectories()) { subfiles_.clear(); subdirs_.clear(); return; } LOG(DEBUG) << "Processing SubFiles"; if (!MakeSubFiles()) { subfiles_.clear(); subdirs_.clear(); return; } LOG(DEBUG) << "SubDirectory " << offset_ << " initialized successfully"; } bool SubDirectory::MakeSubDirectories() { BinaryData data(1024); dat_->ReadData(data, 63 * 8, offset_); if (data.Empty()) { LOG(ERROR) << "(READ ERROR) Incorrect directory at offset " << offset_; return false; } if (data.ToNumber<4>(0) != 0 || data.ToNumber<4>(4) != 0) { LOG(DEBUG) << "first 8 bytes are not equal to 0 at offset " << offset_; return false; } for (unsigned int i = 8; i < 63 * 8; i += 8) { if (data.ToNumber<4>(i) == 0 || data.ToNumber<4>(i + 4) == 0) break; SubDirectory *subdir = new SubDirectory(data.ToNumber<4>(i + 4), dat_); if (subdir->subfiles_.empty() && subdir->subdirs_.empty()) { LOG(WARNING) << "Sub-subdirectory is empty or made empty... Dictionary offset = " << offset_ + i << "; Passing others"; break; } else subdirs_.push_back(subdir); } return true; } bool SubDirectory::MakeSubFiles() { BinaryData data = BinaryData(4); dat_->ReadData(data, 4, offset_ + 63 * 8); if (data.Empty()) { LOG(ERROR) << "(READ ERROR) Incorrect directory at offset " << offset_; return false; } auto subfiles_number = data.ToNumber<4>(0); if (subfiles_number >= 64) { LOG(ERROR) << "Incorrect directory at offset " << offset_; return false; } for (int i = 0; i < subfiles_number; i++) { BinaryData header(32); dat_->ReadData(header, 32, offset_ + 63 * 8 + 4 + 32 * i); if (header.Empty()) { LOG(ERROR) << "(READ ERROR) Incorrect directory at offset " << offset_; return false; } if (header.ToNumber<4>(20) == 0 || header.ToNumber<4>(28) != 0) continue; subfiles_.push_back( MakeSubfile( offset_ + 63 * 8 + 4 + 32 * i, header.ToNumber<4>(0), // unknown1 header.ToNumber<4>(4), // file_id header.ToNumber<4>(8), // file_offset header.ToNumber<4>(12), // file_size header.ToNumber<4>(16), // timestamp header.ToNumber<4>(20), // version header.ToNumber<4>(24), // block_size header.ToNumber<4>(28) // unknown2 - must be zero?? ) ); if (!dat_->CorrectSubfile(subfiles_.back())) { LOG(WARNING) << "Incorrect Subfile in directory at offset " << offset_ + 63 * 8 + 4 + 32 * i; subfiles_.pop_back(); //break; } } return true; } void SubDirectory::MakeDictionary(std::unordered_map &dict) { for (Subfile *i : subfiles_) { if (dict.count(i->file_id() != 0)) { LOG(WARNING) << "Found multiple instances of file " << i->file_id() << " at dictionary offset " << i->dictionary_offset() << ". Base offset = " << dict[i->file_id()]->dictionary_offset(); continue; } dict[i->file_id()] = i; } for (SubDirectory *i : subdirs_) i->MakeDictionary(dict); } Subfile *SubDirectory::MakeSubfile(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) { FILE_TYPE type = GetSubfileType(file_id, file_offset); switch (type) { case TEXT: return dynamic_cast(new TextSubfile(dat_, dictionary_offset, unknown1, file_id, file_offset, file_size, timestamp, version, block_size, unknown2)); case JPG: return dynamic_cast(new JpgSubfile(dat_, dictionary_offset, unknown1, file_id, file_offset, file_size, timestamp, version, block_size, unknown2)); case DDS: return dynamic_cast(new DdsSubfile(dat_, dictionary_offset, unknown1, file_id, file_offset, file_size, timestamp, version, block_size, unknown2)); case WAV: return dynamic_cast(new WavSubfile(dat_, dictionary_offset, unknown1, file_id, file_offset, file_size, timestamp, version, block_size, unknown2)); case OGG: return dynamic_cast(new OggSubfile(dat_, dictionary_offset, unknown1, file_id, file_offset, file_size, timestamp, version, block_size, unknown2)); case FONT: return dynamic_cast(new FontSubfile(dat_, dictionary_offset, unknown1, file_id, file_offset, file_size, timestamp, version, block_size, unknown2)); case UNKNOWN: return dynamic_cast(new UnknownSubfile(dat_, dictionary_offset, unknown1, file_id, file_offset, file_size, timestamp, version, block_size, unknown2)); } LOG(ERROR) << "Incorrect file type.."; return dynamic_cast(new UnknownSubfile(dat_, dictionary_offset, unknown1, file_id, file_offset, file_size, timestamp, version, block_size, unknown2)); } FILE_TYPE SubDirectory::GetSubfileType(long long file_id, long long file_offset) const { // Text check based on file_id if ((file_id >> 24ll) == 0x25ll) return TEXT; // Font check based on file_id if ((file_id >> 24ll) == 0x42ll) return FONT; BinaryData header(64); try { dat_->ReadData(header, 64, (unsigned) file_offset + 8); }catch (DatException &e) { if (e.type() == READ_EXCEPTION) { LOG(ERROR) << "Unable to read file header. file_id = " << file_id << ", offset = " << offset_; std::string err = "Bad Subfile::getExtension() - unable to read header of file with id = " + std::to_string(file_id) + " and offset = " + std::to_string(file_offset); throw DatException(err.c_str(), SUBFILE_EXCEPTION); } else throw e; } // jpeg / dds check if ((file_id >> 24ll) == 0x41ll) { long long soi = header.ToNumber<2>(24); long long marker = header.ToNumber<2>(26); //auto markerSize = header.ToNumber(28); //auto four = header.ToNumber(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; } };