Decompiled source of LessBright v1.2.0

LessBright.dll

Decompiled 2 days ago
using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Bootstrap;
using BepInEx.Configuration;
using BepInEx.Logging;
using GameNetcodeStuff;
using HarmonyLib;
using LessBright.Config;
using LessBright.Integrations;
using LessBright.Patches;
using LessBright.Utils;
using LethalCompanyInputUtils.Api;
using LethalConfig;
using LethalConfig.ConfigItems;
using LethalConfig.ConfigItems.Options;
using Microsoft.CodeAnalysis;
using Unity.Netcode;
using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.InputSystem.Controls;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: AssemblyCompany("LessBright")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.2.0")]
[assembly: AssemblyInformationalVersion("1.0.0+fef65e3dbf0712592fd8cb7b1ad104f4e73701e5")]
[assembly: AssemblyProduct("LessBright")]
[assembly: AssemblyTitle("LessBright")]
[assembly: AssemblyVersion("1.2.0.0")]
[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;
		}
	}
}
namespace LessBright
{
	internal static class LessBright
	{
		public static ConfigFile Config { get; set; }
	}
	[BepInPlugin("Ferus.LessBright", "LessBright", "1.2.0")]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	public class LessBrightPlugin : BaseUnityPlugin
	{
		private readonly Harmony _harmony = new Harmony("Ferus.LessBright");

		private void Awake()
		{
			LessBright.Config = ((BaseUnityPlugin)this).Config;
			if (Compat.LethalConfig)
			{
				LethalConfigIntegration.Initialize();
			}
			_harmony.PatchAll(typeof(PlayerControllerBPatches));
		}
	}
}
namespace LessBright.Utils
{
	internal static class Checks
	{
		internal static bool IsLocalPlayer(PlayerControllerB controller)
		{
			return NetworkManager.Singleton.LocalClientId == controller.playerClientId;
		}

		internal static bool IsNotLocalPlayer(PlayerControllerB controller)
		{
			return !IsLocalPlayer(controller);
		}
	}
	internal static class Log
	{
		private static readonly ManualLogSource LogSource = Logger.CreateLogSource("Ferus.LessBright");

		public static void Debug(object data)
		{
			LogSource.LogDebug(data);
		}

		public static void Info(object data)
		{
			LogSource.LogInfo(data);
		}

		public static void Warning(object data)
		{
			LogSource.LogWarning(data);
		}

		public static void Error(object data)
		{
			LogSource.LogError(data);
		}
	}
	internal static class Metadata
	{
		internal static class Dependencies
		{
			public const string LethalConfig = "ainavt.lc.lethalconfig";

			public const string InputUtils = "com.rune580.LethalCompanyInputUtils";
		}

		public const string PluginGuid = "Ferus.LessBright";

		public const string PluginName = "LessBright";

		public const string PluginVersion = "1.2.0";
	}
}
namespace LessBright.Patches
{
	[HarmonyPatch(typeof(PlayerControllerB))]
	public class PlayerControllerBPatches
	{
		private const string FlashlightKey = "LessBright_Flashlight";

		private static Light? flashlight;

		[HarmonyPatch("Start")]
		[HarmonyPostfix]
		private static void PostFixStart(ref PlayerControllerB __instance)
		{
			if (!Checks.IsNotLocalPlayer(__instance))
			{
				if (Object.op_Implicit((Object)(object)flashlight))
				{
					throw new Exception("Flashlight already exists");
				}
				if (!Object.op_Implicit((Object)(object)GetFlashlightComponent(__instance)))
				{
					throw new Exception("Failed to create flashlight component");
				}
			}
		}

		[HarmonyPatch("Update")]
		[HarmonyPostfix]
		private static void PostFixUpdate(ref PlayerControllerB __instance)
		{
			//IL_0087: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d7: Unknown result type (might be due to invalid IL or missing references)
			//IL_00dc: Unknown result type (might be due to invalid IL or missing references)
			//IL_00dd: Unknown result type (might be due to invalid IL or missing references)
			//IL_00de: Unknown result type (might be due to invalid IL or missing references)
			if (!Checks.IsNotLocalPlayer(__instance))
			{
				Light flashlightComponent = GetFlashlightComponent(__instance);
				if (!Object.op_Implicit((Object)(object)flashlightComponent))
				{
					throw new Exception("Failed to create flashlight component");
				}
				if (Compat.IsToggleNightVisionTriggered())
				{
					bool flag = !((Component)flashlightComponent).gameObject.activeSelf;
					((Component)flashlightComponent).gameObject.SetActive(flag);
					((Behaviour)flashlightComponent).enabled = flag;
				}
				flashlightComponent.intensity = (int)Configs.Intensity;
				flashlightComponent.range = (int)Configs.Range;
				flashlightComponent.spotAngle = (int)Configs.SpotAngle;
				flashlightComponent.color = Configs.Color;
				Transform transform = ((Component)flashlightComponent).transform;
				Vector3 val = default(Vector3);
				((Vector3)(ref val))..ctor((float)Configs.OffsetX, (float)Configs.OffsetY, (float)Configs.OffsetZ);
				Quaternion val2 = Quaternion.Euler((float)(int)Configs.Yaw, (float)(int)Configs.Pitch, 0f);
				transform.SetLocalPositionAndRotation(val, val2);
			}
		}

		private static Light GetFlashlightComponent(PlayerControllerB controller)
		{
			//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_0023: 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)
			if (Object.op_Implicit((Object)(object)flashlight))
			{
				return flashlight;
			}
			GameObject val = new GameObject("LessBright_Flashlight");
			val.SetActive(true);
			Light val2 = val.AddComponent<Light>();
			((Behaviour)val2).enabled = true;
			Transform transform = val.transform;
			transform.SetParent(((Component)controller.playerEye).transform);
			transform.rotation = Quaternion.LookRotation(((Component)controller.playerEye).transform.forward);
			val2.type = (LightType)0;
			((Behaviour)val2).enabled = true;
			flashlight = val2;
			return flashlight;
		}
	}
}
namespace LessBright.Integrations
{
	internal static class Compat
	{
		public static bool LethalConfig => Chainloader.PluginInfos.ContainsKey("ainavt.lc.lethalconfig");

		private static bool InputUtils => Chainloader.PluginInfos.ContainsKey("com.rune580.LethalCompanyInputUtils");

		private static InputAction NightVisionToggleKey => InputUtilsIntegration.Instance.NightVisionToggleKey;

		public static bool ThirdPartyToggleTriggered { get; set; }

		public static bool IsToggleNightVisionTriggered()
		{
			if (!ThirdPartyToggleTriggered)
			{
				if (!InputUtils)
				{
					return ((ButtonControl)Keyboard.current[(Key)20]).wasPressedThisFrame;
				}
				return NightVisionToggleKey.triggered;
			}
			ThirdPartyToggleTriggered = false;
			return true;
		}
	}
	internal class InputUtilsIntegration : LcInputActions
	{
		public static readonly InputUtilsIntegration Instance = new InputUtilsIntegration();

		public InputAction NightVisionToggleKey => ((LcInputActions)this).Asset["toggleNightVision"];

		public override void CreateInputActions(in InputActionMapBuilder builder)
		{
			builder.NewActionBinding().WithActionId("toggleNightVision").WithActionType((InputActionType)1)
				.WithKbmPath("<Keyboard>/f")
				.WithBindingName("Toggle Night Vision")
				.Finish();
		}
	}
	internal static class LethalConfigIntegration
	{
		[Serializable]
		[CompilerGenerated]
		private sealed class <>c
		{
			public static readonly <>c <>9 = new <>c();

			public static GenericButtonHandler <>9__0_0;

			internal void <Initialize>b__0_0()
			{
				Compat.ThirdPartyToggleTriggered = true;
			}
		}

		public static void Initialize()
		{
			//IL_0023: Unknown result type (might be due to invalid IL or missing references)
			//IL_0028: Unknown result type (might be due to invalid IL or missing references)
			//IL_002e: Expected O, but got Unknown
			object obj = <>c.<>9__0_0;
			if (obj == null)
			{
				GenericButtonHandler val = delegate
				{
					Compat.ThirdPartyToggleTriggered = true;
				};
				<>c.<>9__0_0 = val;
				obj = (object)val;
			}
			RegisterButtonEntry("Actions", "Toggle Light", "Manually toggle the light", (GenericButtonHandler)obj);
			RegisterIntEntry(Configs.Intensity, Ranges.Intensity);
			RegisterIntEntry(Configs.Range, Ranges.Range);
			RegisterIntEntry(Configs.SpotAngle, Ranges.SpotAngle);
			RegisterTextEntry(Configs.Color);
			RegisterFloatEntry(Configs.OffsetX, Ranges.OffsetX);
			RegisterFloatEntry(Configs.OffsetY, Ranges.OffsetY);
			RegisterFloatEntry(Configs.OffsetZ, Ranges.OffsetZ);
			RegisterIntEntry(Configs.Pitch, Ranges.Pitch);
			RegisterIntEntry(Configs.Yaw, Ranges.Yaw);
		}

		private static void RegisterButtonEntry(string section, string name, string description, GenericButtonHandler handler)
		{
			//IL_0005: Unknown result type (might be due to invalid IL or missing references)
			//IL_000f: Expected O, but got Unknown
			LethalConfigManager.AddConfigItem((BaseConfigItem)new GenericButtonConfigItem(section, name, description, name, handler));
		}

		private static void RegisterTextEntry<TMapped>(WrappedConfigEntry<string, TMapped> entry)
		{
			LethalConfigManager.AddConfigItem(entry.ToTextConfigItem());
		}

		private static void RegisterIntEntry<TMapped>(WrappedConfigEntry<int, TMapped> entry, IntRange range)
		{
			LethalConfigManager.AddConfigItem(entry.ToIntConfigItem(range.Min, range.Max));
		}

		private static void RegisterFloatEntry<TMapped>(WrappedConfigEntry<float, TMapped> entry, FloatRange range)
		{
			LethalConfigManager.AddConfigItem(entry.ToFloatConfigItem(range.Min, range.Max, range.Step));
		}
	}
}
namespace LessBright.Config
{
	internal static class Configs
	{
		public static readonly WrappedConfigEntry<int, int> Intensity = WrappedConfigEntry<float, float>.BasicInt("Basic", "Intensity", "The intensity of the light", Ranges.Intensity.Default);

		public static readonly WrappedConfigEntry<int, int> Range = WrappedConfigEntry<float, float>.BasicInt("Basic", "Range", "The range of the light", Ranges.Range.Default);

		public static readonly WrappedConfigEntry<int, int> SpotAngle = WrappedConfigEntry<float, float>.BasicInt("Basic", "Spot Angle", "The spot angle of the light", Ranges.SpotAngle.Default);

		public static readonly WrappedConfigEntry<string, Color> Color;

		public static readonly WrappedConfigEntry<float, float> OffsetX;

		public static readonly WrappedConfigEntry<float, float> OffsetY;

		public static readonly WrappedConfigEntry<float, float> OffsetZ;

		public static readonly WrappedConfigEntry<int, int> Pitch;

		public static readonly WrappedConfigEntry<int, int> Yaw;

		static Configs()
		{
			Color val = default(Color);
			Color = WrappedConfigEntry<string, Color>.MappedString("Basic", "Color", "The color of the light", "#FFFFFF", (string str) => (!ColorUtility.TryParseHtmlString(str, ref val)) ? Color.white : val);
			OffsetX = WrappedConfigEntry<float, float>.BasicFloat("Position", "X Offset", "The x offset of the light", Ranges.OffsetX.Default);
			OffsetY = WrappedConfigEntry<float, float>.BasicFloat("Position", "Y Offset", "The y offset of the light", Ranges.OffsetY.Default);
			OffsetZ = WrappedConfigEntry<float, float>.BasicFloat("Position", "Z Offset", "The z offset of the light", Ranges.OffsetZ.Default);
			Pitch = WrappedConfigEntry<float, float>.BasicInt("Position", "Pitch", "The x rotation offset of the light", Ranges.Pitch.Default);
			Yaw = WrappedConfigEntry<float, float>.BasicInt("Position", "Yaw", "The y rotation offset of the light", Ranges.Yaw.Default);
		}
	}
	public readonly struct FloatRange
	{
		public float Min { get; }

		public float Default { get; }

		public float Max { get; }

		public float Step { get; }

		public FloatRange(float min, float @default, float max, float step)
		{
			Min = min;
			Default = @default;
			Max = max;
			Step = step;
		}
	}
	public readonly struct IntRange
	{
		public int Min { get; }

		public int Default { get; }

		public int Max { get; }

		public IntRange(int min, int @default, int max)
		{
			Min = min;
			Default = @default;
			Max = max;
		}
	}
	internal static class Ranges
	{
		public const string DefaultColorHex = "#FFFFFF";

		public static readonly IntRange Intensity = new IntRange(0, 3000, 25000);

		public static readonly IntRange Range = new IntRange(0, 99999, 99999);

		public static readonly IntRange SpotAngle = new IntRange(1, 130, 179);

		public static readonly FloatRange OffsetX = new FloatRange(-10f, 0f, 10f, 0.1f);

		public static readonly FloatRange OffsetY = new FloatRange(-10f, 1.75f, 10f, 0.1f);

		public static readonly FloatRange OffsetZ = new FloatRange(-10f, 0f, 10f, 0.1f);

		public static readonly IntRange Pitch = new IntRange(0, 0, 359);

		public static readonly IntRange Yaw = new IntRange(0, 0, 359);
	}
	public class WrappedConfigEntry<TType, TMapped>
	{
		private readonly ConfigEntry<TType> _configEntry;

		private readonly Func<TType, TMapped> _mapper;

		private TMapped MappedValue => _mapper(_configEntry.Value);

		private WrappedConfigEntry(ConfigEntry<TType> configEntry, Func<TType, TMapped> mapper)
		{
			_configEntry = configEntry;
			_mapper = mapper;
		}

		public static implicit operator ConfigEntry<TType>(WrappedConfigEntry<TType, TMapped> wrappedConfigEntry)
		{
			return wrappedConfigEntry._configEntry;
		}

		public static implicit operator TMapped(WrappedConfigEntry<TType, TMapped> wrappedConfigEntry)
		{
			return wrappedConfigEntry.MappedValue;
		}

		public static WrappedConfigEntry<int, int> BasicInt(string section, string key, string description, int defaultValue)
		{
			return new WrappedConfigEntry<int, int>(LessBright.Config.Bind<int>(section, key, defaultValue, description), (int x) => x);
		}

		public static WrappedConfigEntry<float, float> BasicFloat(string section, string key, string description, float defaultValue)
		{
			return new WrappedConfigEntry<float, float>(LessBright.Config.Bind<float>(section, key, defaultValue, description), (float x) => x);
		}

		public static WrappedConfigEntry<string, TTMapped> MappedString<TTMapped>(string section, string key, string description, string defaultValue, Func<string, TTMapped> mapper)
		{
			return new WrappedConfigEntry<string, TTMapped>(LessBright.Config.Bind<string>(section, key, defaultValue, description), mapper);
		}
	}
	public static class WrappedConfigEntryToConfigItemExtension
	{
		public static BaseConfigItem ToIntConfigItem<TMapped>(this WrappedConfigEntry<int, TMapped> entry, int min, int max)
		{
			//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_0012: Expected O, but got Unknown
			//IL_0012: Unknown result type (might be due to invalid IL or missing references)
			//IL_0019: Expected O, but got Unknown
			//IL_0019: Unknown result type (might be due to invalid IL or missing references)
			//IL_0025: Expected O, but got Unknown
			//IL_0020: Unknown result type (might be due to invalid IL or missing references)
			//IL_0026: Expected O, but got Unknown
			ConfigEntry<int> obj = entry;
			IntSliderOptions val = new IntSliderOptions();
			((BaseRangeOptions<int>)val).Min = min;
			((BaseRangeOptions<int>)val).Max = max;
			((BaseOptions)val).RequiresRestart = false;
			return (BaseConfigItem)new IntSliderConfigItem(obj, val);
		}

		public static BaseConfigItem ToFloatConfigItem<TMapped>(this WrappedConfigEntry<float, TMapped> entry, float min, float max, float step)
		{
			//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_0012: Expected O, but got Unknown
			//IL_0012: Unknown result type (might be due to invalid IL or missing references)
			//IL_0019: Expected O, but got Unknown
			//IL_0019: Unknown result type (might be due to invalid IL or missing references)
			//IL_0020: Unknown result type (might be due to invalid IL or missing references)
			//IL_002c: Expected O, but got Unknown
			//IL_0027: Unknown result type (might be due to invalid IL or missing references)
			//IL_002d: Expected O, but got Unknown
			ConfigEntry<float> obj = entry;
			FloatStepSliderOptions val = new FloatStepSliderOptions();
			((BaseRangeOptions<float>)val).Min = min;
			((BaseRangeOptions<float>)val).Max = max;
			val.Step = step;
			((BaseOptions)val).RequiresRestart = false;
			return (BaseConfigItem)new FloatStepSliderConfigItem(obj, val);
		}

		public static BaseConfigItem ToTextConfigItem<TMapped>(this WrappedConfigEntry<string, TMapped> entry)
		{
			//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_0012: Unknown result type (might be due to invalid IL or missing references)
			//IL_001e: Expected O, but got Unknown
			//IL_0019: Unknown result type (might be due to invalid IL or missing references)
			//IL_001f: Expected O, but got Unknown
			return (BaseConfigItem)new TextInputFieldConfigItem((ConfigEntry<string>)entry, new TextInputFieldOptions
			{
				CharacterLimit = 7,
				RequiresRestart = false
			});
		}
	}
}