Decompiled source of ValuableInvulnHighlighter v1.0.0

ValuableInvulnHighlighter.dll

Decompiled 2 days ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Configuration;
using Microsoft.CodeAnalysis;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: IgnoresAccessChecksTo("")]
[assembly: AssemblyCompany("zabu")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("zabumod")]
[assembly: AssemblyTitle("zabumod")]
[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.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]
	[Microsoft.CodeAnalysis.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]
	[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;
		}
	}
}
[BepInPlugin("REPOJP.ValuableInvulnHighlighter", "ValuableInvulnHighlighter", "1.0.0")]
public class ValuableInvulnHighlighterPlugin : BaseUnityPlugin
{
	private sealed class TrackedValuable
	{
		public PhysGrabObjectImpactDetector Impact;

		public TrackedRenderer[] Renderers;

		public bool IsApplied;
	}

	private struct TrackedRenderer
	{
		public Renderer Renderer;

		public int ColorPropId;

		public Color OriginalColor;

		public bool HasEmissionProp;

		public Color OriginalEmissionColor;

		public Material[] OriginalSharedMaterials;

		public Material[] InstancedMaterials;
	}

	public const string PluginGuid = "REPOJP.ValuableInvulnHighlighter";

	public const string PluginName = "ValuableInvulnHighlighter";

	public const string PluginVersion = "1.0.0";

	private static ConfigEntry<bool> CfgEnableMod;

	private static ConfigEntry<float> CfgRadius;

	private static ConfigEntry<float> CfgScanIntervalSeconds;

	private static ConfigEntry<bool> CfgEnableGlow;

	private static ConfigEntry<float> CfgGlowIntensity;

	private static readonly int ShaderPropBaseColor = Shader.PropertyToID("_BaseColor");

	private static readonly int ShaderPropColor = Shader.PropertyToID("_Color");

	private static readonly int ShaderPropEmissionColor = Shader.PropertyToID("_EmissionColor");

	private const string EmissionKeyword = "_EMISSION";

	private const int ColliderBufferSize = 4096;

	private readonly Collider[] _colliderBuffer = (Collider[])(object)new Collider[4096];

	private readonly Dictionary<int, TrackedValuable> _tracked = new Dictionary<int, TrackedValuable>(1024);

	private readonly HashSet<int> _seenThisScan = new HashSet<int>();

	private readonly List<int> _toUntrack = new List<int>(512);

	private readonly MaterialPropertyBlock _mpb = new MaterialPropertyBlock();

	private float _scanTimer;

	private void Awake()
	{
		CfgEnableMod = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "EnableMod", true, "Enable mod.有効化");
		CfgRadius = ((BaseUnityPlugin)this).Config.Bind<float>("General", "Radius", 15f, "Scan radius around local player.プレイヤー周囲の半径");
		CfgScanIntervalSeconds = ((BaseUnityPlugin)this).Config.Bind<float>("General", "ScanIntervalSeconds", 0.1f, "Scan interval in seconds.走査間隔秒");
		CfgEnableGlow = ((BaseUnityPlugin)this).Config.Bind<bool>("Glow", "EnableGlow", true, "Enable red glow via emission.発光有効化");
		CfgGlowIntensity = ((BaseUnityPlugin)this).Config.Bind<float>("Glow", "GlowIntensity", 0.35f, "Glow intensity multiplier.発光強度倍率");
		_scanTimer = 0f;
	}

	private void OnDisable()
	{
		RestoreAll();
	}

	private void Update()
	{
		if (!CfgEnableMod.Value)
		{
			if (_tracked.Count > 0)
			{
				RestoreAll();
			}
			return;
		}
		if ((Object)(object)PlayerController.instance == (Object)null)
		{
			if (_tracked.Count > 0)
			{
				RestoreAll();
			}
			return;
		}
		_scanTimer -= Time.deltaTime;
		if (!(_scanTimer > 0f))
		{
			_scanTimer = Mathf.Max(0.01f, CfgScanIntervalSeconds.Value);
			ScanAndApply();
		}
	}

	private void ScanAndApply()
	{
		//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_0032: Unknown result type (might be due to invalid IL or missing references)
		//IL_016a: Unknown result type (might be due to invalid IL or missing references)
		_seenThisScan.Clear();
		Vector3 position = ((Component)PlayerController.instance).transform.position;
		float num = Mathf.Max(0.1f, CfgRadius.Value);
		int num2 = Physics.OverlapSphereNonAlloc(position, num, _colliderBuffer, -1, (QueryTriggerInteraction)2);
		Color pureRed = default(Color);
		((Color)(ref pureRed))..ctor(1f, 0f, 0f, 1f);
		bool value = CfgEnableGlow.Value;
		float glowIntensity = Mathf.Max(0f, CfgGlowIntensity.Value);
		for (int i = 0; i < num2; i++)
		{
			Collider val = _colliderBuffer[i];
			if ((Object)(object)val == (Object)null)
			{
				continue;
			}
			PhysGrabObjectImpactDetector componentInParent = ((Component)val).GetComponentInParent<PhysGrabObjectImpactDetector>();
			if ((Object)(object)componentInParent == (Object)null || !componentInParent.isValuable)
			{
				continue;
			}
			bool isIndestructible = componentInParent.isIndestructible;
			int instanceID = ((Object)componentInParent).GetInstanceID();
			_seenThisScan.Add(instanceID);
			if (!_tracked.TryGetValue(instanceID, out TrackedValuable value2))
			{
				value2 = CreateTracked(componentInParent);
				if (value2 == null)
				{
					continue;
				}
				_tracked[instanceID] = value2;
			}
			else if ((Object)(object)value2.Impact == (Object)null)
			{
				value2.Impact = componentInParent;
			}
			if (isIndestructible)
			{
				ApplyRedAndGlow(value2, pureRed, value, glowIntensity);
			}
			else
			{
				Restore(value2);
			}
		}
		_toUntrack.Clear();
		foreach (KeyValuePair<int, TrackedValuable> item in _tracked)
		{
			int key = item.Key;
			TrackedValuable value3 = item.Value;
			if (!_seenThisScan.Contains(key))
			{
				Restore(value3);
			}
			if (value3 == null || (Object)(object)value3.Impact == (Object)null)
			{
				_toUntrack.Add(key);
			}
			else if ((Object)(object)((Component)value3.Impact).gameObject == (Object)null)
			{
				_toUntrack.Add(key);
			}
		}
		for (int j = 0; j < _toUntrack.Count; j++)
		{
			_tracked.Remove(_toUntrack[j]);
		}
	}

	private TrackedValuable CreateTracked(PhysGrabObjectImpactDetector impact)
	{
		//IL_010a: Unknown result type (might be due to invalid IL or missing references)
		//IL_010f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0132: Unknown result type (might be due to invalid IL or missing references)
		//IL_0134: Unknown result type (might be due to invalid IL or missing references)
		//IL_0144: Unknown result type (might be due to invalid IL or missing references)
		//IL_0146: Unknown result type (might be due to invalid IL or missing references)
		//IL_00c7: Unknown result type (might be due to invalid IL or missing references)
		//IL_00cc: Unknown result type (might be due to invalid IL or missing references)
		//IL_00e6: Unknown result type (might be due to invalid IL or missing references)
		//IL_00eb: Unknown result type (might be due to invalid IL or missing references)
		//IL_00fe: Unknown result type (might be due to invalid IL or missing references)
		//IL_0103: Unknown result type (might be due to invalid IL or missing references)
		if ((Object)(object)impact == (Object)null)
		{
			return null;
		}
		Renderer[] componentsInChildren = ((Component)impact).GetComponentsInChildren<Renderer>(true);
		if (componentsInChildren == null || componentsInChildren.Length == 0)
		{
			return null;
		}
		List<TrackedRenderer> list = new List<TrackedRenderer>(componentsInChildren.Length);
		foreach (Renderer val in componentsInChildren)
		{
			if ((Object)(object)val == (Object)null)
			{
				continue;
			}
			Material sharedMaterial = val.sharedMaterial;
			if ((Object)(object)sharedMaterial == (Object)null)
			{
				continue;
			}
			int num = 0;
			if (sharedMaterial.HasProperty(ShaderPropBaseColor))
			{
				num = ShaderPropBaseColor;
			}
			else
			{
				if (!sharedMaterial.HasProperty(ShaderPropColor))
				{
					continue;
				}
				num = ShaderPropColor;
			}
			Color color;
			try
			{
				color = sharedMaterial.GetColor(num);
			}
			catch
			{
				continue;
			}
			bool flag = sharedMaterial.HasProperty(ShaderPropEmissionColor);
			Color originalEmissionColor = Color.black;
			if (flag)
			{
				try
				{
					originalEmissionColor = sharedMaterial.GetColor(ShaderPropEmissionColor);
				}
				catch
				{
					originalEmissionColor = Color.black;
				}
			}
			list.Add(new TrackedRenderer
			{
				Renderer = val,
				ColorPropId = num,
				OriginalColor = color,
				HasEmissionProp = flag,
				OriginalEmissionColor = originalEmissionColor,
				OriginalSharedMaterials = null,
				InstancedMaterials = null
			});
		}
		if (list.Count == 0)
		{
			return null;
		}
		return new TrackedValuable
		{
			Impact = impact,
			Renderers = list.ToArray(),
			IsApplied = false
		};
	}

	private void ApplyRedAndGlow(TrackedValuable tracked, Color pureRed, bool enableGlow, float glowIntensity)
	{
		//IL_008b: Unknown result type (might be due to invalid IL or missing references)
		//IL_00bb: Unknown result type (might be due to invalid IL or missing references)
		//IL_00be: Unknown result type (might be due to invalid IL or missing references)
		if (tracked == null || tracked.Renderers == null)
		{
			return;
		}
		for (int i = 0; i < tracked.Renderers.Length; i++)
		{
			TrackedRenderer tr = tracked.Renderers[i];
			if (!((Object)(object)tr.Renderer == (Object)null))
			{
				if (enableGlow && tr.HasEmissionProp && glowIntensity > 0f)
				{
					EnsureRendererMaterialsInstancedForEmission(ref tr);
				}
				tr.Renderer.GetPropertyBlock(_mpb);
				_mpb.SetColor(tr.ColorPropId, pureRed);
				if (enableGlow && tr.HasEmissionProp && glowIntensity > 0f)
				{
					_mpb.SetColor(ShaderPropEmissionColor, pureRed * glowIntensity);
				}
				tr.Renderer.SetPropertyBlock(_mpb);
				tracked.Renderers[i] = tr;
			}
		}
		tracked.IsApplied = true;
	}

	private void EnsureRendererMaterialsInstancedForEmission(ref TrackedRenderer tr)
	{
		//IL_0069: Unknown result type (might be due to invalid IL or missing references)
		//IL_0070: Expected O, but got Unknown
		if (tr.OriginalSharedMaterials != null)
		{
			return;
		}
		Material[] sharedMaterials = tr.Renderer.sharedMaterials;
		if (sharedMaterials == null || sharedMaterials.Length == 0)
		{
			return;
		}
		tr.OriginalSharedMaterials = sharedMaterials;
		Material[] array = (Material[])(object)new Material[sharedMaterials.Length];
		for (int i = 0; i < sharedMaterials.Length; i++)
		{
			Material val = sharedMaterials[i];
			if ((Object)(object)val == (Object)null)
			{
				array[i] = null;
				continue;
			}
			Material val2 = new Material(val);
			if (val2.HasProperty(ShaderPropEmissionColor))
			{
				val2.EnableKeyword("_EMISSION");
			}
			array[i] = val2;
		}
		tr.InstancedMaterials = array;
		tr.Renderer.materials = array;
	}

	private void Restore(TrackedValuable tracked)
	{
		//IL_0079: Unknown result type (might be due to invalid IL or missing references)
		//IL_009d: Unknown result type (might be due to invalid IL or missing references)
		if (tracked == null || tracked.Renderers == null || !tracked.IsApplied)
		{
			return;
		}
		for (int i = 0; i < tracked.Renderers.Length; i++)
		{
			TrackedRenderer trackedRenderer = tracked.Renderers[i];
			if ((Object)(object)trackedRenderer.Renderer == (Object)null)
			{
				continue;
			}
			trackedRenderer.Renderer.GetPropertyBlock(_mpb);
			_mpb.SetColor(trackedRenderer.ColorPropId, trackedRenderer.OriginalColor);
			if (trackedRenderer.HasEmissionProp)
			{
				_mpb.SetColor(ShaderPropEmissionColor, trackedRenderer.OriginalEmissionColor);
			}
			trackedRenderer.Renderer.SetPropertyBlock(_mpb);
			if (trackedRenderer.OriginalSharedMaterials != null)
			{
				trackedRenderer.Renderer.sharedMaterials = trackedRenderer.OriginalSharedMaterials;
				if (trackedRenderer.InstancedMaterials != null)
				{
					for (int j = 0; j < trackedRenderer.InstancedMaterials.Length; j++)
					{
						Material val = trackedRenderer.InstancedMaterials[j];
						if ((Object)(object)val != (Object)null)
						{
							Object.Destroy((Object)(object)val);
						}
					}
				}
				trackedRenderer.OriginalSharedMaterials = null;
				trackedRenderer.InstancedMaterials = null;
			}
			tracked.Renderers[i] = trackedRenderer;
		}
		tracked.IsApplied = false;
	}

	private void RestoreAll()
	{
		foreach (KeyValuePair<int, TrackedValuable> item in _tracked)
		{
			Restore(item.Value);
		}
		_tracked.Clear();
		_seenThisScan.Clear();
		_toUntrack.Clear();
	}
}