Decompiled source of more breakthrough points v1.0.0

more breakthrough points/ResetBreakthroughs.dll

Decompiled 10 months ago
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Logging;
using HarmonyLib;
using MonoMod.RuntimeDetour;
using Rewired;
using Rewired.Data;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyTitle("ResetBreakthroughs")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("ResetBreakthroughs")]
[assembly: AssemblyCopyright("Copyright ©  2020")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("87632925-08f4-4363-92ac-b7c18068785e")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.0", FrameworkDisplayName = ".NET Framework 4")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace ResetBreakthroughs;

public class CustomKeybindings
{
	public enum InputActionType
	{
		Axis,
		Button
	}

	public enum KeybindingsCategory
	{
		Actions = 4,
		QuickSlot = 2,
		Menus = 1
	}

	public enum ControlType
	{
		Keyboard,
		Gamepad,
		Both
	}

	public struct InputActionDescription
	{
		public string name;

		public int categoryId;

		public int sectionId;

		public ControlType controlType;

		public InputActionType type;

		public InputActionDescription(string name, int categoryId, ControlType controlType = ControlType.Keyboard, int sectionId = 0, InputActionType type = InputActionType.Button)
		{
			this.name = name;
			this.categoryId = categoryId;
			this.controlType = controlType;
			this.sectionId = sectionId;
			this.type = type;
		}
	}

	private delegate void d_Initialize(InputManager_Base self);

	public static ManualLogSource MyLogger = Logger.CreateLogSource("CustomKeyBindings");

	public static Dictionary<int, RewiredInputs> m_playerInputManager = (Dictionary<int, RewiredInputs>)typeof(ControlsInput).GetField("m_playerInputManager", BindingFlags.Static | BindingFlags.NonPublic).GetValue(null);

	public static List<InputActionDescription> myActionInfos = new List<InputActionDescription>();

	public static Dictionary<int, InputActionDescription> myCustomActionIds = new Dictionary<int, InputActionDescription>();

	private static Detour h_Initialize;

	private static d_Initialize orig_Initialize;

	public static void AddAction(string name, KeybindingsCategory categoryId, ControlType controlType, int sectionId = 0, InputActionType type = InputActionType.Button)
	{
		if (orig_Initialize == null)
		{
			InitHooks();
		}
		InputActionDescription item = new InputActionDescription(name, (int)categoryId, controlType, sectionId, type);
		myActionInfos.Add(item);
		if (false && (bool)typeof(ReInput).GetProperty("initialized", BindingFlags.Static | BindingFlags.NonPublic).GetGetMethod(nonPublic: true).Invoke(null, null))
		{
			MyLogger.LogDebug((object)"Tried to add action too late. Add your action earlier in the life cycle.");
		}
	}

	private static void InitHooks()
	{
		//IL_002c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0031: Unknown result type (might be due to invalid IL or missing references)
		//IL_0037: Expected O, but got Unknown
		Detour val = new Detour((MethodBase)typeof(InputManager_Base).GetMethod("Initialize", BindingFlags.Instance | BindingFlags.NonPublic), (MethodBase)typeof(CustomKeybindings).GetMethod("InputManager_Base_Initialize", BindingFlags.Static | BindingFlags.NonPublic));
		h_Initialize = val;
		orig_Initialize = val.GenerateTrampoline<d_Initialize>();
	}

	private static void InputManager_Base_Initialize(InputManager_Base self)
	{
		foreach (InputActionDescription myActionInfo in myActionInfos)
		{
			InputAction val = AddRewiredAction(self.userData, myActionInfo.name, myActionInfo.type, "No descriptiveName", myActionInfo.categoryId, userAssignable: true);
			myCustomActionIds[val.id] = myActionInfo;
		}
		orig_Initialize(self);
	}

	private static InputAction AddRewiredAction(UserData userData, string name, InputActionType type, string descriptiveName, int categoryId, bool userAssignable)
	{
		userData.AddAction(categoryId);
		int num = userData.GetActionIds()[^1];
		InputAction actionById = userData.GetActionById(num);
		if (!string.IsNullOrEmpty(name))
		{
			FieldInfo field = ((object)actionById).GetType().GetField("_name", BindingFlags.Instance | BindingFlags.NonPublic);
			field.SetValue(actionById, name);
		}
		if (type != InputActionType.Button)
		{
			FieldInfo field2 = ((object)actionById).GetType().GetField("_type", BindingFlags.Instance | BindingFlags.NonPublic);
			field2.SetValue(actionById, type);
		}
		if (!string.IsNullOrEmpty(descriptiveName))
		{
			FieldInfo field3 = ((object)actionById).GetType().GetField("_descriptiveName", BindingFlags.Instance | BindingFlags.NonPublic);
			field3.SetValue(actionById, descriptiveName);
		}
		if (!userAssignable)
		{
			FieldInfo field4 = ((object)actionById).GetType().GetField("_userAssignable", BindingFlags.Instance | BindingFlags.NonPublic);
			field4.SetValue(actionById, userAssignable);
		}
		return actionById;
	}
}
[HarmonyPatch(typeof(ControlMappingPanel), "InitSections")]
public class ControlMappingPanel_InitSections
{
	[HarmonyPrefix]
	public static bool InitSections(ControllerMap _controllerMap)
	{
		foreach (int key in CustomKeybindings.myCustomActionIds.Keys)
		{
			CustomKeybindings.InputActionDescription inputActionDescription = CustomKeybindings.myCustomActionIds[key];
			if (((inputActionDescription.controlType == CustomKeybindings.ControlType.Keyboard && (_controllerMap is KeyboardMap || _controllerMap is MouseMap)) || (inputActionDescription.controlType == CustomKeybindings.ControlType.Gamepad && _controllerMap is JoystickMap) || inputActionDescription.controlType == CustomKeybindings.ControlType.Both) && _controllerMap.categoryId == inputActionDescription.sectionId)
			{
				_controllerMap.CreateElementMap(key, (Pole)0, (KeyCode)0, (ModifierKeyFlags)0);
			}
		}
		return true;
	}
}
[HarmonyPatch(typeof(LocalizationManager), "StartLoading")]
public class LocalizationManager_StartLoading
{
	[HarmonyPostfix]
	public static void StartLoading(LocalizationManager __instance)
	{
		Dictionary<string, string> dictionary = (Dictionary<string, string>)((object)__instance).GetType().GetField("m_generalLocalization", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(__instance);
		foreach (int key in CustomKeybindings.myCustomActionIds.Keys)
		{
			InputAction action = ReInput.mapping.GetAction(key);
			CustomKeybindings.InputActionDescription inputActionDescription = CustomKeybindings.myCustomActionIds[key];
			dictionary.Add("InputAction_" + action.name, inputActionDescription.name);
		}
	}
}
[BepInPlugin("com.Exfinite.ResetBreakthroughs", "Reset Breakthrough Points", "1.0")]
public class ResetBreakthroughs : BaseUnityPlugin
{
	internal void Awake()
	{
		//IL_0005: Unknown result type (might be due to invalid IL or missing references)
		//IL_000b: Expected O, but got Unknown
		Harmony val = new Harmony("com.Exfinite.ResetBreakthroughs");
		val.PatchAll();
		CustomKeybindings.AddAction("Reset Breakthroughs", CustomKeybindings.KeybindingsCategory.Menus, CustomKeybindings.ControlType.Both, 5);
	}

	internal void Update()
	{
		foreach (PlayerSystem item in Global.Lobby.PlayersInLobby.Where((PlayerSystem x) => x.ControlledCharacter.IsLocalPlayer))
		{
			Character controlledCharacter = item.ControlledCharacter;
			if (CustomKeybindings.m_playerInputManager[controlledCharacter.OwnerPlayerSys.PlayerID].GetButtonDown("Reset Breakthroughs"))
			{
				FieldInfo field = typeof(PlayerCharacterStats).GetField("m_usedBreakthroughCount", BindingFlags.Instance | BindingFlags.NonPublic);
				field.SetValue(controlledCharacter.CharacterControl.Character.PlayerStats, 0);
			}
		}
	}
}