Decompiled source of BuyableCat v1.0.0

BuyableCat.dll

Decompiled 9 hours ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
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 GameNetcodeStuff;
using HarmonyLib;
using LethalLib.Modules;
using LobbyCompatibility.Attributes;
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: IgnoresAccessChecksTo("AmazingAssets.TerrainToMesh")]
[assembly: IgnoresAccessChecksTo("Assembly-CSharp-firstpass")]
[assembly: IgnoresAccessChecksTo("Assembly-CSharp")]
[assembly: IgnoresAccessChecksTo("ClientNetworkTransform")]
[assembly: IgnoresAccessChecksTo("DissonanceVoip")]
[assembly: IgnoresAccessChecksTo("Facepunch Transport for Netcode for GameObjects")]
[assembly: IgnoresAccessChecksTo("Facepunch.Steamworks.Win64")]
[assembly: IgnoresAccessChecksTo("Unity.AI.Navigation")]
[assembly: IgnoresAccessChecksTo("Unity.Animation.Rigging")]
[assembly: IgnoresAccessChecksTo("Unity.Animation.Rigging.DocCodeExamples")]
[assembly: IgnoresAccessChecksTo("Unity.Burst")]
[assembly: IgnoresAccessChecksTo("Unity.Burst.Unsafe")]
[assembly: IgnoresAccessChecksTo("Unity.Collections")]
[assembly: IgnoresAccessChecksTo("Unity.Collections.LowLevel.ILSupport")]
[assembly: IgnoresAccessChecksTo("Unity.InputSystem")]
[assembly: IgnoresAccessChecksTo("Unity.InputSystem.ForUI")]
[assembly: IgnoresAccessChecksTo("Unity.Jobs")]
[assembly: IgnoresAccessChecksTo("Unity.Mathematics")]
[assembly: IgnoresAccessChecksTo("Unity.Multiplayer.Tools.Common")]
[assembly: IgnoresAccessChecksTo("Unity.Multiplayer.Tools.MetricTypes")]
[assembly: IgnoresAccessChecksTo("Unity.Multiplayer.Tools.NetStats")]
[assembly: IgnoresAccessChecksTo("Unity.Multiplayer.Tools.NetStatsMonitor.Component")]
[assembly: IgnoresAccessChecksTo("Unity.Multiplayer.Tools.NetStatsMonitor.Configuration")]
[assembly: IgnoresAccessChecksTo("Unity.Multiplayer.Tools.NetStatsMonitor.Implementation")]
[assembly: IgnoresAccessChecksTo("Unity.Multiplayer.Tools.NetStatsReporting")]
[assembly: IgnoresAccessChecksTo("Unity.Multiplayer.Tools.NetworkProfiler.Runtime")]
[assembly: IgnoresAccessChecksTo("Unity.Multiplayer.Tools.NetworkSolutionInterface")]
[assembly: IgnoresAccessChecksTo("Unity.Netcode.Components")]
[assembly: IgnoresAccessChecksTo("Unity.Netcode.Runtime")]
[assembly: IgnoresAccessChecksTo("Unity.Networking.Transport")]
[assembly: IgnoresAccessChecksTo("Unity.ProBuilder.Csg")]
[assembly: IgnoresAccessChecksTo("Unity.ProBuilder")]
[assembly: IgnoresAccessChecksTo("Unity.ProBuilder.KdTree")]
[assembly: IgnoresAccessChecksTo("Unity.ProBuilder.Poly2Tri")]
[assembly: IgnoresAccessChecksTo("Unity.ProBuilder.Stl")]
[assembly: IgnoresAccessChecksTo("Unity.Profiling.Core")]
[assembly: IgnoresAccessChecksTo("Unity.RenderPipelines.Core.Runtime")]
[assembly: IgnoresAccessChecksTo("Unity.RenderPipelines.Core.ShaderLibrary")]
[assembly: IgnoresAccessChecksTo("Unity.RenderPipelines.HighDefinition.Config.Runtime")]
[assembly: IgnoresAccessChecksTo("Unity.RenderPipelines.HighDefinition.Runtime")]
[assembly: IgnoresAccessChecksTo("Unity.RenderPipelines.ShaderGraph.ShaderGraphLibrary")]
[assembly: IgnoresAccessChecksTo("Unity.Services.Authentication")]
[assembly: IgnoresAccessChecksTo("Unity.Services.Core.Analytics")]
[assembly: IgnoresAccessChecksTo("Unity.Services.Core.Configuration")]
[assembly: IgnoresAccessChecksTo("Unity.Services.Core.Device")]
[assembly: IgnoresAccessChecksTo("Unity.Services.Core")]
[assembly: IgnoresAccessChecksTo("Unity.Services.Core.Environments")]
[assembly: IgnoresAccessChecksTo("Unity.Services.Core.Environments.Internal")]
[assembly: IgnoresAccessChecksTo("Unity.Services.Core.Internal")]
[assembly: IgnoresAccessChecksTo("Unity.Services.Core.Networking")]
[assembly: IgnoresAccessChecksTo("Unity.Services.Core.Registration")]
[assembly: IgnoresAccessChecksTo("Unity.Services.Core.Scheduler")]
[assembly: IgnoresAccessChecksTo("Unity.Services.Core.Telemetry")]
[assembly: IgnoresAccessChecksTo("Unity.Services.Core.Threading")]
[assembly: IgnoresAccessChecksTo("Unity.Services.QoS")]
[assembly: IgnoresAccessChecksTo("Unity.Services.Relay")]
[assembly: IgnoresAccessChecksTo("Unity.TextMeshPro")]
[assembly: IgnoresAccessChecksTo("Unity.Timeline")]
[assembly: IgnoresAccessChecksTo("Unity.VisualEffectGraph.Runtime")]
[assembly: IgnoresAccessChecksTo("UnityEngine.ARModule")]
[assembly: IgnoresAccessChecksTo("UnityEngine.NVIDIAModule")]
[assembly: IgnoresAccessChecksTo("UnityEngine.UI")]
[assembly: AssemblyCompany("BuyableCat")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("BuyableCat")]
[assembly: AssemblyTitle("BuyableCat")]
[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 BuyableCat
{
	[BepInPlugin("BuyableCat", "BuyableCat", "1.0.0")]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	[LobbyCompatibility(/*Could not decode attribute arguments.*/)]
	public class BuyableCat : BaseUnityPlugin
	{
		public class ClonedItem : Item
		{
			public Item original;
		}

		internal class Unflagger : MonoBehaviour
		{
			public void Awake()
			{
				((Object)((Component)this).gameObject).hideFlags = (HideFlags)0;
			}
		}

		[HarmonyPatch]
		internal static class Patches
		{
			[CompilerGenerated]
			private static class <>O
			{
				public static HandleNamedMessageDelegate <0>__OnRequestSync;

				public static HandleNamedMessageDelegate <1>__OnReceiveSync;
			}

			[HarmonyPostfix]
			[HarmonyPatch(typeof(PlayerControllerB), "ConnectClientToPlayerObject")]
			public static void ServerConnect()
			{
				//IL_00a9: Unknown result type (might be due to invalid IL or missing references)
				//IL_0084: Unknown result type (might be due to invalid IL or missing references)
				//IL_0089: Unknown result type (might be due to invalid IL or missing references)
				//IL_008f: Expected O, but got Unknown
				//IL_003a: Unknown result type (might be due to invalid IL or missing references)
				//IL_003f: Unknown result type (might be due to invalid IL or missing references)
				//IL_0045: Expected O, but got Unknown
				if (IsHost)
				{
					LoggerInstance.LogInfo((object)"Started hosting, using local settings");
					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);
					UpdateShopItemPrice();
				}
				else
				{
					LoggerInstance.LogInfo((object)"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);
					NetworkManager.Singleton.CustomMessagingManager.SendNamedMessage("BuyableShotgun_OnRequestConfigSync", 0uL, new FastBufferWriter(0, (Allocator)2, -1), (NetworkDelivery)2);
				}
			}

			[HarmonyPostfix]
			[HarmonyPatch(typeof(GameNetworkManager), "Start")]
			public static void Start()
			{
				LoggerInstance.LogWarning((object)"Game network manager start");
			}

			[HarmonyPostfix]
			[HarmonyPatch(typeof(GameNetworkManager), "StartDisconnect")]
			public static void ServerDisconnect()
			{
				LoggerInstance.LogInfo((object)"Server disconnect");
				ShotgunPriceRemote = -1;
			}

			[HarmonyPatch(typeof(StartOfRound), "Awake")]
			[HarmonyPostfix]
			public static void StartOfRound_Awake(StartOfRound __instance)
			{
				CloneShotgun();
			}
		}

		private readonly Harmony harmony = new Harmony("BuyableCat");

		private static ConfigEntry<int> ShotgunPriceConfig;

		internal static int ShotgunPriceRemote = -1;

		private static Dictionary<string, TerminalNode> infoNodes = new Dictionary<string, TerminalNode>();

		public static byte CurrentVersionByte = 1;

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


		private static ManualLogSource LoggerInstance => ((BaseUnityPlugin)Instance).Logger;

		public static List<Item> AllItems => Resources.FindObjectsOfTypeAll<Item>().Reverse().ToList();

		public static ClonedItem ShotgunClone { get; private set; }

		public static int ShotgunPriceLocal => ShotgunPriceConfig.Value;

		public static int ShotgunPrice => (ShotgunPriceRemote > -1) ? ShotgunPriceRemote : ShotgunPriceLocal;

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

		private static ulong LocalClientId => NetworkManager.Singleton.LocalClientId;

		private void Awake()
		{
			if ((Object)(object)Instance == (Object)null)
			{
				Object.DontDestroyOnLoad((Object)(object)this);
				Instance = this;
			}
			harmony.PatchAll();
			ShotgunPriceConfig = ((BaseUnityPlugin)this).Config.Bind<int>("Prices", "CatPrice", 10, "Credits needed to buy a cat");
			((BaseUnityPlugin)this).Logger.LogInfo((object)"Plugin BuyableCat is loaded with version 1.0.0!");
		}

		private static ClonedItem CloneNonScrap(Item original, int price)
		{
			ClonedItem clonedItem = ScriptableObject.CreateInstance<ClonedItem>();
			Object.DontDestroyOnLoad((Object)(object)clonedItem);
			clonedItem.original = original;
			GameObject val = NetworkPrefabs.CloneNetworkPrefab(original.spawnPrefab, "Buyable" + ((Object)original).name);
			val.AddComponent<Unflagger>();
			Object.DontDestroyOnLoad((Object)(object)val);
			CopyFields(original, (Item)(object)clonedItem);
			val.GetComponent<GrabbableObject>().itemProperties = (Item)(object)clonedItem;
			((Item)clonedItem).spawnPrefab = val;
			((Object)clonedItem).name = "Buyable" + ((Object)original).name;
			((Item)clonedItem).creditsWorth = price;
			((Item)clonedItem).isScrap = false;
			return clonedItem;
		}

		public static void CopyFields(Item source, Item destination)
		{
			FieldInfo[] fields = typeof(Item).GetFields();
			FieldInfo[] array = fields;
			foreach (FieldInfo fieldInfo in array)
			{
				fieldInfo.SetValue(destination, fieldInfo.GetValue(source));
			}
		}

		private static TerminalNode CreateInfoNode(string name, string description)
		{
			if (infoNodes.ContainsKey(name))
			{
				return infoNodes[name];
			}
			TerminalNode val = ScriptableObject.CreateInstance<TerminalNode>();
			Object.DontDestroyOnLoad((Object)(object)val);
			val.clearPreviousText = true;
			((Object)val).name = name + "InfoNode";
			val.displayText = description + "\n\n";
			infoNodes.Add(name, val);
			return val;
		}

		private static void CloneShotgun()
		{
			Item val = ((IEnumerable<Item>)AllItems).FirstOrDefault((Func<Item, bool>)((Item item) => ((Object)item).name.Equals("CatItem")));
			if (!((Object)(object)val == (Object)null) && !((Object)(object)ShotgunClone != (Object)null))
			{
				ShotgunClone = CloneNonScrap(val, ShotgunPrice);
				AddToShop();
			}
		}

		private static void AddToShop()
		{
			ClonedItem shotgunClone = ShotgunClone;
			int shotgunPrice = ShotgunPrice;
			Items.RegisterShopItem((Item)(object)shotgunClone, (TerminalNode)null, (TerminalNode)null, CreateInfoNode("Cat", "Needy Cat"), shotgunPrice);
			LoggerInstance.LogInfo((object)$"Cat added to Shop for {ShotgunPrice} credits");
		}

		private static void UpdateShopItemPrice()
		{
			((Item)ShotgunClone).creditsWorth = ShotgunPrice;
			Items.UpdateShopItemPrice((Item)(object)ShotgunClone, ShotgunPrice);
			LoggerInstance.LogInfo((object)$"Cat price updated to {ShotgunPrice} credits");
		}

		public static void WriteData(FastBufferWriter writer)
		{
			((FastBufferWriter)(ref writer)).WriteByte(CurrentVersionByte);
			((FastBufferWriter)(ref writer)).WriteBytes(BitConverter.GetBytes(ShotgunPriceLocal), -1, 0);
		}

		public static void ReadData(FastBufferReader reader)
		{
			byte b = default(byte);
			((FastBufferReader)(ref reader)).ReadByte(ref b);
			if (b == CurrentVersionByte)
			{
				byte[] value = new byte[4];
				((FastBufferReader)(ref reader)).ReadBytes(ref value, 4, 0);
				ShotgunPriceRemote = BitConverter.ToInt32(value, 0);
				UpdateShopItemPrice();
				LoggerInstance.LogInfo((object)"Host config set successfully");
				return;
			}
			throw new Exception("Invalid version byte");
		}

		public static void OnRequestSync(ulong clientID, FastBufferReader reader)
		{
			//IL_0032: Unknown result type (might be due to invalid IL or missing references)
			//IL_0049: Unknown result type (might be due to invalid IL or missing references)
			if (!IsHost)
			{
				return;
			}
			LoggerInstance.LogInfo((object)("Sending config to client " + clientID));
			FastBufferWriter val = default(FastBufferWriter);
			((FastBufferWriter)(ref val))..ctor(5, (Allocator)2, 5);
			try
			{
				WriteData(val);
				NetworkManager.Singleton.CustomMessagingManager.SendNamedMessage("BuyableShotgun_OnReceiveConfigSync", clientID, val, (NetworkDelivery)2);
			}
			catch (Exception arg)
			{
				LoggerInstance.LogError((object)$"Failed to send config: {arg}");
			}
			finally
			{
				((FastBufferWriter)(ref val)).Dispose();
			}
		}

		public static void OnReceiveSync(ulong clientID, FastBufferReader reader)
		{
			//IL_0012: Unknown result type (might be due to invalid IL or missing references)
			LoggerInstance.LogInfo((object)"Received config from host");
			try
			{
				ReadData(reader);
			}
			catch (Exception arg)
			{
				LoggerInstance.LogError((object)$"Failed to receive config: {arg}");
				ShotgunPriceRemote = -1;
			}
		}
	}
	public static class MyPluginInfo
	{
		public const string PLUGIN_GUID = "BuyableCat";

		public const string PLUGIN_NAME = "BuyableCat";

		public const string PLUGIN_VERSION = "1.0.0";
	}
}
namespace System.Runtime.CompilerServices
{
	[AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)]
	internal sealed class IgnoresAccessChecksToAttribute : Attribute
	{
		public IgnoresAccessChecksToAttribute(string assemblyName)
		{
		}
	}
}