Decompiled source of HeadLamp v1.3.2

HeadLamp.dll

Decompiled 4 months ago
using System;
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.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[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.0.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyVersion("1.0.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_0001: Unknown result type (might be due to invalid IL or missing references)
		//IL_0006: Unknown result type (might be due to invalid IL or missing references)
		//IL_000f: Expected O, but got Unknown
		//IL_0050: Unknown result type (might be due to invalid IL or missing references)
		//IL_005a: Expected O, but got Unknown
		//IL_0076: Unknown result type (might be due to invalid IL or missing references)
		//IL_009a: Unknown result type (might be due to invalid IL or missing references)
		//IL_00b3: Unknown result type (might be due to invalid IL or missing references)
		//IL_00bd: Expected O, but got Unknown
		//IL_00f9: Unknown result type (might be due to invalid IL or missing references)
		//IL_0103: Expected O, but got Unknown
		//IL_013f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0149: Expected O, but got Unknown
		//IL_0185: Unknown result type (might be due to invalid IL or missing references)
		//IL_018f: Expected O, but got Unknown
		//IL_01c2: Unknown result type (might be due to invalid IL or missing references)
		//IL_01cc: Expected O, but got Unknown
		//IL_01ff: Unknown result type (might be due to invalid IL or missing references)
		//IL_0209: Expected O, but got Unknown
		//IL_023c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0246: 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 more 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;
	}

	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_0010: Unknown result type (might be due to invalid IL or missing references)
		//IL_0015: Unknown result type (might be due to invalid IL or missing references)
		return (!enable_settings_sync_with_server.Value) ? ((ConfigurationManagerAttributes)null) : new ConfigurationManagerAttributes
		{
			IsAdminOnly = true
		};
	}

	private static void Config_SettingChanged(object sender, SettingChangedEventArgs e)
	{
		HeadLamp.UpdateLightProperties(HeadLamp.fxHeadLampInstance);
		HeadLamp.UpdateLightSphereProperties(HeadLamp.fxHeadLampInstance);
	}
}
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.3.2")]
[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)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)
			{
				HeadLampSwitchedOn = false;
			}
		}
	}

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

	public static GameObject fxHeadLampPrefab;

	public static GameObject fxHeadLampInstance;

	public static bool HeadLampSwitchedOn { get; private 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);
		PrefabManager.OnPrefabsRegistered += ShapeAndRegisterPrefab;
		harmony = Harmony.CreateAndPatchAll(Assembly.GetExecutingAssembly(), "HeadLamp");
	}

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

	private void ShapeAndRegisterPrefab()
	{
		Light val = fxHeadLampPrefab.AddComponent<Light>();
		val.type = (LightType)0;
		val.shape = (LightShape)0;
		UpdateLightProperties(fxHeadLampPrefab);
		fxHeadLampPrefab.AddComponent<ZNetView>();
		ZSyncTransform val2 = fxHeadLampPrefab.AddComponent<ZSyncTransform>();
		val2.m_syncScale = true;
		val2.m_characterParentSync = true;
		fxHeadLampPrefab.AddComponent<FxDestroyer>();
		PrefabManager.Instance.RegisterToZNetScene(fxHeadLampPrefab);
	}

	private static void ProcessInput(Player player)
	{
		//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_006b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0070: Unknown result type (might be due to invalid IL or missing references)
		//IL_0052: 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_005a: 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_007f;
			}
		}
		value = Configs.hotkey_trigger_lamp.Value;
		if (!((KeyboardShortcut)(ref value)).IsDown())
		{
			return;
		}
		goto IL_007f;
		IL_007f:
		TriggerHeadLamp(player);
	}

	public static void TriggerHeadLamp(Player player)
	{
		//IL_0046: Unknown result type (might be due to invalid IL or missing references)
		//IL_0051: Unknown result type (might be due to invalid IL or missing references)
		//IL_008f: Unknown result type (might be due to invalid IL or missing references)
		//IL_00a4: Unknown result type (might be due to invalid IL or missing references)
		HeadLampSwitchedOn = !HeadLampSwitchedOn;
		DestroyLightSphere();
		if (HeadLampSwitchedOn)
		{
			DestroyLightSphere();
			Transform boneTransform = ((Component)((Component)player).GetComponent<ZSyncAnimation>()).GetComponentInChildren<Animator>().GetBoneTransform((HumanBodyBones)10);
			fxHeadLampInstance = Object.Instantiate<GameObject>(fxHeadLampPrefab, ((Component)boneTransform).transform.position, ((Component)boneTransform).transform.rotation);
			fxHeadLampInstance.transform.SetParent(((Component)boneTransform).transform);
			fxHeadLampInstance.transform.localRotation = Quaternion.Euler(0f, 270f, 180f);
			fxHeadLampInstance.transform.localPosition = Vector3.zero;
			UpdateLightProperties(fxHeadLampInstance);
			UpdateLightSphereProperties(fxHeadLampInstance);
		}
	}

	public static void UpdateLightProperties(GameObject headLamp)
	{
		//IL_0026: 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_003c: Unknown result type (might be due to invalid IL or missing references)
		//IL_004d: Unknown result type (might be due to invalid IL or missing references)
		//IL_005c: 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_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)
		//IL_0096: Unknown result type (might be due to invalid IL or missing references)
		//IL_009b: 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>();
			ParticleSystem[] array = componentsInChildren;
			foreach (ParticleSystem val in array)
			{
				MainModule main = val.main;
				((MainModule)(ref main)).startColor = MinMaxGradient.op_Implicit(headLamp.GetComponent<Light>().color);
			}
		}
	}

	public static void DestroyLightSphere()
	{
		if ((Object)(object)fxHeadLampInstance != (Object)null)
		{
			if ((Object)(object)ZNetScene.instance != (Object)null)
			{
				ZNetScene.instance.Destroy(fxHeadLampInstance);
			}
			else
			{
				Object.Destroy((Object)(object)fxHeadLampInstance);
			}
		}
	}
}