Decompiled source of GuodaLight v0.2.2

Tomatobird.GuodaLight.dll

Decompiled a month ago
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Logging;
using GameNetcodeStuff;
using GuodaLight.Utils;
using HarmonyLib;
using LethalCompanyInputUtils.Api;
using Microsoft.CodeAnalysis;
using Unity.Netcode;
using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.Rendering;
using UnityEngine.Rendering.HighDefinition;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("Tomatobird.GuodaLight")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("0.2.2.0")]
[assembly: AssemblyInformationalVersion("0.2.2+85e43ce2222a84b2fb53882a6d63458f31df8728")]
[assembly: AssemblyProduct("GuodaLight")]
[assembly: AssemblyTitle("Tomatobird.GuodaLight")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("0.2.2.0")]
[module: UnverifiableCode]
[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;
		}
	}
}
[HarmonyPatch(typeof(NetworkManager))]
internal static class RegisterNetworkPrefabPatch
{
	private static readonly string MOD_GUID = "Tomatobird.GuodaLight";

	[HarmonyPatch("SetSingleton")]
	[HarmonyPostfix]
	private static void RegisterPrefab()
	{
		//IL_0010: Unknown result type (might be due to invalid IL or missing references)
		//IL_0016: Expected O, but got Unknown
		//IL_0018: Unknown result type (might be due to invalid IL or missing references)
		//IL_001f: Unknown result type (might be due to invalid IL or missing references)
		GameObject val = new GameObject(MOD_GUID + " Prefab");
		((Object)val).hideFlags = (HideFlags)(((Object)val).hideFlags | 0x3D);
		Object.DontDestroyOnLoad((Object)(object)val);
		NetworkObject obj = val.AddComponent<NetworkObject>();
		FieldInfo field = typeof(NetworkObject).GetField("GlobalObjectIdHash", BindingFlags.Instance | BindingFlags.NonPublic);
		field.SetValue(obj, GetHash(MOD_GUID));
		NetworkManager.Singleton.PrefabHandler.AddNetworkPrefab(val);
		static uint GetHash(string value)
		{
			return value?.Aggregate(17u, (uint current, char c) => (current * 31) ^ c) ?? 0;
		}
	}
}
namespace GuodaLight
{
	[BepInPlugin("Tomatobird.GuodaLight", "GuodaLight", "0.2.2")]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	public class GuodaLight : BaseUnityPlugin
	{
		internal static LightKey InputActionInstance = new LightKey();

		public static float lightIntensity = 486.8536f;

		public static float lightRange = 55f;

		public static float lightColorTemperature = 8371f;

		public static bool lightOnByDefault = true;

		public static bool silentLight = false;

		public static float clickVolume = 0.5f;

		public static GuodaLight Instance { get; private set; } = null;

		internal static ManualLogSource Logger { get; private set; } = null;

		internal static Harmony? Harmony { get; set; }

		private void Awake()
		{
			Logger = ((BaseUnityPlugin)this).Logger;
			Instance = this;
			lightIntensity = ((BaseUnityPlugin)this).Config.Bind<float>("General", "LightIntensity", 486.8536f, "Intensity of the light").Value;
			lightRange = ((BaseUnityPlugin)this).Config.Bind<float>("General", "LightRange", 55f, "Light reach distance").Value;
			lightColorTemperature = ((BaseUnityPlugin)this).Config.Bind<float>("General", "lightColorTemperature", 8371f, "Color temperature of the light").Value;
			lightOnByDefault = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "lightOnByDefault", true, "Should the light default to being on when player joins?").Value;
			silentLight = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "SilentLight", false, "Should toggling the light be silent?").Value;
			clickVolume = ((BaseUnityPlugin)this).Config.Bind<float>("General", "ClickVolume", 0.5f, "Audio volume of the flashlight toggle click.").Value;
			Patch();
			Logger.LogInfo((object)"Tomatobird.GuodaLight v0.2.2 has loaded!");
		}

		internal static void Patch()
		{
			//IL_000d: 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: Expected O, but got Unknown
			if (Harmony == null)
			{
				Harmony = new Harmony("Tomatobird.GuodaLight");
			}
			Logger.LogDebug((object)"Patching...");
			Harmony.PatchAll();
			Logger.LogDebug((object)"Finished patching!");
		}

		internal static void Unpatch()
		{
			Logger.LogDebug((object)"Unpatching...");
			Harmony? harmony = Harmony;
			if (harmony != null)
			{
				harmony.UnpatchSelf();
			}
			Logger.LogDebug((object)"Finished unpatching!");
		}
	}
	public class LightKey : LcInputActions
	{
		[InputAction(/*Could not decode attribute arguments.*/)]
		public InputAction? LightButton { get; set; }
	}
	public static class MyPluginInfo
	{
		public const string PLUGIN_GUID = "Tomatobird.GuodaLight";

		public const string PLUGIN_NAME = "GuodaLight";

		public const string PLUGIN_VERSION = "0.2.2";
	}
}
namespace GuodaLight.Utils
{
	internal class FlashKeyPressListener : MonoBehaviour
	{
		internal List<AudioClip>? flashlightClips;

		private bool lightState = GuodaLight.lightOnByDefault;

		private Light? light;

		private AudioSource? audio;

		private HDAdditionalLightData? lightdata;

		public void Awake()
		{
			if (GuodaLight.InputActionInstance.LightButton != null)
			{
				GuodaLight.InputActionInstance.LightButton.performed += OnActionPerformed;
				light = ((Component)this).GetComponent<Light>();
				if ((Object)(object)light != (Object)null)
				{
					Texture cookie = StartOfRound.Instance.allPlayerScripts[0].allHelmetLights[0].cookie;
					light.cookie = cookie;
				}
				((MonoBehaviour)this).StartCoroutine(WaitForComponents());
			}
		}

		private IEnumerator WaitForComponents()
		{
			yield return (object)new WaitForEndOfFrame();
			yield return (object)new WaitUntil((Func<bool>)(() => lightState));
			int iteration = 0;
			float interval = 1f;
			float current = 0.2f;
			bool allFound = false;
			for (; iteration < 10; iteration++)
			{
				if (allFound)
				{
					break;
				}
				while (current > 0f)
				{
					current -= Time.deltaTime;
					yield return (object)new WaitForEndOfFrame();
				}
				current = interval;
				if ((Object)(object)light == (Object)null)
				{
					light = ((Component)this).GetComponent<Light>();
					if ((Object)(object)light != (Object)null)
					{
						GuodaLight.Logger.LogDebug((object)"Got component light");
						Texture cookieTexture = StartOfRound.Instance.allPlayerScripts[0].allHelmetLights[0].cookie;
						light.cookie = cookieTexture;
					}
				}
				if ((Object)(object)audio == (Object)null)
				{
					audio = ((Component)this).GetComponent<AudioSource>();
					if ((Object)(object)audio != (Object)null)
					{
						GuodaLight.Logger.LogDebug((object)"Got component audio");
					}
				}
				if ((Object)(object)lightdata == (Object)null)
				{
					lightdata = ((Component)this).GetComponent<HDAdditionalLightData>();
					if ((Object)(object)lightdata != (Object)null)
					{
						GuodaLight.Logger.LogDebug((object)"Got component lightdata");
						lightdata.fadeDistance = 1000f;
						lightdata.shadowFadeDistance = 1000f;
						lightdata.shadowNearPlane = 0.66f;
					}
				}
				allFound = (Object)(object)light != (Object)null && (Object)(object)audio != (Object)null && (Object)(object)lightdata != (Object)null;
			}
			yield return (object)new WaitForEndOfFrame();
			GuodaLight.Logger.LogDebug((object)$"All components were found: {allFound}. Iterations: {iteration}");
		}

		public void OnActionPerformed(CallbackContext context)
		{
			if (!((Object)(object)light == (Object)null) && Application.isFocused && !((Object)(object)GameNetworkManager.Instance?.localPlayerController == (Object)null) && !GameNetworkManager.Instance.localPlayerController.isPlayerDead && !GameNetworkManager.Instance.localPlayerController.isTypingChat && !GameNetworkManager.Instance.localPlayerController.inTerminalMenu && !GameNetworkManager.Instance.localPlayerController.quickMenuManager.isMenuOpen)
			{
				((Behaviour)light).enabled = !((Behaviour)light).enabled;
				lightState = ((Behaviour)light).enabled;
				if (!((Object)(object)audio == (Object)null) && flashlightClips != null)
				{
					audio.PlayOneShot(((Behaviour)light).enabled ? flashlightClips[0] : flashlightClips[1]);
				}
			}
		}
	}
}
namespace GuodaLight.Patches
{
	[HarmonyPatch(typeof(PlayerControllerB))]
	public class AddLightObject
	{
		[HarmonyPatch("ConnectClientToPlayerObject")]
		[HarmonyPostfix]
		public static void AddLightObjectPatch(PlayerControllerB __instance)
		{
			//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_0013: Expected O, but got Unknown
			//IL_003b: Unknown result type (might be due to invalid IL or missing references)
			//IL_014e: Unknown result type (might be due to invalid IL or missing references)
			//IL_016c: Unknown result type (might be due to invalid IL or missing references)
			//IL_017e: Unknown result type (might be due to invalid IL or missing references)
			GameObject val = new GameObject
			{
				name = "GuodaLight"
			};
			Light val2 = val.AddComponent<Light>();
			((Behaviour)val2).enabled = GuodaLight.lightOnByDefault;
			val2.boundingSphereOverride = new Vector4(0f, 0f, 0f, 0f);
			val2.bounceIntensity = 0f;
			val2.colorTemperature = GuodaLight.lightColorTemperature;
			val2.cookieSize = 10f;
			val2.cullingMask = -1;
			val2.innerSpotAngle = 21.8021f;
			val2.intensity = GuodaLight.lightIntensity;
			val2.lightShadowCasterMode = (LightShadowCasterMode)2;
			val2.range = GuodaLight.lightRange;
			val2.renderingLayerMask = 1;
			val2.shadowBias = 0.05f;
			val2.shadowNearPlane = 0.2f;
			val2.shadowNormalBias = 0.4f;
			val2.shadowResolution = (LightShadowResolution)(-1);
			val2.shadows = (LightShadows)1;
			val2.shadowStrength = 1f;
			val2.shape = (LightShape)0;
			val2.spotAngle = 73f;
			val2.type = (LightType)0;
			val2.useBoundingSphereOverride = false;
			val2.useColorTemperature = true;
			val2.useShadowMatrixOverride = false;
			val2.useViewFrustumForShadowCasterCull = true;
			val.transform.SetParent(((Component)__instance.gameplayCamera).transform);
			val.transform.localScale = new Vector3(0.3627f, 0.4234f, 0.4918f);
			val.transform.SetPositionAndRotation(((Component)__instance.allHelmetLights[0]).transform.position, ((Component)__instance.allHelmetLights[0]).transform.rotation);
			FlashKeyPressListener flashKeyPressListener = val.AddComponent<FlashKeyPressListener>();
			if (GuodaLight.silentLight)
			{
				return;
			}
			AudioSource val3 = val.AddComponent<AudioSource>();
			val3.dopplerLevel = 0.3f;
			val3.maxDistance = 18f;
			val3.minDistance = 4f;
			val3.playOnAwake = false;
			val3.loop = false;
			val3.rolloffMode = (AudioRolloffMode)1;
			val3.volume = GuodaLight.clickVolume;
			val3.priority = 128;
			val3.spread = 71f;
			val3.spatialBlend = 1f;
			val3.outputAudioMixerGroup = SoundManager.Instance.ambienceAudio.outputAudioMixerGroup;
			Item[] buyableItemsList = HUDManager.Instance.terminalScript.buyableItemsList;
			foreach (Item val4 in buyableItemsList)
			{
				if (((Object)val4).name == "ProFlashlight")
				{
					flashKeyPressListener.flashlightClips = val4.spawnPrefab.GetComponent<FlashlightItem>().flashlightClips.ToList();
					break;
				}
			}
		}
	}
}