Decompiled source of BuyableShotgunPlus v1.2.1

BepInEx/plugins/BuyableShotgunPlus/BuyableShotgunPlus.dll

Decompiled 4 months ago
using System;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Configuration;
using BuyableShotgun.Config;
using GameNetcodeStuff;
using HarmonyLib;
using LethalLib.Modules;
using Microsoft.CodeAnalysis;
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("BuyableShotgunPlus")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("BuyableShotgunPlus")]
[assembly: AssemblyTitle("BuyableShotgunPlus")]
[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.Module, AllowMultiple = false, Inherited = false)]
	internal sealed class RefSafetyRulesAttribute : Attribute
	{
		public readonly int Version;

		public RefSafetyRulesAttribute(int P_0)
		{
			Version = P_0;
		}
	}
}
namespace BuyableShotgunPlus
{
	public static class PluginInfo
	{
		public const string PLUGIN_GUID = "BuyableShotgunPlus";

		public const string PLUGIN_NAME = "BuyableShotgunPlus";

		public const string PLUGIN_VERSION = "1.0.0";
	}
}
namespace BuyableShotgun
{
	[BepInPlugin("Entity378.BuyableShotgunPlus", "BuyableShotgunPlus", "1.2.1")]
	[BepInDependency("evaisa.lethallib", "0.14.2")]
	public class Plugin : BaseUnityPlugin
	{
		private const string GUID = "Entity378.BuyableShotgunPlus";

		private const string NAME = "BuyableShotgunPlus";

		private const string VERSION = "1.2.1";

		private Harmony harmony = new Harmony("Entity378.BuyableShotgunPlus");

		public static Item ShotgunItem;

		public static Item ShotgunShellItem;

		private void Awake()
		{
			BuyableShotgunConfigs.LoadConfig(((BaseUnityPlugin)this).Config);
			harmony.PatchAll();
			((BaseUnityPlugin)this).Logger.LogInfo((object)"BuyableShotgunPlus Log: Config loaded");
			string text = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "shotgun");
			AssetBundle val = AssetBundle.LoadFromFile(text);
			ShotgunItem = val.LoadAsset<Item>("Assets/LethalCompany/SHOTGUN/ShotgunItem.asset");
			ShotgunShellItem = val.LoadAsset<Item>("Assets/LethalCompany/SHOTGUN/ShotgunShellItem.asset");
			NetworkPrefabs.RegisterNetworkPrefab(ShotgunItem.spawnPrefab);
			Utilities.FixMixerGroups(ShotgunItem.spawnPrefab);
			NetworkPrefabs.RegisterNetworkPrefab(ShotgunShellItem.spawnPrefab);
			Utilities.FixMixerGroups(ShotgunShellItem.spawnPrefab);
		}
	}
}
namespace BuyableShotgun.Patch
{
	[HarmonyPatch(typeof(StartOfRound))]
	internal class ShotgunPatcher
	{
		[HarmonyPostfix]
		[HarmonyPatch("Awake")]
		private static void PatchAwake(StartOfRound __instance)
		{
			if (BuyableShotgunConfigs.EnableBuyableShotgun)
			{
				Items.RegisterShopItem(Plugin.ShotgunItem, (TerminalNode)null, (TerminalNode)null, CreateInfoNode("Shotgun+", "Nutcracker's Shotgun. Can hold 2 shells. Recommended to keep safety on while not using or it might shoot randomly."), BuyableShotgunConfigs.ShotgunPrice);
			}
			if (BuyableShotgunConfigs.EnableBuyableShotgunShell)
			{
				Items.RegisterShopItem(Plugin.ShotgunShellItem, (TerminalNode)null, (TerminalNode)null, CreateInfoNode("ShotgunShell+", "Ammo for the Nutcracker's Shotgun."), BuyableShotgunConfigs.ShotgunShellPrice);
			}
		}

		private static TerminalNode CreateInfoNode(string name, string description)
		{
			TerminalNode val = ScriptableObject.CreateInstance<TerminalNode>();
			val.clearPreviousText = true;
			((Object)val).name = name;
			val.displayText = description + "\n\n";
			return val;
		}
	}
}
namespace BuyableShotgun.Config
{
	internal class BuyableShotgunConfigs
	{
		[CompilerGenerated]
		private static class <>O
		{
			public static HandleNamedMessageDelegate <0>__OnRequestSync;

			public static HandleNamedMessageDelegate <1>__OnReceiveSync;
		}

		private const int byteDimInt = 4;

		private const int byteDimBool = 1;

		private const int byteDimConfig = 11;

		private static int ShotgunPriceLocal = 666;

		private static int ShotgunShellPriceLocal = 20;

		private static bool EnableBuyableShotgunLocal = true;

		private static bool EnableBuyableShotgunShellLocal = true;

		public static int ShotgunPrice = 666;

		public static int ShotgunShellPrice = 20;

		public static bool EnableBuyableShotgun = true;

		public static bool EnableBuyableShotgunShell = true;

		private static void SetValues(int PriceShotgun, int PriceShotgunShell, bool EnableShotgun, bool EnableShotgunShell)
		{
			ShotgunPrice = PriceShotgun;
			ShotgunShellPrice = PriceShotgunShell;
			EnableBuyableShotgun = EnableShotgun;
			EnableBuyableShotgunShell = EnableShotgunShell;
		}

		private static void SetToLocalValues()
		{
			SetValues(ShotgunPriceLocal, ShotgunShellPriceLocal, EnableBuyableShotgunLocal, EnableBuyableShotgunShellLocal);
		}

		public static void LoadConfig(ConfigFile config)
		{
			ShotgunPriceLocal = config.Bind<int>("General", "ShotgunPrice", 666, (ConfigDescription)null).Value;
			ShotgunShellPriceLocal = config.Bind<int>("General", "ShotgunShellPrice", 20, (ConfigDescription)null).Value;
			EnableBuyableShotgunLocal = config.Bind<bool>("General", "EnableBuyableShotgun", true, (ConfigDescription)null).Value;
			EnableBuyableShotgunShellLocal = config.Bind<bool>("General", "EnableBuyableShotgunShell", true, (ConfigDescription)null).Value;
			SetToLocalValues();
		}

		public static byte[] GetSettings()
		{
			byte[] array = new byte[11]
			{
				1, 0, 0, 0, 0, 0, 0, 0, 0, 0,
				0
			};
			Array.Copy(BitConverter.GetBytes(ShotgunPriceLocal), 0, array, 1, 4);
			Array.Copy(BitConverter.GetBytes(ShotgunShellPriceLocal), 0, array, 5, 4);
			Array.Copy(BitConverter.GetBytes(EnableBuyableShotgunLocal), 0, array, 9, 1);
			Array.Copy(BitConverter.GetBytes(EnableBuyableShotgunShellLocal), 0, array, 10, 1);
			return array;
		}

		public static void SetSettings(byte[] data)
		{
			byte b = data[0];
			byte b2 = b;
			if (b2 == 1)
			{
				ShotgunPrice = BitConverter.ToInt32(data, 1);
				ShotgunShellPrice = BitConverter.ToInt32(data, 5);
				EnableBuyableShotgun = BitConverter.ToBoolean(data, 9);
				EnableBuyableShotgunShell = BitConverter.ToBoolean(data, 10);
				Debug.Log((object)"BuyableShotgunPlus Log: Host config set successfully");
				return;
			}
			throw new Exception("BuyableShotgunPlus Log: Invalid version byte");
		}

		private static bool IsHost()
		{
			return NetworkManager.Singleton.IsHost;
		}

		public static void OnRequestSync(ulong clientID, FastBufferReader reader)
		{
			//IL_0059: Unknown result type (might be due to invalid IL or missing references)
			if (!IsHost())
			{
				return;
			}
			Debug.Log((object)("BuyableShotgunPlus Log: Sending config to client " + clientID));
			byte[] settings = GetSettings();
			FastBufferWriter val = default(FastBufferWriter);
			((FastBufferWriter)(ref val))..ctor(settings.Length, (Allocator)2, settings.Length);
			try
			{
				((FastBufferWriter)(ref val)).WriteBytes(settings, -1, 0);
				NetworkManager.Singleton.CustomMessagingManager.SendNamedMessage("BuyableShotgun_OnReceiveConfigSync", clientID, val, (NetworkDelivery)2);
			}
			catch (Exception ex)
			{
				Debug.LogError((object)("BuyableShotgunPlus Log: Failed to send config: " + ex));
			}
			finally
			{
				((FastBufferWriter)(ref val)).Dispose();
			}
		}

		public static void OnReceiveSync(ulong clientID, FastBufferReader reader)
		{
			Debug.Log((object)"BuyableShotgunPlus Log: Received config from host");
			byte[] settings = new byte[11];
			try
			{
				((FastBufferReader)(ref reader)).ReadBytes(ref settings, 11, 0);
				SetSettings(settings);
			}
			catch (Exception ex)
			{
				Debug.LogError((object)("BuyableShotgunPlus Log: Failed to receive config: " + ex));
				SetToLocalValues();
			}
		}

		[HarmonyPostfix]
		[HarmonyPatch(typeof(PlayerControllerB), "ConnectClientToPlayerObject")]
		private static void ServerConnect()
		{
			//IL_00a6: Unknown result type (might be due to invalid IL or missing references)
			//IL_007a: Unknown result type (might be due to invalid IL or missing references)
			//IL_007f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0085: Expected O, but got Unknown
			//IL_003b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0040: Unknown result type (might be due to invalid IL or missing references)
			//IL_0046: Expected O, but got Unknown
			if (IsHost())
			{
				Debug.Log((object)"BuyableShotgunPlus Log: Started hosting, using local settings");
				SetToLocalValues();
				CustomMessagingManager customMessagingManager = NetworkManager.Singleton.CustomMessagingManager;
				object obj = <>O.<0>__OnRequestSync;
				if (obj == null)
				{
					HandleNamedMessageDelegate val = OnRequestSync;
					<>O.<0>__OnRequestSync = val;
					obj = (object)val;
				}
				customMessagingManager.RegisterNamedMessageHandler("BuyableShotgun_OnRequestConfigSync", (HandleNamedMessageDelegate)obj);
				return;
			}
			Debug.Log((object)"BuyableShotgunPlus Log: Connected to server, requesting settings");
			CustomMessagingManager customMessagingManager2 = NetworkManager.Singleton.CustomMessagingManager;
			object obj2 = <>O.<1>__OnReceiveSync;
			if (obj2 == null)
			{
				HandleNamedMessageDelegate val2 = OnReceiveSync;
				<>O.<1>__OnReceiveSync = val2;
				obj2 = (object)val2;
			}
			customMessagingManager2.RegisterNamedMessageHandler("BuyableShotgun_OnReceiveConfigSync", (HandleNamedMessageDelegate)obj2);
			FastBufferWriter val3 = default(FastBufferWriter);
			((FastBufferWriter)(ref val3))..ctor(0, (Allocator)2, -1);
			NetworkManager.Singleton.CustomMessagingManager.SendNamedMessage("BuyableShotgun_OnRequestConfigSync", 0uL, val3, (NetworkDelivery)2);
		}

		[HarmonyPostfix]
		[HarmonyPatch(typeof(GameNetworkManager), "StartDisconnect")]
		private static void ServerDisconnect()
		{
			Debug.Log((object)"BuyableShotgunPlus Log: Server disconnect");
			SetToLocalValues();
		}
	}
}