Decompiled source of Better Flashlight v1.0.0

Mods/WoodStudios.BetterFlashlight.dll

Decompiled an hour ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BetterFlashlight;
using HarmonyLib;
using Il2CppInterop.Runtime.InteropTypes;
using Il2CppScheduleOne.AvatarFramework;
using Il2CppScheduleOne.AvatarFramework.Equipping;
using Il2CppScheduleOne.Equipping;
using Il2CppScheduleOne.PlayerScripts;
using Il2CppScheduleOne.Tools;
using MelonLoader;
using MelonLoader.Preferences;
using Microsoft.CodeAnalysis;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: MelonInfo(typeof(Core), "Better Flashlight", "1.0.0", "Wood Studios", null)]
[assembly: MelonGame("TVGS", "Schedule I")]
[assembly: AssemblyTitle("WoodStudios.BetterFlashlight")]
[assembly: TargetFramework(".NETCoreApp,Version=v6.0", FrameworkDisplayName = ".NET 6.0")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.0.0")]
[module: UnverifiableCode]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[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 BetterFlashlight
{
	public class Core : MelonMod
	{
		internal static Instance Log;

		internal static MelonPreferences_Entry<float> Brightness;

		internal static MelonPreferences_Entry<float> Reach;

		internal static MelonPreferences_Entry<float> Width;

		internal static MelonPreferences_Entry<bool> Enabled;

		public override void OnInitializeMelon()
		{
			Log = ((MelonBase)this).LoggerInstance;
			MelonPreferences_Category obj = MelonPreferences.CreateCategory("BetterFlashlight", "Better Flashlight");
			Enabled = obj.CreateEntry<bool>("Enabled", true, "Enabled", "Off = the game's normal flashlight.", false, false, (ValueValidator)null, (string)null);
			Brightness = obj.CreateEntry<float>("Brightness", 1.8f, "Brightness", "1 = the game's normal flashlight, 1.8 = default, up to 3.", false, false, (ValueValidator)null, (string)null);
			Reach = obj.CreateEntry<float>("Reach", 1.6f, "Reach", "How far the beam goes. 1 = normal, 1.6 = default, up to 2.5.", false, false, (ValueValidator)null, (string)null);
			Width = obj.CreateEntry<float>("Width", 1.35f, "Width", "How wide the beam is. 1 = normal, 1.35 = default, up to 1.8.", false, false, (ValueValidator)null, (string)null);
			Log.Msg("Better Flashlight ready.");
		}

		public override void OnPreferencesSaved()
		{
			Beam.ReapplyAll();
		}

		public override void OnPreferencesLoaded()
		{
			Beam.ReapplyAll();
		}
	}
	internal static class Beam
	{
		private sealed class Orig
		{
			public Light L;

			public float Intensity;

			public float Range;

			public float Angle;

			public float Inner;
		}

		private static readonly Dictionary<IntPtr, Orig> Lights = new Dictionary<IntPtr, Orig>();

		private static bool _logged;

		public static void Apply(Light l)
		{
			//IL_000a: 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_0018: Invalid comparison between Unknown and I4
			//IL_00bc: Unknown result type (might be due to invalid IL or missing references)
			//IL_01e8: Unknown result type (might be due to invalid IL or missing references)
			if ((Object)(object)l == (Object)null || ((int)l.type != 0 && (int)l.type != 2))
			{
				return;
			}
			if (!Lights.TryGetValue(((Il2CppObjectBase)l).Pointer, out var value) || (Object)(object)value.L == (Object)null)
			{
				value = new Orig
				{
					L = l,
					Intensity = l.intensity,
					Range = l.range,
					Angle = l.spotAngle,
					Inner = l.innerSpotAngle
				};
				Lights[((Il2CppObjectBase)l).Pointer] = value;
				if (!_logged)
				{
					_logged = true;
					Core.Log.Msg($"Flashlight beam found: {l.type}, intensity {value.Intensity:0.##}, range {value.Range:0.#}, angle {value.Angle:0.#}/{value.Inner:0.#}.");
				}
			}
			bool value2 = Core.Enabled.Value;
			float num = (value2 ? Mathf.Clamp(Core.Brightness.Value, 1f, 3f) : 1f);
			float num2 = (value2 ? Mathf.Clamp(Core.Reach.Value, 1f, 2.5f) : 1f);
			float num3 = (value2 ? Mathf.Clamp(Core.Width.Value, 1f, 1.8f) : 1f);
			l.intensity = value.Intensity * num;
			l.range = value.Range * num2;
			if ((int)l.type == 0)
			{
				l.spotAngle = Mathf.Min(value.Angle * num3, 110f);
				l.innerSpotAngle = Mathf.Min(value.Inner * num3, l.spotAngle - 2f);
			}
		}

		public static void ApplyAll(GameObject g)
		{
			if ((Object)(object)g == (Object)null)
			{
				return;
			}
			foreach (Light componentsInChild in g.GetComponentsInChildren<Light>(true))
			{
				Apply(componentsInChild);
			}
		}

		public static void ReapplyAll()
		{
			List<IntPtr> list = new List<IntPtr>();
			foreach (KeyValuePair<IntPtr, Orig> light in Lights)
			{
				if ((Object)(object)light.Value.L == (Object)null)
				{
					list.Add(light.Key);
					continue;
				}
				try
				{
					Apply(light.Value.L);
				}
				catch
				{
					list.Add(light.Key);
				}
			}
			foreach (IntPtr item in list)
			{
				Lights.Remove(item);
			}
		}

		public static string Info()
		{
			ReapplyAll();
			List<string> list = new List<string>();
			foreach (KeyValuePair<IntPtr, Orig> light in Lights)
			{
				Light l = light.Value.L;
				if (!((Object)(object)l == (Object)null))
				{
					list.Add($"{((Object)l).name}: {light.Value.Intensity:0.##}->{l.intensity:0.##}, {light.Value.Range:0.#}->{l.range:0.#} m, {light.Value.Angle:0.#}->{l.spotAngle:0.#} deg, on={((Behaviour)l).enabled && ((Component)l).gameObject.activeInHierarchy}");
				}
			}
			if (list.Count != 0)
			{
				return string.Join(" | ", list);
			}
			return "no flashlight beams yet";
		}
	}
	[HarmonyPatch(typeof(FlashlightAvatarEquippable), "Equip")]
	internal static class AvatarFlashlightPatch
	{
		private static void Postfix(FlashlightAvatarEquippable __instance, Avatar _avatar)
		{
			try
			{
				if (!((Object)(object)_avatar == (Object)null) && !((Object)(object)((Component)_avatar).GetComponentInParent<Player>() == (Object)null))
				{
					if ((Object)(object)__instance.Light != (Object)null && (Object)(object)__instance.Light._Light != (Object)null)
					{
						Beam.Apply(__instance.Light._Light);
					}
					Beam.ApplyAll(((Component)__instance).gameObject);
				}
			}
			catch (Exception ex)
			{
				Core.Log.Warning("Flashlight: " + ex.Message);
			}
		}
	}
	[HarmonyPatch(typeof(Equippable_Viewmodel), "Equip")]
	internal static class ViewmodelFlashlightPatch
	{
		private static void Postfix(Equippable_Viewmodel __instance)
		{
			try
			{
				if ((Object)(object)((Il2CppObjectBase)__instance).TryCast<Flashlight>() != (Object)null)
				{
					Beam.ApplyAll(((Component)__instance).gameObject);
				}
			}
			catch (Exception ex)
			{
				Core.Log.Warning("Flashlight: " + ex.Message);
			}
		}
	}
}