Decompiled source of Avatariya Shop Music v1.0.0

plugins/AvatariyaShopMusic/AvatariyaShopMusic.dll

Decompiled 2 days 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.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.SceneManagement;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: AssemblyCompany("AvatariyaShopMusic")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0+0d77aba4ef99a87d6be4daeb81b0de1f2e0c0dca")]
[assembly: AssemblyProduct("AvatariyaShopMusic")]
[assembly: AssemblyTitle("AvatariyaShopMusic")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.0.0")]
[module: UnverifiableCode]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	[AttributeUsage(AttributeTargets.Module, AllowMultiple = false, Inherited = false)]
	internal sealed class RefSafetyRulesAttribute : Attribute
	{
		public readonly int Version;

		public RefSafetyRulesAttribute(int P_0)
		{
			Version = P_0;
		}
	}
}
namespace AvatariyaShopMusic
{
	[BepInPlugin("headclef.avatariyashopmusic", "Avatariya Shop Music", "1.0.0")]
	public class Plugin : BaseUnityPlugin
	{
		[CompilerGenerated]
		private sealed class <LoadAudioAndPatch>d__11 : IEnumerator<object>, IDisposable, IEnumerator
		{
			private int <>1__state;

			private object <>2__current;

			public Plugin <>4__this;

			private string[] <trackPaths>5__2;

			private int <i>5__3;

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

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

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

			[DebuggerHidden]
			void IDisposable.Dispose()
			{
				<trackPaths>5__2 = null;
				<>1__state = -2;
			}

			private bool MoveNext()
			{
				//IL_0203: Unknown result type (might be due to invalid IL or missing references)
				//IL_020d: Expected O, but got Unknown
				//IL_0244: Unknown result type (might be due to invalid IL or missing references)
				//IL_0252: Expected O, but got Unknown
				int num = <>1__state;
				Plugin plugin = <>4__this;
				if (num != 0)
				{
					if (num != 1)
					{
						return false;
					}
					<>1__state = -1;
					goto IL_0186;
				}
				<>1__state = -1;
				string text = Path.Combine(Path.GetDirectoryName(((BaseUnityPlugin)plugin).Info.Location) ?? string.Empty, "tracks");
				LoadedClips.Clear();
				AudioReady = false;
				if (!Directory.Exists(text))
				{
					Log.LogError((object)("[AvatariyaShopMusic] Tracks folder not found at: " + text));
					return false;
				}
				<trackPaths>5__2 = (from path in Directory.GetFiles(text, "*.*", SearchOption.TopDirectoryOnly)
					where path.EndsWith(".ogg", StringComparison.OrdinalIgnoreCase) || path.EndsWith(".mp3", StringComparison.OrdinalIgnoreCase)
					select path).ToArray();
				Array.Sort(<trackPaths>5__2, delegate(string a, string b)
				{
					string text3 = FormatTrackName(Path.GetFileName(a));
					string strB = FormatTrackName(Path.GetFileName(b));
					return text3.CompareTo(strB);
				});
				plugin._trackConfigs = new ConfigEntry<bool>[<trackPaths>5__2.Length];
				for (int i = 0; i < <trackPaths>5__2.Length; i++)
				{
					string text2 = FormatTrackName(Path.GetFileName(<trackPaths>5__2[i]));
					plugin._trackConfigs[i] = ((BaseUnityPlugin)plugin).Config.Bind<bool>("Tracks", text2, true, "Enable " + text2);
				}
				<i>5__3 = 0;
				goto IL_0198;
				IL_0198:
				if (<i>5__3 < <trackPaths>5__2.Length)
				{
					if (plugin._trackConfigs[<i>5__3].Value)
					{
						<>2__current = plugin.LoadClip(<trackPaths>5__2[<i>5__3]);
						<>1__state = 1;
						return true;
					}
					goto IL_0186;
				}
				AudioReady = LoadedClips.Count > 0;
				if (!AudioReady)
				{
					Log.LogError((object)"[AvatariyaShopMusic] Failed to load audio tracks!");
					return false;
				}
				Log.LogInfo((object)$"[AvatariyaShopMusic] Successfully loaded {LoadedClips.Count} shop music tracks");
				if (plugin._harmony == null)
				{
					plugin._harmony = new Harmony("headclef.avatariyashopmusic");
				}
				MethodInfo method = typeof(ShopRadio).GetMethod("Start", BindingFlags.Instance | BindingFlags.NonPublic);
				if (method != null)
				{
					plugin._harmony.Patch((MethodBase)method, new HarmonyMethod(typeof(Plugin), "PatchShopRadioStart", (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
					Log.LogInfo((object)"[AvatariyaShopMusic] Successfully patched ShopRadio.Start()");
				}
				else
				{
					Log.LogError((object)"[AvatariyaShopMusic] Failed to find ShopRadio.Start() method");
				}
				SceneManager.sceneLoaded += plugin.OnSceneLoaded;
				SceneManager.sceneUnloaded += plugin.OnSceneUnloaded;
				return false;
				IL_0186:
				<i>5__3++;
				goto IL_0198;
			}

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

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

		[CompilerGenerated]
		private sealed class <LoadClip>d__12 : IEnumerator<object>, IDisposable, IEnumerator
		{
			private int <>1__state;

			private object <>2__current;

			public string clipPath;

			private string <fileName>5__2;

			private UnityWebRequest <request>5__3;

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

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

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

			[DebuggerHidden]
			void IDisposable.Dispose()
			{
				<fileName>5__2 = null;
				<request>5__3 = null;
				<>1__state = -2;
			}

			private bool MoveNext()
			{
				//IL_0085: Unknown result type (might be due to invalid IL or missing references)
				//IL_008b: Invalid comparison between Unknown and I4
				//IL_0041: Unknown result type (might be due to invalid IL or missing references)
				//IL_0053: Unknown result type (might be due to invalid IL or missing references)
				switch (<>1__state)
				{
				default:
					return false;
				case 0:
				{
					<>1__state = -1;
					<fileName>5__2 = Path.GetFileName(clipPath);
					AudioType val = (AudioType)(clipPath.EndsWith(".mp3", StringComparison.OrdinalIgnoreCase) ? 13 : 14);
					<request>5__3 = UnityWebRequestMultimedia.GetAudioClip("file://" + clipPath, val);
					<>2__current = <request>5__3.SendWebRequest();
					<>1__state = 1;
					return true;
				}
				case 1:
				{
					<>1__state = -1;
					if ((int)<request>5__3.result != 1)
					{
						Log.LogError((object)("[AvatariyaShopMusic] Failed to load " + <fileName>5__2 + ": " + <request>5__3.error));
						return false;
					}
					AudioClip content = DownloadHandlerAudioClip.GetContent(<request>5__3);
					if ((Object)(object)content == (Object)null)
					{
						Log.LogError((object)("[AvatariyaShopMusic] Got null clip for " + <fileName>5__2));
						return false;
					}
					((Object)content).name = <fileName>5__2;
					LoadedClips.Add(content);
					return false;
				}
				}
			}

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

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

		internal static Plugin Instance = null;

		private const string PluginGuid = "headclef.avatariyashopmusic";

		private const string PluginName = "Avatariya Shop Music";

		private const string PluginVersion = "1.0.0";

		internal static ManualLogSource Log = null;

		internal static readonly List<AudioClip> LoadedClips = new List<AudioClip>();

		internal static bool AudioReady;

		private Harmony _harmony;

		private static readonly HashSet<int> ReplacedRadioIds = new HashSet<int>();

		private ConfigEntry<bool>[] _trackConfigs;

		private void Awake()
		{
			Instance = this;
			Log = ((BaseUnityPlugin)this).Logger;
			((MonoBehaviour)this).StartCoroutine(LoadAudioAndPatch());
		}

		[IteratorStateMachine(typeof(<LoadAudioAndPatch>d__11))]
		private IEnumerator LoadAudioAndPatch()
		{
			//yield-return decompiler failed: Unexpected instruction in Iterator.Dispose()
			return new <LoadAudioAndPatch>d__11(0)
			{
				<>4__this = this
			};
		}

		[IteratorStateMachine(typeof(<LoadClip>d__12))]
		private IEnumerator LoadClip(string clipPath)
		{
			//yield-return decompiler failed: Unexpected instruction in Iterator.Dispose()
			return new <LoadClip>d__12(0)
			{
				clipPath = clipPath
			};
		}

		private void OnDestroy()
		{
			SceneManager.sceneLoaded -= OnSceneLoaded;
			SceneManager.sceneUnloaded -= OnSceneUnloaded;
			Harmony harmony = _harmony;
			if (harmony != null)
			{
				harmony.UnpatchSelf();
			}
		}

		private void OnSceneLoaded(Scene scene, LoadSceneMode mode)
		{
			ReplacedRadioIds.Clear();
		}

		private void OnSceneUnloaded(Scene scene)
		{
			ReplacedRadioIds.Clear();
		}

		private static string FormatTrackName(string fileName)
		{
			string text = Path.GetFileNameWithoutExtension(fileName).Replace("-", " ");
			if (text.Length > 0)
			{
				text = char.ToUpper(text[0]) + text.Substring(1);
			}
			return text;
		}

		private static void PatchShopRadioStart(ShopRadio __instance)
		{
			if (AudioReady && LoadedClips.Count != 0 && __instance?.shopMusicClips != null)
			{
				int instanceID = ((Object)__instance).GetInstanceID();
				if (!ReplacedRadioIds.Contains(instanceID))
				{
					__instance.shopMusicClips.Clear();
					__instance.shopMusicClips.AddRange(LoadedClips);
					ReplacedRadioIds.Add(instanceID);
					Log.LogInfo((object)"[AvatariyaShopMusic] Replaced shop radio on Start()");
				}
			}
		}
	}
}