diff --git a/NewHorizons/Builder/Props/ProjectionBuilder.cs b/NewHorizons/Builder/Props/ProjectionBuilder.cs index 439faaeb..0f34410e 100644 --- a/NewHorizons/Builder/Props/ProjectionBuilder.cs +++ b/NewHorizons/Builder/Props/ProjectionBuilder.cs @@ -83,7 +83,7 @@ namespace NewHorizons.Builder.Props imageLoader.pathsToLoad.Add(mod.ModHelper.Manifest.ModFolderPath + slideInfo.imagePath); - AddModules(slideInfo, ref slide); + AddModules(slideInfo, ref slide, mod); slideCollection.slides[i] = slide; } @@ -171,7 +171,7 @@ namespace NewHorizons.Builder.Props imageLoader.pathsToLoad.Add(mod.ModHelper.Manifest.ModFolderPath + slideInfo.imagePath); - AddModules(slideInfo, ref slide); + AddModules(slideInfo, ref slide, mod); slideCollection.slides[i] = slide; } @@ -218,7 +218,7 @@ namespace NewHorizons.Builder.Props imageLoader.pathsToLoad.Add(mod.ModHelper.Manifest.ModFolderPath + slideInfo.imagePath); - AddModules(slideInfo, ref slide); + AddModules(slideInfo, ref slide, mod); slideCollection.slides[i] = slide; } @@ -282,7 +282,7 @@ namespace NewHorizons.Builder.Props imageLoader.pathsToLoad.Add(mod.ModHelper.Manifest.ModFolderPath + slideInfo.imagePath); - AddModules(slideInfo, ref slide); + AddModules(slideInfo, ref slide, mod); slideCollection.slides[i] = slide; } @@ -322,20 +322,20 @@ namespace NewHorizons.Builder.Props return standingTorch; } - private static void AddModules(PropModule.SlideInfo slideInfo, ref Slide slide) + private static void AddModules(PropModule.SlideInfo slideInfo, ref Slide slide, IModBehaviour mod) { var modules = new List(); if (!String.IsNullOrEmpty(slideInfo.beatAudio)) { var audioBeat = new SlideBeatAudioModule(); - audioBeat._audioType = (AudioType)Enum.Parse(typeof(AudioType), slideInfo.beatAudio); + audioBeat._audioType = AudioTypeHandler.GetAudioType(slideInfo.beatAudio, mod); audioBeat._delay = slideInfo.beatDelay; modules.Add(audioBeat); } if (!String.IsNullOrEmpty(slideInfo.backdropAudio)) { var audioBackdrop = new SlideBackdropAudioModule(); - audioBackdrop._audioType = (AudioType)Enum.Parse(typeof(AudioType), slideInfo.backdropAudio); + audioBackdrop._audioType = AudioTypeHandler.GetAudioType(slideInfo.backdropAudio, mod); audioBackdrop._fadeTime = slideInfo.backdropFadeTime; modules.Add(audioBackdrop); } diff --git a/NewHorizons/Handlers/AudioTypeHandler.cs b/NewHorizons/Handlers/AudioTypeHandler.cs new file mode 100644 index 00000000..8d22b764 --- /dev/null +++ b/NewHorizons/Handlers/AudioTypeHandler.cs @@ -0,0 +1,91 @@ +using NewHorizons.Utility; +using OWML.Common; +using System; +using System.Collections.Generic; +using System.Linq; +using UnityEngine; +using Logger = NewHorizons.Utility.Logger; + +namespace NewHorizons.Handlers +{ + public static class AudioTypeHandler + { + private static Dictionary _customAudioTypes; + private static List _audioEntries; + private static int _startingInt = 4000; + + public static void Init() + { + _customAudioTypes = new Dictionary(); + _audioEntries = new List(); + + Main.Instance.ModHelper.Events.Unity.RunWhen( + () => Locator.GetAudioManager()?._libraryAsset != null, + PostInit + ); + } + + private static void PostInit() + { + Logger.LogVerbose($"Adding all custom AudioTypes to the library"); + + var library = Locator.GetAudioManager()._libraryAsset; + library.audioEntries = library.audioEntries.Concat(_audioEntries).ToArray(); + + Locator.GetAudioManager()._audioLibraryDict = library.BuildAudioEntryDictionary(); + } + + // Will return an existing audio type or create a new one for the given audio string + public static AudioType GetAudioType(string audio, IModBehaviour mod) + { + try + { + if (audio.Contains(".wav") || audio.Contains(".mp3") || audio.Contains(".ogg")) + { + return AddCustomAudioType(audio, mod); + } + else + { + return (AudioType)Enum.Parse(typeof(AudioType), audio); + } + } + catch (Exception e) + { + Logger.LogError($"Couldn't load AudioType {e.Message}, {e.StackTrace}"); + return AudioType.None; + } + } + + // Create a custom audio type from relative file path and the mod + public static AudioType AddCustomAudioType(string audioPath, IModBehaviour mod) + { + AudioType audioType; + + var id = mod.ModHelper.Manifest.UniqueName + "_" + audioPath; + if (_customAudioTypes.TryGetValue(id, out audioType)) return audioType; + + var audioClip = AudioUtilities.LoadAudio(mod.ModHelper.Manifest.ModFolderPath + "/" + audioPath); + + if (audioClip == null) + { + Logger.LogError($"Couldn't create audioType for {audioPath}"); + return AudioType.None; + } + + return AddCustomAudioType(id, new AudioClip[] { audioClip }); + } + + // Create a custom audio type from a set of audio clips. Needs a unique ID + public static AudioType AddCustomAudioType(string id, AudioClip[] audioClips) + { + var audioType = (AudioType)_startingInt + _customAudioTypes.Count(); + + Logger.LogVerbose($"Registering custom audio type {id} as {audioType}"); + + _audioEntries.Add(new AudioLibrary.AudioEntry(audioType, audioClips)); + _customAudioTypes.Add(id, audioType); + + return audioType; + } + } +} diff --git a/NewHorizons/Main.cs b/NewHorizons/Main.cs index 06b8e93b..a6b2dc49 100644 --- a/NewHorizons/Main.cs +++ b/NewHorizons/Main.cs @@ -273,7 +273,10 @@ namespace NewHorizons BrambleNodeBuilder.Init(); AstroObjectLocator.Init(); StreamingHandler.Init(); + AudioTypeHandler.Init(); + PlanetCreationHandler.Init(BodyDict[CurrentStarSystem]); + VesselWarpHandler.LoadVessel(); SystemCreationHandler.LoadSystem(SystemDict[CurrentStarSystem]); LoadTranslations(ModHelper.Manifest.ModFolderPath + "Assets/", this);