Decompiled source of DARKER levels v2.5.0

sonic.dll

Decompiled 4 days ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using BepInEx;
using ExitGames.Client.Photon;
using HarmonyLib;
using Photon.Pun;
using Photon.Realtime;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: AssemblyTitle("sonic")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("sonic")]
[assembly: AssemblyCopyright("Copyright ©  2026")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("98f6da0c-f28e-4d99-a81d-92f0e99ae946")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: AssemblyVersion("1.0.0.0")]
[BepInPlugin("com.feralelf.darkness", "LethalDarkness", "3.6.1")]
public class DarkLevelPlugin : BaseUnityPlugin
{
	private static float DarknessIntensity = 0.5f;

	private bool showUI = false;

	private Rect windowRect = new Rect(20f, 20f, 250f, 150f);

	private static bool isMenu = false;

	private static bool levelScanned = false;

	private static Dictionary<Light, float> originalLightIntensities = new Dictionary<Light, float>();

	private void Awake()
	{
		LoadConfig();
		Harmony.CreateAndPatchAll(typeof(DarkLevelPlugin), (string)null);
	}

	private void Update()
	{
		//IL_00ae: Unknown result type (might be due to invalid IL or missing references)
		//IL_00ba: Unknown result type (might be due to invalid IL or missing references)
		//IL_00c0: Invalid comparison between Unknown and I4
		if ((SemiFunc.IsMasterClient() || !PhotonNetwork.InRoom) && Input.GetKeyDown((KeyCode)282))
		{
			showUI = !showUI;
		}
		if (PhotonNetwork.InRoom && !PhotonNetwork.IsMasterClient)
		{
			Room currentRoom = PhotonNetwork.CurrentRoom;
			if (currentRoom != null && ((Dictionary<object, object>)(object)((RoomInfo)currentRoom).CustomProperties).TryGetValue((object)"LethalDarkness", out object value))
			{
				DarknessIntensity = (float)value;
			}
		}
		try
		{
			if (!((Object)(object)LevelGenerator.Instance != (Object)null))
			{
				return;
			}
			isMenu = CheckIfMenuRoom();
			if (isMenu || (int)LevelGenerator.Instance.State == 0 || (int)LevelGenerator.Instance.State == 1)
			{
				if (levelScanned)
				{
					levelScanned = false;
					originalLightIntensities.Clear();
				}
				return;
			}
			if (!levelScanned)
			{
				PerformInitialScan();
				levelScanned = true;
			}
			UpdateCameraRenderDistance();
			ApplyLethalDarkness();
		}
		catch
		{
		}
	}

	private void OnGUI()
	{
		//IL_0028: Unknown result type (might be due to invalid IL or missing references)
		//IL_0034: Unknown result type (might be due to invalid IL or missing references)
		//IL_0043: Expected O, but got Unknown
		//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)
		if (!showUI || (PhotonNetwork.InRoom && !PhotonNetwork.IsMasterClient))
		{
			return;
		}
		windowRect = GUI.Window(99, windowRect, (WindowFunction)delegate
		{
			//IL_006b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0071: Expected O, but got Unknown
			GUILayout.Label($"Darkness Intensity: {DarknessIntensity:F2}", Array.Empty<GUILayoutOption>());
			float num = GUILayout.HorizontalSlider(DarknessIntensity, 0f, 1f, Array.Empty<GUILayoutOption>());
			if (num != DarknessIntensity)
			{
				DarknessIntensity = num;
				SaveConfig();
				if (PhotonNetwork.InRoom && PhotonNetwork.IsMasterClient)
				{
					Hashtable val = new Hashtable();
					((Dictionary<object, object>)(object)val).Add((object)"LethalDarkness", (object)DarknessIntensity);
					PhotonNetwork.CurrentRoom.SetCustomProperties(val, (Hashtable)null, (WebFlags)null);
				}
			}
			if (GUILayout.Button("Close", Array.Empty<GUILayoutOption>()))
			{
				showUI = false;
			}
			GUI.DragWindow();
		}, "Lethal Darkness Control");
	}

	private bool CheckIfMenuRoom()
	{
		//IL_004b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0052: Expected O, but got Unknown
		if (SemiFunc.MenuLevel())
		{
			return true;
		}
		if ((Object)(object)LevelGenerator.Instance?.LevelParent != (Object)null)
		{
			foreach (Transform item in LevelGenerator.Instance.LevelParent.transform)
			{
				Transform val = item;
				string text = ((Object)val).name.ToLower();
				if (text.Contains("menu") || text.Contains("lobby"))
				{
					return true;
				}
			}
		}
		return false;
	}

	private void PerformInitialScan()
	{
		originalLightIntensities.Clear();
		Light[] array = Object.FindObjectsOfType<Light>();
		Light[] array2 = array;
		foreach (Light val in array2)
		{
			if (((Behaviour)val).enabled && ((Component)val).gameObject.activeInHierarchy)
			{
				originalLightIntensities.Add(val, val.intensity);
			}
		}
	}

	private void UpdateCameraRenderDistance()
	{
		if ((Object)(object)Camera.main != (Object)null)
		{
			Camera.main.farClipPlane = Mathf.Lerp(2000f, 30f, DarknessIntensity);
		}
	}

	private static void ApplyLethalDarkness()
	{
		//IL_003a: Unknown result type (might be due to invalid IL or missing references)
		float reflectionIntensity = (RenderSettings.ambientIntensity = 1f - DarknessIntensity);
		RenderSettings.reflectionIntensity = reflectionIntensity;
		if (DarknessIntensity > 0.01f)
		{
			RenderSettings.fog = true;
			RenderSettings.fogMode = (FogMode)3;
			RenderSettings.fogColor = Color.black;
			RenderSettings.fogDensity = Mathf.Lerp(0f, 0.45f, DarknessIntensity);
		}
		else
		{
			RenderSettings.fog = false;
		}
		foreach (KeyValuePair<Light, float> item in originalLightIntensities.ToList())
		{
			if ((Object)(object)item.Key != (Object)null)
			{
				if (((Behaviour)item.Key).enabled)
				{
					item.Key.intensity = Mathf.Lerp(item.Value, 0.01f, DarknessIntensity);
				}
			}
			else
			{
				originalLightIntensities.Remove(item.Key);
			}
		}
	}

	private void SaveConfig()
	{
		File.WriteAllText(Path.Combine(Paths.ConfigPath, "LethalDarkness.cfg"), DarknessIntensity.ToString());
	}

	private void LoadConfig()
	{
		string path = Path.Combine(Paths.ConfigPath, "LethalDarkness.cfg");
		if (File.Exists(path) && float.TryParse(File.ReadAllText(path), out var result))
		{
			DarknessIntensity = result;
		}
	}

	[HarmonyPatch(/*Could not decode attribute arguments.*/)]
	[HarmonyPrefix]
	private static bool ForceBlackout(ref Color value)
	{
		//IL_003b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0041: Invalid comparison between Unknown and I4
		//IL_004d: 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_005c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0061: Unknown result type (might be due to invalid IL or missing references)
		if (isMenu || SemiFunc.MenuLevel() || DarknessIntensity <= 0f)
		{
			return true;
		}
		if ((Object)(object)LevelGenerator.Instance != (Object)null && (int)LevelGenerator.Instance.State > 0)
		{
			value = Color.Lerp(value, Color.black, DarknessIntensity);
		}
		return true;
	}
}