Decompiled source of ForceModifiers v1.0.0

ForceModifiers.dll

Decompiled 4 days ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using BepInEx;
using BepInEx.Configuration;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using Pigeon;
using TMPro;
using UnityEngine;
using UnityEngine.UI;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: AssemblyVersion("0.0.0.0")]
[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.Module, AllowMultiple = false, Inherited = false)]
	internal sealed class RefSafetyRulesAttribute : Attribute
	{
		public readonly int Version;

		public RefSafetyRulesAttribute(int P_0)
		{
			Version = P_0;
		}
	}
}
namespace MycopunkModifiers
{
	[BepInPlugin("com.coloron.forcemods", "Force Modifiers", "1.0.0")]
	[MycoMod(/*Could not decode attribute arguments.*/)]
	public class ForceModifiersPlugin : BaseUnityPlugin
	{
		public static ForceModifiersPlugin Instance;

		public ModifierGUI guiController;

		public Dictionary<int, int> ForcedModifiers = new Dictionary<int, int>();

		public ConfigEntry<KeyCode> ToggleKey;

		private void Awake()
		{
			//IL_0042: Unknown result type (might be due to invalid IL or missing references)
			//IL_0048: Expected O, but got Unknown
			Instance = this;
			ToggleKey = ((BaseUnityPlugin)this).Config.Bind<KeyCode>("General", "MenuKey", (KeyCode)283, "Key to toggle the modifier menu");
			guiController = ((Component)this).gameObject.AddComponent<ModifierGUI>();
			Harmony val = new Harmony("com.coloron.forcemods");
			val.PatchAll();
			((BaseUnityPlugin)this).Logger.LogInfo((object)"Force Modifiers Loaded!");
		}
	}
	public class ModifierGUI : MonoBehaviour
	{
		private class ModCacheData
		{
			public int RealIndex;

			public string Name;
		}

		private bool showGui = false;

		private float windowWidth = 640f;

		private float cellHeight = 40f;

		private int columns = 4;

		private float footerHeight = 40f;

		private float headerHeight = 25f;

		private float sidePadding = 15f;

		private Rect windowRect;

		private GUIStyle activeBtnStyle;

		private GUIStyle normalBtnStyle;

		private List<ModCacheData> cachedModifiers = new List<ModCacheData>();

		private float cachedWindowHeight = 100f;

		private int lastHoveredIndex = -1;

		public bool IsMissionSelectOpen = false;

		private bool preVisible;

		private CursorLockMode preLockMode;

		private void Awake()
		{
			//IL_0017: Unknown result type (might be due to invalid IL or missing references)
			//IL_001c: Unknown result type (might be due to invalid IL or missing references)
			windowRect = new Rect(50f, 50f, windowWidth, 0f);
		}

		private void ToggleMenu()
		{
			//IL_00bb: Unknown result type (might be due to invalid IL or missing references)
			//IL_0092: Unknown result type (might be due to invalid IL or missing references)
			//IL_0097: Unknown result type (might be due to invalid IL or missing references)
			showGui = !showGui;
			if (showGui)
			{
				bool flag = cachedModifiers.Count == 0;
				if (!flag && (Object)(object)Global.Instance != (Object)null && Global.Instance.MissionModifiers != null && Global.Instance.MissionModifiers.Length != cachedModifiers.Count)
				{
					flag = true;
				}
				if (flag)
				{
					RefreshModifierCache();
				}
				preVisible = Cursor.visible;
				preLockMode = Cursor.lockState;
				Cursor.visible = true;
				Cursor.lockState = (CursorLockMode)0;
			}
			else
			{
				Cursor.visible = preVisible;
				Cursor.lockState = preLockMode;
			}
		}

		private void RefreshModifierCache()
		{
			if ((Object)(object)Global.Instance == (Object)null || Global.Instance.MissionModifiers == null)
			{
				return;
			}
			WeightedArray<MissionModifier> missionModifiers = Global.Instance.MissionModifiers;
			int length = missionModifiers.Length;
			cachedModifiers.Clear();
			if (cachedModifiers.Capacity < length)
			{
				cachedModifiers.Capacity = length;
			}
			for (int i = 0; i < length; i++)
			{
				MissionModifier val = missionModifiers[i];
				if ((Object)(object)val != (Object)null)
				{
					cachedModifiers.Add(new ModCacheData
					{
						RealIndex = i,
						Name = (val.ModifierName ?? "")
					});
				}
			}
			cachedModifiers.Sort((ModCacheData a, ModCacheData b) => string.Compare(a.Name, b.Name, StringComparison.Ordinal));
			int num = Mathf.CeilToInt((float)cachedModifiers.Count / (float)columns);
			cachedWindowHeight = headerHeight + (float)num * cellHeight + 5f + footerHeight + sidePadding;
			((Rect)(ref windowRect)).height = cachedWindowHeight;
		}

		private void OnGUI()
		{
			//IL_0010: Unknown result type (might be due to invalid IL or missing references)
			//IL_0016: Invalid comparison between Unknown and I4
			//IL_0019: Unknown result type (might be due to invalid IL or missing references)
			//IL_0028: Unknown result type (might be due to invalid IL or missing references)
			//IL_0096: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a8: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b4: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c3: Expected O, but got Unknown
			//IL_00be: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c3: Unknown result type (might be due to invalid IL or missing references)
			Event current = Event.current;
			if (current.isKey && (int)current.type == 4 && current.keyCode == ForceModifiersPlugin.Instance.ToggleKey.Value)
			{
				ToggleMenu();
			}
			if (showGui)
			{
				if (activeBtnStyle == null)
				{
					InitStyles();
				}
				if (cachedModifiers.Count == 0 && Global.Instance?.MissionModifiers != null)
				{
					RefreshModifierCache();
				}
				GUI.backgroundColor = Color.black;
				windowRect = GUI.Window(88890, windowRect, new WindowFunction(DrawWindow), "Force Modifiers");
				((Rect)(ref windowRect)).x = Mathf.Clamp(((Rect)(ref windowRect)).x, 0f, (float)Screen.width - ((Rect)(ref windowRect)).width);
				((Rect)(ref windowRect)).y = Mathf.Clamp(((Rect)(ref windowRect)).y, 0f, (float)Screen.height - ((Rect)(ref windowRect)).height);
			}
		}

		private void InitStyles()
		{
			//IL_000c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0016: Expected O, but got Unknown
			//IL_003b: Unknown result type (might be due to invalid IL or missing references)
			//IL_005f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0069: Expected O, but got Unknown
			//IL_0090: Unknown result type (might be due to invalid IL or missing references)
			activeBtnStyle = new GUIStyle(GUI.skin.box);
			activeBtnStyle.alignment = (TextAnchor)4;
			activeBtnStyle.fontStyle = (FontStyle)1;
			activeBtnStyle.normal.textColor = Color.cyan;
			activeBtnStyle.fontSize = 11;
			normalBtnStyle = new GUIStyle(GUI.skin.button);
			normalBtnStyle.alignment = (TextAnchor)4;
			normalBtnStyle.normal.textColor = new Color(0.7f, 0.7f, 0.7f);
			normalBtnStyle.fontSize = 11;
		}

		private void DrawWindow(int windowID)
		{
			//IL_0073: Unknown result type (might be due to invalid IL or missing references)
			//IL_00bb: 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)
			//IL_00cb: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d1: Invalid comparison between Unknown and I4
			//IL_00e0: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d5: Unknown result type (might be due to invalid IL or missing references)
			//IL_01c6: Unknown result type (might be due to invalid IL or missing references)
			//IL_01cc: Invalid comparison between Unknown and I4
			//IL_00fd: Unknown result type (might be due to invalid IL or missing references)
			//IL_0113: Unknown result type (might be due to invalid IL or missing references)
			//IL_0320: Unknown result type (might be due to invalid IL or missing references)
			//IL_037d: Unknown result type (might be due to invalid IL or missing references)
			//IL_02a7: Unknown result type (might be due to invalid IL or missing references)
			//IL_02b2: Unknown result type (might be due to invalid IL or missing references)
			//IL_0272: Unknown result type (might be due to invalid IL or missing references)
			//IL_027d: Unknown result type (might be due to invalid IL or missing references)
			if (cachedModifiers.Count == 0)
			{
				GUILayout.Label("Loading...", Array.Empty<GUILayoutOption>());
				GUI.DragWindow();
				return;
			}
			Rect val = default(Rect);
			((Rect)(ref val))..ctor(sidePadding, headerHeight, ((Rect)(ref windowRect)).width - sidePadding * 2f, ((Rect)(ref windowRect)).height - headerHeight - sidePadding);
			GUILayout.BeginArea(val);
			float width = ((Rect)(ref val)).width;
			float num = width / (float)columns;
			int count = cachedModifiers.Count;
			int num2 = Mathf.CeilToInt((float)count / (float)columns);
			float num3 = (float)num2 * cellHeight;
			Rect rect = GUILayoutUtility.GetRect(width, num3);
			Event current = Event.current;
			if (((int)current.type == 3 || (int)current.type == 0) && ((Rect)(ref rect)).Contains(current.mousePosition))
			{
				float num4 = current.mousePosition.x - ((Rect)(ref rect)).x;
				float num5 = current.mousePosition.y - ((Rect)(ref rect)).y;
				int num6 = Mathf.FloorToInt(num5 / cellHeight) * columns + Mathf.FloorToInt(num4 / num);
				if (num6 >= 0 && num6 < count && num6 != lastHoveredIndex)
				{
					lastHoveredIndex = num6;
					int realIndex = cachedModifiers[num6].RealIndex;
					if (current.button == 0)
					{
						AddStack(realIndex);
					}
					if (current.button == 1)
					{
						RemoveStack(realIndex);
					}
					current.Use();
				}
			}
			else if ((int)current.type == 1)
			{
				lastHoveredIndex = -1;
			}
			Rect val2 = default(Rect);
			for (int i = 0; i < count; i++)
			{
				ModCacheData modCacheData = cachedModifiers[i];
				int num7 = i / columns;
				int num8 = i % columns;
				((Rect)(ref val2))..ctor(((Rect)(ref rect)).x + (float)num8 * num, ((Rect)(ref rect)).y + (float)num7 * cellHeight, num, cellHeight);
				int value;
				int num9 = (ForceModifiersPlugin.Instance.ForcedModifiers.TryGetValue(modCacheData.RealIndex, out value) ? value : 0);
				if (num9 > 0)
				{
					GUI.backgroundColor = new Color(0f, 0.2f, 0.6f);
					GUI.Box(val2, $"{modCacheData.Name} [x{num9}]", activeBtnStyle);
				}
				else
				{
					GUI.backgroundColor = Color.black;
					GUI.Button(val2, modCacheData.Name, normalBtnStyle);
				}
			}
			GUILayout.FlexibleSpace();
			GUILayout.BeginHorizontal((GUILayoutOption[])(object)new GUILayoutOption[1] { GUILayout.Height(footerHeight) });
			float num10 = 5f;
			float num11 = (width - num10) / 2f;
			GUI.backgroundColor = new Color(0f, 0.4f, 0.8f);
			if (GUILayout.Button("RANDOM", normalBtnStyle, (GUILayoutOption[])(object)new GUILayoutOption[2]
			{
				GUILayout.Width(num11),
				GUILayout.Height(footerHeight)
			}))
			{
				RandomizeSelection();
			}
			GUILayout.Space(num10);
			GUI.backgroundColor = new Color(0.6f, 0.1f, 0.1f);
			if (GUILayout.Button("RESET", normalBtnStyle, (GUILayoutOption[])(object)new GUILayoutOption[2]
			{
				GUILayout.Width(num11),
				GUILayout.Height(footerHeight)
			}))
			{
				ForceModifiersPlugin.Instance.ForcedModifiers.Clear();
			}
			GUILayout.EndHorizontal();
			GUILayout.EndArea();
			GUI.DragWindow();
		}

		private void RandomizeSelection()
		{
			ForceModifiersPlugin.Instance.ForcedModifiers.Clear();
			if ((Object)(object)Global.Instance == (Object)null || Global.Instance.MissionModifiers == null)
			{
				return;
			}
			int length = Global.Instance.MissionModifiers.Length;
			if (length != 0)
			{
				int[] array = new int[length];
				for (int i = 0; i < length; i++)
				{
					array[i] = i;
				}
				int num = Random.Range(1, length + 1);
				for (int j = 0; j < length; j++)
				{
					int num2 = array[j];
					int num3 = Random.Range(j, length);
					array[j] = array[num3];
					array[num3] = num2;
				}
				for (int k = 0; k < num; k++)
				{
					ForceModifiersPlugin.Instance.ForcedModifiers[array[k]] = 1;
				}
			}
		}

		private void AddStack(int id)
		{
			if (!ForceModifiersPlugin.Instance.ForcedModifiers.ContainsKey(id))
			{
				ForceModifiersPlugin.Instance.ForcedModifiers[id] = 0;
			}
			ForceModifiersPlugin.Instance.ForcedModifiers[id]++;
		}

		private void RemoveStack(int id)
		{
			if (ForceModifiersPlugin.Instance.ForcedModifiers.ContainsKey(id))
			{
				ForceModifiersPlugin.Instance.ForcedModifiers[id]--;
				if (ForceModifiersPlugin.Instance.ForcedModifiers[id] <= 0)
				{
					ForceModifiersPlugin.Instance.ForcedModifiers.Remove(id);
				}
			}
		}
	}
	[HarmonyPatch(typeof(MissionManager), "SetupMissionModifiers")]
	public class ApplyModifiersPatch
	{
		[HarmonyPostfix]
		private static void Postfix(bool apply, bool safe)
		{
			if (ForceModifiersPlugin.Instance.ForcedModifiers.Count == 0)
			{
				return;
			}
			WeightedArray<MissionModifier> missionModifiers = Global.Instance.MissionModifiers;
			foreach (KeyValuePair<int, int> forcedModifier in ForceModifiersPlugin.Instance.ForcedModifiers)
			{
				if (forcedModifier.Key < 0 || forcedModifier.Key >= missionModifiers.Length)
				{
					continue;
				}
				MissionModifier val = missionModifiers[forcedModifier.Key];
				if (!((Object)(object)val != (Object)null))
				{
					continue;
				}
				try
				{
					for (int i = 0; i < forcedModifier.Value; i++)
					{
						if (apply)
						{
							val.Apply(true);
						}
						else
						{
							val.Remove(true);
						}
					}
				}
				catch
				{
				}
			}
		}
	}
	[HarmonyPatch(typeof(MissionInfoWindow), "Setup")]
	public class UI_Setup_Patch
	{
		[HarmonyPostfix]
		private static void Postfix(MissionInfoWindow __instance)
		{
			//IL_00b3: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c4: Unknown result type (might be due to invalid IL or missing references)
			//IL_02df: Unknown result type (might be due to invalid IL or missing references)
			//IL_0304: Unknown result type (might be due to invalid IL or missing references)
			//IL_0309: Unknown result type (might be due to invalid IL or missing references)
			//IL_030d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0312: Unknown result type (might be due to invalid IL or missing references)
			//IL_031b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0365: Unknown result type (might be due to invalid IL or missing references)
			//IL_01ce: Unknown result type (might be due to invalid IL or missing references)
			//IL_0200: Unknown result type (might be due to invalid IL or missing references)
			//IL_0285: Unknown result type (might be due to invalid IL or missing references)
			//IL_0240: Unknown result type (might be due to invalid IL or missing references)
			ForceModifiersPlugin.Instance.guiController.IsMissionSelectOpen = true;
			if (ForceModifiersPlugin.Instance.ForcedModifiers.Count == 0)
			{
				return;
			}
			Traverse val = Traverse.Create((object)__instance);
			List<TextMeshProUGUI> value = val.Field("modifiers").GetValue<List<TextMeshProUGUI>>();
			TextMeshProUGUI value2 = val.Field("modifierPrefab").GetValue<TextMeshProUGUI>();
			RectTransform value3 = val.Field("modifierScrollContainer").GetValue<RectTransform>();
			TextMeshProUGUI value4 = val.Field("modifierHeader").GetValue<TextMeshProUGUI>();
			float num = 0f;
			int num2 = 0;
			foreach (TextMeshProUGUI item2 in value)
			{
				if (((Component)item2).gameObject.activeSelf)
				{
					num = ((TMP_Text)item2).rectTransform.anchoredPosition.y - ((TMP_Text)item2).rectTransform.sizeDelta.y - 10f;
					num2++;
				}
			}
			if (num2 == 0)
			{
				((Component)value4).gameObject.SetActive(true);
				num = 0f;
			}
			foreach (KeyValuePair<int, int> forcedModifier in ForceModifiersPlugin.Instance.ForcedModifiers)
			{
				int key = forcedModifier.Key;
				if (key < 0 || key >= Global.Instance.MissionModifiers.Length)
				{
					continue;
				}
				MissionModifier val2 = Global.Instance.MissionModifiers[key];
				if (num2 >= value.Count)
				{
					TextMeshProUGUI item = Object.Instantiate<TextMeshProUGUI>(value2, (Transform)(object)value3);
					value.Add(item);
				}
				TextMeshProUGUI val3 = value[num2];
				((Component)val3).gameObject.SetActive(true);
				((TMP_Text)val3).rectTransform.anchoredPosition = new Vector2(0f, num);
				((TMP_Text)val3).text = $"{val2.ModifierName} <color=#00FFFF>[x{forcedModifier.Value}]</color>";
				((Graphic)val3).color = Color.white;
				HoverInfoMissionModifier component = ((Component)val3).GetComponent<HoverInfoMissionModifier>();
				if ((Object)(object)component != (Object)null)
				{
					component.Icon.sprite = val2.Icon;
					((Graphic)component.Icon).color = val2.IconColor;
					component.Setup(val2);
					if ((Object)(object)component.XPText != (Object)null)
					{
						((Component)component.XPText).gameObject.SetActive(false);
					}
				}
				num -= ((TMP_Text)val3).rectTransform.sizeDelta.y + 10f;
				num2++;
			}
			object value5 = val.Field("modifierScrollMask").GetValue();
			if (value5 == null)
			{
				return;
			}
			RectTransform component2 = ((Component)value5).GetComponent<RectTransform>();
			Vector4 val4 = (Vector4)AccessTools.Property(value5.GetType(), "padding").GetValue(value5, null);
			Rect rect = component2.rect;
			float num3 = ((Rect)(ref rect)).height - val4.y;
			float num4 = 0f - num - num3;
			val.Field("modifierScrollHeight").SetValue((object)num4);
			object value6 = val.Field("modifierScrollBar").GetValue();
			if (value6 != null)
			{
				Transform transform = ((Component)value6).transform;
				if ((Object)(object)transform.parent != (Object)null && (Object)(object)transform.parent.parent != (Object)null)
				{
					((Component)transform.parent.parent).gameObject.SetActive(num4 > 0f);
				}
			}
		}
	}
	[HarmonyPatch(typeof(MissionInfoWindow), "OnDisable")]
	public class UI_Close_Patch
	{
		[HarmonyPostfix]
		private static void Postfix()
		{
			ForceModifiersPlugin.Instance.guiController.IsMissionSelectOpen = false;
		}
	}
}