Decompiled source of Custom Host Voting Power v1.0.0

plugins/CustomHostVotingPower/CustomHostVotingPower.dll

Decompiled 8 hours ago
using System;
using System.Collections.Generic;
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.Bootstrap;
using BepInEx.Configuration;
using BepInEx.Logging;
using CustomHostVotingPower.Compatibility;
using CustomHostVotingPower.Configuration;
using CustomHostVotingPower.Hooks;
using HarmonyLib;
using LethalConfig;
using LethalConfig.ConfigItems;
using LobbyCompatibility.Enums;
using LobbyCompatibility.Features;
using Microsoft.CodeAnalysis;
using MonoMod.RuntimeDetour;
using TMPro;
using Unity.Netcode;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: IgnoresAccessChecksTo("")]
[assembly: AssemblyCompany("CustomHostVotingPower.Confusified.com.GitHub")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0+1fec286e9d119407273c914d99442c73a1a4abd6")]
[assembly: AssemblyProduct("Custom Host Voting Power")]
[assembly: AssemblyTitle("CustomHostVotingPower.Confusified.com.GitHub")]
[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.Class | AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Event | AttributeTargets.Parameter | AttributeTargets.ReturnValue | AttributeTargets.GenericParameter, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableAttribute : Attribute
	{
		public readonly byte[] NullableFlags;

		public NullableAttribute(byte P_0)
		{
			NullableFlags = new byte[1] { P_0 };
		}

		public NullableAttribute(byte[] P_0)
		{
			NullableFlags = P_0;
		}
	}
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Method | AttributeTargets.Interface | AttributeTargets.Delegate, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableContextAttribute : Attribute
	{
		public readonly byte Flag;

		public NullableContextAttribute(byte P_0)
		{
			Flag = P_0;
		}
	}
	[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 CustomHostVotingPower
{
	[BepInPlugin("CustomHostVotingPower.Confusified.com.GitHub", "Custom Host Voting Power", "1.0.0")]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	public class CustomHostVotingPower : BaseUnityPlugin
	{
		private static readonly string configLocation = Utility.CombinePaths(new string[1] { Paths.ConfigPath + "\\" + "CustomHostVotingPower.Confusified.com.GitHub".Substring(22, 12).Replace(".", "\\") }) + "CustomHostVotingPower.Confusified.com.GitHub".Substring(0, 21);

		internal static ConfigFile cfg = new ConfigFile(configLocation + ".cfg", false);

		public static CustomHostVotingPower Instance { get; private set; } = null;


		internal static ManualLogSource Logger { get; private set; } = null;


		internal static List<IDetour> Hooks { get; set; } = new List<IDetour>();


		private void Awake()
		{
			Logger = ((BaseUnityPlugin)this).Logger;
			Instance = this;
			Config.Init();
			InitCompatibility();
			Hook();
			Logger.LogInfo((object)"Custom Host Voting Power v1.0.0 has loaded!");
		}

		internal static void Hook()
		{
			//IL_0048: Unknown result type (might be due to invalid IL or missing references)
			//IL_0052: Expected O, but got Unknown
			//IL_008b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0095: Expected O, but got Unknown
			//IL_00ce: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d8: Expected O, but got Unknown
			Logger.LogDebug((object)"Hooking methods");
			Hooks.Add((IDetour)new Hook((MethodBase)typeof(TimeOfDay).GetMethod("SetShipLeaveEarlyServerRpc", AccessTools.allDeclared), (Delegate)new Action<Action<TimeOfDay>, TimeOfDay>(AdjustHostVotingPower.UpdateHostVotingPower)));
			Hooks.Add((IDetour)new Hook((MethodBase)typeof(TimeOfDay).GetMethod("VoteShipToLeaveEarly", AccessTools.allDeclared), (Delegate)new Action<Action<TimeOfDay>, TimeOfDay>(AdjustHostVotingPower.CheckForHostVote)));
			Hooks.Add((IDetour)new Hook((MethodBase)typeof(HUDManager).GetMethod("Awake", AccessTools.allDeclared), (Delegate)new Action<Action<HUDManager>, HUDManager>(AdjustHostVotingPower.GetHUDManager)));
			Logger.LogDebug((object)"Finished hooking");
		}

		internal static void InitCompatibility()
		{
			foreach (string dependency in DependencyStrings.dependencyList)
			{
				if (!Chainloader.PluginInfos.ContainsKey(dependency))
				{
					continue;
				}
				if (!(dependency == "ainavt.lc.lethalconfig"))
				{
					if (dependency == "BMX.LobbyCompatibility")
					{
						RegisterLobbyCompatibility.Init();
					}
					else
					{
						Logger.LogDebug((object)"Dependency does not have any code made for it");
					}
				}
				else
				{
					RegisterLethalConfig.Init();
				}
			}
		}
	}
	internal static class DependencyStrings
	{
		internal const string LethalConfig = "ainavt.lc.lethalconfig";

		internal const string LobbyCompatibility = "BMX.LobbyCompatibility";

		internal static List<string> dependencyList = new List<string>(2) { "ainavt.lc.lethalconfig", "BMX.LobbyCompatibility" };
	}
	public static class MyPluginInfo
	{
		public const string PLUGIN_GUID = "CustomHostVotingPower.Confusified.com.GitHub";

		public const string PLUGIN_NAME = "Custom Host Voting Power";

		public const string PLUGIN_VERSION = "1.0.0";
	}
}
namespace CustomHostVotingPower.Hooks
{
	public static class AdjustHostVotingPower
	{
		private static int customVoteInt;

		private static bool hostCalledVote;

		private static int totalVotesNeeded;

		public static HUDManager hudManager;

		public static void CheckForHostVote(Action<TimeOfDay> orig, TimeOfDay self)
		{
			if (!self.votedShipToLeaveEarlyThisRound)
			{
				hostCalledVote = ((NetworkBehaviour)self).NetworkManager.IsHost;
			}
			orig(self);
			hostCalledVote = false;
		}

		public static void UpdateHostVotingPower(Action<TimeOfDay> orig, TimeOfDay self)
		{
			CustomHostVotingPower.Logger.LogDebug((object)$"host called vote: {hostCalledVote}");
			if (Config.useCustomVotingPower.Value && hostCalledVote)
			{
				StartOfRound instance = StartOfRound.Instance;
				totalVotesNeeded = int.Parse(((TMP_Text)hudManager.holdButtonToEndGameEarlyVotesText).text.Split("/")[1].Split(" ")[0]);
				if (Config.usePercentageOfTotalVotesRequired.Value)
				{
					float num = (float)Config.customVotingPowerPercentage.Value * 0.01f;
					customVoteInt = (int)MathF.Round((float)totalVotesNeeded * num);
				}
				else
				{
					customVoteInt = Config.customVotingPower.Value;
				}
				if (customVoteInt <= 0)
				{
					customVoteInt = 0;
					customVoteInt++;
				}
				self.votesForShipToLeaveEarly += customVoteInt;
				CustomHostVotingPower.Logger.LogDebug((object)$"Increased votes with {customVoteInt}");
				if (self.votesForShipToLeaveEarly >= totalVotesNeeded)
				{
					if (Config.limitCustomVotes.Value)
					{
						self.votesForShipToLeaveEarly = totalVotesNeeded;
						CustomHostVotingPower.Logger.LogDebug((object)"Amount of votes was set to the total required amount because the threshold has been reached");
					}
					self.SetShipLeaveEarlyClientRpc(self.normalizedTimeOfDay + 0.1f, self.votesForShipToLeaveEarly);
				}
				else
				{
					for (int i = 0; i < customVoteInt; i++)
					{
						self.AddVoteForShipToLeaveEarlyClientRpc();
					}
				}
			}
			else
			{
				orig(self);
			}
		}

		public static void GetHUDManager(Action<HUDManager> orig, HUDManager self)
		{
			orig(self);
			hudManager = self;
		}
	}
}
namespace CustomHostVotingPower.Configuration
{
	public static class Config
	{
		public static ConfigEntry<bool> useCustomVotingPower { get; private set; }

		public static ConfigEntry<int> customVotingPower { get; private set; }

		public static ConfigEntry<bool> usePercentageOfTotalVotesRequired { get; private set; }

		public static ConfigEntry<int> customVotingPowerPercentage { get; private set; }

		public static ConfigEntry<bool> limitCustomVotes { get; private set; }

		public static void Init()
		{
			//IL_0027: Unknown result type (might be due to invalid IL or missing references)
			//IL_002d: Expected O, but got Unknown
			CustomHostVotingPower.Logger.LogDebug((object)"Initialising config");
			ConfigFile cfg = CustomHostVotingPower.cfg;
			ConfigDescription val = new ConfigDescription("Determines how many votes will be added when you vote (as a host)", (AcceptableValueBase)(object)new AcceptableValueRange<int>(1, 100), Array.Empty<object>());
			useCustomVotingPower = cfg.Bind<bool>("Global Settings", "Use Custom Voting Power", true, "Enables custom voting power for the host. (You must be the host)");
			limitCustomVotes = cfg.Bind<bool>("Global Settings", "Do Not Go Over Vote Threshold", true, "When enabled, your voting power will be limited by the total required amount of votes");
			customVotingPower = cfg.Bind<int>("Voting Settings", "Custom Voting Power", 2, val);
			usePercentageOfTotalVotesRequired = cfg.Bind<bool>("Percentage Voting Settings", "Use Percentage Of Total Required Votes", false, "When enabled, your voting power will be equal to the percentage of required total votes");
			customVotingPowerPercentage = cfg.Bind<int>("Percentage Voting Settings", "Custom Voting Power Percentage", 50, val);
			CustomHostVotingPower.Logger.LogDebug((object)"Config initialised");
		}
	}
}
namespace CustomHostVotingPower.Compatibility
{
	public static class RegisterLethalConfig
	{
		public static void Init()
		{
			//IL_001a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0020: Expected O, but got Unknown
			//IL_0026: Unknown result type (might be due to invalid IL or missing references)
			//IL_002c: Expected O, but got Unknown
			//IL_0032: Unknown result type (might be due to invalid IL or missing references)
			//IL_0038: Expected O, but got Unknown
			//IL_003e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0044: Expected O, but got Unknown
			//IL_004a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0051: Expected O, but got Unknown
			CustomHostVotingPower.Logger.LogDebug((object)"Creating config entries");
			LethalConfigManager.SkipAutoGen();
			BoolCheckBoxConfigItem val = new BoolCheckBoxConfigItem(Config.useCustomVotingPower, false);
			BoolCheckBoxConfigItem val2 = new BoolCheckBoxConfigItem(Config.usePercentageOfTotalVotesRequired, false);
			BoolCheckBoxConfigItem val3 = new BoolCheckBoxConfigItem(Config.limitCustomVotes, false);
			IntInputFieldConfigItem val4 = new IntInputFieldConfigItem(Config.customVotingPower, false);
			IntSliderConfigItem val5 = new IntSliderConfigItem(Config.customVotingPowerPercentage, false);
			LethalConfigManager.AddConfigItem((BaseConfigItem)(object)val);
			LethalConfigManager.AddConfigItem((BaseConfigItem)(object)val2);
			LethalConfigManager.AddConfigItem((BaseConfigItem)(object)val3);
			LethalConfigManager.AddConfigItem((BaseConfigItem)(object)val4);
			LethalConfigManager.AddConfigItem((BaseConfigItem)(object)val5);
			CustomHostVotingPower.Logger.LogDebug((object)"Config entries created");
		}
	}
	public static class RegisterLobbyCompatibility
	{
		public static void Init()
		{
			CustomHostVotingPower.Logger.LogDebug((object)"Registering mod to LobbyCompatibility");
			PluginHelper.RegisterPlugin("CustomHostVotingPower.Confusified.com.GitHub", Version.Parse("1.0.0"), (CompatibilityLevel)1, (VersionStrictness)0);
			CustomHostVotingPower.Logger.LogDebug((object)"Mod registered");
		}
	}
}
namespace System.Runtime.CompilerServices
{
	[AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)]
	internal sealed class IgnoresAccessChecksToAttribute : Attribute
	{
		public IgnoresAccessChecksToAttribute(string assemblyName)
		{
		}
	}
}