Decompiled source of PeakDropPlus v0.1.1

plugins/dev.kesor.peakdropplus.dll

Decompiled 2 months ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
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;
using UnityEngine.SceneManagement;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: IgnoresAccessChecksTo("Assembly-CSharp")]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("dev.kesor.peakdropplus")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("0.1.0.0")]
[assembly: AssemblyInformationalVersion("0.1.0")]
[assembly: AssemblyProduct("dev.kesor.peakdropplus")]
[assembly: AssemblyTitle("PeakDropPlus")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("0.1.0.0")]
[module: UnverifiableCode]
[module: RefSafetyRules(11)]
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;
		}
	}
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.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 BepInEx
{
	[AttributeUsage(AttributeTargets.Class, Inherited = false, AllowMultiple = false)]
	[Conditional("CodeGeneration")]
	internal sealed class BepInAutoPluginAttribute : Attribute
	{
		public BepInAutoPluginAttribute(string? id = null, string? name = null, string? version = null)
		{
		}
	}
}
namespace BepInEx.Preloader.Core.Patching
{
	[AttributeUsage(AttributeTargets.Class, Inherited = false, AllowMultiple = false)]
	[Conditional("CodeGeneration")]
	internal sealed class PatcherAutoPluginAttribute : Attribute
	{
		public PatcherAutoPluginAttribute(string? id = null, string? name = null, string? version = null)
		{
		}
	}
}
namespace Peak.PeakDropPlus
{
	[BepInPlugin("dev.kesor.peakdropplus", "PeakDropPlus", "1.2.0")]
	public class PeakDropPlus : BaseUnityPlugin
	{
		internal static ManualLogSource Log;

		internal static ConfigEntry<float> ChancePercent;

		private Harmony? _harmony;

		private bool _showWindow;

		private Rect _windowRect = new Rect(20f, 20f, 320f, 140f);

		private string _chanceInput = "";

		private float _nextUiRefresh;

		private void Awake()
		{
			//IL_0052: Unknown result type (might be due to invalid IL or missing references)
			//IL_005c: Expected O, but got Unknown
			Log = ((BaseUnityPlugin)this).Logger;
			ChancePercent = ((BaseUnityPlugin)this).Config.Bind<float>("Drop", "ChancePercent", 10f, "Chance percent for RescueHook_Infinite from scout statue");
			_chanceInput = ChancePercent.Value.ToString("0.##");
			_harmony = new Harmony("dev.kesor.peakdropplus");
			_harmony.PatchAll();
			SceneManager.sceneLoaded += OnSceneLoaded;
			Log.LogInfo((object)"PeakDropPlus loaded");
		}

		private void OnDestroy()
		{
			SceneManager.sceneLoaded -= OnSceneLoaded;
			Harmony? harmony = _harmony;
			if (harmony != null)
			{
				harmony.UnpatchSelf();
			}
		}

		private void OnSceneLoaded(Scene scene, LoadSceneMode mode)
		{
			_showWindow = false;
			_chanceInput = ChancePercent.Value.ToString("0.##");
		}

		private void Update()
		{
			if (Time.time >= _nextUiRefresh)
			{
				_nextUiRefresh = Time.time + 1f;
				if (!_showWindow)
				{
					_chanceInput = ChancePercent.Value.ToString("0.##");
				}
			}
			if (Input.GetKeyDown((KeyCode)27) && ShouldAllowMenu())
			{
				_showWindow = !_showWindow;
			}
		}

		private void OnGUI()
		{
			//IL_0019: Unknown result type (might be due to invalid IL or missing references)
			//IL_0025: Unknown result type (might be due to invalid IL or missing references)
			//IL_0034: Expected O, but got Unknown
			//IL_002f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0034: Unknown result type (might be due to invalid IL or missing references)
			if (ShouldAllowMenu() && _showWindow)
			{
				_windowRect = GUI.Window(937451, _windowRect, new WindowFunction(DrawWindow), "PeakDropPlus");
			}
		}

		private void DrawWindow(int id)
		{
			//IL_001a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0044: Unknown result type (might be due to invalid IL or missing references)
			//IL_006f: Unknown result type (might be due to invalid IL or missing references)
			//IL_009a: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ca: Unknown result type (might be due to invalid IL or missing references)
			//IL_00fa: Unknown result type (might be due to invalid IL or missing references)
			//IL_012a: Unknown result type (might be due to invalid IL or missing references)
			//IL_015a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0190: Unknown result type (might be due to invalid IL or missing references)
			//IL_01bd: Unknown result type (might be due to invalid IL or missing references)
			bool flag = IsHost();
			GUI.Label(new Rect(15f, 28f, 290f, 20f), "Scout statue RescueHook chance (%)");
			GUI.enabled = flag;
			_chanceInput = GUI.TextField(new Rect(15f, 52f, 100f, 24f), _chanceInput, 16);
			if (GUI.Button(new Rect(125f, 52f, 60f, 24f), "Apply"))
			{
				ApplyChanceFromInput();
			}
			if (GUI.Button(new Rect(195f, 52f, 45f, 24f), "5"))
			{
				SetChance(5f);
			}
			if (GUI.Button(new Rect(245f, 52f, 45f, 24f), "25"))
			{
				SetChance(25f);
			}
			if (GUI.Button(new Rect(15f, 84f, 45f, 24f), "50"))
			{
				SetChance(50f);
			}
			if (GUI.Button(new Rect(65f, 84f, 45f, 24f), "75"))
			{
				SetChance(75f);
			}
			if (GUI.Button(new Rect(115f, 84f, 45f, 24f), "100"))
			{
				SetChance(100f);
			}
			GUI.enabled = true;
			GUI.Label(new Rect(15f, 112f, 290f, 20f), flag ? "Host can edit this value" : "Only host can edit this value");
			GUI.DragWindow(new Rect(0f, 0f, 10000f, 20f));
		}

		private void ApplyChanceFromInput()
		{
			if (IsHost() && (float.TryParse(_chanceInput, NumberStyles.Float, CultureInfo.InvariantCulture, out var result) || float.TryParse(_chanceInput, out result)))
			{
				SetChance(result);
			}
		}

		private void SetChance(float value)
		{
			if (IsHost())
			{
				value = Mathf.Clamp(value, 0f, 100f);
				ChancePercent.Value = value;
				((ConfigEntryBase)ChancePercent).ConfigFile.Save();
				_chanceInput = value.ToString("0.##");
				Log.LogInfo((object)("ChancePercent set to " + value.ToString("0.##")));
			}
		}

		private bool ShouldAllowMenu()
		{
			//IL_0000: Unknown result type (might be due to invalid IL or missing references)
			//IL_0005: Unknown result type (might be due to invalid IL or missing references)
			Scene activeScene = SceneManager.GetActiveScene();
			string name = ((Scene)(ref activeScene)).name;
			if (!(name == "Airport") && !(name == "Title"))
			{
				return name == "Pretitle";
			}
			return true;
		}

		private static bool IsHost()
		{
			if (!PhotonNetwork.InRoom)
			{
				return true;
			}
			return PhotonNetwork.IsMasterClient;
		}

		internal static string CleanName(string name)
		{
			if (!string.IsNullOrWhiteSpace(name))
			{
				return name.Replace("(Clone)", "").Trim();
			}
			return string.Empty;
		}

		internal static GameObject? FindRescueHookPrefab()
		{
			Item[] array = Resources.FindObjectsOfTypeAll<Item>();
			foreach (Item val in array)
			{
				if (!((Object)(object)val == (Object)null))
				{
					string text = CleanName(((Object)((Component)val).gameObject).name);
					if (text == "RescueHook_Infinite")
					{
						return ((Component)val).gameObject;
					}
				}
			}
			GameObject[] array2 = Resources.FindObjectsOfTypeAll<GameObject>();
			foreach (GameObject val2 in array2)
			{
				if (!((Object)(object)val2 == (Object)null))
				{
					string text2 = CleanName(((Object)val2).name);
					if (text2 == "RescueHook_Infinite" && (Object)(object)val2.GetComponent<Item>() != (Object)null)
					{
						return val2;
					}
				}
			}
			return null;
		}
	}
	[HarmonyPatch(typeof(Spawner), "GetObjectsToSpawn")]
	internal static class Spawner_GetObjectsToSpawn_Patch
	{
		private static void Postfix(Spawner __instance, int spawnCount, bool canRepeat, ref List<GameObject> __result)
		{
			//IL_0038: Unknown result type (might be due to invalid IL or missing references)
			//IL_0042: Invalid comparison between Unknown and I4
			if ((Object)(object)__instance == (Object)null || (Object)(object)((Component)__instance).gameObject == (Object)null)
			{
				return;
			}
			string text = PeakDropPlus.CleanName(((Object)((Component)__instance).gameObject).name);
			if (text != "scout statue" || (int)__instance.spawnPool != 131072)
			{
				return;
			}
			float num = Mathf.Clamp(PeakDropPlus.ChancePercent.Value, 0f, 100f);
			if (num <= 0f || Random.value * 100f > num)
			{
				return;
			}
			GameObject val = PeakDropPlus.FindRescueHookPrefab();
			if ((Object)(object)val == (Object)null)
			{
				PeakDropPlus.Log.LogError((object)"RescueHook_Infinite prefab not found at spawn time");
				return;
			}
			int num2 = ((spawnCount <= 0) ? 1 : spawnCount);
			List<GameObject> list = new List<GameObject>(num2);
			for (int i = 0; i < num2; i++)
			{
				list.Add(val);
			}
			__result = list;
			PeakDropPlus.Log.LogInfo((object)"scout statue drop replaced with RescueHook_Infinite");
		}
	}
}
namespace System.Runtime.CompilerServices
{
	[AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)]
	internal sealed class IgnoresAccessChecksToAttribute : Attribute
	{
		public IgnoresAccessChecksToAttribute(string assemblyName)
		{
		}
	}
}