Merge branch 'master' of ssh://git.axiodl.com:6431/AxioDL/nod

This commit is contained in:
Jack Andersen 2019-09-30 21:30:57 -10:00
commit 11a0351d1c
9 changed files with 104 additions and 99 deletions

View File

@ -96,7 +96,7 @@ int main(int argc, char* argv[])
return 1;
}
if (nod::DiscBuilderGCN::CalculateTotalSizeRequired(argv[2]) == UINT64_MAX)
if (!nod::DiscBuilderGCN::CalculateTotalSizeRequired(argv[2]))
return 1;
nod::EBuildResult ret;
@ -123,7 +123,7 @@ int main(int argc, char* argv[])
}
bool dual = false;
if (nod::DiscBuilderWii::CalculateTotalSizeRequired(argv[2], dual) == UINT64_MAX)
if (!nod::DiscBuilderWii::CalculateTotalSizeRequired(argv[2], dual))
return 1;
nod::EBuildResult ret;
@ -164,7 +164,7 @@ int main(int argc, char* argv[])
return 1;
}
if (nod::DiscMergerGCN::CalculateTotalSizeRequired(static_cast<nod::DiscGCN&>(*disc), argv[2]) == UINT64_MAX)
if (!nod::DiscMergerGCN::CalculateTotalSizeRequired(static_cast<nod::DiscGCN&>(*disc), argv[2]))
return 1;
nod::EBuildResult ret;
@ -206,7 +206,7 @@ int main(int argc, char* argv[])
}
bool dual = false;
if (nod::DiscMergerWii::CalculateTotalSizeRequired(static_cast<nod::DiscWii&>(*disc), argv[2], dual) == UINT64_MAX)
if (!nod::DiscMergerWii::CalculateTotalSizeRequired(static_cast<nod::DiscWii&>(*disc), argv[2], dual))
return 1;
nod::EBuildResult ret;

View File

@ -5,6 +5,7 @@
#include <cstring>
#include <functional>
#include <memory>
#include <optional>
#include <string>
#include <unordered_map>
#include <vector>
@ -35,10 +36,10 @@ public:
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); }
bool isDir() const { return ((SBig(typeAndNameOffset) >> 24) != 0); }
uint32_t getNameOffset() const { return SBig(typeAndNameOffset) & 0xffffff; }
uint32_t getOffset() const { return SBig(offset); }
uint32_t getLength() const { return SBig(length); }
void incrementLength() {
uint32_t orig = SBig(length);
++orig;
@ -187,13 +188,13 @@ private:
public:
Node(const IPartition& parent, const FSTNode& node, std::string_view name);
inline Kind getKind() const { return m_kind; }
inline std::string_view getName() const { return m_name; }
inline uint64_t size() const { return m_discLength; }
Kind getKind() const { return m_kind; }
std::string_view getName() const { return m_name; }
uint64_t size() const { return m_discLength; }
std::unique_ptr<IPartReadStream> beginReadStream(uint64_t offset = 0) const;
std::unique_ptr<uint8_t[]> getBuf() const;
inline std::vector<Node>::iterator rawBegin() const { return m_childrenBegin; }
inline std::vector<Node>::iterator rawEnd() const { return m_childrenEnd; }
std::vector<Node>::iterator rawBegin() const { return m_childrenBegin; }
std::vector<Node>::iterator rawEnd() const { return m_childrenEnd; }
class DirectoryIterator {
friend class Node;
@ -207,21 +208,23 @@ public:
using pointer = Node*;
using reference = Node&;
inline bool operator!=(const DirectoryIterator& other) { return m_it != other.m_it; }
inline bool operator==(const DirectoryIterator& other) { return m_it == other.m_it; }
inline DirectoryIterator& operator++() {
bool operator==(const DirectoryIterator& other) const { return m_it == other.m_it; }
bool operator!=(const DirectoryIterator& other) const { return !operator==(other); }
DirectoryIterator& operator++() {
if (m_it->m_kind == Kind::Directory)
m_it = m_it->rawEnd();
else
++m_it;
return *this;
}
inline Node& operator*() { return *m_it; }
inline Node* operator->() { return &*m_it; }
Node& operator*() { return *m_it; }
const Node& operator*() const { return *m_it; }
Node* operator->() { return &*m_it; }
const Node* operator->() const { return &*m_it; }
};
inline DirectoryIterator begin() const { return DirectoryIterator(m_childrenBegin); }
inline DirectoryIterator end() const { return DirectoryIterator(m_childrenEnd); }
inline DirectoryIterator find(std::string_view name) const {
DirectoryIterator begin() const { return DirectoryIterator(m_childrenBegin); }
DirectoryIterator end() const { return DirectoryIterator(m_childrenEnd); }
DirectoryIterator find(std::string_view name) const {
if (m_kind == Kind::Directory) {
DirectoryIterator it = begin();
for (; it != end(); ++it) {
@ -289,47 +292,47 @@ public:
IPartition(const DiscBase& parent, PartitionKind kind, bool isWii, uint64_t offset)
: m_parent(parent), m_kind(kind), m_offset(offset), m_isWii(isWii) {}
virtual uint64_t normalizeOffset(uint64_t anOffset) const { return anOffset; }
inline PartitionKind getKind() const { return m_kind; }
inline bool isWii() const { return m_isWii; }
inline uint64_t getDiscOffset() const { return m_offset; }
PartitionKind getKind() const { return m_kind; }
bool isWii() const { return m_isWii; }
uint64_t getDiscOffset() const { return m_offset; }
virtual std::unique_ptr<IPartReadStream> beginReadStream(uint64_t offset = 0) const = 0;
inline std::unique_ptr<IPartReadStream> beginDOLReadStream(uint64_t offset = 0) const {
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 {
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 {
std::unique_ptr<IPartReadStream> beginApploaderReadStream(uint64_t offset = 0) const {
return beginReadStream(0x2440 + offset);
}
inline const Node& getFSTRoot() const { return m_nodes[0]; }
inline Node& getFSTRoot() { return m_nodes[0]; }
const Node& getFSTRoot() const { return m_nodes[0]; }
Node& getFSTRoot() { return m_nodes[0]; }
bool extractToDirectory(SystemStringView path, const ExtractionContext& ctx);
inline uint64_t getDOLSize() const { return m_dolSz; }
inline std::unique_ptr<uint8_t[]> getDOLBuf() const {
uint64_t getDOLSize() const { return m_dolSz; }
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 {
uint64_t getFSTSize() const { return m_fstSz; }
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 {
uint64_t getApploaderSize() const { return m_apploaderSz; }
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;
}
inline size_t getNodeCount() const { return m_nodes.size(); }
inline const Header& getHeader() const { return m_header; }
inline const BI2Header& getBI2() const { return m_bi2Header; }
size_t getNodeCount() const { return m_nodes.size(); }
const Header& getHeader() const { return m_header; }
const BI2Header& getBI2() const { return m_bi2Header; }
virtual bool extractCryptoFiles(SystemStringView path, const ExtractionContext& ctx) const { return true; }
bool extractSysFiles(SystemStringView path, const ExtractionContext& ctx) const;
};
@ -346,29 +349,30 @@ protected:
public:
DiscBase(std::unique_ptr<IDiscIO>&& dio, bool& err) : m_discIO(std::move(dio)), m_header(*m_discIO, err) {}
inline const Header& getHeader() const { return m_header; }
inline const IDiscIO& getDiscIO() const { return *m_discIO; }
inline size_t getPartitonNodeCount(size_t partition = 0) const {
if (partition > m_partitions.size())
const Header& getHeader() const { return m_header; }
const IDiscIO& getDiscIO() const { return *m_discIO; }
size_t getPartitionNodeCount(size_t partition = 0) const {
if (partition >= m_partitions.size()) {
return -1;
}
return m_partitions[partition]->getNodeCount();
}
inline IPartition* getDataPartition() {
IPartition* getDataPartition() {
for (const std::unique_ptr<IPartition>& part : m_partitions)
if (part->getKind() == PartitionKind::Data)
return part.get();
return nullptr;
}
inline IPartition* getUpdatePartition() {
IPartition* getUpdatePartition() {
for (const std::unique_ptr<IPartition>& part : m_partitions)
if (part->getKind() == PartitionKind::Update)
return part.get();
return nullptr;
}
inline void extractToDirectory(SystemStringView path, const ExtractionContext& ctx) {
void extractToDirectory(SystemStringView path, const ExtractionContext& ctx) {
for (std::unique_ptr<IPartition>& part : m_partitions)
part->extractToDirectory(path, ctx);
}
@ -422,9 +426,9 @@ public:
: m_parent(parent), m_kind(kind), m_isWii(isWii) {}
virtual std::unique_ptr<IPartWriteStream> beginWriteStream(uint64_t offset) = 0;
bool buildFromDirectory(IPartWriteStream& ws, SystemStringView dirIn);
static uint64_t CalculateTotalSizeBuild(SystemStringView dirIn, PartitionKind kind, bool isWii);
static std::optional<uint64_t> CalculateTotalSizeBuild(SystemStringView dirIn, PartitionKind kind, bool isWii);
bool mergeFromDirectory(IPartWriteStream& ws, const IPartition* partIn, SystemStringView dirIn);
static uint64_t CalculateTotalSizeMerge(const IPartition* partIn, SystemStringView dirIn);
static std::optional<uint64_t> CalculateTotalSizeMerge(const IPartition* partIn, SystemStringView dirIn);
};
protected:
@ -455,7 +459,7 @@ public:
: m_outPath(outPath)
, m_fileIO(NewFileIO(outPath, discCapacity))
, m_discCapacity(discCapacity)
, m_progressCB(progressCB) {}
, m_progressCB(std::move(progressCB)) {}
DiscBuilderBase(DiscBuilderBase&&) = default;
DiscBuilderBase& operator=(DiscBuilderBase&&) = default;

View File

@ -20,7 +20,7 @@ class DiscBuilderGCN : public DiscBuilderBase {
public:
DiscBuilderGCN(SystemStringView outPath, FProgress progressCB);
EBuildResult buildFromDirectory(SystemStringView dirIn);
static uint64_t CalculateTotalSizeRequired(SystemStringView dirIn);
static std::optional<uint64_t> CalculateTotalSizeRequired(SystemStringView dirIn);
};
class DiscMergerGCN {
@ -30,7 +30,7 @@ class DiscMergerGCN {
public:
DiscMergerGCN(SystemStringView outPath, DiscGCN& sourceDisc, FProgress progressCB);
EBuildResult mergeFromDirectory(SystemStringView dirIn);
static uint64_t CalculateTotalSizeRequired(DiscGCN& sourceDisc, SystemStringView dirIn);
static std::optional<uint64_t> CalculateTotalSizeRequired(DiscGCN& sourceDisc, SystemStringView dirIn);
};
} // namespace nod

View File

@ -16,7 +16,7 @@ class DiscBuilderWii : public DiscBuilderBase {
public:
DiscBuilderWii(SystemStringView outPath, bool dualLayer, FProgress progressCB);
EBuildResult buildFromDirectory(SystemStringView dirIn);
static uint64_t CalculateTotalSizeRequired(SystemStringView dirIn, bool& dualLayer);
static std::optional<uint64_t> CalculateTotalSizeRequired(SystemStringView dirIn, bool& dualLayer);
};
class DiscMergerWii {
@ -26,7 +26,7 @@ class DiscMergerWii {
public:
DiscMergerWii(SystemStringView outPath, DiscWii& sourceDisc, bool dualLayer, FProgress progressCB);
EBuildResult mergeFromDirectory(SystemStringView dirIn);
static uint64_t CalculateTotalSizeRequired(DiscWii& sourceDisc, SystemStringView dirIn, bool& dualLayer);
static std::optional<uint64_t> CalculateTotalSizeRequired(DiscWii& sourceDisc, SystemStringView dirIn, bool& dualLayer);
};
} // namespace nod

View File

@ -49,11 +49,12 @@ public:
AthenaPartReadStream(std::unique_ptr<IPartReadStream>&& rs) : m_rs(std::move(rs)) {}
void seek(atInt64 off, athena::SeekOrigin origin) override {
if (origin == athena::Begin)
if (origin == athena::SeekOrigin::Begin) {
m_rs->seek(off, SEEK_SET);
else if (origin == athena::Current)
} else if (origin == athena::SeekOrigin::Current) {
m_rs->seek(off, SEEK_CUR);
}
}
atUint64 position() const override { return m_rs->position(); }
atUint64 length() const override { return 0; }
atUint64 readUBytesToBuf(void* buf, atUint64 sz) override {

View File

@ -94,8 +94,8 @@ public:
m_utf8.assign(len, '\0');
WideCharToMultiByte(CP_UTF8, 0, str.data(), str.size(), &m_utf8[0], len, nullptr, nullptr);
}
inline std::string_view utf8_str() const { return m_utf8; }
inline const char* c_str() const { return m_utf8.c_str(); }
std::string_view utf8_str() const { return m_utf8; }
const char* c_str() const { return m_utf8.c_str(); }
};
class SystemStringConv {
std::wstring m_sys;
@ -106,8 +106,8 @@ public:
m_sys.assign(len, L'\0');
MultiByteToWideChar(CP_UTF8, 0, str.data(), str.size(), &m_sys[0], len);
}
inline SystemStringView sys_str() const { return m_sys; }
inline const SystemChar* c_str() const { return m_sys.c_str(); }
SystemStringView sys_str() const { return m_sys; }
const SystemChar* c_str() const { return m_sys.c_str(); }
};
#ifndef _SYS_STR
#define _SYS_STR(val) L##val
@ -124,16 +124,16 @@ class SystemUTF8Conv {
public:
explicit SystemUTF8Conv(SystemStringView str) : m_utf8(str) {}
inline std::string_view utf8_str() const { return m_utf8; }
inline const char* c_str() const { return m_utf8.data(); }
std::string_view utf8_str() const { return m_utf8; }
const char* c_str() const { return m_utf8.data(); }
};
class SystemStringConv {
SystemStringView m_sys;
public:
explicit SystemStringConv(std::string_view str) : m_sys(str) {}
inline SystemStringView sys_str() const { return m_sys; }
inline const SystemChar* c_str() const { return m_sys.data(); }
SystemStringView sys_str() const { return m_sys; }
const SystemChar* c_str() const { return m_sys.data(); }
};
#ifndef _SYS_STR
#define _SYS_STR(val) val

View File

@ -769,8 +769,8 @@ bool DiscBuilderBase::PartitionBuilderBase::buildFromDirectory(IPartWriteStream&
return true;
}
uint64_t DiscBuilderBase::PartitionBuilderBase::CalculateTotalSizeBuild(SystemStringView dirIn, PartitionKind kind,
bool isWii) {
std::optional<uint64_t> DiscBuilderBase::PartitionBuilderBase::CalculateTotalSizeBuild(SystemStringView dirIn,
PartitionKind kind, bool isWii) {
SystemString dirStr(dirIn);
SystemString basePath = isWii ? dirStr + _SYS_STR("/") + getKindString(kind) : dirStr;
SystemString dolIn = basePath + _SYS_STR("/sys/main.dol");
@ -779,11 +779,11 @@ uint64_t DiscBuilderBase::PartitionBuilderBase::CalculateTotalSizeBuild(SystemSt
Sstat dolStat;
if (Stat(dolIn.c_str(), &dolStat)) {
LogModule.report(logvisor::Error, fmt(_SYS_STR("unable to stat {}")), dolIn);
return UINT64_MAX;
return std::nullopt;
}
uint64_t totalSz = ROUND_UP_32(dolStat.st_size);
if (!RecursiveCalculateTotalSize(totalSz, nullptr, filesIn.c_str()))
return UINT64_MAX;
return std::nullopt;
return totalSz;
}
@ -843,7 +843,7 @@ bool DiscBuilderBase::PartitionBuilderBase::mergeFromDirectory(IPartWriteStream&
return true;
}
uint64_t DiscBuilderBase::PartitionBuilderBase::CalculateTotalSizeMerge(const IPartition* partIn,
std::optional<uint64_t> DiscBuilderBase::PartitionBuilderBase::CalculateTotalSizeMerge(const IPartition* partIn,
SystemStringView dirIn) {
SystemString dirStr(dirIn);
SystemString basePath = partIn->isWii() ? dirStr + _SYS_STR("/") + getKindString(partIn->getKind()) : dirStr;
@ -851,7 +851,7 @@ uint64_t DiscBuilderBase::PartitionBuilderBase::CalculateTotalSizeMerge(const IP
uint64_t totalSz = ROUND_UP_32(partIn->getDOLSize());
if (!RecursiveCalculateTotalSize(totalSz, &partIn->getFSTRoot(), filesIn.c_str()))
return UINT64_MAX;
return std::nullopt;
return totalSz;
}

View File

@ -372,14 +372,14 @@ EBuildResult DiscBuilderGCN::buildFromDirectory(SystemStringView dirIn) {
return pb.buildFromDirectory(dirIn) ? EBuildResult::Success : EBuildResult::Failed;
}
uint64_t DiscBuilderGCN::CalculateTotalSizeRequired(SystemStringView dirIn) {
uint64_t sz = DiscBuilderBase::PartitionBuilderBase::CalculateTotalSizeBuild(dirIn, PartitionKind::Data, false);
if (sz == UINT64_MAX)
return UINT64_MAX;
sz += 0x30000;
std::optional<uint64_t> DiscBuilderGCN::CalculateTotalSizeRequired(SystemStringView dirIn) {
std::optional<uint64_t> sz = DiscBuilderBase::PartitionBuilderBase::CalculateTotalSizeBuild(dirIn, PartitionKind::Data, false);
if (!sz)
return sz;
*sz += 0x30000;
if (sz > 0x57058000) {
LogModule.report(logvisor::Error, fmt(_SYS_STR("disc capacity exceeded [{} / {}]")), sz, 0x57058000);
return UINT64_MAX;
LogModule.report(logvisor::Error, fmt(_SYS_STR("disc capacity exceeded [{} / {}]")), *sz, 0x57058000);
return std::nullopt;
}
return sz;
}
@ -416,14 +416,14 @@ EBuildResult DiscMergerGCN::mergeFromDirectory(SystemStringView dirIn) {
: EBuildResult::Failed;
}
uint64_t DiscMergerGCN::CalculateTotalSizeRequired(DiscGCN& sourceDisc, SystemStringView dirIn) {
uint64_t sz = DiscBuilderBase::PartitionBuilderBase::CalculateTotalSizeMerge(sourceDisc.getDataPartition(), dirIn);
if (sz == UINT64_MAX)
return UINT64_MAX;
sz += 0x30000;
std::optional<uint64_t> DiscMergerGCN::CalculateTotalSizeRequired(DiscGCN& sourceDisc, SystemStringView dirIn) {
std::optional<uint64_t> sz = DiscBuilderBase::PartitionBuilderBase::CalculateTotalSizeMerge(sourceDisc.getDataPartition(), dirIn);
if (!sz)
return std::nullopt;
*sz += 0x30000;
if (sz > 0x57058000) {
LogModule.report(logvisor::Error, fmt(_SYS_STR("disc capacity exceeded [{} / {}]")), sz, 0x57058000);
return -1;
LogModule.report(logvisor::Error, fmt(_SYS_STR("disc capacity exceeded [{} / {}]")), *sz, 0x57058000);
return std::nullopt;
}
return sz;
}

View File

@ -1229,19 +1229,19 @@ EBuildResult DiscBuilderWii::buildFromDirectory(SystemStringView dirIn) {
return EBuildResult::Success;
}
uint64_t DiscBuilderWii::CalculateTotalSizeRequired(SystemStringView dirIn, bool& dualLayer) {
uint64_t sz = DiscBuilderBase::PartitionBuilderBase::CalculateTotalSizeBuild(dirIn, PartitionKind::Data, true);
if (sz == UINT64_MAX)
return UINT64_MAX;
auto szDiv = std::lldiv(sz, 0x1F0000);
std::optional<uint64_t> DiscBuilderWii::CalculateTotalSizeRequired(SystemStringView dirIn, bool& dualLayer) {
std::optional<uint64_t> sz = DiscBuilderBase::PartitionBuilderBase::CalculateTotalSizeBuild(dirIn, PartitionKind::Data, true);
if (!sz)
return sz;
auto szDiv = std::lldiv(*sz, 0x1F0000);
if (szDiv.rem)
++szDiv.quot;
sz = szDiv.quot * 0x200000;
sz += 0x200000;
*sz += 0x200000;
dualLayer = (sz > 0x118240000);
if (sz > 0x1FB4E0000) {
LogModule.report(logvisor::Error, fmt(_SYS_STR("disc capacity exceeded [{} / {}]")), sz, 0x1FB4E0000);
return UINT64_MAX;
LogModule.report(logvisor::Error, fmt(_SYS_STR("disc capacity exceeded [{} / {}]")), *sz, 0x1FB4E0000);
return std::nullopt;
}
return sz;
}
@ -1336,19 +1336,19 @@ EBuildResult DiscMergerWii::mergeFromDirectory(SystemStringView dirIn) {
return EBuildResult::Success;
}
uint64_t DiscMergerWii::CalculateTotalSizeRequired(DiscWii& sourceDisc, SystemStringView dirIn, bool& dualLayer) {
uint64_t sz = DiscBuilderBase::PartitionBuilderBase::CalculateTotalSizeMerge(sourceDisc.getDataPartition(), dirIn);
if (sz == UINT64_MAX)
return UINT64_MAX;
auto szDiv = std::lldiv(sz, 0x1F0000);
std::optional<uint64_t> DiscMergerWii::CalculateTotalSizeRequired(DiscWii& sourceDisc, SystemStringView dirIn, bool& dualLayer) {
std::optional<uint64_t> sz = DiscBuilderBase::PartitionBuilderBase::CalculateTotalSizeMerge(sourceDisc.getDataPartition(), dirIn);
if (!sz)
return std::nullopt;
auto szDiv = std::lldiv(*sz, 0x1F0000);
if (szDiv.rem)
++szDiv.quot;
sz = szDiv.quot * 0x200000;
sz += 0x200000;
*sz += 0x200000;
dualLayer = (sz > 0x118240000);
if (sz > 0x1FB4E0000) {
LogModule.report(logvisor::Error, fmt(_SYS_STR("disc capacity exceeded [{} / {}]")), sz, 0x1FB4E0000);
return UINT64_MAX;
LogModule.report(logvisor::Error, fmt(_SYS_STR("disc capacity exceeded [{} / {}]")), *sz, 0x1FB4E0000);
return std::nullopt;
}
return sz;
}