Exemplo n.º 1
0
        /// <summary>
        /// Returns a cached <see cref="XWaveSound"/> or creates a new one if the requested ID is not cached.
        /// </summary>
        /// <param name="engine">The <see cref="Engine"/> providing the cache.</param>
        /// <param name="id">The ID of the asset to be returned.</param>
        /// <returns>The requested asset; <c>null</c> if <paramref name="id"/> was empty.</returns>
        /// <exception cref="FileNotFoundException">The specified file could not be found.</exception>
        /// <exception cref="IOException">There was an error reading the file.</exception>
        /// <exception cref="UnauthorizedAccessException">Read access to the file is not permitted.</exception>
        /// <exception cref="InvalidDataException">The file does not contain valid WAVE sound data.</exception>
        /// <remarks>Remember to call <see cref="CacheManager.Clean"/> when done, otherwise this object will never be released.</remarks>
        public new static XWaveSound Get(Engine engine, string id)
        {
            #region Sanity checks
            if (engine == null)
            {
                throw new ArgumentNullException(nameof(engine));
            }
            if (string.IsNullOrEmpty(id))
            {
                return(null);
            }
            #endregion

            // Try to find existing asset in cache
            const string type = "Sounds";
            id = FileUtils.UnifySlashes(id);
            string fullID = Path.Combine(type, id);
            var    data   = engine.Cache.GetAsset <XWaveSound>(fullID);

            // Load from file if not in cache
            if (data == null)
            {
                using (new TimedLogEvent("Loading WAVE sound: " + id))
                    using (var stream = ContentManager.GetFileStream("Sounds", id))
                        data = new XWaveSound(stream)
                        {
                            Name = fullID
                        };
                engine.Cache.AddAsset(data);
            }

            return(data);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Returns a cached <see cref="XWaveSound"/> or <see cref="XOggSound"/> (based on the file ending) or creates a new one if the requested ID is not cached.
        /// </summary>
        /// <param name="engine">The <see cref="Engine"/> providing the cache.</param>
        /// <param name="id">The ID of the asset to be returned.</param>
        /// <returns>The requested asset; <c>null</c> if <paramref name="id"/> was empty.</returns>
        /// <exception cref="FileNotFoundException">The specified file could not be found.</exception>
        /// <exception cref="IOException">There was an error reading the file.</exception>
        /// <exception cref="UnauthorizedAccessException">Read access to the file is not permitted.</exception>
        /// <exception cref="InvalidDataException">The file does not contain valid sound data.</exception>
        /// <remarks>Remember to call <see cref="CacheManager.Clean"/> when done, otherwise this object will never be released.</remarks>
        public static XSound Get(Engine engine, string id)
        {
            #region Sanity checks
            if (engine == null)
            {
                throw new ArgumentNullException(nameof(engine));
            }
            if (string.IsNullOrEmpty(id))
            {
                return(null);
            }
            #endregion

            if (id.EndsWith(".ogg", StringComparison.OrdinalIgnoreCase))
            {
                return(XOggSound.Get(engine, id));
            }
            return(XWaveSound.Get(engine, id));
        }