Allow adding custom AudioType

Closes #230, #229, and #118
This commit is contained in:
Nick 2022-07-13 23:40:10 -04:00
parent a7f2e3a796
commit a70b2489b9
3 changed files with 101 additions and 7 deletions

View File

@ -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<SlideFunctionModule>();
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);
}

View File

@ -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<string, AudioType> _customAudioTypes;
private static List<AudioLibrary.AudioEntry> _audioEntries;
private static int _startingInt = 4000;
public static void Init()
{
_customAudioTypes = new Dictionary<string, AudioType>();
_audioEntries = new List<AudioLibrary.AudioEntry>();
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;
}
}
}

View File

@ -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);