示例#1
0
        /// <summary>
        /// Checks the existence of the identified asset as an async method.
        /// </summary>
        /// <param name="id">The asset identifier.</param>
        /// <returns>Implementors should return true if a stream can be created.</returns>
        protected override async Task <bool> CheckExistsAsync(string id)
        {
            return(await Task.Factory.StartNew(() =>
            {
                if (id == null)
                {
                    throw new ArgumentNullException(nameof(id));
                }

                // If it is an absolute path (e.g. C:\SomeDir\AnAssetFile.ext) directly check its presence
                if (Path.IsPathRooted(id))
                {
                    return File.Exists(id);
                }

                // Path seems relative. First see if the file exists at the current working directory
                if (File.Exists(id))
                {
                    return true;
                }

                foreach (var baseDir in _baseDirs)
                {
                    string path = Path.Combine(baseDir, id);
                    if (File.Exists(path))
                    {
                        return true;
                    }
                }
                return false;
            }));
        }
示例#2
0
        /// <summary>
        /// Create an async stream for the asset identified by id.
        /// </summary>
        /// <param name="id">The asset identifier.</param>
        /// <returns>Implementors should return null if the asset cannot be retrieved. Otherwise returns a file stream to the asset.</returns>
        protected override async Task <Stream> GetStreamAsync(string id)
        {
            return(await Task <Stream> .Factory.StartNew(() =>
            {
                if (id == null)
                {
                    throw new ArgumentNullException(nameof(id));
                }

                // If it is an absolute path (e.g. C:\SomeDir\AnAssetFile.ext) open it directly
                if (Path.IsPathRooted(id))
                {
                    return new FileStream(id, FileMode.Open);
                }

                // Path seems relative. First see if the file exists at the current working directory
                if (File.Exists(id))
                {
                    return new FileStream(id, FileMode.Open);
                }

                // At last, look at the specified base directories
                foreach (var baseDir in _baseDirs)
                {
                    string path = Path.Combine(baseDir, id);
                    if (File.Exists(path))
                    {
                        return new FileStream(path, FileMode.Open);
                    }
                }
                return null;
            }));
        }