[nostalgia] Make assets loadable through UUID references

This commit is contained in:
Gary Talent 2023-02-12 23:37:58 -06:00
parent a96d173fdc
commit 86a3bf1248
13 changed files with 27 additions and 20 deletions

Binary file not shown.

View File

@ -1 +1 @@
M2;net.drinkingtea.nostalgia.core.Palette;1;ûÿ³Ö N1;14fc3dd8-42ff-4bf9-81f1-a010cc5ac251;M2;net.drinkingtea.nostalgia.core.Palette;1;ûÿ³Ö

View File

@ -1 +1 @@
M2;net.drinkingtea.nostalgia.core.Palette;1;ûÿ³Ö N1;0f75977f-1c52-45f8-9793-52ea2dc200a0;M2;net.drinkingtea.nostalgia.core.Palette;1;ûÿ³Ö

View File

@ -1 +1 @@
M2;net.drinkingtea.nostalgia.core.Palette;1;PÛ{³ÖCˆ N1;c79f21e2-f74f-4ad9-90ed-32b0ef7da6ed;M2;net.drinkingtea.nostalgia.core.Palette;1;PÛ{³ÖCˆ

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -21,7 +21,6 @@ class Context {
ox::StringView appName = "Nostalgia Foundation App"; ox::StringView appName = "Nostalgia Foundation App";
#ifndef OX_BARE_METAL #ifndef OX_BARE_METAL
AssetManager assetManager; AssetManager assetManager;
bool useUuids = false;
ox::HashMap<ox::String, ox::UUID> pathToUuid; ox::HashMap<ox::String, ox::UUID> pathToUuid;
ox::HashMap<ox::UUIDStr, ox::String> uuidToPath; ox::HashMap<ox::UUIDStr, ox::String> uuidToPath;
ox::Vector<const class BaseConverter*> converters; ox::Vector<const class BaseConverter*> converters;

View File

@ -37,7 +37,7 @@ ox::Result<std::size_t> getPreloadAddr(foundation::Context *ctx, ox::CRStringVie
template<typename T> template<typename T>
ox::Result<foundation::AssetRef<T>> readObjFile( ox::Result<foundation::AssetRef<T>> readObjFile(
foundation::Context *ctx, foundation::Context *ctx,
ox::CRStringView assetId, ox::StringView assetId,
bool forceLoad) noexcept { bool forceLoad) noexcept {
constexpr auto readConvert = [](Context *ctx, const ox::Buffer &buff) -> ox::Result<T> { constexpr auto readConvert = [](Context *ctx, const ox::Buffer &buff) -> ox::Result<T> {
auto [obj, err] = readAsset<T>(buff); auto [obj, err] = readAsset<T>(buff);
@ -50,20 +50,24 @@ ox::Result<foundation::AssetRef<T>> readObjFile(
return std::move(obj); return std::move(obj);
}; };
ox::StringView path; ox::StringView path;
if (ctx->useUuids) { ox::UUIDStr uuidStr;
if (beginsWith(assetId, "uuid://")) {
assetId = assetId.substr(7);
path = ctx->uuidToPath[assetId]; path = ctx->uuidToPath[assetId];
} else { } else {
path = assetId; path = assetId;
uuidStr = ctx->pathToUuid[path].toString();
assetId = uuidStr;
} }
if (forceLoad) { if (forceLoad) {
oxRequire(buff, ctx->rom->read(assetId)); oxRequire(buff, ctx->rom->read(path));
oxRequire(obj, readConvert(ctx, buff)); oxRequire(obj, readConvert(ctx, buff));
oxRequire(cached, ctx->assetManager.setAsset(assetId, obj)); oxRequire(cached, ctx->assetManager.setAsset(assetId, obj));
return cached; return cached;
} else { } else {
auto [cached, err] = ctx->assetManager.getAsset<T>(assetId); auto [cached, err] = ctx->assetManager.getAsset<T>(assetId);
if (err) { if (err) {
oxRequire(buff, ctx->rom->read(assetId)); oxRequire(buff, ctx->rom->read(path));
oxRequire(obj, readConvert(ctx, buff)); oxRequire(obj, readConvert(ctx, buff));
oxReturnError(ctx->assetManager.setAsset(assetId, obj).moveTo(&cached)); oxReturnError(ctx->assetManager.setAsset(assetId, obj).moveTo(&cached));
} }

View File

@ -13,7 +13,7 @@
namespace nostalgia { namespace nostalgia {
static ox::Error pathToInode(ox::FileSystem *dest, ox::ModelObject *obj) noexcept { static ox::Error pathToInode(foundation::Context *ctx, ox::FileSystem *dest, ox::ModelObject *obj) noexcept {
auto &o = *obj; auto &o = *obj;
auto type = static_cast<ox::FileAddressType>(o["type"].get<int8_t>()); auto type = static_cast<ox::FileAddressType>(o["type"].get<int8_t>());
auto &data = o["data"].get<ox::ModelUnion>(); auto &data = o["data"].get<ox::ModelUnion>();
@ -29,28 +29,32 @@ static ox::Error pathToInode(ox::FileSystem *dest, ox::ModelObject *obj) noexcep
case ox::FileAddressType::None: case ox::FileAddressType::None:
return {}; return {};
} }
if (beginsWith(path, "uuid://")) {
const auto uuid = ox::StringView(path).substr(7);
path = ctx->uuidToPath[uuid];
}
oxRequire(s, dest->stat(path)); oxRequire(s, dest->stat(path));
oxReturnError(o["type"].set(static_cast<int8_t>(ox::FileAddressType::Inode))); oxReturnError(o["type"].set(static_cast<int8_t>(ox::FileAddressType::Inode)));
return data.set(2, s.inode); return data.set(2, s.inode);
} }
static ox::Error transformFileAddressesObj(ox::FileSystem *dest, ox::ModelObject *obj) noexcept; static ox::Error transformFileAddressesObj(foundation::Context *ctx, ox::FileSystem *dest, ox::ModelObject *obj) noexcept;
static ox::Error transformFileAddressesVec(ox::FileSystem *dest, ox::ModelValueVector *v) noexcept; static ox::Error transformFileAddressesVec(foundation::Context *ctx, ox::FileSystem *dest, ox::ModelValueVector *v) noexcept;
static ox::Error transformFileAddresses(ox::FileSystem *dest, ox::ModelValue *v) noexcept { static ox::Error transformFileAddresses(foundation::Context *ctx, ox::FileSystem *dest, ox::ModelValue *v) noexcept {
if (v->type() == ox::ModelValue::Type::Object) { if (v->type() == ox::ModelValue::Type::Object) {
auto &obj = v->get<ox::ModelObject>(); auto &obj = v->get<ox::ModelObject>();
return transformFileAddressesObj(dest, &obj); return transformFileAddressesObj(ctx, dest, &obj);
} else if (v->type() == ox::ModelValue::Type::Vector) { } else if (v->type() == ox::ModelValue::Type::Vector) {
auto &vec = v->get<ox::ModelValueVector>(); auto &vec = v->get<ox::ModelValueVector>();
return transformFileAddressesVec(dest, &vec); return transformFileAddressesVec(ctx, dest, &vec);
} }
return {}; return {};
} }
static ox::Error transformFileAddressesVec(ox::FileSystem *dest, ox::ModelValueVector *v) noexcept { static ox::Error transformFileAddressesVec(foundation::Context *ctx, ox::FileSystem *dest, ox::ModelValueVector *v) noexcept {
for (auto &f : *v) { for (auto &f : *v) {
oxReturnError(transformFileAddresses(dest, &f)); oxReturnError(transformFileAddresses(ctx, dest, &f));
} }
return {}; return {};
} }
@ -59,13 +63,13 @@ static ox::Error transformFileAddressesVec(ox::FileSystem *dest, ox::ModelValueV
* Convert path references in Claw data to inodes to save space * Convert path references in Claw data to inodes to save space
* @return error * @return error
*/ */
static ox::Error transformFileAddressesObj(ox::FileSystem *dest, ox::ModelObject *obj) noexcept { static ox::Error transformFileAddressesObj(foundation::Context *ctx, ox::FileSystem *dest, ox::ModelObject *obj) noexcept {
if (obj->typeName() == "net.drinkingtea.ox.FileAddress" && obj->typeVersion() == 1) { if (obj->typeName() == "net.drinkingtea.ox.FileAddress" && obj->typeVersion() == 1) {
return pathToInode(dest, obj); return pathToInode(ctx, dest, obj);
} }
for (auto &f : *obj) { for (auto &f : *obj) {
auto &v = f->value; auto &v = f->value;
oxReturnError(transformFileAddresses(dest, &v)); oxReturnError(transformFileAddresses(ctx, dest, &v));
} }
return {}; return {};
} }
@ -80,7 +84,7 @@ static ox::Error doTransformations(foundation::Context *ctx, ox::TypeStore *ts,
} }
// transform FileAddresses // transform FileAddresses
oxRequireM(obj, foundation::readAsset(ts, buff)); oxRequireM(obj, foundation::readAsset(ts, buff));
oxReturnError(transformFileAddressesObj(dest, &obj)); oxReturnError(transformFileAddressesObj(ctx, 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()));