Decompiled source of CoolRandomArenaSound v1.5.0

BepInEx/plugins/CoolRandomArenaSound/CoolRandomArenaSound.dll

Decompiled 2 years ago
using System;
using System.Collections;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Logging;
using HarmonyLib;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: AssemblyTitle("CoolRandomArenaSound")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("CoolRandomArenaSound")]
[assembly: AssemblyCopyright("Copyright ©  2025")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("9c631775-9d03-49e6-9a28-061df1b3025b")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace CoolRandomArenaSound;

[BepInPlugin("cool_random_arena_sound", "Cool Random Arena Sound", "1.5.0")]
internal class Plugin : BaseUnityPlugin
{
	private readonly Harmony harmony = new Harmony("cool_random_arena_sound");

	internal static ManualLogSource logger;

	private void Awake()
	{
		logger = ((BaseUnityPlugin)this).Logger;
		logger.LogInfo((object)" Cool Random Arena Sound Plugin Loaded");
		harmony.PatchAll(typeof(ArenaMusicAwakePatch));
	}
}
[HarmonyPatch(typeof(Arena), "Awake")]
internal static class ArenaMusicAwakePatch
{
	[HarmonyPostfix]
	private static void ReplaceArenaMusic()
	{
		TryLoadRandomMusic();
	}

	public static void TryLoadRandomMusic()
	{
		string text = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "ArenaSounds");
		if (!Directory.Exists(text))
		{
			Plugin.logger.LogError((object)(" Folder not found: " + text));
			return;
		}
		string[] files = Directory.GetFiles(text, "*.ogg");
		if (files.Length == 0)
		{
			Plugin.logger.LogError((object)" No .ogg files found.");
			return;
		}
		string text2 = files[Random.Range(0, files.Length)];
		Plugin.logger.LogInfo((object)(Path.GetFileName(text2) ?? ""));
		Arena instance = Arena.instance;
		if (instance != null)
		{
			((MonoBehaviour)instance).StartCoroutine(LoadAndSetClip(text2));
		}
	}

	public static IEnumerator LoadAndSetClip(string filePath)
	{
		string uri = "file://" + filePath.Replace("\\", "/");
		WWW www = new WWW(uri);
		try
		{
			yield return www;
			if (!string.IsNullOrEmpty(www.error))
			{
				Plugin.logger.LogError((object)("WWW error: " + www.error));
				yield break;
			}
			AudioClip clip = www.GetAudioClip(false, false, (AudioType)14);
			if ((Object)(object)clip == (Object)null)
			{
				Plugin.logger.LogError((object)(" Failed to load clip from: " + filePath));
				yield break;
			}
			Plugin.logger.LogInfo((object)(" Successfully loaded: " + ((Object)clip).name));
			Arena.instance.soundArenaMusic.Sounds = (AudioClip[])(object)new AudioClip[1] { clip };
			Arena.instance.soundArenaMusic.PlayLoop(true, 20f, 2f, 1f);
			Arena.instance.soundArenaMusic.Source.time = 0f;
			Plugin.logger.LogInfo((object)" Forced playback of the new track.");
		}
		finally
		{
			((IDisposable)www)?.Dispose();
		}
	}
}