Пример #1
0
        /// <summary>
        /// Loads the shader source with the specified type name.
        /// </summary>
        /// <param name="type">The typeName.</param>
        /// <returns>ShaderSourceWithHash.</returns>
        /// <exception cref="System.IO.FileNotFoundException">If the file was not found</exception>
        public ShaderSourceWithHash LoadShaderSource(string type)
        {
            lock (locker)
            {
                // Load file
                ShaderSourceWithHash shaderSource;
                if (!loadedShaderSources.TryGetValue(type, out shaderSource))
                {
                    var sourceUrl = FindFilePath(type);
                    if (sourceUrl != null)
                    {
                        shaderSource = new ShaderSourceWithHash();
                        if (!UrlToFilePath.TryGetValue(sourceUrl, out shaderSource.Path))
                        {
                            shaderSource.Path = sourceUrl;
                        }

                        // On Windows, Always try to load first from the original URL in order to get the latest version
                        if (Platform.IsWindowsDesktop)
                        {
                            // TODO: the "/path" is hardcoded, used in ImportStreamCommand and EffectSystem. Find a place to share this correctly.
                            var pathUrl = sourceUrl + "/path";
                            if (FileExists(pathUrl))
                            {
                                using (var fileStream = OpenStream(pathUrl))
                                {
                                    string shaderSourcePath;
                                    using (var sr = new StreamReader(fileStream, Encoding.UTF8))
                                        shaderSourcePath = sr.ReadToEnd();

                                    if (File.Exists(shaderSourcePath))
                                    {
                                        byte[] fileData = null;
                                        for (int tries = 10; tries >= 0; --tries)
                                        {
                                            try
                                            {
                                                fileData = File.ReadAllBytes(shaderSourcePath);
                                                break;
                                            }
                                            catch (IOException)
                                            {
                                                // Try again
                                            }
                                        }

                                        if (fileData != null)
                                        {
                                            // Replace path with a local path
                                            shaderSource.Path = Path.Combine(PlatformFolders.ApplicationBinaryDirectory, shaderSourcePath);
                                            shaderSource.Hash = ObjectId.FromBytes(fileData);

                                            // Note: we can't use Encoding.UTF8.GetString directly because there might be the UTF8 BOM at the beginning of the file
                                            using (StreamReader reader = new StreamReader(new MemoryStream(fileData), Encoding.UTF8))
                                                shaderSource.Source = reader.ReadToEnd();
                                        }
                                    }
                                }
                            }
                        }

                        if (shaderSource.Source == null)
                        {
                            using (var sourceStream = OpenStream(sourceUrl))
                            {
                                var databaseStream = sourceStream as IDatabaseStream;

                                using (var sr = new StreamReader(sourceStream))
                                {
                                    shaderSource.Source = sr.ReadToEnd();

                                    if (databaseStream == null)
                                    {
                                        sourceStream.Position = 0;
                                        var data = new byte[sourceStream.Length];
                                        sourceStream.Read(data, 0, (int)sourceStream.Length);
                                        shaderSource.Hash = ObjectId.FromBytes(data);
                                    }
                                    else
                                    {
                                        shaderSource.Hash = databaseStream.ObjectId;
                                    }
                                }
                            }
                        }

                        loadedShaderSources[type] = shaderSource;
                    }
                    else
                    {
                        throw new FileNotFoundException($"Unable to find shader [{type}]", $"{type}.xksl");
                    }
                }
                return(shaderSource);
            }
        }
Пример #2
0
        /// <summary>
        /// Loads the shader source with the specified type name.
        /// </summary>
        /// <param name="type">The typeName.</param>
        /// <returns>ShaderSourceWithHash.</returns>
        /// <exception cref="System.IO.FileNotFoundException">If the file was not found</exception>
        public ShaderSourceWithHash LoadShaderSource(string type)
        {
            lock (locker)
            {
                // Load file
                ShaderSourceWithHash shaderSource;
                if (!loadedShaderSources.TryGetValue(type, out shaderSource))
                {
                    var sourceUrl = FindFilePath(type);
                    if (sourceUrl != null)
                    {
                        shaderSource = new ShaderSourceWithHash();
                        if (!UrlToFilePath.TryGetValue(sourceUrl, out shaderSource.Path))
                        {
                            shaderSource.Path = sourceUrl;
                        }

                        // On Windows, Always try to load first from the original URL in order to get the latest version
                        if (Platform.IsWindowsDesktop)
                        {
                            // TODO: the "/path" is hardcoded, used in ImportStreamCommand and EffectSystem. Find a place to share this correctly.
                            var pathUrl = sourceUrl + "/path";
                            if (FileExists(pathUrl))
                            {
                                using (var fileStream = OpenStream(pathUrl))
                                {
                                    string shaderSourcePath;
                                    using (var sr = new StreamReader(fileStream, Encoding.UTF8))
                                        shaderSourcePath = sr.ReadToEnd();

                                    if (File.Exists(shaderSourcePath))
                                    {
                                        // Replace path with a local path
                                        shaderSource.Path = Path.Combine(Environment.CurrentDirectory, shaderSourcePath);

                                        // Optimization: It currently reads the source file twice
                                        shaderSource.Hash = ObjectId.FromBytes(File.ReadAllBytes(shaderSourcePath));
                                        using (var sourceStream = File.Open(shaderSourcePath, FileMode.Open, FileAccess.Read, FileShare.Read))
                                        {
                                            using (var sr = new StreamReader(sourceStream))
                                                shaderSource.Source = sr.ReadToEnd();
                                        }
                                    }
                                }
                            }
                        }

                        if (shaderSource.Source == null)
                        {
                            using (var sourceStream = OpenStream(sourceUrl))
                            {
                                var databaseStream = sourceStream as IDatabaseStream;
                                var fileStream     = sourceStream as FileStream;
                                if (databaseStream != null || fileStream != null)
                                {
                                    using (var sr = new StreamReader(sourceStream))
                                        shaderSource.Source = sr.ReadToEnd();

                                    if (databaseStream != null)
                                    {
                                        shaderSource.Hash = databaseStream.ObjectId;
                                    }
                                    else
                                    {
                                        shaderSource.Hash = ObjectId.FromBytes(File.ReadAllBytes(sourceUrl));
                                    }
                                }
                                else
                                {
                                    throw new Exception(string.Format("Unsupported Stream type to load shader [{0}.pdxsl]", type));
                                }
                            }
                        }

                        loadedShaderSources[type] = shaderSource;
                    }
                    else
                    {
                        throw new FileNotFoundException(string.Format("Unable to find shader [{0}]", type), string.Format("{0}.pdxsl", type));
                    }
                }
                return(shaderSource);
            }
        }
Пример #3
0
        /// <summary>
        /// Loads the shader source with the specified type name.
        /// </summary>
        /// <param name="type">The typeName.</param>
        /// <returns>ShaderSourceWithHash.</returns>
        /// <exception cref="System.IO.FileNotFoundException">If the file was not found</exception>
        public ShaderSourceWithHash LoadShaderSource(string type)
        {
            lock (locker)
            {
                // Load file
                ShaderSourceWithHash shaderSource;
                if (!loadedShaderSources.TryGetValue(type, out shaderSource))
                {
                    var sourceUrl = FindFilePath(type);
                    if (sourceUrl != null)
                    {
                        shaderSource = new ShaderSourceWithHash();
                        if (!UrlToFilePath.TryGetValue(sourceUrl, out shaderSource.Path))
                        {
                            shaderSource.Path = sourceUrl;
                        }

                        // On Windows, Always try to load first from the original URL in order to get the latest version
#if SILICONSTUDIO_PLATFORM_WINDOWS_DESKTOP
                        if (Platform.IsWindowsDesktop)
                        {
                            // TODO: the "/path" is hardcoded, used in ImportStreamCommand and EffectSystem. Find a place to share this correctly.
                            var pathUrl = sourceUrl + "/path";
                            if (FileExists(pathUrl))
                            {
                                using (var fileStream = OpenStream(pathUrl))
                                {
                                    string shaderSourcePath;
                                    using (var sr = new StreamReader(fileStream, Encoding.UTF8))
                                        shaderSourcePath = sr.ReadToEnd();

                                    if (File.Exists(shaderSourcePath))
                                    {
                                        // Replace path with a local path
                                        shaderSource.Path = Path.Combine(Directory.GetCurrentDirectory(), shaderSourcePath);

                                        // Optimization: It currently reads the source file twice
                                        shaderSource.Hash = ObjectId.FromBytes(File.ReadAllBytes(shaderSourcePath));
                                        using (var sourceStream = File.Open(shaderSourcePath, FileMode.Open, FileAccess.Read, FileShare.Read))
                                        {
                                            using (var sr = new StreamReader(sourceStream))
                                                shaderSource.Source = sr.ReadToEnd();
                                        }
                                    }
                                }
                            }
                        }
#endif

                        if (shaderSource.Source == null)
                        {
                            using (var sourceStream = OpenStream(sourceUrl))
                            {
                                var databaseStream = sourceStream as IDatabaseStream;

                                using (var sr = new StreamReader(sourceStream))
                                {
                                    shaderSource.Source = sr.ReadToEnd();

                                    if (databaseStream == null)
                                    {
                                        sourceStream.Position = 0;
                                        var data = new byte[sourceStream.Length];
                                        sourceStream.Read(data, 0, (int)sourceStream.Length);
                                        shaderSource.Hash = ObjectId.FromBytes(data);
                                    }
                                    else
                                    {
                                        shaderSource.Hash = databaseStream.ObjectId;
                                    }
                                }
                            }
                        }

                        loadedShaderSources[type] = shaderSource;
                    }
                    else
                    {
                        throw new FileNotFoundException($"Unable to find shader [{type}]", $"{type}.xksl");
                    }
                }
                return(shaderSource);
            }
        }