[keel,studio] Add ability to rename files

This commit is contained in:
2025-01-25 22:59:01 -06:00
parent f7a7a66a6a
commit cfa91d3d39
14 changed files with 227 additions and 29 deletions

View File

@ -9,7 +9,6 @@
#endif
#include <ox/event/signal.hpp>
#include <ox/fs/fs.hpp>
#include <ox/model/typenamecatcher.hpp>
#include <ox/std/hashmap.hpp>
#include <ox/std/utility.hpp>
@ -190,6 +189,8 @@ class AssetManager {
public:
~AssetTypeManagerBase() override = default;
virtual ox::Error updateAssetId(ox::StringViewCR oldId, ox::StringViewCR newId) noexcept = 0;
virtual void gc() noexcept = 0;
};
@ -202,7 +203,7 @@ class AssetManager {
ox::HashMap<ox::String, ox::UPtr<AssetContainer<T>>> m_cache;
public:
AssetTypeManager(Loader &&loader) noexcept: m_loader(std::move(loader)) {}
explicit AssetTypeManager(Loader &&loader) noexcept: m_loader(std::move(loader)) {}
ox::Result<AssetRef<T>> getAsset(ox::StringViewCR assetId) const noexcept {
OX_REQUIRE(out, m_cache.at(assetId));
@ -236,6 +237,14 @@ class AssetManager {
return {};
}
ox::Error updateAssetId(ox::StringViewCR oldId, ox::StringViewCR newId) noexcept final {
auto &o = m_cache[oldId];
auto &n = m_cache[newId];
n = std::move(o);
m_cache.erase(oldId);
return {};
}
void gc() noexcept final {
for (auto const&ack : m_cache.keys()) {
auto &ac = m_cache[ack];
@ -244,10 +253,11 @@ class AssetManager {
}
}
}
};
ox::HashMap<ox::String, ox::UPtr<AssetTypeManagerBase>> m_assetTypeManagers;
ox::HashMap<ox::String, ox::Signal<ox::Error(ox::StringViewCR assetId)>> m_fileUpdated;
ox::HashMap<ox::String, ox::UPtr<ox::Signal<ox::Error(ox::StringViewCR assetId)>>> m_fileUpdated;
template<typename T>
ox::Result<AssetTypeManager<T>*> getTypeManager() noexcept {
@ -279,7 +289,7 @@ class AssetManager {
}
ox::Error reloadAsset(ox::StringViewCR assetId) noexcept {
m_fileUpdated[assetId].emit(assetId);
m_fileUpdated[assetId]->emit(assetId);
return {};
}
@ -287,15 +297,40 @@ class AssetManager {
ox::Result<AssetRef<T>> loadAsset(ox::StringViewCR assetId) noexcept {
OX_REQUIRE(m, getTypeManager<T>());
OX_REQUIRE(out, m->loadAsset(assetId));
m_fileUpdated[assetId].connect(m, &AssetTypeManager<T>::reloadAsset);
if (!m_fileUpdated.contains(assetId)) [[unlikely]] {
m_fileUpdated[assetId] = ox::make_unique<ox::Signal<ox::Error(ox::StringViewCR assetId)>>();
}
m_fileUpdated[assetId]->connect(m, &AssetTypeManager<T>::reloadAsset);
return out;
}
ox::Error updateAssetId(ox::StringViewCR oldId, ox::StringViewCR newId) noexcept {
gc();
if (m_fileUpdated.contains(newId)) {
return ox::Error{1, "new asset ID already has an entry"};
}
auto &o = m_fileUpdated[oldId];
auto &n = m_fileUpdated[newId];
n = std::move(o);
m_fileUpdated.erase(oldId);
for (auto &k : m_assetTypeManagers.keys()) {
auto const &tm = m_assetTypeManagers[k];
std::ignore = tm->updateAssetId(oldId, newId);
}
return {};
}
void gc() noexcept {
for (auto const&amk : m_assetTypeManagers.keys()) {
auto &am = m_assetTypeManagers[amk];
am->gc();
}
for (auto const&k : m_fileUpdated.keys()) {
auto &s = m_fileUpdated[k];
if (s->connectionCnt() == 0) {
m_fileUpdated.erase(k);
}
}
}
};
#else

View File

@ -47,6 +47,8 @@ ox::Result<ox::CStringView> getPath(Context &ctx, ox::FileAddress const&fileAddr
ox::Result<ox::CStringView> getPath(Context &ctx, ox::CStringViewCR fileId) noexcept;
ox::Error updatePath(Context &ctx, ox::StringViewCR oldPath, ox::StringViewCR newPath) noexcept;
constexpr ox::Result<ox::UUID> uuidUrlToUuid(ox::StringViewCR uuidUrl) noexcept {
return ox::UUID::fromString(substr(uuidUrl, 7));
}

View File

@ -129,6 +129,21 @@ ox::Result<ox::CStringView> getPath(Context &ctx, ox::CStringViewCR fileId) noex
}
}
ox::Error updatePath(Context &ctx, ox::StringViewCR oldPath, ox::StringViewCR newPath) noexcept {
#ifndef OX_BARE_METAL
if (auto const r = ctx.pathToUuid.at(oldPath); r.ok()) {
auto const ustr = r.value->toString();
ctx.pathToUuid[newPath] = *r.value;
ctx.pathToUuid.erase(oldPath);
ctx.uuidToPath[ustr] = newPath;
return {};
}
return ctx.assetManager.updateAssetId(oldPath, newPath);
#else
return ox::Error(1, "updating path is not supported on this platform");
#endif
}
ox::Result<ox::CStringView> uuidUrlToPath(Context &ctx, ox::StringViewCR uuid) noexcept {
#ifndef OX_BARE_METAL
OX_REQUIRE_M(out, ctx.uuidToPath.at(substr(uuid, 7)));

View File

@ -39,10 +39,11 @@ static ox::Error pathToInode(
auto const uuid = ox::substr(path, 7);
OX_RETURN_ERROR(keel::uuidToPath(ctx, uuid).to<ox::String>().moveTo(path));
}
OX_REQUIRE(s, dest.stat(path));
auto const s = dest.stat(path);
auto const inode = s.ok() ? s.value.inode : 0;
OX_RETURN_ERROR(typeVal->set(static_cast<int8_t>(ox::FileAddressType::Inode)));
oxOutf("\tpath to inode: {} => {}\n", path, s.inode);
return data.set(2, s.inode);
oxOutf("\tpath to inode: {} => {}\n", path, inode);
return data.set(2, inode);
}
static ox::Error transformFileAddressesObj(