[keel,nostalgia] Cleanup pack, move toward further library-ization

This commit is contained in:
Gary Talent 2023-12-03 19:09:23 -06:00
parent 8f5173e52a
commit 7666bcc2db
3 changed files with 65 additions and 66 deletions

View File

@ -126,15 +126,6 @@ static ox::Error transformClaw(
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);
}
static ox::Error copy(
ox::FileSystem &src,
ox::FileSystem &dest,
@ -158,7 +149,6 @@ static ox::Error copy(
// write file to dest
oxOutf("writing {}\n", currentFile);
oxReturnError(dest.write(currentFile, buff));
oxReturnError(verifyFile(dest, currentFile, buff));
}
}
return {};

View File

@ -83,19 +83,19 @@ namespace detail {
// transformations need to be done after the copy to the new FS is complete
template<typename PlatSpec>
ox::Error preloadObj(
ox::TypeStore *ts,
ox::FileSystem *romFs,
ox::Preloader<PlatSpec> *pl,
ox::TypeStore &ts,
ox::FileSystem &romFs,
ox::Preloader<PlatSpec> &pl,
ox::CRStringView path) noexcept {
// load file
oxRequireM(buff, romFs->read(path));
oxRequireM(obj, keel::readAsset(ts, buff));
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());
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));
@ -103,7 +103,7 @@ ox::Error preloadObj(
// 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()));
oxReturnError(romFs.write(path, buff.data(), buff.size()));
return {};
}
@ -111,16 +111,16 @@ ox::Error preloadObj(
// transformations need to be done after the copy to the new FS is complete
template<typename PlatSpec>
ox::Error preloadDir(
ox::TypeStore *ts,
ox::FileSystem *romFs,
ox::Preloader<PlatSpec> *pl,
ox::TypeStore &ts,
ox::FileSystem &romFs,
ox::Preloader<PlatSpec> &pl,
ox::CRStringView path) noexcept {
// copy
oxTracef("pack.preload", "path: {}", path);
oxRequire(fileList, romFs->ls(path));
oxRequire(fileList, romFs.ls(path));
for (const auto &name : fileList) {
const auto filePath = ox::sfmt("{}{}", path, name);
oxRequire(stat, romFs->stat(filePath));
oxRequire(stat, romFs.stat(filePath));
if (stat.fileType == ox::FileType::Directory) {
const auto dir = ox::sfmt("{}{}/", path, name);
oxReturnError(preloadDir(ts, romFs, pl, dir));
@ -134,29 +134,29 @@ ox::Error preloadDir(
}
template<typename PlatSpec>
ox::Error appendBinary(ox::Buffer *binBuff, ox::Buffer *fsBuff, ox::Preloader<PlatSpec> *pl) noexcept {
constexpr auto padbin = [](ox::BufferWriter *w, unsigned factor) noexcept -> ox::Error {
return w->write(nullptr, factor - w->buff().size() % factor);
ox::Error appendBinary(ox::Buffer &binBuff, ox::Buffer &fsBuff, ox::Preloader<PlatSpec> &pl) noexcept {
constexpr auto padbin = [](ox::BufferWriter &w, unsigned factor) noexcept -> ox::Error {
return w.write(nullptr, factor - w.buff().size() % factor);
};
constexpr ox::StringView mediaHdr = "KEEL_MEDIA_HEADER_______________";
constexpr ox::StringView preloadHdr = "KEEL_PRELOAD_HEADER_____________";
constexpr auto hdrSize = 32u;
static_assert(mediaHdr.bytes() == hdrSize);
static_assert(preloadHdr.bytes() == hdrSize);
ox::BufferWriter w(binBuff);
oxReturnError(padbin(&w, 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(fsBuff.data(), fsBuff.size()));
oxReturnError(padbin(w, hdrSize));
oxReturnError(w.write(preloadHdr.data(), preloadHdr.bytes()));
oxReturnError(pl->offsetPtrs(binBuff->size()));
const auto &plBuff = pl->buff();
oxReturnError(pl.offsetPtrs(binBuff.size()));
const auto &plBuff = pl.buff();
oxReturnError(w.write(plBuff.data(), plBuff.size()));
return {};
}
template<typename PlatSpec>
ox::Error preload(ox::TypeStore *ts, ox::FileSystem *src, ox::Preloader<PlatSpec> *pl) noexcept {
ox::Error preload(ox::TypeStore &ts, ox::FileSystem &src, ox::Preloader<PlatSpec> &pl) noexcept {
oxOut("Preloading\n");
return detail::preloadDir(ts, src, pl, "/");
}

View File

@ -13,17 +13,17 @@
#include <nostalgia/modules/keelmodules.hpp>
static ox::Error writeFileBuff(ox::CRStringView path, const ox::Buffer &buff) noexcept {
static ox::Error writeFileBuff(ox::StringView path, ox::Buffer const&buff) noexcept {
try {
std::ofstream f(std::string(toStdStringView(path)), std::ios::binary);
f.write(buff.data(), static_cast<intptr_t>(buff.size()));
} catch (const std::fstream::failure&) {
} catch (std::fstream::failure const&) {
return OxError(2, "failed to write file");
}
return {};
}
static ox::Result<ox::Buffer> readFileBuff(ox::CRStringView path) noexcept {
static ox::Result<ox::Buffer> readFileBuff(ox::StringView path) noexcept {
std::ifstream file(std::string(toStdStringView(path)), std::ios::binary | std::ios::ate);
if (!file.good()) {
oxErrorf("Could not find OxFS file: {}", path);
@ -35,7 +35,7 @@ static ox::Result<ox::Buffer> readFileBuff(ox::CRStringView path) noexcept {
file.seekg(0, std::ios::beg);
file.read(buff.data(), static_cast<std::streamsize>(buff.size()));
return buff;
} catch (const std::ios_base::failure &e) {
} catch (std::ios_base::failure const&e) {
oxErrorf("Could not read OxFS file: {}", e.what());
return OxError(2, "Could not read OxFS file");
}
@ -50,7 +50,34 @@ static ox::Error generateTypes(ox::TypeStore *ts) noexcept {
return {};
}
static ox::Error run(const ox::ClArgs &args) noexcept {
static ox::Error pack(ox::StringView argSrc, ox::StringView argRomBin, ox::StringView projectDataDir) noexcept {
ox::Buffer dstBuff(32 * ox::units::MB);
oxReturnError(ox::FileSystem32::format(dstBuff.data(), dstBuff.size()));
ox::FileSystem32 dst(dstBuff);
oxRequire(ctx, keel::init(ox::make_unique<ox::PassThroughFS>(argSrc), "keel-pack"));
keel::TypeStore ts(*ctx->rom, ox::sfmt("{}/type_descriptors", projectDataDir));
oxReturnError(generateTypes(&ts));
oxReturnError(keel::pack(*ctx, ts, dst));
oxRequireM(pl, keel::GbaPreloader::make());
oxReturnError(preload(ts, dst, *pl));
oxReturnError(dst.resize());
// resize buffer
oxRequire(dstSize, dst.size());
dstBuff.resize(dstSize);
oxRequireM(romBuff, readFileBuff(argRomBin));
oxReturnError(appendBinary(romBuff, dstBuff, *pl));
oxOutf("Dest FS size: {} bytes\n", dstSize);
oxOutf("Preload buff size: {} bytes\n", pl->buff().size());
oxOutf("ROM buff size: {} bytes\n", romBuff.size());
oxReturnError(writeFileBuff(argRomBin, romBuff));
return {};
}
static ox::Error run(int argc, const char **argv, ox::StringView projectDataDir) noexcept {
ox::ClArgs const args(argc, argv);
const auto argSrc = args.getString("src", "");
const auto argRomBin = args.getString("rom-bin", "");
if (argSrc == "") {
@ -61,35 +88,17 @@ static ox::Error run(const ox::ClArgs &args) noexcept {
oxErr("\033[31;1;1merror:\033[0m must specify a path for ROM file\n");
return OxError(1, "must specify a path for preload file");
}
ox::Buffer dstBuff(32 * ox::units::MB);
oxReturnError(ox::FileSystem32::format(dstBuff.data(), dstBuff.size()));
ox::FileSystem32 dst(dstBuff);
oxRequire(ctx, keel::init(ox::make_unique<ox::PassThroughFS>(argSrc), "nost-pack"));
keel::TypeStore ts(*ctx->rom, ox::String("/.nostalgia/type_descriptors"));
oxReturnError(generateTypes(&ts));
oxReturnError(keel::pack(*ctx, ts, dst));
oxRequireM(pl, keel::GbaPreloader::make());
oxReturnError(preload(&ts, &dst, pl.get()));
oxReturnError(dst.resize());
// resize buffer
oxRequire(dstSize, dst.size());
dstBuff.resize(dstSize);
oxRequireM(romBuff, readFileBuff(argRomBin));
oxReturnError(appendBinary(&romBuff, &dstBuff, pl.get()));
oxOutf("Dest FS size: {} bytes\n", dstSize);
oxOutf("Preload buff size: {} bytes\n", pl->buff().size());
oxOutf("ROM buff size: {} bytes\n", romBuff.size());
oxReturnError(writeFileBuff(argRomBin, romBuff));
return {};
return pack(argSrc, argRomBin, projectDataDir);
}
int main(int argc, const char **args) {
OX_INIT_DEBUG_LOGGER(loggerConn, "nost-pack")
nostalgia::registerKeelModules();
const auto err = run(ox::ClArgs(argc, args));
[[nodiscard]]
int packMain(int argc, const char **argv, ox::StringView projectDataDir) noexcept {
const auto err = run(argc, argv, projectDataDir);
oxAssert(err, "pack failed");
return static_cast<int>(err);
}
int main(int argc, const char **argv) {
OX_INIT_DEBUG_LOGGER(loggerConn, "nost-pack")
nostalgia::registerKeelModules();
return packMain(argc, argv, "/.nostalgia");
}