diff --git a/RogueLibsCore/Patches/Patches_Misc.cs b/RogueLibsCore/Patches/Patches_Misc.cs index fcfbccb15..69e5586d8 100644 --- a/RogueLibsCore/Patches/Patches_Misc.cs +++ b/RogueLibsCore/Patches/Patches_Misc.cs @@ -25,6 +25,10 @@ public void PatchMisc() // IDoFixedUpdate.FixedUpdate Patcher.Postfix(typeof(Updater), nameof(Updater.FixedUpdate)); + // load prepared AudioClips + Patcher.Prefix(typeof(AudioHandler), nameof(AudioHandler.SetupDics), nameof(AudioHandler_SetupDics_Prefix)); + Patcher.Postfix(typeof(AudioHandler), nameof(AudioHandler.SetupDics)); + // remove 99 nuggets limit Patcher.Prefix(typeof(Unlocks), nameof(Unlocks.AddNuggets)); } @@ -149,6 +153,21 @@ public static void Updater_FixedUpdate() catch (Exception e) { RogueFramework.LogError(e, "IDoFixedUpdate.FixedUpdate", obj, item); } } + public static void AudioHandler_SetupDics_Prefix(AudioHandler __instance, ref bool __state) + => __state = __instance.loadedDics; + internal static List preparedClips = new List(); + public static void AudioHandler_SetupDics(AudioHandler __instance, ref bool __state) + { + if (__state) return; + foreach (AudioClip clip in preparedClips) + { + __instance.audioClipRealList.Add(clip); + __instance.audioClipList.Add(clip.name); + __instance.audioClipDic.Add(clip.name, clip); + } + preparedClips = null; + } + public static bool Unlocks_AddNuggets(int numNuggets) { GameController.gameController.sessionDataBig.nuggets += numNuggets; diff --git a/RogueLibsCore/RogueLibs.cs b/RogueLibsCore/RogueLibs.cs index a1b263795..7ed2335ef 100644 --- a/RogueLibsCore/RogueLibs.cs +++ b/RogueLibsCore/RogueLibs.cs @@ -4,6 +4,10 @@ using System.Collections.Generic; using System.Runtime.CompilerServices; using UnityEngine; +using System.IO; +using System.Threading; +using UnityEngine.Networking; +using BepInEx; namespace RogueLibsCore { @@ -77,6 +81,33 @@ public static RogueSprite CreateCustomSprite(string name, SpriteScope scope, byt return sprite; } + internal static string audioCachePath; + public static AudioClip CreateCustomAudio(string name, byte[] rawData) + { + string myPath = Path.Combine(audioCachePath, name + ".ogg.request"); + try + { + File.WriteAllBytes(myPath, rawData); + + UnityWebRequest request = UnityWebRequestMultimedia.GetAudioClip("file:///" + myPath, AudioType.OGGVORBIS); + request.SendWebRequest(); + while (!request.isDone) Thread.Sleep(1); + AudioClip clip = DownloadHandlerAudioClip.GetContent(request); + clip.name = name; + if (GameController.gameController?.audioHandler != null) + { + GameController.gameController.audioHandler.audioClipList.Add(name); + GameController.gameController.audioHandler.audioClipRealList.Add(clip); + GameController.gameController.audioHandler.audioClipDic.Add(name, clip); + } + else RogueLibsPlugin.preparedClips.Add(clip); + return clip; + } + finally + { + File.Delete(myPath); + } + } public static CustomName CreateCustomName(string name, string type, CustomNameInfo info) => NameProvider.AddName(name, type, info); diff --git a/RogueLibsCore/RogueLibsPlugin.cs b/RogueLibsCore/RogueLibsPlugin.cs index fbf39233c..e015585e6 100644 --- a/RogueLibsCore/RogueLibsPlugin.cs +++ b/RogueLibsCore/RogueLibsPlugin.cs @@ -6,6 +6,7 @@ using UnityEngine; using BepInEx; using HarmonyLib; +using System.IO; namespace RogueLibsCore { @@ -23,6 +24,13 @@ public void Awake() RogueFramework.Plugin = this; RogueFramework.Logger = Logger; + RogueLibs.audioCachePath = Path.Combine(Paths.CachePath, "RogueLibs Audio"); + Directory.CreateDirectory(RogueLibs.audioCachePath); + foreach (string file in Directory.EnumerateFiles(RogueLibs.audioCachePath)) + File.Delete(file); + foreach (string directory in Directory.EnumerateDirectories(RogueLibs.audioCachePath)) + Directory.Delete(directory, true); + Patcher = new RoguePatcher(this) { EnableStopwatch = true }; PatchItems();