From 05a46c3b07037dfec98b26c5ca51806853b65b04 Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Fri, 3 Feb 2023 22:11:58 -0600 Subject: [PATCH] [nostalgia] Move pack transforms to modules --- src/nostalgia/appmodules/CMakeLists.txt | 1 + src/nostalgia/core/module.cpp | 20 +++++++++++++++- src/nostalgia/core/module.hpp | 2 ++ src/nostalgia/foundation/context.hpp | 4 ++++ src/nostalgia/foundation/foundation.hpp | 12 ++++++---- src/nostalgia/foundation/module.cpp | 21 ++++++---------- src/nostalgia/foundation/module.hpp | 2 ++ src/nostalgia/tools/pack/pack.cpp | 32 ++++++++++++------------- 8 files changed, 58 insertions(+), 36 deletions(-) diff --git a/src/nostalgia/appmodules/CMakeLists.txt b/src/nostalgia/appmodules/CMakeLists.txt index ded19ad3..5394d5b3 100644 --- a/src/nostalgia/appmodules/CMakeLists.txt +++ b/src/nostalgia/appmodules/CMakeLists.txt @@ -10,6 +10,7 @@ endif() target_link_libraries( NostalgiaAppModules PUBLIC NostalgiaCore + NostalgiaFoundation ) install( diff --git a/src/nostalgia/core/module.cpp b/src/nostalgia/core/module.cpp index f67ed51b..fe93aaf9 100644 --- a/src/nostalgia/core/module.cpp +++ b/src/nostalgia/core/module.cpp @@ -2,14 +2,19 @@ * Copyright 2016 - 2023 Gary Talent (gary@drinkingtea.net). All rights reserved. */ +#include + #include +#include "gfx.hpp" #include "typeconv.hpp" #include "module.hpp" namespace nostalgia::core { +CoreModule CoreModule::mod; + ox::Vector CoreModule::converters() const noexcept { return { &nostalgiaPaletteToPaletteConverter, @@ -18,6 +23,19 @@ ox::Vector CoreModule::converters() const noexcept { }; } -CoreModule CoreModule::mod; +ox::Vector CoreModule::packTransforms() const noexcept { + return { + // convert tilesheets to CompactTileSheets + [](foundation::Context *ctx, ox::Buffer *buff) -> ox::Error { + oxRequire(hdr, ox::readClawHeader(*buff)); + const auto typeId = ox::buildTypeId(hdr.typeName, hdr.typeVersion); + if (typeId == ox::buildTypeId() || + typeId == ox::buildTypeId()) { + oxReturnError(foundation::convertBuffToBuff(ctx, *buff, ox::ClawFormat::Metal).moveTo(buff)); + } + return {}; + }, + }; +} } diff --git a/src/nostalgia/core/module.hpp b/src/nostalgia/core/module.hpp index 1bcc7874..712351fa 100644 --- a/src/nostalgia/core/module.hpp +++ b/src/nostalgia/core/module.hpp @@ -20,6 +20,8 @@ class CoreModule: public foundation::Module { static CoreModule mod; [[nodiscard]] ox::Vector converters() const noexcept override; + [[nodiscard]] + ox::Vector packTransforms() const noexcept override; }; } diff --git a/src/nostalgia/foundation/context.hpp b/src/nostalgia/foundation/context.hpp index 017b446a..e2c0a695 100644 --- a/src/nostalgia/foundation/context.hpp +++ b/src/nostalgia/foundation/context.hpp @@ -12,6 +12,9 @@ namespace nostalgia::foundation { +class Context; +using PackTransform = ox::Error(*)(Context*, ox::Buffer *clawData); + class Context { public: ox::UPtr rom; @@ -19,6 +22,7 @@ class Context { #ifndef OX_BARE_METAL AssetManager assetManager; ox::Vector converters; + ox::Vector packTransforms; #else std::size_t preloadSectionOffset = 0; #endif diff --git a/src/nostalgia/foundation/foundation.hpp b/src/nostalgia/foundation/foundation.hpp index a1838f86..a6f3df5e 100644 --- a/src/nostalgia/foundation/foundation.hpp +++ b/src/nostalgia/foundation/foundation.hpp @@ -11,18 +11,22 @@ namespace nostalgia::foundation { -template -ox::UPtr init(ox::UPtr &&fs, ox::CRStringView appName) noexcept { - auto ctx = ox::make_unique(); +template +ox::UPtr init(ox::UPtr &&fs, ox::CRStringView appName) noexcept { + auto ctx = ox::make_unique(); ctx->rom = std::move(fs); ctx->appName = appName; auto mods = modules(); if (mods) { for (auto &mod : *mods) { - // load type converters + // register type converters for (auto c : mod->converters()) { ctx->converters.emplace_back(c); } + // register pack transforms + for (auto c : mod->packTransforms()) { + ctx->packTransforms.emplace_back(c); + } } } return ctx; diff --git a/src/nostalgia/foundation/module.cpp b/src/nostalgia/foundation/module.cpp index 6f3efa2a..9fe570e8 100644 --- a/src/nostalgia/foundation/module.cpp +++ b/src/nostalgia/foundation/module.cpp @@ -6,26 +6,15 @@ namespace nostalgia::foundation { -[[nodiscard]] -static ox::Vector *moduleRegistry() noexcept { -#ifdef OX_BARE_METAL - return nullptr; -#else - static ox::Vector modules; - return &modules; -#endif -} +static ox::Vector mods; void registerModule(const Module *mod) noexcept { - auto mods = moduleRegistry(); - if (mods) { - mods->emplace_back(mod); - } + mods.emplace_back(mod); } [[nodiscard]] const ox::Vector *modules() noexcept { - return moduleRegistry(); + return &mods; } @@ -33,4 +22,8 @@ ox::Vector Module::converters() const noexcept { return {}; } +ox::Vector Module::packTransforms() const noexcept { + return {}; +} + } diff --git a/src/nostalgia/foundation/module.hpp b/src/nostalgia/foundation/module.hpp index 34d8b2ac..6687f262 100644 --- a/src/nostalgia/foundation/module.hpp +++ b/src/nostalgia/foundation/module.hpp @@ -20,6 +20,8 @@ class Module { constexpr virtual ~Module() noexcept = default; [[nodiscard]] virtual ox::Vector converters() const noexcept; + [[nodiscard]] + virtual ox::Vector packTransforms() const noexcept; }; void registerModule(const Module *mod) noexcept; diff --git a/src/nostalgia/tools/pack/pack.cpp b/src/nostalgia/tools/pack/pack.cpp index ac930dd0..0ee9fcba 100644 --- a/src/nostalgia/tools/pack/pack.cpp +++ b/src/nostalgia/tools/pack/pack.cpp @@ -8,7 +8,6 @@ #include #include -#include #include #include @@ -42,7 +41,7 @@ static ox::Error pathToInode(ox::FileSystem *dest, ox::ModelObject *obj) noexcep * @param buff buffer holding file * @return error */ -static ox::Error transformObj(ox::FileSystem *dest, ox::ModelObject *obj) noexcept { +static ox::Error transformFileAddresses(ox::FileSystem *dest, ox::ModelObject *obj) noexcept { for (auto &f : *obj) { auto &v = f->value; if (v.type() != ox::ModelValue::Type::Object) { @@ -52,31 +51,30 @@ static ox::Error transformObj(ox::FileSystem *dest, ox::ModelObject *obj) noexce if (o.typeName() == "net.drinkingtea.ox.FileAddress" && o.typeVersion() == 1) { oxReturnError(pathToInode(dest, &o)); } else { - oxReturnError(transformObj(dest, &o)); + oxReturnError(transformFileAddresses(dest, &o)); } } return {}; } static ox::Error doTransformations(foundation::Context *ctx, core::TypeStore *ts, ox::FileSystem *dest, ox::CRStringView filePath) noexcept { - if (endsWith(filePath, ".ng") || endsWith(filePath, ".npal")) { - // load file - oxRequire(s, dest->stat(filePath)); - oxRequireM(buff, dest->read(s.inode)); - if (endsWith(filePath, ".ng")) { - oxReturnError(foundation::convertBuffToBuff(ctx, buff, ox::ClawFormat::Metal).moveTo(&buff)); - } - oxRequireM(obj, ox::readClaw(ts, buff)); - // do transformations - oxReturnError(transformObj(dest, &obj)); - oxReturnError(ox::writeClaw(&obj).moveTo(&buff)); - // write file to dest - oxReturnError(dest->write(s.inode, buff.data(), buff.size())); + // load file + oxRequire(s, dest->stat(filePath)); + // do transformations + oxRequireM(buff, dest->read(s.inode)); + for (auto tr : ctx->packTransforms) { + oxReturnError(tr(ctx, &buff)); } + // transform FileAddresses + oxRequireM(obj, ox::readClaw(ts, buff)); + oxReturnError(transformFileAddresses(dest, &obj)); + oxReturnError(ox::writeClaw(&obj).moveTo(&buff)); + // write file to dest + oxReturnError(dest->write(s.inode, buff.data(), buff.size())); return {}; } -// claw file transformations are broken out because path to inode +// 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(foundation::Context *ctx, core::TypeStore *ts, ox::FileSystem *dest, ox::CRStringView path) noexcept { // copy