Decompiled source of TurretAmmoFix v1.0.0

TurretAmmoFix.dll

Decompiled 4 days 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 UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETStandard,Version=v2.0", FrameworkDisplayName = ".NET Standard 2.0")]
[assembly: AssemblyCompany("TurretAmmoFix")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("TurretAmmoFix")]
[assembly: AssemblyTitle("TurretAmmoFix")]
[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.Module, AllowMultiple = false, Inherited = false)]
	internal sealed class RefSafetyRulesAttribute : Attribute
	{
		public readonly int Version;

		public RefSafetyRulesAttribute(int P_0)
		{
			Version = P_0;
		}
	}
}
namespace TurretAmmoFix
{
	[BepInPlugin("custom.turretammofix", "TurretAmmoFix", "1.0.0")]
	public class TurretAmmoFixPlugin : BaseUnityPlugin
	{
		[HarmonyPatch(typeof(Turret), "Awake")]
		private static class TurretAwakePatch
		{
			private static void Postfix(Turret __instance)
			{
				//IL_008d: Unknown result type (might be due to invalid IL or missing references)
				//IL_0094: Invalid comparison between Unknown and I4
				//IL_00b4: Unknown result type (might be due to invalid IL or missing references)
				//IL_00ba: Expected O, but got Unknown
				//IL_00db: Unknown result type (might be due to invalid IL or missing references)
				//IL_00f6: Unknown result type (might be due to invalid IL or missing references)
				if (!Enabled.Value || !AllowAllBoltAmmo.Value || ((Object)(object)ObjectDB.instance == (Object)null && (Object)(object)ZNetScene.instance == (Object)null))
				{
					return;
				}
				try
				{
					ItemDrop[] array = FindAmmoItems();
					foreach (ItemDrop itemDrop in array)
					{
						if (!((Object)(object)itemDrop == (Object)null) && itemDrop.m_itemData?.m_shared != null && (int)itemDrop.m_itemData.m_shared.m_itemType == 9 && !__instance.m_allowedAmmo.Any((AmmoType ammo) => (Object)(object)ammo.m_ammo == (Object)(object)itemDrop))
						{
							GameObject val = new GameObject("TurretAmmoFixVisual");
							val.transform.SetParent(((Component)__instance).transform, false);
							val.SetActive(false);
							__instance.m_allowedAmmo.Add(new AmmoType
							{
								m_ammo = itemDrop,
								m_visual = val
							});
						}
					}
				}
				catch (Exception ex)
				{
					Log.LogWarning((object)("Failed to add ammo to turret: " + ex));
				}
			}

			private static ItemDrop[] FindAmmoItems()
			{
				HashSet<ItemDrop> hashSet = new HashSet<ItemDrop>();
				if ((Object)(object)ObjectDB.instance != (Object)null && ObjectDB.instance.m_items != null)
				{
					foreach (GameObject item in ObjectDB.instance.m_items)
					{
						if ((Object)(object)item != (Object)null)
						{
							hashSet.Add(item.GetComponent<ItemDrop>());
						}
					}
				}
				if ((Object)(object)ZNetScene.instance != (Object)null)
				{
					List<GameObject> prefabs = ZNetScene.instance.m_prefabs;
					if (prefabs != null)
					{
						foreach (GameObject item2 in prefabs)
						{
							if ((Object)(object)item2 != (Object)null)
							{
								hashSet.Add(item2.GetComponent<ItemDrop>());
							}
						}
					}
				}
				ItemDrop[] array = (ItemDrop[])(object)new ItemDrop[hashSet.Count];
				hashSet.CopyTo(array);
				return array;
			}
		}

		internal static ConfigEntry<bool> Enabled;

		internal static ConfigEntry<bool> AllowAllBoltAmmo;

		internal static ManualLogSource Log;

		private void Awake()
		{
			//IL_0050: Unknown result type (might be due to invalid IL or missing references)
			Enabled = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Enabled", true, "Master toggle. When false, TurretAmmoFix makes no changes to the game.");
			AllowAllBoltAmmo = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "AllowAllBoltAmmo", true, "Allow all bolt items to be loaded into turrets and ballistas.");
			Log = ((BaseUnityPlugin)this).Logger;
			new Harmony("custom.turretammofix").PatchAll();
		}
	}
}