Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 40 additions & 3 deletions QtPMbrowser/pmbrowserwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#include <QTableView>
#include <string>
#include <fstream>
#include <filesystem>
#include <iostream>
#include <ctime>
#include <cstring>
Expand Down Expand Up @@ -306,10 +307,46 @@ void PMbrowserWindow::loadFile(QString filename)
datfile->InitFromStream(infile);
}
catch (const std::exception& e) {
QMessageBox::warning(this, QString("File Error"),
QString("error while processing dat file:\n") + QString(e.what()));
//QMessageBox::warning(this, QString("File Error"),
// QString("error while processing dat file:\n") + QString(e.what()));
qDebug() << e.what();
datfile = nullptr;
infile.close();
infile.seekg(0, std::ios_base::beg);
//infile.close();
}
if (!datfile) {
try {
// we might habe an unbundled dat file
datfile = std::make_unique<DatFile>();
std::filesystem::path path(QFile::encodeName(filename).constData());
path.replace_extension(hkLib::ExtPul);
std::ifstream pulstream(path, std::ios_base::binary | std::ios_base::in);
if (!pulstream) {
throw std::runtime_error(std::string("could not open pul file, ") + ::strerror(errno));
}
auto pullength = std::filesystem::file_size(path);
path.replace_extension(hkLib::ExtPgf);
std::ifstream pgfstream(path, std::ios_base::binary | std::ios_base::in);
if (!pgfstream) {
throw std::runtime_error(std::string("could not open pgf file, ") + ::strerror(errno));
}
auto pgflength = std::filesystem::file_size(path);
path.replace_extension(hkLib::ExtAmp);
std::ifstream ampstream(path, std::ios_base::binary | std::ios_base::in);
if (!ampstream) {
datfile->InitFromStream(infile, pulstream, pullength, pgfstream, pgflength, nullptr, 0);
}
else {
datfile->InitFromStream(infile, pulstream, pullength, pgfstream, pgflength, &ampstream, std::filesystem::file_size(path));
}
}
catch (const std::exception& e) {
qDebug() << e.what();
QMessageBox::warning(this, QString("File Error"),
QString("error while processing unbundled dat file:\n") + QString(e.what()));
datfile = nullptr;
infile.close();
}
}
if(datfile) {
populateTreeView();
Expand Down
35 changes: 35 additions & 0 deletions hekatoolslib/DatFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,41 @@ void DatFile::InitFromStream(std::istream& infile)
if (!PgfTree.isValid())throw std::runtime_error("no valid Pgf in file");
}

void hkLib::DatFile::InitFromStream(std::istream& infile, std::istream& pulstream, std::uintmax_t pullength, std::istream& pgfstream,
std::uintmax_t pgflength, std::istream* ampstream, std::uintmax_t amplength)
{
if (!infile) {
throw std::runtime_error("cannot access file");
}
auto bh = std::make_unique<BundleHeader>();
infile.read(reinterpret_cast<char*>(bh.get()), BundleHeaderSize);
if (!infile) {
throw std::runtime_error("cannot read file");
}

bool has_header = std::memcmp(bh->Signature, BundleSignatureInvalid, 8) == 0;
// Note: if it has a valid signature, it should not be handeled by this function
if (has_header) {
Version = bh->Version;
Time = bh->Time;
isSwapped = bool(bh->IsLittleEndian) != MachineIsLittleEndian();
if (isSwapped) {
swapInPlace(Time);
}
}
if(!PulTree.InitFromStream(ExtPul, pulstream, 0, static_cast<unsigned int>(pullength))){
throw std::runtime_error("error processing pulse tree");
}
if (!PgfTree.InitFromStream(ExtPgf, pgfstream, 0, static_cast<unsigned int>(pgflength))) {
throw std::runtime_error("error processing pgf tree");
}
if (ampstream && amplength) {
if (!AmpTree.InitFromStream(ExtAmp, *ampstream, 0, static_cast<unsigned int>(amplength))) {
throw std::runtime_error("error processing amp tree");
}
}
}

std::string DatFile::getFileDate() const
{
#ifndef NDEBUG
Expand Down
19 changes: 18 additions & 1 deletion hekatoolslib/DatFile.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,24 @@ namespace hkLib {
PgfTree{}, AmpTree{} {};
DatFile(const DatFile&) = delete;
DatFile operator=(const DatFile&) = delete;
void InitFromStream(std::istream& istream);
/// <summary>
/// initialize from bundle file stream, reads header and tree data, but not raw data
/// </summary>
/// <param name="istream">input stream of the bundle file</param>
void InitFromStream(std::istream& infile);
/// <summary>
/// initialized from unbundles dat file, requires separate streams for each tree, and their lengths
/// </summary>
/// <param name="infile">dat file stream</param>
/// <param name="pulstream">pul file stream</param>
/// <param name="pullength">length of pul file</param>
/// <param name="pgfstream">pgf file stream</param>
/// <param name="pgflength">length of pgf file</param>
/// <param name="ampstream">pointer to optional amp file stream, can be nullptr</param>
/// <param name="amplength"length of amp file, 0 if no amp file</param>
void InitFromStream(std::istream& infile, std::istream& pulstream, std::uintmax_t pullength,
std::istream& pgfstream, std::uintmax_t pgflength,
std::istream* ampstream, std::uintmax_t amplength);
std::string getFileDate() const; // return formatted file creation date
hkTree& GetPulTree() { return PulTree; };
hkTree& GetPgfTree() { return PgfTree; };
Expand Down
Loading