Decompiled source of NeedleSwapper v0.3.0

plugins/PaleOilSoap/PaleOilSoap.dll

Decompiled 5 days ago
using System;
using System.Diagnostics;
using System.IO;
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 GlobalEnums;
using GlobalSettings;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using Mono.Cecil.Cil;
using MonoMod.Cil;
using TeamCherry.Localization;
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: AssemblyCompany("PaleOilSoap")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("0.3.0.0")]
[assembly: AssemblyInformationalVersion("0.3.0+1774c6641fe93d08655897e26872f92efad5f632")]
[assembly: AssemblyProduct("PaleOilSoap")]
[assembly: AssemblyTitle("PaleOilSoap")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("0.3.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 PaleOilSoap
{
	internal sealed class Assets
	{
		public readonly AudioClip disableSound;

		public readonly AudioClip overrideSound;

		public readonly AudioClip constrainedSound;

		public bool initialized
		{
			get
			{
				if (Object.op_Implicit((Object)(object)disableSound) && Object.op_Implicit((Object)(object)overrideSound))
				{
					return Object.op_Implicit((Object)(object)constrainedSound);
				}
				return false;
			}
		}

		public Assets()
		{
			AssetBundle val = AssetBundle.LoadFromFile(Path.Combine(Application.streamingAssetsPath, "aa", "StandaloneWindows64", "sfxstatic_assets_shared.bundle"));
			if ((Object)(object)val == (Object)null)
			{
				Plugin.Logger.LogWarning((object)"Failed to locate asset bundle.");
				return;
			}
			disableSound = val.LoadAsset<AudioClip>("Assets/Audio/SFX/Heroes/Hornet/Crest Weapons/hornet_hunter_needleart_slash_2.wav");
			overrideSound = val.LoadAsset<AudioClip>("Assets/Audio/SFX/Enemy/Bosses/Hornet/hornet_needle_catch.wav");
			constrainedSound = val.LoadAsset<AudioClip>("Assets/Audio/SFX/sword_hit_reject.wav");
			val.Unload(false);
			if (initialized)
			{
				Plugin.Logger.LogDebug((object)"Located assets.");
			}
			else
			{
				Plugin.Logger.LogWarning((object)"Failed to locate assets.");
			}
		}
	}
	internal sealed class Config
	{
		private readonly ConfigFile file;

		private readonly AcceptableValueRange<int> acceptableTargetNeedleUpgradeLevels = new AcceptableValueRange<int>(-1, 4);

		private readonly ConfigEntry<int> targetNeedleUpgradeLevel;

		private readonly ConfigEntry<bool> allowTargetAboveUpgradedLevel;

		public int TargetNeedleUpgradeLevel
		{
			get
			{
				return targetNeedleUpgradeLevel.Value;
			}
			internal set
			{
				targetNeedleUpgradeLevel.Value = value;
			}
		}

		public bool AllowTargetAboveUpgradedLevel => allowTargetAboveUpgradedLevel.Value;

		internal void Reload()
		{
			Plugin.Logger.LogDebug((object)("Reloading " + file.ConfigFilePath.Substring(file.ConfigFilePath.LastIndexOf(Path.DirectorySeparatorChar) + 1)));
			file.Reload();
		}

		internal Config(ConfigFile config)
		{
			//IL_0037: Unknown result type (might be due to invalid IL or missing references)
			//IL_0041: Expected O, but got Unknown
			file = config;
			targetNeedleUpgradeLevel = config.Bind<int>("Needle", "targetNeedleUpgradeLevel", -1, new ConfigDescription("The target upgrade level to override the Needle to.\n(-1 to disable override, 0 for starting Needle, 1–4 for each Needle upgrade)", (AcceptableValueBase)(object)acceptableTargetNeedleUpgradeLevels, Array.Empty<object>()));
			allowTargetAboveUpgradedLevel = config.Bind<bool>("Needle", "allowTargetAboveUpgradedLevel", false, "Allow targetNeedleUpgradeLevel to use levels higher than has been acquired through the Pinmaster.");
		}
	}
	[HarmonyPatch]
	internal static class NailUpgrades
	{
		private static int AdjustNailUpgrade(int acquiredNailUpgrade)
		{
			Config config = Plugin.Config;
			if (config.TargetNeedleUpgradeLevel < 0)
			{
				return acquiredNailUpgrade;
			}
			if (!config.AllowTargetAboveUpgradedLevel && config.TargetNeedleUpgradeLevel > acquiredNailUpgrade)
			{
				return acquiredNailUpgrade;
			}
			return config.TargetNeedleUpgradeLevel;
		}

		[HarmonyILManipulator]
		[HarmonyPatch(typeof(InventoryItemNail), "UpdateState")]
		[HarmonyPatch(/*Could not decode attribute arguments.*/)]
		[HarmonyPatch(typeof(HealthManager), "ApplyDamageScaling")]
		private static void Redirect__PlayerData_nailUpgrades(ILContext il)
		{
			//IL_0001: Unknown result type (might be due to invalid IL or missing references)
			//IL_0007: Expected O, but got Unknown
			ILCursor val = new ILCursor(il);
			int num = default(int);
			Func<Instruction, bool>[] array = new Func<Instruction, bool>[2]
			{
				(Instruction x) => ILPatternMatchingExt.MatchLdfld<PlayerData>(x, "nailUpgrades"),
				(Instruction x) => ILPatternMatchingExt.MatchStloc(x, ref num)
			};
			if (val.TryGotoNext((MoveType)2, array))
			{
				int index = val.Index;
				val.Index = index - 1;
				val.EmitDelegate<Func<int, int>>((Func<int, int>)AdjustNailUpgrade);
			}
			else
			{
				Plugin.Logger.LogError((object)"Cannot hook: failed to match IL instructions.");
			}
		}
	}
	[BepInPlugin("itsschwer.NeedleSwapper", "NeedleSwapper", "0.3.0")]
	public sealed class Plugin : BaseUnityPlugin
	{
		public const string GUID = "itsschwer.NeedleSwapper";

		public const string Author = "itsschwer";

		public const string Name = "NeedleSwapper";

		public const string Version = "0.3.0";

		internal static ManualLogSource Logger { get; private set; }

		internal static Config Config { get; private set; }

		internal static Assets Assets { get; private set; }

		private void Awake()
		{
			//IL_004a: Unknown result type (might be due to invalid IL or missing references)
			Logger.Sources.Remove((ILogSource)(object)((BaseUnityPlugin)this).Logger);
			Logger = Logger.CreateLogSource("itsschwer.NeedleSwapper");
			Config = new Config(((BaseUnityPlugin)this).Config);
			Assets = new Assets();
			new Harmony(((BaseUnityPlugin)this).Info.Metadata.GUID).PatchAll();
			Logger.LogMessage((object)"~awake.");
		}

		private void Update()
		{
			if (Input.GetKey((KeyCode)278) && Input.GetKeyDown((KeyCode)279))
			{
				Logger.LogWarning((object)"Debugging input triggered, reloading config.");
				Config.Reload();
			}
		}
	}
	[HarmonyPatch(typeof(InventoryItemSelectable))]
	internal static class UX
	{
		[HarmonyPostfix]
		[HarmonyPatch(typeof(InventoryItemNail), "Start")]
		private static void InventoryItemNail_Start(InventoryItemNail __instance)
		{
			//IL_005b: Unknown result type (might be due to invalid IL or missing references)
			//IL_005c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0084: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b8: Unknown result type (might be due to invalid IL or missing references)
			//IL_00bd: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c8: Unknown result type (might be due to invalid IL or missing references)
			InventoryItemButtonPrompt component = ((Component)__instance).GetComponent<InventoryItemButtonPrompt>();
			if ((Object)(object)component == (Object)null)
			{
				Plugin.Logger.LogWarning((object)"Tried to set up button prompts but InventoryItemNail did not have any to copy from!");
				return;
			}
			LocalisedString responseText = default(LocalisedString);
			((LocalisedString)(ref responseText))..ctor("Tools", "UI_BUTTON_TOGGLE_STATE");
			InventoryItemButtonPrompt val = ((Component)__instance).gameObject.AddComponent<InventoryItemButtonPrompt>();
			((InventoryItemButtonPromptBase<InventoryItemButtonPromptData>)(object)val).appearCondition = ((InventoryItemButtonPromptBase<InventoryItemButtonPromptData>)(object)component).appearCondition;
			((InventoryItemButtonPromptBase<InventoryItemButtonPromptData>)(object)val).display = ((InventoryItemButtonPromptBase<InventoryItemButtonPromptData>)(object)component).display;
			((InventoryItemButtonPromptBase<InventoryItemButtonPromptData>)(object)val).data.ResponseText = responseText;
			((InventoryItemButtonPromptBase<InventoryItemButtonPromptData>)(object)val).data.Action = (HeroActionButton)((Object.op_Implicit((Object)(object)Platform.Current) && Platform.Current.WasLastInputKeyboard) ? 2 : 18);
			InventoryItemButtonPrompt obj = ((Component)__instance).gameObject.AddComponent<InventoryItemButtonPrompt>();
			((InventoryItemButtonPromptBase<InventoryItemButtonPromptData>)(object)obj).appearCondition = ((InventoryItemButtonPromptBase<InventoryItemButtonPromptData>)(object)val).appearCondition;
			((InventoryItemButtonPromptBase<InventoryItemButtonPromptData>)(object)obj).display = ((InventoryItemButtonPromptBase<InventoryItemButtonPromptData>)(object)val).display;
			((InventoryItemButtonPromptBase<InventoryItemButtonPromptData>)(object)obj).data.ResponseText = ((InventoryItemButtonPromptBase<InventoryItemButtonPromptData>)(object)val).data.ResponseText;
			((InventoryItemButtonPromptBase<InventoryItemButtonPromptData>)(object)obj).data.Action = (HeroActionButton)0;
			Plugin.Logger.LogDebug((object)"Set up button prompts.\n The preceding two instances of NullReferenceException from InventoryItemButtonPromptBase.OnEnable should be safe to ignore (no elegant workaround).");
		}

		[HarmonyPatch("Submit")]
		[HarmonyPostfix]
		private static void InventoryItemSelectable_Submit(InventoryItemSelectable __instance)
		{
			InventoryItemNail val = (InventoryItemNail)(object)((__instance is InventoryItemNail) ? __instance : null);
			if (val != null)
			{
				ChangeNail(val, 1);
			}
		}

		[HarmonyPatch("Extra")]
		[HarmonyPostfix]
		private static void InventoryItemSelectable_Extra(InventoryItemSelectable __instance)
		{
			InventoryItemNail val = (InventoryItemNail)(object)((__instance is InventoryItemNail) ? __instance : null);
			if (val != null)
			{
				ChangeNail(val, -1);
			}
		}

		private static void ChangeNail(InventoryItemNail nail, int delta)
		{
			int targetNeedleUpgradeLevel = Plugin.Config.TargetNeedleUpgradeLevel;
			Plugin.Config.TargetNeedleUpgradeLevel += delta;
			nail.UpdateState();
			((InventoryItemSelectable)nail).UpdateDisplay();
			Plugin.Logger.LogDebug((object)string.Format("Changed {0} from {1} to {2}", "TargetNeedleUpgradeLevel", targetNeedleUpgradeLevel, Plugin.Config.TargetNeedleUpgradeLevel));
			PlayAudioFeedback(targetNeedleUpgradeLevel);
		}

		private static void PlayAudioFeedback(int before)
		{
			//IL_002e: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e2: Unknown result type (might be due to invalid IL or missing references)
			if (!Plugin.Assets.initialized)
			{
				return;
			}
			Config config = Plugin.Config;
			PlayerData instance = PlayerData.instance;
			if (instance == null)
			{
				Plugin.Logger.LogWarning((object)"Tried to play audio feedback but PlayerData.instance is null!");
				return;
			}
			AudioEvent val = default(AudioEvent);
			val.PitchMin = 0.95f;
			val.PitchMax = 1.05f;
			val.Volume = 1f;
			if (config.TargetNeedleUpgradeLevel < 0)
			{
				val.Clip = Plugin.Assets.disableSound;
			}
			else if ((!config.AllowTargetAboveUpgradedLevel && config.TargetNeedleUpgradeLevel > instance.nailUpgrades) || before == config.TargetNeedleUpgradeLevel)
			{
				val.Clip = Plugin.Assets.constrainedSound;
			}
			else
			{
				val.Clip = Plugin.Assets.overrideSound;
				val.PitchMax = (val.PitchMin = 1f + 0.1f * (float)config.TargetNeedleUpgradeLevel);
			}
			((AudioEvent)(ref val)).SpawnAndPlayOneShot(Audio.DefaultUIAudioSourcePrefab, Vector3.zero, (Action)null);
		}
	}
}