Decompiled source of PostmortemGrenades v1.0.0

Postmortem Grenades.dll

Decompiled 2 hours ago
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using Photon.Pun;
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("Postmortem Grenades")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("Postmortem Grenades")]
[assembly: AssemblyTitle("Postmortem Grenades")]
[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 PostmortemGrenades
{
	[BepInPlugin("padrug.repo.postmortemgrenades", "Postmortem Grenades", "1.0.0")]
	public class Plugin : BaseUnityPlugin
	{
		internal static ManualLogSource Log;

		private void Awake()
		{
			Log = ((BaseUnityPlugin)this).Logger;
			harmonyPatchInit();
		}

		private void harmonyPatchInit()
		{
			//IL_0006: Unknown result type (might be due to invalid IL or missing references)
			//IL_000c: Expected O, but got Unknown
			Harmony val = new Harmony("padrug.repo.postmortemgrenades");
			val.PatchAll();
			Log.LogInfo((object)"Postmortem Grenades loaded");
		}
	}
	[HarmonyPatch(typeof(PlayerAvatar), "PlayerDeathRPC")]
	public class PostmortemGrenadesPatch
	{
		private static void Prefix(PlayerAvatar __instance, out List<ItemGrenade> __state)
		{
			__state = new List<ItemGrenade>();
			if ((Object)(object)__instance == (Object)null || (SemiFunc.IsMultiplayer() && !PhotonNetwork.IsMasterClient))
			{
				return;
			}
			ItemGrenade[] array = Object.FindObjectsOfType<ItemGrenade>();
			ItemGrenade[] array2 = array;
			foreach (ItemGrenade val in array2)
			{
				if (!((Object)(object)val == (Object)null))
				{
					ItemEquippable component = ((Component)val).GetComponent<ItemEquippable>();
					if ((Object)(object)component != (Object)null && component.IsEquipped() && (Object)(object)component.GetOwnerPlayerAvatar() == (Object)(object)__instance)
					{
						__state.Add(val);
					}
				}
			}
		}

		private static void Postfix(PlayerAvatar __instance, List<ItemGrenade> __state)
		{
			if (!((Object)(object)__instance == (Object)null) && __state != null && __state.Count != 0 && (!SemiFunc.IsMultiplayer() || PhotonNetwork.IsMasterClient))
			{
				int playerViewId = (((Object)(object)__instance.photonView != (Object)null) ? __instance.photonView.ViewID : (-1));
				((MonoBehaviour)__instance).StartCoroutine(ActivateAfterDrop(__instance, __state, playerViewId));
			}
		}

		private static IEnumerator ActivateAfterDrop(PlayerAvatar player, List<ItemGrenade> grenades, int playerViewId)
		{
			yield return (object)new WaitForSeconds(0.15f);
			foreach (ItemGrenade grenade in grenades)
			{
				if (!((Object)(object)grenade == (Object)null))
				{
					ItemEquippable equippable = ((Component)grenade).GetComponent<ItemEquippable>();
					if ((Object)(object)equippable != (Object)null && equippable.IsEquipped())
					{
						Vector3 dropPos = ((Component)player).transform.position + Vector3.up * 0.5f;
						int grabberViewId = (((Object)(object)player.physGrabber != (Object)null && (Object)(object)player.physGrabber.photonView != (Object)null) ? player.physGrabber.photonView.ViewID : (-1));
						equippable.ForceUnequip(dropPos, grabberViewId);
						yield return null;
					}
					grenade.SetActivator(player);
					object toggleObj = AccessTools.Field(typeof(ItemGrenade), "itemToggle")?.GetValue(grenade);
					if (toggleObj != null)
					{
						AccessTools.Method(toggleObj.GetType(), "ToggleItem", new Type[2]
						{
							typeof(bool),
							typeof(int)
						}, (Type[])null)?.Invoke(toggleObj, new object[2] { true, playerViewId });
					}
					AccessTools.Field(typeof(ItemGrenade), "isActive")?.SetValue(grenade, true);
					Debug.Log((object)("Grenade " + ((Object)grenade).name + " activated at player " + ((Object)player).name + "!"));
				}
			}
		}
	}
}