[nostalgia] Add type registration to Module, fix FileAddress conversion in vectors for nost-pack

This commit is contained in:
Gary Talent 2023-02-08 03:04:59 -06:00
parent dba31d2cd9
commit 3b05d4e16b
10 changed files with 129 additions and 39 deletions

View File

@ -15,6 +15,17 @@ namespace nostalgia::core {
CoreModule CoreModule::mod; CoreModule CoreModule::mod;
ox::Vector<foundation::TypeDescGenerator> CoreModule::types() const noexcept {
return {
foundation::generateTypeDesc<TileSheetV1>,
foundation::generateTypeDesc<TileSheetV2>,
foundation::generateTypeDesc<TileSheet>,
foundation::generateTypeDesc<CompactTileSheet>,
foundation::generateTypeDesc<NostalgiaPalette>,
foundation::generateTypeDesc<Palette>,
};
}
ox::Vector<const foundation::BaseConverter*> CoreModule::converters() const noexcept { ox::Vector<const foundation::BaseConverter*> CoreModule::converters() const noexcept {
return { return {
&nostalgiaPaletteToPaletteConverter, &nostalgiaPaletteToPaletteConverter,
@ -33,7 +44,8 @@ ox::Vector<foundation::PackTransform> CoreModule::packTransforms() const noexcep
if (typeId == ox::buildTypeId<TileSheetV1>() || if (typeId == ox::buildTypeId<TileSheetV1>() ||
typeId == ox::buildTypeId<TileSheetV2>() || typeId == ox::buildTypeId<TileSheetV2>() ||
typeId == ox::buildTypeId<TileSheet>()) { typeId == ox::buildTypeId<TileSheet>()) {
oxReturnError(foundation::convertBuffToBuff<core::CompactTileSheet>(ctx, *buff, ox::ClawFormat::Metal).moveTo(buff)); oxReturnError(foundation::convertBuffToBuff<core::CompactTileSheet>(
ctx, *buff, ox::ClawFormat::Metal).moveTo(buff));
} }
return {}; return {};
}, },

View File

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

View File

@ -18,6 +18,10 @@ const ox::Vector<const Module*> *modules() noexcept {
} }
ox::Vector<TypeDescGenerator> Module::types() const noexcept {
return {};
}
ox::Vector<const foundation::BaseConverter*> Module::converters() const noexcept { ox::Vector<const foundation::BaseConverter*> Module::converters() const noexcept {
return {}; return {};
} }

View File

@ -5,11 +5,19 @@
#pragma once #pragma once
#include <ox/std/vector.hpp> #include <ox/std/vector.hpp>
#include <ox/model/descwrite.hpp>
#include "typeconv.hpp" #include "typeconv.hpp"
namespace nostalgia::foundation { namespace nostalgia::foundation {
using TypeDescGenerator = ox::Error(*)(ox::TypeStore*);
template<typename T>
ox::Error generateTypeDesc(ox::TypeStore *ts) noexcept {
return ox::buildTypeDef<T>(ts).error;
}
class Module { class Module {
public: public:
constexpr Module() noexcept = default; constexpr Module() noexcept = default;
@ -19,6 +27,8 @@ class Module {
Module &operator=(Module&&) noexcept = delete; Module &operator=(Module&&) noexcept = delete;
constexpr virtual ~Module() noexcept = default; constexpr virtual ~Module() noexcept = default;
[[nodiscard]] [[nodiscard]]
virtual ox::Vector<TypeDescGenerator> types() const noexcept;
[[nodiscard]]
virtual ox::Vector<const foundation::BaseConverter*> converters() const noexcept; virtual ox::Vector<const foundation::BaseConverter*> converters() const noexcept;
[[nodiscard]] [[nodiscard]]
virtual ox::Vector<PackTransform> packTransforms() const noexcept; virtual ox::Vector<PackTransform> packTransforms() const noexcept;

View File

@ -19,6 +19,28 @@ class Wrap {
virtual ~Wrap() = default; virtual ~Wrap() = default;
}; };
template<typename T>
class WrapIndirect: public Wrap {
private:
T *m_obj = nullptr;
public:
template<typename... Args>
constexpr explicit WrapIndirect(Args &&...args): m_obj(ox::forward<Args>(args)...) {
}
[[nodiscard]]
constexpr auto obj() const noexcept {
return &m_obj;
}
[[nodiscard]]
constexpr auto obj() noexcept {
return &m_obj;
}
};
template<typename T> template<typename T>
class WrapInline: public Wrap { class WrapInline: public Wrap {
private: private:

View File

@ -4,12 +4,20 @@
#include <ox/model/model.hpp> #include <ox/model/model.hpp>
#include "scenestatic.hpp"
#include "scenemodule.hpp" #include "scenemodule.hpp"
namespace nostalgia::scene { namespace nostalgia::scene {
SceneModule SceneModule::mod; SceneModule SceneModule::mod;
ox::Vector<foundation::TypeDescGenerator> SceneModule::types() const noexcept {
return {
foundation::generateTypeDesc<SceneDoc>,
foundation::generateTypeDesc<SceneStatic>,
};
}
ox::Vector<const foundation::BaseConverter*> SceneModule::converters() const noexcept { ox::Vector<const foundation::BaseConverter*> SceneModule::converters() const noexcept {
return { return {
&sceneDocToSceneStaticConverter, &sceneDocToSceneStaticConverter,

View File

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

View File

@ -11,6 +11,7 @@
#include <nostalgia/appmodules/appmodules.hpp> #include <nostalgia/appmodules/appmodules.hpp>
#include <nostalgia/core/typestore.hpp> #include <nostalgia/core/typestore.hpp>
#include <nostalgia/foundation/foundation.hpp> #include <nostalgia/foundation/foundation.hpp>
#include <nostalgia/foundation/module.hpp>
#include "pack/pack.hpp" #include "pack/pack.hpp"
@ -44,6 +45,15 @@ static ox::Result<ox::Buffer> readFileBuff(ox::CRStringView path) noexcept {
} }
} }
static ox::Error generateTypes(ox::TypeStore *ts) noexcept {
for (const auto mod : *foundation::modules()) {
for (auto gen : mod->types()) {
oxReturnError(gen(ts));
}
}
return {};
}
static ox::Error run(const ox::ClArgs &args) noexcept { static ox::Error run(const ox::ClArgs &args) noexcept {
loadModules(); loadModules();
const auto argSrc = args.getString("src", ""); const auto argSrc = args.getString("src", "");
@ -61,6 +71,7 @@ static ox::Error run(const ox::ClArgs &args) noexcept {
ox::FileSystem32 dst(ox::FileStore32(dstBuff.data(), dstBuff.size())); ox::FileSystem32 dst(ox::FileStore32(dstBuff.data(), dstBuff.size()));
const auto ctx = foundation::init(ox::make_unique<ox::PassThroughFS>(argSrc), "nost-pack"); const auto ctx = foundation::init(ox::make_unique<ox::PassThroughFS>(argSrc), "nost-pack");
core::TypeStore ts(ctx->rom.get()); core::TypeStore ts(ctx->rom.get());
oxReturnError(generateTypes(&ts));
oxReturnError(pack(ctx.get(), &ts, &dst)); oxReturnError(pack(ctx.get(), &ts, &dst));
oxRequireM(pl, GbaPreloader::make()); oxRequireM(pl, GbaPreloader::make());
oxReturnError(preload(&ts, &dst, pl.get())); oxReturnError(preload(&ts, &dst, pl.get()));

View File

@ -7,8 +7,6 @@
#include <ox/model/descwrite.hpp> #include <ox/model/descwrite.hpp>
#include <ox/model/modelvalue.hpp> #include <ox/model/modelvalue.hpp>
#include <nostalgia/core/gfx.hpp>
#include <nostalgia/core/typestore.hpp>
#include <nostalgia/foundation/media.hpp> #include <nostalgia/foundation/media.hpp>
#include "pack.hpp" #include "pack.hpp"
@ -36,28 +34,43 @@ static ox::Error pathToInode(ox::FileSystem *dest, ox::ModelObject *obj) noexcep
return data.set(2, s.inode); return data.set(2, s.inode);
} }
/** static ox::Error transformFileAddressesObj(ox::FileSystem *dest, ox::ModelObject *obj) noexcept;
* Convert path references in Claw data to inodes to save space static ox::Error transformFileAddressesVec(ox::FileSystem *dest, ox::ModelValueVector *v) noexcept;
* @param buff buffer holding file
* @return error static ox::Error transformFileAddresses(ox::FileSystem *dest, ox::ModelValue *v) noexcept {
*/ if (v->type() == ox::ModelValue::Type::Object) {
static ox::Error transformFileAddresses(ox::FileSystem *dest, ox::ModelObject *obj) noexcept { auto &obj = v->get<ox::ModelObject>();
for (auto &f : *obj) { return transformFileAddressesObj(dest, &obj);
auto &v = f->value; } else if (v->type() == ox::ModelValue::Type::Vector) {
if (v.type() != ox::ModelValue::Type::Object) { auto &vec = v->get<ox::ModelValueVector>();
continue; return transformFileAddressesVec(dest, &vec);
}
auto &o = v.get<ox::ModelObject>();
if (o.typeName() == "net.drinkingtea.ox.FileAddress" && o.typeVersion() == 1) {
oxReturnError(pathToInode(dest, &o));
} else {
oxReturnError(transformFileAddresses(dest, &o));
}
} }
return {}; return {};
} }
static ox::Error doTransformations(foundation::Context *ctx, core::TypeStore *ts, ox::FileSystem *dest, ox::CRStringView filePath) noexcept { static ox::Error transformFileAddressesVec(ox::FileSystem *dest, ox::ModelValueVector *v) noexcept {
for (auto &f : *v) {
oxReturnError(transformFileAddresses(dest, &f));
}
return {};
}
/**
* Convert path references in Claw data to inodes to save space
* @return error
*/
static ox::Error transformFileAddressesObj(ox::FileSystem *dest, ox::ModelObject *obj) noexcept {
if (obj->typeName() == "net.drinkingtea.ox.FileAddress" && obj->typeVersion() == 1) {
return pathToInode(dest, obj);
}
for (auto &f : *obj) {
auto &v = f->value;
oxReturnError(transformFileAddresses(dest, &v));
}
return {};
}
static ox::Error doTransformations(foundation::Context *ctx, ox::TypeStore *ts, ox::FileSystem *dest, ox::CRStringView filePath) noexcept {
// load file // load file
oxRequire(s, dest->stat(filePath)); oxRequire(s, dest->stat(filePath));
// do transformations // do transformations
@ -67,7 +80,7 @@ static ox::Error doTransformations(foundation::Context *ctx, core::TypeStore *ts
} }
// transform FileAddresses // transform FileAddresses
oxRequireM(obj, ox::readClaw(ts, buff)); oxRequireM(obj, ox::readClaw(ts, buff));
oxReturnError(transformFileAddresses(dest, &obj)); oxReturnError(transformFileAddressesObj(dest, &obj));
oxReturnError(ox::writeClaw(&obj).moveTo(&buff)); oxReturnError(ox::writeClaw(&obj).moveTo(&buff));
// write file to dest // write file to dest
oxReturnError(dest->write(s.inode, buff.data(), buff.size())); oxReturnError(dest->write(s.inode, buff.data(), buff.size()));
@ -76,7 +89,7 @@ static ox::Error doTransformations(foundation::Context *ctx, core::TypeStore *ts
// claw file transformations are broken out from copy 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 // 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 { static ox::Error transformClaw(foundation::Context *ctx, ox::TypeStore *ts, ox::FileSystem *dest, ox::CRStringView path) noexcept {
// copy // copy
oxTracef("pack::transformClaw", "path: {}", path); oxTracef("pack::transformClaw", "path: {}", path);
oxRequire(fileList, dest->ls(path)); oxRequire(fileList, dest->ls(path));
@ -108,9 +121,8 @@ struct VerificationPair {
} }
}; };
static ox::Error copy(ox::FileSystem *src, ox::FileSystem *dest, ox::CRStringView path) noexcept { static ox::Error copy(ox::FileSystem *src, ox::FileSystem *dest, ox::CRStringView path, ox::Vector<VerificationPair> *verificationPairs) noexcept {
oxOutf("copying directory: {}\n", path); oxOutf("copying directory: {}\n", path);
ox::Vector<VerificationPair> verificationPairs;
// copy // copy
oxRequire(fileList, src->ls(path)); oxRequire(fileList, src->ls(path));
for (const auto &name : fileList) { for (const auto &name : fileList) {
@ -122,7 +134,7 @@ static ox::Error copy(ox::FileSystem *src, ox::FileSystem *dest, ox::CRStringVie
oxRequire(stat, src->stat(currentFile)); oxRequire(stat, src->stat(currentFile));
if (stat.fileType == ox::FileType::Directory) { if (stat.fileType == ox::FileType::Directory) {
oxReturnError(dest->mkdir(currentFile, true)); oxReturnError(dest->mkdir(currentFile, true));
oxReturnError(copy(src, dest, currentFile + '/')); oxReturnError(copy(src, dest, currentFile + '/', verificationPairs));
} else { } else {
// load file // load file
oxRequireM(buff, src->read(currentFile)); oxRequireM(buff, src->read(currentFile));
@ -130,10 +142,17 @@ static ox::Error copy(ox::FileSystem *src, ox::FileSystem *dest, ox::CRStringVie
oxOutf("writing {}\n", currentFile); oxOutf("writing {}\n", currentFile);
oxReturnError(dest->write(currentFile, buff.data(), buff.size())); oxReturnError(dest->write(currentFile, buff.data(), buff.size()));
oxReturnError(verifyFile(dest, currentFile, buff)); oxReturnError(verifyFile(dest, currentFile, buff));
verificationPairs.emplace_back(std::move(currentFile), std::move(buff)); verificationPairs->emplace_back(std::move(currentFile), std::move(buff));
} }
} }
return {};
}
static ox::Error copyFS(ox::FileSystem *src, ox::FileSystem *dest) noexcept {
ox::Vector<VerificationPair> verificationPairs;
oxReturnError(copy(src, dest, "/", &verificationPairs));
// verify all at once in addition to right after the files are written // verify all at once in addition to right after the files are written
oxOutf("Verifying completed destination\n");
for (const auto &v : verificationPairs) { for (const auto &v : verificationPairs) {
oxReturnError(verifyFile(dest, v.path, v.buff)); oxReturnError(verifyFile(dest, v.path, v.buff));
} }
@ -141,11 +160,14 @@ static ox::Error copy(ox::FileSystem *src, ox::FileSystem *dest, ox::CRStringVie
} }
// transformations need to be done after the copy to the new FS is complete // transformations need to be done after the copy to the new FS is complete
static ox::Error preloadObj(core::TypeStore *ts, ox::FileSystem *romFs, GbaPreloader *pl, ox::CRStringView path) noexcept { static ox::Error preloadObj(
ox::TypeStore *ts, ox::FileSystem *romFs,
GbaPreloader *pl, ox::CRStringView path) noexcept {
// load file // load file
oxRequireM(buff, romFs->read(path)); oxRequireM(buff, romFs->read(path));
oxRequireM(obj, ox::readClaw(ts, buff)); oxRequireM(obj, ox::readClaw(ts, buff));
if (obj.type()->preloadable) { if (obj.type()->preloadable) {
oxOutf("preloading {}\n", path);
// preload // preload
oxRequire(a, pl->startAlloc(ox::sizeOf<GbaPlatSpec>(&obj))); oxRequire(a, pl->startAlloc(ox::sizeOf<GbaPlatSpec>(&obj)));
const auto err = ox::preload<GbaPlatSpec, decltype(obj)>(pl, &obj); const auto err = ox::preload<GbaPlatSpec, decltype(obj)>(pl, &obj);
@ -163,7 +185,7 @@ static ox::Error preloadObj(core::TypeStore *ts, ox::FileSystem *romFs, GbaPrelo
// claw file transformations are broken out because path to inode // claw file transformations are broken out because path to inode
// transformations need to be done after the copy to the new FS is complete // transformations need to be done after the copy to the new FS is complete
static ox::Error preloadDir(core::TypeStore *ts, ox::FileSystem *romFs, GbaPreloader *pl, ox::CRStringView path) noexcept { static ox::Error preloadDir(ox::TypeStore *ts, ox::FileSystem *romFs, GbaPreloader *pl, ox::CRStringView path) noexcept {
// copy // copy
oxTracef("pack::preload", "path: {}", path); oxTracef("pack::preload", "path: {}", path);
oxRequire(fileList, romFs->ls(path)); oxRequire(fileList, romFs->ls(path));
@ -202,14 +224,15 @@ ox::Error appendBinary(ox::Buffer *binBuff, ox::Buffer *fsBuff, GbaPreloader *pl
return {}; return {};
} }
ox::Error pack(foundation::Context *ctx, core::TypeStore *ts, ox::FileSystem *dest) noexcept { ox::Error pack(foundation::Context *ctx, ox::TypeStore *ts, ox::FileSystem *dest) noexcept {
oxReturnError(copy(ctx->rom.get(), dest, "/")); oxReturnError(copyFS(ctx->rom.get(), dest));
oxReturnError(ox::buildTypeDef<core::CompactTileSheet>(ts)); oxOut("Doing transforms\n");
oxReturnError(transformClaw(ctx, ts, dest, "/")); oxReturnError(transformClaw(ctx, ts, dest, "/"));
return {}; return {};
} }
ox::Error preload(core::TypeStore *ts, ox::FileSystem *src, GbaPreloader *pl) noexcept { ox::Error preload(ox::TypeStore *ts, ox::FileSystem *src, GbaPreloader *pl) noexcept {
oxOut("Preloading\n");
return preloadDir(ts, src, pl, "/"); return preloadDir(ts, src, pl, "/");
} }

View File

@ -11,10 +11,6 @@ namespace foundation {
class Context; class Context;
} }
namespace core {
class TypeStore;
}
struct GbaPlatSpec { struct GbaPlatSpec {
using PtrType = uint32_t; using PtrType = uint32_t;
using size_t = uint32_t; using size_t = uint32_t;
@ -95,8 +91,8 @@ using GbaPreloader = ox::Preloader<GbaPlatSpec>;
ox::Error appendBinary(ox::Buffer *binBuff, ox::Buffer *fsBuff, GbaPreloader *pl) noexcept; ox::Error appendBinary(ox::Buffer *binBuff, ox::Buffer *fsBuff, GbaPreloader *pl) noexcept;
auto pack(foundation::Context *ctx, core::TypeStore *ts, ox::FileSystem *dest) noexcept -> ox::Error; auto pack(foundation::Context *ctx, ox::TypeStore *ts, ox::FileSystem *dest) noexcept -> ox::Error;
auto preload(core::TypeStore *ts, ox::FileSystem *src, GbaPreloader *ph) noexcept -> ox::Error; auto preload(ox::TypeStore *ts, ox::FileSystem *src, GbaPreloader *ph) noexcept -> ox::Error;
} }