Decompiled source of PocketLightSimple v1.0.0

BepInEx/plugins/PocketLightSimple.dll

Decompiled 7 hours ago
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Logging;
using GameNetcodeStuff;
using HarmonyLib;
using LethalCompanyInputUtils.Api;
using Microsoft.CodeAnalysis;
using PocketLightSimple.Patches;
using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.Rendering.HighDefinition;

[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("PocketLightSimple")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0+1be8bdc94643edba81a650bf88e04ba82fca2f75")]
[assembly: AssemblyProduct("PocketLightSimple")]
[assembly: AssemblyTitle("PocketLightSimple")]
[assembly: AssemblyVersion("1.0.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 PocketLightSimple
{
	[BepInPlugin("rusty.PocketLightSimple", "PocketLightSimple", "1.0.0")]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	public class Plugin : BaseUnityPlugin
	{
		public const string ModGuid = "rusty.PocketLightSimple";

		public const string ModName = "PocketLightSimple";

		public const string ModVersion = "1.0.0";

		public static ManualLogSource Log;

		public static PocketKeybinds Keys;

		public static Light Light;

		private void Awake()
		{
			//IL_0048: Unknown result type (might be due to invalid IL or missing references)
			Log = ((BaseUnityPlugin)this).Logger;
			Keys = new PocketKeybinds();
			Keys.Toggle.performed += delegate
			{
				Toggle();
			};
			new Harmony("rusty.PocketLightSimple").PatchAll(typeof(PlayerSpawnPatch));
			Log.LogInfo((object)"PocketLightSimple loaded. A dim built-in flashlight, on by default, toggled with F.");
		}

		private static void Toggle()
		{
			if (!((Object)(object)Light == (Object)null))
			{
				PlayerControllerB val = GameNetworkManager.Instance?.localPlayerController;
				if (!((Object)(object)val == (Object)null) && !val.isPlayerDead && !val.isTypingChat && !val.inTerminalMenu && !val.quickMenuManager.isMenuOpen)
				{
					((Behaviour)Light).enabled = !((Behaviour)Light).enabled;
				}
			}
		}
	}
	public class PocketKeybinds : LcInputActions
	{
		[InputAction("<Keyboard>/f", Name = "Toggle Pocket Light")]
		public InputAction Toggle { get; set; }
	}
	internal class PocketLightSimpleFlicker : MonoBehaviour
	{
		private const int Smoothing = 4;

		private Light light;

		private HDAdditionalLightData? hd;

		private float baseIntensity;

		private readonly Queue<float> samples = new Queue<float>(4);

		private float sampleSum;

		private void Awake()
		{
			light = ((Component)this).GetComponent<Light>();
			hd = ((Component)this).GetComponent<HDAdditionalLightData>();
			baseIntensity = (((Object)(object)hd != (Object)null) ? hd.intensity : light.intensity);
			((MonoBehaviour)this).StartCoroutine(Loop());
		}

		private IEnumerator Loop()
		{
			while (true)
			{
				SetIntensity(baseIntensity);
				yield return (object)new WaitForSeconds(Random.Range(4f, 10f));
				if (!((Behaviour)light).enabled)
				{
					continue;
				}
				int flickers = Random.Range(2, 5);
				for (int i = 0; i < flickers; i++)
				{
					if (!((Behaviour)light).enabled)
					{
						break;
					}
					yield return Dip();
					SetIntensity(baseIntensity);
					yield return (object)new WaitForSeconds(Random.Range(0.03f, 0.1f));
				}
				SetIntensity(baseIntensity);
			}
		}

		private IEnumerator Dip()
		{
			samples.Clear();
			sampleSum = 0f;
			float floor = baseIntensity * Random.Range(0.05f, 0.4f);
			float duration = Random.Range(0.06f, 0.16f);
			for (float t = 0f; t < duration; t += Time.deltaTime)
			{
				if (!((Behaviour)light).enabled)
				{
					break;
				}
				while (samples.Count >= 4)
				{
					sampleSum -= samples.Dequeue();
				}
				float num = Random.Range(floor, baseIntensity);
				samples.Enqueue(num);
				sampleSum += num;
				SetIntensity(sampleSum / (float)samples.Count);
				yield return null;
			}
		}

		private void SetIntensity(float value)
		{
			if ((Object)(object)hd != (Object)null)
			{
				hd.intensity = value;
			}
			else
			{
				light.intensity = value;
			}
		}
	}
}
namespace PocketLightSimple.Patches
{
	[HarmonyPatch(typeof(PlayerControllerB), "ConnectClientToPlayerObject")]
	internal static class PlayerSpawnPatch
	{
		[HarmonyPostfix]
		private static void Postfix(PlayerControllerB __instance)
		{
			//IL_0091: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a1: 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)
			if ((Object)(object)__instance != (Object)(object)GameNetworkManager.Instance.localPlayerController)
			{
				return;
			}
			if ((Object)(object)Plugin.Light != (Object)null)
			{
				Object.Destroy((Object)(object)((Component)Plugin.Light).gameObject);
			}
			Light val = FindProFlashlightBulb();
			if ((Object)(object)val == (Object)null)
			{
				Plugin.Log.LogWarning((object)"Pro-Flashlight bulb not found; PocketLightSimple will not spawn.");
				return;
			}
			GameObject val2 = Object.Instantiate<GameObject>(((Component)val).gameObject);
			((Object)val2).name = "PocketLightSimple";
			val2.transform.SetParent(((Component)__instance.gameplayCamera).transform, false);
			val2.transform.localPosition = new Vector3(0f, 0f, 0.5f);
			val2.transform.localRotation = Quaternion.identity;
			val2.transform.localScale = Vector3.one;
			Light component = val2.GetComponent<Light>();
			component.range *= 0.5f;
			component.cookie = null;
			component.spotAngle = 60f;
			HDAdditionalLightData component2 = val2.GetComponent<HDAdditionalLightData>();
			if ((Object)(object)component2 != (Object)null)
			{
				component2.intensity *= 0.175f;
				component2.SetSpotAngle(60f, 90f);
			}
			((Behaviour)component).enabled = true;
			val2.SetActive(true);
			val2.AddComponent<PocketLightSimpleFlicker>();
			Plugin.Light = component;
		}

		private static Light? FindProFlashlightBulb()
		{
			Terminal val = Object.FindObjectOfType<Terminal>();
			if ((Object)(object)val == (Object)null)
			{
				return null;
			}
			Item[] buyableItemsList = val.buyableItemsList;
			foreach (Item val2 in buyableItemsList)
			{
				if (!(((Object)val2).name != "ProFlashlight"))
				{
					return val2.spawnPrefab.GetComponent<FlashlightItem>().flashlightBulb;
				}
			}
			return null;
		}
	}
}