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
|
#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 relative path, just using Path:: string processing
static SlangResult _calcRelativePath(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::calcRelativePath(SlangPathType fromPathType, const char* fromPath, const char* path, ISlangBlob** pathOut)
{
return _calcRelativePath(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, bool useSimplifyForCanonicalPath) :
m_fileSystem(fileSystem),
m_useSimplifyForCanonicalPath(useSimplifyForCanonicalPath)
{
// Try to get the more sophisticated interface
fileSystem->queryInterface(IID_ISlangFileSystemExt, (void**)m_fileSystemExt.writeRef());
}
CacheFileSystem::~CacheFileSystem()
{
for (const auto& pair : m_canonicalMap)
{
delete pair.Value;
}
}
CacheFileSystem::PathInfo* CacheFileSystem::_getPathInfo(const String& relPath)
{
PathInfo** infoPtr = m_pathMap.TryGetValue(relPath);
if (infoPtr)
{
return *infoPtr;
}
String canonicalPath;
if (m_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
canonicalPath = StringUtil::getString(canonicalBlob);
}
else
{
canonicalPath = m_useSimplifyForCanonicalPath ? Path::Simplify(relPath.getUnownedSlice()) : relPath;
}
PathInfo* pathInfo;
infoPtr = m_canonicalMap.TryGetValue(canonicalPath);
if (infoPtr)
{
pathInfo = *infoPtr;
}
else
{
// Create and add to canonical path
pathInfo = new PathInfo(canonicalPath);
m_canonicalMap.Add(canonicalPath, pathInfo);
}
// 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::calcRelativePath(SlangPathType fromPathType, const char* fromPath, const char* path, ISlangBlob** pathOut)
{
// Just defer to contained implementation
if (m_fileSystemExt)
{
return m_fileSystemExt->calcRelativePath(fromPathType, fromPath, path, pathOut);
}
else
{
// Just use the default implementation
return _calcRelativePath(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);
}
}
|