Esempio n. 1
0
        /// <inheritdoc/>
        public virtual Stream OpenStream(ObjectId objectId, VirtualFileMode mode = VirtualFileMode.Open, VirtualFileAccess access = VirtualFileAccess.Read, VirtualFileShare share = VirtualFileShare.Read)
        {
            var url = BuildUrl(vfsRootUrl, objectId);

            // Try to early exit if file does not exists while opening, so that it doesn't
            // throw a file not found exception for default logic.
            if (!virtualFileProvider.FileExists(url))
            {
                if (mode == VirtualFileMode.Open || mode == VirtualFileMode.Truncate)
                {
                    return(null);
                }

                // Otherwise, file creation is allowed, so make sure directory exists
                virtualFileProvider.CreateDirectory(ExtractPath(url));
            }

            try
            {
                return(virtualFileProvider.OpenStream(url, mode, access, share));
            }
            catch (FileNotFoundException)
            {
                return(null);
            }
#if !SILICONSTUDIO_PLATFORM_WINDOWS_RUNTIME
            catch (DirectoryNotFoundException)
            {
                return(null);
            }
#endif
        }
Esempio n. 2
0
        internal void LoadSoundInMemory()
        {
            if (PreloadedBuffer.Ptr != IntPtr.Zero)
            {
                return;
            }

            using (var soundStream = FileProvider.OpenStream(CompressedDataUrl, VirtualFileMode.Open, VirtualFileAccess.Read, VirtualFileShare.Read, StreamFlags.Seekable))
                using (var decoder = new Celt(SampleRate, CompressedSoundSource.SamplesPerFrame, Channels, true))
                {
                    var reader           = new BinarySerializationReader(soundStream);
                    var samplesPerPacket = CompressedSoundSource.SamplesPerFrame * Channels;

                    PreloadedBuffer = AudioLayer.BufferCreate(samplesPerPacket * NumberOfPackets * sizeof(short));

                    var memory = new UnmanagedArray <short>(samplesPerPacket * NumberOfPackets);

                    var offset       = 0;
                    var outputBuffer = new short[samplesPerPacket];
                    for (var i = 0; i < NumberOfPackets; i++)
                    {
                        var len = reader.ReadInt16();
                        var compressedBuffer = reader.ReadBytes(len);
                        var samplesDecoded   = decoder.Decode(compressedBuffer, len, outputBuffer);
                        memory.Write(outputBuffer, offset, 0, samplesDecoded * Channels);
                        offset += samplesDecoded * Channels * sizeof(short);
                    }

                    // Ignore invalid data at beginning (due to encoder delay) & end of stream (due to packet size)
                    var samplesToSkip = decoder.GetDecoderSampleDelay();
                    AudioLayer.BufferFill(PreloadedBuffer, memory.Pointer + samplesToSkip * Channels * sizeof(short), Samples * Channels * sizeof(short), SampleRate, Channels == 1);
                    memory.Dispose();
                }
        }
Esempio n. 3
0
        /// <summary>
        /// Opens the stream from a given path.
        /// </summary>
        /// <param name="path">The path.</param>
        /// <param name="mode">The stream opening mode (append, open, create, etc...).</param>
        /// <param name="access">The stream access.</param>
        /// <param name="share">The stream share mode.</param>
        /// <param name="provider">The provider used to load the stream.</param>
        /// <returns>The stream.</returns>
        public static Stream OpenStream(string path, VirtualFileMode mode, VirtualFileAccess access, VirtualFileShare share, out IVirtualFileProvider provider)
        {
            var resolveProviderResult = ResolveProvider(path, false);

            provider = resolveProviderResult.Provider;
            return(provider.OpenStream(resolveProviderResult.Path, mode, access, share));
        }
Esempio n. 4
0
        /// <inheritdoc/>
        public override void FileMove(string sourceUrl, IVirtualFileProvider destinationProvider, string destinationUrl)
        {
            if (destinationProvider is FileSystemProvider filesystemProvider)
            {
                filesystemProvider.CreateDirectory(destinationUrl.Substring(0, destinationUrl.LastIndexOf(VirtualFileSystem.DirectorySeparatorChar)));

                var sourcePath = ConvertUrlToFullPath(sourceUrl);
                var destPath   = filesystemProvider.ConvertUrlToFullPath(destinationUrl);

                File.Move(sourcePath, destPath);
            }
            else
            {
                bool copySuccesful = false;

                using (Stream sourceStream = OpenStream(sourceUrl, VirtualFileMode.Open, VirtualFileAccess.Read))
                    using (Stream destinationStream = destinationProvider.OpenStream(destinationUrl, VirtualFileMode.CreateNew, VirtualFileAccess.Write))
                    {
                        sourceStream.CopyTo(destinationStream);
                        copySuccesful = true;
                    }

                if (copySuccesful)
                {
                    FileDelete(sourceUrl);
                }
            }
        }
Esempio n. 5
0
        /// <summary>
        ///   Creates a temporary zero-byte file and returns its full path.
        /// </summary>
        /// <returns>The full path of the created temporary file.</returns>
        public static string GetTempFileName()
        {
            int    tentatives = 0;
            Stream stream     = null;
            string filename;

            do
            {
                filename = "sd" + (tempFileRandom.Next() + 1).ToString("x") + ".tmp";
                try
                {
                    stream = ApplicationTemporary.OpenStream(filename, VirtualFileMode.CreateNew, VirtualFileAccess.ReadWrite);
                }
                catch (IOException)
                {
                    // No more than 65536 files
                    if (tentatives++ > 0x10000)
                    {
                        throw;
                    }
                }
            }while (stream is null);

            stream.Dispose();

            return(ApplicationTemporary.RootPath + "/" + filename);
        }
Esempio n. 6
0
            public override Stream OpenStream(string url, VirtualFileMode mode, VirtualFileAccess access, VirtualFileShare share = VirtualFileShare.Read, StreamFlags streamFlags = StreamFlags.None)
            {
                if (inMemory.TryGetValue(url, out var bytes))
                {
                    return(new MemoryStream(bytes));
                }

                return(virtualFileProvider.OpenStream(url, mode, access, share, streamFlags));
            }
Esempio n. 7
0
        protected override void InitializeInternal()
        {
            if (soundStreamUrl != null)
            {
                compressedSoundStream = fileProvider.OpenStream(soundStreamUrl, VirtualFileMode.Open, VirtualFileAccess.Read, VirtualFileShare.Read, StreamFlags.Seekable);
                decoder          = new Celt(sampleRate, SamplesPerFrame, channels, true);
                compressedBuffer = new byte[maxCompressedSize];
                reader           = new BinarySerializationReader(compressedSoundStream);

                base.InitializeInternal();
            }
        }
 /// <inheritdoc/>
 public override void FileMove(string sourceUrl, IVirtualFileProvider destinationProvider, string destinationUrl)
 {
     var fsProvider = destinationProvider as FileSystemProvider;
     if (fsProvider != null)
     {
         destinationProvider.CreateDirectory(destinationUrl.Substring(0, destinationUrl.LastIndexOf(VirtualFileSystem.DirectorySeparatorChar)));
         File.Move(ConvertUrlToFullPath(sourceUrl), fsProvider.ConvertUrlToFullPath(destinationUrl));
     }
     else
     {
         using (Stream sourceStream = OpenStream(sourceUrl, VirtualFileMode.Open, VirtualFileAccess.Read),
             destinationStream = destinationProvider.OpenStream(destinationUrl, VirtualFileMode.CreateNew, VirtualFileAccess.Write))
         {
             sourceStream.CopyTo(destinationStream);
         }
         FileDelete(sourceUrl);
     }
 }
Esempio n. 9
0
        /// <inheritdoc/>
        public override void FileMove(string sourceUrl, IVirtualFileProvider destinationProvider, string destinationUrl)
        {
            var fsProvider = destinationProvider as FileSystemProvider;

            if (fsProvider != null)
            {
                destinationProvider.CreateDirectory(destinationUrl.Substring(0, destinationUrl.LastIndexOf(VirtualFileSystem.DirectorySeparatorChar)));
                NativeFile.FileMove(ConvertUrlToFullPath(sourceUrl), fsProvider.ConvertUrlToFullPath(destinationUrl));
            }
            else
            {
                using (Stream sourceStream = OpenStream(sourceUrl, VirtualFileMode.Open, VirtualFileAccess.Read),
                       destinationStream = destinationProvider.OpenStream(destinationUrl, VirtualFileMode.CreateNew, VirtualFileAccess.Write))
                {
                    sourceStream.CopyTo(destinationStream);
                }
                NativeFile.FileDelete(sourceUrl);
            }
        }
Esempio n. 10
0
        public static string GetPathOfSdslShader(string effectName, IVirtualFileProvider fileProvider, IVirtualFileProvider dbFileProvider = null)
        {
            var path = EffectCompilerBase.GetStoragePathFromShaderType(effectName);

            if (fileProvider.TryGetFileLocation(path, out var filePath, out _, out _))
            {
                if (File.Exists(filePath))
                {
                    return(filePath);
                }
            }

            var pathUrl = path + "/path";

            if (fileProvider.FileExists(pathUrl))
            {
                using (var pathStream = fileProvider.OpenStream(pathUrl, VirtualFileMode.Open, VirtualFileAccess.Read))
                    using (var reader = new StreamReader(pathStream))
                    {
                        var dbPath = reader.ReadToEnd();
                        if (File.Exists(dbPath))
                        {
                            return(dbPath);
                        }
                    }
            }

            if (dbFileProvider != null)
            {
                return(GetPathOfSdslShader(effectName, dbFileProvider));
            }

            //find locally
            if (LocalShaderFilePaths.TryGetValue(effectName, out var fp))
            {
                return(fp);
            }

            return(null);
        }
Esempio n. 11
0
 public WaveSampleReader(IVirtualFileProvider fileProvider, string url)
 {
     fileStream     = fileProvider.OpenStream(url, VirtualFileMode.Open, VirtualFileAccess.Read);
     waveFileReader = new WaveFileReader(fileStream);
 }
Esempio n. 12
0
 /// <summary>
 /// Opens the stream from a given path.
 /// </summary>
 /// <param name="path">The path.</param>
 /// <param name="mode">The stream opening mode (append, open, create, etc...).</param>
 /// <param name="access">The stream access.</param>
 /// <param name="share">The stream share mode.</param>
 /// <param name="provider">The provider used to load the stream.</param>
 /// <returns>The stream.</returns>
 public static Stream OpenStream(string path, VirtualFileMode mode, VirtualFileAccess access, VirtualFileShare share, out IVirtualFileProvider provider)
 {
     var resolveProviderResult = ResolveProvider(path, false);
     provider = resolveProviderResult.Provider;
     return provider.OpenStream(resolveProviderResult.Path, mode, access, share);
 }