// // Created by Иван_Архипов on 31.10.2017. // #include "BinaryData.h" #include "DatException.h" extern "C++" { namespace LOTRO_DAT { BinaryData::BinaryData() { data_ = nullptr; size_ = 0; } BinaryData::BinaryData(const BinaryData &d) { size_ = d.size_; data_ = new unsigned char[size_]; memcpy(data_, d.data_, size_); } BinaryData::BinaryData(unsigned int size) { data_ = new unsigned char[size]; size_ = size; } BinaryData::~BinaryData() { if (size_ != 0 && data_ != nullptr) delete[] data_; } unsigned char& BinaryData::operator[](const unsigned int &pos) { if (pos >= size_) throw DatException("Bad BinaryData::operator[]. Position is out of range."); return data_[pos]; } // Translates data into specified number type due to UTF-16LE encoding of the .dat file template long long BinaryData::ToNumber(const long long &pos) const { try { long long ans = 0; if (pos + 3 >= size_) throw DatException("Bad BinaryData::ToNumber(). Reached end of BinaryData!", DATA_EXCEPTION); for (int i = T - 1; i >= 0; i--) ans = ((ans << 8ll) | (unsigned char)(data_[pos + i])); return ans; } catch (...) { throw DatException("Bad BinaryData::ToNumber(). Error while using template function", DATA_EXCEPTION); } } // Makes data from specified number type template void BinaryData::FromNumber(const long long &number) { if (T < 0) throw DatException("Bad BinaryData::FromNumber() - trying to make data from amount of bytes < 0"); try { if (size_ != 0 && data_ != nullptr) delete[] data_; size_ = size_t(T); data_ = new unsigned char[size_]; for (size_t i = 0; i < size_; i++) { data_[i] = (unsigned char)((number >> (8 * i)) & 0xFF); } } catch (...) { throw DatException("Bad BinaryData::ToNumber(). Error in using template function", DATA_EXCEPTION); } } BinaryData &BinaryData::operator=(const BinaryData &data) { if (&data == this) return *this; if (size_ != 0 && data_ != nullptr) delete[] data_; size_ = data.size_; data_ = nullptr; if (size_ != 0) { data_ = new unsigned char[size_]; memcpy(data_, data.data_, size_); } return *this; } size_t BinaryData::size() const { return size_; } unsigned char *BinaryData::data() const { return data_; } bool BinaryData::WriteToFile(const char *filename) const { FILE *f; fopen_s(&f, filename, "wb"); if (f == nullptr) { throw DatException("Bad BinaryData::WriteToFile() - unable to open output file", EXPORT_EXCEPTION); return false; } fwrite(data(), size(), 1, f); fclose(f); return true; } bool BinaryData::WriteToFile(const std::string &filename) const { FILE *f; fopen_s(&f, filename.c_str(), "wb"); if (f == nullptr) { throw DatException("Bad BinaryData::WriteToFile() - unable to open output file", EXPORT_EXCEPTION); return false; } fwrite(data(), size(), 1, f); fclose(f); return true; } template long long BinaryData::ToNumber<1>(long long const&) const; template long long BinaryData::ToNumber<2>(long long const&) const; template long long BinaryData::ToNumber<4>(long long const&) const; template long long BinaryData::ToNumber<8>(long long const&) const; template void BinaryData::FromNumber<1>(const long long &); template void BinaryData::FromNumber<2>(const long long &); template void BinaryData::FromNumber<4>(const long long &); template void BinaryData::FromNumber<8>(const long long &); } }