Decompiled source of BetterLanternsAndLights v1.0.0

BetterLanternsAndLights/BetterLanternsAndLights.dll

Decompiled 4 hours 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.InteropServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Bootstrap;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using UnityEngine;
using UnityEngine.SceneManagement;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: AssemblyTitle("BetterLanternsAndLights")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("https://github.com/bryon82/SailwindBetterLanternsAndLights")]
[assembly: AssemblyCompany("raddude")]
[assembly: AssemblyProduct("BetterLanternsAndLights")]
[assembly: AssemblyCopyright("Copyright ©  2026")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("609d6bc4-2d2c-4fb9-98b4-b62976cadb47")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.0.0")]
[module: UnverifiableCode]
namespace BetterLanternsAndLights;

[BepInPlugin("com.raddude.betterlanternsandlights", "Better Lanterns and Lights", "1.0.0")]
[BepInDependency(/*Could not decode attribute arguments.*/)]
[BepInDependency(/*Could not decode attribute arguments.*/)]
public class BLL_Plugin : BaseUnityPlugin
{
	public const string PLUGIN_GUID = "com.raddude.betterlanternsandlights";

	public const string PLUGIN_NAME = "Better Lanterns and Lights";

	public const string PLUGIN_VERSION = "1.0.0";

	public const string DYNAMIC_LIGHTS_GUID = "com.nandbrew.dynamiclights";

	public const string BETTER_NPC_BOATS_GUID = "com.raddude.betternpcboats";

	internal const string DRAGON_CLIFFS_SCENE = "island 9 E Dragon Cliffs";

	private static ManualLogSource _logger;

	public static Light PlayerLight { get; internal set; }

	internal static BLL_Plugin Instance { get; private set; }

	internal static void LogDebug(string message)
	{
		_logger.LogDebug((object)message);
	}

	internal static void LogInfo(string message)
	{
		_logger.LogInfo((object)message);
	}

	internal static void LogWarning(string message)
	{
		_logger.LogWarning((object)message);
	}

	internal static void LogError(string message)
	{
		_logger.LogError((object)message);
	}

	private void Awake()
	{
		if ((Object)(object)Instance != (Object)null && (Object)(object)Instance != (Object)(object)this)
		{
			Object.Destroy((Object)(object)((Component)this).gameObject);
			return;
		}
		Instance = this;
		_logger = ((BaseUnityPlugin)this).Logger;
		Configs.InitializeConfigs();
		Harmony harmony = Harmony.CreateAndPatchAll(Assembly.GetExecutingAssembly(), "com.raddude.betterlanternsandlights");
		bool flag = false;
		bool flag2 = false;
		foreach (PluginInfo value in Chainloader.PluginInfos.Values)
		{
			if (value.Metadata.GUID == "com.nandbrew.dynamiclights")
			{
				LogInfo("Enabling compatibility with Dynamic Lights.");
				DynamicLights.ApplyPatch(harmony);
				flag = true;
			}
			else if (value.Metadata.GUID == "com.raddude.betternpcboats")
			{
				LogInfo("Enabling compatibility with Better NPC Boats.");
				BetterNPCBoats.ApplyPatch(harmony);
				flag2 = true;
			}
			if (flag && flag2)
			{
				break;
			}
		}
		SceneManager.sceneLoaded += SceneLoaded;
	}

	private void Update()
	{
		Configs.UpdateConfigs();
	}

	private void SceneLoaded(Scene scene, LoadSceneMode _)
	{
		if (((Scene)(ref scene)).name == "island 9 E Dragon Cliffs")
		{
			DragonCliffs.Initialize();
		}
	}
}
internal class BetterNPCBoats
{
	public static void ApplyPatch(Harmony harmony)
	{
		//IL_002d: Unknown result type (might be due to invalid IL or missing references)
		//IL_003a: Expected O, but got Unknown
		Type type = AccessTools.TypeByName("BetterNPCBoats.BetterNPCFishingBoat");
		MethodInfo methodInfo = AccessTools.Method(type, "ToggleLanterns", (Type[])null, (Type[])null);
		harmony.Patch((MethodBase)methodInfo, (HarmonyMethod)null, new HarmonyMethod(typeof(BetterNPCBoats), "ToggleLanternsPostfix", (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
	}

	private static void ToggleLanternsPostfix(object __instance, bool state)
	{
		//IL_0002: Unknown result type (might be due to invalid IL or missing references)
		//IL_0008: Expected O, but got Unknown
		Component val = (Component)__instance;
		LanternFlicker component = val.GetComponent<LanternFlicker>();
		if (!((Object)(object)component == (Object)null))
		{
			component.SetFlicker(state);
		}
	}
}
internal class DynamicLights
{
	public static void ApplyPatch(Harmony harmony)
	{
		//IL_002d: Unknown result type (might be due to invalid IL or missing references)
		//IL_003a: Expected O, but got Unknown
		Type type = AccessTools.TypeByName("Dynamic_Lights.IslandStreetlightFire");
		MethodInfo methodInfo = AccessTools.Method(type, "SetLight", (Type[])null, (Type[])null);
		harmony.Patch((MethodBase)methodInfo, (HarmonyMethod)null, new HarmonyMethod(typeof(DynamicLights), "SetLightPostfix", (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
	}

	private static void SetLightPostfix(object __instance, bool newState)
	{
		//IL_0002: Unknown result type (might be due to invalid IL or missing references)
		//IL_0008: Expected O, but got Unknown
		Component val = (Component)__instance;
		LanternFlicker component = val.GetComponent<LanternFlicker>();
		if (!((Object)(object)component == (Object)null))
		{
			component.SetFlicker(newState);
		}
	}
}
internal class LanternPatches
{
	[HarmonyPatch(typeof(PrefabsDirectory), "PopulateShipItems")]
	internal class AddPaperLanternFlickerPatch
	{
		public static void Postfix(PrefabsDirectory __instance)
		{
			int[] array = new int[7] { 110, 111, 112, 113, 114, 133, 134 };
			int[] array2 = array;
			foreach (int num in array2)
			{
				GameObject val = __instance.directory[num];
				val.AddComponent<LanternFlicker>();
			}
		}
	}

	[HarmonyPatch(typeof(ShipItemLight), "SetLight")]
	internal class ShipItemLightSetLightPatch
	{
		public static void Postfix(ShipItemLight __instance, bool state)
		{
			LanternFlicker component = ((Component)__instance).GetComponent<LanternFlicker>();
			if (!((Object)(object)component == (Object)null))
			{
				component.SetFlicker(state);
			}
		}
	}

	[HarmonyPatch(typeof(GPButtonInventorySlot), "Awake")]
	public class AddInventoryLantern
	{
		public static void Postfix(GPButtonInventorySlot __instance)
		{
			InventoryLantern inventoryLantern = ((Component)__instance).gameObject.AddComponent<InventoryLantern>();
			inventoryLantern.InventorySlot = __instance;
		}
	}
}
internal class PlayerLightPatches
{
	[HarmonyPatch(typeof(PlayerControllerMirror), "Awake")]
	internal class PlayerControllerMirrorAwakePatch
	{
		public static void Postfix()
		{
			Camera main = Camera.main;
			BLL_Plugin.PlayerLight = ((main != null) ? ((Component)main).GetComponent<Light>() : null);
		}
	}
}
internal class StreetLightPatches
{
	[HarmonyPatch(typeof(IslandStreetlight))]
	internal class IslandStreetlightPatches
	{
		[HarmonyPostfix]
		[HarmonyPatch("Awake")]
		public static void AddFlicker(IslandStreetlight __instance, Light ___light)
		{
			if (!(((Object)___light).name == "senna_lantern_light"))
			{
				((Component)__instance).gameObject.AddComponent<LanternFlicker>();
			}
		}

		[HarmonyPostfix]
		[HarmonyPatch("SetLight")]
		public static void AddFlickerToLight(IslandStreetlight __instance, bool newState)
		{
			LanternFlicker component = ((Component)__instance).GetComponent<LanternFlicker>();
			if (!((Object)(object)component == (Object)null))
			{
				component.SetFlicker(newState);
			}
		}
	}
}
internal class DragonCliffs
{
	internal static List<GameObject> DCGateLampGOs { get; private set; }

	internal 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_003e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0043: Unknown result type (might be due to invalid IL or missing references)
		//IL_00cc: Unknown result type (might be due to invalid IL or missing references)
		//IL_00d1: Unknown result type (might be due to invalid IL or missing references)
		//IL_00f8: Unknown result type (might be due to invalid IL or missing references)
		//IL_010a: Unknown result type (might be due to invalid IL or missing references)
		//IL_0128: Unknown result type (might be due to invalid IL or missing references)
		DCGateLampGOs = new List<GameObject>();
		Vector3[] array = (Vector3[])(object)new Vector3[2]
		{
			new Vector3(0f, 3.35f, 3.1f),
			new Vector3(0f, -3.35f, 3.1f)
		};
		GameObject val = GameObject.Find("island 9 E (dragon cliffs) scenery");
		Transform val2 = ((IEnumerable<Transform>)((Component)val.transform).GetComponentsInChildren<Transform>(true)).FirstOrDefault((Func<Transform, bool>)((Transform t) => ((Object)t).name.Equals("east_gate (4)")));
		Transform child = ((IEnumerable<Transform>)((Component)val.transform).GetComponentsInChildren<Transform>(true)).FirstOrDefault((Func<Transform, bool>)((Transform t) => ((Object)t).name.Equals("east_street_rope (1)"))).GetChild(0);
		Vector3[] array2 = array;
		foreach (Vector3 localPosition in array2)
		{
			GameObject val3 = Object.Instantiate<GameObject>(((Component)child).gameObject, val2);
			val3.transform.localScale = new Vector3(1.33f, 1.33f, 1.33f);
			val3.transform.localPosition = localPosition;
			val3.transform.rotation = Quaternion.Euler(270f, 0f, 0f);
			val3.GetComponent<Light>().range = 50f;
			val3.GetComponent<Light>().intensity = 1.5f;
			DCGateLampGOs.Add(val3);
		}
		BLL_Plugin.LogDebug("Dragon Cliffs gate lamps added");
	}
}
internal class InventoryLantern : MonoBehaviour
{
	internal GPButtonInventorySlot InventorySlot { get; set; }

	private void LateUpdate()
	{
		if (Configs.enableInventoryLantern.Value && ((GoPointerButton)InventorySlot).IsLookedAt())
		{
			ShipItem currentItem = InventorySlot.currentItem;
			if (currentItem is ShipItemLight && (GameInput.GetKeyDown((InputName)9) || Input.GetMouseButtonDown(1)))
			{
				((GoPointerButton)currentItem).OnAltActivate();
			}
		}
	}
}
internal class LanternFlicker : MonoBehaviour
{
	private Coroutine flickerCoroutine;

	internal Material paperOnMat;

	internal Material paperOffMat;

	public Light light;

	public Renderer paperRenderer;

	private float minIntensity = 1f;

	private float maxIntensity = 1.5f;

	private float minEmission = 0.5f;

	private float maxEmission = 0.8f;

	private void Awake()
	{
		light = ((Component)this).GetComponentInChildren<Light>();
		ShipItemLight component = ((Component)this).GetComponent<ShipItemLight>();
		if ((Object)(object)component != (Object)null)
		{
			paperRenderer = component.GetPrivateField<Renderer>("paperRenderer");
			paperOffMat = component.GetPrivateField<Material>("paperOffMat");
			Renderer obj = paperRenderer;
			paperOnMat = ((obj != null) ? obj.sharedMaterial : null);
		}
		IslandStreetlight component2 = ((Component)this).GetComponent<IslandStreetlight>();
		if ((Object)(object)component2 != (Object)null)
		{
			paperRenderer = component2.GetPrivateField<Renderer>("renderer");
			paperOffMat = component2.GetPrivateField<Material>("offMat");
			Renderer obj2 = paperRenderer;
			paperOnMat = ((obj2 != null) ? obj2.sharedMaterial : null);
		}
	}

	internal void SetFlicker(bool enable)
	{
		if (Configs.enableLanternFlicker.Value && enable && flickerCoroutine == null)
		{
			flickerCoroutine = ((MonoBehaviour)this).StartCoroutine(Flicker());
		}
		else if (!enable && flickerCoroutine != null)
		{
			((MonoBehaviour)this).StopCoroutine(flickerCoroutine);
			flickerCoroutine = null;
		}
	}

	private IEnumerator Flicker()
	{
		float slowTime = Random.value * 100f;
		float fastTime = Random.value * 100f;
		while (true)
		{
			slowTime += Time.deltaTime * 0.5f;
			fastTime += Time.deltaTime * 6f;
			float slow = Mathf.PerlinNoise(slowTime, 0f);
			float fast = Mathf.PerlinNoise(fastTime, 100f);
			float flicker = slow * 0.8f + fast * 0.2f;
			flicker = Mathf.Clamp01(flicker);
			light.intensity = Mathf.Lerp(minIntensity, maxIntensity, flicker);
			if ((Object)(object)paperOffMat == (Object)null)
			{
				yield return null;
				continue;
			}
			float materialAmount = Mathf.Lerp(0.9f, 1f, flicker);
			paperRenderer.material.Lerp(paperOffMat, paperOnMat, materialAmount);
			float emissionStrength = Mathf.Lerp(minEmission, maxEmission, flicker);
			Color baseEmission = paperOnMat.GetColor("_EmissionColor");
			paperRenderer.material.SetColor("_EmissionColor", baseEmission * emissionStrength);
			yield return null;
		}
	}
}
internal class Configs
{
	internal static ConfigEntry<bool> enableLanternFlicker;

	internal static ConfigEntry<bool> addDcGateLamps;

	internal static ConfigEntry<bool> enablePlayerLight;

	internal static ConfigEntry<bool> enableInventoryLantern;

	private static bool _lastAddDcGateLamps;

	internal static void InitializeConfigs()
	{
		ConfigFile config = ((BaseUnityPlugin)BLL_Plugin.Instance).Config;
		enableLanternFlicker = config.Bind<bool>("Settings", "Enable lantern flicker", true, (ConfigDescription)null);
		addDcGateLamps = config.Bind<bool>("Settings", "Add Dragon Cliffs Gate Lamps", true, (ConfigDescription)null);
		enablePlayerLight = config.Bind<bool>("Settings", "Ambient Player Light", false, "Light emanating from the player.");
		enableInventoryLantern = config.Bind<bool>("Settings", "Activate Lantern In Inventory", true, "The ability to activate the lantern in an inventory slot by right-clicking it (or using other activation button).");
		_lastAddDcGateLamps = addDcGateLamps.Value;
	}

	internal static void UpdateConfigs()
	{
		//IL_0039: Unknown result type (might be due to invalid IL or missing references)
		//IL_003e: Unknown result type (might be due to invalid IL or missing references)
		bool currentAddDcGateLamps = addDcGateLamps.Value;
		if (currentAddDcGateLamps != _lastAddDcGateLamps && DragonCliffs.DCGateLampGOs != null)
		{
			Scene sceneByName = SceneManager.GetSceneByName("island 9 E Dragon Cliffs");
			if (((Scene)(ref sceneByName)).isLoaded)
			{
				DragonCliffs.DCGateLampGOs.ForEach(delegate(GameObject lamp)
				{
					if (lamp != null)
					{
						lamp.SetActive(currentAddDcGateLamps);
					}
				});
			}
			_lastAddDcGateLamps = currentAddDcGateLamps;
		}
		if ((Object)(object)BLL_Plugin.PlayerLight != (Object)null && ((Behaviour)BLL_Plugin.PlayerLight).enabled != enablePlayerLight.Value)
		{
			((Behaviour)BLL_Plugin.PlayerLight).enabled = enablePlayerLight.Value;
		}
	}
}
internal static class Extensions
{
	public static T GetPrivateField<T>(this object obj, string field)
	{
		return (T)Traverse.Create(obj).Field(field).GetValue();
	}
}