Decompiled source of Erenshor Levelup Sound v1.0.1

BepInEx\plugins\LevelupSound\Erenshor-LevelupSound.dll

Decompiled 2 months ago
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using BepInEx;
using UnityEngine;
using UnityEngine.Networking;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyTitle("Erenshor-LevelupSound")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("Erenshor-LevelupSound")]
[assembly: AssemblyCopyright("Copyright ©  2025")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("bc4a8707-5e0e-4a26-a49e-439f28dbc304")]
[assembly: AssemblyFileVersion("1.0.1")]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyVersion("1.0.1.0")]
namespace Erenshor_LevelupSound;

[BepInPlugin("Brad522.LevelupSound", "Levelup Sound Changer", "1.0.1")]
public class LevelupSound : BaseUnityPlugin
{
	[CompilerGenerated]
	private sealed class <GrabSFX>d__9 : IEnumerator<object>, IDisposable, IEnumerator
	{
		private int <>1__state;

		private object <>2__current;

		public string path;

		public LevelupSound <>4__this;

		public Action<AudioClip> onLoaded;

		private UnityWebRequest <uwr>5__2;

		object IEnumerator<object>.Current
		{
			[DebuggerHidden]
			get
			{
				return <>2__current;
			}
		}

		object IEnumerator.Current
		{
			[DebuggerHidden]
			get
			{
				return <>2__current;
			}
		}

		[DebuggerHidden]
		public <GrabSFX>d__9(int <>1__state)
		{
			this.<>1__state = <>1__state;
		}

		[DebuggerHidden]
		void IDisposable.Dispose()
		{
			int num = <>1__state;
			if (num == -3 || num == 1)
			{
				try
				{
				}
				finally
				{
					<>m__Finally1();
				}
			}
			<uwr>5__2 = null;
			<>1__state = -2;
		}

		private bool MoveNext()
		{
			//IL_0079: Unknown result type (might be due to invalid IL or missing references)
			//IL_007e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0080: Unknown result type (might be due to invalid IL or missing references)
			//IL_00af: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ed: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f3: Invalid comparison between Unknown and I4
			try
			{
				int num = <>1__state;
				LevelupSound levelupSound = <>4__this;
				switch (num)
				{
				default:
					return false;
				case 0:
				{
					<>1__state = -1;
					path = path.Replace("\\", "/");
					string text = "file:///" + path;
					((BaseUnityPlugin)levelupSound).Logger.LogMessage((object)("Levelup Sound: Attempting to load audio file from: " + path));
					AudioType audioTypeFromExtension = levelupSound.GetAudioTypeFromExtension(Path.GetExtension(path));
					if ((int)audioTypeFromExtension == 0)
					{
						((BaseUnityPlugin)levelupSound).Logger.LogMessage((object)"Levelup Sound: Unsupported audio file type, cannot load sound.");
						onLoaded?.Invoke(null);
						return false;
					}
					<uwr>5__2 = UnityWebRequestMultimedia.GetAudioClip(text, audioTypeFromExtension);
					<>1__state = -3;
					<>2__current = <uwr>5__2.SendWebRequest();
					<>1__state = 1;
					return true;
				}
				case 1:
					<>1__state = -3;
					if ((int)<uwr>5__2.result != 1)
					{
						((BaseUnityPlugin)levelupSound).Logger.LogMessage((object)("Levelup Sound: UWR failed: " + <uwr>5__2.error));
						onLoaded?.Invoke(null);
					}
					else
					{
						onLoaded?.Invoke(DownloadHandlerAudioClip.GetContent(<uwr>5__2));
					}
					<>m__Finally1();
					<uwr>5__2 = null;
					return false;
				}
			}
			catch
			{
				//try-fault
				((IDisposable)this).Dispose();
				throw;
			}
		}

		bool IEnumerator.MoveNext()
		{
			//ILSpy generated this explicit interface implementation from .override directive in MoveNext
			return this.MoveNext();
		}

		private void <>m__Finally1()
		{
			<>1__state = -1;
			if (<uwr>5__2 != null)
			{
				((IDisposable)<uwr>5__2).Dispose();
			}
		}

		[DebuggerHidden]
		void IEnumerator.Reset()
		{
			throw new NotSupportedException();
		}
	}

	internal const string ModName = "LevelupSound";

	internal const string ModVersion = "1.0.1";

	internal const string ModDescription = "Levelup Sound Changer";

	internal const string Author = "Brad522";

	private const string ModGUID = "Brad522.LevelupSound";

	public static AudioClip lvlupSFX;

	private bool isLoaded;

	public void Awake()
	{
		((BaseUnityPlugin)this).Logger.LogMessage((object)"Levelup Sound: Started");
		string directoryName = Path.GetDirectoryName(((BaseUnityPlugin)this).Info.Location);
		string[] supportedExtensions = new string[3] { ".wav", ".mp3", ".ogg" };
		string audioFilePath = Directory.EnumerateFiles(directoryName).FirstOrDefault((string file) => supportedExtensions.Contains(Path.GetExtension(file).ToLower()));
		if (!string.IsNullOrEmpty(audioFilePath))
		{
			((MonoBehaviour)this).StartCoroutine(GrabSFX(audioFilePath, delegate(AudioClip clip)
			{
				if ((Object)(object)clip != (Object)null)
				{
					((BaseUnityPlugin)this).Logger.LogMessage((object)("Levelup Sound: Audio file loaded successfully: " + audioFilePath));
					lvlupSFX = clip;
				}
				else
				{
					((BaseUnityPlugin)this).Logger.LogMessage((object)("Levelup Sound: Failed to load audio file: " + audioFilePath));
				}
			}));
		}
		else
		{
			((BaseUnityPlugin)this).Logger.LogMessage((object)"Levelup Sound: No supported audio file found in mod directory.");
		}
	}

	public void Update()
	{
		if (!((Object)(object)lvlupSFX == (Object)null))
		{
			if (isLoaded && (Object)(object)GameData.Misc == (Object)null)
			{
				isLoaded = false;
			}
			else if (!isLoaded && (Object)(object)GameData.Misc != (Object)null)
			{
				GameData.Misc.LvlUpSFX = lvlupSFX;
				((BaseUnityPlugin)this).Logger.LogMessage((object)"Levelup Sound: Custom levelup sound changed.");
				isLoaded = true;
			}
		}
	}

	[IteratorStateMachine(typeof(<GrabSFX>d__9))]
	private IEnumerator GrabSFX(string path, Action<AudioClip> onLoaded)
	{
		//yield-return decompiler failed: Unexpected instruction in Iterator.Dispose()
		return new <GrabSFX>d__9(0)
		{
			<>4__this = this,
			path = path,
			onLoaded = onLoaded
		};
	}

	private AudioType GetAudioTypeFromExtension(string extension)
	{
		switch (extension.ToLower())
		{
		case ".wav":
			return (AudioType)20;
		case ".mp3":
			return (AudioType)13;
		case ".ogg":
			return (AudioType)14;
		default:
			((BaseUnityPlugin)this).Logger.LogMessage((object)("Levelup Sound: Unsupported audio file type: " + extension));
			return (AudioType)0;
		}
	}
}