nod/include/NOD/DiscBase.hpp

196 lines
6.2 KiB
C++
Raw Normal View History

2015-06-26 19:30:03 +00:00
#ifndef __NOD_DISC_BASE__
#define __NOD_DISC_BASE__
2015-06-28 05:43:53 +00:00
#include <vector>
2015-06-26 19:30:03 +00:00
#include <memory>
#include <string>
2015-06-30 00:07:46 +00:00
#include <stdio.h>
#include <stdint.h>
2015-06-28 05:43:53 +00:00
#include "Util.hpp"
2015-06-26 19:30:03 +00:00
#include "IDiscIO.hpp"
#include "IFileIO.hpp"
namespace NOD
{
class DiscBase
{
2015-06-26 20:01:15 +00:00
public:
2015-06-28 05:43:53 +00:00
struct Header
{
char gameID[6];
char discNum;
char discVersion;
char audioStreaming;
char streamBufSz;
char unk[14];
uint32_t wiiMagic;
uint32_t gcnMagic;
char gameTitle[64];
Header(IDiscIO& dio)
{
std::unique_ptr<IDiscIO::IReadStream> s = dio.beginReadStream(0);
s->read(this, sizeof(*this));
wiiMagic = SBig(wiiMagic);
gcnMagic = SBig(gcnMagic);
}
};
2015-06-30 00:07:46 +00:00
class FSTNode
{
uint32_t typeAndNameOffset;
uint32_t offset;
uint32_t length;
public:
inline bool isDir() const {return SBig(typeAndNameOffset) >> 24;}
inline uint32_t getNameOffset() const {return SBig(typeAndNameOffset) & 0xffffff;}
inline uint32_t getOffset() const {return SBig(offset);}
inline uint32_t getLength() const {return SBig(length);}
};
2015-06-28 05:43:53 +00:00
class IPartition
2015-06-26 19:30:03 +00:00
{
2015-06-26 20:01:15 +00:00
public:
2015-06-26 19:30:03 +00:00
enum Kind
{
PART_DATA,
2015-06-28 05:43:53 +00:00
PART_UPDATE,
PART_CHANNEL
2015-06-26 19:30:03 +00:00
};
2015-07-03 03:49:31 +00:00
struct DOLHeader
{
uint32_t textOff[7];
uint32_t dataOff[11];
uint32_t textStarts[7];
uint32_t dataStarts[11];
uint32_t textSizes[7];
uint32_t dataSizes[11];
uint32_t bssStart;
uint32_t bssSize;
uint32_t entryPoint;
};
2015-06-28 05:43:53 +00:00
class Node
2015-06-26 19:30:03 +00:00
{
2015-06-28 05:43:53 +00:00
public:
enum Kind
{
NODE_FILE,
NODE_DIRECTORY
};
private:
2015-06-30 00:07:46 +00:00
friend class IPartition;
2015-06-28 05:43:53 +00:00
const IPartition& m_parent;
Kind m_kind;
2015-06-26 19:30:03 +00:00
std::unique_ptr<IFileIO> m_hddFile;
uint64_t m_discOffset;
uint64_t m_discLength;
2015-06-30 00:07:46 +00:00
std::string m_name;
2015-06-30 06:46:19 +00:00
std::vector<Node>::iterator m_childrenBegin;
std::vector<Node>::iterator m_childrenEnd;
2015-06-28 05:43:53 +00:00
2015-06-26 20:01:15 +00:00
public:
2015-06-30 00:07:46 +00:00
Node(const IPartition& parent, const FSTNode& node, const char* name)
: m_parent(parent),
m_kind(node.isDir() ? NODE_DIRECTORY : NODE_FILE),
m_discOffset(parent.normalizeOffset(node.getOffset())),
2015-06-30 00:07:46 +00:00
m_discLength(node.getLength()),
m_name(name) {}
2015-06-28 05:43:53 +00:00
inline Kind getKind() const {return m_kind;}
2015-06-30 00:07:46 +00:00
inline const std::string& getName() const {return m_name;}
2015-06-29 05:35:25 +00:00
std::unique_ptr<IPartReadStream> beginReadStream() const
{
if (m_kind != NODE_FILE)
{
throw std::runtime_error("unable to stream a non-file");
return std::unique_ptr<IPartReadStream>();
}
return m_parent.beginReadStream(m_discOffset);
}
2015-06-30 06:46:19 +00:00
inline std::vector<Node>::iterator rawBegin() const {return m_childrenBegin;}
inline std::vector<Node>::iterator rawEnd() const {return m_childrenEnd;}
2015-06-30 00:07:46 +00:00
2015-06-30 06:46:19 +00:00
class DirectoryIterator : std::iterator<std::forward_iterator_tag, Node>
2015-06-30 00:07:46 +00:00
{
friend class Node;
2015-06-30 06:46:19 +00:00
std::vector<Node>::iterator m_it;
DirectoryIterator(const std::vector<Node>::iterator& it)
: m_it(it) {}
2015-06-30 00:07:46 +00:00
public:
inline bool operator!=(const DirectoryIterator& other) {return m_it != other.m_it;}
inline DirectoryIterator& operator++()
{
if (m_it->m_kind == NODE_DIRECTORY)
m_it = m_it->rawEnd();
else
++m_it;
return *this;
}
2015-06-30 06:46:19 +00:00
inline Node& operator*() {return *m_it;}
2015-06-30 00:07:46 +00:00
};
2015-06-30 06:46:19 +00:00
inline DirectoryIterator begin() const {return DirectoryIterator(m_childrenBegin);}
inline DirectoryIterator end() const {return DirectoryIterator(m_childrenEnd);}
2015-07-02 18:33:55 +00:00
void extractToDirectory(const SystemString& basePath, bool force=false);
2015-06-26 19:30:03 +00:00
};
2015-06-28 05:43:53 +00:00
protected:
uint64_t m_dolOff;
uint64_t m_fstOff;
uint64_t m_fstSz;
uint64_t m_apploaderSz;
2015-06-30 00:07:46 +00:00
std::vector<Node> m_nodes;
void parseFST(IPartReadStream& s);
2015-06-29 05:35:25 +00:00
2015-06-28 05:43:53 +00:00
const DiscBase& m_parent;
2015-06-26 19:30:03 +00:00
Kind m_kind;
uint64_t m_offset;
2015-06-26 20:01:15 +00:00
public:
IPartition(const DiscBase& parent, Kind kind, uint64_t offset)
2015-06-28 05:43:53 +00:00
: m_parent(parent), m_kind(kind), m_offset(offset) {}
virtual uint64_t normalizeOffset(uint64_t anOffset) const {return anOffset;}
2015-06-28 05:43:53 +00:00
inline Kind getKind() const {return m_kind;}
virtual std::unique_ptr<IPartReadStream> beginReadStream(uint64_t offset=0) const=0;
2015-06-30 00:07:46 +00:00
inline const Node& getFSTRoot() const {return m_nodes[0];}
2015-06-30 06:46:19 +00:00
inline Node& getFSTRoot() {return m_nodes[0];}
2015-07-02 18:33:55 +00:00
void extractToDirectory(const SystemString& path, bool force=false);
2015-06-26 19:30:03 +00:00
};
2015-06-28 05:43:53 +00:00
protected:
std::unique_ptr<IDiscIO> m_discIO;
Header m_header;
std::vector<std::unique_ptr<IPartition>> m_partitions;
public:
2015-07-03 22:40:34 +00:00
DiscBase(std::unique_ptr<IDiscIO>&& dio)
: m_discIO(std::move(dio)), m_header(*m_discIO.get()) {}
2015-06-28 05:43:53 +00:00
virtual bool commit()=0;
2015-06-30 00:07:46 +00:00
inline const Header& getHeader() const {return m_header;}
2015-06-28 05:43:53 +00:00
inline const IDiscIO& getDiscIO() const {return *m_discIO.get();}
inline IPartition* getDataPartition()
2015-06-30 00:07:46 +00:00
{
for (const std::unique_ptr<IPartition>& part : m_partitions)
if (part->getKind() == IPartition::PART_DATA)
return part.get();
return nullptr;
}
inline IPartition* getUpdatePartition()
2015-06-30 00:07:46 +00:00
{
for (const std::unique_ptr<IPartition>& part : m_partitions)
if (part->getKind() == IPartition::PART_UPDATE)
return part.get();
return nullptr;
}
2015-07-02 18:33:55 +00:00
inline void extractToDirectory(const SystemString& path, bool force=false)
2015-06-30 06:46:19 +00:00
{
for (std::unique_ptr<IPartition>& part : m_partitions)
part->extractToDirectory(path, force);
}
2015-06-26 19:30:03 +00:00
};
}
#endif // __NOD_DISC_BASE__