[keel,nostalgia] Cleanup pack, move toward further library-ization
This commit is contained in:
parent
8f5173e52a
commit
7666bcc2db
@ -126,15 +126,6 @@ static ox::Error transformClaw(
|
|||||||
return {};
|
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(
|
static ox::Error copy(
|
||||||
ox::FileSystem &src,
|
ox::FileSystem &src,
|
||||||
ox::FileSystem &dest,
|
ox::FileSystem &dest,
|
||||||
@ -158,7 +149,6 @@ static ox::Error copy(
|
|||||||
// write file to dest
|
// write file to dest
|
||||||
oxOutf("writing {}\n", currentFile);
|
oxOutf("writing {}\n", currentFile);
|
||||||
oxReturnError(dest.write(currentFile, buff));
|
oxReturnError(dest.write(currentFile, buff));
|
||||||
oxReturnError(verifyFile(dest, currentFile, buff));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return {};
|
return {};
|
||||||
|
@ -83,19 +83,19 @@ namespace detail {
|
|||||||
// transformations need to be done after the copy to the new FS is complete
|
// transformations need to be done after the copy to the new FS is complete
|
||||||
template<typename PlatSpec>
|
template<typename PlatSpec>
|
||||||
ox::Error preloadObj(
|
ox::Error preloadObj(
|
||||||
ox::TypeStore *ts,
|
ox::TypeStore &ts,
|
||||||
ox::FileSystem *romFs,
|
ox::FileSystem &romFs,
|
||||||
ox::Preloader<PlatSpec> *pl,
|
ox::Preloader<PlatSpec> &pl,
|
||||||
ox::CRStringView path) noexcept {
|
ox::CRStringView path) noexcept {
|
||||||
// load file
|
// load file
|
||||||
oxRequireM(buff, romFs->read(path));
|
oxRequireM(buff, romFs.read(path));
|
||||||
oxRequireM(obj, keel::readAsset(ts, buff));
|
oxRequireM(obj, keel::readAsset(&ts, buff));
|
||||||
if (obj.type()->preloadable) {
|
if (obj.type()->preloadable) {
|
||||||
oxOutf("preloading {}\n", path);
|
oxOutf("preloading {}\n", path);
|
||||||
// preload
|
// preload
|
||||||
oxRequire(a, pl->startAlloc(ox::sizeOf<GbaPlatSpec>(&obj)));
|
oxRequire(a, pl.startAlloc(ox::sizeOf<GbaPlatSpec>(&obj)));
|
||||||
const auto err = ox::preload<GbaPlatSpec, decltype(obj)>(pl, &obj);
|
const auto err = ox::preload<GbaPlatSpec, decltype(obj)>(&pl, &obj);
|
||||||
oxReturnError(pl->endAlloc());
|
oxReturnError(pl.endAlloc());
|
||||||
oxReturnError(err);
|
oxReturnError(err);
|
||||||
const keel::PreloadPtr p{.preloadAddr = static_cast<uint32_t>(a)};
|
const keel::PreloadPtr p{.preloadAddr = static_cast<uint32_t>(a)};
|
||||||
oxReturnError(ox::writeMC(p).moveTo(&buff));
|
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
|
// strip the Claw header (it is not needed after preloading) and write back out to dest fs
|
||||||
oxReturnError(ox::writeMC(obj).moveTo(&buff));
|
oxReturnError(ox::writeMC(obj).moveTo(&buff));
|
||||||
}
|
}
|
||||||
oxReturnError(romFs->write(path, buff.data(), buff.size()));
|
oxReturnError(romFs.write(path, buff.data(), buff.size()));
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -111,16 +111,16 @@ ox::Error preloadObj(
|
|||||||
// transformations need to be done after the copy to the new FS is complete
|
// transformations need to be done after the copy to the new FS is complete
|
||||||
template<typename PlatSpec>
|
template<typename PlatSpec>
|
||||||
ox::Error preloadDir(
|
ox::Error preloadDir(
|
||||||
ox::TypeStore *ts,
|
ox::TypeStore &ts,
|
||||||
ox::FileSystem *romFs,
|
ox::FileSystem &romFs,
|
||||||
ox::Preloader<PlatSpec> *pl,
|
ox::Preloader<PlatSpec> &pl,
|
||||||
ox::CRStringView path) noexcept {
|
ox::CRStringView path) noexcept {
|
||||||
// copy
|
// copy
|
||||||
oxTracef("pack.preload", "path: {}", path);
|
oxTracef("pack.preload", "path: {}", path);
|
||||||
oxRequire(fileList, romFs->ls(path));
|
oxRequire(fileList, romFs.ls(path));
|
||||||
for (const auto &name : fileList) {
|
for (const auto &name : fileList) {
|
||||||
const auto filePath = ox::sfmt("{}{}", path, name);
|
const auto filePath = ox::sfmt("{}{}", path, name);
|
||||||
oxRequire(stat, romFs->stat(filePath));
|
oxRequire(stat, romFs.stat(filePath));
|
||||||
if (stat.fileType == ox::FileType::Directory) {
|
if (stat.fileType == ox::FileType::Directory) {
|
||||||
const auto dir = ox::sfmt("{}{}/", path, name);
|
const auto dir = ox::sfmt("{}{}/", path, name);
|
||||||
oxReturnError(preloadDir(ts, romFs, pl, dir));
|
oxReturnError(preloadDir(ts, romFs, pl, dir));
|
||||||
@ -134,29 +134,29 @@ ox::Error preloadDir(
|
|||||||
}
|
}
|
||||||
|
|
||||||
template<typename PlatSpec>
|
template<typename PlatSpec>
|
||||||
ox::Error appendBinary(ox::Buffer *binBuff, ox::Buffer *fsBuff, ox::Preloader<PlatSpec> *pl) noexcept {
|
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 {
|
constexpr auto padbin = [](ox::BufferWriter &w, unsigned factor) noexcept -> ox::Error {
|
||||||
return w->write(nullptr, factor - w->buff().size() % factor);
|
return w.write(nullptr, factor - w.buff().size() % factor);
|
||||||
};
|
};
|
||||||
constexpr ox::StringView mediaHdr = "KEEL_MEDIA_HEADER_______________";
|
constexpr ox::StringView mediaHdr = "KEEL_MEDIA_HEADER_______________";
|
||||||
constexpr ox::StringView preloadHdr = "KEEL_PRELOAD_HEADER_____________";
|
constexpr ox::StringView preloadHdr = "KEEL_PRELOAD_HEADER_____________";
|
||||||
constexpr auto hdrSize = 32u;
|
constexpr auto hdrSize = 32u;
|
||||||
static_assert(mediaHdr.bytes() == hdrSize);
|
static_assert(mediaHdr.bytes() == hdrSize);
|
||||||
static_assert(preloadHdr.bytes() == hdrSize);
|
static_assert(preloadHdr.bytes() == hdrSize);
|
||||||
ox::BufferWriter w(binBuff);
|
ox::BufferWriter w(&binBuff);
|
||||||
oxReturnError(padbin(&w, hdrSize));
|
oxReturnError(padbin(w, hdrSize));
|
||||||
oxReturnError(w.write(mediaHdr.data(), mediaHdr.bytes()));
|
oxReturnError(w.write(mediaHdr.data(), mediaHdr.bytes()));
|
||||||
oxReturnError(w.write(fsBuff->data(), fsBuff->size()));
|
oxReturnError(w.write(fsBuff.data(), fsBuff.size()));
|
||||||
oxReturnError(padbin(&w, hdrSize));
|
oxReturnError(padbin(w, hdrSize));
|
||||||
oxReturnError(w.write(preloadHdr.data(), preloadHdr.bytes()));
|
oxReturnError(w.write(preloadHdr.data(), preloadHdr.bytes()));
|
||||||
oxReturnError(pl->offsetPtrs(binBuff->size()));
|
oxReturnError(pl.offsetPtrs(binBuff.size()));
|
||||||
const auto &plBuff = pl->buff();
|
const auto &plBuff = pl.buff();
|
||||||
oxReturnError(w.write(plBuff.data(), plBuff.size()));
|
oxReturnError(w.write(plBuff.data(), plBuff.size()));
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename PlatSpec>
|
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");
|
oxOut("Preloading\n");
|
||||||
return detail::preloadDir(ts, src, pl, "/");
|
return detail::preloadDir(ts, src, pl, "/");
|
||||||
}
|
}
|
||||||
|
@ -13,17 +13,17 @@
|
|||||||
|
|
||||||
#include <nostalgia/modules/keelmodules.hpp>
|
#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 {
|
try {
|
||||||
std::ofstream f(std::string(toStdStringView(path)), std::ios::binary);
|
std::ofstream f(std::string(toStdStringView(path)), std::ios::binary);
|
||||||
f.write(buff.data(), static_cast<intptr_t>(buff.size()));
|
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 OxError(2, "failed to write file");
|
||||||
}
|
}
|
||||||
return {};
|
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);
|
std::ifstream file(std::string(toStdStringView(path)), std::ios::binary | std::ios::ate);
|
||||||
if (!file.good()) {
|
if (!file.good()) {
|
||||||
oxErrorf("Could not find OxFS file: {}", path);
|
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.seekg(0, std::ios::beg);
|
||||||
file.read(buff.data(), static_cast<std::streamsize>(buff.size()));
|
file.read(buff.data(), static_cast<std::streamsize>(buff.size()));
|
||||||
return buff;
|
return buff;
|
||||||
} catch (const std::ios_base::failure &e) {
|
} catch (std::ios_base::failure const&e) {
|
||||||
oxErrorf("Could not read OxFS file: {}", e.what());
|
oxErrorf("Could not read OxFS file: {}", e.what());
|
||||||
return OxError(2, "Could not read OxFS file");
|
return OxError(2, "Could not read OxFS file");
|
||||||
}
|
}
|
||||||
@ -50,7 +50,34 @@ static ox::Error generateTypes(ox::TypeStore *ts) noexcept {
|
|||||||
return {};
|
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 argSrc = args.getString("src", "");
|
||||||
const auto argRomBin = args.getString("rom-bin", "");
|
const auto argRomBin = args.getString("rom-bin", "");
|
||||||
if (argSrc == "") {
|
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");
|
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");
|
return OxError(1, "must specify a path for preload file");
|
||||||
}
|
}
|
||||||
ox::Buffer dstBuff(32 * ox::units::MB);
|
return pack(argSrc, argRomBin, projectDataDir);
|
||||||
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 {};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, const char **args) {
|
[[nodiscard]]
|
||||||
OX_INIT_DEBUG_LOGGER(loggerConn, "nost-pack")
|
int packMain(int argc, const char **argv, ox::StringView projectDataDir) noexcept {
|
||||||
nostalgia::registerKeelModules();
|
const auto err = run(argc, argv, projectDataDir);
|
||||||
const auto err = run(ox::ClArgs(argc, args));
|
|
||||||
oxAssert(err, "pack failed");
|
oxAssert(err, "pack failed");
|
||||||
return static_cast<int>(err);
|
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");
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user