Compare commits

..

7 Commits

Author SHA1 Message Date
27b38ed250 [keel,studio] Fix hotloading for files that get loaded as multiple types
All checks were successful
Build / build (push) Successful in 2m33s
2024-05-27 19:26:58 -05:00
2bb7c51425 [studio/modlib] Fix type desc writing logic inversion
Also, provide foundation for making file types configurable for default Claw format.
2024-05-27 00:53:24 -05:00
5177cfb0e3 [studio/modlib] Make Project::mkdir only mkdir if dir does not exist 2024-05-27 00:51:00 -05:00
f9a14433d1 [studio/modlib] Add variant of ComboBox that takes callback 2024-05-27 00:46:55 -05:00
e62426b085 [keel] Ensure consistent asset IDs in AssetManager 2024-05-27 00:45:41 -05:00
af634bd4e5 [ox/fs] Add FileSystem::exists 2024-05-27 00:44:58 -05:00
49b859ecf5 [studio/modlib] Give Selection constructors 2024-05-26 02:09:59 -05:00
9 changed files with 291 additions and 65 deletions

View File

@ -99,6 +99,21 @@ class FileSystem {
Result<FileStat> stat(const FileAddress &addr) const noexcept;
[[nodiscard]]
inline bool exists(uint64_t inode) const noexcept {
return statInode(inode).ok();
}
[[nodiscard]]
inline bool exists(ox::StringView path) const noexcept {
return statPath(path).ok();
}
[[nodiscard]]
inline bool exists(FileAddress const&addr) const noexcept {
return stat(addr).ok();
}
[[nodiscard]]
virtual uint64_t spaceNeeded(uint64_t size) const noexcept = 0;

View File

@ -4,6 +4,10 @@
#pragma once
#ifndef OX_BARE_METAL
#include <functional>
#endif
#include <ox/event/signal.hpp>
#include <ox/fs/fs.hpp>
#include <ox/model/typenamecatcher.hpp>
@ -178,9 +182,11 @@ constexpr AssetRef<T>::AssetRef(AssetRef &&h) noexcept: m_ctr(h.m_ctr) {
h.m_ctr = nullptr;
}
class Context;
class AssetManager {
private:
class AssetTypeManagerBase {
class AssetTypeManagerBase: public ox::SignalHandler {
public:
virtual ~AssetTypeManagerBase() = default;
@ -189,17 +195,24 @@ class AssetManager {
template<typename T>
class AssetTypeManager: public AssetTypeManagerBase {
public:
using Loader = std::function<ox::Result<T>(ox::StringView assetId)>;
private:
Loader m_loader{};
ox::HashMap<ox::String, ox::UPtr<AssetContainer<T>>> m_cache;
public:
ox::Result<AssetRef<T>> getAsset(ox::StringView const&assetId) const noexcept {
auto out = m_cache.at(assetId);
oxReturnError(out);
return AssetRef<T>(out.value->get());
AssetTypeManager(Loader loader) noexcept: m_loader(loader) {}
ox::Result<AssetRef<T>> getAsset(ox::StringView const assetId) const noexcept {
oxRequire(out, m_cache.at(assetId));
if (!out || !*out) {
return OxError(1, "asset is null");
}
return AssetRef<T>(out->get());
}
ox::Result<AssetRef<T>> setAsset(ox::StringView const&assetId, T const&obj) noexcept {
ox::Result<AssetRef<T>> setAsset(ox::StringView const assetId, T const&obj) noexcept {
auto &p = m_cache[assetId];
if (!p) {
p = ox::make_unique<AssetContainer<T>>(obj);
@ -210,7 +223,7 @@ class AssetManager {
return AssetRef<T>(p.get());
}
ox::Result<AssetRef<T>> setAsset(ox::StringView const&assetId, T &&obj) noexcept {
ox::Result<AssetRef<T>> setAsset(ox::StringView const assetId, T &&obj) noexcept {
auto &p = m_cache[assetId];
if (!p) {
p = ox::make_unique<AssetContainer<T>>(obj);
@ -221,6 +234,30 @@ class AssetManager {
return AssetRef<T>(p.get());
}
ox::Result<AssetRef<T>> loadAsset(ox::StringView const assetId) noexcept {
auto &p = m_cache[assetId];
oxRequireM(obj, m_loader(assetId));
if (!p) {
p = ox::make_unique<AssetContainer<T>>(std::move(obj));
} else {
p->set(std::move(obj));
p->updated.emit();
}
return AssetRef<T>(p.get());
}
ox::Error reloadAsset(ox::StringView const assetId) noexcept {
auto &p = m_cache[assetId];
oxRequireM(obj, m_loader(assetId));
if (!p) {
p = ox::make_unique<AssetContainer<T>>(std::move(obj));
} else {
p->set(std::move(obj));
p->updated.emit();
}
return {};
}
void gc() noexcept final {
for (auto const&ack : m_cache.keys()) {
auto &ac = m_cache[ack];
@ -232,29 +269,58 @@ class AssetManager {
};
ox::HashMap<ox::String, ox::UPtr<AssetTypeManagerBase>> m_assetTypeManagers;
struct FileTracker {
ox::Signal<ox::Error(ox::StringView assetId)> updated;
};
ox::HashMap<ox::String, FileTracker> m_fileTrackers;
template<typename T>
AssetTypeManager<T> *getTypeManager() noexcept {
constexpr auto typeName = ox::requireModelTypeName<T>();
static_assert(ox::StringView(typeName) != "", "Types must have TypeName to use AssetManager");
auto &am = m_assetTypeManagers[typeName];
if (!am) {
am = ox::make_unique<AssetTypeManager<T>>();
ox::Result<AssetTypeManager<T>*> getTypeManager() noexcept {
constexpr auto &typeId = ox::ModelTypeId_v<T>;
static_assert(typeId != "", "Types must have TypeName to use AssetManager");
auto &am = m_assetTypeManagers[typeId];
auto const out = dynamic_cast<AssetTypeManager<T>*>(am.get());
if (!out) {
return OxError(1, "no AssetTypeManager for type");
}
return dynamic_cast<AssetTypeManager<T>*>(am.get());
return out;
}
public:
template<typename T>
ox::Result<AssetRef<T>> getAsset(ox::CRStringView assetId) noexcept {
auto m = getTypeManager<T>();
void initTypeManager(auto const&makeLoader, Context &ctx) noexcept {
constexpr auto &typeId = ox::ModelTypeId_v<T>;
static_assert(typeId != "", "Types must have TypeName to use AssetManager");
auto &am = m_assetTypeManagers[typeId];
if (!am) {
am = ox::make_unique<AssetTypeManager<T>>(makeLoader(ctx));
}
}
template<typename T>
ox::Result<AssetRef<T>> getAsset(ox::StringView assetId) noexcept {
oxRequire(m, getTypeManager<T>());
return m->getAsset(assetId);
}
template<typename T>
ox::Result<AssetRef<T>> setAsset(ox::CRStringView assetId, T const&obj) noexcept {
auto m = getTypeManager<T>();
return m->setAsset(assetId, obj);
ox::Result<AssetRef<T>> setAsset(ox::StringView assetId, T const&obj) noexcept {
oxRequire(m, getTypeManager<T>());
oxReturnError(m->setAsset(assetId, obj));
return {};
}
ox::Error reloadAsset(ox::StringView assetId) noexcept {
m_fileTrackers[assetId].updated.emit(assetId);
return {};
}
template<typename T>
ox::Result<AssetRef<T>> loadAsset(ox::StringView assetId) noexcept {
oxRequire(m, getTypeManager<T>());
oxRequire(out, m->loadAsset(assetId));
m_fileTrackers[assetId].updated.connect(m, &AssetTypeManager<T>::reloadAsset);
return out;
}
void gc() noexcept {

View File

@ -32,14 +32,43 @@ oxModelEnd()
ox::Result<std::size_t> getPreloadAddr(keel::Context &ctx, ox::FileAddress const&file) noexcept;
ox::Result<std::size_t> getPreloadAddr(keel::Context &ctx, ox::CRStringView file) noexcept;
void createUuidMapping(Context &ctx, ox::StringView filePath, ox::UUID const&uuid) noexcept;
ox::Error buildUuidMap(Context &ctx) noexcept;
ox::Result<ox::UUID> pathToUuid(Context &ctx, ox::CRStringView path) noexcept;
ox::Result<ox::UUID> getUuid(Context &ctx, ox::FileAddress const&fileAddr) noexcept;
ox::Result<ox::UUID> getUuid(Context &ctx, ox::StringView path) noexcept;
ox::Result<ox::CStringView> getPath(Context &ctx, ox::FileAddress const&fileAddr) noexcept;
ox::Result<ox::CStringView> getPath(Context &ctx, ox::CStringView fileId) noexcept;
constexpr ox::Result<ox::UUID> uuidUrlToUuid(ox::StringView uuidUrl) noexcept {
return ox::UUID::fromString(substr(uuidUrl, 7));
}
ox::Result<ox::CStringView> uuidUrlToPath(Context &ctx, ox::StringView uuid) noexcept;
ox::Result<ox::CStringView> uuidToPath(Context &ctx, ox::CRStringView uuid) noexcept;
ox::Result<ox::CStringView> uuidToPath(Context &ctx, ox::UUID const&uuid) noexcept;
ox::Error performPackTransforms(Context &ctx, ox::Buffer &clawData) noexcept;
#ifndef OX_BARE_METAL
namespace detail {
template<typename T>
ox::Result<keel::AssetRef<T>> readObjFile(
keel::Context &ctx,
ox::StringView assetId,
bool forceLoad) noexcept {
constexpr auto readConvert = [](Context &ctx, const ox::Buffer &buff) -> ox::Result<T> {
constexpr auto makeLoader(Context &ctx) {
return [&ctx](ox::StringView assetId) -> ox::Result<T> {
ox::StringView path;
oxRequire(p, ctx.uuidToPath.at(assetId));
path = *p;
oxRequire(buff, ctx.rom->read(path));
auto [obj, err] = readAsset<T>(buff);
if (err) {
if (err != ox::Error_ClawTypeVersionMismatch && err != ox::Error_ClawTypeMismatch) {
@ -49,25 +78,34 @@ ox::Result<keel::AssetRef<T>> readObjFile(
}
return std::move(obj);
};
ox::StringView path;
};
}
template<typename T>
ox::Result<keel::AssetRef<T>> readObjFile(
keel::Context &ctx,
ox::StringView assetId,
bool forceLoad) noexcept {
ox::UUIDStr uuidStr;
if (beginsWith(assetId, "uuid://")) {
assetId = substr(assetId, 7);
oxRequire(p, ctx.uuidToPath.at(assetId));
path = *p;
oxRequire(p, keel::uuidToPath(ctx, assetId));
} else {
path = assetId;
auto const [uuid, uuidErr] = getUuid(ctx, assetId);
if (!uuidErr) {
uuidStr = uuid.toString();
assetId = uuidStr;
}
}
if (forceLoad) {
oxRequire(buff, ctx.rom->read(path));
oxRequire(obj, readConvert(ctx, buff));
oxRequire(cached, ctx.assetManager.setAsset(assetId, obj));
ctx.assetManager.initTypeManager<T>(detail::makeLoader<T>, ctx);
oxRequire(cached, ctx.assetManager.loadAsset<T>(assetId));
return cached;
} else {
auto [cached, err] = ctx.assetManager.getAsset<T>(assetId);
if (err) {
oxRequire(buff, ctx.rom->read(path));
oxRequire(obj, readConvert(ctx, buff));
oxReturnError(ctx.assetManager.setAsset(assetId, obj).moveTo(cached));
ctx.assetManager.initTypeManager<T>(detail::makeLoader<T>, ctx);
oxReturnError(ctx.assetManager.loadAsset<T>(assetId).moveTo(cached));
}
return cached;
}
@ -89,37 +127,24 @@ ox::Result<keel::AssetRef<T>> readObjNoCache(
#endif
void createUuidMapping(Context &ctx, ox::StringView filePath, ox::UUID const&uuid) noexcept;
ox::Error buildUuidMap(Context &ctx) noexcept;
ox::Result<ox::UUID> pathToUuid(Context &ctx, ox::CRStringView path) noexcept;
constexpr ox::Result<ox::UUID> uuidUrlToUuid(ox::StringView uuidUrl) noexcept {
return ox::UUID::fromString(substr(uuidUrl, 7));
}
ox::Result<ox::CStringView> uuidUrlToPath(Context &ctx, ox::StringView uuid) noexcept;
ox::Result<ox::CStringView> uuidToPath(Context &ctx, ox::CRStringView uuid) noexcept;
ox::Result<ox::CStringView> uuidToPath(Context &ctx, ox::UUID const&uuid) noexcept;
ox::Error performPackTransforms(Context &ctx, ox::Buffer &clawData) noexcept;
ox::Error reloadAsset(keel::Context &ctx, ox::StringView assetId) noexcept;
template<typename T>
ox::Result<AssetRef<T>> setAsset(keel::Context &ctx, ox::StringView assetId, T const&asset) noexcept {
ox::Result<AssetRef<T>> setAsset(
keel::Context &ctx,
ox::StringView assetId,
T const&asset) noexcept {
#ifndef OX_BARE_METAL
if (assetId.len() == 0) {
return OxError(1, "Invalid asset ID");
}
ox::UUIDStr idStr;
ox::UUIDStr uuidStr;
if (assetId[0] == '/') {
auto const [id, err] = ctx.pathToUuid.at(assetId);
oxReturnError(err);
idStr = id->toString();
assetId = idStr;
oxRequire(id, ctx.pathToUuid.at(assetId));
uuidStr = id->toString();
assetId = uuidStr;
}
ctx.assetManager.initTypeManager<T>(detail::makeLoader<T>, ctx);
return ctx.assetManager.setAsset(assetId, asset);
#else
return OxError(1, "Not supported on this platform");

View File

@ -83,6 +83,49 @@ ox::Result<ox::UUID> pathToUuid(Context &ctx, ox::CRStringView path) noexcept {
#endif
}
ox::Result<ox::UUID> getUuid(Context &ctx, ox::FileAddress const&fileAddr) noexcept {
oxRequire(path, fileAddr.getPath());
return getUuid(ctx, path);
}
ox::Result<ox::UUID> getUuid(Context &ctx, ox::StringView path) noexcept {
if (beginsWith(path, "uuid://")) {
auto const uuid = substr(path, 7);
return ox::UUID::fromString(uuid);
} else {
return pathToUuid(ctx, path);
}
}
ox::Result<ox::CStringView> getPath(Context &ctx, ox::FileAddress const&fileAddr) noexcept {
oxRequire(path, fileAddr.getPath());
if (beginsWith(path, "uuid://")) {
auto const uuid = substr(path, 7);
#ifndef OX_BARE_METAL
oxRequireM(out, ctx.uuidToPath.at(uuid));
return ox::CStringView{*out};
#else
return OxError(1, "UUID to path conversion not supported on this platform");
#endif
} else {
return ox::CStringView{path};
}
}
ox::Result<ox::CStringView> getPath(Context &ctx, ox::CStringView fileId) noexcept {
if (beginsWith(fileId, "uuid://")) {
auto const uuid = substr(fileId, 7);
#ifndef OX_BARE_METAL
oxRequireM(out, ctx.uuidToPath.at(uuid));
return ox::CStringView{*out};
#else
return OxError(1, "UUID to path conversion not supported on this platform");
#endif
} else {
return ox::CStringView{fileId};
}
}
ox::Result<ox::CStringView> uuidUrlToPath(Context &ctx, ox::StringView uuid) noexcept {
uuid = substr(uuid, 7);
#ifndef OX_BARE_METAL
@ -123,6 +166,21 @@ ox::Error performPackTransforms(Context &ctx, ox::Buffer &clawData) noexcept {
return {};
}
ox::Error reloadAsset(keel::Context &ctx, ox::StringView assetId) noexcept {
ox::UUIDStr uuidStr;
if (beginsWith(assetId, "uuid://")) {
assetId = substr(assetId, 7);
oxRequire(p, keel::uuidToPath(ctx, assetId));
} else {
auto const [uuid, uuidErr] = getUuid(ctx, assetId);
if (!uuidErr) {
uuidStr = uuid.toString();
assetId = uuidStr;
}
}
return ctx.assetManager.reloadAsset(assetId);
}
}
#else
@ -176,6 +234,10 @@ ox::Result<std::size_t> getPreloadAddr(keel::Context &ctx, ox::FileAddress const
return static_cast<std::size_t>(p.preloadAddr) + ctx.preloadSectionOffset;
}
ox::Error reloadAsset(keel::Context&, ox::StringView) noexcept {
return OxError(1, "reloadAsset is unsupported on this platform");
}
}
#endif

View File

@ -148,6 +148,19 @@ bool BeginPopup(turbine::Context &ctx, ox::CStringView popupName, bool &show, Im
*/
bool ComboBox(ox::CStringView lbl, ox::SpanView<ox::String> list, size_t &selectedIdx) noexcept;
/**
*
* @param lbl
* @param callback
* @param selectedIdx
* @return true if new value selected, false otherwise
*/
bool ComboBox(
ox::CStringView lbl,
std::function<ox::CStringView(size_t)> const&f,
size_t strCnt,
size_t &selectedIdx) noexcept;
bool FileComboBox(
ox::CStringView lbl,
studio::StudioContext &sctx,

View File

@ -31,7 +31,7 @@ enum class ProjectEvent {
constexpr ox::Result<ox::StringView> fileExt(ox::CRStringView path) noexcept {
auto const extStart = ox::find(path.crbegin(), path.crend(), '.').offset();
if (!extStart) {
return OxError(1, "Cannot open a file without valid extension.");
return OxError(1, "file path does not have valid extension");
}
return substr(path, extStart + 1);
}
@ -47,6 +47,7 @@ constexpr ox::StringView parentDir(ox::StringView path) noexcept {
class Project {
private:
ox::SmallMap<ox::String, ox::Optional<ox::ClawFormat>> m_typeFmt;
keel::Context &m_ctx;
ox::String m_path;
ox::String m_projectDataDir;
@ -75,7 +76,15 @@ class Project {
ox::Error writeObj(
ox::CRStringView path,
T const&obj,
ox::ClawFormat fmt = ox::ClawFormat::Metal) noexcept;
ox::ClawFormat fmt) noexcept;
/**
* Writes a MetalClaw object to the project at the given path.
*/
template<typename T>
ox::Error writeObj(
ox::CRStringView path,
T const&obj) noexcept;
template<typename T>
ox::Result<T> loadObj(ox::CRStringView path) const noexcept;
@ -130,16 +139,24 @@ ox::Error Project::writeObj(ox::CRStringView path, T const&obj, ox::ClawFormat f
oxReturnError(ox::buildTypeDef(&m_typeStore, &obj));
}
oxRequire(desc, m_typeStore.get<T>());
auto const descExists = m_fs.stat(ox::sfmt("{}/{}", m_typeDescPath, buildTypeId(*desc))).error != 0;
auto const descPath = ox::sfmt("{}/{}", m_typeDescPath, buildTypeId(*desc));
auto const descExists = m_fs.exists(descPath);
if (!descExists) {
oxReturnError(writeTypeStore());
}
oxReturnError(keel::setAsset(m_ctx, path, obj));
oxReturnError(keel::reloadAsset(m_ctx, path));
oxRequire(uuid, pathToUuid(m_ctx, path));
fileUpdated.emit(path, uuid);
return {};
}
template<typename T>
ox::Error Project::writeObj(ox::CRStringView path, T const&obj) noexcept {
oxRequire(ext, fileExt(path));
auto const fmt = m_typeFmt[ext].or_value(ox::ClawFormat::Metal);
return writeObj(path, obj, fmt);
}
template<typename T>
ox::Result<T> Project::loadObj(ox::CRStringView path) const noexcept {
oxRequire(buff, loadBuff(path));

View File

@ -9,7 +9,11 @@
namespace studio {
struct Selection {ox::Point a, b;};
struct Selection {
ox::Point a, b;
constexpr Selection() noexcept = default;
constexpr Selection(ox::Point const&pA, ox::Point const&pB) noexcept: a(pA), b(pB) {}
};
constexpr auto iterateSelection(studio::Selection const&sel, auto const&cb) {
constexpr auto retErr = ox::is_same_v<decltype(cb(0, 0)), ox::Error>;

View File

@ -100,6 +100,26 @@ bool ComboBox(
return out;
}
bool ComboBox(
ox::CStringView lbl,
std::function<ox::CStringView(size_t)> const&f,
size_t strCnt,
size_t &selectedIdx) noexcept {
bool out{};
auto const first = selectedIdx < strCnt ? f(selectedIdx).c_str() : "";
if (ImGui::BeginCombo(lbl.c_str(), first, 0)) {
for (auto i = 0u; i < strCnt; ++i) {
const auto selected = (selectedIdx == i);
if (ImGui::Selectable(f(i).c_str(), selected) && selectedIdx != i) {
selectedIdx = i;
out = true;
}
}
ImGui::EndCombo();
}
return out;
}
bool FileComboBox(
ox::CStringView lbl,
studio::StudioContext &sctx,

View File

@ -56,9 +56,13 @@ ox::FileSystem &Project::romFs() noexcept {
}
ox::Error Project::mkdir(ox::CRStringView path) const noexcept {
oxReturnError(m_fs.mkdir(path, true));
fileUpdated.emit(path, {});
return {};
auto const [stat, err] = m_fs.stat(path);
if (err) {
oxReturnError(m_fs.mkdir(path, true));
fileUpdated.emit(path, {});
}
return stat.fileType == ox::FileType::Directory ?
ox::Error{} : OxError(1, "path exists as normal file");
}
ox::Result<ox::FileStat> Project::stat(ox::CRStringView path) const noexcept {