Decompiled source of OnlyHostShopkeeperSwitch v1.1.0

OnlyHostShopkeeperSwitch.dll

Decompiled 2 weeks ago
using System;
using System.Collections;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using Photon.Pun;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: IgnoresAccessChecksTo("")]
[assembly: AssemblyCompany("RED")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.1.0.0")]
[assembly: AssemblyInformationalVersion("1.1.0")]
[assembly: AssemblyProduct("OnlyHostShopkeeperSwitch")]
[assembly: AssemblyTitle("OnlyHostShopkeeperSwitch")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.1.0.0")]
[module: UnverifiableCode]
[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 OnlyHostShopkeeperSwitch
{
	[BepInPlugin("com.red.OnlyHostShopkeeperSwitch", "OnlyHostShopkeeperSwitch", "1.1.0")]
	public class Plugin : BaseUnityPlugin
	{
		public const string Guid = "com.red.OnlyHostShopkeeperSwitch";

		public const string Name = "OnlyHostShopkeeperSwitch";

		public const string Version = "1.1.0";

		internal static ManualLogSource Log { get; private set; }

		internal static ConfigEntry<bool> OnlyHostPressesSwitch { get; private set; }

		internal static ConfigEntry<bool> ShopkeeperOnByDefault { get; private set; }

		internal static ConfigEntry<bool> HostNeverTargeted { get; private set; }

		private void Awake()
		{
			//IL_0070: Unknown result type (might be due to invalid IL or missing references)
			Log = ((BaseUnityPlugin)this).Logger;
			OnlyHostPressesSwitch = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "OnlyHostPressesSwitch", true, "Only the host's press of the shopkeeper's on/off switch does anything.");
			ShopkeeperOnByDefault = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "ShopkeeperOnByDefault", true, "The shopkeeper wakes up on its own when the shop loads.");
			HostNeverTargeted = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "HostNeverTargeted", true, "The shopkeeper never warns, grabs or shoots the host.");
			new Harmony("com.red.OnlyHostShopkeeperSwitch").PatchAll();
			Log.LogInfo((object)"OnlyHostShopkeeperSwitch v1.1.0 loaded.");
		}
	}
}
namespace OnlyHostShopkeeperSwitch.Patches
{
	[HarmonyPatch(typeof(ShopKeeper))]
	internal static class AutoWakePatches
	{
		[HarmonyPostfix]
		[HarmonyPatch("Start")]
		private static void WakeOnSpawn(ShopKeeper __instance)
		{
			if (Plugin.ShopkeeperOnByDefault.Value && SemiFunc.IsMasterClientOrSingleplayer())
			{
				((MonoBehaviour)__instance).StartCoroutine(WakeWhenReady(__instance));
			}
		}

		private static IEnumerator WakeWhenReady(ShopKeeper keeper)
		{
			while (!LevelGenerator.Instance.Generated)
			{
				yield return (object)new WaitForSeconds(0.2f);
			}
			yield return (object)new WaitForSeconds(1f);
			if (!Object.op_Implicit((Object)(object)keeper) || (int)keeper.currentState != 0)
			{
				yield break;
			}
			SwitchGatePatches.OpenWindow();
			try
			{
				if (SemiFunc.IsMultiplayer())
				{
					keeper.photonView.RPC("WakeUpRPC", (RpcTarget)0, Array.Empty<object>());
				}
				else
				{
					keeper.WakeUpRPC(default(PhotonMessageInfo));
				}
			}
			finally
			{
				SwitchGatePatches.CloseWindow();
			}
		}
	}
	[HarmonyPatch(typeof(ShopKeeper))]
	internal static class HostImmunityPatches
	{
		private static bool IsHostAvatar(PlayerAvatar player)
		{
			if (Plugin.HostNeverTargeted.Value && Object.op_Implicit((Object)(object)player))
			{
				return (Object)(object)player == (Object)(object)SemiFunc.PlayerAvatarLocal();
			}
			return false;
		}

		[HarmonyPrefix]
		[HarmonyPatch("AddRuckusScore")]
		private static bool SkipHostRuckus(PlayerAvatar _player)
		{
			return !IsHostAvatar(_player);
		}

		[HarmonyPrefix]
		[HarmonyPatch("WarnPlayer")]
		private static bool SkipHostWarn(PlayerAvatar _player)
		{
			return !IsHostAvatar(_player);
		}

		[HarmonyPrefix]
		[HarmonyPatch("ShootAtPlayer")]
		private static bool SkipHostShoot(PlayerAvatar _player)
		{
			return !IsHostAvatar(_player);
		}

		[HarmonyPrefix]
		[HarmonyPatch("TriggerAngryObservation")]
		private static bool SkipHostAngry(PlayerAvatar _offender)
		{
			return !IsHostAvatar(_offender);
		}
	}
	[HarmonyPatch(typeof(ShopKeeper))]
	internal static class SwitchGatePatches
	{
		private static bool _windowOpen;

		internal static void OpenWindow()
		{
			_windowOpen = true;
		}

		internal static void CloseWindow()
		{
			_windowOpen = false;
		}

		[HarmonyPrefix]
		[HarmonyPatch("WakeUpOrSleepLogic")]
		private static bool OnlyLocalPress(ShopKeeper __instance)
		{
			if (!Plugin.OnlyHostPressesSwitch.Value || !SemiFunc.IsMultiplayer())
			{
				_windowOpen = true;
				return true;
			}
			PhysGrabObjectGrabArea val = (Object.op_Implicit((Object)(object)__instance.OnOffSwitch) ? __instance.OnOffSwitch.GetComponentInChildren<PhysGrabObjectGrabArea>() : null);
			if (!Object.op_Implicit((Object)(object)val))
			{
				Plugin.Log.LogWarning((object)"Switch grab area not found; press ignored.");
				return false;
			}
			PhysGrabber instance = PhysGrabber.instance;
			if (!Object.op_Implicit((Object)(object)instance) || !val.listOfAllGrabbers.Contains(instance))
			{
				Plugin.Log.LogInfo((object)"Blocked a non-host press of the shopkeeper switch.");
				return false;
			}
			_windowOpen = true;
			return true;
		}

		[HarmonyPostfix]
		[HarmonyPatch("WakeUpOrSleepLogic")]
		private static void PressDone()
		{
			_windowOpen = false;
		}

		[HarmonyPrefix]
		[HarmonyPatch("WakeUpRPC")]
		private static bool GateWakeUp()
		{
			return RpcAllowed();
		}

		[HarmonyPrefix]
		[HarmonyPatch("SleepRPC")]
		private static bool GateSleep()
		{
			return RpcAllowed();
		}

		private static bool RpcAllowed()
		{
			if (Plugin.OnlyHostPressesSwitch.Value && SemiFunc.IsMultiplayer())
			{
				return _windowOpen;
			}
			return true;
		}
	}
}