Decompiled source of LinconStrongerThenYouThemeReplacer v1.0.0

Lincon stronger then you theme replacer.dll

Decompiled 2 weeks ago
using System;
using System.Collections;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using UnityEngine;
using UnityEngine.Networking;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("Lincon stronger then you theme replacer")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("Lincon stronger then you theme replacer")]
[assembly: AssemblyTitle("Lincon stronger then you theme replacer")]
[assembly: AssemblyVersion("1.0.0.0")]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[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 LincolnTheme
{
	[BepInPlugin("jaxon.electoralcarnage.lincolntheme", "Lincon stronger then you theme replacer", "1.0.0")]
	public sealed class Plugin : BaseUnityPlugin
	{
		internal static Plugin Instance;

		internal static ManualLogSource Log;

		private Harmony _harmony;

		private AudioClip _lincolnClip;

		private AudioSource _music;

		private AudioClip _originalClip;

		private bool _lincolnMatch;

		private bool _applied;

		private void Awake()
		{
			//IL_0017: Unknown result type (might be due to invalid IL or missing references)
			//IL_0021: Expected O, but got Unknown
			Instance = this;
			Log = ((BaseUnityPlugin)this).Logger;
			_harmony = new Harmony("jaxon.electoralcarnage.lincolntheme");
			_harmony.PatchAll();
			((MonoBehaviour)this).StartCoroutine(LoadTheme());
			((BaseUnityPlugin)this).Logger.LogInfo((object)"Lincon stronger then you theme replacer 1.0.0 loaded");
		}

		private IEnumerator LoadTheme()
		{
			string directoryName = Path.GetDirectoryName(((BaseUnityPlugin)this).Info.Location);
			string[] array = new string[3] { "lincon stronger then you.ogg", "lincon stronger then you.mp3", "lincon stronger then you.wav" };
			string path = null;
			AudioType val = (AudioType)0;
			for (int i = 0; i < array.Length; i++)
			{
				string text = Path.Combine(directoryName, array[i]);
				if (File.Exists(text))
				{
					path = text;
					val = (AudioType)(array[i].EndsWith(".ogg") ? 14 : (array[i].EndsWith(".wav") ? 20 : 13));
					break;
				}
			}
			if (path == null)
			{
				((BaseUnityPlugin)this).Logger.LogWarning((object)"No Lincon theme found. Put lincon stronger then you.ogg, .mp3, or .wav beside the mod DLL.");
				yield break;
			}
			UnityWebRequest request = UnityWebRequestMultimedia.GetAudioClip(new Uri(path).AbsoluteUri, val);
			try
			{
				yield return request.SendWebRequest();
				if ((int)request.result != 1)
				{
					((BaseUnityPlugin)this).Logger.LogError((object)("Could not load Lincoln theme: " + request.error));
					yield break;
				}
				_lincolnClip = DownloadHandlerAudioClip.GetContent(request);
				((Object)_lincolnClip).name = "Lincon stronger then you";
				((BaseUnityPlugin)this).Logger.LogInfo((object)("Loaded Lincoln theme: " + Path.GetFileName(path)));
			}
			finally
			{
				((IDisposable)request)?.Dispose();
			}
			TryApply();
		}

		internal void SetMatchSelection(bool hasLincoln)
		{
			_lincolnMatch = hasLincoln;
			_applied = false;
			TryApply();
		}

		private void Update()
		{
			if (_lincolnMatch && !_applied)
			{
				TryApply();
			}
		}

		private void TryApply()
		{
			GameObject val = GameObject.Find("Music");
			if ((Object)(object)val == (Object)null)
			{
				return;
			}
			AudioSource component = val.GetComponent<AudioSource>();
			if ((Object)(object)component == (Object)null)
			{
				return;
			}
			if ((Object)(object)_music != (Object)(object)component)
			{
				_music = component;
				_originalClip = component.clip;
			}
			if (_lincolnMatch)
			{
				if (!((Object)(object)_lincolnClip == (Object)null))
				{
					if ((Object)(object)component.clip != (Object)(object)_lincolnClip)
					{
						component.Stop();
						component.clip = _lincolnClip;
						component.loop = true;
						component.Play();
					}
					_applied = true;
					((BaseUnityPlugin)this).Logger.LogInfo((object)"Playing custom Lincoln match theme.");
				}
			}
			else if ((Object)(object)_originalClip != (Object)null && (Object)(object)component.clip == (Object)(object)_lincolnClip)
			{
				component.Stop();
				component.clip = _originalClip;
				component.loop = true;
				component.Play();
				_applied = true;
			}
		}

		private void OnDestroy()
		{
			if (_harmony != null)
			{
				_harmony.UnpatchSelf();
			}
		}
	}
	[HarmonyPatch(typeof(bigEnabler), "FixedUpdate")]
	internal static class MatchMusicSelectionPatch
	{
		private static void Prefix(bigEnabler __instance)
		{
			if (__instance.doEnable && __instance.timer > 10 && !((Object)(object)Plugin.Instance == (Object)null))
			{
				Plugin.Instance.SetMatchSelection(IsLincoln(__instance.p1asset) || IsLincoln(__instance.p2asset));
			}
		}

		private static bool IsLincoln(GameObject prefab)
		{
			if ((Object)(object)prefab == (Object)null)
			{
				return false;
			}
			PlayerInfo val = prefab.GetComponent<PlayerInfo>();
			if ((Object)(object)val == (Object)null)
			{
				val = prefab.GetComponentInChildren<PlayerInfo>(true);
			}
			if ((Object)(object)val != (Object)null)
			{
				return string.Equals(val.characterID, "Lincoln", StringComparison.OrdinalIgnoreCase);
			}
			return false;
		}
	}
}