Decompiled source of OdinsVision v0.0.4

plugins\OdinsVision.dll

Decompiled 40 minutes ago
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Configuration;
using Jotunn;
using Jotunn.Configs;
using Jotunn.Entities;
using Jotunn.Managers;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyTitle("OdinsVision")]
[assembly: AssemblyDescription("Vision of Odin shows you the Living Entities and a glowing red object")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("OdinsVision")]
[assembly: AssemblyCopyright("Copyright ©  2026")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("e3243d22-4307-4008-ba36-9f326008cde5")]
[assembly: AssemblyFileVersion("0.0.4")]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("0.0.4.0")]
namespace OdinsVision;

[BepInPlugin("de.odinsvision.zerocodefx", "OdinsVision", "0.0.4")]
[BepInDependency(/*Could not decode attribute arguments.*/)]
internal class OdinsVision : BaseUnityPlugin
{
	public const string PluginGUID = "de.odinsvision.zerocodefx";

	public const string PluginName = "OdinsVision";

	public const string PluginVersion = "0.0.4";

	private const string ToggleVisionButton = "Monostar_ToggleVision";

	private ConfigEntry<KeyboardShortcut> ToggleVisionKey;

	private bool IsVisionActive;

	private float nextCharacterScan;

	public static CustomLocalization Localization = LocalizationManager.Instance.GetLocalization();

	private void Awake()
	{
		//IL_001b: 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_003e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0049: Unknown result type (might be due to invalid IL or missing references)
		//IL_0050: Unknown result type (might be due to invalid IL or missing references)
		//IL_005a: Unknown result type (might be due to invalid IL or missing references)
		//IL_006a: Expected O, but got Unknown
		ToggleVisionKey = ((BaseUnityPlugin)this).Config.Bind<KeyboardShortcut>("Predator Vision Controls", "ToggleVisionKey", new KeyboardShortcut((KeyCode)289, Array.Empty<KeyCode>()), "Keyboard shortcut used to toggle the red vision outline.");
		InputManager.Instance.AddButton("de.odinsvision.zerocodefx", new ButtonConfig
		{
			Name = "Monostar_ToggleVision",
			Shortcut = ToggleVisionKey.Value,
			Hint = "Toggle Vision"
		});
		Logger.LogInfo((object)"OdinsVision is enabled");
	}

	private void Update()
	{
		//IL_0006: Unknown result type (might be due to invalid IL or missing references)
		//IL_000b: Unknown result type (might be due to invalid IL or missing references)
		//IL_000e: Unknown result type (might be due to invalid IL or missing references)
		//IL_003c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0041: Unknown result type (might be due to invalid IL or missing references)
		//IL_0044: Unknown result type (might be due to invalid IL or missing references)
		KeyboardShortcut value = ToggleVisionKey.Value;
		if ((Input.GetKeyDown(((KeyboardShortcut)(ref value)).MainKey) && !IsTextInputFocused()) || IsToggleVisionPressed())
		{
			value = ToggleVisionKey.Value;
			Logger.LogInfo((object)$"{((KeyboardShortcut)(ref value)).MainKey} pressed by OdinsVision.");
			IsVisionActive = !IsVisionActive;
			if (IsVisionActive)
			{
				UpdateCharacterHighlights(logResults: true);
				nextCharacterScan = Time.time + 0.5f;
			}
			else
			{
				ThermalMaterialManager.RestoreAll();
			}
			Logger.LogInfo((object)("Predator vision " + (IsVisionActive ? "enabled" : "disabled") + "."));
		}
		if (IsVisionActive && Time.time >= nextCharacterScan)
		{
			nextCharacterScan = Time.time + 0.5f;
			UpdateCharacterHighlights();
		}
	}

	private void UpdateCharacterHighlights(bool logResults = false)
	{
		Type type = FindType("Character");
		IEnumerable enumerable = ((type == null) ? null : Array.Find(type.GetMethods(BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic), (MethodInfo methodInfo) => methodInfo.Name == "GetAllCharacters" && methodInfo.GetParameters().Length == 0))?.Invoke(null, null) as IEnumerable;
		if (enumerable == null && type != null)
		{
			enumerable = Object.FindObjectsOfType(type);
		}
		if (enumerable == null)
		{
			Logger.LogWarning((object)"OdinsVision could not find the active character list.");
			return;
		}
		int num = 0;
		int num2 = 0;
		foreach (object item in enumerable)
		{
			Component val = (Component)((item is Component) ? item : null);
			if (val != null && !((Object)(object)val == (Object)null))
			{
				num++;
				Type type2 = ((object)val).GetType();
				MethodInfo method = type2.GetMethod("IsPlayer", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
				MethodInfo method2 = type2.GetMethod("IsDead", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
				if (method != null && (bool)method.Invoke(val, null))
				{
					ThermalMaterialManager.Restore(val);
				}
				else if (method2 != null && (bool)method2.Invoke(val, null))
				{
					ThermalMaterialManager.Restore(val);
				}
				else
				{
					num2 += ThermalMaterialManager.Apply(val);
				}
			}
		}
		ThermalMaterialManager.RemoveDestroyedRenderers();
		if (logResults)
		{
			Logger.LogInfo((object)$"Predator vision scanned {num} characters and applied red material to {num2} renderers.");
		}
	}

	private static bool IsToggleVisionPressed()
	{
		MethodInfo methodInfo = FindType("ZInput")?.GetMethod("GetButtonDown", BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic, null, new Type[1] { typeof(string) }, null);
		if (methodInfo != null)
		{
			return (bool)methodInfo.Invoke(null, new object[1] { "Monostar_ToggleVision" });
		}
		return false;
	}

	private static bool IsTextInputFocused()
	{
		Type type = FindType("Chat");
		object obj = type?.GetField("instance", BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic)?.GetValue(null);
		MethodInfo methodInfo = type?.GetMethod("HasFocus", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
		if (obj != null && methodInfo != null && (bool)methodInfo.Invoke(obj, null))
		{
			return true;
		}
		MethodInfo methodInfo2 = FindType("TextInput")?.GetMethod("IsVisible", BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
		if (methodInfo2 != null)
		{
			return (bool)methodInfo2.Invoke(null, null);
		}
		return false;
	}

	private static Type FindType(string name)
	{
		Type type = null;
		Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
		foreach (Assembly assembly in assemblies)
		{
			Type type2 = assembly.GetType(name);
			if (type2 != null)
			{
				type = type ?? type2;
				if (typeof(Object).IsAssignableFrom(type2))
				{
					return type2;
				}
			}
			try
			{
				type2 = Array.Find(assembly.GetTypes(), (Type candidate) => candidate.Name == name);
				if (type2 != null)
				{
					type = type ?? type2;
					if (typeof(Object).IsAssignableFrom(type2))
					{
						return type2;
					}
				}
			}
			catch (ReflectionTypeLoadException)
			{
			}
		}
		return type;
	}

	private void OnDestroy()
	{
		ThermalMaterialManager.RestoreAll();
	}
}
internal static class ThermalMaterialManager
{
	private sealed class RendererState
	{
		public Renderer Renderer;

		public Component Owner;

		public Material[] OriginalMaterials;

		public Material[] VisionMaterials;
	}

	private static readonly Dictionary<int, RendererState> RendererStates = new Dictionary<int, RendererState>();

	public static int Apply(Component character)
	{
		//IL_0075: Unknown result type (might be due to invalid IL or missing references)
		//IL_007a: Unknown result type (might be due to invalid IL or missing references)
		//IL_0086: Expected O, but got Unknown
		if ((Object)(object)character == (Object)null)
		{
			return 0;
		}
		int num = 0;
		Renderer[] componentsInChildren = character.GetComponentsInChildren<Renderer>(true);
		foreach (Renderer val in componentsInChildren)
		{
			if ((Object)(object)val == (Object)null)
			{
				continue;
			}
			int instanceID = ((Object)val).GetInstanceID();
			if (!RendererStates.TryGetValue(instanceID, out var value))
			{
				Material[] sharedMaterials = val.sharedMaterials;
				Material[] array = (Material[])(object)new Material[sharedMaterials.Length];
				for (int j = 0; j < sharedMaterials.Length; j++)
				{
					if ((Object)(object)sharedMaterials[j] != (Object)null)
					{
						array[j] = new Material(sharedMaterials[j])
						{
							name = "OdinsVision_PredatorVision_Red"
						};
						ConfigureVisionMaterial(array[j]);
					}
				}
				value = new RendererState
				{
					Renderer = val,
					Owner = character,
					OriginalMaterials = sharedMaterials,
					VisionMaterials = array
				};
				RendererStates.Add(instanceID, value);
			}
			val.sharedMaterials = value.VisionMaterials;
			num++;
		}
		return num;
	}

	public static int Restore(Component character)
	{
		if ((Object)(object)character == (Object)null)
		{
			return 0;
		}
		int num = 0;
		foreach (int item in new List<int>(RendererStates.Keys))
		{
			RendererState rendererState = RendererStates[item];
			if (!((Object)(object)rendererState.Owner != (Object)(object)character))
			{
				if ((Object)(object)rendererState.Renderer != (Object)null)
				{
					rendererState.Renderer.sharedMaterials = rendererState.OriginalMaterials;
					num++;
				}
				DestroyVisionMaterials(rendererState.VisionMaterials);
				RendererStates.Remove(item);
			}
		}
		return num;
	}

	public static void RestoreAll()
	{
		foreach (RendererState value in RendererStates.Values)
		{
			if ((Object)(object)value.Renderer != (Object)null)
			{
				value.Renderer.sharedMaterials = value.OriginalMaterials;
			}
			DestroyVisionMaterials(value.VisionMaterials);
		}
		RendererStates.Clear();
	}

	public static void RemoveDestroyedRenderers()
	{
		List<int> list = new List<int>();
		foreach (KeyValuePair<int, RendererState> rendererState in RendererStates)
		{
			if ((Object)(object)rendererState.Value.Renderer == (Object)null || (Object)(object)rendererState.Value.Owner == (Object)null)
			{
				list.Add(rendererState.Key);
			}
		}
		foreach (int item in list)
		{
			DestroyVisionMaterials(RendererStates[item].VisionMaterials);
			RendererStates.Remove(item);
		}
	}

	private static void ConfigureVisionMaterial(Material material)
	{
		//IL_0029: 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_0066: Unknown result type (might be due to invalid IL or missing references)
		//IL_006c: Unknown result type (might be due to invalid IL or missing references)
		Color val = default(Color);
		((Color)(ref val))..ctor(1f, 0f, 0.06f);
		if (material.HasProperty("_Color"))
		{
			material.SetColor("_Color", val);
		}
		if (material.HasProperty("_BaseColor"))
		{
			material.SetColor("_BaseColor", val);
		}
		if (material.HasProperty("_EmissionColor"))
		{
			material.EnableKeyword("_EMISSION");
			material.SetColor("_EmissionColor", val * 3f);
		}
	}

	private static void DestroyVisionMaterials(Material[] materials)
	{
		foreach (Material val in materials)
		{
			if ((Object)(object)val != (Object)null)
			{
				Object.Destroy((Object)(object)val);
			}
		}
	}
}