Decompiled source of EvenMoney v1.0.1

EvenMoney.dll

Decompiled 13 hours ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using RoR2;
using UnityEngine;
using UnityEngine.Networking;

[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("EvenMoney")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("EvenMoney")]
[assembly: AssemblyTitle("EvenMoney")]
[assembly: AssemblyVersion("1.0.0.0")]
[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 EvenMoney
{
	[BepInPlugin("com.dino.evenmoney", "EvenMoney", "1.0.1")]
	public class EvenMoneyPlugin : BaseUnityPlugin
	{
		public const string Guid = "com.dino.evenmoney";

		public const string Name = "EvenMoney";

		public const string Version = "1.0.1";

		internal static ManualLogSource Log;

		internal static ConfigEntry<bool> IncludeDeadPlayers;

		private static bool _splitting;

		private static int _teamMoneyDepth;

		private void Awake()
		{
			//IL_0030: Unknown result type (might be due to invalid IL or missing references)
			//IL_0036: Expected O, but got Unknown
			//IL_00d2: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e0: Expected O, but got Unknown
			//IL_015a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0171: Unknown result type (might be due to invalid IL or missing references)
			//IL_017c: Expected O, but got Unknown
			//IL_017c: Expected O, but got Unknown
			Log = ((BaseUnityPlugin)this).Logger;
			IncludeDeadPlayers = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "IncludeDeadPlayers", true, "If true, a dead player still receives their share of split gold (they will have it when they respawn). If false, only living players share it.");
			Harmony val = new Harmony("com.dino.evenmoney");
			List<MethodInfo> list = (from m in typeof(CharacterMaster).GetMethods(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)
				where m.Name == "GiveMoney"
				select m).Where(delegate(MethodInfo m)
			{
				ParameterInfo[] parameters = m.GetParameters();
				return parameters.Length >= 1 && parameters[0].ParameterType == typeof(uint);
			}).ToList();
			if (list.Count == 0)
			{
				Log.LogError((object)"Could not find CharacterMaster.GiveMoney. The game may have changed; EvenMoney is inactive.");
				return;
			}
			foreach (MethodInfo item in list)
			{
				val.Patch((MethodBase)item, new HarmonyMethod(typeof(EvenMoneyPlugin), "GiveMoneyPrefix", (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
			}
			List<MethodInfo> list2 = (from m in typeof(TeamManager).GetMethods(BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic)
				where m.Name == "GiveTeamMoney" && !m.IsAbstract && !m.ContainsGenericParameters
				select m).ToList();
			foreach (MethodInfo item2 in list2)
			{
				val.Patch((MethodBase)item2, new HarmonyMethod(typeof(EvenMoneyPlugin), "TeamMoneyPrefix", (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null, new HarmonyMethod(typeof(EvenMoneyPlugin), "TeamMoneyFinalizer", (Type[])null), (HarmonyMethod)null);
			}
			if (list2.Count == 0)
			{
				Log.LogWarning((object)"Could not find TeamManager.GiveTeamMoney. Kill gold will also be re-split (still even).");
			}
			Log.LogInfo((object)$"EvenMoney loaded. Patched {list.Count} GiveMoney and {list2.Count} GiveTeamMoney method(s).");
		}

		private static void TeamMoneyPrefix()
		{
			_teamMoneyDepth++;
		}

		private static Exception TeamMoneyFinalizer(Exception __exception)
		{
			_teamMoneyDepth = Math.Max(0, _teamMoneyDepth - 1);
			return __exception;
		}

		private static bool GiveMoneyPrefix(CharacterMaster __instance, MethodBase __originalMethod, object[] __args)
		{
			if (_splitting || _teamMoneyDepth > 0 || !NetworkServer.active || !Object.op_Implicit((Object)(object)__instance))
			{
				return true;
			}
			uint num = (uint)__args[0];
			if (num == 0)
			{
				return true;
			}
			if (!Object.op_Implicit((Object)(object)((Component)__instance).GetComponent<PlayerCharacterMasterController>()))
			{
				return true;
			}
			List<CharacterMaster> sharingPlayers = GetSharingPlayers();
			if (sharingPlayers.Count < 2 || !sharingPlayers.Contains(__instance))
			{
				return true;
			}
			_splitting = true;
			int num2 = 0;
			try
			{
				uint count = (uint)sharingPlayers.Count;
				uint num3 = num / count;
				uint num4 = num % count;
				sharingPlayers.Sort((CharacterMaster a, CharacterMaster b) => a.money.CompareTo(b.money));
				for (int num5 = 0; num5 < sharingPlayers.Count; num5++)
				{
					uint num6 = num3 + ((num5 < num4) ? 1u : 0u);
					if (num6 != 0)
					{
						object[] array = (object[])__args.Clone();
						array[0] = num6;
						__originalMethod.Invoke(sharingPlayers[num5], array);
						num2++;
					}
				}
			}
			catch (Exception arg)
			{
				Log.LogError((object)$"Split failed: {arg}");
				return num2 == 0;
			}
			finally
			{
				_splitting = false;
			}
			return false;
		}

		private static List<CharacterMaster> GetSharingPlayers()
		{
			List<CharacterMaster> list = new List<CharacterMaster>();
			foreach (PlayerCharacterMasterController instance in PlayerCharacterMasterController.instances)
			{
				if (!Object.op_Implicit((Object)(object)instance) || !instance.isConnected)
				{
					continue;
				}
				CharacterMaster master = instance.master;
				if (!Object.op_Implicit((Object)(object)master))
				{
					continue;
				}
				if (!IncludeDeadPlayers.Value)
				{
					CharacterBody body = master.GetBody();
					if (!Object.op_Implicit((Object)(object)body) || !Object.op_Implicit((Object)(object)body.healthComponent) || !body.healthComponent.alive)
					{
						continue;
					}
				}
				list.Add(master);
			}
			return list;
		}
	}
}