Decompiled source of ArcanumEitr v1.0.0

BepInEx/plugins/ArcanumEitr/ArcanumEitr.dll

Decompiled 3 weeks ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using BepInEx;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyTitle("ArcanumEitr")]
[assembly: AssemblyDescription("Configurable suppression of Eitr item and refinery projectiles. Inspired by joryjuky_team's No Eitr Projectiles.")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyVersion("1.0.0.0")]
[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 ArcanumEitr
{
	internal enum EitrSource
	{
		None,
		Item,
		Refinery
	}
	internal static class Detection
	{
		internal static EitrSource Classify(string name)
		{
			if (name == null)
			{
				return EitrSource.None;
			}
			if (name.EndsWith("(Clone)", StringComparison.Ordinal))
			{
				name = name.Substring(0, name.Length - "(Clone)".Length);
			}
			if (string.Equals(name, "Eitr", StringComparison.Ordinal))
			{
				return EitrSource.Item;
			}
			if (string.Equals(name, "eitrrefinery", StringComparison.Ordinal))
			{
				return EitrSource.Refinery;
			}
			return EitrSource.None;
		}

		internal static bool ShouldBlock(EitrSource source, bool items, bool refineries)
		{
			if (!(source == EitrSource.Item && items))
			{
				return source == EitrSource.Refinery && refineries;
			}
			return true;
		}
	}
	[BepInPlugin("arcanum.eitr", "ArcanumEitr", "1.0.0")]
	[BepInIncompatibility("NoRefineryProjectiles")]
	public sealed class Plugin : BaseUnityPlugin
	{
		public const string Guid = "arcanum.eitr";

		private static Plugin instance;

		private Harmony harmony;

		private bool blockItems;

		private bool blockRefineries;

		private bool diagnostics;

		private readonly HashSet<string> reported = new HashSet<string>();

		private void Awake()
		{
			//IL_007e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0088: Expected O, but got Unknown
			instance = this;
			blockItems = ((BaseUnityPlugin)this).Config.Bind<bool>("Projectiles", "DisableDroppedEitrProjectiles", true, "Prevent dropped refined Eitr from emitting damaging projectiles. Restart the game/server after changes.").Value;
			blockRefineries = ((BaseUnityPlugin)this).Config.Bind<bool>("Projectiles", "DisableRefineryProjectiles", true, "Prevent Eitr refineries from emitting damaging projectiles. Restart the game/server after changes.").Value;
			diagnostics = ((BaseUnityPlugin)this).Config.Bind<bool>("Diagnostics", "LogDetection", false, "Log each distinct radiator hierarchy and decision once per session. Restart after changes.").Value;
			try
			{
				harmony = new Harmony("arcanum.eitr");
				harmony.PatchAll(typeof(Plugin));
				((BaseUnityPlugin)this).Logger.LogInfo((object)("ArcanumEitr 1.0.0 active. Dropped Eitr blocked=" + blockItems + "; refinery blocked=" + blockRefineries + ". Settings apply at startup."));
			}
			catch (Exception ex)
			{
				Harmony obj = harmony;
				if (obj != null)
				{
					obj.UnpatchSelf();
				}
				instance = null;
				((BaseUnityPlugin)this).Logger.LogError((object)("Could not install Radiator.OnEnable patch; vanilla behavior retained. " + ex));
			}
		}

		[HarmonyPatch(typeof(Radiator), "OnEnable")]
		[HarmonyPrefix]
		private static bool BeforeRadiatorEnabled(Radiator __instance)
		{
			if ((Object)(object)instance == (Object)null || (Object)(object)__instance == (Object)null)
			{
				return true;
			}
			List<string> list = (instance.diagnostics ? new List<string>() : null);
			EitrSource eitrSource = EitrSource.None;
			Transform val = ((Component)__instance).transform;
			while ((Object)(object)val != (Object)null)
			{
				list?.Add(((Object)val).name);
				EitrSource eitrSource2 = Detection.Classify(((Object)val).name);
				if (eitrSource2 == EitrSource.Item && (Object)(object)((Component)val).GetComponent<ItemDrop>() != (Object)null)
				{
					eitrSource = eitrSource2;
				}
				else if (eitrSource2 == EitrSource.Refinery && (Object)(object)((Component)val).GetComponent<Smelter>() != (Object)null)
				{
					eitrSource = eitrSource2;
				}
				if (eitrSource != EitrSource.None || (Object)(object)((Component)val).GetComponent<ZNetView>() != (Object)null)
				{
					break;
				}
				val = val.parent;
			}
			bool flag = Detection.ShouldBlock(eitrSource, instance.blockItems, instance.blockRefineries);
			if (list != null)
			{
				string text = string.Join(" -> ", list) + ": " + eitrSource.ToString() + ", " + (flag ? "blocked" : "allowed");
				if (instance.reported.Add(text))
				{
					((BaseUnityPlugin)instance).Logger.LogInfo((object)text);
				}
			}
			return !flag;
		}

		private void OnDestroy()
		{
			Harmony obj = harmony;
			if (obj != null)
			{
				obj.UnpatchSelf();
			}
			if ((Object)(object)instance == (Object)(object)this)
			{
				instance = null;
			}
		}
	}
}