Decompiled source of Emergency Dice REPOED Fixed v1.0.0

EmergencyDiceREPOEDFixed.dll

Decompiled 7 hours ago
using System;
using System.Collections;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
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(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyCompany("EmergencyDiceREPOEDFixed")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.1.0")]
[assembly: AssemblyProduct("EmergencyDiceREPOEDFixed")]
[assembly: AssemblyTitle("EmergencyDiceREPOEDFixed")]
[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.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]
	[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;
		}
	}
	[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 Matsu.EmergencyDiceREPOEDFixed
{
	[BepInPlugin("matsu.repo.emergency_dice_fixed", "Emergency Dice REPOED Fixed", "1.0.0")]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	public class Plugin : BaseUnityPlugin
	{
		public static ManualLogSource? Log;

		private static FieldInfo? _f_isRolling;

		private static FieldInfo? _f_itemToggle;

		private static FieldInfo? _f_prevToggleState;

		private static FieldInfo? _f_rolledFromE;

		private static FieldInfo? _f_physGrabObject;

		private static FieldInfo? _f_view;

		private static FieldInfo? _f_rb;

		private static FieldInfo? _f_lastHolder;

		private static MethodInfo? _m_blockPickupAndRollRPC;

		private static FieldInfo? _f_toggleState;

		private static FieldInfo? _f_grabbed;

		private static FieldInfo? _f_playerGrabbing;

		private static FieldInfo? _f_playerAvatar;

		private static FieldInfo? _f_rmStaticInstance;

		private static FieldInfo? _f_levelIsShop;

		private static Type? _t_runManager;

		private static MethodInfo? _m_isMaster;

		private static FieldInfo? _f_throwSpeed;

		private static MethodInfo? _m_photonRPC;

		private static Type? _t_rpcTarget;

		private static object? _v_masterClient;

		private static PropertyInfo? _p_effectName;

		private static Type? _t_chatManager;

		private static FieldInfo? _f_chatManagerInstance;

		private static MethodInfo? _m_forceSendMessage;

		private void Awake()
		{
			//IL_0011: Unknown result type (might be due to invalid IL or missing references)
			//IL_0017: Expected O, but got Unknown
			Log = ((BaseUnityPlugin)this).Logger;
			Harmony val = new Harmony("matsu.repo.emergency_dice_fixed");
			val.PatchAll();
			HookRepoDiceLoad();
			((BaseUnityPlugin)this).Logger.LogInfo((object)"Emergency Dice REPOED Fixed v1.0.0 loaded.");
		}

		private void HookRepoDiceLoad()
		{
			Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
			foreach (Assembly assembly in assemblies)
			{
				if (assembly.GetName().Name == "RepoDice")
				{
					ApplyPatch(assembly);
					return;
				}
			}
			AppDomain.CurrentDomain.AssemblyLoad += delegate(object _, AssemblyLoadEventArgs args)
			{
				if (args.LoadedAssembly.GetName().Name == "RepoDice")
				{
					ApplyPatch(args.LoadedAssembly);
				}
			};
		}

		private static FieldInfo? FindField(Type t, string name)
		{
			Type type = t;
			while (type != null)
			{
				FieldInfo field = type.GetField(name, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
				if (field != null)
				{
					return field;
				}
				type = type.BaseType;
			}
			return null;
		}

		private static void ApplyPatch(Assembly repoDiceAsm)
		{
			//IL_0410: Unknown result type (might be due to invalid IL or missing references)
			//IL_041e: Expected O, but got Unknown
			//IL_0338: Unknown result type (might be due to invalid IL or missing references)
			//IL_0341: Unknown result type (might be due to invalid IL or missing references)
			//IL_034f: Expected O, but got Unknown
			//IL_0396: Unknown result type (might be due to invalid IL or missing references)
			//IL_039d: Expected O, but got Unknown
			try
			{
				_t_chatManager = AccessTools.TypeByName("ChatManager");
				if (_t_chatManager != null)
				{
					_f_chatManagerInstance = _t_chatManager.GetField("instance", BindingFlags.Static | BindingFlags.Public);
					_m_forceSendMessage = _t_chatManager.GetMethod("ForceSendMessage", BindingFlags.Instance | BindingFlags.Public);
				}
				Type type = repoDiceAsm.GetType("RepoDice.Dice.DieBehaviour");
				if (type == null)
				{
					ManualLogSource? log = Log;
					if (log != null)
					{
						log.LogWarning((object)"[Fixed] DieBehaviour type not found");
					}
					return;
				}
				_f_isRolling = FindField(type, "isRolling");
				_f_itemToggle = FindField(type, "itemToggle");
				_f_prevToggleState = FindField(type, "prevToggleState");
				_f_rolledFromE = FindField(type, "rolledFromE");
				_f_physGrabObject = FindField(type, "physGrabObject");
				_f_view = FindField(type, "view");
				_f_rb = FindField(type, "rb");
				_f_lastHolder = FindField(type, "lastHolder");
				_m_blockPickupAndRollRPC = type.GetMethod("BlockPickupAndRollRPC", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
				Type type2 = _f_itemToggle?.FieldType;
				if (type2 != null)
				{
					_f_toggleState = FindField(type2, "toggleState");
				}
				Type type3 = _f_physGrabObject?.FieldType;
				if (type3 != null)
				{
					_f_grabbed = FindField(type3, "grabbed");
					_f_playerGrabbing = FindField(type3, "playerGrabbing");
				}
				_t_runManager = AccessTools.TypeByName("RunManager");
				if (_t_runManager != null)
				{
					_f_rmStaticInstance = _t_runManager.GetField("instance", BindingFlags.Static | BindingFlags.Public) ?? _t_runManager.GetField("Instance", BindingFlags.Static | BindingFlags.Public);
					_f_levelIsShop = _t_runManager.GetField("levelIsShop", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
				}
				Type type4 = AccessTools.TypeByName("SemiFunc");
				if (type4 != null)
				{
					_m_isMaster = type4.GetMethod("IsMasterClientOrSingleplayer", BindingFlags.Static | BindingFlags.Public);
				}
				Type type5 = repoDiceAsm.GetType("RepoDice.RepoDice");
				if (type5 != null)
				{
					_f_throwSpeed = type5.GetField("throwSpeed", BindingFlags.Static | BindingFlags.Public);
				}
				_t_rpcTarget = AccessTools.TypeByName("Photon.Pun.RpcTarget");
				_v_masterClient = ((_t_rpcTarget != null) ? Enum.Parse(_t_rpcTarget, "MasterClient") : null);
				Type type6 = _f_view?.FieldType;
				if (type6 != null)
				{
					_m_photonRPC = type6.GetMethod("RPC", new Type[3]
					{
						typeof(string),
						_t_rpcTarget,
						typeof(object[])
					});
				}
				Type type7 = repoDiceAsm.GetType("RepoDice.Effects.IEffect");
				if (type7 != null)
				{
					_p_effectName = type7.GetProperty("Name", BindingFlags.Instance | BindingFlags.Public);
				}
				MethodInfo method = type.GetMethod("FixedUpdate", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
				if (method != null)
				{
					MethodInfo method2 = typeof(Plugin).GetMethod("DieBehaviourFixedUpdatePrefix", BindingFlags.Static | BindingFlags.NonPublic);
					new Harmony("matsu.repo.emergency_dice_fixed.fixedupdate").Patch((MethodBase)method, new HarmonyMethod(method2), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
					ManualLogSource? log2 = Log;
					if (log2 != null)
					{
						log2.LogInfo((object)"[Fixed] DieBehaviour.FixedUpdate patched.");
					}
				}
				if (!(type7 != null))
				{
					return;
				}
				MethodInfo method3 = typeof(Plugin).GetMethod("IEffectUsePrefix", BindingFlags.Static | BindingFlags.NonPublic);
				Harmony val = new Harmony("matsu.repo.emergency_dice_fixed.effectchat");
				int num = 0;
				Type[] types = repoDiceAsm.GetTypes();
				foreach (Type type8 in types)
				{
					if (type8.IsAbstract || type8.IsInterface || !type7.IsAssignableFrom(type8))
					{
						continue;
					}
					MethodInfo method4 = type8.GetMethod("Use", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
					if (!(method4 == null))
					{
						try
						{
							val.Patch((MethodBase)method4, new HarmonyMethod(method3), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
							num++;
						}
						catch
						{
						}
					}
				}
				ManualLogSource? log3 = Log;
				if (log3 != null)
				{
					log3.LogInfo((object)$"[Fixed] Patched {num} IEffect.Use() implementations for chat messages.");
				}
			}
			catch (Exception arg)
			{
				ManualLogSource? log4 = Log;
				if (log4 != null)
				{
					log4.LogError((object)$"[Fixed] ApplyPatch failed: {arg}");
				}
			}
		}

		private static void IEffectUsePrefix(object __instance)
		{
			try
			{
				if (_p_effectName == null || _f_chatManagerInstance == null || _m_forceSendMessage == null)
				{
					return;
				}
				string text = _p_effectName.GetValue(__instance) as string;
				if (!string.IsNullOrEmpty(text))
				{
					object value = _f_chatManagerInstance.GetValue(null);
					if (value != null)
					{
						_m_forceSendMessage.Invoke(value, new object[1] { "[Dice] " + text });
					}
				}
			}
			catch
			{
			}
		}

		private static bool DieBehaviourFixedUpdatePrefix(object __instance)
		{
			//IL_0373: Unknown result type (might be due to invalid IL or missing references)
			//IL_0378: Unknown result type (might be due to invalid IL or missing references)
			if (__instance == null)
			{
				return true;
			}
			bool flag = _f_isRolling != null && (bool)(_f_isRolling.GetValue(__instance) ?? ((object)false));
			bool flag2 = false;
			if (_f_rmStaticInstance != null && _f_levelIsShop != null)
			{
				try
				{
					object value = _f_rmStaticInstance.GetValue(null);
					if (value != null)
					{
						flag2 = (bool)(_f_levelIsShop.GetValue(value) ?? ((object)false));
					}
				}
				catch
				{
					flag2 = false;
				}
			}
			if (flag || flag2)
			{
				return false;
			}
			object obj2 = _f_itemToggle?.GetValue(__instance);
			bool flag3 = _f_prevToggleState != null && (bool)(_f_prevToggleState.GetValue(__instance) ?? ((object)false));
			bool flag4 = false;
			if (obj2 != null && _f_toggleState != null)
			{
				flag4 = (bool)(_f_toggleState.GetValue(obj2) ?? ((object)false));
			}
			object obj3 = _f_physGrabObject?.GetValue(__instance);
			object view = _f_view?.GetValue(__instance);
			object? obj4 = _f_rb?.GetValue(__instance);
			Rigidbody val = (Rigidbody)((obj4 is Rigidbody) ? obj4 : null);
			bool isMaster = !(_m_isMaster != null) || (bool)(_m_isMaster.Invoke(null, null) ?? ((object)true));
			if (obj2 != null && flag4 != flag3)
			{
				_f_isRolling?.SetValue(__instance, true);
				_f_rolledFromE?.SetValue(__instance, true);
				TriggerBlockPickupAndRoll(__instance, view, isMaster);
			}
			_f_prevToggleState?.SetValue(__instance, flag4);
			bool flag5 = false;
			object obj5 = null;
			if (obj3 != null)
			{
				flag5 = _f_grabbed != null && (bool)(_f_grabbed.GetValue(obj3) ?? ((object)false));
				if (_f_playerGrabbing?.GetValue(obj3) is IList { Count: 1 } list && list[0] != null)
				{
					object obj6 = list[0];
					if (_f_playerAvatar == null)
					{
						_f_playerAvatar = FindField(obj6.GetType(), "playerAvatar");
					}
					obj5 = _f_playerAvatar?.GetValue(obj6);
				}
			}
			object obj7 = _f_lastHolder?.GetValue(__instance);
			if (obj5 != null && obj7 != obj5)
			{
				_f_lastHolder?.SetValue(__instance, obj5);
				return false;
			}
			if (flag || flag5)
			{
				return false;
			}
			if ((Object)(object)val != (Object)null)
			{
				float num = 8f;
				if (_f_throwSpeed != null && _f_throwSpeed.GetValue(null) is ConfigEntry<float> val2)
				{
					num = val2.Value;
				}
				Vector3 velocity = val.velocity;
				if (((Vector3)(ref velocity)).magnitude > num)
				{
					_f_isRolling?.SetValue(__instance, true);
					TriggerBlockPickupAndRoll(__instance, view, isMaster);
				}
			}
			return false;
		}

		private static void TriggerBlockPickupAndRoll(object instance, object? view, bool isMaster)
		{
			if (isMaster)
			{
				_m_blockPickupAndRollRPC?.Invoke(instance, null);
			}
			else if (view != null && _m_photonRPC != null && _v_masterClient != null)
			{
				_m_photonRPC.Invoke(view, new object[3]
				{
					"BlockPickupAndRollRPC",
					_v_masterClient,
					Array.Empty<object>()
				});
			}
		}
	}
}