Decompiled source of MajorityVote v1.1.0

MajorityVote.dll

Decompiled 5 months ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Reflection.Emit;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;

[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("MajorityVote")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyDescription("Mod for Lethal Company, that allow adjusting percentage of votes required for the ship to leave")]
[assembly: AssemblyFileVersion("1.1.0.0")]
[assembly: AssemblyInformationalVersion("1.1.0+9410d6868e36b4f0da99614dfb7d845853fe5f5b")]
[assembly: AssemblyProduct("MajorityVote")]
[assembly: AssemblyTitle("MajorityVote")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.1.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 MajorityVote
{
	public class Config
	{
		public static ConfigEntry<bool> mVoteEnabled;

		public static ConfigEntry<float> mVotePercent;

		public static ConfigEntry<int> mVoteMin;

		public static ConfigEntry<int> mVoteMax;

		public static void Load()
		{
			mVoteEnabled = Plugin.config.Bind<bool>("ShipLeaveEarly", "MVoteEnabled", true, "Will count required votes for Ship to Leave Early based on total amount of players in lobby instead of amount of dead players?\nVanilla value False.\n[Votes count functionality is fully Server-side, but Clients would need this mod too for proper value on HUD]");
			mVotePercent = Plugin.config.Bind<float>("ShipLeaveEarly", "MVotePercent", 0.5f, "The percentage of all players who need to vote for Ship to Leave Early.\nValues between 0-1. Will round amount of needed votes to the nearest integer.\nP | 1 | 0,75 | 0,65 | 0,5 | 0,35 | 0,25\n2   2     2      1     1     1      1  \n3   3     2      2     2     1      1  \n4   4     3      3     2     1      1  \n5   5     4      3     3     2      1  \n6   6     5      4     3     2      2  ");
			mVoteMin = Plugin.config.Bind<int>("ShipLeaveEarly", "MVoteMin", 1, "Minimum amount of votes for Ship to Leave Early. Useful for lobby with small amount of players.\nValues between 1-31.");
			mVoteMax = Plugin.config.Bind<int>("ShipLeaveEarly", "MVoteMax", 31, "Minimum amount of votes for Ship to Leave Early. Useful for lobby with giant amount of players.\nValues between 1-31.");
		}
	}
	public class HarmonyPatches
	{
		private static readonly Type patchType;

		static HarmonyPatches()
		{
			//IL_0014: Unknown result type (might be due to invalid IL or missing references)
			//IL_0019: Unknown result type (might be due to invalid IL or missing references)
			//IL_003d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0049: Expected O, but got Unknown
			//IL_004a: Unknown result type (might be due to invalid IL or missing references)
			//IL_006e: Unknown result type (might be due to invalid IL or missing references)
			//IL_007a: Expected O, but got Unknown
			//IL_009e: Unknown result type (might be due to invalid IL or missing references)
			//IL_00aa: Expected O, but got Unknown
			patchType = typeof(HarmonyPatches);
			Harmony val = new Harmony("LethalCompany.MrHydralisk.MajorityVote");
			val.Patch((MethodBase)AccessTools.Method(typeof(HUDManager), "SetShipLeaveEarlyVotesText", (Type[])null, (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null, new HarmonyMethod(patchType, "MajorityVote_Transpiler", (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null);
			val.Patch((MethodBase)AccessTools.Method(typeof(HUDManager), "Update", (Type[])null, (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null, new HarmonyMethod(patchType, "MajorityVote_Transpiler", (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null);
			val.Patch((MethodBase)AccessTools.Method(typeof(TimeOfDay), "SetShipLeaveEarlyServerRpc", (Type[])null, (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null, new HarmonyMethod(patchType, "MajorityVote_Transpiler", (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null);
		}

		public static IEnumerable<CodeInstruction> MajorityVote_Transpiler(IEnumerable<CodeInstruction> instructions)
		{
			//IL_0067: Unknown result type (might be due to invalid IL or missing references)
			//IL_0071: Expected O, but got Unknown
			int num = -1;
			List<CodeInstruction> list = new List<CodeInstruction>(instructions);
			for (int i = 0; i < list.Count; i++)
			{
				if (CodeInstructionExtensions.LoadsField(list[i], AccessTools.Field(typeof(StartOfRound), "livingPlayers"), false))
				{
					num = i;
					break;
				}
			}
			if (num > -1)
			{
				List<CodeInstruction> list2 = new List<CodeInstruction>();
				list2.Add(new CodeInstruction(OpCodes.Call, (object)AccessTools.Method(typeof(HarmonyPatches), "MajorityVote", (Type[])null, (Type[])null)));
				list.InsertRange(num + 2, list2);
			}
			return list.AsEnumerable();
		}

		public static int MajorityVote(int orig)
		{
			ConfigEntry<bool> mVoteEnabled = Config.mVoteEnabled;
			if (mVoteEnabled == null || mVoteEnabled.Value)
			{
				return Math.Max(Math.Min((int)MathF.Round((float)(StartOfRound.Instance.connectedPlayersAmount + 1) * (Config.mVotePercent?.Value ?? 0.5f)), Math.Min(StartOfRound.Instance.connectedPlayersAmount, Config.mVoteMax?.Value ?? 31)), Math.Max(1, Config.mVoteMin?.Value ?? 1));
			}
			return orig;
		}
	}
	[BepInPlugin("MrHydralisk.MajorityVote", "Majority Vote", "1.1.0")]
	public class Plugin : BaseUnityPlugin
	{
		private const string MOD_GUID = "MrHydralisk.MajorityVote";

		private const string MOD_NAME = "Majority Vote";

		public static Plugin instance;

		public static ManualLogSource MLogS;

		public static ConfigFile config;

		private void Awake()
		{
			MLogS = Logger.CreateLogSource("MrHydralisk.MajorityVote");
			config = ((BaseUnityPlugin)this).Config;
			Config.Load();
			instance = this;
			try
			{
				RuntimeHelpers.RunClassConstructor(typeof(HarmonyPatches).TypeHandle);
			}
			catch (Exception ex)
			{
				MLogS.LogError((object)string.Concat("Error in static constructor of ", typeof(HarmonyPatches), ": ", ex));
			}
			MLogS.LogInfo((object)"Plugin is loaded!");
		}
	}
	public static class PluginInfo
	{
		public const string PLUGIN_GUID = "MajorityVote";

		public const string PLUGIN_NAME = "MajorityVote";

		public const string PLUGIN_VERSION = "1.1.0";
	}
}