[nostalgia] Integrate Ox Preloader

This commit is contained in:
2022-11-30 01:47:33 -06:00
parent cbb496c59f
commit 090fe28b44
41 changed files with 404 additions and 159 deletions
+46 -15
View File
@@ -7,43 +7,74 @@
#include <ox/clargs/clargs.hpp>
#include <ox/fs/fs.hpp>
#include <nostalgia/core/typestore.hpp>
#include "pack/pack.hpp"
static ox::Error writeFileBuff(const ox::String &path, const ox::Buffer &buff) noexcept {
using namespace nostalgia;
static ox::Error writeFileBuff(ox::CRString path, const ox::Buffer &buff) noexcept {
try {
std::ofstream f(path.c_str(), std::ios::binary);
f.write(buff.data(), static_cast<intptr_t>(buff.size()));
} catch (const std::fstream::failure&) {
return OxError(2, "failed to write file");
}
return OxError(0);
return {};
}
static ox::Result<ox::Buffer> readFileBuff(const char *path) noexcept {
std::ifstream file(path, std::ios::binary | std::ios::ate);
if (!file.good()) {
oxErrorf("Could not find OxFS file: {}", path);
return OxError(1, "Could not find OxFS file");
}
try {
const auto size = static_cast<std::size_t>(file.tellg());
ox::Buffer buff(size);
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) {
oxErrorf("Could not read OxFS file: {}", e.what());
return OxError(2, "Could not read OxFS file");
}
}
static ox::Error run(const ox::ClArgs &args) noexcept {
ox::trace::init();
const auto argSrc = args.getString("src", "");
const auto argDst = args.getString("dst", "");
const auto argRomBin = args.getString("rom-bin", "");
if (argSrc == "") {
oxErr("\033[31;1;1merror:\033[0m must specify a source directory\n");
return OxError(1, "must specify a source directory");
}
if (argDst == "") {
oxErr("\033[31;1;1merror:\033[0m must specify a destination ROM file\n");
return OxError(1, "must specify a destination ROM file");
if (argRomBin == "") {
oxErr("\033[31;1;1merror:\033[0m must specify a path for preload file\n");
return OxError(1, "must specify a path for preload file");
}
ox::Buffer buff(32 * ox::units::MB);
oxReturnError(ox::FileSystem32::format(buff.data(), buff.size()));
ox::Buffer dstBuff(32 * ox::units::MB);
oxReturnError(ox::FileSystem32::format(dstBuff.data(), dstBuff.size()));
ox::PassThroughFS src(argSrc.c_str());
ox::FileSystem32 dst(ox::FileStore32(buff.data(), buff.size()));
oxReturnError(nostalgia::pack(&src, &dst));
ox::FileSystem32 dst(ox::FileStore32(dstBuff.data(), dstBuff.size()));
core::TypeStore ts(&src);
oxReturnError(pack(&ts, &src, &dst));
oxRequireM(pl, GbaPreloader::make());
oxReturnError(preload(&ts, &dst, pl.get()));
oxReturnError(dst.resize());
// resize buffer
oxRequire(dstSize, dst.size());
oxOutf("new size: {} bytes\n", dstSize);
buff.resize(dstSize);
dstBuff.resize(dstSize);
oxReturnError(writeFileBuff(argDst, buff));
return OxError(0);
oxRequireM(romBuff, readFileBuff(argRomBin.c_str()));
oxReturnError(appendBinary(&romBuff, &dstBuff, pl.get()));
oxOutf("new 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) {