6 using System.Text.RegularExpressions;
8 using SiliconStudio.Core.Storage;
9 using SiliconStudio.Core.Serialization;
10 using SiliconStudio.Core.Serialization.Assets;
12 namespace SiliconStudio.Core.IO
25 this.assetIndexMap = assetIndexMap;
26 this.objectDatabase = objectDatabase;
31 get {
return assetIndexMap; }
36 get {
return objectDatabase; }
47 if (url.StartsWith(
"obj/"))
49 else if (!assetIndexMap.TryGetValue(url, out objectId))
50 throw new FileNotFoundException();
52 var result = objectDatabase.OpenStream(objectId, mode, access, share);
55 if (streamFlags ==
StreamFlags.Seekable && !result.CanSeek)
57 var buffer =
new byte[result.Length - result.Position];
58 result.Read(buffer, 0, buffer.Length);
59 return new DatabaseReadFileStream(objectId,
new MemoryStream(buffer), 0);
62 return new DatabaseReadFileStream(objectId, result, result.Position);
67 if (url.StartsWith(
"obj/"))
68 throw new NotSupportedException();
70 var stream = objectDatabase.CreateStream();
73 var result =
new DatabaseWriteFileStream(stream, stream.Position);
75 stream.Disposed += x =>
78 assetIndexMap[url] = x.CurrentHash;
84 throw new ArgumentException(
"mode");
89 url = Regex.Escape(url);
90 searchPattern = Regex.Escape(searchPattern).Replace(
@"\*",
"[^/]*").Replace(
@"\?",
"[^/]");
91 string recursivePattern = searchOption == VirtualSearchOption.AllDirectories ?
"(.*/)*" :
"/?";
92 var regex =
new Regex(
"^" + url + recursivePattern + searchPattern +
"$");
94 return assetIndexMap.SearchValues(x => regex.IsMatch(x.Key)).Select(x => x.Key).ToArray();
100 return assetIndexMap.TryGetValue(url, out objectId)
101 && objectDatabase.Exists(objectId);
107 if (!assetIndexMap.TryGetValue(url, out objectId))
108 throw new FileNotFoundException();
110 return objectDatabase.GetSize(objectId);
116 if (!assetIndexMap.TryGetValue(url, out objectId))
117 throw new FileNotFoundException();
119 return objectDatabase.GetFilePath(objectId);
131 var resolveProviderResult = VirtualFileSystem.ResolveProvider(url,
false);
133 if (provider == null)
135 objectId = ObjectId.Empty;
138 return provider.AssetIndexMap.TryGetValue(resolveProviderResult.Path, out objectId) ? provider : null;
143 protected DatabaseFileStream(
Stream internalStream,
long startPosition,
bool seekToBeginning =
true)
144 : base(internalStream, startPosition, seekToBeginning: seekToBeginning)
151 class DatabaseReadFileStream : DatabaseFileStream
154 public DatabaseReadFileStream(
ObjectId id,
Stream internalStream,
long startPosition)
155 : base(internalStream, startPosition,
false)
169 class DatabaseWriteFileStream : DatabaseFileStream
171 public DatabaseWriteFileStream(
Stream internalStream,
long startPosition)
172 : base(internalStream, startPosition,
false)
180 throw new NotSupportedException();
static DatabaseFileProvider ResolveObjectId(string url, out ObjectId objectId)
Resolves the given VFS URL into a ObjectId and its DatabaseFileProvider.
Abstract base class for IVirtualFileProvider.
VirtualFileShare
File share capabilities, equivalent of System.IO.FileShare.
override bool FileExists(string url)
Determines whether the specified path points to an existing file. The path.
Gives access to the object database.
override Stream OpenStream(string url, VirtualFileMode mode, VirtualFileAccess access, VirtualFileShare share=VirtualFileShare.Read, StreamFlags streamFlags=StreamFlags.None)
Opens a Stream from the specified path. The path.The mode.The access.The process sharing mode...
VirtualFileAccess
File access equivalent of System.IO.FileAccess.
VirtualFileMode
File mode equivalent of System.IO.FileMode.
A hash to uniquely identify data.
DatabaseFileProvider(IAssetIndexMap assetIndexMap, ObjectDatabase objectDatabase, string mountPoint=null)
override string GetAbsolutePath(string url)
Gets the absolute path for the specified local path from this provider. The path local to this instan...
static bool TryParse(string input, out ObjectId result)
Tries to parse an ObjectId from a string.
A multithreaded wrapper over a Stream, used by the VirtualFileSystem. It also allows restricted acces...
StreamFlags
Describes the different type of streams.
DatabaseFileProvider(ObjectDatabase objectDatabase, string mountPoint=null)
override long FileSize(string url)
Returns the size of the specified file in bytes The file or directory for which to obtain sizeA long ...
override string[] ListFiles(string url, string searchPattern, VirtualSearchOption searchOption)
Returns the list of files from the specified path. The path.The search pattern.The search option...