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

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

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");
}