Decompiled source of Emote Binding v1.2.0

EmoteBinding.dll

Decompiled 3 weeks ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Bootstrap;
using BepInEx.Configuration;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using PEAKEmoteLib;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("EmoteBinding")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0+a96716da768f0a127ed58feed07140a19a86a55c")]
[assembly: AssemblyProduct("EmoteBinding")]
[assembly: AssemblyTitle("EmoteBinding")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	[AttributeUsage(AttributeTargets.Class | AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Event | AttributeTargets.Parameter | AttributeTargets.ReturnValue | AttributeTargets.GenericParameter, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableAttribute : Attribute
	{
		public readonly byte[] NullableFlags;

		public NullableAttribute(byte P_0)
		{
			NullableFlags = new byte[1] { P_0 };
		}

		public NullableAttribute(byte[] P_0)
		{
			NullableFlags = P_0;
		}
	}
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Method | AttributeTargets.Interface | AttributeTargets.Delegate, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableContextAttribute : Attribute
	{
		public readonly byte Flag;

		public NullableContextAttribute(byte P_0)
		{
			Flag = P_0;
		}
	}
}
namespace EmoteBinding
{
	[BepInPlugin("com.atomic.emotebinding", "Emote Binding", "1.1.0")]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	public class Plugin : BaseUnityPlugin
	{
		public static Plugin instance;

		public static Dictionary<string, ConfigEntry<KeyCode>> EmoteBindings = new Dictionary<string, ConfigEntry<KeyCode>>();

		public static Dictionary<string, EmoteWheelData> EmoteData = new Dictionary<string, EmoteWheelData>();

		private static Type emoteRegistryType;

		private static MethodInfo getEmotesMethod;

		private bool baseEmotesRegistered = false;

		private bool customEmotesRegistered = false;

		private static readonly MethodInfo playEmoteMethod = AccessTools.Method(typeof(CharacterAnimations), "PlayEmote", (Type[])null, (Type[])null);

		private static readonly KeyCode[] DefaultKeySequence;

		private void Awake()
		{
			//IL_0033: Unknown result type (might be due to invalid IL or missing references)
			//IL_0039: Expected O, but got Unknown
			instance = this;
			if (Chainloader.PluginInfos.ContainsKey("com.github.WaporVave.PEAKEmoteLib"))
			{
				SetupEmoteRegistryReflection();
			}
			else
			{
				customEmotesRegistered = true;
			}
			Harmony val = new Harmony("com.atomic.emotebinding");
			val.PatchAll(Assembly.GetExecutingAssembly());
			((BaseUnityPlugin)this).Logger.LogInfo((object)"Plugin Awake. Waiting for GUIManager to provide Emote Wheel.");
		}

		private void SetupEmoteRegistryReflection()
		{
			try
			{
				Assembly assembly = Assembly.GetAssembly(typeof(Emote));
				if (!(assembly == null))
				{
					emoteRegistryType = assembly.GetType("PEAKEmoteLib.EmoteRegistry");
					if (emoteRegistryType != null)
					{
						getEmotesMethod = AccessTools.Method(emoteRegistryType, "GetEmotes", (Type[])null, (Type[])null);
					}
				}
			}
			catch (Exception ex)
			{
				((BaseUnityPlugin)this).Logger.LogError((object)("Reflection error on EmoteRegistry: " + ex.Message));
			}
		}

		public void RegisterEmotesFromWheel(GameObject wheelObject)
		{
			//IL_00b8: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c0: Unknown result type (might be due to invalid IL or missing references)
			if (baseEmotesRegistered || (Object)(object)wheelObject == (Object)null)
			{
				return;
			}
			EmoteWheel component = wheelObject.GetComponent<EmoteWheel>();
			if ((Object)(object)component == (Object)null || component.data == null)
			{
				RegisterDefaultEmotesFallback();
				return;
			}
			int num = 0;
			EmoteWheelData[] data = component.data;
			foreach (EmoteWheelData val in data)
			{
				if (!((Object)(object)val == (Object)null) && !string.IsNullOrEmpty(val.emoteName) && !EmoteBindings.ContainsKey(val.emoteName))
				{
					KeyCode defaultKeycode = (KeyCode)((num < DefaultKeySequence.Length) ? ((int)DefaultKeySequence[num++]) : 0);
					RegisterBaseEmoteBinding("Base Game Emotes", defaultKeycode, val.emoteName, val.anim);
				}
			}
			baseEmotesRegistered = true;
			((BaseUnityPlugin)this).Logger.LogInfo((object)"Registered base emotes directly from GUIManager.");
		}

		private void RegisterCustomEmotes()
		{
			if (getEmotesMethod == null || customEmotesRegistered || !(getEmotesMethod.Invoke(null, null) is IReadOnlyDictionary<string, Emote> readOnlyDictionary))
			{
				return;
			}
			foreach (KeyValuePair<string, Emote> item in readOnlyDictionary)
			{
				Emote value = item.Value;
				if (value != null && !string.IsNullOrEmpty(value.Name) && !EmoteBindings.ContainsKey(value.Name))
				{
					ConfigEntry<KeyCode> value2 = ((BaseUnityPlugin)this).Config.Bind<KeyCode>("Custom Emotes", value.Name, (KeyCode)0, "Key binding for: " + value.Name);
					EmoteBindings[value.Name] = value2;
					EmoteWheelData val = ScriptableObject.CreateInstance<EmoteWheelData>();
					val.emoteName = value.Name;
					val.anim = value.Name;
					EmoteData[value.Name] = val;
				}
			}
			customEmotesRegistered = true;
		}

		private void RegisterDefaultEmotesFallback()
		{
			if (!baseEmotesRegistered)
			{
				baseEmotesRegistered = true;
				((BaseUnityPlugin)this).Logger.LogWarning((object)"Using fallback emote list.");
			}
		}

		private void RegisterBaseEmoteBinding(string section, KeyCode defaultKeycode, string emoteName, string animName)
		{
			//IL_0009: Unknown result type (might be due to invalid IL or missing references)
			ConfigEntry<KeyCode> value = ((BaseUnityPlugin)this).Config.Bind<KeyCode>(section, emoteName, defaultKeycode, "Key binding for: " + emoteName);
			EmoteBindings[emoteName] = value;
			EmoteWheelData val = ScriptableObject.CreateInstance<EmoteWheelData>();
			val.emoteName = emoteName;
			val.anim = animName;
			EmoteData[emoteName] = val;
		}

		private void Update()
		{
			//IL_005f: Unknown result type (might be due to invalid IL or missing references)
			//IL_006d: Unknown result type (might be due to invalid IL or missing references)
			if (!customEmotesRegistered)
			{
				RegisterCustomEmotes();
			}
			if ((Object)(object)Character.localCharacter?.refs?.animations == (Object)null)
			{
				return;
			}
			foreach (KeyValuePair<string, ConfigEntry<KeyCode>> emoteBinding in EmoteBindings)
			{
				if ((int)emoteBinding.Value.Value != 0 && Input.GetKeyDown(emoteBinding.Value.Value) && EmoteData.TryGetValue(emoteBinding.Key, out EmoteWheelData value))
				{
					TryPlayEmote(value.anim);
				}
			}
		}

		public static void TryPlayEmote(string animName)
		{
			CharacterAnimations val = Character.localCharacter?.refs?.animations;
			if ((Object)(object)val != (Object)null && playEmoteMethod != null)
			{
				playEmoteMethod.Invoke(val, new object[1] { animName });
			}
		}

		static Plugin()
		{
			KeyCode[] array = new KeyCode[8];
			RuntimeHelpers.InitializeArray(array, (RuntimeFieldHandle)/*OpCode not supported: LdMemberToken*/);
			DefaultKeySequence = (KeyCode[])(object)array;
		}
	}
	[HarmonyPatch]
	public class Patches
	{
		[HarmonyPostfix]
		[HarmonyPatch(typeof(GUIManager), "Awake")]
		public static void RegEmoteWheel(GUIManager __instance)
		{
			Plugin.instance.RegisterEmotesFromWheel(__instance.emoteWheel);
		}
	}
}