Example #1
0
        /*********
        ** Public methods
        *********/
        /// <summary>Load content from SMAPI's content API with added support for audio files.</summary>
        /// <typeparam name="T">The expected data type.</typeparam>
        /// <param name="content">The content manager to extend.</param>
        /// <param name="key">The asset key to fetch (if the <paramref name="source"/> is <see cref="ContentSource.GameContent"/>), or the local path to a content file relative to the mod folder.</param>
        /// <param name="source">Where to search for a matching content asset.</param>
        /// <remarks>Since this is just a quick prototype, this doesn't handle caching and such for audio files.</remarks>
        public static T ExtendedLoad <T>(this IContentHelper content, string key, ContentSource source = ContentSource.ModFolder)
        {
            // check filetype
            FileType type = GetAudioType(key);

            // ignore non-audio files
            if (source != ContentSource.ModFolder || type == FileType.Unknown)
            {
                return(content.Load <T>(key, source));
            }

            // get mod file
            key = content.NormalizeAssetName(key);
            FileInfo file = content.GetPrivateField <object>("ModContentManager").InvokePrivateMethod <FileInfo>("GetModFile", key);

            if (!file.Exists)
            {
                return(content.Load <T>(key, source));
            }

            // load unpacked audio file
            if (typeof(T) != typeof(IModCue) && typeof(T) != typeof(ICue))
            {
                throw new ContentLoadException($"Failed loading asset '{key}' from content manager: can't read file with extension '{file.Extension}' as type '{typeof(T)}'; must be type '{typeof(ICue)}' or '{typeof(IModCue)}'.");
            }
            SoundEffectCue effect = new SoundEffectCue(file.Name, file.FullName, type);

            return((T)(object)effect);
        }
        /*********
        ** Public methods
        *********/
        /// <summary>Load content from SMAPI's content API with added support for audio files.</summary>
        /// <typeparam name="T">The expected data type.</typeparam>
        /// <param name="content">The content manager to extend.</param>
        /// <param name="key">The asset key to fetch (if the <paramref name="source"/> is <see cref="ContentSource.GameContent"/>), or the local path to a content file relative to the mod folder.</param>
        /// <param name="source">Where to search for a matching content asset.</param>
        /// <remarks>Since this is just a quick prototype, this doesn't handle caching and such for audio files.</remarks>
        public static T ExtendedLoad <T>(this IContentHelper content, string key, ContentSource source = ContentSource.ModFolder)
        {
            // ignore non-audio files
            if (source != ContentSource.ModFolder || key?.Trim().EndsWith(".ogg", StringComparison.InvariantCultureIgnoreCase) != true)
            {
                return(content.Load <T>(key, source));
            }

            // get mod file
            key = content.NormalizeAssetName(key);
            FileInfo file = content.GetPrivateField <object>("ModContentManager").InvokePrivateMethod <FileInfo>("GetModFile", key);

            if (!file.Exists)
            {
                return(content.Load <T>(key, source));
            }

            // load unpacked audio file
            if (typeof(T) != typeof(IModCue) && typeof(T) != typeof(ICue))
            {
                throw new ContentLoadException($"Failed loading asset '{key}' from content manager: can't read file with extension '{file.Extension}' as type '{typeof(T)}'; must be type '{typeof(ICue)}' or '{typeof(IModCue)}'.");
            }
            SoundEffect effect = OggLoader.Load(file.FullName);

            return((T)(object)new SoundEffectCue(key, effect));
        }