Decompiled source of NoCorpseHigherQuota v2.6.0

NoCorpseHigherQuota.dll

Decompiled 4 months ago
using System;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using GameNetcodeStuff;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using NoCorpseHigherQuota.Configs;
using NoCorpseHigherQuota.Patches;
using TMPro;
using Unity.Collections;
using Unity.Netcode;
using UnityEngine;

[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("NoCorpseHigherQuota")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyDescription("Increases the quota for each unrecovered body dynamically")]
[assembly: AssemblyFileVersion("2.6.0.0")]
[assembly: AssemblyInformationalVersion("2.6.0")]
[assembly: AssemblyProduct("NoCorpseHigherQuota")]
[assembly: AssemblyTitle("NoCorpseHigherQuota")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("2.6.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 NoCorpseHigherQuota
{
	[BepInPlugin("NoCorpseHigherQuota", "NoCorpseHigherQuota", "2.6.0")]
	public class NoCorpseHigherQuota : BaseUnityPlugin
	{
		private readonly Harmony _harmony = new Harmony("NoCorpseHigherQuota");

		public static NoCorpseHigherQuota Instance;

		internal static ManualLogSource Mls;

		public static Config MyConfig { get; internal set; }

		private void Awake()
		{
			if ((Object)(object)Instance == (Object)null)
			{
				Instance = this;
			}
			MyConfig = new Config(((BaseUnityPlugin)this).Config);
			Mls = Logger.CreateLogSource("NoCorpseHigherQuota");
			Mls.LogInfo((object)"NoCorpseHigherQuota is loaded - version 2.6.0");
			_harmony.PatchAll(typeof(NoCorpseHigherQuota));
			_harmony.PatchAll(typeof(LeavingBodyPatch));
			_harmony.PatchAll(typeof(ReportPatch));
			_harmony.PatchAll(typeof(Config));
			_harmony.PatchAll(typeof(NewQuotaPatch));
		}
	}
	public static class PluginInfo
	{
		public const string PLUGIN_GUID = "NoCorpseHigherQuota";

		public const string PLUGIN_NAME = "NoCorpseHigherQuota";

		public const string PLUGIN_VERSION = "2.6.0";
	}
}
namespace NoCorpseHigherQuota.Patches
{
	[HarmonyPatch(typeof(StartOfRound))]
	internal class LeavingBodyPatch
	{
		public static int Num;

		public static int Iter;

		[HarmonyPatch("ShipHasLeft")]
		[HarmonyPostfix]
		private static void SumQuota(StartOfRound __instance)
		{
			int num = __instance.gameStats.daysSpent + 1;
			int num2 = Math.Abs(__instance.livingPlayers - GameNetworkManager.Instance.connectedPlayers);
			int bodiesInShip = GetBodiesInShip();
			int profitQuota = TimeOfDay.Instance.profitQuota;
			if (num2 != 0 && bodiesInShip != num2)
			{
				Iter = 0;
				if (SyncedInstance<Config>.Instance.Configdynamic)
				{
					int value = (num * 5 + num2 * SyncedInstance<Config>.Instance.Configcost - bodiesInShip * (SyncedInstance<Config>.Instance.Configcost / 2)) * (TimeOfDay.Instance.profitQuota / profitQuota);
					Num += Math.Abs(value);
					TimeOfDay instance = TimeOfDay.Instance;
					instance.profitQuota += Math.Abs(value);
					return;
				}
				while (Iter < num2)
				{
					TimeOfDay instance2 = TimeOfDay.Instance;
					instance2.profitQuota += SyncedInstance<Config>.Instance.Configcost;
					Num += SyncedInstance<Config>.Instance.Configcost;
					Iter++;
				}
			}
			else
			{
				NoCorpseHigherQuota.Mls.LogMessage((object)"No deaths :D");
			}
		}

		private static int GetBodiesInShip()
		{
			int num = 0;
			DeadBodyInfo[] array = Object.FindObjectsOfType<DeadBodyInfo>();
			for (int i = 0; i < array.Length; i++)
			{
				if (array[i].isInShip)
				{
					num++;
				}
			}
			return num;
		}
	}
	[HarmonyPatch(typeof(TimeOfDay))]
	internal class NewQuotaPatch
	{
		[HarmonyPatch("SetNewProfitQuota")]
		[HarmonyPrefix]
		private static void CalculateProfitQuota()
		{
			if (!SyncedInstance<Config>.Instance.Confignewquota && LeavingBodyPatch.Num != 0)
			{
				TimeOfDay instance = TimeOfDay.Instance;
				instance.profitQuota -= LeavingBodyPatch.Num;
				LeavingBodyPatch.Num = 0;
			}
		}
	}
	[HarmonyPatch(typeof(HUDManager))]
	internal class ReportPatch
	{
		[HarmonyPatch("ApplyPenalty")]
		[HarmonyPostfix]
		private static void CancelAnimation(ref EndOfGameStatUIElements ___statsUIElements)
		{
			TextMeshProUGUI penaltyAddition = ___statsUIElements.penaltyAddition;
			((TMP_Text)penaltyAddition).text = ((TMP_Text)penaltyAddition).text + "\nUnrecovered body/s: " + LeavingBodyPatch.Iter + "\nQuota has increased by " + LeavingBodyPatch.Num;
		}
	}
}
namespace NoCorpseHigherQuota.Configs
{
	[Serializable]
	public class Config : SyncedInstance<Config>
	{
		[CompilerGenerated]
		private static class <>O
		{
			public static HandleNamedMessageDelegate <0>__OnRequestSync;

			public static HandleNamedMessageDelegate <1>__OnReceiveSync;
		}

		public int Configcost { get; private set; }

		public bool Configdynamic { get; private set; }

		public bool Confignewquota { get; private set; }

		public Config(ConfigFile cfg)
		{
			InitInstance(this);
			Configcost = cfg.Bind<int>("Costs", "Priceperbody", 30, "This value is the cost per body lost.").Value;
			Configdynamic = cfg.Bind<bool>("Dynamic", "Allow", true, "This allow/deny if you want to dynamically increase the quota per unrecovered body depending on how much days have passed. If False it will only count the unrecovered corpses.").Value;
			Confignewquota = cfg.Bind<bool>("NewQuotaDependsOn", "Allow", false, "When setting a new quota on deadline. If false the increase will NOT be counting on the increase on unrecovered bodies when recalculating.").Value;
		}

		public static void RequestSync()
		{
			//IL_0029: Unknown result type (might be due to invalid IL or missing references)
			if (!SyncedInstance<Config>.IsClient)
			{
				return;
			}
			FastBufferWriter val = default(FastBufferWriter);
			((FastBufferWriter)(ref val))..ctor(SyncedInstance<Config>.IntSize, (Allocator)2, -1);
			try
			{
				SyncedInstance<Config>.MessageManager.SendNamedMessage("ModName_OnRequestConfigSync", 0uL, val, (NetworkDelivery)3);
			}
			finally
			{
				((IDisposable)(FastBufferWriter)(ref val)).Dispose();
			}
		}

		public static void OnRequestSync(ulong clientId, FastBufferReader _)
		{
			//IL_0053: Unknown result type (might be due to invalid IL or missing references)
			//IL_0059: Unknown result type (might be due to invalid IL or missing references)
			//IL_0077: Unknown result type (might be due to invalid IL or missing references)
			if (!SyncedInstance<Config>.IsHost)
			{
				return;
			}
			NoCorpseHigherQuota.Mls.LogInfo((object)$"Config sync request received from client: {clientId}");
			byte[] array = SyncedInstance<Config>.SerializeToBytes(SyncedInstance<Config>.Instance);
			int num = array.Length;
			FastBufferWriter val = default(FastBufferWriter);
			((FastBufferWriter)(ref val))..ctor(num + SyncedInstance<Config>.IntSize, (Allocator)2, -1);
			try
			{
				((FastBufferWriter)(ref val)).WriteValueSafe<int>(ref num, default(ForPrimitives));
				((FastBufferWriter)(ref val)).WriteBytesSafe(array, -1, 0);
				SyncedInstance<Config>.MessageManager.SendNamedMessage("ModName_OnReceiveConfigSync", clientId, val, (NetworkDelivery)4);
			}
			catch (Exception arg)
			{
				NoCorpseHigherQuota.Mls.LogInfo((object)$"Error occurred syncing config with client: {clientId}\n{arg}");
			}
			finally
			{
				((IDisposable)(FastBufferWriter)(ref val)).Dispose();
			}
		}

		public static void OnReceiveSync(ulong _, FastBufferReader reader)
		{
			//IL_002d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0033: Unknown result type (might be due to invalid IL or missing references)
			if (!((FastBufferReader)(ref reader)).TryBeginRead(SyncedInstance<Config>.IntSize))
			{
				NoCorpseHigherQuota.Mls.LogError((object)"Config sync error: Could not begin reading buffer.");
				return;
			}
			int num = default(int);
			((FastBufferReader)(ref reader)).ReadValueSafe<int>(ref num, default(ForPrimitives));
			if (!((FastBufferReader)(ref reader)).TryBeginRead(num))
			{
				NoCorpseHigherQuota.Mls.LogError((object)"Config sync error: Host could not sync.");
				return;
			}
			byte[] data = new byte[num];
			((FastBufferReader)(ref reader)).ReadBytesSafe(ref data, num, 0);
			SyncedInstance<Config>.SyncInstance(data);
			NoCorpseHigherQuota.Mls.LogInfo((object)"Successfully synced config with host.");
		}

		[HarmonyPostfix]
		[HarmonyPatch(typeof(PlayerControllerB), "ConnectClientToPlayerObject")]
		public static void InitializeLocalPlayer()
		{
			//IL_0060: Unknown result type (might be due to invalid IL or missing references)
			//IL_0065: Unknown result type (might be due to invalid IL or missing references)
			//IL_006b: Expected O, but got Unknown
			//IL_0025: Unknown result type (might be due to invalid IL or missing references)
			//IL_002a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0030: Expected O, but got Unknown
			if (SyncedInstance<Config>.IsHost)
			{
				CustomMessagingManager messageManager = SyncedInstance<Config>.MessageManager;
				object obj = <>O.<0>__OnRequestSync;
				if (obj == null)
				{
					HandleNamedMessageDelegate val = OnRequestSync;
					<>O.<0>__OnRequestSync = val;
					obj = (object)val;
				}
				messageManager.RegisterNamedMessageHandler("ModName_OnRequestConfigSync", (HandleNamedMessageDelegate)obj);
				SyncedInstance<Config>.Synced = true;
				return;
			}
			SyncedInstance<Config>.Synced = false;
			CustomMessagingManager messageManager2 = SyncedInstance<Config>.MessageManager;
			object obj2 = <>O.<1>__OnReceiveSync;
			if (obj2 == null)
			{
				HandleNamedMessageDelegate val2 = OnReceiveSync;
				<>O.<1>__OnReceiveSync = val2;
				obj2 = (object)val2;
			}
			messageManager2.RegisterNamedMessageHandler("ModName_OnReceiveConfigSync", (HandleNamedMessageDelegate)obj2);
			RequestSync();
		}

		[HarmonyPostfix]
		[HarmonyPatch(typeof(GameNetworkManager), "StartDisconnect")]
		public static void PlayerLeave()
		{
			SyncedInstance<Config>.RevertSync();
		}
	}
	[Serializable]
	public class SyncedInstance<T>
	{
		[NonSerialized]
		protected static int IntSize = 4;

		internal static CustomMessagingManager MessageManager => NetworkManager.Singleton.CustomMessagingManager;

		internal static bool IsClient => NetworkManager.Singleton.IsClient;

		internal static bool IsHost => NetworkManager.Singleton.IsHost;

		public static T Default { get; private set; }

		public static T Instance { get; private set; }

		public static bool Synced { get; internal set; }

		protected void InitInstance(T instance)
		{
			Default = instance;
			Instance = instance;
			IntSize = 4;
		}

		internal static void SyncInstance(byte[] data)
		{
			Instance = DeserializeFromBytes(data);
			Synced = true;
		}

		internal static void RevertSync()
		{
			Instance = Default;
			Synced = false;
		}

		public static byte[] SerializeToBytes(T val)
		{
			BinaryFormatter binaryFormatter = new BinaryFormatter();
			using MemoryStream memoryStream = new MemoryStream();
			try
			{
				binaryFormatter.Serialize(memoryStream, val);
				return memoryStream.ToArray();
			}
			catch (Exception arg)
			{
				NoCorpseHigherQuota.Mls.LogError((object)$"Error serializing instance: {arg}");
				return null;
			}
		}

		public static T DeserializeFromBytes(byte[] data)
		{
			BinaryFormatter binaryFormatter = new BinaryFormatter();
			using MemoryStream serializationStream = new MemoryStream(data);
			try
			{
				return (T)binaryFormatter.Deserialize(serializationStream);
			}
			catch (Exception arg)
			{
				NoCorpseHigherQuota.Mls.LogError((object)$"Error deserializing instance: {arg}");
				return default(T);
			}
		}
	}
}