[nostalgia] Move pack transforms to modules

This commit is contained in:
Gary Talent 2023-02-03 22:11:58 -06:00
parent b1b2780eb9
commit 05a46c3b07
8 changed files with 58 additions and 36 deletions

View File

@ -10,6 +10,7 @@ endif()
target_link_libraries(
NostalgiaAppModules PUBLIC
NostalgiaCore
NostalgiaFoundation
)
install(

View File

@ -2,14 +2,19 @@
* Copyright 2016 - 2023 Gary Talent (gary@drinkingtea.net). All rights reserved.
*/
#include <ox/model/model.hpp>
#include <nostalgia/foundation/module.hpp>
#include "gfx.hpp"
#include "typeconv.hpp"
#include "module.hpp"
namespace nostalgia::core {
CoreModule CoreModule::mod;
ox::Vector<foundation::BaseConverter*> CoreModule::converters() const noexcept {
return {
&nostalgiaPaletteToPaletteConverter,
@ -18,6 +23,19 @@ ox::Vector<foundation::BaseConverter*> CoreModule::converters() const noexcept {
};
}
CoreModule CoreModule::mod;
ox::Vector<foundation::PackTransform> 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<TileSheet>() ||
typeId == ox::buildTypeId<NostalgiaGraphic>()) {
oxReturnError(foundation::convertBuffToBuff<core::CompactTileSheet>(ctx, *buff, ox::ClawFormat::Metal).moveTo(buff));
}
return {};
},
};
}
}

View File

@ -20,6 +20,8 @@ class CoreModule: public foundation::Module {
static CoreModule mod;
[[nodiscard]]
ox::Vector<foundation::BaseConverter*> converters() const noexcept override;
[[nodiscard]]
ox::Vector<foundation::PackTransform> packTransforms() const noexcept override;
};
}

View File

@ -12,6 +12,9 @@
namespace nostalgia::foundation {
class Context;
using PackTransform = ox::Error(*)(Context*, ox::Buffer *clawData);
class Context {
public:
ox::UPtr<ox::FileSystem> rom;
@ -19,6 +22,7 @@ class Context {
#ifndef OX_BARE_METAL
AssetManager assetManager;
ox::Vector<class BaseConverter*> converters;
ox::Vector<PackTransform> packTransforms;
#else
std::size_t preloadSectionOffset = 0;
#endif

View File

@ -11,18 +11,22 @@
namespace nostalgia::foundation {
template<typename Context = foundation::Context>
ox::UPtr<Context> init(ox::UPtr<ox::FileSystem> &&fs, ox::CRStringView appName) noexcept {
auto ctx = ox::make_unique<Context>();
template<typename Ctx = foundation::Context>
ox::UPtr<Ctx> init(ox::UPtr<ox::FileSystem> &&fs, ox::CRStringView appName) noexcept {
auto ctx = ox::make_unique<Ctx>();
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;

View File

@ -6,26 +6,15 @@
namespace nostalgia::foundation {
[[nodiscard]]
static ox::Vector<const Module*> *moduleRegistry() noexcept {
#ifdef OX_BARE_METAL
return nullptr;
#else
static ox::Vector<const Module*> modules;
return &modules;
#endif
}
static ox::Vector<const Module*> mods;
void registerModule(const Module *mod) noexcept {
auto mods = moduleRegistry();
if (mods) {
mods->emplace_back(mod);
}
mods.emplace_back(mod);
}
[[nodiscard]]
const ox::Vector<const Module*> *modules() noexcept {
return moduleRegistry();
return &mods;
}
@ -33,4 +22,8 @@ ox::Vector<foundation::BaseConverter*> Module::converters() const noexcept {
return {};
}
ox::Vector<PackTransform> Module::packTransforms() const noexcept {
return {};
}
}

View File

@ -20,6 +20,8 @@ class Module {
constexpr virtual ~Module() noexcept = default;
[[nodiscard]]
virtual ox::Vector<foundation::BaseConverter*> converters() const noexcept;
[[nodiscard]]
virtual ox::Vector<PackTransform> packTransforms() const noexcept;
};
void registerModule(const Module *mod) noexcept;

View File

@ -8,7 +8,6 @@
#include <ox/model/modelvalue.hpp>
#include <nostalgia/core/gfx.hpp>
#include <nostalgia/core/typeconv.hpp>
#include <nostalgia/core/typestore.hpp>
#include <nostalgia/foundation/media.hpp>
@ -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<core::CompactTileSheet>(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