Decompiled source of MusicHax v3.5.2

plugins/MusicHax/MusicHax.dll

Decompiled 9 months ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using LLBML;
using LLBML.Audio;
using LLBML.Utils;
using LLHandlers;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyTitle("MusicHax (org.pkpenguin.plugins.llb.musichax)")]
[assembly: AssemblyProduct("MusicHax")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("3.5.2.0")]
[module: UnverifiableCode]
namespace MusicHax;

[BepInPlugin("org.pkpenguin.plugins.llb.musichax", "MusicHax", "3.5.2")]
[BepInDependency(/*Could not decode attribute arguments.*/)]
[BepInDependency(/*Could not decode attribute arguments.*/)]
[BepInProcess("LLBlaze.exe")]
public class MusicHax : BaseUnityPlugin
{
	internal static AudioInfo currentInfo;

	private static AudioCache musicCache = new AudioCache();

	private static AudioCache sfxCache = new AudioCache();

	private static AudioCache voiceCache = new AudioCache();

	private static ConfigEntry<bool> enablePreloading;

	private static ConfigEntry<bool> enableVanillaMusics;

	private static DirectoryInfo SongsDirectory;

	private static DirectoryInfo SFXDirectory;

	private static DirectoryInfo VoicesDirectory;

	private readonly string loadingText = "MusicHax is loading External Songs...";

	private bool directoryAlreadyCreated;

	internal static DirectoryInfo MHResourcesDir { get; private set; } = null;


	internal static ManualLogSource Log { get; private set; } = null;


	private void Awake()
	{
		//IL_0030: Unknown result type (might be due to invalid IL or missing references)
		Log = ((BaseUnityPlugin)this).Logger;
		((BaseUnityPlugin)this).Logger.LogInfo((object)"Hello, world!");
		MHResourcesDir = ModdingFolder.GetModSubFolder(((BaseUnityPlugin)this).Info);
		Harmony val = new Harmony("org.pkpenguin.plugins.llb.musichax");
		((BaseUnityPlugin)this).Logger.LogDebug((object)"Patching AudioHandler");
		val.PatchAll(typeof(PlayMusicPatch));
		enablePreloading = ((BaseUnityPlugin)this).Config.Bind<bool>("Toggles", "EnablePreloading", true, (ConfigDescription)null);
		enableVanillaMusics = ((BaseUnityPlugin)this).Config.Bind<bool>("Toggles", "EnableVanillaMusics", false, (ConfigDescription)null);
		CreateDirectoryStructure();
	}

	private void Start()
	{
		ModDependenciesUtils.RegisterToModMenu(((BaseUnityPlugin)this).Info, (List<string>)null);
		if (enablePreloading.Value)
		{
			LoadMusics();
			LoadSfx();
			LoadVoice();
		}
	}

	private void Update()
	{
	}

	private void LoadMusics()
	{
		//IL_002d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0032: Unknown result type (might be due to invalid IL or missing references)
		//IL_004a: Unknown result type (might be due to invalid IL or missing references)
		//IL_006b: Unknown result type (might be due to invalid IL or missing references)
		((GenericCache<string, AudioAsset>)(object)musicCache).Clear();
		DirectoryInfo[] directories = SongsDirectory.GetDirectories();
		foreach (DirectoryInfo directoryInfo in directories)
		{
			foreach (AudioInfo audioInfo in AudioUtils.GetAudioInfos(directoryInfo))
			{
				((BaseUnityPlugin)this).Logger.LogDebug((object)("Loading new " + directoryInfo.Name + " : " + audioInfo.file.FullName));
				musicCache.LoadClip(directoryInfo.Name, audioInfo);
			}
		}
	}

	private void LoadSfx()
	{
		//IL_002d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0032: Unknown result type (might be due to invalid IL or missing references)
		//IL_004a: Unknown result type (might be due to invalid IL or missing references)
		//IL_006b: Unknown result type (might be due to invalid IL or missing references)
		((GenericCache<string, AudioAsset>)(object)sfxCache).Clear();
		DirectoryInfo[] directories = SFXDirectory.GetDirectories();
		foreach (DirectoryInfo directoryInfo in directories)
		{
			foreach (AudioInfo audioInfo in AudioUtils.GetAudioInfos(directoryInfo))
			{
				((BaseUnityPlugin)this).Logger.LogDebug((object)("Loading new " + directoryInfo.Name + " : " + audioInfo.file.FullName));
				sfxCache.LoadClip(directoryInfo.Name, audioInfo);
			}
		}
	}

	private static string GetVoiceCacheKey(string audioFile, Character character)
	{
		return ((object)(Character)(ref character)).ToString() + "_" + audioFile;
	}

	private void LoadVoice()
	{
		//IL_0047: Unknown result type (might be due to invalid IL or missing references)
		//IL_004c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0065: Unknown result type (might be due to invalid IL or missing references)
		//IL_0088: Unknown result type (might be due to invalid IL or missing references)
		//IL_009b: Unknown result type (might be due to invalid IL or missing references)
		((GenericCache<string, AudioAsset>)(object)voiceCache).Clear();
		DirectoryInfo[] directories = VoicesDirectory.GetDirectories();
		foreach (DirectoryInfo directoryInfo in directories)
		{
			DirectoryInfo[] directories2 = directoryInfo.GetDirectories();
			foreach (DirectoryInfo directoryInfo2 in directories2)
			{
				foreach (AudioInfo audioInfo in AudioUtils.GetAudioInfos(directoryInfo2))
				{
					((BaseUnityPlugin)this).Logger.LogDebug((object)("Loading new " + directoryInfo2.Name + " : " + audioInfo.file.FullName));
					string voiceCacheKey = GetVoiceCacheKey(directoryInfo2.Name, CharacterApi.GetCharacterByName(directoryInfo.Name));
					voiceCache.LoadClip(voiceCacheKey, audioInfo);
				}
			}
		}
	}

	private void ReloadMusics()
	{
		LoadMusics();
	}

	public static void CreateDirectoryStructure()
	{
		//IL_0094: Unknown result type (might be due to invalid IL or missing references)
		//IL_0099: Unknown result type (might be due to invalid IL or missing references)
		SongsDirectory = MHResourcesDir.CreateSubdirectory("Songs");
		string[] songs = FileStructure.Songs;
		foreach (string path in songs)
		{
			SongsDirectory.CreateSubdirectory(path);
		}
		SFXDirectory = MHResourcesDir.CreateSubdirectory("SFX");
		songs = FileStructure.SFX;
		foreach (string path2 in songs)
		{
			SFXDirectory.CreateSubdirectory(path2);
		}
		VoicesDirectory = MHResourcesDir.CreateSubdirectory("Voices");
		foreach (Character playableCharacter in CharacterApi.GetPlayableCharacters())
		{
			Character current = playableCharacter;
			DirectoryInfo directoryInfo = VoicesDirectory.CreateSubdirectory(((object)(Character)(ref current)).ToString());
			songs = FileStructure.Voices;
			foreach (string text in songs)
			{
				directoryInfo.CreateSubdirectory(((object)(Character)(ref current)).ToString().ToLower() + text);
			}
		}
	}

	public static void CreateMusicDirectory(string clipName)
	{
		Directory.CreateDirectory(Path.Combine(SongsDirectory.FullName, clipName));
	}

	public void CreateMusicDirectories()
	{
		if (directoryAlreadyCreated)
		{
			return;
		}
		for (int i = 1; i < 21; i++)
		{
			if (i != 13)
			{
				string audioClipName = DNPFJHMAIBP.AKGAOAEJ.musicAssetLinks.GetMusicAsset((AudioTrack)i).audioClipName;
				((BaseUnityPlugin)this).Logger.LogDebug((object)("Creating directory: " + audioClipName));
				Directory.CreateDirectory(Path.Combine(SongsDirectory.FullName, audioClipName));
			}
		}
		directoryAlreadyCreated = true;
	}

	public static AudioAsset GetAudioAssetFor(string clipName)
	{
		Log.LogDebug((object)("Got asked for a clip named: \"" + clipName + "\""));
		if (enablePreloading.Value && ((GenericCache<string, AudioAsset>)(object)musicCache).ContainsKey(clipName))
		{
			int num = ((GenericCache<string, AudioAsset>)(object)musicCache)[clipName].Count;
			Log.LogDebug((object)$"Preloading: key exists : \"{clipName}\" | Found list length: {((GenericCache<string, AudioAsset>)(object)musicCache)[clipName].Count}");
			if (enableVanillaMusics.Value)
			{
				num++;
			}
			if (num > 0)
			{
				int num2 = Random.Range(0, num);
				if (enableVanillaMusics.Value)
				{
					num2--;
				}
				Log.LogDebug((object)$"Rolled {num2} out of {num}");
				if (num2 < 0)
				{
					return null;
				}
				return ((GenericCache<string, AudioAsset>)(object)musicCache)[clipName][num2];
			}
		}
		else
		{
			try
			{
				string[] files = Directory.GetFiles(Path.Combine(SongsDirectory.FullName, clipName));
				if (files.Length != 0)
				{
					return AudioUtils.GetAssetSynchronously(files[Random.Range(0, files.Length - 1)]);
				}
			}
			catch (Exception ex)
			{
				Log.LogError((object)("MusicHax: Exception caught:\n" + ex.Message));
			}
		}
		return null;
	}

	public static AudioClip GetAudioClipFor(string clipName)
	{
		return GetAudioAssetFor(clipName).audioClip;
	}

	public static AudioClip[] GetSfxClips(string audioFile)
	{
		Log.LogDebug((object)("MusicHax: Got asked for a sfx clip named: \"" + audioFile + "\""));
		Directory.CreateDirectory(Path.Combine(SFXDirectory.FullName, audioFile));
		if (((GenericCache<string, AudioAsset>)(object)sfxCache).ContainsKey(audioFile) && ((GenericCache<string, AudioAsset>)(object)sfxCache)[audioFile].Count > 0)
		{
			return ((GenericCache<string, AudioAsset>)(object)sfxCache)[audioFile].ConvertAll((AudioAsset input) => input.audioClip).ToArray();
		}
		return null;
	}

	public static AudioClip[] GetVoiceClips(string audioFile, Character character)
	{
		//IL_0050: Unknown result type (might be due to invalid IL or missing references)
		Log.LogDebug((object)("MusicHax: Got asked for a voice clip named: \"" + audioFile + "\" for " + ((object)(Character)(ref character)).ToString()));
		Directory.CreateDirectory(Path.Combine(Path.Combine(VoicesDirectory.FullName, ((object)(Character)(ref character)).ToString()), audioFile));
		string voiceCacheKey = GetVoiceCacheKey(audioFile, character);
		if (((GenericCache<string, AudioAsset>)(object)voiceCache).ContainsKey(voiceCacheKey) && ((GenericCache<string, AudioAsset>)(object)voiceCache)[voiceCacheKey].Count > 0)
		{
			return ((GenericCache<string, AudioAsset>)(object)voiceCache)[voiceCacheKey].ConvertAll((AudioAsset input) => input.audioClip).ToArray();
		}
		return null;
	}
}
internal static class PluginInfos
{
	public const string PLUGIN_NAME = "MusicHax";

	public const string PLUGIN_ID = "org.pkpenguin.plugins.llb.musichax";

	public const string PLUGIN_VERSION = "3.5.2";
}
public static class PlayMusicPatch
{
	[HarmonyPatch(typeof(AudioHandler), "PlayMusic", new Type[]
	{
		typeof(AudioClip),
		typeof(Vector2)
	})]
	[HarmonyPrefix]
	public static bool PlayMusic_Prefix(ref AudioClip clip, ref Vector2 pLoopData)
	{
		//IL_0012: Unknown result type (might be due to invalid IL or missing references)
		//IL_0070: Unknown result type (might be due to invalid IL or missing references)
		//IL_0075: Unknown result type (might be due to invalid IL or missing references)
		//IL_0089: Unknown result type (might be due to invalid IL or missing references)
		//IL_008e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0055: Unknown result type (might be due to invalid IL or missing references)
		//IL_0067: Unknown result type (might be due to invalid IL or missing references)
		//IL_0068: Unknown result type (might be due to invalid IL or missing references)
		//IL_00c1: Unknown result type (might be due to invalid IL or missing references)
		//IL_00c6: Unknown result type (might be due to invalid IL or missing references)
		//IL_013e: Unknown result type (might be due to invalid IL or missing references)
		MusicHax.Log.LogDebug((object)$"Clip {((Object)clip).name} had loop data {pLoopData}");
		MusicHax.CreateMusicDirectory(((Object)clip).name);
		AudioAsset audioAssetFor = MusicHax.GetAudioAssetFor(((Object)clip).name);
		if ((Object)(object)audioAssetFor?.audioClip == (Object)null)
		{
			AudioInfo currentInfo = default(AudioInfo);
			currentInfo.volume = 1f;
			MusicHax.currentInfo = currentInfo;
			return true;
		}
		MusicHax.currentInfo = audioAssetFor.audioInfo;
		clip = audioAssetFor.audioClip;
		pLoopData = audioAssetFor.audioInfo.loopData;
		if (DebugSettings.instance.musicOff)
		{
			return false;
		}
		if ((Object)(object)clip == (Object)null || AudioHandler.musicPlaying == ((Object)clip).name)
		{
			return false;
		}
		AudioHandler.musicLoop = pLoopData;
		if (AudioHandler.musicLoop.y <= 0f)
		{
			AudioHandler.musicLoop.y = clip.length;
		}
		AudioHandler.audioSourceMusic1.clip = clip;
		AudioHandler.audioSourceMusic2.clip = clip;
		AudioHandler.audioSourceMusic1.loop = true;
		AudioHandler.audioSourceMusic2.loop = true;
		AudioHandler.activeMusicChannel = 1;
		AudioHandler.musicPlaying = ((Object)clip).name;
		if (CGLLJHHAJAK.GIGAKBJGFDI.delayMusicUntilLoaded)
		{
			((KoroutineBehaviour)AudioHandler.instance).StopKoroutine(AudioHandler.kPlayMusic, true);
			((KoroutineBehaviour)AudioHandler.instance).StartKoroutine(AudioHandler.instance.KPlayMusic(), ref AudioHandler.kPlayMusic);
		}
		else if (AudioHandler.musicLoop.x > 0f)
		{
			double num = AudioSettings.dspTime + 0.009999999776482582;
			AudioHandler.nextMusicLoopTime = num + (double)AudioHandler.musicLoop.y;
			AudioHandler.audioSourceMusic1.time = 0f;
			AudioHandler.audioSourceMusic1.PlayScheduled(num);
			AudioHandler.audioSourceMusic1.SetScheduledEndTime(AudioHandler.nextMusicLoopTime);
		}
		else
		{
			AudioHandler.audioSourceMusic1.Play();
		}
		AudioHandler.SetMusicLoudness(audioAssetFor.audioInfo.volume);
		return false;
	}

	[HarmonyPatch(typeof(AudioHandler), "SetMusicLoudness")]
	[HarmonyPrefix]
	public static bool SetMusicLoudnessPrefix(ref float f)
	{
		f *= MusicHax.currentInfo.volume;
		return true;
	}

	[HarmonyPatch(typeof(AudioHandler), "Update")]
	[HarmonyPrefix]
	public static bool Update_Prefix()
	{
		if (AudioHandler.musicLoop.x > 0f && (AudioHandler.audioSourceMusic1.isPlaying || AudioHandler.audioSourceMusic2.isPlaying) && AudioSettings.dspTime + 1.0 > AudioHandler.nextMusicLoopTime)
		{
			if (AudioHandler.activeMusicChannel == 0)
			{
				AudioHandler.audioSourceMusic1.time = AudioHandler.musicLoop.x;
				AudioHandler.audioSourceMusic1.PlayScheduled(AudioHandler.nextMusicLoopTime);
				AudioHandler.audioSourceMusic1.SetScheduledEndTime(AudioHandler.nextMusicLoopTime + (double)AudioHandler.musicLoop.y - (double)AudioHandler.musicLoop.x);
			}
			else if (AudioHandler.activeMusicChannel == 1)
			{
				AudioHandler.audioSourceMusic2.time = AudioHandler.musicLoop.x;
				AudioHandler.audioSourceMusic2.PlayScheduled(AudioHandler.nextMusicLoopTime);
				AudioHandler.audioSourceMusic2.SetScheduledEndTime(AudioHandler.nextMusicLoopTime + (double)AudioHandler.musicLoop.y - (double)AudioHandler.musicLoop.x);
			}
			AudioHandler.activeMusicChannel = 1 - AudioHandler.activeMusicChannel;
			AudioHandler.nextMusicLoopTime += (double)(AudioHandler.musicLoop.y - AudioHandler.musicLoop.x);
		}
		return false;
	}

	[HarmonyPatch(typeof(AudioHandler), "PlayMusic", new Type[]
	{
		typeof(AudioClip),
		typeof(Vector2)
	})]
	[HarmonyPostfix]
	public static void PlayMusic_Postfix()
	{
		//IL_000a: Unknown result type (might be due to invalid IL or missing references)
		MusicHax.Log.LogDebug((object)$"At the end of PlayMusic, the loop data was {AudioHandler.musicLoop})");
	}

	[HarmonyPatch(typeof(SfxInfo), "Preload")]
	[HarmonyPrefix]
	public static bool SfxInfoPreload_Prefix(SfxInfo __instance, ref bool __result)
	{
		if (__instance.isLoaded)
		{
			__result = false;
			return false;
		}
		AudioClip[] sfxClips = MusicHax.GetSfxClips(__instance.audioFile);
		if (sfxClips != null)
		{
			__instance.audioClips = sfxClips;
			__instance.isLoaded = true;
			__result = true;
			return false;
		}
		return true;
	}

	[HarmonyPatch(typeof(VoiceInfo), "Preload")]
	[HarmonyPrefix]
	public static bool VoiceInfoPreload_Prefix(VoiceInfo __instance, ref bool __result)
	{
		//IL_0014: Unknown result type (might be due to invalid IL or missing references)
		if (((SfxInfo)__instance).isLoaded)
		{
			__result = false;
			return false;
		}
		AudioClip[] voiceClips = MusicHax.GetVoiceClips(((SfxInfo)__instance).audioFile, __instance.character);
		if (voiceClips != null)
		{
			((SfxInfo)__instance).audioClips = voiceClips;
			((SfxInfo)__instance).isLoaded = true;
			__result = true;
			return false;
		}
		return true;
	}
}
public static class FileStructure
{
	public static readonly string[] SFX = new string[94]
	{
		"announcer_candyman", "announcer_dice", "announcer_doombox", "announcer_dustandashes", "announcer_eight", "announcer_five", "announcer_four", "announcer_game", "announcer_get_ready", "announcer_grid",
		"announcer_jet", "announcer_latch", "announcer_lethal_league", "announcer_nine", "announcer_nitro", "announcer_one", "announcer_play_ball", "announcer_random", "announcer_raptor", "announcer_select",
		"announcer_seven", "announcer_six", "announcer_sonata", "announcer_switch", "announcer_ten", "announcer_three", "announcer_time_up", "announcer_toxic", "announcer_two", "bubble_pop",
		"catch", "deflect", "express", "get_hit1", "get_hit2", "get_hit3", "get_hit4", "getupgrab", "graffitiPlace", "jump",
		"kill", "KO_in", "KO_out", "lobby_character_select", "lobby_player_in", "lobby_player_out", "lobby_start_game", "medium_hit", "medium_smash", "menu_back",
		"menu_confirm", "menu_scroll", "menu_set", "next_line", "page_turn", "parry", "parry_activate", "parry_counter", "powerup_heal", "powerup_insert",
		"powerup_insert_noroll", "powerup_invisible", "powerup_multi_bounce", "powerup_phantom_hit", "powerup_random", "powerup_result", "powerup_spark", "powerup_ultra", "powerup_warp", "result_xp_gain",
		"sax1", "sax10", "sax11", "sax2", "sax3", "sax4", "sax5", "sax6", "sax7", "sax8",
		"sax9", "special_activate", "strong_hit", "strong_smash", "swing", "teleport", "throw", "tutorial_ok", "volleyball_hit", "volleyball_smash",
		"wall_bounce", "wall_bounce_fast", "weak_hit", "weak_smash"
	};

	public static readonly string[] Songs = new string[19]
	{
		"AAron EVO - Beverly Chills", "B. Bravo & Lean Rock - Shred", "Bignic - Ruiner", "Bignic - Scream", "D Fast - Jungaaaa", "D Fast - Whipz", "Doctor Lazer - Headbangeren", "Doctor Lazer - Thuggin", "Frank Klepacki - Decible", "Grillo - Urabon",
		"Hideki Naganuma - AIN'T NOTHIN' LIKE A FUNKY BEAT", "Klaus Veen - Dial Hop", "Klaus Veen - Ordinary Days", "Klaus Veen - Ordinary Days V2", "Klaus Veen - Snibbit", "Pixelord - Dominator", "Pixelord - Killa Swing", "The Sleepers RecordZ ft. Nasty Den - Nasty Lovers", "zeroSCAR - X"
	};

	public static readonly string[] Voices = new string[7] { "_gamewin", "_gethit", "_intense_swing", "_jump", "_normal_swing", "_score", "_special_activation" };
}