Decompiled source of CompanyLottery v1.0.3

BepInEx/plugins/Company_lottery.dll

Decompiled a day ago
using System;
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.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using LobbyCompatibility.Attributes;
using Microsoft.CodeAnalysis;
using Unity.Netcode;
using UnityEngine;
using UnityEngine.EventSystems;

[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("Company_lottery")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("Company Lottery")]
[assembly: AssemblyTitle("Company_lottery")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.0.0")]
[module: UnverifiableCode]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[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]
	[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]
	[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 Company_Lottery
{
	[BepInPlugin("Company_lottery", "Company Lottery", "1.0.0")]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	[LobbyCompatibility(/*Could not decode attribute arguments.*/)]
	public class CompanyLotteryPlugin : BaseUnityPlugin
	{
		public static CompanyLotteryPlugin Instance { get; private set; }

		internal static ManualLogSource Logger { get; private set; }

		internal static Harmony? Harmony { get; set; }

		internal static ConfigEntry<float> RewardChance { get; private set; }

		private void Awake()
		{
			Logger = ((BaseUnityPlugin)this).Logger;
			Instance = this;
			RewardChance = ((BaseUnityPlugin)this).Config.Bind<float>("Lottery", "RewardChance", 50f, "Chance for the lottery to award credits. 50 = 50% chance. 0 = never, 100 = always");
			Patch();
			Logger.LogInfo((object)"Company_lottery v1.0.0 has loaded!");
		}

		internal static void Patch()
		{
			//IL_000d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0012: Unknown result type (might be due to invalid IL or missing references)
			//IL_0018: Expected O, but got Unknown
			if (Harmony == null)
			{
				Harmony = new Harmony("Company_lottery");
			}
			Harmony.PatchAll();
		}

		internal static void Unpatch()
		{
			Harmony? harmony = Harmony;
			if (harmony != null)
			{
				harmony.UnpatchSelf();
			}
		}
	}
	[HarmonyPatch(typeof(StartOfRound), "ShipLeave")]
	internal class ShipLeavePatch
	{
		[HarmonyPostfix]
		[HarmonyWrapSafe]
		private static void Postfix()
		{
			CompanyLotteryManager.ExecuteLottery();
		}
	}
	[HarmonyPatch(typeof(StartOfRound), "ShipLeaveAutomatically")]
	internal class ShipLeaveAutoPatch
	{
		[HarmonyPostfix]
		[HarmonyWrapSafe]
		private static void Postfix()
		{
			CompanyLotteryManager.ExecuteLottery();
		}
	}
	[HarmonyPatch(typeof(HUDManager), "SubmitChat_performed")]
	internal class ChatCommandPatch
	{
		[HarmonyPrefix]
		[HarmonyWrapSafe]
		private static bool Prefix(HUDManager __instance)
		{
			if ((Object)(object)__instance == (Object)null || (Object)(object)__instance.chatTextField == (Object)null)
			{
				return true;
			}
			string text = __instance.chatTextField.text.Trim();
			if (!text.StartsWith("/"))
			{
				return true;
			}
			text = text.TrimStart('/').Trim();
			if (!text.Equals("credits", StringComparison.OrdinalIgnoreCase))
			{
				return true;
			}
			ResetChat(__instance);
			if (!CompanyLotteryManager.CanRoll())
			{
				__instance.DisplayTip("Access Denied", "Only the host can roll the /credits pool.", true, false, "LC_Tip1");
				return false;
			}
			CompanyLotteryManager.ExecuteLottery();
			return false;
		}

		private static void ResetChat(HUDManager hud)
		{
			if ((Object)(object)hud.localPlayer != (Object)null)
			{
				hud.localPlayer.isTypingChat = false;
			}
			if ((Object)(object)hud.chatTextField != (Object)null)
			{
				hud.chatTextField.text = "";
			}
			((Behaviour)hud.typingIndicator).enabled = false;
			hud.PingHUDElement(hud.Chat, 1f, 1f, 0.2f);
			if ((Object)(object)EventSystem.current != (Object)null)
			{
				EventSystem.current.SetSelectedGameObject((GameObject)null);
			}
		}
	}
	public static class CompanyLotteryManager
	{
		private static readonly Random rand = new Random();

		private static float lastRoll;

		public static void ExecuteLottery()
		{
			if (!CanRoll() || Time.realtimeSinceStartup - lastRoll < 2f)
			{
				return;
			}
			lastRoll = Time.realtimeSinceStartup;
			Terminal val = Object.FindObjectOfType<Terminal>();
			if ((Object)(object)val == (Object)null)
			{
				CompanyLotteryPlugin.Logger.LogWarning((object)"Terminal not found, lottery was not executed.");
				return;
			}
			double num = rand.NextDouble() * 100.0;
			string text;
			if (num < (double)CompanyLotteryPlugin.RewardChance.Value)
			{
				int num2 = rand.Next(100, 501);
				val.groupCredits += num2;
				val.SyncGroupCreditsServerRpc(val.groupCredits, val.numberOfItemsInDropship);
				text = $"[Company Lottery] Players received {num2} credits!";
			}
			else
			{
				text = "[Company Lottery] The lottery ran. No credit prize was awarded.";
			}
			CompanyLotteryPlugin.Logger.LogInfo((object)text);
			if ((Object)(object)HUDManager.Instance != (Object)null)
			{
				HUDManager.Instance.AddTextToChatOnServer(text, -1);
			}
		}

		public static bool CanRoll()
		{
			StartOfRound instance = StartOfRound.Instance;
			return (Object)(object)instance != (Object)null && (((NetworkBehaviour)instance).IsHost || ((NetworkBehaviour)instance).IsServer);
		}
	}
	public static class MyPluginInfo
	{
		public const string PLUGIN_GUID = "Company_lottery";

		public const string PLUGIN_NAME = "Company Lottery";

		public const string PLUGIN_VERSION = "1.0.0";
	}
}