[nostalgia] Integrate Ox Preloader

This commit is contained in:
2022-11-30 01:47:33 -06:00
parent cbb496c59f
commit 090fe28b44
41 changed files with 404 additions and 159 deletions
+85 -22
View File
@@ -4,11 +4,11 @@
#include <ox/claw/read.hpp>
#include <ox/fs/fs.hpp>
#include <ox/mc/write.hpp>
#include <ox/model/descwrite.hpp>
#include <ox/model/modelvalue.hpp>
#include <nostalgia/core/gfx.hpp>
#include <nostalgia/core/media.hpp>
#include <nostalgia/core/typeconv.hpp>
#include <nostalgia/core/typestore.hpp>
@@ -16,6 +16,8 @@
namespace nostalgia {
using Preloader = ox::ModelHandlerInterface<GbaPreloader>;
static ox::Error pathToInode(ox::FileSystem *dest, ox::ModelObject *obj) noexcept {
auto &o = *obj;
auto type = static_cast<ox::FileAddressType>(o["type"].get<int8_t>());
@@ -30,7 +32,7 @@ static ox::Error pathToInode(ox::FileSystem *dest, ox::ModelObject *obj) noexcep
break;
case ox::FileAddressType::Inode:
case ox::FileAddressType::None:
return OxError(0);
return {};
}
oxRequire(s, dest->stat(path.c_str()));
oxReturnError(o["type"].set(static_cast<int8_t>(ox::FileAddressType::Inode)));
@@ -41,7 +43,6 @@ static ox::Error pathToInode(ox::FileSystem *dest, ox::ModelObject *obj) noexcep
* Convert path references in Claw data to inodes to save space
* @param buff buffer holding file
* @return error
* stub for now
*/
static ox::Error transformObj(ox::FileSystem *dest, ox::ModelObject *obj) noexcept {
for (auto &f : *obj) {
@@ -56,10 +57,10 @@ static ox::Error transformObj(ox::FileSystem *dest, ox::ModelObject *obj) noexce
oxReturnError(transformObj(dest, &o));
}
}
return OxError(0);
return {};
}
static ox::Error doTransformations(core::TypeStore *ts, ox::FileSystem *dest, const ox::String &filePath) noexcept {
static ox::Error doTransformations(core::TypeStore *ts, ox::FileSystem *dest, ox::CRString filePath) noexcept {
if (filePath.endsWith(".ng") || filePath.endsWith(".npal")) {
// load file
oxRequire(s, dest->stat(filePath.c_str()));
@@ -70,16 +71,16 @@ static ox::Error doTransformations(core::TypeStore *ts, ox::FileSystem *dest, co
oxRequireM(obj, ox::readClaw(ts, buff));
// do transformations
oxReturnError(transformObj(dest, &obj));
oxReturnError(ox::writeMC(&obj).moveTo(&buff));
oxReturnError(ox::writeClaw(&obj).moveTo(&buff));
// write file to dest
oxReturnError(dest->write(s.inode, buff.data(), buff.size()));
}
return OxError(0);
return {};
}
// claw file transformations are broken out because path to inode
// transformations need to be done after the copy to the new FS is complete
static ox::Error transformClaw(core::TypeStore *ts, ox::FileSystem *dest, const ox::String &path) noexcept {
static ox::Error transformClaw(core::TypeStore *ts, ox::FileSystem *dest, ox::CRString path) noexcept {
// copy
oxTracef("pack::transformClaw", "path: {}", path);
oxRequire(fileList, dest->ls(path));
@@ -93,10 +94,10 @@ static ox::Error transformClaw(core::TypeStore *ts, ox::FileSystem *dest, const
oxReturnError(doTransformations(ts, dest, filePath));
}
}
return OxError(0);
return {};
}
static ox::Error verifyFile(ox::FileSystem *fs, const ox::String &path, const ox::Buffer &expected) noexcept {
static ox::Error verifyFile(ox::FileSystem *fs, ox::CRString path, const ox::Buffer &expected) noexcept {
ox::Buffer buff(expected.size());
oxReturnError(fs->read(path.c_str(), buff.data(), buff.size()));
return OxError(buff == expected ? 0 : 1);
@@ -105,19 +106,19 @@ static ox::Error verifyFile(ox::FileSystem *fs, const ox::String &path, const ox
struct VerificationPair {
ox::String path;
ox::Buffer buff;
VerificationPair(const ox::String &pPath, ox::Buffer pBuff) noexcept:
path(pPath),
buff(std::move(pBuff)) {
VerificationPair(ox::String &&pPath, ox::Buffer &&pBuff) noexcept:
path(std::forward<ox::String>(pPath)),
buff(std::forward<ox::Buffer>(pBuff)) {
}
};
static ox::Error copy(ox::FileSystem *src, ox::FileSystem *dest, const ox::String &path) noexcept {
static ox::Error copy(ox::FileSystem *src, ox::FileSystem *dest, ox::CRString path) noexcept {
oxOutf("copying directory: {}\n", path);
ox::Vector<VerificationPair> verificationPairs;
// copy
oxRequire(fileList, src->ls(path));
for (const auto &name : fileList) {
const auto currentFile = path + name;
auto currentFile = ox::sfmt("{}{}", path, name);
if (currentFile == "/.nostalgia") {
continue;
}
@@ -133,22 +134,84 @@ static ox::Error copy(ox::FileSystem *src, ox::FileSystem *dest, const ox::Strin
oxOutf("writing {}\n", currentFile);
oxReturnError(dest->write(currentFile.c_str(), buff.data(), buff.size()));
oxReturnError(verifyFile(dest, currentFile, buff));
verificationPairs.emplace_back(currentFile, std::move(buff));
verificationPairs.emplace_back(std::move(currentFile), std::move(buff));
}
}
// verify all at once in addition to right after the files are written
for (const auto &v : verificationPairs) {
oxReturnError(verifyFile(dest, v.path, v.buff));
}
return OxError(0);
return {};
}
ox::Error pack(ox::FileSystem *src, ox::FileSystem *dest) noexcept {
// transformations need to be done after the copy to the new FS is complete
static ox::Error preloadObj(core::TypeStore *ts, ox::FileSystem *romFs, Preloader *pl, ox::CRString path) noexcept {
// load file
oxRequireM(buff, romFs->read(path.c_str()));
oxRequireM(obj, ox::readClaw(ts, buff));
if (obj.type()->preloadable) {
// preload
oxReturnError(model(pl, &obj));
const core::PreloadPtr p{.preloadAddr = 0};
oxReturnError(ox::writeMC(&p).moveTo(&buff));
} else {
// strip the Claw header (it is not needed after preloading) and write back out to dest fs
oxReturnError(ox::writeMC(&obj).moveTo(&buff));
}
oxReturnError(romFs->write(path.c_str(), buff.data(), buff.size()));
return {};
}
// claw file transformations are broken out because path to inode
// transformations need to be done after the copy to the new FS is complete
static ox::Error preload(core::TypeStore *ts, ox::FileSystem *romFs, Preloader *pl, ox::CRString path) noexcept {
// copy
oxTracef("pack::preload", "path: {}", path);
oxRequire(fileList, romFs->ls(path));
for (const auto &name : fileList) {
const auto filePath = path + name;
oxRequire(stat, romFs->stat(filePath.c_str()));
if (stat.fileType == ox::FileType::Directory) {
const auto dir = path + name + '/';
oxReturnError(preload(ts, romFs, pl, dir));
} else {
oxReturnError(preloadObj(ts, romFs, pl, filePath));
}
}
return {};
}
static ox::Error padbin(ox::BufferWriter *w, unsigned factor) noexcept {
return w->write(nullptr, factor - w->data()->size() % factor);
}
ox::Error appendBinary(ox::Buffer *binBuff, ox::Buffer *fsBuff, GbaPreloader *pl) noexcept {
constexpr ox::StringView mediaHdr = "NOSTALGIA_MEDIA_HEADER__________";
constexpr ox::StringView preloadHdr = "NOSTALGIA_PRELOAD_HEADER________";
constexpr auto hdrSize = 32;
static_assert(mediaHdr.bytes() == hdrSize);
static_assert(preloadHdr.bytes() == hdrSize);
ox::BufferWriter w(binBuff);
oxReturnError(padbin(&w, hdrSize));
oxReturnError(w.write(mediaHdr.data(), mediaHdr.bytes()));
oxReturnError(w.write(fsBuff->data(), fsBuff->size()));
oxReturnError(padbin(&w, hdrSize));
oxReturnError(w.write(preloadHdr.data(), preloadHdr.bytes()));
const auto &plBuff = pl->buff();
oxReturnError(pl->offsetPtrs(binBuff->size()));
oxReturnError(w.write(plBuff.data(), plBuff.size()));
return {};
}
ox::Error pack(core::TypeStore *ts, ox::FileSystem *src, ox::FileSystem *dest) noexcept {
oxReturnError(copy(src, dest, "/"));
core::TypeStore ts(src);
oxReturnError(ox::buildTypeDef<core::CompactTileSheet>(&ts));
oxReturnError(transformClaw(&ts, dest, "/"));
return OxError(0);
oxReturnError(ox::buildTypeDef<core::CompactTileSheet>(ts));
oxReturnError(transformClaw(ts, dest, "/"));
return {};
}
ox::Error preload(core::TypeStore *ts, ox::FileSystem *src, GbaPreloader *pl) noexcept {
return preload(ts, src, pl->interface(), "/");
}
}