Decompiled source of HeadLamp v1.5.0

plugins/HeadLamp.dll

Decompiled a week ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using HarmonyLib;
using Jotunn.Managers;
using Jotunn.Utils;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyTitle("HeadLamp")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("HeadLamp")]
[assembly: AssemblyCopyright("Copyright ©  2023")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("a923c236-d09c-47f9-8e86-3007a1d2721f")]
[assembly: AssemblyFileVersion("1.5.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyVersion("1.5.0.0")]
namespace HeadLamp;

public static class Configs
{
	public static ConfigEntry<bool> enable_debug_logs;

	public static ConfigEntry<bool> enable_settings_sync_with_server;

	public static ConfigEntry<KeyboardShortcut> hotkey_trigger_lamp;

	public static ConfigEntry<Color> light_color;

	public static ConfigEntry<float> light_intensity;

	public static ConfigEntry<float> light_range;

	public static ConfigEntry<float> light_spot_angle;

	public static ConfigEntry<float> light_sphere_height_offset;

	public static ConfigEntry<float> light_sphere_look_offset;

	public static ConfigEntry<float> light_sphere_scale;

	public static void SetupConfigs(ConfigFile config)
	{
		//IL_0000: Unknown result type (might be due to invalid IL or missing references)
		//IL_0005: Unknown result type (might be due to invalid IL or missing references)
		//IL_000d: Expected O, but got Unknown
		//IL_004e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0058: Expected O, but got Unknown
		//IL_0074: Unknown result type (might be due to invalid IL or missing references)
		//IL_0098: Unknown result type (might be due to invalid IL or missing references)
		//IL_00b1: Unknown result type (might be due to invalid IL or missing references)
		//IL_00bb: Expected O, but got Unknown
		//IL_00f7: Unknown result type (might be due to invalid IL or missing references)
		//IL_0101: Expected O, but got Unknown
		//IL_013d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0147: Expected O, but got Unknown
		//IL_0183: Unknown result type (might be due to invalid IL or missing references)
		//IL_018d: Expected O, but got Unknown
		//IL_01c0: Unknown result type (might be due to invalid IL or missing references)
		//IL_01ca: Expected O, but got Unknown
		//IL_01fd: Unknown result type (might be due to invalid IL or missing references)
		//IL_0207: Expected O, but got Unknown
		//IL_023a: Unknown result type (might be due to invalid IL or missing references)
		//IL_0244: Expected O, but got Unknown
		ConfigurationManagerAttributes val = new ConfigurationManagerAttributes
		{
			IsAdminOnly = true
		};
		enable_debug_logs = config.Bind<bool>("General", GenerateConfigKey("enable_debug_logs"), false, "Enables debug logs.");
		enable_settings_sync_with_server = config.Bind<bool>("General", GenerateConfigKey("enable_settings_sync_with_server"), true, new ConfigDescription("Locks the settings (apart from the hotkey) on the server side (if hosting a server), so that every player's settings are synchronized with the server.", (AcceptableValueBase)null, new object[1] { val }));
		hotkey_trigger_lamp = config.Bind<KeyboardShortcut>("General", GenerateConfigKey("hotkey_trigger_lamp"), new KeyboardShortcut((KeyCode)96, Array.Empty<KeyCode>()), "Hotkey for switching the head lamp on and off. Examples: Mouse 0, Mouse 1, Q, C, Shift + Q, Shift + C");
		light_color = config.Bind<Color>("Light", GenerateConfigKey("light_color"), Color.white, new ConfigDescription("Color of the head lamp.", (AcceptableValueBase)null, new object[1] { IsAdminOnly() }));
		light_intensity = config.Bind<float>("Light", GenerateConfigKey("light_intensity"), 2f, new ConfigDescription("Intensity of the head lamp.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0.1f, 10f), new object[1] { IsAdminOnly() }));
		light_range = config.Bind<float>("Light", GenerateConfigKey("light_range"), 30f, new ConfigDescription("Range of the head lamp.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(5f, 100f), new object[1] { IsAdminOnly() }));
		light_spot_angle = config.Bind<float>("Light", GenerateConfigKey("light_spot_angle"), 90f, new ConfigDescription("Spot angle of the head lamp.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(30f, 150f), new object[1] { IsAdminOnly() }));
		light_sphere_height_offset = config.Bind<float>("Light Sphere", GenerateConfigKey("light_sphere_height_offset"), 0.2f, new ConfigDescription("Height offset of the light sphere. The higher the value, the higher up it will be above the player's head.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0f, 1f), Array.Empty<object>()));
		light_sphere_look_offset = config.Bind<float>("Light Sphere", GenerateConfigKey("light_sphere_look_offset"), 0.03f, new ConfigDescription("Look offset of the light sphere. The higher the value, the further forward it will be placed.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0f, 1f), Array.Empty<object>()));
		light_sphere_scale = config.Bind<float>("Light Sphere", GenerateConfigKey("light_sphere_scale"), 0.25f, new ConfigDescription("Scale of the light sphere.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0f, 1f), Array.Empty<object>()));
		config.SettingChanged += Config_SettingChanged;
	}

	private static void Config_SettingChanged(object sender, SettingChangedEventArgs e)
	{
		PlayerHeadLamp.RefreshLights();
	}

	public static string GenerateConfigKey(string key)
	{
		return GenerateConfigKey(key, null);
	}

	public static string GenerateConfigKey(string key, string unit)
	{
		key = CultureInfo.CurrentCulture.TextInfo.ToTitleCase(key.Replace('_', ' ').ToLower());
		if (unit != null)
		{
			key += $" ({unit})";
		}
		return key;
	}

	private static ConfigurationManagerAttributes IsAdminOnly()
	{
		//IL_000e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0013: Unknown result type (might be due to invalid IL or missing references)
		//IL_001b: Expected O, but got Unknown
		if (!enable_settings_sync_with_server.Value)
		{
			return null;
		}
		return new ConfigurationManagerAttributes
		{
			IsAdminOnly = true
		};
	}
}
public class FxDestroyer : MonoBehaviour
{
	private void OnDestroy()
	{
		if ((Object)(object)ZNetScene.instance != (Object)null && (Object)(object)((Component)this).gameObject != (Object)null)
		{
			ZNetScene.instance.Destroy(((Component)this).gameObject);
		}
	}
}
[BepInPlugin("HeadLamp", "Head Lamp", "1.5.0")]
[BepInDependency(/*Could not decode attribute arguments.*/)]
[NetworkCompatibility(/*Could not decode attribute arguments.*/)]
public class HeadLamp : BaseUnityPlugin
{
	[HarmonyPatch(typeof(Player), "Update")]
	private class Player_Update_Patch
	{
		private static void Postfix(Player __instance)
		{
			if ((Object)(object)((Component)__instance).GetComponent<PlayerHeadLamp>() == (Object)null)
			{
				((Component)__instance).gameObject.AddComponent<PlayerHeadLamp>();
			}
			if ((Object)(object)Player.m_localPlayer == (Object)(object)__instance)
			{
				ProcessInput(__instance);
			}
		}
	}

	[HarmonyPatch(typeof(Player), "OnDeath")]
	private class Player_OnDeath_Patch
	{
		private static void Postfix(Player __instance)
		{
			if ((Object)(object)Player.m_localPlayer == (Object)(object)__instance)
			{
				ZNetView component = ((Component)__instance).GetComponent<ZNetView>();
				if ((Object)(object)component != (Object)null && component.IsValid() && component.IsOwner())
				{
					component.GetZDO().Set("HeadLamp.Enabled", false);
				}
				HeadLampSwitchedOn = false;
				DestroyLightSphere();
			}
		}
	}

	public const string PluginVersion = "1.5.0";

	private Harmony harmony = new Harmony("HeadLamp");

	public static GameObject fxHeadLampPrefab;

	public static GameObject fxHeadLampInstance;

	public static bool HeadLampSwitchedOn { get; internal set; }

	private void Awake()
	{
		Configs.SetupConfigs(((BaseUnityPlugin)this).Config);
		Assembly executingAssembly = Assembly.GetExecutingAssembly();
		string name = executingAssembly.GetManifestResourceNames().Single((string str) => str.EndsWith("headlamp"));
		Stream manifestResourceStream = executingAssembly.GetManifestResourceStream(name);
		AssetBundle val;
		using (manifestResourceStream)
		{
			val = AssetBundle.LoadFromStream(manifestResourceStream);
		}
		fxHeadLampPrefab = val.LoadAsset<GameObject>("Assets/CustomAssets/Prefabs/vfx_headlamp.prefab");
		val.Unload(false);
		ShapePrefab();
		SynchronizationManager.OnConfigurationSynchronized += ConfigurationSynchronized;
		harmony = Harmony.CreateAndPatchAll(Assembly.GetExecutingAssembly(), "HeadLamp");
	}

	private void OnDestroy()
	{
		SynchronizationManager.OnConfigurationSynchronized -= ConfigurationSynchronized;
		Harmony obj = harmony;
		if (obj != null)
		{
			obj.UnpatchSelf();
		}
		PlayerHeadLamp.Shutdown();
	}

	private static void ConfigurationSynchronized(object sender, ConfigurationSynchronizationEventArgs e)
	{
		PlayerHeadLamp.RefreshLights();
	}

	private void ShapePrefab()
	{
		fxHeadLampPrefab.AddComponent<Light>().type = (LightType)0;
		UpdateLightProperties(fxHeadLampPrefab);
	}

	private static void ProcessInput(Player player)
	{
		//IL_002e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0033: Unknown result type (might be due to invalid IL or missing references)
		//IL_0060: Unknown result type (might be due to invalid IL or missing references)
		//IL_0065: Unknown result type (might be due to invalid IL or missing references)
		//IL_0047: Unknown result type (might be due to invalid IL or missing references)
		//IL_004c: Unknown result type (might be due to invalid IL or missing references)
		//IL_004f: Unknown result type (might be due to invalid IL or missing references)
		if (((Character)player).IsDead() || !(bool)((object)player).GetType().GetMethod("TakeInput", BindingFlags.Instance | BindingFlags.NonPublic).Invoke(player, null))
		{
			return;
		}
		KeyboardShortcut value = Configs.hotkey_trigger_lamp.Value;
		if (((KeyboardShortcut)(ref value)).Modifiers.Count() == 0)
		{
			value = Configs.hotkey_trigger_lamp.Value;
			if (Input.GetKeyDown(((KeyboardShortcut)(ref value)).MainKey))
			{
				goto IL_006f;
			}
		}
		value = Configs.hotkey_trigger_lamp.Value;
		if (!((KeyboardShortcut)(ref value)).IsDown())
		{
			return;
		}
		goto IL_006f;
		IL_006f:
		TriggerHeadLamp(player);
	}

	public static void TriggerHeadLamp(Player player)
	{
		ZNetView component = ((Component)player).GetComponent<ZNetView>();
		if (!((Object)(object)component == (Object)null) && component.IsValid() && component.IsOwner() && !((Character)player).IsDead())
		{
			HeadLampSwitchedOn = !component.GetZDO().GetBool("HeadLamp.Enabled", false);
			component.GetZDO().Set("HeadLamp.Enabled", HeadLampSwitchedOn);
		}
	}

	public static void UpdateLightProperties(GameObject headLamp)
	{
		//IL_001f: Unknown result type (might be due to invalid IL or missing references)
		if ((Object)(object)headLamp != (Object)null)
		{
			Light component = headLamp.GetComponent<Light>();
			if ((Object)(object)component != (Object)null)
			{
				component.color = Configs.light_color.Value;
				component.intensity = Configs.light_intensity.Value;
				component.range = Configs.light_range.Value;
				float spotAngle = (component.innerSpotAngle = Configs.light_spot_angle.Value);
				component.spotAngle = spotAngle;
			}
		}
	}

	public static void UpdateLightSphereProperties(GameObject headLamp)
	{
		//IL_0038: Unknown result type (might be due to invalid IL or missing references)
		//IL_0048: Unknown result type (might be due to invalid IL or missing references)
		//IL_0057: Unknown result type (might be due to invalid IL or missing references)
		//IL_0061: Unknown result type (might be due to invalid IL or missing references)
		//IL_0079: Unknown result type (might be due to invalid IL or missing references)
		//IL_007e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0087: Unknown result type (might be due to invalid IL or missing references)
		//IL_008c: Unknown result type (might be due to invalid IL or missing references)
		if ((Object)(object)headLamp != (Object)null)
		{
			headLamp.transform.localPosition = new Vector3((0f - Configs.light_sphere_look_offset.Value) * 0.01f, Configs.light_sphere_height_offset.Value * 0.01f, 0f);
			headLamp.transform.localScale = Vector3.one * Configs.light_sphere_scale.Value * 0.01f;
			ParticleSystem[] componentsInChildren = headLamp.GetComponentsInChildren<ParticleSystem>();
			for (int i = 0; i < componentsInChildren.Length; i++)
			{
				MainModule main = componentsInChildren[i].main;
				((MainModule)(ref main)).startColor = MinMaxGradient.op_Implicit(headLamp.GetComponent<Light>().color);
			}
		}
	}

	public static void DestroyLightSphere()
	{
		if ((Object)(object)fxHeadLampInstance != (Object)null)
		{
			Object.Destroy((Object)(object)fxHeadLampInstance);
			fxHeadLampInstance = null;
		}
	}
}
public class PlayerHeadLamp : MonoBehaviour
{
	internal const string EnabledKey = "HeadLamp.Enabled";

	private static readonly List<PlayerHeadLamp> instances = new List<PlayerHeadLamp>();

	private Player player;

	private ZNetView view;

	private GameObject lightInstance;

	private void Awake()
	{
		player = ((Component)this).GetComponent<Player>();
		view = ((Component)this).GetComponent<ZNetView>();
		instances.Add(this);
	}

	internal static void Shutdown()
	{
		PlayerHeadLamp[] array = instances.ToArray();
		foreach (PlayerHeadLamp obj in array)
		{
			((Behaviour)obj).enabled = false;
			Object.Destroy((Object)(object)obj);
		}
		HeadLamp.HeadLampSwitchedOn = false;
	}

	private void LateUpdate()
	{
		//IL_0082: Unknown result type (might be due to invalid IL or missing references)
		//IL_0088: Invalid comparison between Unknown and I4
		//IL_00e7: Unknown result type (might be due to invalid IL or missing references)
		bool flag = (Object)(object)view != (Object)null && view.IsValid() && !((Character)player).IsDead() && view.GetZDO().GetBool("HeadLamp.Enabled", false);
		if ((Object)(object)player == (Object)(object)Player.m_localPlayer)
		{
			HeadLamp.HeadLampSwitchedOn = flag;
		}
		if (!flag)
		{
			DestroyLight();
			return;
		}
		if ((Object)(object)lightInstance == (Object)null && (Object)(object)HeadLamp.fxHeadLampPrefab != (Object)null && (int)SystemInfo.graphicsDeviceType != 4)
		{
			Animator componentInChildren = ((Component)player).GetComponentInChildren<Animator>();
			if ((Object)(object)componentInChildren == (Object)null || !componentInChildren.isHuman)
			{
				return;
			}
			Transform boneTransform = componentInChildren.GetBoneTransform((HumanBodyBones)10);
			if ((Object)(object)boneTransform == (Object)null)
			{
				return;
			}
			lightInstance = Object.Instantiate<GameObject>(HeadLamp.fxHeadLampPrefab, boneTransform, false);
			lightInstance.transform.localRotation = Quaternion.Euler(0f, 270f, 180f);
			ApplySettings();
		}
		if ((Object)(object)player == (Object)(object)Player.m_localPlayer)
		{
			HeadLamp.fxHeadLampInstance = lightInstance;
		}
	}

	private void OnDisable()
	{
		DestroyLight();
	}

	private void OnDestroy()
	{
		instances.Remove(this);
		DestroyLight();
		if ((Object)(object)player == (Object)(object)Player.m_localPlayer)
		{
			HeadLamp.HeadLampSwitchedOn = false;
		}
	}

	internal static void RefreshLights()
	{
		foreach (PlayerHeadLamp instance in instances)
		{
			instance.ApplySettings();
		}
	}

	private void ApplySettings()
	{
		HeadLamp.UpdateLightProperties(lightInstance);
		HeadLamp.UpdateLightSphereProperties(lightInstance);
	}

	private void DestroyLight()
	{
		if ((Object)(object)lightInstance != (Object)null)
		{
			if ((Object)(object)HeadLamp.fxHeadLampInstance == (Object)(object)lightInstance)
			{
				HeadLamp.fxHeadLampInstance = null;
			}
			Object.Destroy((Object)(object)lightInstance);
			lightInstance = null;
		}
	}
}