From 7666bcc2db7adf5563facbebe8fe73188bc4c082 Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Sun, 3 Dec 2023 19:09:23 -0600 Subject: [PATCH] [keel,nostalgia] Cleanup pack, move toward further library-ization --- src/keel/pack.cpp | 10 ----- src/keel/pack.hpp | 48 ++++++++++++------------ src/nostalgia/tools/pack.cpp | 73 ++++++++++++++++++++---------------- 3 files changed, 65 insertions(+), 66 deletions(-) diff --git a/src/keel/pack.cpp b/src/keel/pack.cpp index 7c5f6d08..7f1cf309 100644 --- a/src/keel/pack.cpp +++ b/src/keel/pack.cpp @@ -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 {}; diff --git a/src/keel/pack.hpp b/src/keel/pack.hpp index 8c6cf5ea..888f621c 100644 --- a/src/keel/pack.hpp +++ b/src/keel/pack.hpp @@ -83,19 +83,19 @@ namespace detail { // transformations need to be done after the copy to the new FS is complete template ox::Error preloadObj( - ox::TypeStore *ts, - ox::FileSystem *romFs, - ox::Preloader *pl, + ox::TypeStore &ts, + ox::FileSystem &romFs, + ox::Preloader &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(&obj))); - const auto err = ox::preload(pl, &obj); - oxReturnError(pl->endAlloc()); + oxRequire(a, pl.startAlloc(ox::sizeOf(&obj))); + const auto err = ox::preload(&pl, &obj); + oxReturnError(pl.endAlloc()); oxReturnError(err); const keel::PreloadPtr p{.preloadAddr = static_cast(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 ox::Error preloadDir( - ox::TypeStore *ts, - ox::FileSystem *romFs, - ox::Preloader *pl, + ox::TypeStore &ts, + ox::FileSystem &romFs, + ox::Preloader &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 -ox::Error appendBinary(ox::Buffer *binBuff, ox::Buffer *fsBuff, ox::Preloader *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 &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 -ox::Error preload(ox::TypeStore *ts, ox::FileSystem *src, ox::Preloader *pl) noexcept { +ox::Error preload(ox::TypeStore &ts, ox::FileSystem &src, ox::Preloader &pl) noexcept { oxOut("Preloading\n"); return detail::preloadDir(ts, src, pl, "/"); } diff --git a/src/nostalgia/tools/pack.cpp b/src/nostalgia/tools/pack.cpp index c2f9214e..7a58ce7b 100644 --- a/src/nostalgia/tools/pack.cpp +++ b/src/nostalgia/tools/pack.cpp @@ -13,17 +13,17 @@ #include -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(buff.size())); - } catch (const std::fstream::failure&) { + } catch (std::fstream::failure const&) { return OxError(2, "failed to write file"); } return {}; } -static ox::Result readFileBuff(ox::CRStringView path) noexcept { +static ox::Result 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 readFileBuff(ox::CRStringView path) noexcept { file.seekg(0, std::ios::beg); file.read(buff.data(), static_cast(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(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(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(err); } +int main(int argc, const char **argv) { + OX_INIT_DEBUG_LOGGER(loggerConn, "nost-pack") + nostalgia::registerKeelModules(); + return packMain(argc, argv, "/.nostalgia"); +}