Decompiled source of BuyableShotgunShells v1.1.0

BuyableShotgunShells.dll

Decompiled 2 months 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 HarmonyLib;
using LethalLib.Modules;
using Microsoft.CodeAnalysis;
using UnityEngine;
using UnityEngine.SceneManagement;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: AssemblyCompany("BuyableShotgunShells")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyCopyright("Copyright © 2023 MegaPiggy")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0+04f63d17d79539fa78b9ec2ebfd4ae2fb22ef878")]
[assembly: AssemblyProduct("BuyableShotgunShells")]
[assembly: AssemblyTitle("BuyableShotgunShells")]
[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 BuyableShotgunShells
{
	[BepInDependency("evaisa.lethallib", "0.13.2")]
	[BepInPlugin("MegaPiggy.BuyableShotgunShells", "Buyable Shotgun Shells", "1.1.0")]
	public class BuyableShotgunShells : BaseUnityPlugin
	{
		private const string modGUID = "MegaPiggy.BuyableShotgunShells";

		private const string modName = "Buyable Shotgun Shells";

		private const string modVersion = "1.1.0";

		private readonly Harmony harmony = new Harmony("MegaPiggy.BuyableShotgunShells");

		private static BuyableShotgunShells Instance;

		private static ConfigEntry<int> ShellPriceConfig;

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

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

		public static List<Item> AllItems => Resources.FindObjectsOfTypeAll<Item>().Concat(Object.FindObjectsByType<Item>((FindObjectsInactive)1, (FindObjectsSortMode)1)).ToList();

		public static Item ShotgunShell => ((IEnumerable<Item>)AllItems).FirstOrDefault((Func<Item, bool>)((Item item) => ((Object)item).name.Equals("GunAmmo")));

		public static Item ShotgunShellClone { get; private set; }

		public static GameObject ShotgunShellObjectClone { get; private set; }

		public static int ShellPrice => ShellPriceConfig.Value;

		private void Awake()
		{
			if ((Object)(object)Instance == (Object)null)
			{
				Object.DontDestroyOnLoad((Object)(object)this);
				Instance = this;
			}
			harmony.PatchAll();
			ShellPriceConfig = ((BaseUnityPlugin)this).Config.Bind<int>("Prices", "ShotgunShellPrice", 20, "Credits needed to buy shotgun shells");
			SceneManager.sceneLoaded += OnSceneLoaded;
			ShotgunShellClone = MakeNonScrap(ShellPrice);
			AddToShop();
			((BaseUnityPlugin)this).Logger.LogInfo((object)"Plugin Buyable Shotgun Shells is loaded with version 1.1.0!");
		}

		private static Item MakeNonScrap(int price)
		{
			//IL_00cb: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d5: Unknown result type (might be due to invalid IL or missing references)
			//IL_0167: Unknown result type (might be due to invalid IL or missing references)
			//IL_017a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0184: Unknown result type (might be due to invalid IL or missing references)
			//IL_01d9: Unknown result type (might be due to invalid IL or missing references)
			//IL_01e3: Unknown result type (might be due to invalid IL or missing references)
			Item val = ScriptableObject.CreateInstance<Item>();
			Object.DontDestroyOnLoad((Object)(object)val);
			((Object)val).name = "Error";
			val.itemName = "Error";
			val.itemId = 6624;
			val.isScrap = false;
			val.creditsWorth = price;
			val.canBeGrabbedBeforeGameStart = true;
			val.automaticallySetUsingPower = false;
			val.batteryUsage = 300f;
			val.canBeInspected = false;
			val.isDefensiveWeapon = true;
			val.saveItemVariable = true;
			val.syncGrabFunction = false;
			val.twoHandedAnimation = true;
			val.verticalOffset = 0.25f;
			GameObject val2 = NetworkPrefabs.CreateNetworkPrefab("Cube");
			GameObject val3 = GameObject.CreatePrimitive((PrimitiveType)3);
			val3.transform.SetParent(val2.transform, false);
			((Renderer)val3.GetComponent<MeshRenderer>()).sharedMaterial.shader = Shader.Find("HDRP/Lit");
			val2.AddComponent<BoxCollider>().size = Vector3.one * 2f;
			val2.AddComponent<AudioSource>();
			PhysicsProp val4 = val2.AddComponent<PhysicsProp>();
			((GrabbableObject)val4).itemProperties = val;
			((GrabbableObject)val4).grabbable = true;
			val.spawnPrefab = val2;
			val2.tag = "PhysicsProp";
			val2.layer = LayerMask.NameToLayer("Props");
			val3.layer = LayerMask.NameToLayer("Props");
			try
			{
				GameObject val5 = Object.Instantiate<GameObject>(Items.scanNodePrefab, val2.transform);
				((Object)val5).name = "ScanNode";
				val5.transform.localPosition = new Vector3(0f, 0f, 0f);
				Transform transform = val5.transform;
				transform.localScale *= 2f;
				ScanNodeProperties component = val5.GetComponent<ScanNodeProperties>();
				component.nodeType = 1;
				component.headerText = "Error";
				component.subText = "A mod is incompatible with Buyable Shotgun Shells";
			}
			catch (Exception ex)
			{
				LoggerInstance.LogError((object)ex.ToString());
			}
			val2.transform.localScale = Vector3.one / 2f;
			return val;
		}

		private static GameObject CloneNonScrap(Item original, Item clone, int price)
		{
			GameObject val = NetworkPrefabs.CloneNetworkPrefab(original.spawnPrefab, (string)null);
			Object.DontDestroyOnLoad((Object)(object)val);
			CopyFields(original, clone);
			val.GetComponent<GrabbableObject>().itemProperties = clone;
			clone.spawnPrefab = val;
			((Object)clone).name = "Buyable" + ((Object)original).name;
			clone.creditsWorth = price;
			return val;
		}

		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>();
			val.clearPreviousText = true;
			((Object)val).name = name + "InfoNode";
			val.displayText = description + "\n\n";
			infoNodes.Add(name, val);
			return val;
		}

		private static void OnSceneLoaded(Scene scene, LoadSceneMode mode)
		{
			if (!((Object)(object)ShotgunShell == (Object)null) && !((Object)(object)ShotgunShellObjectClone != (Object)null))
			{
				ShotgunShellObjectClone = CloneNonScrap(ShotgunShell, ShotgunShellClone, ShellPrice);
				ShotgunShellClone.itemName = "Shells";
			}
		}

		private static void AddToShop()
		{
			Item shotgunShellClone = ShotgunShellClone;
			int shellPrice = ShellPrice;
			Items.RegisterShopItem(shotgunShellClone, (TerminalNode)null, (TerminalNode)null, CreateInfoNode("ShotgunShell", "Ammo for the Nutcracker's Shotgun."), shellPrice);
			LoggerInstance.LogInfo((object)$"Shotgun Shell added to Shop for {ShellPrice} credits");
		}
	}
}