Skip to content

Commit cb5450a

Browse files
authored
Mounting directories (#492)
* Create CMountDirectoryArchive.h * Update CMountDirectoryArchive.h * changed variable name and func to get relative path
1 parent 8373b38 commit cb5450a

File tree

2 files changed

+65
-4
lines changed

2 files changed

+65
-4
lines changed

include/nbl/system/IFileArchive.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,10 @@ class IFileArchive : public core::IReferenceCounted
6464
};
6565

6666
//
67-
core::SRange<const SListEntry> listAssets() const {return {m_items.data(),m_items.data()+m_items.size()};}
67+
virtual core::SRange<const SListEntry> listAssets() const {return {m_items.data(),m_items.data()+m_items.size()};}
6868

6969
// List all files and directories in a specific dir of the archive
70-
core::SRange<const SListEntry> listAssets(const path& asset_path) const;
70+
virtual core::SRange<const SListEntry> listAssets(const path& asset_path) const;
7171

7272
//
7373
virtual core::smart_refctd_ptr<IFile> getFile(const path& pathRelativeToArchive, const std::string_view& password) = 0;
@@ -88,10 +88,10 @@ class IFileArchive : public core::IReferenceCounted
8888
return &(*found);
8989
}
9090

91-
std::mutex itemMutex; // TODO: update to readers writer lock
91+
mutable std::mutex itemMutex; // TODO: update to readers writer lock
9292
path m_defaultAbsolutePath;
9393
// files and directories
94-
core::vector<SListEntry> m_items;
94+
mutable core::vector<SListEntry> m_items;
9595
//
9696
system::logger_opt_smart_ptr m_logger;
9797
};
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#include "nbl/system/IFileArchive.h"
2+
3+
#include "nbl/system/IFile.h"
4+
5+
namespace nbl::system {
6+
7+
8+
9+
class CMountDirectoryArchive : public IFileArchive
10+
{
11+
ISystem* m_system;
12+
13+
public:
14+
inline CMountDirectoryArchive(path&& _defaultAbsolutePath, system::logger_opt_smart_ptr&& logger, ISystem* system) :
15+
IFileArchive(std::move(_defaultAbsolutePath), std::move(logger))
16+
{
17+
m_system = system;
18+
}
19+
20+
core::smart_refctd_ptr<IFile> getFile(const path& pathRelativeToArchive, const std::string_view& password) override
21+
{
22+
{
23+
//std::unique_lock(itemMutex); already inside `getItemFromPath`
24+
if (!getItemFromPath(pathRelativeToArchive))
25+
return nullptr;
26+
}
27+
system::ISystem::future_t<core::smart_refctd_ptr<system::IFile>> future;
28+
m_system->createFile(future, m_defaultAbsolutePath / pathRelativeToArchive, system::IFile::ECF_READ);
29+
if (auto file = future.acquire())
30+
return *file;
31+
}
32+
33+
core::SRange<const IFileArchive::SListEntry> listAssets(const path& asset_path) const override
34+
{
35+
populateItemList(asset_path);
36+
return IFileArchive::listAssets(asset_path);
37+
}
38+
virtual core::SRange<const SListEntry> listAssets() const override {
39+
populateItemList(path());
40+
return IFileArchive::listAssets();
41+
}
42+
43+
void populateItemList(const path& p) const {
44+
std::unique_lock lock(itemMutex);
45+
auto items = m_system->listItemsInDirectory(m_defaultAbsolutePath/p);
46+
m_items.clear();
47+
48+
for (auto item : items)
49+
{
50+
if (item.has_extension())
51+
{
52+
auto relpath = item.lexically_relative(m_defaultAbsolutePath);
53+
auto entry = SListEntry{ relpath, 0xdeadbeefu, 0xdeadbeefu, 0xdeadbeefu, EAT_NONE };
54+
m_items.push_back(entry);
55+
}
56+
}
57+
lock.unlock();
58+
}
59+
};
60+
61+
} //namespace nbl::system

0 commit comments

Comments
 (0)