nod/include/NOD/DiscBase.hpp

378 lines
13 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>
#include <unordered_map>
2015-06-30 00:07:46 +00:00
#include <stdio.h>
#include <stdint.h>
2016-01-24 05:06:54 +00:00
#include <functional>
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
{
2016-01-21 06:30:37 +00:00
class FSTNode
2015-06-26 19:30:03 +00:00
{
2016-01-21 06:30:37 +00:00
uint32_t typeAndNameOffset;
uint32_t offset;
uint32_t length;
2015-06-26 20:01:15 +00:00
public:
2016-01-21 06:30:37 +00:00
FSTNode(bool isDir, uint32_t nameOff, uint32_t off, uint32_t len)
2015-06-28 05:43:53 +00:00
{
2016-01-21 06:30:37 +00:00
typeAndNameOffset = nameOff & 0xffffff;
typeAndNameOffset |= isDir << 24;
typeAndNameOffset = SBig(typeAndNameOffset);
offset = SBig(off);
length = SBig(len);
}
inline bool isDir() const {return ((SBig(typeAndNameOffset) >> 24) != 0);}
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);}
void incrementLength()
{
uint32_t orig = SBig(length);
++orig;
length = SBig(orig);
}
};
2015-06-28 05:43:53 +00:00
2016-01-21 06:30:37 +00:00
struct Header
{
char m_gameID[6];
char m_discNum;
char m_discVersion;
char m_audioStreaming;
char m_streamBufSz;
char m_unk[14];
uint32_t m_wiiMagic;
uint32_t m_gcnMagic;
char m_gameTitle[64];
2016-01-20 03:17:24 +00:00
2016-01-21 06:30:37 +00:00
Header(IDiscIO& dio)
{
std::unique_ptr<IDiscIO::IReadStream> s = dio.beginReadStream(0);
s->read(this, sizeof(*this));
m_wiiMagic = SBig(m_wiiMagic);
m_gcnMagic = SBig(m_gcnMagic);
}
2016-01-20 03:17:24 +00:00
2016-01-21 06:30:37 +00:00
Header(const char gameID[6], const char* gameTitle, bool wii, char discNum=0, char discVersion=0,
char audioStreaming=1, char streamBufSz=0)
{
memset(this, 0, sizeof(*this));
memcpy(m_gameID, gameID, 6);
strncpy(m_gameTitle, gameTitle, 64);
m_discNum = discNum;
m_discVersion = discVersion;
m_audioStreaming = audioStreaming;
m_streamBufSz = streamBufSz;
if (wii)
m_wiiMagic = 0x5D1C9EA3;
else
m_gcnMagic = 0xC2339F3D;
}
2015-06-28 05:43:53 +00:00
2016-01-25 02:00:02 +00:00
template <class WriteStream>
void write(WriteStream& ws) const
2015-06-30 00:07:46 +00:00
{
2016-01-21 06:30:37 +00:00
Header hs(*this);
hs.m_wiiMagic = SBig(hs.m_wiiMagic);
hs.m_gcnMagic = SBig(hs.m_gcnMagic);
ws.write(&hs, sizeof(hs));
}
};
struct ExtractionContext;
class DiscBase
{
public:
virtual ~DiscBase() {}
2015-06-30 00:07:46 +00:00
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:
virtual ~IPartition() {}
2015-11-21 01:15:33 +00:00
enum class Kind : uint32_t
2015-06-26 19:30:03 +00:00
{
2015-11-21 01:15:33 +00:00
Data,
Update,
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:
2015-11-21 01:15:33 +00:00
enum class Kind
2015-06-28 05:43:53 +00:00
{
2015-11-21 01:15:33 +00:00
File,
Directory
2015-06-28 05:43:53 +00:00
};
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;
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),
2015-11-21 01:15:33 +00:00
m_kind(node.isDir() ? Kind::Directory : Kind::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;}
inline uint64_t size() const {return m_discLength;}
2015-07-10 03:11:30 +00:00
std::unique_ptr<IPartReadStream> beginReadStream(uint64_t offset=0) const
2015-06-29 05:35:25 +00:00
{
2015-11-21 01:15:33 +00:00
if (m_kind != Kind::File)
2015-06-29 05:35:25 +00:00
{
2015-07-26 02:44:44 +00:00
LogModule.report(LogVisor::Error, "unable to stream a non-file %s", m_name.c_str());
2015-06-29 05:35:25 +00:00
return std::unique_ptr<IPartReadStream>();
}
2015-07-10 03:11:30 +00:00
return m_parent.beginReadStream(m_discOffset + offset);
2015-06-29 05:35:25 +00:00
}
std::unique_ptr<uint8_t[]> getBuf() const
{
2015-11-21 01:15:33 +00:00
if (m_kind != Kind::File)
{
2015-07-26 02:44:44 +00:00
LogModule.report(LogVisor::Error, "unable to buffer a non-file %s", m_name.c_str());
return std::unique_ptr<uint8_t[]>();
}
uint8_t* buf = new uint8_t[m_discLength];
beginReadStream()->read(buf, m_discLength);
return std::unique_ptr<uint8_t[]>(buf);
}
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 bool operator==(const DirectoryIterator& other) {return m_it == other.m_it;}
2015-06-30 00:07:46 +00:00
inline DirectoryIterator& operator++()
{
2015-11-21 01:15:33 +00:00
if (m_it->m_kind == Kind::Directory)
2015-06-30 00:07:46 +00:00
m_it = m_it->rawEnd();
else
++m_it;
return *this;
}
2015-06-30 06:46:19 +00:00
inline Node& operator*() {return *m_it;}
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);}
inline DirectoryIterator find(const std::string& name) const
{
2015-11-21 01:15:33 +00:00
if (m_kind == Kind::Directory)
{
DirectoryIterator it=begin();
for (; it != end() ; ++it)
{
if (!it->getName().compare(name))
return it;
}
return it;
}
return end();
}
2015-06-30 06:46:19 +00:00
2015-09-28 01:55:59 +00:00
bool extractToDirectory(const SystemString& basePath, const ExtractionContext& ctx) const;
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
2016-01-21 06:30:37 +00:00
std::vector<FSTNode> m_buildNodes;
std::vector<std::string> m_buildNames;
size_t m_buildNameOff = 0;
void recursiveBuildNodes(const SystemChar* dirIn, std::function<void(void)> incParents);
2015-07-10 03:11:30 +00:00
uint64_t m_dolSz;
void parseDOL(IPartReadStream& s);
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;}
2016-01-22 23:45:58 +00:00
inline uint64_t getDiscOffset() const {return m_offset;}
virtual std::unique_ptr<IPartReadStream> beginReadStream(uint64_t offset=0) const=0;
2015-07-10 03:11:30 +00:00
inline std::unique_ptr<IPartReadStream> beginDOLReadStream(uint64_t offset=0) const
{return beginReadStream(m_dolOff + offset);}
inline std::unique_ptr<IPartReadStream> beginFSTReadStream(uint64_t offset=0) const
{return beginReadStream(m_fstOff + offset);}
inline std::unique_ptr<IPartReadStream> beginApploaderReadStream(uint64_t offset=0) const
{return beginReadStream(0x2440 + offset);}
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-09-28 01:55:59 +00:00
bool extractToDirectory(const SystemString& path, const ExtractionContext& ctx);
2015-07-10 03:11:30 +00:00
inline uint64_t getDOLSize() const {return m_dolSz;}
inline std::unique_ptr<uint8_t[]> getDOLBuf() const
{
std::unique_ptr<uint8_t[]> buf(new uint8_t[m_dolSz]);
beginDOLReadStream()->read(buf.get(), m_dolSz);
return buf;
}
inline uint64_t getFSTSize() const {return m_fstSz;}
inline std::unique_ptr<uint8_t[]> getFSTBuf() const
{
std::unique_ptr<uint8_t[]> buf(new uint8_t[m_fstSz]);
beginFSTReadStream()->read(buf.get(), m_fstSz);
return buf;
}
inline uint64_t getApploaderSize() const {return m_apploaderSz;}
inline std::unique_ptr<uint8_t[]> getApploaderBuf() const
{
std::unique_ptr<uint8_t[]> buf(new uint8_t[m_apploaderSz]);
beginApploaderReadStream()->read(buf.get(), m_apploaderSz);
return buf;
}
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)
2015-08-05 21:45:25 +00:00
: m_discIO(std::move(dio)), m_header(*m_discIO) {}
2015-07-03 22:40:34 +00:00
2015-06-30 00:07:46 +00:00
inline const Header& getHeader() const {return m_header;}
2015-08-05 21:45:25 +00:00
inline const IDiscIO& getDiscIO() const {return *m_discIO;}
inline IPartition* getDataPartition()
2015-06-30 00:07:46 +00:00
{
for (const std::unique_ptr<IPartition>& part : m_partitions)
2015-11-21 01:15:33 +00:00
if (part->getKind() == IPartition::Kind::Data)
2015-06-30 00:07:46 +00:00
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)
2015-11-21 01:15:33 +00:00
if (part->getKind() == IPartition::Kind::Update)
2015-06-30 00:07:46 +00:00
return part.get();
return nullptr;
}
2015-09-28 01:55:59 +00:00
inline void extractToDirectory(const SystemString& path, const ExtractionContext& ctx)
2015-06-30 06:46:19 +00:00
{
for (std::unique_ptr<IPartition>& part : m_partitions)
2015-09-28 01:55:59 +00:00
part->extractToDirectory(path, ctx);
2015-06-30 06:46:19 +00:00
}
2016-01-21 06:30:37 +00:00
};
class DiscBuilderBase
{
public:
class PartitionBuilderBase
2016-01-21 06:30:37 +00:00
{
public:
virtual ~PartitionBuilderBase() {}
2016-01-21 06:30:37 +00:00
enum class Kind : uint32_t
{
Data,
Update,
Channel
};
protected:
std::unordered_map<SystemString, std::pair<uint64_t,uint64_t>> m_fileOffsetsSizes;
2016-01-21 06:30:37 +00:00
std::vector<FSTNode> m_buildNodes;
std::vector<std::string> m_buildNames;
size_t m_buildNameOff = 0;
2016-01-25 02:00:02 +00:00
virtual uint64_t userAllocate(uint64_t reqSz, IPartWriteStream& ws)=0;
2016-01-22 23:45:58 +00:00
virtual uint32_t packOffset(uint64_t offset) const=0;
2016-01-25 02:00:02 +00:00
void recursiveBuildNodes(IPartWriteStream& ws, bool system, const SystemChar* dirIn, uint64_t dolInode);
void recursiveBuildFST(const SystemChar* dirIn, uint64_t dolInode,
std::function<void(void)> incParents);
2016-01-21 06:30:37 +00:00
void addBuildName(const SystemString& str)
{
SystemUTF8View utf8View(str);
m_buildNames.push_back(utf8View.utf8_str());
m_buildNameOff += str.size() + 1;
}
DiscBuilderBase& m_parent;
Kind m_kind;
char m_gameID[6];
std::string m_gameTitle;
uint64_t m_dolOffset = 0;
2016-01-22 23:45:58 +00:00
uint64_t m_dolSize = 0;
2016-01-21 06:30:37 +00:00
public:
PartitionBuilderBase(DiscBuilderBase& parent, Kind kind,
const char gameID[6], const char* gameTitle)
: m_parent(parent), m_kind(kind), m_gameTitle(gameTitle)
2016-01-21 06:30:37 +00:00
{
memcpy(m_gameID, gameID, 6);
}
2016-01-25 02:00:02 +00:00
virtual std::unique_ptr<IPartWriteStream> beginWriteStream(uint64_t offset)=0;
bool buildFromDirectory(IPartWriteStream& ws,
const SystemChar* dirIn, const SystemChar* dolIn,
2016-01-21 06:30:37 +00:00
const SystemChar* apploaderIn);
2016-01-22 23:45:58 +00:00
const char* getGameID() const {return m_gameID;}
const std::string& getGameTitle() const {return m_gameTitle;}
2016-01-21 06:30:37 +00:00
};
protected:
2016-01-25 02:00:02 +00:00
const SystemChar* m_outPath;
2016-01-21 06:30:37 +00:00
std::unique_ptr<IFileIO> m_fileIO;
std::vector<std::unique_ptr<PartitionBuilderBase>> m_partitions;
2016-02-02 21:18:16 +00:00
int64_t m_discCapacity;
2016-01-21 06:30:37 +00:00
public:
std::function<void(size_t idx, const SystemString&, size_t)> m_progressCB;
size_t m_progressIdx = 0;
virtual ~DiscBuilderBase() {}
2016-02-02 21:18:16 +00:00
DiscBuilderBase(const SystemChar* outPath, int64_t discCapacity,
2016-01-21 06:30:37 +00:00
std::function<void(size_t idx, const SystemString&, size_t)> progressCB)
2016-02-02 21:18:16 +00:00
: m_outPath(outPath), m_fileIO(std::move(NewFileIO(outPath, discCapacity))),
m_discCapacity(discCapacity), m_progressCB(progressCB) {}
2016-01-21 06:30:37 +00:00
IFileIO& getFileIO() {return *m_fileIO;}
2015-06-26 19:30:03 +00:00
};
using Partition = DiscBase::IPartition;
using Node = Partition::Node;
2015-06-26 19:30:03 +00:00
}
#endif // __NOD_DISC_BASE__