Decompiled source of RegalsSonorousTweaks v1.0.0

plugins/RegalsSonorousTweaks/RegalsSonorousTweaks.dll

Decompiled a day ago
using System;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Logging;
using Microsoft.CodeAnalysis;
using Mono.Cecil.Cil;
using MonoMod.Cil;
using MonoMod.RuntimeDetour.HookGen;
using RoR2;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("RegalsSonorousTweaks")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("RegalsSonorousTweaks")]
[assembly: AssemblyTitle("RegalsSonorousTweaks")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.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.Module, AllowMultiple = false, Inherited = false)]
	internal sealed class RefSafetyRulesAttribute : Attribute
	{
		public readonly int Version;

		public RefSafetyRulesAttribute(int P_0)
		{
			Version = P_0;
		}
	}
}
namespace RegalsSonorousTweaks
{
	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);
		}
	}
	[BepInPlugin("RegalTurtle.RegalsSonorousTweaks", "RegalsSonorousTweaks", "1.0.0")]
	public class RegalsSonorousTweaks : BaseUnityPlugin
	{
		public const string PluginGUID = "RegalTurtle.RegalsSonorousTweaks";

		public const string PluginAuthor = "RegalTurtle";

		public const string PluginName = "RegalsSonorousTweaks";

		public const string PluginVersion = "1.0.0";

		public static readonly MethodInfo OnCharacterDeath = typeof(GlobalEventManager).GetMethod("OnCharacterDeath", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);

		public void Awake()
		{
			Log.Init(((BaseUnityPlugin)this).Logger);
			if (OnCharacterDeath == null)
			{
				Log.Error("[Init] Failed to find method: GlobalEventManager.OnCharacterDeath");
			}
		}

		public void Start()
		{
			HookEndpointManager.Modify((MethodBase)OnCharacterDeath, (Delegate)new Action<ILContext>(OnCharacterDeathEdit));
		}

		public void Destroy()
		{
			HookEndpointManager.Unmodify((MethodBase)OnCharacterDeath, (Delegate)new Action<ILContext>(OnCharacterDeathEdit));
		}

		private static int CalculateTeamSonorouses(TeamIndex teamIndex)
		{
			//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)
			int num = 0;
			ReadOnlyCollection<CharacterMaster> readOnlyInstancesList = CharacterMaster.readOnlyInstancesList;
			for (int i = 0; i < readOnlyInstancesList.Count; i++)
			{
				CharacterMaster val = readOnlyInstancesList[i];
				if (val.teamIndex == teamIndex)
				{
					num += val.inventory.GetItemCountEffective(Items.ItemDropChanceOnKill);
				}
			}
			return num;
		}

		private static void OnCharacterDeathEdit(ILContext il)
		{
			//IL_0002: Unknown result type (might be due to invalid IL or missing references)
			//IL_0008: Expected O, but got Unknown
			//IL_01b0: Unknown result type (might be due to invalid IL or missing references)
			ILCursor val = new ILCursor(il);
			ILLabel val2 = default(ILLabel);
			if (!val.TryGotoNext((MoveType)0, new Func<Instruction, bool>[5]
			{
				(Instruction x) => ILPatternMatchingExt.MatchCallvirt<CharacterMaster>(x, "get_inventory"),
				(Instruction x) => ILPatternMatchingExt.MatchLdsfld(x, typeof(Items), "ItemDropChanceOnKill"),
				(Instruction x) => ILPatternMatchingExt.MatchCallvirt<Inventory>(x, "GetItemCountEffective"),
				(Instruction x) => ILPatternMatchingExt.MatchLdcI4(x, 0),
				(Instruction x) => ILPatternMatchingExt.MatchBle(x, ref val2)
			}))
			{
				Log.Error("Failed to find Sonorous Whispers OnCharacterDeath hook (condition)");
				return;
			}
			val.EmitDelegate<Func<CharacterMaster, int>>((Func<CharacterMaster, int>)((CharacterMaster attackerMaster) => CalculateTeamSonorouses(attackerMaster.teamIndex)));
			val.RemoveRange(3);
			int num = default(int);
			if (!val.TryGotoNext((MoveType)2, new Func<Instruction, bool>[4]
			{
				(Instruction x) => ILPatternMatchingExt.MatchLdloc(x, ref num),
				(Instruction x) => ILPatternMatchingExt.MatchCallvirt<CharacterMaster>(x, "get_inventory"),
				(Instruction x) => ILPatternMatchingExt.MatchLdsfld(x, typeof(Items), "ItemDropChanceOnKill"),
				(Instruction x) => ILPatternMatchingExt.MatchCallvirt<Inventory>(x, "GetItemCountEffective")
			}))
			{
				Log.Error("Failed to find Sonorous Whispers OnCharacterDeath hook (item amt calc)");
				return;
			}
			val.Emit(OpCodes.Ldloc_S, (byte)17);
			val.EmitDelegate<Func<int, CharacterMaster, int>>((Func<int, CharacterMaster, int>)((int _prevCount, CharacterMaster attackerMaster) => CalculateTeamSonorouses(attackerMaster.teamIndex)));
		}
	}
}