Decompiled source of SubtleShield v1.0.0

SubtleShield.dll

Decompiled 2 months ago
using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using Microsoft.CodeAnalysis;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: AssemblyCompany("SubtleShield")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("SubtleShield")]
[assembly: AssemblyTitle("SubtleShield")]
[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 SubtleShield
{
	[BepInPlugin("com.soulfulrebel.subtleshield", "Subtle Shield", "1.0.0")]
	public sealed class Plugin : BaseUnityPlugin
	{
		public const string PluginGuid = "com.soulfulrebel.subtleshield";

		public const string PluginName = "Subtle Shield";

		public const string PluginVersion = "1.0.0";

		private const string ShieldPrefabName = "vfx_StaffShield";

		private const string SphereObjectName = "Sphere";

		private ConfigEntry<bool> _enabled;

		private ConfigEntry<float> _colorR;

		private ConfigEntry<float> _colorG;

		private ConfigEntry<float> _colorB;

		private ConfigEntry<float> _colorA;

		private ConfigEntry<float> _scaleX;

		private ConfigEntry<float> _scaleY;

		private ConfigEntry<float> _scaleZ;

		private ConfigEntry<float> _offsetX;

		private ConfigEntry<float> _offsetY;

		private ConfigEntry<float> _offsetZ;

		private ZNetScene _scene;

		private float _nextSceneCheck;

		private bool _settingsDirty = true;

		private bool _loggedSuccess;

		private bool _capturedVanilla;

		private Vector3 _vanillaScale;

		private Vector3 _vanillaLocalPosition;

		private Material _vanillaMaterial;

		private Material _customMaterial;

		private void Awake()
		{
			BindConfig();
			((BaseUnityPlugin)this).Logger.LogInfo((object)"Subtle Shield 1.0.0 loaded.");
		}

		private void BindConfig()
		{
			_enabled = ((BaseUnityPlugin)this).Config.Bind<bool>("1 - General", "Enable custom shield VFX", true, "Use the custom Staff of Protection shield appearance. Turn this off to restore the vanilla shield while the mod stays loaded.");
			_colorR = BindRanged("2 - Color", "Red", 0.2f, 0f, 1f, "Red channel of the shield material color.");
			_colorG = BindRanged("2 - Color", "Green", 0f, 0f, 1f, "Green channel of the shield material color.");
			_colorB = BindRanged("2 - Color", "Blue", 0f, 0f, 1f, "Blue channel of the shield material color.");
			_colorA = BindRanged("2 - Color", "Opacity", 0.3f, 0f, 1f, "Alpha/transparency of the shield material. 0 is fully transparent; 1 is fully opaque.");
			_scaleX = BindRanged("3 - Size", "Width", 1.5f, 0.1f, 5f, "Horizontal X scale of the shield Sphere.");
			_scaleY = BindRanged("3 - Size", "Height", 2.4f, 0.1f, 5f, "Vertical Y scale of the shield Sphere.");
			_scaleZ = BindRanged("3 - Size", "Depth", 1.5f, 0.1f, 5f, "Forward/back Z scale of the shield Sphere.");
			_offsetX = BindRanged("4 - Position", "Horizontal offset", 0f, -5f, 5f, "Local X offset of the shield Sphere relative to the shield object.");
			_offsetY = BindRanged("4 - Position", "Vertical offset", 0f, -5f, 5f, "Local Y offset of the shield Sphere relative to the shield object.");
			_offsetZ = BindRanged("4 - Position", "Forward offset", 0f, -5f, 5f, "Local Z offset of the shield Sphere relative to the shield object.");
			_enabled.SettingChanged += OnSettingChanged;
			_colorR.SettingChanged += OnSettingChanged;
			_colorG.SettingChanged += OnSettingChanged;
			_colorB.SettingChanged += OnSettingChanged;
			_colorA.SettingChanged += OnSettingChanged;
			_scaleX.SettingChanged += OnSettingChanged;
			_scaleY.SettingChanged += OnSettingChanged;
			_scaleZ.SettingChanged += OnSettingChanged;
			_offsetX.SettingChanged += OnSettingChanged;
			_offsetY.SettingChanged += OnSettingChanged;
			_offsetZ.SettingChanged += OnSettingChanged;
		}

		private ConfigEntry<float> BindRanged(string section, string key, float defaultValue, float min, float max, string description)
		{
			//IL_0019: Unknown result type (might be due to invalid IL or missing references)
			//IL_0023: Expected O, but got Unknown
			return ((BaseUnityPlugin)this).Config.Bind<float>(section, key, defaultValue, new ConfigDescription(description, (AcceptableValueBase)(object)new AcceptableValueRange<float>(min, max), Array.Empty<object>()));
		}

		private void OnSettingChanged(object sender, EventArgs e)
		{
			_settingsDirty = true;
		}

		private void Update()
		{
			if (Time.unscaledTime >= _nextSceneCheck)
			{
				_nextSceneCheck = Time.unscaledTime + 1f;
				if ((Object)(object)ZNetScene.instance != (Object)(object)_scene)
				{
					_scene = ZNetScene.instance;
					ResetSceneState();
					_settingsDirty = true;
				}
			}
			if (_settingsDirty)
			{
				_settingsDirty = false;
				if ((Object)(object)_scene != (Object)null)
				{
					ApplyToPrefab(_scene);
				}
				ApplyToExistingShieldInstances();
			}
		}

		private void ResetSceneState()
		{
			_capturedVanilla = false;
			_vanillaMaterial = null;
			_customMaterial = null;
			_loggedSuccess = false;
		}

		private bool ApplyToPrefab(ZNetScene scene)
		{
			GameObject prefab = scene.GetPrefab("vfx_StaffShield");
			if ((Object)(object)prefab == (Object)null)
			{
				return false;
			}
			Transform val = FindChildRecursive(prefab.transform, "Sphere");
			if ((Object)(object)val == (Object)null)
			{
				return false;
			}
			MeshRenderer component = ((Component)val).GetComponent<MeshRenderer>();
			if ((Object)(object)component == (Object)null || (Object)(object)((Renderer)component).sharedMaterial == (Object)null)
			{
				return false;
			}
			CaptureVanillaIfNeeded(val, component);
			ApplyValues(val, component);
			if (!_loggedSuccess)
			{
				((BaseUnityPlugin)this).Logger.LogInfo((object)"Staff of Protection shield VFX patched. Config changes can be applied live through a BepInEx config manager.");
				_loggedSuccess = true;
			}
			return true;
		}

		private void CaptureVanillaIfNeeded(Transform sphere, MeshRenderer renderer)
		{
			//IL_000b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0010: Unknown result type (might be due to invalid IL or missing references)
			//IL_0017: Unknown result type (might be due to invalid IL or missing references)
			//IL_001c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0034: Unknown result type (might be due to invalid IL or missing references)
			//IL_0039: Unknown result type (might be due to invalid IL or missing references)
			//IL_0059: Expected O, but got Unknown
			if (!_capturedVanilla)
			{
				_vanillaScale = sphere.localScale;
				_vanillaLocalPosition = sphere.localPosition;
				_vanillaMaterial = ((Renderer)renderer).sharedMaterial;
				_customMaterial = new Material(_vanillaMaterial)
				{
					name = ((Object)_vanillaMaterial).name + " (SubtleShield)"
				};
				_capturedVanilla = true;
			}
		}

		private void ApplyValues(Transform sphere, MeshRenderer renderer)
		{
			//IL_00bc: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c8: Unknown result type (might be due to invalid IL or missing references)
			//IL_003b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0067: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a3: Unknown result type (might be due to invalid IL or missing references)
			if (_capturedVanilla)
			{
				if (_enabled.Value)
				{
					sphere.localScale = new Vector3(_scaleX.Value, _scaleY.Value, _scaleZ.Value);
					sphere.localPosition = new Vector3(_offsetX.Value, _offsetY.Value, _offsetZ.Value);
					_customMaterial.color = new Color(_colorR.Value, _colorG.Value, _colorB.Value, _colorA.Value);
					((Renderer)renderer).sharedMaterial = _customMaterial;
				}
				else
				{
					sphere.localScale = _vanillaScale;
					sphere.localPosition = _vanillaLocalPosition;
					((Renderer)renderer).sharedMaterial = _vanillaMaterial;
				}
			}
		}

		private void ApplyToExistingShieldInstances()
		{
			if (!_capturedVanilla)
			{
				return;
			}
			GameObject[] array = Object.FindObjectsByType<GameObject>((FindObjectsSortMode)0);
			string text = "vfx_StaffShield(Clone)";
			foreach (GameObject val in array)
			{
				if ((Object)(object)val == (Object)null || ((Object)val).name != text)
				{
					continue;
				}
				Transform val2 = FindChildRecursive(val.transform, "Sphere");
				if (!((Object)(object)val2 == (Object)null))
				{
					MeshRenderer component = ((Component)val2).GetComponent<MeshRenderer>();
					if (!((Object)(object)component == (Object)null))
					{
						ApplyValues(val2, component);
					}
				}
			}
		}

		private static Transform FindChildRecursive(Transform root, string wantedName)
		{
			if ((Object)(object)root == (Object)null)
			{
				return null;
			}
			if (((Object)root).name == wantedName)
			{
				return root;
			}
			for (int i = 0; i < root.childCount; i++)
			{
				Transform val = FindChildRecursive(root.GetChild(i), wantedName);
				if ((Object)(object)val != (Object)null)
				{
					return val;
				}
			}
			return null;
		}
	}
}