Decompiled source of StartWithAllWeapons v1.0.0

StartWithAllWeapons.dll

Decompiled a day 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 Microsoft.CodeAnalysis;
using Photon.Pun;
using UnityEngine;

[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-CSharp")]
[assembly: AssemblyCompany("Adir")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("StartWithAllWeapons")]
[assembly: AssemblyTitle("StartWithAllWeapons")]
[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 StartWithAllWeapons
{
	[BepInPlugin("Adir.StartWithAllWeapons", "StartWithAllWeapons", "1.0.0")]
	public class StartWithAllWeapons : BaseUnityPlugin
	{
		internal static ConfigEntry<bool> includeGuns;

		internal static ConfigEntry<bool> includeMelee;

		internal static ConfigEntry<bool> includeLaunchers;

		internal static ConfigEntry<string> extraItems;

		internal static ConfigEntry<string> excludedItems;

		internal static ConfigEntry<bool> spawnOverflow;

		internal static StartWithAllWeapons Instance { get; private set; }

		internal static ManualLogSource Logger => Instance._logger;

		private ManualLogSource _logger => ((BaseUnityPlugin)this).Logger;

		internal Harmony? Harmony { get; set; }

		private void Awake()
		{
			Instance = this;
			((Component)this).gameObject.transform.parent = null;
			((Object)((Component)this).gameObject).hideFlags = (HideFlags)61;
			includeGuns = ((BaseUnityPlugin)this).Config.Bind<bool>("Categories", "Guns", true, "Give every gun (handgun, shotgun, tranq, laser...)");
			includeMelee = ((BaseUnityPlugin)this).Config.Bind<bool>("Categories", "Melee", true, "Give every melee weapon (swords, hammers, bats, frying pan...)");
			includeLaunchers = ((BaseUnityPlugin)this).Config.Bind<bool>("Categories", "Launchers", true, "Give every launcher-type weapon");
			extraItems = ((BaseUnityPlugin)this).Config.Bind<string>("Items", "Extra", "", "Comma-separated item asset names to also give (e.g. \"Item Grenade Explosive, Item Health Pack Large\")");
			excludedItems = ((BaseUnityPlugin)this).Config.Bind<string>("Items", "Excluded", "", "Comma-separated item asset names to never give (e.g. \"Item Melee Frying Pan\")");
			spawnOverflow = ((BaseUnityPlugin)this).Config.Bind<bool>("Spawning", "SpawnOverflow", true, "The truck only has a limited number of item slots. If enabled, weapons that don't fit are spawned in the truck anyway, stacked above the slots.");
			Patch();
			Logger.LogInfo((object)$"{((BaseUnityPlugin)this).Info.Metadata.GUID} v{((BaseUnityPlugin)this).Info.Metadata.Version} has loaded!");
		}

		internal static bool IsWeapon(Item item)
		{
			//IL_0001: Unknown result type (might be due to invalid IL or missing references)
			//IL_0006: Unknown result type (might be due to invalid IL or missing references)
			//IL_0007: Unknown result type (might be due to invalid IL or missing references)
			//IL_0009: Invalid comparison between Unknown and I4
			//IL_000b: Unknown result type (might be due to invalid IL or missing references)
			//IL_000e: Invalid comparison between Unknown and I4
			//IL_0010: Unknown result type (might be due to invalid IL or missing references)
			//IL_0013: Invalid comparison between Unknown and I4
			itemType itemType = item.itemType;
			if ((int)itemType != 7)
			{
				if ((int)itemType != 9)
				{
					if ((int)itemType == 15)
					{
						return includeLaunchers.Value;
					}
					return false;
				}
				return includeGuns.Value;
			}
			return includeMelee.Value;
		}

		private static HashSet<string> ParseList(string value)
		{
			return new HashSet<string>(from s in value.Split(',')
				select s.Trim() into s
				where s.Length > 0
				select s, StringComparer.OrdinalIgnoreCase);
		}

		internal static List<string> GetStartingItems()
		{
			Dictionary<string, Item> itemDictionary = StatsManager.instance.itemDictionary;
			HashSet<string> excluded = ParseList(excludedItems.Value);
			HashSet<string> extra = ParseList(extraItems.Value);
			return (from kv in itemDictionary
				where !kv.Value.disabled && (IsWeapon(kv.Value) || extra.Contains(kv.Key))
				select kv.Key into name
				where !excluded.Contains(name)
				select name).ToList();
		}

		internal void Patch()
		{
			//IL_0019: Unknown result type (might be due to invalid IL or missing references)
			//IL_001e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0020: Expected O, but got Unknown
			//IL_0025: Expected O, but got Unknown
			if (Harmony == null)
			{
				Harmony val = new Harmony(((BaseUnityPlugin)this).Info.Metadata.GUID);
				Harmony val2 = val;
				Harmony = val;
			}
			Harmony.PatchAll();
		}

		internal void Unpatch()
		{
			Harmony? harmony = Harmony;
			if (harmony != null)
			{
				harmony.UnpatchSelf();
			}
		}
	}
	[HarmonyPatch(typeof(ItemManager), "ItemsInitialize")]
	internal static class ItemsInitializePatch
	{
		private static bool loggedAvailable;

		[HarmonyPrefix]
		private static void Prefix()
		{
			if (!SemiFunc.RunIsLevel() || !SemiFunc.IsMasterClientOrSingleplayer())
			{
				return;
			}
			StatsManager instance = StatsManager.instance;
			if (!loggedAvailable)
			{
				loggedAvailable = true;
				IEnumerable<string> values = from i in instance.itemDictionary.Values.Where(delegate(Item i)
					{
						//IL_0001: Unknown result type (might be due to invalid IL or missing references)
						//IL_0006: Unknown result type (might be due to invalid IL or missing references)
						//IL_0007: Unknown result type (might be due to invalid IL or missing references)
						//IL_0009: Invalid comparison between Unknown and I4
						//IL_000b: Unknown result type (might be due to invalid IL or missing references)
						//IL_000e: Invalid comparison between Unknown and I4
						//IL_0010: Unknown result type (might be due to invalid IL or missing references)
						//IL_0013: Invalid comparison between Unknown and I4
						itemType itemType = i.itemType;
						return ((int)itemType == 7 || (int)itemType == 9 || (int)itemType == 15) ? true : false;
					})
					select string.Format("{0} ({1}{2})", ((Object)i).name, i.itemType, i.disabled ? ", disabled" : "");
				StartWithAllWeapons.Logger.LogInfo((object)("Weapons found in game: " + string.Join(", ", values)));
			}
			foreach (string startingItem in StartWithAllWeapons.GetStartingItems())
			{
				if (instance.itemsPurchased.GetValueOrDefault(startingItem, 0) < 1)
				{
					instance.itemsPurchased[startingItem] = 1;
					instance.itemsPurchasedTotal[startingItem] = instance.itemsPurchasedTotal.GetValueOrDefault(startingItem, 0) + 1;
					StartWithAllWeapons.Logger.LogInfo((object)("Added " + startingItem));
				}
			}
		}
	}
	[HarmonyPatch(typeof(PunManager), "TruckPopulateItemVolumes")]
	internal static class TruckPopulatePatch
	{
		private static readonly List<Vector3> volumePositions = new List<Vector3>();

		private static readonly List<Item> overflow = new List<Item>();

		[HarmonyPrefix]
		private static void Prefix(PunManager __instance)
		{
			volumePositions.Clear();
			overflow.Clear();
			if (!StartWithAllWeapons.spawnOverflow.Value || SemiFunc.IsNotMasterClient() || !SemiFunc.RunIsLevel())
			{
				return;
			}
			ItemManager itemManager = __instance.itemManager;
			List<ItemVolume> list = itemManager.itemVolumes.Where((ItemVolume v) => Object.op_Implicit((Object)(object)v)).ToList();
			List<Item> list2 = new List<Item>(itemManager.purchasedItems);
			volumePositions.AddRange(list.Select((ItemVolume v) => ((Component)v).transform.position));
			while (list.Count > 0 && list2.Count > 0)
			{
				bool flag = false;
				for (int num = 0; num < list2.Count; num++)
				{
					Item item = list2[num];
					ItemVolume val = list.Find((ItemVolume v) => v.itemVolume == item.itemVolume);
					if (Object.op_Implicit((Object)(object)val))
					{
						list.Remove(val);
						list2.RemoveAt(num);
						flag = true;
						break;
					}
				}
				if (!flag)
				{
					break;
				}
			}
			HashSet<string> ours = new HashSet<string>(StartWithAllWeapons.GetStartingItems());
			overflow.AddRange(list2.Where((Item i) => ours.Contains(((Object)i).name)));
		}

		[HarmonyPostfix]
		private static void Postfix()
		{
			//IL_003d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0042: Unknown result type (might be due to invalid IL or missing references)
			//IL_005c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0061: Unknown result type (might be due to invalid IL or missing references)
			//IL_0066: Unknown result type (might be due to invalid IL or missing references)
			//IL_0068: Unknown result type (might be due to invalid IL or missing references)
			//IL_006d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0080: Unknown result type (might be due to invalid IL or missing references)
			//IL_0081: Unknown result type (might be due to invalid IL or missing references)
			//IL_009e: Unknown result type (might be due to invalid IL or missing references)
			//IL_009f: Unknown result type (might be due to invalid IL or missing references)
			if (overflow.Count == 0 || volumePositions.Count == 0)
			{
				return;
			}
			for (int i = 0; i < overflow.Count; i++)
			{
				Item val = overflow[i];
				Vector3 val2 = volumePositions[i % volumePositions.Count] + Vector3.up * (0.6f * (float)(1 + i / volumePositions.Count));
				Quaternion spawnRotationOffset = val.spawnRotationOffset;
				if (SemiFunc.IsMasterClient())
				{
					PhotonNetwork.InstantiateRoomObject(((PrefabRef<GameObject>)(object)val.prefab).ResourcePath, val2, spawnRotationOffset, (byte)0, (object[])null);
				}
				else if (!SemiFunc.IsMultiplayer())
				{
					Object.Instantiate<GameObject>(((PrefabRef<GameObject>)(object)val.prefab).Prefab, val2, spawnRotationOffset);
				}
				StartWithAllWeapons.Logger.LogInfo((object)("Truck full, spawned " + ((Object)val).name + " as overflow"));
			}
			overflow.Clear();
			volumePositions.Clear();
		}
	}
}
namespace System.Runtime.CompilerServices
{
	[AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)]
	internal sealed class IgnoresAccessChecksToAttribute(string assemblyName) : Attribute()
	{
		public string AssemblyName { get; } = assemblyName;
	}
}