Skip to content

Commit bd74b8c

Browse files
fix braindead design of archives
1 parent e198a8e commit bd74b8c

File tree

3 files changed

+82
-192
lines changed

3 files changed

+82
-192
lines changed

include/nbl/system/IFileArchive.h

Lines changed: 18 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,8 @@ class IFileArchive : public core::IReferenceCounted
3232
//! An entry in a list of items, can be a folder or a file.
3333
struct SListEntry
3434
{
35-
#if 0
36-
//! The name of the file
37-
/** If this is a file or folder in the virtual filesystem and the archive
38-
was created with the ignoreCase flag then the file name will be lower case. */
39-
system::path name;
40-
#endif
41-
//! The name of the file including the path
42-
/** If this is a file or folder in the virtual filesystem and the archive was
43-
created with the ignoreDirs flag then it will be the same as Name. */
44-
system::path fullName;
35+
//! The name of the file including the path relative to archive root
36+
system::path pathRelativeToArchive;
4537

4638
//! The size of the file in bytes
4739
size_t size;
@@ -58,15 +50,15 @@ class IFileArchive : public core::IReferenceCounted
5850
E_ALLOCATOR_TYPE allocatorType;
5951

6052
//! The == operator is provided so that CFileList can slowly search the list!
61-
inline bool operator ==(const struct SListEntry& other) const
53+
inline bool operator==(const struct SListEntry& other) const
6254
{
63-
return fullName.string()==other.fullName.string();
55+
return pathRelativeToArchive.string()==other.pathRelativeToArchive.string();
6456
}
6557

6658
//! The < operator is provided so that CFileList can sort and quickly search the list.
6759
inline bool operator<(const struct SListEntry& other) const
6860
{
69-
return fullName<other.fullName;
61+
return pathRelativeToArchive<other.pathRelativeToArchive;
7062
}
7163
};
7264

@@ -76,57 +68,15 @@ class IFileArchive : public core::IReferenceCounted
7668
// List all files and directories in a specific dir of the archive
7769
core::SRange<const SListEntry> listAssets(const path& asset_path) const;
7870

79-
struct SOpenFileParams
80-
{
81-
path filename;
82-
path absolutePath;
83-
std::string_view password;
84-
};
85-
#if 0
86-
core::smart_refctd_ptr<IFile> readFile(const SOpenFileParams& params)
87-
{
88-
auto index = getIndexByPath(params.filename);
89-
if (index == -1) return nullptr;
90-
switch (this->listAssets(index))
91-
{
92-
case EAT_NULL:
93-
return getFile_impl<CNullAllocator>(params, index);
94-
break;
95-
case EAT_MALLOC:
96-
return getFile_impl<CPlainHeapAllocator>(params, index);
97-
break;
98-
case EAT_VIRTUAL_ALLOC:
99-
return getFile_impl<VirtualMemoryAllocator>(params, index);
100-
break;
101-
}
102-
assert(false);
103-
return nullptr;
104-
}
105-
virtual core::smart_refctd_ptr<IFile> readFile_impl(const SOpenFileParams& params) = 0;
106-
int32_t getIndexByPath(const system::path& p)
107-
{
108-
for (int i = 0; i < m_files.size(); ++i)
109-
{
110-
if (p == m_files[i].fullName) return i;
111-
}
112-
return -1;
113-
}
114-
E_ALLOCATOR_TYPE getFileType(uint32_t index)
115-
{
116-
return m_files[index].allocatorType;
117-
}
118-
119-
#endif
71+
//
72+
virtual core::smart_refctd_ptr<IFile> getFile(const path& pathRelativeToArchive, const std::string_view& password) = 0;
12073

12174
//
122-
IFile* asFile() { return m_file.get(); }
123-
const IFile* asFile() const { return m_file.get(); }
75+
const path& getDefaultAbsolutePath() const {return m_defaultAbsolutePath;}
12476

12577
protected:
126-
IFileArchive(core::smart_refctd_ptr<IFile>&& file, system::logger_opt_smart_ptr&& logger) :
127-
m_file(std::move(file)), m_logger(std::move(logger))
128-
{
129-
}
78+
IFileArchive(path&& _defaultAbsolutePath, system::logger_opt_smart_ptr&& logger) :
79+
m_defaultAbsolutePath(std::move(_defaultAbsolutePath)), m_logger(std::move(logger)) {}
13080
/*
13181
virtual void addItem(const system::path& fullPath, uint32_t offset, uint32_t size, E_ALLOCATOR_TYPE allocatorType, uint32_t id = 0)
13282
{
@@ -143,9 +93,16 @@ class IFileArchive : public core::IReferenceCounted
14393
m_items.insert(std::lower_bound(m_items.begin(), m_items.end(), entry), entry);
14494
}
14595
*/
96+
inline uint32_t getIndexFromPath(const system::path& pathRelativeToArchive) const
97+
{
98+
const IFileArchive::SListEntry itemToFind = { pathRelativeToArchive };
99+
return std::distance(m_items.begin(),std::lower_bound(m_items.begin(),m_items.end(),itemToFind));
100+
}
101+
102+
path m_defaultAbsolutePath;
146103
// files and directories
147104
core::vector<SListEntry> m_items;
148-
core::smart_refctd_ptr<IFile> m_file;
105+
//
149106
system::logger_opt_smart_ptr m_logger;
150107
};
151108

include/nbl/system/ISystem.h

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ class ISystem : public core::IReferenceCounted
156156
/*
157157
Recursively lists all files and directories in the directory.
158158
*/
159-
core::vector<system::path> listFilesInDirectory(const system::path& p) const;
159+
core::vector<system::path> listItemsInDirectory(const system::path& p) const;
160160

161161
// can only perform operations on non-virtual filesystem paths
162162
bool createDirectory(const system::path& p);
@@ -175,7 +175,12 @@ class ISystem : public core::IReferenceCounted
175175
bool copy(const system::path& from, const system::path& to);
176176

177177
//
178-
void createFile(future_t<core::smart_refctd_ptr<IFile>>& future, std::filesystem::path filename, const core::bitflag<IFileBase::E_CREATE_FLAGS> flags);
178+
void createFile(
179+
future_t<core::smart_refctd_ptr<IFile>>& future, // creation may happen on a dedicated thread, so its async
180+
std::filesystem::path filename, // absolute path within our virtual filesystem
181+
const core::bitflag<IFileBase::E_CREATE_FLAGS> flags, // access flags (IMPORTANT: files from most archives wont open with ECF_WRITE bit)
182+
const std::string_view& accessToken="" // usually password for archives, but should be SSH key for URL downloads
183+
);
179184

180185
// Create a IFileArchive from a IFile
181186
core::smart_refctd_ptr<IFileArchive> openFileArchive(core::smart_refctd_ptr<IFile>&& file, const std::string_view& password="");
@@ -193,8 +198,23 @@ class ISystem : public core::IReferenceCounted
193198
}
194199

195200
// After opening and archive, you must mount it if you want the global path lookup to work seamlessly.
196-
void mount(core::smart_refctd_ptr<IFileArchive>&& archive, const system::path& pathAlias="");
197-
void unmount(const IFileArchive* archive, const system::path& pathAlias="");
201+
inline void mount(core::smart_refctd_ptr<IFileArchive>&& archive, const system::path& pathAlias="")
202+
{
203+
if (pathAlias.empty())
204+
m_cachedArchiveFiles.insert(archive->getDefaultAbsolutePath(),std::move(archive));
205+
else
206+
m_cachedArchiveFiles.insert(pathAlias,std::move(archive));
207+
}
208+
209+
//
210+
inline void unmount(const IFileArchive* archive, const system::path& pathAlias = "")
211+
{
212+
auto dummy = reinterpret_cast<const core::smart_refctd_ptr<IFileArchive>&>(archive);
213+
if (pathAlias.empty())
214+
m_cachedArchiveFiles.removeObject(dummy,archive->getDefaultAbsolutePath());
215+
else
216+
m_cachedArchiveFiles.removeObject(dummy,pathAlias);
217+
}
198218

199219
//
200220
struct SystemInfo
@@ -243,11 +263,13 @@ class ISystem : public core::IReferenceCounted
243263
//
244264
core::smart_refctd_ptr<IFile> impl_loadEmbeddedBuiltinData(const std::string& builtinPath, const std::pair<const uint8_t*,size_t>& found) const;
245265

246-
// given an absolute `path` find the archive it belongs to
247-
std::pair<IFileArchive*,IFileArchive::SOpenFileParams> findFileInArchive(const system::path& _path) const;
248-
249-
//
250-
core::smart_refctd_ptr<IFile> getFileFromArchive(const system::path& path);
266+
// given an `absolutePath` find the archive it belongs to
267+
struct FoundArchiveFile
268+
{
269+
IFileArchive* archive;
270+
path pathRelativeToArchive;
271+
};
272+
FoundArchiveFile findFileInArchive(const system::path& absolutePath) const;
251273

252274

253275
//

0 commit comments

Comments
 (0)