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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
|
#include "slang-file-system.h"
#include "../../slang-com-ptr.h"
#include "../core/slang-io.h"
#include "../core/slang-string-util.h"
#include "compiler.h"
namespace Slang
{
// Allocate static const storage for the various interface IDs that the Slang API needs to expose
static const Guid IID_ISlangUnknown = SLANG_UUID_ISlangUnknown;
static const Guid IID_ISlangFileSystem = SLANG_UUID_ISlangFileSystem;
static const Guid IID_ISlangFileSystemExt = SLANG_UUID_ISlangFileSystemExt;
// Cacluate a combined path, just using Path:: string processing
static SlangResult _calcCombinedPath(SlangPathType fromPathType, const char* fromPath, const char* path, ISlangBlob** pathOut)
{
String relPath;
switch (fromPathType)
{
case SLANG_PATH_TYPE_FILE:
{
relPath = Path::Combine(Path::GetDirectoryName(fromPath), path);
break;
}
case SLANG_PATH_TYPE_DIRECTORY:
{
relPath = Path::Combine(fromPath, path);
break;
}
}
*pathOut = StringUtil::createStringBlob(relPath).detach();
return SLANG_OK;
}
/* !!!!!!!!!!!!!!!!!!!!!!!!!! IncludeFileSystem !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
/* static */DefaultFileSystem DefaultFileSystem::s_singleton;
template <typename T>
static ISlangFileSystemExt* _getInterface(T* ptr, const Guid& guid)
{
return (guid == IID_ISlangUnknown || guid == IID_ISlangFileSystem || guid == IID_ISlangFileSystemExt) ? static_cast<ISlangFileSystemExt*>(ptr) : nullptr;
}
ISlangUnknown* DefaultFileSystem::getInterface(const Guid& guid)
{
return _getInterface(this, guid);
}
SlangResult DefaultFileSystem::getCanoncialPath(const char* path, ISlangBlob** canonicalPathOut)
{
String canonicalPath;
SLANG_RETURN_ON_FAIL(Path::GetCanonical(path, canonicalPath));
*canonicalPathOut = StringUtil::createStringBlob(canonicalPath).detach();
return SLANG_OK;
}
SlangResult DefaultFileSystem::calcCombinedPath(SlangPathType fromPathType, const char* fromPath, const char* path, ISlangBlob** pathOut)
{
return _calcCombinedPath(fromPathType, fromPath, path, pathOut);
}
SlangResult SLANG_MCALL DefaultFileSystem::getPathType(
const char* path,
SlangPathType* pathTypeOut)
{
return Path::GetPathType(path, pathTypeOut);
}
SlangResult DefaultFileSystem::loadFile(char const* path, ISlangBlob** outBlob)
{
// Default implementation that uses the `core` libraries facilities for talking to the OS filesystem.
//
// TODO: we might want to conditionally compile these in, so that
// a user could create a build of Slang that doesn't include any OS
// filesystem calls.
if (!File::Exists(path))
{
return SLANG_E_NOT_FOUND;
}
try
{
String sourceString = File::ReadAllText(path);
*outBlob = StringUtil::createStringBlob(sourceString).detach();
return SLANG_OK;
}
catch (...)
{
}
return SLANG_E_CANNOT_OPEN;
}
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! CacheFileSystem !!!!!!!!!!!!!!!!!!!!!!!!!!!
/* static */ const Result CacheFileSystem::s_compressedResultToResult[] =
{
SLANG_E_UNINITIALIZED,
SLANG_OK, ///< Ok
SLANG_E_NOT_FOUND, ///< File not found
SLANG_E_CANNOT_OPEN, ///< CannotOpen,
SLANG_FAIL, ///< Fail
};
/* static */CacheFileSystem::CompressedResult CacheFileSystem::toCompressedResult(Result res)
{
if (SLANG_SUCCEEDED(res))
{
return CompressedResult::Ok;
}
switch (res)
{
case SLANG_E_CANNOT_OPEN: return CompressedResult::CannotOpen;
case SLANG_E_NOT_FOUND: return CompressedResult::NotFound;
default: return CompressedResult::Fail;
}
}
ISlangUnknown* CacheFileSystem::getInterface(const Guid& guid)
{
return _getInterface(this, guid);
}
CacheFileSystem::CacheFileSystem(ISlangFileSystem* fileSystem, CanonicalMode canonicalMode) :
m_fileSystem(fileSystem),
m_canonicalMode(canonicalMode)
{
// Try to get the more sophisticated interface
fileSystem->queryInterface(IID_ISlangFileSystemExt, (void**)m_fileSystemExt.writeRef());
switch (canonicalMode)
{
case CanonicalMode::Default:
case CanonicalMode::FileSystemExt:
{
m_canonicalMode = m_fileSystemExt ? CanonicalMode::FileSystemExt : CanonicalMode::Hash;
break;
}
default: break;
}
// It can't be default
SLANG_ASSERT(m_canonicalMode != CanonicalMode::Default);
}
CacheFileSystem::~CacheFileSystem()
{
for (const auto& pair : m_canonicalMap)
{
delete pair.Value;
}
}
CacheFileSystem::PathInfo* CacheFileSystem::_getPathInfoFromCanonical(const String& canonicalPath)
{
// First see if we have it.. if not add it
PathInfo** infoPtr = m_canonicalMap.TryGetValue(canonicalPath);
if (infoPtr)
{
return *infoPtr;
}
else
{
// Create and add to canonical path
PathInfo* pathInfo = new PathInfo(canonicalPath);
m_canonicalMap.Add(canonicalPath, pathInfo);
return pathInfo;
}
}
CacheFileSystem::PathInfo* CacheFileSystem::_getPathInfo(const String& relPath)
{
PathInfo** infoPtr = m_pathMap.TryGetValue(relPath);
if (infoPtr)
{
return *infoPtr;
}
PathInfo* pathInfo = nullptr;
switch (m_canonicalMode)
{
case CanonicalMode::FileSystemExt:
{
// Try getting the canonical path
// Okay request from the underlying file system the canonical path
ComPtr<ISlangBlob> canonicalBlob;
if (SLANG_FAILED(m_fileSystemExt->getCanoncialPath(relPath.Buffer(), canonicalBlob.writeRef())))
{
// Write in result as being null ptr so not tried again
m_pathMap.Add(relPath, nullptr);
return nullptr;
}
// Get the path as a string
String canonicalPath = StringUtil::getString(canonicalBlob);
pathInfo = _getPathInfoFromCanonical(canonicalPath);
break;
}
case CanonicalMode::Path:
{
pathInfo = _getPathInfoFromCanonical(relPath);
break;
}
case CanonicalMode::SimplifiedPath:
{
pathInfo = _getPathInfoFromCanonical(Path::Simplify(relPath.getUnownedSlice()));
break;
}
case CanonicalMode::Hash:
{
// I can only see if this is the same file as already loaded by loading the file and doing a hash
ComPtr<ISlangBlob> fileBlob;
Result res = m_fileSystem->loadFile(relPath.Buffer(), fileBlob.writeRef());
if (SLANG_FAILED(res) || fileBlob == nullptr)
{
// Write in result as being null ptr so not tried again
m_pathMap.Add(relPath, nullptr);
return nullptr;
}
// Calculate the hash on the contents
const uint64_t hash = GetHashCode64((const char*)fileBlob->getBufferPointer(), fileBlob->getBufferSize());
String hashString = Path::GetFileName(relPath);
hashString = hashString.ToLower();
hashString.append(':');
// The canonical name is.. combination of name and hash
hashString.append(hash, 16);
// We'll use the 'hashString' as the canonical path
pathInfo = _getPathInfoFromCanonical(hashString);
// We have the contents, so store it on the PathInfo, along with the result
if (pathInfo->m_loadFileResult == CompressedResult::Uninitialized)
{
// Save the contents of the saved file
pathInfo->m_loadFileResult = toCompressedResult(res);
pathInfo->m_fileBlob = fileBlob;
}
break;
}
}
// Add the relPath
m_pathMap.Add(relPath, pathInfo);
return pathInfo;
}
SlangResult CacheFileSystem::loadFile(char const* pathIn, ISlangBlob** blobOut)
{
*blobOut = nullptr;
String path(pathIn);
PathInfo* info = _getPathInfo(path);
if (!info)
{
return SLANG_FAIL;
}
if (info->m_loadFileResult == CompressedResult::Uninitialized)
{
info->m_loadFileResult = toCompressedResult(m_fileSystem->loadFile(path.Buffer(), info->m_fileBlob.writeRef()));
}
*blobOut = info->m_fileBlob;
if (*blobOut)
{
(*blobOut)->addRef();
}
return toResult(info->m_loadFileResult);
}
SlangResult CacheFileSystem::getCanoncialPath(const char* path, ISlangBlob** canonicalPathOut)
{
PathInfo* info = _getPathInfo(path);
if (!info)
{
return SLANG_E_NOT_FOUND;
}
info->m_canonicalPath->addRef();
*canonicalPathOut = info->m_canonicalPath;
return SLANG_OK;
}
SlangResult CacheFileSystem::calcCombinedPath(SlangPathType fromPathType, const char* fromPath, const char* path, ISlangBlob** pathOut)
{
// Just defer to contained implementation
if (m_fileSystemExt)
{
return m_fileSystemExt->calcCombinedPath(fromPathType, fromPath, path, pathOut);
}
else
{
// Just use the default implementation
return _calcCombinedPath(fromPathType, fromPath, path, pathOut);
}
}
SlangResult CacheFileSystem::getPathType(const char* pathIn, SlangPathType* pathTypeOut)
{
String path(pathIn);
PathInfo* info = _getPathInfo(path);
if (!info)
{
return SLANG_E_NOT_FOUND;
}
if (info->m_getPathTypeResult == CompressedResult::Uninitialized)
{
if (m_fileSystemExt)
{
info->m_getPathTypeResult = toCompressedResult(m_fileSystemExt->getPathType(pathIn, &info->m_pathType));
}
else
{
// Okay try to load the file
if (info->m_loadFileResult == CompressedResult::Uninitialized)
{
info->m_loadFileResult = toCompressedResult(m_fileSystem->loadFile(path.Buffer(), info->m_fileBlob.writeRef()));
}
// Make the getPathResult the same as the load result
info->m_getPathTypeResult = info->m_loadFileResult;
// Just set to file... the result is what matters in this case
info->m_pathType = SLANG_PATH_TYPE_FILE;
}
}
*pathTypeOut = info->m_pathType;
return toResult(info->m_getPathTypeResult);
}
}
|