Decompiled source of Stronger Than You Character Themes v1.1.1

CharacterThemeSelector.dll

Decompiled a month ago
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
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("CharacterThemeSelector")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("CharacterThemeSelector")]
[assembly: AssemblyTitle("CharacterThemeSelector")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace CharacterThemeSelector;

[BepInPlugin("jaxon.electoralcarnage.characterthemes", "Stronger Than You Character Themes", "1.1.1")]
public sealed class Plugin : BaseUnityPlugin
{
	internal static Plugin Instance;

	internal static ManualLogSource Log;

	private readonly Dictionary<Theme, AudioClip> _clips = new Dictionary<Theme, AudioClip>();

	private Harmony _harmony;

	private AudioSource _music;

	private AudioClip _originalClip;

	private Theme _selected;

	private bool _applied;

	private Dictionary<string, string> _themePaths;

	private ConfigEntry<bool> _enableLincoln;

	private ConfigEntry<bool> _enableLincolnVsJfk;

	private ConfigEntry<bool> _enableJfk;

	private ConfigEntry<bool> _enableWashington;

	private ConfigEntry<bool> _enableNixon;

	private ConfigEntry<bool> _enableObama;

	private ConfigEntry<bool> _enableTeddy;

	private ConfigEntry<bool> _enableJfkMirror;

	private ConfigEntry<bool> _enableTrumpMirror;

	private ConfigEntry<bool> _enableTrump;

	private ConfigEntry<bool> _enableRareLincoln;

	private ConfigEntry<float> _rareLincolnChance;

	private void Awake()
	{
		//IL_001d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0027: Expected O, but got Unknown
		Instance = this;
		Log = ((BaseUnityPlugin)this).Logger;
		BindConfiguration();
		_harmony = new Harmony("jaxon.electoralcarnage.characterthemes");
		_harmony.PatchAll();
		((MonoBehaviour)this).StartCoroutine(LoadAllThemes());
		((BaseUnityPlugin)this).Logger.LogInfo((object)"Stronger Than You Character Themes 1.1.1 loaded");
	}

	private void BindConfiguration()
	{
		//IL_0163: Unknown result type (might be due to invalid IL or missing references)
		//IL_016d: Expected O, but got Unknown
		_enableLincoln = SongSetting("01 - Lincoln", "EnableLincoln", value: true, "Enable the normal Lincoln song.");
		_enableLincolnVsJfk = SongSetting("02 - Lincoln vs JFK", "EnableLincolnVsJFK", value: true, "Enable the Lincoln versus JFK matchup song.");
		_enableJfk = SongSetting("03 - JFK", "EnableJFK", value: true, "Enable the normal JFK song.");
		_enableWashington = SongSetting("04 - Washington", "EnableWashington", value: true, "Enable the Washington song.");
		_enableNixon = SongSetting("05 - Nixon", "EnableNixon", value: true, "Enable the Nixon song.");
		_enableObama = SongSetting("06 - Obama", "EnableObama", value: true, "Enable the Obama song.");
		_enableTeddy = SongSetting("07 - Teddy", "EnableTeddy", value: true, "Enable the Teddy and RoboTeddy song.");
		_enableJfkMirror = SongSetting("08 - JFK vs JFK", "EnableJFKMirror", value: true, "Enable the JFK mirror-match song.");
		_enableTrumpMirror = SongSetting("09 - Trump vs Trump", "EnableTrumpMirror", value: true, "Enable the Trump mirror-match song.");
		_enableTrump = SongSetting("10 - Trump", "EnableTrump", value: true, "Enable the normal single-Trump song.");
		_enableRareLincoln = SongSetting("11 - Rare Lincoln Cats", "EnableRareLincolnCats", value: true, "Allow the rare Cats of Abraham Lincoln replacement.");
		_rareLincolnChance = ((BaseUnityPlugin)this).Config.Bind<float>("11 - Rare Lincoln Cats", "ChancePercent", 5f, new ConfigDescription("Chance from 0 to 100 that a normal Lincoln match uses the Cats version.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0f, 100f), Array.Empty<object>()));
	}

	private ConfigEntry<bool> SongSetting(string section, string key, bool value, string description)
	{
		return ((BaseUnityPlugin)this).Config.Bind<bool>(section, key, value, description);
	}

	private IEnumerator LoadAllThemes()
	{
		BuildThemePathIndex();
		ThemeFile[] all = ThemeFiles.All;
		foreach (ThemeFile entry in all)
		{
			yield return LoadTheme(entry);
		}
		((BaseUnityPlugin)this).Logger.LogInfo((object)("Loaded " + _clips.Count + "/11 character themes."));
		TryApply();
	}

	private IEnumerator LoadTheme(ThemeFile entry)
	{
		string text = ResolveThemePath(entry.FileName);
		if (text == null)
		{
			((BaseUnityPlugin)this).Logger.LogError((object)("Missing theme file after searching the DLL folder and BepInEx/plugins: " + entry.FileName));
			yield break;
		}
		((BaseUnityPlugin)this).Logger.LogInfo((object)("Loading " + entry.FileName + " from " + text));
		UnityWebRequest request = UnityWebRequestMultimedia.GetAudioClip(new Uri(text).AbsoluteUri, (AudioType)13);
		try
		{
			yield return request.SendWebRequest();
			if ((int)request.result != 1)
			{
				((BaseUnityPlugin)this).Logger.LogError((object)("Could not load " + entry.FileName + ": " + request.error));
				yield break;
			}
			AudioClip content = DownloadHandlerAudioClip.GetContent(request);
			((Object)content).name = entry.Theme.ToString();
			_clips[entry.Theme] = content;
		}
		finally
		{
			((IDisposable)request)?.Dispose();
		}
	}

	private void BuildThemePathIndex()
	{
		_themePaths = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
		string directoryName = Path.GetDirectoryName(((BaseUnityPlugin)this).Info.Location);
		IndexFolder(directoryName);
		if (!string.Equals(directoryName, Paths.PluginPath, StringComparison.OrdinalIgnoreCase))
		{
			IndexFolder(Paths.PluginPath);
		}
		((BaseUnityPlugin)this).Logger.LogInfo((object)("Audio search roots: DLL=" + directoryName + " | Plugins=" + Paths.PluginPath));
	}

	private void IndexFolder(string root)
	{
		if (string.IsNullOrEmpty(root) || !Directory.Exists(root))
		{
			return;
		}
		try
		{
			string[] files = Directory.GetFiles(root, "*.mp3", SearchOption.AllDirectories);
			for (int i = 0; i < files.Length; i++)
			{
				string fileName = Path.GetFileName(files[i]);
				if (!_themePaths.ContainsKey(fileName))
				{
					_themePaths[fileName] = files[i];
				}
			}
		}
		catch (Exception ex)
		{
			((BaseUnityPlugin)this).Logger.LogWarning((object)("Could not scan audio folder " + root + ": " + ex.Message));
		}
	}

	private string ResolveThemePath(string fileName)
	{
		string directoryName = Path.GetDirectoryName(((BaseUnityPlugin)this).Info.Location);
		string[] array = new string[4]
		{
			Path.Combine(directoryName, "Themes", fileName),
			Path.Combine(directoryName, fileName),
			Path.Combine(Paths.PluginPath, "Themes", fileName),
			Path.Combine(Paths.PluginPath, fileName)
		};
		for (int i = 0; i < array.Length; i++)
		{
			if (File.Exists(array[i]))
			{
				return array[i];
			}
		}
		if (_themePaths == null || !_themePaths.TryGetValue(fileName, out var value))
		{
			return null;
		}
		return value;
	}

	internal void SelectMatch(string p1, string p2)
	{
		_selected = ChooseTheme(p1, p2);
		_applied = false;
		((BaseUnityPlugin)this).Logger.LogInfo((object)("Match theme selection: " + p1 + " vs " + p2 + " -> " + _selected));
		TryApply();
	}

	private Theme ChooseTheme(string p1, string p2)
	{
		bool flag = Is(p1, "Lincoln");
		bool flag2 = Is(p2, "Lincoln");
		bool flag3 = Is(p1, "JFK");
		bool flag4 = Is(p2, "JFK");
		bool flag5 = IsTrump(p1);
		bool flag6 = IsTrump(p2);
		if (((flag && flag4) || (flag2 && flag3)) && _enableLincolnVsJfk.Value)
		{
			return Theme.LincolnVsJfk;
		}
		if (flag3 && flag4 && _enableJfkMirror.Value)
		{
			return Theme.JfkMirror;
		}
		if (flag5 && flag6 && _enableTrumpMirror.Value)
		{
			return Theme.TrumpMirror;
		}
		if (flag || flag2)
		{
			float num = Mathf.Clamp(_rareLincolnChance.Value, 0f, 100f) / 100f;
			if (_enableRareLincoln.Value && Random.value < num)
			{
				return Theme.RareLincoln;
			}
			if (_enableLincoln.Value)
			{
				return Theme.Lincoln;
			}
		}
		if ((flag3 || flag4) && _enableJfk.Value)
		{
			return Theme.Jfk;
		}
		if ((Is(p1, "Washington") || Is(p2, "Washington")) && _enableWashington.Value)
		{
			return Theme.Washington;
		}
		if ((Is(p1, "Nixon") || Is(p2, "Nixon")) && _enableNixon.Value)
		{
			return Theme.Nixon;
		}
		if ((IsObama(p1) || IsObama(p2)) && _enableObama.Value)
		{
			return Theme.Obama;
		}
		if ((IsTeddy(p1) || IsTeddy(p2)) && _enableTeddy.Value)
		{
			return Theme.Teddy;
		}
		if ((flag5 || flag6) && _enableTrump.Value)
		{
			return Theme.Trump;
		}
		return Theme.None;
	}

	private static bool Is(string id, string wanted)
	{
		return string.Equals(id, wanted, StringComparison.OrdinalIgnoreCase);
	}

	private static bool IsObama(string id)
	{
		if (!Is(id, "Obama"))
		{
			return Is(id, "Obama TA");
		}
		return true;
	}

	private static bool IsTeddy(string id)
	{
		if (!Is(id, "Teddy") && !Is(id, "BoxerTeddy"))
		{
			return Is(id, "RoboTeddy");
		}
		return true;
	}

	private static bool IsTrump(string id)
	{
		if (!Is(id, "Trump") && !Is(id, "MGT"))
		{
			return Is(id, "Trump (Magical Girl)");
		}
		return true;
	}

	private void Update()
	{
		if (!_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;
		}
		AudioClip value;
		if (_selected == Theme.None)
		{
			if ((Object)(object)_originalClip != (Object)null && (Object)(object)component.clip != (Object)(object)_originalClip)
			{
				component.Stop();
				component.clip = _originalClip;
				component.loop = true;
				component.Play();
			}
			_applied = true;
		}
		else if (_clips.TryGetValue(_selected, out value))
		{
			if ((Object)(object)component.clip != (Object)(object)value)
			{
				component.Stop();
				component.clip = value;
				component.loop = true;
				component.Play();
			}
			_applied = true;
			((BaseUnityPlugin)this).Logger.LogInfo((object)("Playing theme: " + _selected));
		}
	}

	private void OnDestroy()
	{
		if (_harmony != null)
		{
			_harmony.UnpatchSelf();
		}
	}
}
internal enum Theme
{
	None,
	Lincoln,
	LincolnVsJfk,
	Jfk,
	Washington,
	Nixon,
	Obama,
	Teddy,
	JfkMirror,
	TrumpMirror,
	Trump,
	RareLincoln
}
internal sealed class ThemeFile
{
	internal readonly Theme Theme;

	internal readonly string FileName;

	internal ThemeFile(Theme theme, string fileName)
	{
		Theme = theme;
		FileName = fileName;
	}
}
internal static class ThemeFiles
{
	internal static readonly ThemeFile[] All = new ThemeFile[11]
	{
		new ThemeFile(Theme.Lincoln, "01_lincoln.mp3"),
		new ThemeFile(Theme.LincolnVsJfk, "02_lincoln_vs_jfk.mp3"),
		new ThemeFile(Theme.Jfk, "03_jfk.mp3"),
		new ThemeFile(Theme.Washington, "04_washington.mp3"),
		new ThemeFile(Theme.Nixon, "05_nixon.mp3"),
		new ThemeFile(Theme.Obama, "06_obama.mp3"),
		new ThemeFile(Theme.Teddy, "07_teddy.mp3"),
		new ThemeFile(Theme.JfkMirror, "08_jfk_mirror.mp3"),
		new ThemeFile(Theme.TrumpMirror, "09_trump_mirror.mp3"),
		new ThemeFile(Theme.Trump, "10_trump.mp3"),
		new ThemeFile(Theme.RareLincoln, "11_rare_lincoln.mp3")
	};
}
[HarmonyPatch(typeof(bigEnabler), "FixedUpdate")]
internal static class MatchSelectionPatch
{
	private static void Prefix(bigEnabler __instance)
	{
		if (__instance.doEnable && __instance.timer > 10 && !((Object)(object)Plugin.Instance == (Object)null))
		{
			Plugin.Instance.SelectMatch(CharacterId(__instance.p1asset), CharacterId(__instance.p2asset));
		}
	}

	private static string CharacterId(GameObject prefab)
	{
		if ((Object)(object)prefab == (Object)null)
		{
			return "Unknown";
		}
		PlayerInfo val = prefab.GetComponent<PlayerInfo>();
		if ((Object)(object)val == (Object)null)
		{
			val = prefab.GetComponentInChildren<PlayerInfo>(true);
		}
		if (!((Object)(object)val == (Object)null) && !string.IsNullOrEmpty(val.characterID))
		{
			return val.characterID;
		}
		return ((Object)prefab).name;
	}
}