diff --git a/src/keel/media.cpp b/src/keel/media.cpp index def57fb6..b80ec3b4 100644 --- a/src/keel/media.cpp +++ b/src/keel/media.cpp @@ -46,9 +46,9 @@ static void clearUuidMap(Context *ctx) noexcept { ctx->pathToUuid.clear(); } -void createUuidMapping(Context *ctx, const ox::String &filePath, const ox::UUID &uuid) noexcept { +void createUuidMapping(Context *ctx, ox::String filePath, const ox::UUID &uuid) noexcept { ctx->pathToUuid[filePath] = uuid; - ctx->uuidToPath[uuid.toString()] = filePath; + ctx->uuidToPath[uuid.toString()] = std::move(filePath); } static ox::Error buildUuidMap(Context *ctx, ox::CRStringView path) noexcept { @@ -78,6 +78,41 @@ ox::Error buildUuidMap(Context *ctx) noexcept { return buildUuidMap(ctx, ""); } +ox::Result pathToUuid(Context &ctx, ox::CRStringView path) noexcept { +#ifndef OX_BARE_METAL + return ctx.pathToUuid[path]; +#else + return OxError(1, "UUID to path conversion not supported on this platform"); +#endif +} + +ox::Result uuidToPath(Context &ctx, ox::CRStringView uuid) noexcept { +#ifndef OX_BARE_METAL + return ctx.uuidToPath[uuid]; +#else + return OxError(1, "UUID to path conversion not supported on this platform"); +#endif +} + +ox::Result uuidToPath(Context &ctx, ox::UUID const&uuid) noexcept { +#ifndef OX_BARE_METAL + return ctx.uuidToPath[uuid.toString()]; +#else + return OxError(1, "UUID to path conversion not supported on this platform"); +#endif +} + +ox::Error performPackTransforms(Context &ctx, ox::Buffer &clawData) noexcept { +#ifndef OX_BARE_METAL + for (auto tr : ctx.packTransforms) { + oxReturnError(tr(&ctx, &clawData)); + } + return {}; +#else + return OxError(1, "Transformations not supported on this platform"); +#endif +} + } #else diff --git a/src/keel/media.hpp b/src/keel/media.hpp index 8d5ef1dd..a76813c8 100644 --- a/src/keel/media.hpp +++ b/src/keel/media.hpp @@ -91,10 +91,16 @@ ox::Result> readObjNoCache( #endif -void createUuidMapping(Context *ctx, const ox::String &filePath, const ox::UUID &uuid) noexcept; +void createUuidMapping(Context *ctx, ox::String filePath, const ox::UUID &uuid) noexcept; ox::Error buildUuidMap(Context *ctx) noexcept; +ox::Result uuidToPath(Context &ctx, ox::CRStringView uuid) noexcept; + +ox::Result uuidToPath(Context &ctx, ox::UUID const&uuid) noexcept; + +ox::Error performPackTransforms(Context &ctx, ox::Buffer &clawData) noexcept; + template ox::Result> setAsset(keel::Context *ctx, ox::StringView assetId, T const&asset) noexcept { #ifndef OX_BARE_METAL diff --git a/src/keel/pack.cpp b/src/keel/pack.cpp index 402f1916..7c5f6d08 100644 --- a/src/keel/pack.cpp +++ b/src/keel/pack.cpp @@ -4,7 +4,6 @@ #include #include -#include #include @@ -13,8 +12,8 @@ namespace keel { static ox::Error pathToInode( - [[maybe_unused]] keel::Context *ctx, ox::FileSystem *dest, ox::ModelObject *obj) noexcept { - auto &o = *obj; + keel::Context &ctx, ox::FileSystem &dest, ox::ModelObject &obj) noexcept { + auto &o = obj; auto type = static_cast(o["type"].get()); auto &data = o["data"].get(); ox::String path; @@ -30,35 +29,39 @@ static ox::Error pathToInode( return {}; } if (beginsWith(path, "uuid://")) { -#ifndef OX_BARE_METAL - const auto uuid = substr(ox::StringView(path), 7); - path = ctx->uuidToPath[uuid]; -#else - return OxError(1, "UUID to path conversion not supported on this platform"); -#endif + const auto uuid = substr(path, 7); + oxReturnError(keel::uuidToPath(ctx, uuid).moveTo(&path)); } - oxRequire(s, dest->stat(path)); + oxRequire(s, dest.stat(path)); oxReturnError(o["type"].set(static_cast(ox::FileAddressType::Inode))); return data.set(2, s.inode); } -static ox::Error transformFileAddressesObj(keel::Context *ctx, ox::FileSystem *dest, ox::ModelObject *obj) noexcept; -static ox::Error transformFileAddressesVec(keel::Context *ctx, ox::FileSystem *dest, ox::ModelValueVector *v) noexcept; +static ox::Error transformFileAddressesObj( + keel::Context &ctx, ox::FileSystem &dest, ox::ModelObject &obj) noexcept; +static ox::Error transformFileAddressesVec( + keel::Context &ctx, ox::FileSystem &dest, ox::ModelValueVector &v) noexcept; -static ox::Error transformFileAddresses(keel::Context *ctx, ox::FileSystem *dest, ox::ModelValue *v) noexcept { - if (v->type() == ox::ModelValue::Type::Object) { - auto &obj = v->get(); - return transformFileAddressesObj(ctx, dest, &obj); - } else if (v->type() == ox::ModelValue::Type::Vector) { - auto &vec = v->get(); - return transformFileAddressesVec(ctx, dest, &vec); +static ox::Error transformFileAddresses( + keel::Context &ctx, + ox::FileSystem &dest, + ox::ModelValue &v) noexcept { + if (v.type() == ox::ModelValue::Type::Object) { + auto &obj = v.get(); + return transformFileAddressesObj(ctx, dest, obj); + } else if (v.type() == ox::ModelValue::Type::Vector) { + auto &vec = v.get(); + return transformFileAddressesVec(ctx, dest, vec); } return {}; } -static ox::Error transformFileAddressesVec(keel::Context *ctx, ox::FileSystem *dest, ox::ModelValueVector *v) noexcept { - for (auto &f : *v) { - oxReturnError(transformFileAddresses(ctx, dest, &f)); +static ox::Error transformFileAddressesVec( + keel::Context &ctx, + ox::FileSystem &dest, + ox::ModelValueVector &v) noexcept { + for (auto &f : v) { + oxReturnError(transformFileAddresses(ctx, dest, f)); } return {}; } @@ -67,51 +70,52 @@ static ox::Error transformFileAddressesVec(keel::Context *ctx, ox::FileSystem *d * Convert path references in Claw data to inodes to save space * @return error */ -static ox::Error transformFileAddressesObj(keel::Context *ctx, ox::FileSystem *dest, ox::ModelObject *obj) noexcept { - if (obj->typeName() == "net.drinkingtea.ox.FileAddress" && obj->typeVersion() == 1) { +static ox::Error transformFileAddressesObj( + keel::Context &ctx, + ox::FileSystem &dest, + ox::ModelObject &obj) noexcept { + if (obj.typeName() == "net.drinkingtea.ox.FileAddress" && obj.typeVersion() == 1) { return pathToInode(ctx, dest, obj); } - for (auto &f : *obj) { + for (auto &f : obj) { auto &v = f->value; - oxReturnError(transformFileAddresses(ctx, dest, &v)); + oxReturnError(transformFileAddresses(ctx, dest, v)); } return {}; } static ox::Error doTransformations( - [[maybe_unused]] keel::Context *ctx, - [[maybe_unused]] ox::TypeStore *ts, - [[maybe_unused]] ox::FileSystem *dest, - [[maybe_unused]] ox::CRStringView filePath) noexcept { -#ifndef OX_BARE_METAL + keel::Context &ctx, + ox::TypeStore &ts, + ox::FileSystem &dest, + ox::CRStringView filePath) noexcept { // load file - oxRequire(s, dest->stat(filePath)); + oxRequire(s, dest.stat(filePath)); // do transformations - oxRequireM(buff, dest->read(s.inode)); - for (auto tr : ctx->packTransforms) { - oxReturnError(tr(ctx, &buff)); - } + oxRequireM(buff, dest.read(s.inode)); + oxReturnError(keel::performPackTransforms(ctx, buff)); // transform FileAddresses - oxRequireM(obj, keel::readAsset(ts, buff)); - oxReturnError(transformFileAddressesObj(ctx, dest, &obj)); + oxRequireM(obj, keel::readAsset(&ts, buff)); + oxReturnError(transformFileAddressesObj(ctx, dest, obj)); oxReturnError(ox::writeClaw(obj).moveTo(&buff)); // write file to dest - oxReturnError(dest->write(s.inode, buff.data(), buff.size())); + oxReturnError(dest.write(s.inode, buff)); return {}; -#else - return OxError(1, "Transformations not supported on this platform"); -#endif } // claw file transformations are broken out from copy because path to inode // transformations need to be done after the copy to the new FS is complete -static ox::Error transformClaw(keel::Context *ctx, ox::TypeStore *ts, ox::FileSystem *dest, ox::CRStringView path) noexcept { +static ox::Error transformClaw( + keel::Context &ctx, + ox::TypeStore &ts, + ox::FileSystem &dest, + ox::CRStringView path) noexcept { // copy oxTracef("pack.transformClaw", "path: {}", path); - oxRequire(fileList, dest->ls(path)); + oxRequire(fileList, dest.ls(path)); for (const auto &name : fileList) { const auto filePath = ox::sfmt("{}{}", path, name); - oxRequire(stat, dest->stat(filePath)); + oxRequire(stat, dest.stat(filePath)); if (stat.fileType == ox::FileType::Directory) { const auto dir = ox::sfmt("{}{}/", path, name); oxReturnError(transformClaw(ctx, ts, dest, dir)); @@ -122,61 +126,51 @@ static ox::Error transformClaw(keel::Context *ctx, ox::TypeStore *ts, ox::FileSy return {}; } -static ox::Error verifyFile(ox::FileSystem *fs, ox::CRStringView path, const ox::Buffer &expected) noexcept { +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())); + oxReturnError(fs.read(path, buff.data(), buff.size())); return OxError(buff == expected ? 0 : 1); } -struct VerificationPair { - ox::String path; - ox::Buffer buff; - VerificationPair(ox::String &&pPath, ox::Buffer &&pBuff) noexcept: - path(ox::forward(pPath)), - buff(ox::forward(pBuff)) { - } -}; - -static ox::Error copy(ox::FileSystem *src, ox::FileSystem *dest, ox::CRStringView path, ox::Vector *verificationPairs) noexcept { +static ox::Error copy( + ox::FileSystem &src, + ox::FileSystem &dest, + ox::CRStringView path) noexcept { oxOutf("copying directory: {}\n", path); // copy - oxRequire(fileList, src->ls(path)); + oxRequire(fileList, src.ls(path)); for (const auto &name : fileList) { auto currentFile = ox::sfmt("{}{}", path, name); if (beginsWith(name, ".")) { continue; } oxOutf("reading {}\n", currentFile); - oxRequire(stat, src->stat(currentFile)); + oxRequire(stat, src.stat(currentFile)); if (stat.fileType == ox::FileType::Directory) { - oxReturnError(dest->mkdir(currentFile, true)); - oxReturnError(copy(src, dest, currentFile + '/', verificationPairs)); + oxReturnError(dest.mkdir(currentFile, true)); + oxReturnError(copy(src, dest, currentFile + '/')); } else { // load file - oxRequireM(buff, src->read(currentFile)); + oxRequireM(buff, src.read(currentFile)); // write file to dest oxOutf("writing {}\n", currentFile); - oxReturnError(dest->write(currentFile, buff.data(), buff.size())); + oxReturnError(dest.write(currentFile, buff)); oxReturnError(verifyFile(dest, currentFile, buff)); - verificationPairs->emplace_back(std::move(currentFile), std::move(buff)); } } return {}; } -static ox::Error copyFS(ox::FileSystem *src, ox::FileSystem *dest) noexcept { - ox::Vector verificationPairs; - oxReturnError(copy(src, dest, "/", &verificationPairs)); - // verify all at once in addition to right after the files are written - oxOut("Verifying completed destination\n"); - for (const auto &v : verificationPairs) { - oxReturnError(verifyFile(dest, v.path, v.buff)); - } +static ox::Error copyFS(ox::FileSystem &src, ox::FileSystem &dest) noexcept { + oxReturnError(copy(src, dest, "/")); return {}; } -ox::Error pack(keel::Context *ctx, ox::TypeStore *ts, ox::FileSystem *dest) noexcept { - oxReturnError(copyFS(ctx->rom.get(), dest)); +ox::Error pack(keel::Context &ctx, ox::TypeStore &ts, ox::FileSystem &dest) noexcept { + oxReturnError(copyFS(*ctx.rom, dest)); oxOut("Doing transforms\n"); oxReturnError(transformClaw(ctx, ts, dest, "/")); return {}; diff --git a/src/keel/pack.hpp b/src/keel/pack.hpp index e9c117ea..8c6cf5ea 100644 --- a/src/keel/pack.hpp +++ b/src/keel/pack.hpp @@ -161,6 +161,6 @@ ox::Error preload(ox::TypeStore *ts, ox::FileSystem *src, ox::Preloader