1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
|
#ifndef SLANG_FILE_SYSTEM_H_INCLUDED
#define SLANG_FILE_SYSTEM_H_INCLUDED
#include "../../slang.h"
#include "../../slang-com-helper.h"
#include "../../slang-com-ptr.h"
#include "../core/slang-blob.h"
#include "../core/slang-string-util.h"
#include "../core/slang-dictionary.h"
namespace Slang
{
enum class FileSystemStyle
{
Load, ///< Equivalent to ISlangFileSystem
Ext, ///< Equivalent to ISlangFileSystemExt
Mutable, ///< Equivalent to ISlangModifyableFileSystem
};
// Can be used for all styles of file system
class OSFileSystem : public ISlangMutableFileSystem
{
public:
// ISlangUnknown
// override ref counting, as DefaultFileSystem is singleton
SLANG_IUNKNOWN_QUERY_INTERFACE
SLANG_NO_THROW uint32_t SLANG_MCALL addRef() SLANG_OVERRIDE { return 1; }
SLANG_NO_THROW uint32_t SLANG_MCALL release() SLANG_OVERRIDE { return 1; }
// ISlangFileSystem
virtual SLANG_NO_THROW SlangResult SLANG_MCALL loadFile(char const* path, ISlangBlob** outBlob) SLANG_OVERRIDE;
// ISlangFileSystemExt
virtual SLANG_NO_THROW SlangResult SLANG_MCALL getFileUniqueIdentity(const char* path, ISlangBlob** uniqueIdentityOut) SLANG_OVERRIDE;
virtual SLANG_NO_THROW SlangResult SLANG_MCALL calcCombinedPath(SlangPathType fromPathType, const char* fromPath, const char* path, ISlangBlob** pathOut) SLANG_OVERRIDE;
virtual SLANG_NO_THROW SlangResult SLANG_MCALL getPathType(const char* path, SlangPathType* pathTypeOut) SLANG_OVERRIDE;
virtual SLANG_NO_THROW SlangResult SLANG_MCALL getSimplifiedPath(const char* path, ISlangBlob** outSimplifiedPath) SLANG_OVERRIDE;
virtual SLANG_NO_THROW SlangResult SLANG_MCALL getCanonicalPath(const char* path, ISlangBlob** outCanonicalPath) SLANG_OVERRIDE;
virtual SLANG_NO_THROW void SLANG_MCALL clearCache() SLANG_OVERRIDE {}
virtual SLANG_NO_THROW SlangResult SLANG_MCALL enumeratePathContents(const char* path, FileSystemContentsCallBack callback, void* userData) SLANG_OVERRIDE;
// ISlangModifyableFileSystem
virtual SLANG_NO_THROW SlangResult SLANG_MCALL saveFile(const char* path, const void* data, size_t size) SLANG_OVERRIDE;
virtual SLANG_NO_THROW SlangResult SLANG_MCALL remove(const char* path) SLANG_OVERRIDE;
virtual SLANG_NO_THROW SlangResult SLANG_MCALL createDirectory(const char* path) SLANG_OVERRIDE;
/// Get a default instance
static ISlangFileSystem* getLoadSingleton() { return &g_load; }
static ISlangFileSystemExt* getExtSingleton() { return &g_ext; }
static ISlangMutableFileSystem* getMutableSingleton() { return &g_mutable; }
private:
/// Make so not constructible
OSFileSystem(FileSystemStyle style):
m_style(style)
{}
virtual ~OSFileSystem() {}
ISlangUnknown* getInterface(const Guid& guid);
FileSystemStyle m_style;
static OSFileSystem g_load;
static OSFileSystem g_ext;
static OSFileSystem g_mutable;
};
/* Wraps an underlying ISlangFileSystem or ISlangFileSystemExt and provides caching,
as well as emulation of methods if only has ISlangFileSystem interface. Will query capabilities
of the interface on the constructor.
NOTE! That this behavior is the same as previously in that....
1) calcRelativePath, just returns the path as processed by the Path:: methods
2) getUniqueIdentity behavior depends on the UniqueIdentityMode.
*/
class CacheFileSystem: public ISlangFileSystemExt, public RefObject
{
public:
SLANG_CLASS_GUID(0x2f4d1d03, 0xa0d1, 0x434b, { 0x87, 0x7a, 0x65, 0x5, 0xa4, 0xa0, 0x9a, 0x3b })
enum class PathStyle
{
Default, ///< Pass to say use the default
Simplifiable, ///< It can be simplified by Path::Simplify
FileSystemExt, ///< Use file system
};
enum UniqueIdentityMode
{
Default, ///< If passed, will default to the others depending on what kind of ISlangFileSystem is passed in
Path, ///< Just use the path as is (old style slang behavior)
SimplifyPath, ///< Use the input path 'simplified' (ie removing . and .. aspects)
Hash, ///< Use hashing
SimplifyPathAndHash, ///< Tries simplifying path first, and if that doesn't work it hashes
FileSystemExt, ///< Use the file system extended interface.
};
/* Cannot change order/add members without changing s_compressedResultToResult */
enum class CompressedResult: uint8_t
{
Uninitialized, ///< Holds no value
Ok, ///< Ok
NotFound, ///< File not found
CannotOpen, ///< Cannot open
Fail, ///< Generic failure
CountOf,
};
struct PathInfo
{
PathInfo(const String& uniqueIdentity)
{
m_uniqueIdentity = new StringBlob(uniqueIdentity);
m_loadFileResult = CompressedResult::Uninitialized;
m_getPathTypeResult = CompressedResult::Uninitialized;
m_getCanonicalPathResult = CompressedResult::Uninitialized;
m_pathType = SLANG_PATH_TYPE_FILE;
}
/// Get the unique identity path as a string
const String& getUniqueIdentity() const { SLANG_ASSERT(m_uniqueIdentity); return m_uniqueIdentity->getString(); }
RefPtr<StringBlob> m_uniqueIdentity;
CompressedResult m_loadFileResult;
CompressedResult m_getPathTypeResult;
CompressedResult m_getCanonicalPathResult;
SlangPathType m_pathType;
ComPtr<ISlangBlob> m_fileBlob;
RefPtr<StringBlob> m_canonicalPath;
};
Dictionary<String, PathInfo*>& getPathMap() { return m_pathMap; }
Dictionary<String, PathInfo*>& getUniqueMap() { return m_uniqueIdentityMap; }
// ISlangUnknown
SLANG_NO_THROW SlangResult SLANG_MCALL queryInterface(SlangUUID const& uuid, void** outObject) SLANG_OVERRIDE;
SLANG_REF_OBJECT_IUNKNOWN_ADD_REF
SLANG_REF_OBJECT_IUNKNOWN_RELEASE
// ISlangFileSystem
virtual SLANG_NO_THROW SlangResult SLANG_MCALL loadFile(char const* path, ISlangBlob** outBlob) SLANG_OVERRIDE;
// ISlangFileSystemExt
virtual SLANG_NO_THROW SlangResult SLANG_MCALL getFileUniqueIdentity(const char* path, ISlangBlob** outUniqueIdentity) SLANG_OVERRIDE;
virtual SLANG_NO_THROW SlangResult SLANG_MCALL calcCombinedPath(SlangPathType fromPathType, const char* fromPath, const char* path, ISlangBlob** pathOut) SLANG_OVERRIDE;
virtual SLANG_NO_THROW SlangResult SLANG_MCALL getPathType(const char* path, SlangPathType* outPathType) SLANG_OVERRIDE;
virtual SLANG_NO_THROW SlangResult SLANG_MCALL getSimplifiedPath(const char* path, ISlangBlob** outSimplifiedPath) SLANG_OVERRIDE;
virtual SLANG_NO_THROW SlangResult SLANG_MCALL getCanonicalPath(const char* path, ISlangBlob** outCanonicalPath) SLANG_OVERRIDE;
virtual SLANG_NO_THROW void SLANG_MCALL clearCache() SLANG_OVERRIDE;
virtual SLANG_NO_THROW SlangResult SLANG_MCALL enumeratePathContents(const char* path, FileSystemContentsCallBack callback, void* userData) SLANG_OVERRIDE;
/// Get the unique identity mode
UniqueIdentityMode getUniqueIdentityMode() const { return m_uniqueIdentityMode; }
/// Get the path style
PathStyle getPathStyle() const { return m_pathStyle; }
/// Set the inner file system
void setInnerFileSystem(ISlangFileSystem* fileSystem, UniqueIdentityMode uniqueIdentityMode = UniqueIdentityMode::Default, PathStyle pathStyle = PathStyle::Default);
/// Ctor
CacheFileSystem(ISlangFileSystem* fileSystem, UniqueIdentityMode uniqueIdentityMode = UniqueIdentityMode::Default, PathStyle pathStyle = PathStyle::Default);
/// Dtor
virtual ~CacheFileSystem();
static CompressedResult toCompressedResult(Result res);
static Result toResult(CompressedResult compRes) { return s_compressedResultToResult[int(compRes)]; }
static const Result s_compressedResultToResult[int(CompressedResult::CountOf)];
protected:
/// Given a path, works out a uniqueIdentity, based on the uniqueIdentityMode.
/// outFileContents will be set if file had to be read to produce the uniqueIdentity (ie with Hash)
/// If the file doesn't have to be read, then outFileContents will be nullptr, even if it is backed by a file.
SlangResult _calcUniqueIdentity(const String& path, String& outUniqueIdentity, ComPtr<ISlangBlob>& outFileContents);
/// For a given path gets a PathInfo. Can return nullptr, if it is not possible to create the PathInfo for some reason
PathInfo* _resolvePathCacheInfo(const String& path);
/// Turns the path into a uniqueIdentity, and then tries to look up in the uniqueIdentityMap.
PathInfo* _resolveUniqueIdentityCacheInfo(const String& path);
/// Will simplify the path (if possible) to lookup on the pathCache else will create on uniqueIdentityMap
PathInfo* _resolveSimplifiedPathCacheInfo(const String& path);
SlangResult _getPathType(PathInfo* pathInfo, const char* inPath, SlangPathType* pathTypeOut);
/* TODO: This may be improved by mapping to a ISlangBlob. This makes output fast and easy, and if constructed
as a StringBlob, we can just static_cast to get as a string to use internally, instead of constantly converting.
It is probably the case we cannot do dynamic_cast on ISlangBlob if we don't know where constructed -> if outside of slang codebase
doing such a cast can cause an exception. So we *never* want to do dynamic cast from blobs which could be created by external code. */
Dictionary<String, PathInfo*> m_pathMap; ///< Maps a path to a PathInfo (and unique identity)
Dictionary<String, PathInfo*> m_uniqueIdentityMap; ///< Maps a unique identity for a file to its contents. This OWNs the PathInfo.
UniqueIdentityMode m_uniqueIdentityMode; ///< Determines how the 'uniqueIdentity' is produced. Cannot be Default in usage.
PathStyle m_pathStyle; ///< Style of paths
ComPtr<ISlangFileSystem> m_fileSystem; ///< Must always be set
ComPtr<ISlangFileSystemExt> m_fileSystemExt; ///< Optionally set -> if nullptr will fall back on the m_fileSystem and emulate all the other methods of ISlangFileSystemExt
};
class RelativeFileSystem : public ISlangMutableFileSystem, public RefObject
{
public:
SLANG_REF_OBJECT_IUNKNOWN_ALL
// ISlangFileSystem
virtual SLANG_NO_THROW SlangResult SLANG_MCALL loadFile(char const* path, ISlangBlob** outBlob) SLANG_OVERRIDE;
// ISlangFileSystemExt
virtual SLANG_NO_THROW SlangResult SLANG_MCALL getFileUniqueIdentity(const char* path, ISlangBlob** outUniqueIdentity) SLANG_OVERRIDE;
virtual SLANG_NO_THROW SlangResult SLANG_MCALL calcCombinedPath(SlangPathType fromPathType, const char* fromPath, const char* path, ISlangBlob** pathOut) SLANG_OVERRIDE;
virtual SLANG_NO_THROW SlangResult SLANG_MCALL getPathType(const char* path, SlangPathType* outPathType) SLANG_OVERRIDE;
virtual SLANG_NO_THROW SlangResult SLANG_MCALL getSimplifiedPath(const char* path, ISlangBlob** outSimplifiedPath) SLANG_OVERRIDE;
virtual SLANG_NO_THROW SlangResult SLANG_MCALL getCanonicalPath(const char* path, ISlangBlob** outCanonicalPath) SLANG_OVERRIDE;
virtual SLANG_NO_THROW void SLANG_MCALL clearCache() SLANG_OVERRIDE;
virtual SLANG_NO_THROW SlangResult SLANG_MCALL enumeratePathContents(const char* path, FileSystemContentsCallBack callback, void* userData) SLANG_OVERRIDE;
// ISlangModifyableFileSystem
virtual SLANG_NO_THROW SlangResult SLANG_MCALL saveFile(const char* path, const void* data, size_t size) SLANG_OVERRIDE;
virtual SLANG_NO_THROW SlangResult SLANG_MCALL remove(const char* path) SLANG_OVERRIDE;
virtual SLANG_NO_THROW SlangResult SLANG_MCALL createDirectory(const char* path) SLANG_OVERRIDE;
RelativeFileSystem(ISlangFileSystem* fileSystem, const String& relativePath, bool stripPath = false);
protected:
ISlangFileSystemExt* _getExt() { return Index(m_style) >= Index(FileSystemStyle::Ext) ? reinterpret_cast<ISlangFileSystemExt*>(m_fileSystem.get()) : nullptr; }
ISlangMutableFileSystem* _getMutable() { return Index(m_style) >= Index(FileSystemStyle::Mutable) ? reinterpret_cast<ISlangMutableFileSystem*>(m_fileSystem.get()) : nullptr; }
SlangResult _calcCombinedPathInner(SlangPathType fromPathType, const char* fromPath, const char* path, ISlangBlob** pathOut);
SlangResult _getFixedPath(const char* path, String& outPath);
ISlangUnknown* getInterface(const Guid& guid);
bool m_stripPath;
FileSystemStyle m_style;
ComPtr<ISlangFileSystem> m_fileSystem; ///< NOTE! Has to match what's in style, such style can be reached via reinterpret_cast
String m_relativePath;
};
}
#endif // SLANG_FILE_SYSTEM_H_INCLUDED
|