diff --git a/src/nostalgia/modules/core/src/gba/gfx.cpp b/src/nostalgia/modules/core/src/gba/gfx.cpp index 308be953..e2404fd2 100644 --- a/src/nostalgia/modules/core/src/gba/gfx.cpp +++ b/src/nostalgia/modules/core/src/gba/gfx.cpp @@ -196,7 +196,7 @@ static ox::Error loadTileSheetSet( .targetBpp = static_cast(set.bpp), .setEntry = &entry, }; - oxReturnError(ox::readMC(ts, static_cast(tsStat.size), &target)); + oxReturnError(ox::readMC({ts, static_cast(tsStat.size)}, target)); tileWriteIdx += target.tileWriteIdx; } return {}; @@ -215,7 +215,7 @@ ox::Error loadBgTileSheet( .defaultPalette = {}, .tileMap = MEM_BG_TILES[cbb].data(), }; - oxReturnError(ox::readMC(ts, static_cast(tsStat.size), &target)); + oxReturnError(ox::readMC({ts, static_cast(tsStat.size)}, target)); // update bpp of all bgs with the updated cbb const auto bpp = ctx.cbbData[cbb].bpp; teagba::iterateBgCtl([bpp, cbb](volatile BgCtl &bgCtl) { @@ -267,7 +267,7 @@ ox::Error loadSpriteTileSheet( .defaultPalette = {}, .tileMap = MEM_SPRITE_TILES, }; - oxReturnError(ox::readMC(ts, static_cast(tsStat.size), &target)); + oxReturnError(ox::readMC({ts, static_cast(tsStat.size)}, target)); if (loadDefaultPalette && target.defaultPalette) { oxReturnError(loadSpritePalette(ctx, target.defaultPalette)); } diff --git a/src/nostalgia/modules/core/src/keel/keelmodule.cpp b/src/nostalgia/modules/core/src/keel/keelmodule.cpp index cc59d504..43221940 100644 --- a/src/nostalgia/modules/core/src/keel/keelmodule.cpp +++ b/src/nostalgia/modules/core/src/keel/keelmodule.cpp @@ -58,27 +58,21 @@ static class: public keel::Module { ox::Vector packTransforms() const noexcept final { return { // convert tilesheets to CompactTileSheets - [](keel::Context &ctx, ox::Buffer &buff) -> ox::Error { - oxRequire(hdr, keel::readAssetHeader(buff)); - auto const typeId = ox::buildTypeId( - hdr.clawHdr.typeName, hdr.clawHdr.typeVersion, hdr.clawHdr.typeParams); + [](keel::Context &ctx, ox::Buffer &buff, ox::StringView typeId) -> ox::Error { if (typeId == ox::ModelTypeId_v || typeId == ox::ModelTypeId_v || typeId == ox::ModelTypeId_v || typeId == ox::ModelTypeId_v) { - oxReturnError(keel::convertBuffToBuff( - ctx, buff, ox::ClawFormat::Metal).moveTo(buff)); + return keel::convertBuffToBuff( + ctx, buff, ox::ClawFormat::Metal).moveTo(buff); } return {}; }, - [](keel::Context &ctx, ox::Buffer &buff) -> ox::Error { - oxRequire(hdr, keel::readAssetHeader(buff)); - auto const typeId = ox::buildTypeId( - hdr.clawHdr.typeName, hdr.clawHdr.typeVersion, hdr.clawHdr.typeParams); + [](keel::Context &ctx, ox::Buffer &buff, ox::StringView typeId) -> ox::Error { if (typeId == ox::ModelTypeId_v || typeId == ox::ModelTypeId_v) { - oxReturnError(keel::convertBuffToBuff( - ctx, buff, ox::ClawFormat::Metal).moveTo(buff)); + return keel::convertBuffToBuff( + ctx, buff, ox::ClawFormat::Metal).moveTo(buff); } return {}; }, diff --git a/src/nostalgia/modules/core/src/keel/typeconv.hpp b/src/nostalgia/modules/core/src/keel/typeconv.hpp index 7c27fa7d..b7b47722 100644 --- a/src/nostalgia/modules/core/src/keel/typeconv.hpp +++ b/src/nostalgia/modules/core/src/keel/typeconv.hpp @@ -29,11 +29,11 @@ class TileSheetV1ToTileSheetV2Converter: public keel::Converter { - static void convertSubsheet( - TileSheetV2::SubSheet &src, - TileSheetV3::SubSheet &dst, - SubSheetId &idIt) noexcept; - ox::Error convert(keel::Context&, TileSheetV2 &src, TileSheetV3 &dst) const noexcept final; + static void convertSubsheet( + TileSheetV2::SubSheet &src, + TileSheetV3::SubSheet &dst, + SubSheetId &idIt) noexcept; + ox::Error convert(keel::Context&, TileSheetV2 &src, TileSheetV3 &dst) const noexcept final; }; class TileSheetV3ToTileSheetV4Converter: public keel::Converter { @@ -45,7 +45,7 @@ class TileSheetV3ToTileSheetV4Converter: public keel::Converter { - ox::Error convert(keel::Context&, TileSheet &src, CompactTileSheet &dst) const noexcept final; + ox::Error convert(keel::Context&, TileSheet &src, CompactTileSheet &dst) const noexcept final; }; } diff --git a/src/olympic/keel/include/keel/asset.hpp b/src/olympic/keel/include/keel/asset.hpp index f03b2a06..6fbd5584 100644 --- a/src/olympic/keel/include/keel/asset.hpp +++ b/src/olympic/keel/include/keel/asset.hpp @@ -11,9 +11,7 @@ namespace keel { constexpr auto K1HdrSz = 40; -ox::Result readUuidHeader(ox::Buffer const&buff) noexcept; - -ox::Result readUuidHeader(const char *buff, std::size_t buffLen) noexcept; +ox::Result readUuidHeader(ox::BufferView buff) noexcept; ox::Error writeUuidHeader(ox::Writer_c auto &writer, ox::UUID const&uuid) noexcept { oxReturnError(write(writer, "K1;")); @@ -22,24 +20,24 @@ ox::Error writeUuidHeader(ox::Writer_c auto &writer, ox::UUID const&uuid) noexce } template -ox::Result readAsset(ox::Buffer const&buff) noexcept { +ox::Result readAsset(ox::BufferView buff) noexcept { std::size_t offset = 0; const auto err = readUuidHeader(buff).error; if (!err) { offset = K1HdrSz; // the size of K1 headers } - return ox::readClaw(buff.data() + offset, buff.size() - offset); + return ox::readClaw(buff + offset); } -ox::Result readAsset(ox::TypeStore &ts, ox::Buffer const&buff) noexcept; +ox::Result readAsset(ox::TypeStore &ts, ox::BufferView buff) noexcept; struct AssetHdr { ox::UUID uuid; ox::ClawHeader clawHdr; }; -ox::Result readAssetHeader(char const*buff, std::size_t buffLen) noexcept; +ox::Result readAssetTypeId(ox::BufferView buff) noexcept; -ox::Result readAssetHeader(ox::Buffer const&buff) noexcept; +ox::Result readAssetHeader(ox::BufferView buff) noexcept; } diff --git a/src/olympic/keel/include/keel/context.hpp b/src/olympic/keel/include/keel/context.hpp index acc936c9..30847cb6 100644 --- a/src/olympic/keel/include/keel/context.hpp +++ b/src/olympic/keel/include/keel/context.hpp @@ -12,7 +12,7 @@ namespace keel { class Context; -using PackTransform = ox::Error(*)(Context&, ox::Buffer &clawData); +using PackTransform = ox::Error(*)(Context&, ox::Buffer &clawData, ox::StringView); class Context { public: @@ -36,4 +36,22 @@ class Context { constexpr virtual ~Context() noexcept = default; }; +constexpr ox::SpanView packTransforms( + [[maybe_unused]] Context const&ctx) noexcept { +#ifndef OX_BARE_METAL + return ctx.packTransforms; +#else + return {}; +#endif +} + +constexpr ox::SpanView converters( + [[maybe_unused]] Context const&ctx) noexcept { +#ifndef OX_BARE_METAL + return ctx.converters; +#else + return {}; +#endif +} + } diff --git a/src/olympic/keel/include/keel/typeconv.hpp b/src/olympic/keel/include/keel/typeconv.hpp index 35239488..2ece88c1 100644 --- a/src/olympic/keel/include/keel/typeconv.hpp +++ b/src/olympic/keel/include/keel/typeconv.hpp @@ -40,7 +40,7 @@ class WrapInline: public Wrap { }; template -constexpr auto makeWrap(Args &&...args) noexcept { +constexpr ox::UPtr makeWrap(Args &&...args) noexcept { return ox::make_unique>(ox::forward(args)...); } @@ -65,9 +65,10 @@ class BaseConverter { [[nodiscard]] virtual bool dstMatches(ox::CRStringView dstTypeName, int dstTypeVersion) const noexcept = 0; - virtual ox::Result> convertPtrToPtr(keel::Context &ctx, Wrap &src) const noexcept = 0; + virtual ox::Result> convertPtrToPtr(keel::Context &ctx, Wrap &src) const noexcept = 0; - virtual ox::Result> convertBuffToPtr(keel::Context &ctx, ox::Buffer const&srcBuff) const noexcept = 0; + virtual ox::Result> convertBuffToPtr( + keel::Context &ctx, ox::Buffer const&srcBuff) const noexcept = 0; [[nodiscard]] inline bool matches( @@ -84,12 +85,12 @@ class Converter: public BaseConverter { public: [[nodiscard]] ox::StringView srcTypeName() const noexcept final { - return ox::requireModelTypeName(); + return ox::ModelTypeName_v; } [[nodiscard]] int srcTypeVersion() const noexcept final { - return ox::requireModelTypeVersion(); + return ox::ModelTypeVersion_v; } [[nodiscard]] @@ -97,7 +98,7 @@ class Converter: public BaseConverter { constexpr auto SrcTypeName = ox::requireModelTypeName(); constexpr auto SrcTypeVersion = ox::requireModelTypeVersion(); return pSrcTypeName == SrcTypeName - && pSrcTypeVersion == SrcTypeVersion; + && pSrcTypeVersion == SrcTypeVersion; } [[nodiscard]] @@ -105,20 +106,22 @@ class Converter: public BaseConverter { constexpr auto DstTypeName = ox::StringView(ox::requireModelTypeName()); constexpr auto DstTypeVersion = ox::requireModelTypeVersion(); return dstTypeName == DstTypeName - && dstTypeVersion == DstTypeVersion; + && dstTypeVersion == DstTypeVersion; } - ox::Result> convertPtrToPtr(keel::Context &ctx, Wrap &src) const noexcept final { + ox::Result> convertPtrToPtr( + keel::Context &ctx, Wrap &src) const noexcept final { auto dst = makeWrap(); oxReturnError(convert(ctx, wrapCast(src), wrapCast(*dst))); - return ox::Result>(std::move(dst)); + return {std::move(dst)}; } - ox::Result> convertBuffToPtr(keel::Context &ctx, ox::Buffer const&srcBuff) const noexcept final { + ox::Result> convertBuffToPtr( + keel::Context &ctx, ox::Buffer const&srcBuff) const noexcept final { oxRequireM(src, readAsset(srcBuff)); auto dst = makeWrap(); oxReturnError(convert(ctx, src, wrapCast(*dst))); - return ox::Result>(std::move(dst)); + return {std::move(dst)}; } protected: @@ -126,9 +129,11 @@ class Converter: public BaseConverter { }; -ox::Result> convert( - keel::Context &ctx, ox::Buffer const&srcBuffer, - ox::CRStringView dstTypeName, int dstTypeVersion) noexcept; +ox::Result> convert( + keel::Context &ctx, + ox::Buffer const&srcBuffer, + ox::CRStringView dstTypeName, + int dstTypeVersion) noexcept; template ox::Result convert(keel::Context &ctx, ox::Buffer const&srcBuffer) noexcept { @@ -148,7 +153,8 @@ ox::Error convert(keel::Context &ctx, ox::Buffer const&buff, DstType *outObj) no } template -ox::Result convertBuffToBuff(keel::Context &ctx, ox::Buffer const&srcBuffer, ox::ClawFormat fmt) noexcept { +ox::Result convertBuffToBuff( + keel::Context &ctx, ox::Buffer const&srcBuffer, ox::ClawFormat fmt) noexcept { static constexpr auto DstTypeName = ox::requireModelTypeName(); static constexpr auto DstTypeVersion = ox::requireModelTypeVersion(); oxRequire(out, convert(ctx, srcBuffer, DstTypeName, DstTypeVersion)); @@ -156,10 +162,7 @@ ox::Result convertBuffToBuff(keel::Context &ctx, ox::Buffer const&sr } template -auto transformRule(keel::Context &ctx, ox::Buffer &buff) noexcept -> ox::Error { - oxRequire(hdr, readAssetHeader(buff)); - const auto typeId = ox::buildTypeId( - hdr.clawHdr.typeName, hdr.clawHdr.typeVersion, hdr.clawHdr.typeParams); +auto transformRule(keel::Context &ctx, ox::Buffer &buff, ox::StringView typeId) noexcept -> ox::Error { if (typeId == ox::ModelTypeId_v) { oxReturnError(keel::convertBuffToBuff(ctx, buff, fmt).moveTo(buff)); } diff --git a/src/olympic/keel/include/keel/typestore.hpp b/src/olympic/keel/include/keel/typestore.hpp index 699c569b..619c050b 100644 --- a/src/olympic/keel/include/keel/typestore.hpp +++ b/src/olympic/keel/include/keel/typestore.hpp @@ -19,7 +19,7 @@ class TypeStore: public ox::TypeStore { explicit TypeStore(ox::FileSystem &fs, ox::StringView descPath) noexcept; protected: - ox::Result> loadDescriptor(ox::CRStringView typeId) noexcept override; + ox::Result> loadDescriptor(ox::StringView typeId) noexcept override; }; } diff --git a/src/olympic/keel/src/asset.cpp b/src/olympic/keel/src/asset.cpp index 5caf6b2f..338ce25a 100644 --- a/src/olympic/keel/src/asset.cpp +++ b/src/olympic/keel/src/asset.cpp @@ -6,41 +6,45 @@ namespace keel { -ox::Result readUuidHeader(ox::Buffer const&buff) noexcept { - return readUuidHeader(buff.data(), buff.size()); -} - -ox::Result readUuidHeader(char const*buff, std::size_t buffLen) noexcept { - if (buffLen < K1HdrSz) { +ox::Result readUuidHeader(ox::BufferView buff) noexcept { + if (buff.size() < K1HdrSz) [[unlikely]] { return OxError(1, "Insufficient data to contain complete Keel header"); } constexpr ox::StringView k1Hdr = "K1;"; - if (k1Hdr != ox::StringView(buff, k1Hdr.bytes())) { + if (k1Hdr != ox::StringView(buff.data(), k1Hdr.bytes())) [[unlikely]] { return OxError(2, "No Keel asset header data"); } - return ox::UUID::fromString(ox::StringView(buff + k1Hdr.bytes(), 36)); + return ox::UUID::fromString(ox::StringView(buff.data() + k1Hdr.bytes(), 36)); } -ox::Result readAsset(ox::TypeStore &ts, ox::Buffer const&buff) noexcept { +ox::Result readAsset(ox::TypeStore &ts, ox::BufferView buff) noexcept { std::size_t offset = 0; if (!readUuidHeader(buff).error) { offset = K1HdrSz; } - return ox::readClaw(ts, buff.data() + offset, buff.size() - offset); + buff += offset; + return ox::readClaw(ts, buff); } -ox::Result readAssetHeader(char const*buff, std::size_t buffLen) noexcept { - AssetHdr out; - const auto err = readUuidHeader(buff, buffLen).moveTo(out.uuid); +ox::Result readAssetTypeId(ox::BufferView buff) noexcept { + const auto err = readUuidHeader(buff).error; const auto offset = err ? 0u : K1HdrSz; - buff = buff + offset; - buffLen = buffLen - offset; - oxReturnError(ox::readClawHeader(buff, buffLen).moveTo(out.clawHdr)); + if (offset >= buff.size()) [[unlikely]] { + return OxError(1, "Buffer too small for expected data"); + } + return ox::readClawTypeId(buff + offset); +} + +ox::Result readAssetHeader(ox::BufferView buff) noexcept { + ox::Result out; + const auto err = readUuidHeader(buff).moveTo(out.value.uuid); + const auto offset = err ? 0u : K1HdrSz; + if (offset >= buff.size()) [[unlikely]] { + return OxError(1, "Buffer too small for expected data"); + } + buff += offset; + oxReturnError(ox::readClawHeader(buff).moveTo(out.value.clawHdr)); return out; } -ox::Result readAssetHeader(ox::Buffer const&buff) noexcept { - return readAssetHeader(buff.data(), buff.size()); -} - } diff --git a/src/olympic/keel/src/media.cpp b/src/olympic/keel/src/media.cpp index 03a7e718..682f34a4 100644 --- a/src/olympic/keel/src/media.cpp +++ b/src/olympic/keel/src/media.cpp @@ -102,14 +102,11 @@ ox::Result uuidToPath(Context &ctx, ox::UUID const&uuid) noexcept { } ox::Error performPackTransforms(Context &ctx, ox::Buffer &clawData) noexcept { -#ifndef OX_BARE_METAL - for (auto tr : ctx.packTransforms) { - oxReturnError(tr(ctx, clawData)); + oxRequire(typeId, readAssetTypeId(clawData).to()); + for (auto const tr : packTransforms(ctx)) { + oxReturnError(tr(ctx, clawData, typeId)); } return {}; -#else - return OxError(1, "Transformations not supported on this platform"); -#endif } } @@ -153,7 +150,7 @@ ox::Result getPreloadAddr(keel::Context &ctx, ox::CRStringView path oxRequire(stat, ctx.rom->stat(path)); oxRequire(buff, static_cast(ctx.rom.get())->directAccess(path)); PreloadPtr p; - oxReturnError(ox::readMC(buff, static_cast(stat.size), &p)); + oxReturnError(ox::readMC({buff, static_cast(stat.size)}, p)); return static_cast(p.preloadAddr) + ctx.preloadSectionOffset; } @@ -161,7 +158,7 @@ ox::Result getPreloadAddr(keel::Context &ctx, ox::FileAddress const oxRequire(stat, ctx.rom->stat(file)); oxRequire(buff, static_cast(ctx.rom.get())->directAccess(file)); PreloadPtr p; - oxReturnError(ox::readMC(buff, static_cast(stat.size), &p)); + oxReturnError(ox::readMC({buff, static_cast(stat.size)}, p)); return static_cast(p.preloadAddr) + ctx.preloadSectionOffset; } diff --git a/src/olympic/keel/src/typeconv.cpp b/src/olympic/keel/src/typeconv.cpp index a53bea06..bb821797 100644 --- a/src/olympic/keel/src/typeconv.cpp +++ b/src/olympic/keel/src/typeconv.cpp @@ -10,13 +10,13 @@ namespace keel { [[nodiscard]] -static ox::Result findConverter( - ox::Vector const&converters, +static ox::Result findConverter( + ox::SpanView const&converters, ox::CRStringView srcTypeName, int srcTypeVersion, ox::CRStringView dstTypeName, int dstTypeVersion) noexcept { - for (auto &c : converters) { + for (auto const&c : converters) { if (c->matches(srcTypeName, srcTypeVersion, dstTypeName, dstTypeVersion)) { return c; } @@ -24,21 +24,22 @@ static ox::Result findConverter( return OxError(1, "Could not find converter"); }; -static ox::Result> convert( - [[maybe_unused]] keel::Context &ctx, - ox::Vector const&converters, - [[maybe_unused]] ox::Buffer const&srcBuffer, - [[maybe_unused]] ox::CRStringView srcTypeName, - [[maybe_unused]] int srcTypeVersion, - [[maybe_unused]] ox::CRStringView dstTypeName, - [[maybe_unused]] int dstTypeVersion) noexcept { +static ox::Result> convert( + keel::Context &ctx, + ox::SpanView const&converters, + ox::Buffer const&srcBuffer, + ox::CRStringView srcTypeName, + int srcTypeVersion, + ox::CRStringView dstTypeName, + int dstTypeVersion) noexcept { // look for direct converter - auto [c, err] = findConverter(converters, srcTypeName, srcTypeVersion, dstTypeName, dstTypeVersion); + auto [c, err] = findConverter( + converters, srcTypeName, srcTypeVersion, dstTypeName, dstTypeVersion); if (!err) { return c->convertBuffToPtr(ctx, srcBuffer); } // try to chain multiple converters - for (const auto &subConverter : converters) { + for (auto const&subConverter : converters) { if (!subConverter->dstMatches(dstTypeName, dstTypeVersion)) { continue; } @@ -52,24 +53,20 @@ static ox::Result> convert( return OxError(1, "Could not convert between types"); } -ox::Result> convert( - [[maybe_unused]] keel::Context &ctx, - [[maybe_unused]] ox::Buffer const&srcBuffer, - [[maybe_unused]] ox::CRStringView dstTypeName, - [[maybe_unused]] int dstTypeVersion) noexcept { -#ifndef OX_BARE_METAL +ox::Result> convert( + keel::Context &ctx, + ox::Buffer const&srcBuffer, + ox::CRStringView dstTypeName, + int dstTypeVersion) noexcept { oxRequire(hdr, readAssetHeader(srcBuffer)); return convert( ctx, - ctx.converters, + converters(ctx), srcBuffer, hdr.clawHdr.typeName, hdr.clawHdr.typeVersion, dstTypeName, dstTypeVersion); -#else - return OxError(1, "Operation not supported on this platform"); -#endif } } diff --git a/src/olympic/keel/src/typestore.cpp b/src/olympic/keel/src/typestore.cpp index 7afcc6b1..b5a79c74 100644 --- a/src/olympic/keel/src/typestore.cpp +++ b/src/olympic/keel/src/typestore.cpp @@ -11,11 +11,11 @@ TypeStore::TypeStore(ox::FileSystem &fs, ox::StringView descPath) noexcept: m_descPath(descPath) { } -ox::Result> TypeStore::loadDescriptor(ox::CRStringView typeId) noexcept { - auto path = ox::sfmt("{}/{}", m_descPath, typeId); +ox::Result> TypeStore::loadDescriptor(ox::StringView const typeId) noexcept { + auto const path = ox::sfmt("{}/{}", m_descPath, typeId); oxRequire(buff, m_fs.read(path)); auto dt = ox::make_unique(); - oxReturnError(ox::readClaw(buff, dt.get())); + oxReturnError(ox::readClaw(buff, *dt)); return dt; } diff --git a/src/olympic/studio/applib/src/main.cpp b/src/olympic/studio/applib/src/main.cpp index be05412b..b38ec361 100644 --- a/src/olympic/studio/applib/src/main.cpp +++ b/src/olympic/studio/applib/src/main.cpp @@ -68,7 +68,7 @@ static ox::Error run( static_cast(time << 1) }); // run app - auto const err = runApp(appName, projectDataDir, ox::UniquePtr(nullptr)); + auto const err = runApp(appName, projectDataDir, ox::UPtr(nullptr)); oxAssert(err, "Something went wrong..."); return err; }