Debian Codegraph @

include/ifilesystem.h

raw
1 #pragma once
2
3 /**
4 * \defgroup vfs Virtual filesystem
5 * \file ifilesystem.h
6 * Interface types for the VFS module.
7 */
8
9 #include <cstddef>
10 #include <string>
11 #include <list>
12 #include <set>
13 #include <functional>
14 #include <algorithm>
15 #include <sigc++/signal.h>
16
17 #include "imodule.h"
18 #include "iarchive.h"
19
20 namespace vfs
21 {
22
23 // Extension of std::list to check for existing paths before inserting new ones
24 class SearchPaths :
25 public std::list<std::string>
26 {
27 public:
28 bool insertIfNotExists(const std::string& path)
29 {
30 if (std::find(begin(), end(), path) != end())
31 {
32 return false;
33 }
34
35 push_back(path);
36 return true;
37 }
38 };
39
40 /// Visibility of an asset in the mod installation
41 enum class Visibility
42 {
43 /// Standard visibility, shown in all relevant areas
44 NORMAL,
45
46 /// Hidden from selectors, but rendered as normal in the map itself
47 HIDDEN
48 };
49
50 inline std::ostream& operator<< (std::ostream& s, const Visibility& v)
51 {
52 if (v == Visibility::HIDDEN)
53 return s << "Visibility::HIDDEN";
54 else if (v == Visibility::NORMAL)
55 return s << "Visibility::NORMAL";
56 else
57 return s << "Visibility(invalid)";
58 }
59
60 /// Metadata about a file in the virtual filesystem
61 class FileInfo
62 {
63 private:
64 // Info provider to load additional info on demand, used by e.g. getSize()
65 IArchiveFileInfoProvider* _infoProvider;
66 public:
67 FileInfo() :
68 FileInfo(std::string(), std::string(), Visibility::HIDDEN)
69 {}
70
71 FileInfo(const std::string& topDir_, const std::string& name_, Visibility visibility_) :
72 _infoProvider(nullptr),
73 topDir(topDir_),
74 name(name_),
75 visibility(visibility_)
76 {}
77
78 FileInfo(const std::string& topDir_, const std::string& name_,
79 Visibility visibility_, IArchiveFileInfoProvider& infoProvider) :
80 FileInfo(topDir_, name_, visibility_)
81 {
82 _infoProvider = &infoProvider;
83 }
84
85 FileInfo(const FileInfo& other) = default;
86 FileInfo(FileInfo&& other) = default;
87 FileInfo& operator=(const FileInfo& other) = default;
88 FileInfo& operator=(FileInfo&& other) = default;
89
90 /// Top-level directory (if any), e.g. "def" or "models"
91 std::string topDir;
92
93 /// Name of the file, including intermediate directories under the topDir
94 std::string name;
95
96 /// Visibility of the file
97 Visibility visibility = Visibility::NORMAL;
98
99 bool isEmpty() const
100 {
101 return name.empty();
102 }
103
104 /// Return the full mod-relative path, including the containing directory
105 std::string fullPath() const
106 {
107 if (topDir.empty())
108 return name;
109 else
110 return topDir + (topDir.back() == '/' ? "" : "/") + name;
111 }
112
113 // See IArchiveFileInfoProvider::getFileSize
114 std::size_t getSize() const
115 {
116 return _infoProvider ? _infoProvider->getFileSize(fullPath()) : 0;
117 }
118
119 // See IArchiveFileInfoProvider::getIsPhysicalFile
120 bool getIsPhysicalFile() const
121 {
122 return _infoProvider ? _infoProvider->getIsPhysical(fullPath()) : false;
123 }
124
125 // See IArchiveFileInfoProvider::getArchivePath
126 std::string getArchivePath() const
127 {
128 return _infoProvider ? _infoProvider->getArchivePath(fullPath()) : "";
129 }
130
131 /// Equality comparison with another FileInfo
132 bool operator== (const FileInfo& rhs) const
133 {
134 return topDir == rhs.topDir && name == rhs.name
135 && visibility == rhs.visibility;
136 }
137 };
138
139 /**
140 * Main interface for the virtual filesystem.
141 *
142 * The virtual filesystem provides a unified view of the contents of Doom 3's
143 * base and mod subdirectories, including the contents of PK4 files. Assets can
144 * be retrieved using a single unique path, without needing to know whereabouts
145 * in the physical filesystem the asset is located.
146 *
147 * \ingroup vfs
148 */
149 class VirtualFileSystem :
150 public RegisterableModule
151 {
152 public:
153 virtual ~VirtualFileSystem() {}
154
155 // Functor taking the filename and visibility as argument. The filename is
156 // relative to the base path passed to the GlobalFileSystem().foreach*()
157 // method.
158 typedef std::function<void(const FileInfo&)> VisitorFunc;
159
160 // Initialises the filesystem using the given search order.
161 virtual void initialise(const SearchPaths& vfsSearchPaths, const std::set<std::string>& allowedArchiveExtensions) = 0;
162
163 // Returns true if the filesystem has already been initialised
164 virtual bool isInitialised() const = 0;
165
166 /// \brief Shuts down the filesystem.
167 virtual void shutdown() = 0;
168
169 // Returns the extension set this VFS instance has been initialised with
170 virtual const std::set<std::string>& getArchiveExtensions() const = 0;
171
172 // A signal that is emitted when the VFS has been initialised, i.e. the paths have been
173 // set, the archives/directories are known and can be traversed
174 virtual sigc::signal<void>& signal_Initialised() = 0;
175
176 // Returns the number of files in the VFS matching the given filename
177 virtual int getFileCount(const std::string& filename) = 0;
178
179 /// \brief Returns the file identified by \p filename opened in binary mode, or 0 if not found.
180 // greebo: Note: expects the filename to be normalised (forward slashes, trailing slash).
181 virtual ArchiveFilePtr openFile(const std::string& filename) = 0;
182
183 /// \brief Returns the file identified by \p filename opened in binary mode, or 0 if not found.
184 // This is a variant of openFile taking an absolute path as argument.
185 virtual ArchiveFilePtr openFileInAbsolutePath(const std::string& filename) = 0;
186
187 /// \brief Returns the file identified by \p filename opened in text mode, or 0 if not found.
188 virtual ArchiveTextFilePtr openTextFile(const std::string& filename) = 0;
189
190 /// \brief Returns the file identified by \p filename opened in text mode, or NULL if not found.
191 /// This is a variant of openTextFile taking an absolute path as argument.
192 virtual ArchiveTextFilePtr openTextFileInAbsolutePath(const std::string& filename) = 0;
193
194 // Opens an independent archive located in the given physical path.
195 // (This archive can be located somewhere outside the current VFS hierarchy.)
196 // Loading this archive won't have any effect on the VFS setup, it is opened stand-alone.
197 virtual IArchive::Ptr openArchiveInAbsolutePath(const std::string& pathToArchive) = 0;
198
199 /// \brief Calls the visitor function for each file under \p basedir matching \p extension.
200 /// Use "*" as \p extension to match all file extensions.
201 virtual void forEachFile(const std::string& basedir,
202 const std::string& extension,
203 const VisitorFunc& visitorFunc,
204 std::size_t depth = 1) = 0;
205
206 // Similar to forEachFile, this routine traverses an absolute path
207 // searching for files matching a certain extension and invoking
208 // the given visitor functor on each occurrence.
209 virtual void forEachFileInAbsolutePath(const std::string& path,
210 const std::string& extension,
211 const VisitorFunc& visitorFunc,
212 std::size_t depth = 1) = 0;
213
214 // Similar to forEachFile, this routine traverses an archive in the given path
215 // searching for files matching a certain extension and invoking
216 // the given visitor functor on each occurrence.
217 virtual void forEachFileInArchive(const std::string& absoluteArchivePath,
218 const std::string& extension,
219 const VisitorFunc& visitorFunc,
220 std::size_t depth = 1) = 0;
221
222 /// \brief Returns the absolute filename for a relative \p name, or "" if not found.
223 virtual std::string findFile(const std::string& name) = 0;
224
225 /// \brief Returns the filesystem root for an absolute \p name, or "" if not found.
226 /// This can be used to convert an absolute name to a relative name.
227 virtual std::string findRoot(const std::string& name) = 0;
228
229 // Returns the list of registered VFS paths, ordered by search priority
230 virtual const SearchPaths& getVfsSearchPaths() = 0;
231
232 // Gets the file info structure for the given VFS file.
233 // The info structure will be empty if the file was not located in the current VFS tree
234 virtual vfs::FileInfo getFileInfo(const std::string& vfsRelativePath) = 0;
235 };
236
237 }
238
239 constexpr const char* const MODULE_VIRTUALFILESYSTEM("VirtualFileSystem");
240
241 inline vfs::VirtualFileSystem& GlobalFileSystem()
242 {
243 static module::InstanceReference<vfs::VirtualFileSystem> _reference(MODULE_VIRTUALFILESYSTEM);
244 return _reference;
245 }