Decompiled source of DvergrLightControl v1.0.0

DvergrLightControl.dll

Decompiled 11 hours ago
using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using Microsoft.CodeAnalysis;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("DvergrLightControl")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("DvergrLightControl")]
[assembly: AssemblyTitle("DvergrLightControl")]
[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.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]
	[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]
	[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 DvergrLightControl
{
	[BepInPlugin("com.Azura.dvergrlightcontrol", "Dvergr Light Control", "1.0.0")]
	[BepInProcess("valheim.exe")]
	[BepInProcess("valheim.x86_64")]
	public class DvergrLightControlPlugin : BaseUnityPlugin
	{
		public const string PluginGUID = "com.Azura.dvergrlightcontrol";

		public const string PluginName = "Dvergr Light Control";

		public const string PluginVersion = "1.0.0";

		private ConfigEntry<KeyboardShortcut> m_toggleKey;

		private ConfigEntry<bool> m_autoDaylight;

		private ConfigEntry<bool> m_showNotification;

		private ConfigEntry<bool> m_applyToOtherPlayers;

		private bool m_lightEnabled = true;

		private bool? m_lastIsDark;

		private float m_checkTimer;

		public static DvergrLightControlPlugin Instance { get; private set; }

		public static ManualLogSource Log => ((BaseUnityPlugin)Instance).Logger;

		private void Awake()
		{
			//IL_001e: 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)
			//IL_00c3: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c6: Unknown result type (might be due to invalid IL or missing references)
			Instance = this;
			m_toggleKey = ((BaseUnityPlugin)this).Config.Bind<KeyboardShortcut>("1. Raccourcis", "ToggleKey", new KeyboardShortcut((KeyCode)108, Array.Empty<KeyCode>()), "Touche du clavier pour allumer ou eteindre manuellement la lumiere du Diademe de Dverger.");
			m_autoDaylight = ((BaseUnityPlugin)this).Config.Bind<bool>("2. Automatisation", "AutoDaylight", true, "Eteint automatiquement la lumiere en plein jour a l'exterieur, et la rallume la nuit ou dans les cryptes/donjons.");
			m_showNotification = ((BaseUnityPlugin)this).Config.Bind<bool>("3. Affichage", "ShowNotification", true, "Afficher une notification discrete a l'ecran lors de la bascule de la lumiere.");
			m_applyToOtherPlayers = ((BaseUnityPlugin)this).Config.Bind<bool>("4. Multijoueur", "ApplyToOtherPlayers", true, "Eteint egalement la torche des autres joueurs en journee sur votre ecran pour le confort visuel.");
			ManualLogSource logger = ((BaseUnityPlugin)this).Logger;
			object[] obj = new object[4] { "Dvergr Light Control", "1.0.0", null, null };
			KeyboardShortcut value = m_toggleKey.Value;
			obj[2] = ((KeyboardShortcut)(ref value)).MainKey;
			obj[3] = m_autoDaylight.Value;
			logger.LogInfo((object)string.Format("{0} v{1} initialise avec succes ! Touche: {2}, AutoDaylight: {3}", obj));
		}

		private void Update()
		{
			//IL_0027: Unknown result type (might be due to invalid IL or missing references)
			//IL_002c: Unknown result type (might be due to invalid IL or missing references)
			Player localPlayer = Player.m_localPlayer;
			if ((Object)(object)localPlayer == (Object)null || ((Character)localPlayer).IsDead() || IsInputBlocked())
			{
				return;
			}
			KeyboardShortcut value = m_toggleKey.Value;
			if (((KeyboardShortcut)(ref value)).IsDown())
			{
				m_lightEnabled = !m_lightEnabled;
				NotifyPlayer(m_lightEnabled ? "\ud83d\udca1 Diadème de Dverger : Allumé" : "\ud83d\udca1 Diadème de Dverger : Éteint");
			}
			if (m_autoDaylight.Value)
			{
				m_checkTimer += Time.deltaTime;
				if (m_checkTimer >= 1f)
				{
					m_checkTimer = 0f;
					CheckDaylightTransition(localPlayer);
				}
			}
		}

		private void LateUpdate()
		{
			Player localPlayer = Player.m_localPlayer;
			if ((Object)(object)localPlayer == (Object)null)
			{
				return;
			}
			ApplyLightState(localPlayer, m_lightEnabled);
			if (!m_applyToOtherPlayers.Value || Player.GetAllPlayers() == null)
			{
				return;
			}
			bool flag = IsEnvironmentDark(localPlayer);
			foreach (Player allPlayer in Player.GetAllPlayers())
			{
				if ((Object)(object)allPlayer != (Object)null && (Object)(object)allPlayer != (Object)(object)localPlayer)
				{
					bool state = flag || ((Character)allPlayer).InInterior();
					ApplyLightState(allPlayer, state);
				}
			}
		}

		private void CheckDaylightTransition(Player player)
		{
			bool flag = IsEnvironmentDark(player);
			if (!m_lastIsDark.HasValue)
			{
				m_lastIsDark = flag;
				m_lightEnabled = flag;
			}
			else if (m_lastIsDark.Value != flag)
			{
				m_lastIsDark = flag;
				m_lightEnabled = flag;
				NotifyPlayer(flag ? "\ud83d\udca1 Diadème de Dverger : Mode Nuit (Allumé)" : "\ud83d\udca1 Diadème de Dverger : Mode Jour (Éteint)");
			}
		}

		private bool IsEnvironmentDark(Player player)
		{
			if ((Object)(object)player == (Object)null)
			{
				return false;
			}
			if (((Character)player).InInterior())
			{
				return true;
			}
			return !EnvMan.IsDaylight();
		}

		private void ApplyLightState(Player player, bool state)
		{
			VisEquipment component = ((Component)player).GetComponent<VisEquipment>();
			if ((Object)(object)component == (Object)null || (Object)(object)component.m_helmet == (Object)null)
			{
				return;
			}
			Light[] componentsInChildren = ((Component)component.m_helmet).GetComponentsInChildren<Light>(true);
			if (componentsInChildren == null || componentsInChildren.Length == 0)
			{
				return;
			}
			Light[] array = componentsInChildren;
			foreach (Light val in array)
			{
				if ((Object)(object)val != (Object)null && ((Behaviour)val).enabled != state)
				{
					((Behaviour)val).enabled = state;
				}
			}
		}

		private bool IsInputBlocked()
		{
			if ((Object)(object)Chat.instance != (Object)null && Chat.instance.HasFocus())
			{
				return true;
			}
			if (Console.IsVisible())
			{
				return true;
			}
			if (Menu.IsVisible())
			{
				return true;
			}
			if (TextInput.IsVisible())
			{
				return true;
			}
			return false;
		}

		private void NotifyPlayer(string message)
		{
			if (m_showNotification.Value && (Object)(object)MessageHud.instance != (Object)null)
			{
				MessageHud.instance.ShowMessage((MessageType)1, message, 0, (Sprite)null, false, true);
			}
		}
	}
}