Decompiled source of ConeVisionOptimizer v1.0.0

ConeVisionOptimizer.dll

Decompiled 2 hours 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 BepInEx;
using GameNetcodeStuff;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using Unity.Netcode;
using UnityEngine;
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("ConeVisionOptimizer")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("ConeVisionOptimizer")]
[assembly: AssemblyTitle("ConeVisionOptimizer")]
[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 ConeVisionOptimizer
{
	[BepInPlugin("com.mavka.conevisionoptimizer", "ConeVisionOptimizer", "3.4.0")]
	public class Plugin : BaseUnityPlugin
	{
		private void Awake()
		{
			//IL_0005: Unknown result type (might be due to invalid IL or missing references)
			new Harmony("com.mavka.conevisionoptimizer").PatchAll(typeof(SmartShipShadowOptimizer));
			((BaseUnityPlugin)this).Logger.LogInfo((object)"ConeVisionOptimizer 3.4.0 (by mavka) загружен!");
		}
	}
	public class SmartShipShadowOptimizer
	{
		private class ItemLightEntry
		{
			public GrabbableObject Item;

			public Light LightComp;

			public LightShadows OrigShadows;

			public float DistSq;
		}

		private class ShipCeilingLight
		{
			public HDAdditionalLightData Hd;

			public int OrigRes;

			public bool OrigOverride;
		}

		private static readonly List<ItemLightEntry> ItemLights = new List<ItemLightEntry>(64);

		private static readonly List<ItemLightEntry> ActiveGroundLights = new List<ItemLightEntry>(64);

		private static readonly List<ShipCeilingLight> ShipLights = new List<ShipCeilingLight>(16);

		private static int lastTier = -1;

		private static float nextTick = 0f;

		[HarmonyPatch(typeof(RoundManager), "FinishGeneratingNewLevelClientRpc")]
		[HarmonyPostfix]
		private static void OnLevelReady()
		{
			//IL_003a: Unknown result type (might be due to invalid IL or missing references)
			//IL_003f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0077: Unknown result type (might be due to invalid IL or missing references)
			//IL_007d: Invalid comparison between Unknown and I4
			//IL_0097: Unknown result type (might be due to invalid IL or missing references)
			ShipLights.Clear();
			lastTier = -1;
			if ((Object)(object)StartOfRound.Instance == (Object)null || (Object)(object)StartOfRound.Instance.shipBounds == (Object)null)
			{
				return;
			}
			Bounds bounds = StartOfRound.Instance.shipBounds.bounds;
			((Bounds)(ref bounds)).Expand(3f);
			HDAdditionalLightData[] array = Object.FindObjectsOfType<HDAdditionalLightData>(true);
			foreach (HDAdditionalLightData val in array)
			{
				if (!((Object)(object)val == (Object)null))
				{
					Light component = ((Component)val).GetComponent<Light>();
					if ((component == null || (int)component.type != 1) && !((Object)(object)((Component)val).GetComponentInParent<GrabbableObject>() != (Object)null) && ((Bounds)(ref bounds)).Contains(((Component)val).transform.position))
					{
						ShipLights.Add(new ShipCeilingLight
						{
							Hd = val,
							OrigRes = ((((ScalableSettingValue<int>)(object)val.shadowResolution).@override > 0) ? ((ScalableSettingValue<int>)(object)val.shadowResolution).@override : 512),
							OrigOverride = ((ScalableSettingValue<int>)(object)val.shadowResolution).useOverride
						});
					}
				}
			}
		}

		[HarmonyPatch(typeof(GrabbableObject), "Start")]
		[HarmonyPostfix]
		private static void TrackGrabbableLights(GrabbableObject __instance)
		{
			//IL_0024: Unknown result type (might be due to invalid IL or missing references)
			//IL_0080: Unknown result type (might be due to invalid IL or missing references)
			//IL_0085: Unknown result type (might be due to invalid IL or missing references)
			if ((Object)(object)__instance == (Object)null)
			{
				return;
			}
			Light[] componentsInChildren = ((Component)__instance).GetComponentsInChildren<Light>(true);
			foreach (Light val in componentsInChildren)
			{
				if ((Object)(object)val == (Object)null || (int)val.shadows == 0)
				{
					continue;
				}
				bool flag = false;
				for (int j = 0; j < ItemLights.Count; j++)
				{
					if ((Object)(object)ItemLights[j].LightComp == (Object)(object)val)
					{
						flag = true;
						break;
					}
				}
				if (!flag)
				{
					ItemLights.Add(new ItemLightEntry
					{
						Item = __instance,
						LightComp = val,
						OrigShadows = val.shadows
					});
				}
			}
		}

		[HarmonyPatch(typeof(PlayerControllerB), "LateUpdate")]
		[HarmonyPostfix]
		private static void Tick(PlayerControllerB __instance)
		{
			//IL_0077: Unknown result type (might be due to invalid IL or missing references)
			//IL_0065: Unknown result type (might be due to invalid IL or missing references)
			//IL_007c: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e4: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ea: 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_01b1: Unknown result type (might be due to invalid IL or missing references)
			//IL_012f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0134: Unknown result type (might be due to invalid IL or missing references)
			//IL_0135: Unknown result type (might be due to invalid IL or missing references)
			//IL_013a: Unknown result type (might be due to invalid IL or missing references)
			//IL_01b6: Unknown result type (might be due to invalid IL or missing references)
			//IL_01bf: Unknown result type (might be due to invalid IL or missing references)
			//IL_01c4: Unknown result type (might be due to invalid IL or missing references)
			//IL_01cf: Unknown result type (might be due to invalid IL or missing references)
			if ((Object)(object)__instance == (Object)null || !((NetworkBehaviour)__instance).IsOwner || (Object)(object)StartOfRound.Instance == (Object)null || Time.time < nextTick)
			{
				return;
			}
			nextTick = Time.time + 0.6f;
			if (ItemLights.Count == 0 && lastTier <= 0)
			{
				return;
			}
			Vector3 val = (((Object)(object)__instance.gameplayCamera != (Object)null) ? ((Component)__instance.gameplayCamera).transform.position : ((Component)__instance).transform.position);
			ActiveGroundLights.Clear();
			for (int num = ItemLights.Count - 1; num >= 0; num--)
			{
				ItemLightEntry itemLightEntry = ItemLights[num];
				if ((Object)(object)itemLightEntry.Item == (Object)null || (Object)(object)itemLightEntry.LightComp == (Object)null)
				{
					ItemLights.RemoveAt(num);
				}
				else if (itemLightEntry.Item.isHeld)
				{
					if (itemLightEntry.LightComp.shadows != itemLightEntry.OrigShadows)
					{
						itemLightEntry.LightComp.shadows = itemLightEntry.OrigShadows;
					}
				}
				else if (((Behaviour)itemLightEntry.LightComp).enabled && ((Component)itemLightEntry.LightComp).gameObject.activeInHierarchy)
				{
					Vector3 val2 = ((Component)itemLightEntry.LightComp).transform.position - val;
					itemLightEntry.DistSq = ((Vector3)(ref val2)).sqrMagnitude;
					ActiveGroundLights.Add(itemLightEntry);
				}
			}
			if (ActiveGroundLights.Count > 6)
			{
				ActiveGroundLights.Sort((ItemLightEntry a, ItemLightEntry b) => a.DistSq.CompareTo(b.DistSq));
			}
			for (int num2 = 0; num2 < ActiveGroundLights.Count; num2++)
			{
				ItemLightEntry itemLightEntry2 = ActiveGroundLights[num2];
				LightShadows val3 = (LightShadows)((num2 < 6) ? ((int)itemLightEntry2.OrigShadows) : 0);
				if (itemLightEntry2.LightComp.shadows != val3)
				{
					itemLightEntry2.LightComp.shadows = val3;
				}
			}
			int num3 = ((ActiveGroundLights.Count > 6) ? ((ActiveGroundLights.Count <= 14) ? 1 : 2) : 0);
			if (num3 == lastTier)
			{
				return;
			}
			lastTier = num3;
			int num4 = num3 switch
			{
				1 => 256, 
				0 => 512, 
				_ => 128, 
			};
			for (int num5 = 0; num5 < ShipLights.Count; num5++)
			{
				ShipCeilingLight shipCeilingLight = ShipLights[num5];
				if (!((Object)(object)shipCeilingLight.Hd == (Object)null))
				{
					if (num3 == 0)
					{
						((ScalableSettingValue<int>)(object)shipCeilingLight.Hd.shadowResolution).useOverride = shipCeilingLight.OrigOverride;
						((ScalableSettingValue<int>)(object)shipCeilingLight.Hd.shadowResolution).@override = shipCeilingLight.OrigRes;
					}
					else
					{
						((ScalableSettingValue<int>)(object)shipCeilingLight.Hd.shadowResolution).useOverride = true;
						((ScalableSettingValue<int>)(object)shipCeilingLight.Hd.shadowResolution).@override = num4;
					}
				}
			}
		}
	}
}