[keel] Split out Nostalgia Foundation and Pack lib into Keel

This commit is contained in:
2023-03-24 21:20:55 -05:00
parent 4a95a79926
commit 7beb3cc6fc
50 changed files with 185 additions and 206 deletions
+247
View File
@@ -0,0 +1,247 @@
/*
* Copyright 2016 - 2023 Gary Talent (gary@drinkingtea.net). All rights reserved.
*/
#ifndef OX_BARE_METAL
#include <ox/claw/read.hpp>
#include <ox/fs/fs.hpp>
#include <ox/model/descwrite.hpp>
#include <ox/model/modelvalue.hpp>
#include <keel/media.hpp>
#include "pack.hpp"
namespace keel {
static ox::Error pathToInode(keel::Context *ctx, ox::FileSystem *dest, ox::ModelObject *obj) noexcept {
auto &o = *obj;
auto type = static_cast<ox::FileAddressType>(o["type"].get<int8_t>());
auto &data = o["data"].get<ox::ModelUnion>();
ox::String path;
switch (type) {
case ox::FileAddressType::Path:
path = data["path"].get<ox::String>();
break;
case ox::FileAddressType::ConstPath:
path = data["constPath"].get<ox::String>();
break;
case ox::FileAddressType::Inode:
case ox::FileAddressType::None:
return {};
}
if (beginsWith(path, "uuid://")) {
const auto uuid = ox::StringView(path).substr(7);
path = ctx->uuidToPath[uuid];
}
oxRequire(s, dest->stat(path));
oxReturnError(o["type"].set(static_cast<int8_t>(ox::FileAddressType::Inode)));
return data.set(2, s.inode);
}
static ox::Error transformFileAddressesObj(keel::Context *ctx, ox::FileSystem *dest, ox::ModelObject *obj) noexcept;
static ox::Error transformFileAddressesVec(keel::Context *ctx, ox::FileSystem *dest, ox::ModelValueVector *v) noexcept;
static ox::Error transformFileAddresses(keel::Context *ctx, ox::FileSystem *dest, ox::ModelValue *v) noexcept {
if (v->type() == ox::ModelValue::Type::Object) {
auto &obj = v->get<ox::ModelObject>();
return transformFileAddressesObj(ctx, dest, &obj);
} else if (v->type() == ox::ModelValue::Type::Vector) {
auto &vec = v->get<ox::ModelValueVector>();
return transformFileAddressesVec(ctx, dest, &vec);
}
return {};
}
static ox::Error transformFileAddressesVec(keel::Context *ctx, ox::FileSystem *dest, ox::ModelValueVector *v) noexcept {
for (auto &f : *v) {
oxReturnError(transformFileAddresses(ctx, dest, &f));
}
return {};
}
/**
* Convert path references in Claw data to inodes to save space
* @return error
*/
static ox::Error transformFileAddressesObj(keel::Context *ctx, ox::FileSystem *dest, ox::ModelObject *obj) noexcept {
if (obj->typeName() == "net.drinkingtea.ox.FileAddress" && obj->typeVersion() == 1) {
return pathToInode(ctx, dest, obj);
}
for (auto &f : *obj) {
auto &v = f->value;
oxReturnError(transformFileAddresses(ctx, dest, &v));
}
return {};
}
static ox::Error doTransformations(keel::Context *ctx, ox::TypeStore *ts, ox::FileSystem *dest, ox::CRStringView filePath) noexcept {
// load file
oxRequire(s, dest->stat(filePath));
// do transformations
oxRequireM(buff, dest->read(s.inode));
for (auto tr : ctx->packTransforms) {
oxReturnError(tr(ctx, &buff));
}
// transform FileAddresses
oxRequireM(obj, keel::readAsset(ts, buff));
oxReturnError(transformFileAddressesObj(ctx, dest, &obj));
oxReturnError(ox::writeClaw(&obj).moveTo(&buff));
// write file to dest
oxReturnError(dest->write(s.inode, buff.data(), buff.size()));
return {};
}
// claw file transformations are broken out from copy because path to inode
// transformations need to be done after the copy to the new FS is complete
static ox::Error transformClaw(keel::Context *ctx, ox::TypeStore *ts, ox::FileSystem *dest, ox::CRStringView path) noexcept {
// copy
oxTracef("pack::transformClaw", "path: {}", path);
oxRequire(fileList, dest->ls(path));
for (const auto &name : fileList) {
const auto filePath = ox::sfmt("{}{}", path, name);
oxRequire(stat, dest->stat(filePath));
if (stat.fileType == ox::FileType::Directory) {
const auto dir = ox::sfmt("{}{}/", path, name);
oxReturnError(transformClaw(ctx, ts, dest, dir));
} else {
oxReturnError(doTransformations(ctx, ts, dest, filePath));
}
}
return {};
}
static ox::Error verifyFile(ox::FileSystem *fs, ox::CRStringView path, const ox::Buffer &expected) noexcept {
ox::Buffer buff(expected.size());
oxReturnError(fs->read(path, buff.data(), buff.size()));
return OxError(buff == expected ? 0 : 1);
}
struct VerificationPair {
ox::String path;
ox::Buffer buff;
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, ox::CRStringView path, ox::Vector<VerificationPair> *verificationPairs) noexcept {
oxOutf("copying directory: {}\n", path);
// copy
oxRequire(fileList, src->ls(path));
for (const auto &name : fileList) {
auto currentFile = ox::sfmt("{}{}", path, name);
if (beginsWith(name, ".")) {
continue;
}
oxOutf("reading {}\n", currentFile);
oxRequire(stat, src->stat(currentFile));
if (stat.fileType == ox::FileType::Directory) {
oxReturnError(dest->mkdir(currentFile, true));
oxReturnError(copy(src, dest, currentFile + '/', verificationPairs));
} else {
// load file
oxRequireM(buff, src->read(currentFile));
// write file to dest
oxOutf("writing {}\n", currentFile);
oxReturnError(dest->write(currentFile, buff.data(), buff.size()));
oxReturnError(verifyFile(dest, currentFile, buff));
verificationPairs->emplace_back(std::move(currentFile), std::move(buff));
}
}
return {};
}
static ox::Error copyFS(ox::FileSystem *src, ox::FileSystem *dest) noexcept {
ox::Vector<VerificationPair> verificationPairs;
oxReturnError(copy(src, dest, "/", &verificationPairs));
// verify all at once in addition to right after the files are written
oxOutf("Verifying completed destination\n");
for (const auto &v : verificationPairs) {
oxReturnError(verifyFile(dest, v.path, v.buff));
}
return {};
}
// transformations need to be done after the copy to the new FS is complete
static ox::Error preloadObj(
ox::TypeStore *ts, ox::FileSystem *romFs,
GbaPreloader *pl, ox::CRStringView path) noexcept {
// load file
oxRequireM(buff, romFs->read(path));
oxRequireM(obj, keel::readAsset(ts, buff));
if (obj.type()->preloadable) {
oxOutf("preloading {}\n", path);
// preload
oxRequire(a, pl->startAlloc(ox::sizeOf<GbaPlatSpec>(&obj)));
const auto err = ox::preload<GbaPlatSpec, decltype(obj)>(pl, &obj);
oxReturnError(pl->endAlloc());
oxReturnError(err);
const keel::PreloadPtr p{.preloadAddr = static_cast<uint32_t>(a)};
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, 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 preloadDir(ox::TypeStore *ts, ox::FileSystem *romFs, GbaPreloader *pl, ox::CRStringView path) noexcept {
// copy
oxTracef("pack::preload", "path: {}", path);
oxRequire(fileList, romFs->ls(path));
for (const auto &name : fileList) {
const auto filePath = ox::sfmt("{}{}", path, name);
oxRequire(stat, romFs->stat(filePath));
if (stat.fileType == ox::FileType::Directory) {
const auto dir = ox::sfmt("{}{}/", path, name);
oxReturnError(preloadDir(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->buff().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 = 32u;
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(keel::Context *ctx, ox::TypeStore *ts, ox::FileSystem *dest) noexcept {
oxReturnError(copyFS(ctx->rom.get(), dest));
oxOut("Doing transforms\n");
oxReturnError(transformClaw(ctx, ts, dest, "/"));
return {};
}
ox::Error preload(ox::TypeStore *ts, ox::FileSystem *src, GbaPreloader *pl) noexcept {
oxOut("Preloading\n");
return preloadDir(ts, src, pl, "/");
}
}
#endif