Decompiled source of RedFaradaySpur v1.0.1

RedFaradaySpur.dll

Decompiled 11 hours ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
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.Bootstrap;
using BepInEx.Configuration;
using BepInEx.Logging;
using IL.RoR2.Items;
using ItemQualities;
using LookingGlass.ItemStatsNameSpace;
using Microsoft.CodeAnalysis;
using Mono.Cecil.Cil;
using MonoMod.Cil;
using On.RoR2;
using RiskOfOptions;
using RiskOfOptions.OptionConfigs;
using RiskOfOptions.Options;
using RoR2;
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("RedFaradaySpur")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0+5a340810242ccab04fdf8e8bab0683b80263cc25")]
[assembly: AssemblyProduct("RedFaradaySpur")]
[assembly: AssemblyTitle("RedFaradaySpur")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.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 RoR2RedFaradaySpur
{
	internal static class ItemQualitiesInterop
	{
		private static bool GetSpriteFromFile(string filename, out Sprite sprite)
		{
			return RedFaradaySpurPlugin.GetSpriteFromFile(filename, out sprite);
		}

		internal static void HandleQualityFaradaySpur()
		{
			SetQualityTiers();
			SetQualityIcons();
		}

		[MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.NoOptimization)]
		internal static void SetQualityTiers()
		{
			ItemQualityGroup jumpDamageStrike = ItemQualityGroups.JumpDamageStrike;
			ItemTierDef itemTierDef = ItemTierCatalog.GetItemTierDef((ItemTier)2);
			jumpDamageStrike.GetItemDef((QualityTier)0)._itemTierDef = itemTierDef;
			jumpDamageStrike.GetItemDef((QualityTier)1)._itemTierDef = itemTierDef;
			jumpDamageStrike.GetItemDef((QualityTier)2)._itemTierDef = itemTierDef;
			jumpDamageStrike.GetItemDef((QualityTier)3)._itemTierDef = itemTierDef;
		}

		[MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.NoOptimization)]
		internal static void SetQualityIcons()
		{
			ItemQualityGroup jumpDamageStrike = ItemQualityGroups.JumpDamageStrike;
			if (GetSpriteFromFile("texJumpDamageStrikeUncommon_new.png", out var sprite))
			{
				jumpDamageStrike.GetItemDef((QualityTier)0).pickupIconSprite = sprite;
			}
			if (GetSpriteFromFile("texJumpDamageStrikeRare_new.png", out var sprite2))
			{
				jumpDamageStrike.GetItemDef((QualityTier)1).pickupIconSprite = sprite2;
			}
			if (GetSpriteFromFile("texJumpDamageStrikeEpic_new.png", out var sprite3))
			{
				jumpDamageStrike.GetItemDef((QualityTier)2).pickupIconSprite = sprite3;
			}
			if (GetSpriteFromFile("texJumpDamageStrikeLegendary_new.png", out var sprite4))
			{
				jumpDamageStrike.GetItemDef((QualityTier)3).pickupIconSprite = sprite4;
			}
		}
	}
	internal static class Log
	{
		private static ManualLogSource _logSource;

		internal static void Init(ManualLogSource logSource)
		{
			_logSource = logSource;
		}

		internal static void Debug(object data)
		{
			_logSource.LogDebug(data);
		}

		internal static void Error(object data)
		{
			_logSource.LogError(data);
		}

		internal static void Fatal(object data)
		{
			_logSource.LogFatal(data);
		}

		internal static void Info(object data)
		{
			_logSource.LogInfo(data);
		}

		internal static void Message(object data)
		{
			_logSource.LogMessage(data);
		}

		internal static void Warning(object data)
		{
			_logSource.LogWarning(data);
		}
	}
	internal static class LookingGlassInterop
	{
		[MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.NoOptimization)]
		internal static void UpdateStats()
		{
			//IL_000a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0014: Expected I4, but got Unknown
			ItemDefinitions.allItemDefinitions[(int)Items.JumpDamageStrike.itemIndex].calculateValuesFlat = delegate(int stackCount)
			{
				float num = (RedFaradaySpurPlugin.AdjustDamage.Value ? RedFaradaySpurPlugin.BaseDamage.Value : 4f);
				float num2 = (RedFaradaySpurPlugin.AdjustDamage.Value ? RedFaradaySpurPlugin.StackDamage.Value : 2.8f);
				return new List<float>
				{
					num + num2 * (float)(stackCount - 1),
					24.8f + (float)stackCount * 7.5f,
					1f
				};
			};
		}
	}
	[BepInPlugin("SSM24.RedFaradaySpur", "RedFaradaySpur", "1.0.1")]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	public class RedFaradaySpurPlugin : BaseUnityPlugin
	{
		[Serializable]
		[CompilerGenerated]
		private sealed class <>c
		{
			public static readonly <>c <>9 = new <>c();

			public static IsDisabledDelegate <>9__12_0;

			public static IsDisabledDelegate <>9__12_1;

			public static IsDisabledDelegate <>9__12_2;

			public static Func<Instruction, bool> <>9__18_0;

			public static Func<Instruction, bool> <>9__20_0;

			public static Func<Instruction, bool> <>9__20_1;

			internal bool <Awake>b__12_0()
			{
				return !AdjustChargeSpeed.Value;
			}

			internal bool <Awake>b__12_1()
			{
				return !AdjustDamage.Value;
			}

			internal bool <Awake>b__12_2()
			{
				return !AdjustDamage.Value;
			}

			internal bool <IL_JumpDamageStrikeBodyBehavior_UpdateCharge>b__18_0(Instruction instr)
			{
				return ILPatternMatchingExt.MatchCallOrCallvirt<CharacterBody>(instr, "get_moveSpeed");
			}

			internal bool <IL_JumpDamageStrikeBodyBehavior_DischargeEffects>b__20_0(Instruction instr)
			{
				return ILPatternMatchingExt.MatchLdcR4(instr, 4f);
			}

			internal bool <IL_JumpDamageStrikeBodyBehavior_DischargeEffects>b__20_1(Instruction instr)
			{
				return ILPatternMatchingExt.MatchLdcR4(instr, 2.8f);
			}
		}

		public const string PluginGUID = "SSM24.RedFaradaySpur";

		public const string PluginAuthor = "SSM24";

		public const string PluginName = "RedFaradaySpur";

		public const string PluginVersion = "1.0.1";

		internal static RedFaradaySpurPlugin Instance;

		internal bool ItemQualitiesLoaded;

		internal bool LookingGlassLoaded;

		public static ConfigEntry<bool> AdjustChargeSpeed;

		public static ConfigEntry<float> ChargeSpeedMult;

		public static ConfigEntry<bool> AdjustDamage;

		public static ConfigEntry<float> BaseDamage;

		public static ConfigEntry<float> StackDamage;

		private static readonly string[] qualityTokens = new string[5] { "", "_UNCOMMON", "_RARE", "_EPIC", "_LEGENDARY" };

		public void Awake()
		{
			//IL_016b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0175: Expected O, but got Unknown
			//IL_0175: Unknown result type (might be due to invalid IL or missing references)
			//IL_017a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0185: 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_019b: Unknown result type (might be due to invalid IL or missing references)
			//IL_01a6: Unknown result type (might be due to invalid IL or missing references)
			//IL_01b1: Unknown result type (might be due to invalid IL or missing references)
			//IL_01d6: Unknown result type (might be due to invalid IL or missing references)
			//IL_01dd: Unknown result type (might be due to invalid IL or missing references)
			//IL_01e5: Unknown result type (might be due to invalid IL or missing references)
			//IL_01ef: Expected O, but got Unknown
			//IL_01f0: Expected O, but got Unknown
			//IL_01f6: Unknown result type (might be due to invalid IL or missing references)
			//IL_0200: Expected O, but got Unknown
			//IL_0205: Unknown result type (might be due to invalid IL or missing references)
			//IL_020f: Expected O, but got Unknown
			//IL_020f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0214: Unknown result type (might be due to invalid IL or missing references)
			//IL_021f: Unknown result type (might be due to invalid IL or missing references)
			//IL_022a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0235: 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)
			//IL_024b: 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_01cb: Unknown result type (might be due to invalid IL or missing references)
			//IL_01d1: Expected O, but got Unknown
			//IL_0270: Unknown result type (might be due to invalid IL or missing references)
			//IL_0277: Unknown result type (might be due to invalid IL or missing references)
			//IL_027f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0289: Expected O, but got Unknown
			//IL_028a: Expected O, but got Unknown
			//IL_0290: Unknown result type (might be due to invalid IL or missing references)
			//IL_029a: Expected O, but got Unknown
			//IL_029a: Unknown result type (might be due to invalid IL or missing references)
			//IL_029f: Unknown result type (might be due to invalid IL or missing references)
			//IL_02aa: Unknown result type (might be due to invalid IL or missing references)
			//IL_02b5: Unknown result type (might be due to invalid IL or missing references)
			//IL_02c0: Unknown result type (might be due to invalid IL or missing references)
			//IL_02cb: Unknown result type (might be due to invalid IL or missing references)
			//IL_02d6: Unknown result type (might be due to invalid IL or missing references)
			//IL_0260: Unknown result type (might be due to invalid IL or missing references)
			//IL_0265: Unknown result type (might be due to invalid IL or missing references)
			//IL_026b: Expected O, but got Unknown
			//IL_02fb: Unknown result type (might be due to invalid IL or missing references)
			//IL_0302: Unknown result type (might be due to invalid IL or missing references)
			//IL_030a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0314: Expected O, but got Unknown
			//IL_0315: Expected O, but got Unknown
			//IL_031b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0325: Expected O, but got Unknown
			//IL_02eb: Unknown result type (might be due to invalid IL or missing references)
			//IL_02f0: Unknown result type (might be due to invalid IL or missing references)
			//IL_02f6: Expected O, but got Unknown
			//IL_0340: Unknown result type (might be due to invalid IL or missing references)
			//IL_034a: Expected O, but got Unknown
			//IL_0351: Unknown result type (might be due to invalid IL or missing references)
			//IL_035b: Expected O, but got Unknown
			//IL_0362: Unknown result type (might be due to invalid IL or missing references)
			//IL_036c: Expected O, but got Unknown
			Instance = this;
			Log.Init(((BaseUnityPlugin)this).Logger);
			ItemQualitiesLoaded = Chainloader.PluginInfos.ContainsKey("com.Gorakh.ItemQualities");
			LookingGlassLoaded = Chainloader.PluginInfos.ContainsKey("droppod.lookingglass");
			AdjustChargeSpeed = ((BaseUnityPlugin)this).Config.Bind<bool>("Stats", "Adjust Charge Speed", true, "Whether to change the speed at which Faraday Spur charges");
			ChargeSpeedMult = ((BaseUnityPlugin)this).Config.Bind<float>("Stats", "Charge Speed Multiplier", 1.6f, "The rate at which Faraday Spur should charge. Vanilla default is 1");
			AdjustDamage = ((BaseUnityPlugin)this).Config.Bind<bool>("Stats", "Adjust Explosion Damage", true, "Whether to adjust the explosion damage of Faraday Spur");
			BaseDamage = ((BaseUnityPlugin)this).Config.Bind<float>("Stats", "Base Explosion Damage", 10f, "The damage the explosion should do on the first stack. Vanilla default is 4");
			StackDamage = ((BaseUnityPlugin)this).Config.Bind<float>("Stats", "Explosion Damage Per Stack", 7f, "The damage the explosion should do for every stack beyond the first. Vanilla default is 2.8");
			AdjustChargeSpeed.SettingChanged += UpdateDesc;
			ChargeSpeedMult.SettingChanged += UpdateDesc;
			AdjustDamage.SettingChanged += UpdateDesc;
			BaseDamage.SettingChanged += UpdateDesc;
			StackDamage.SettingChanged += UpdateDesc;
			Language.onCurrentLanguageChanged += UpdateDesc;
			ModSettingsManager.AddOption((BaseOption)new CheckBoxOption(AdjustChargeSpeed));
			StepSliderConfig val = new StepSliderConfig
			{
				min = 0f,
				max = 5f,
				increment = 0.05f,
				FormatString = "{0:P0}",
				description = "The rate at which Faraday Spur should charge.\n\nVanilla default is 100%, mod default is 160%"
			};
			object obj = <>c.<>9__12_0;
			if (obj == null)
			{
				IsDisabledDelegate val2 = () => !AdjustChargeSpeed.Value;
				<>c.<>9__12_0 = val2;
				obj = (object)val2;
			}
			((BaseOptionConfig)val).checkIfDisabled = (IsDisabledDelegate)obj;
			val.remapManualInputToStep = false;
			((SliderConfig)val).TryParseDelegate = new SliderTryParse(ParsePercentage);
			StepSliderConfig val3 = val;
			ModSettingsManager.AddOption((BaseOption)new StepSliderOption(ChargeSpeedMult, val3));
			ModSettingsManager.AddOption((BaseOption)new CheckBoxOption(AdjustDamage));
			StepSliderConfig val4 = new StepSliderConfig
			{
				min = 0f,
				max = 40f,
				increment = 0.2f,
				FormatString = "{0:P0}",
				description = "The damage the explosion should do on the first stack.\n\nVanilla default is 400%, mod default is 1000%"
			};
			object obj2 = <>c.<>9__12_1;
			if (obj2 == null)
			{
				IsDisabledDelegate val5 = () => !AdjustDamage.Value;
				<>c.<>9__12_1 = val5;
				obj2 = (object)val5;
			}
			((BaseOptionConfig)val4).checkIfDisabled = (IsDisabledDelegate)obj2;
			val4.remapManualInputToStep = false;
			((SliderConfig)val4).TryParseDelegate = new SliderTryParse(ParsePercentage);
			StepSliderConfig val6 = val4;
			ModSettingsManager.AddOption((BaseOption)new StepSliderOption(BaseDamage, val6));
			StepSliderConfig val7 = new StepSliderConfig
			{
				min = 0f,
				max = 40f,
				increment = 0.2f,
				FormatString = "{0:P0}",
				description = "The damage the explosion should do for every stack beyond the first.\n\nVanilla default is 280%, mod default is 700%"
			};
			object obj3 = <>c.<>9__12_2;
			if (obj3 == null)
			{
				IsDisabledDelegate val8 = () => !AdjustDamage.Value;
				<>c.<>9__12_2 = val8;
				obj3 = (object)val8;
			}
			((BaseOptionConfig)val7).checkIfDisabled = (IsDisabledDelegate)obj3;
			val7.remapManualInputToStep = false;
			((SliderConfig)val7).TryParseDelegate = new SliderTryParse(ParsePercentage);
			StepSliderConfig val9 = val7;
			ModSettingsManager.AddOption((BaseOption)new StepSliderOption(StackDamage, val9));
			if (GetSpriteFromFile("icon.png", out var sprite))
			{
				ModSettingsManager.SetModIcon(sprite);
			}
			ItemCatalog.SetItemDefs += new hook_SetItemDefs(On_ItemCatalog_SetItemDefs);
			JumpDamageStrikeBodyBehavior.UpdateCharge += new Manipulator(IL_JumpDamageStrikeBodyBehavior_UpdateCharge);
			JumpDamageStrikeBodyBehavior.DischargeEffects += new Manipulator(IL_JumpDamageStrikeBodyBehavior_DischargeEffects);
			if (LookingGlassLoaded)
			{
				((ResourceAvailability)(ref ItemCatalog.availability)).CallWhenAvailable((Action)LookingGlassInterop.UpdateStats);
			}
		}

		private void UpdateDesc(object sender, EventArgs e)
		{
			UpdateDesc();
		}

		private void UpdateDesc()
		{
			int num = (int)(AdjustDamage.Value ? (BaseDamage.Value * 100f) : 400f);
			int num2 = (int)(AdjustDamage.Value ? (StackDamage.Value * 100f) : 280f);
			string[] array = qualityTokens;
			foreach (string text in array)
			{
				string text2 = "ITEM_JUMPDAMAGESTRIKE" + text + "_DESC";
				string text3 = "ITEM_JUMPDAMAGESTRIKE" + text + "_DESC_FMT";
				string stringFormatted = Language.GetStringFormatted(text3, new object[2] { num, num2 });
				if (stringFormatted != text3)
				{
					Language.currentLanguage.SetStringByToken(text2, stringFormatted);
				}
			}
		}

		private bool ParsePercentage(string input, CultureInfo cultureInfo, out float value)
		{
			bool num = float.TryParse(input, out value);
			if (num)
			{
				value /= 100f;
			}
			return num;
		}

		private void On_ItemCatalog_SetItemDefs(orig_SetItemDefs orig, ItemDef[] newItemDefs)
		{
			Items.JumpDamageStrike._itemTierDef = ItemTierCatalog.GetItemTierDef((ItemTier)2);
			if (GetSpriteFromFile("texJumpDamageStrike_new.png", out var sprite))
			{
				Items.JumpDamageStrike.pickupIconSprite = sprite;
			}
			if (ItemQualitiesLoaded)
			{
				ItemQualitiesInterop.HandleQualityFaradaySpur();
			}
			orig.Invoke(newItemDefs);
		}

		private void IL_JumpDamageStrikeBodyBehavior_UpdateCharge(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);
			if (val.TryGotoNext((MoveType)2, new Func<Instruction, bool>[1]
			{
				(Instruction instr) => ILPatternMatchingExt.MatchCallOrCallvirt<CharacterBody>(instr, "get_moveSpeed")
			}))
			{
				val.EmitDelegate<Func<float, float>>((Func<float, float>)ModifyMoveSpeed);
			}
			else
			{
				Log.Error("Failed to match IL in JumpDamageStrikeBodyBehavior.UpdateCharge");
			}
		}

		private float ModifyMoveSpeed(float moveSpeed)
		{
			if (AdjustChargeSpeed.Value)
			{
				moveSpeed *= ChargeSpeedMult.Value;
			}
			return moveSpeed;
		}

		private void IL_JumpDamageStrikeBodyBehavior_DischargeEffects(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);
			if (val.TryGotoNext((MoveType)2, new Func<Instruction, bool>[1]
			{
				(Instruction instr) => ILPatternMatchingExt.MatchLdcR4(instr, 4f)
			}))
			{
				val.EmitDelegate<Func<float, float>>((Func<float, float>)ModifyBaseDamage);
			}
			else
			{
				Log.Error("Failed to match base damage IL in JumpDamageStrikeBodyBehavior.DischargeEffects");
			}
			val.Index = 0;
			if (val.TryGotoNext((MoveType)2, new Func<Instruction, bool>[1]
			{
				(Instruction instr) => ILPatternMatchingExt.MatchLdcR4(instr, 2.8f)
			}))
			{
				val.EmitDelegate<Func<float, float>>((Func<float, float>)ModifyStackDamage);
			}
			else
			{
				Log.Error("Failed to match stack damage IL in JumpDamageStrikeBodyBehavior.DischargeEffects");
			}
		}

		private float ModifyBaseDamage(float baseDamage)
		{
			if (AdjustDamage.Value)
			{
				return BaseDamage.Value;
			}
			return baseDamage;
		}

		private float ModifyStackDamage(float stackDamage)
		{
			if (AdjustDamage.Value)
			{
				return StackDamage.Value;
			}
			return stackDamage;
		}

		internal static bool GetSpriteFromFile(string filename, out Sprite sprite)
		{
			//IL_0025: Unknown result type (might be due to invalid IL or missing references)
			//IL_002b: Expected O, but got Unknown
			//IL_006b: Unknown result type (might be due to invalid IL or missing references)
			//IL_007a: Unknown result type (might be due to invalid IL or missing references)
			sprite = null;
			try
			{
				using Stream stream = File.OpenRead(Path.Combine(Path.GetDirectoryName(((BaseUnityPlugin)Instance).Info.Location), filename));
				Texture2D val = new Texture2D(0, 0);
				byte[] array = new byte[stream.Length];
				stream.Read(array, 0, (int)stream.Length);
				if (ImageConversion.LoadImage(val, array))
				{
					sprite = Sprite.Create(val, new Rect(0f, 0f, (float)((Texture)val).width, (float)((Texture)val).height), new Vector2(0f, 0f));
					return true;
				}
				return false;
			}
			catch (FileNotFoundException)
			{
				Log.Error("file " + Path.Combine(filename) + " not found");
				return false;
			}
		}
	}
}