Decompiled source of BetterLighting v1.0.0

BetterLighting.dll

Decompiled a week ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using Jotunn.Entities;
using Jotunn.Managers;
using Jotunn.Utils;
using Microsoft.CodeAnalysis;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETFramework,Version=v4.6.2", FrameworkDisplayName = ".NET Framework 4.6.2")]
[assembly: AssemblyCompany("BetterLighting")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("BetterLighting")]
[assembly: AssemblyTitle("BetterLighting")]
[assembly: AssemblyVersion("1.0.0.0")]
[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 BetterLighting
{
	internal static class LightScaler
	{
		private class VanillaLight
		{
			public float Range;

			public float Intensity;

			public LightFlicker Flicker;

			public float FlickerStrength;

			public float FlickerSpeed;

			public float Movement;

			public LightLod Lod;

			public float LodDistance;
		}

		private static readonly Dictionary<Light, VanillaLight> Vanilla = new Dictionary<Light, VanillaLight>();

		private static readonly Dictionary<string, Light[]> PrefabLights = new Dictionary<string, Light[]>();

		private static int prefabLightCount;

		private static readonly List<Piece> AllPieces = (List<Piece>)AccessTools.Field(typeof(Piece), "s_allPieces").GetValue(null);

		private static readonly FieldRef<LightFlicker, float> FlickerBaseIntensity = AccessTools.FieldRefAccess<LightFlicker, float>("m_baseIntensity");

		private static readonly FieldRef<LightLod, float> LodBaseRange = AccessTools.FieldRefAccess<LightLod, float>("m_baseRange");

		internal static void ScanPrefabs()
		{
			//IL_0058: Unknown result type (might be due to invalid IL or missing references)
			//IL_005e: Invalid comparison between Unknown and I4
			PrefabLights.Clear();
			prefabLightCount = 0;
			foreach (GameObject prefab in ZNetScene.instance.m_prefabs)
			{
				if (!Object.op_Implicit((Object)(object)prefab) || Object.op_Implicit((Object)(object)prefab.GetComponent<ItemDrop>()))
				{
					continue;
				}
				Piece component = prefab.GetComponent<Piece>();
				if (!Object.op_Implicit((Object)(object)component) || (int)component.m_category == 1)
				{
					continue;
				}
				Light[] componentsInChildren = prefab.GetComponentsInChildren<Light>(true);
				if (componentsInChildren.Length == 0)
				{
					continue;
				}
				Light[] array = componentsInChildren;
				foreach (Light val in array)
				{
					if (!Vanilla.ContainsKey(val))
					{
						Vanilla[val] = Capture(val);
					}
				}
				PrefabLights[((Object)prefab).name] = componentsInChildren;
				prefabLightCount += componentsInChildren.Length;
			}
			ApplyAll();
			Plugin.Log.LogInfo((object)$"Scaled {prefabLightCount} lights on {PrefabLights.Count} piece types: {Plugin.GetSettingsSummary()}");
		}

		private static VanillaLight Capture(Light light)
		{
			VanillaLight vanillaLight = new VanillaLight
			{
				Range = light.range,
				Intensity = light.intensity
			};
			vanillaLight.Flicker = ((Component)light).GetComponent<LightFlicker>();
			if (Object.op_Implicit((Object)(object)vanillaLight.Flicker))
			{
				vanillaLight.FlickerStrength = vanillaLight.Flicker.m_flickerIntensity;
				vanillaLight.FlickerSpeed = vanillaLight.Flicker.m_flickerSpeed;
				vanillaLight.Movement = vanillaLight.Flicker.m_movement;
			}
			vanillaLight.Lod = ((Component)light).GetComponent<LightLod>();
			if (Object.op_Implicit((Object)(object)vanillaLight.Lod))
			{
				vanillaLight.LodDistance = vanillaLight.Lod.m_lightDistance;
			}
			return vanillaLight;
		}

		internal static void ApplyAll()
		{
			if ((Object)(object)ZNetScene.instance == (Object)null)
			{
				return;
			}
			float num = 1f + (float)Plugin.PercentLightRangeIncrease.Value / 100f;
			float num2 = 1f + (float)Plugin.PercentLightIntensityIncrease.Value / 100f;
			float num3 = 1f - (float)Plugin.PercentFlickerStrengthReduction.Value / 100f;
			float num4 = 1f - (float)Plugin.PercentFlickerSpeedReduction.Value / 100f;
			foreach (Light[] value2 in PrefabLights.Values)
			{
				foreach (Light val in value2)
				{
					if (Object.op_Implicit((Object)(object)val))
					{
						VanillaLight vanillaLight = Vanilla[val];
						val.range = vanillaLight.Range * num;
						val.intensity = vanillaLight.Intensity * num2;
						if (Object.op_Implicit((Object)(object)vanillaLight.Flicker))
						{
							vanillaLight.Flicker.m_flickerIntensity = vanillaLight.FlickerStrength * num3;
							vanillaLight.Flicker.m_flickerSpeed = vanillaLight.FlickerSpeed * num4;
							vanillaLight.Flicker.m_movement = vanillaLight.Movement * num3;
						}
						if (Object.op_Implicit((Object)(object)vanillaLight.Lod))
						{
							vanillaLight.Lod.m_lightDistance = vanillaLight.LodDistance * num;
						}
					}
				}
			}
			foreach (Piece allPiece in AllPieces)
			{
				if (!Object.op_Implicit((Object)(object)allPiece) || !PrefabLights.TryGetValue(Utils.GetPrefabName(((Component)allPiece).gameObject), out var value))
				{
					continue;
				}
				Light[] componentsInChildren = ((Component)allPiece).GetComponentsInChildren<Light>(true);
				if (componentsInChildren.Length == value.Length)
				{
					for (int j = 0; j < componentsInChildren.Length; j++)
					{
						CopyToPlaced(value[j], componentsInChildren[j]);
					}
				}
			}
		}

		private static void CopyToPlaced(Light source, Light light)
		{
			VanillaLight vanillaLight = Vanilla[source];
			bool flag = !((Component)light).gameObject.activeInHierarchy;
			LightFlicker component = ((Component)light).GetComponent<LightFlicker>();
			if (Object.op_Implicit((Object)(object)component) && Object.op_Implicit((Object)(object)vanillaLight.Flicker))
			{
				component.m_flickerIntensity = vanillaLight.Flicker.m_flickerIntensity;
				component.m_flickerSpeed = vanillaLight.Flicker.m_flickerSpeed;
				component.m_movement = vanillaLight.Flicker.m_movement;
				FlickerBaseIntensity.Invoke(component) = source.intensity;
				if (flag)
				{
					light.intensity = source.intensity;
				}
			}
			else
			{
				light.intensity = source.intensity;
			}
			LightLod component2 = ((Component)light).GetComponent<LightLod>();
			if (Object.op_Implicit((Object)(object)component2) && Object.op_Implicit((Object)(object)vanillaLight.Lod))
			{
				component2.m_lightDistance = vanillaLight.Lod.m_lightDistance;
				LodBaseRange.Invoke(component2) = source.range;
				if (flag || !component2.m_lightLod || light.range > source.range)
				{
					light.range = source.range;
				}
			}
			else
			{
				light.range = source.range;
			}
		}

		internal static string GetStatus()
		{
			if ((Object)(object)ZNetScene.instance == (Object)null)
			{
				return "Not in a world.";
			}
			int num = 0;
			foreach (Piece allPiece in AllPieces)
			{
				if (Object.op_Implicit((Object)(object)allPiece) && PrefabLights.ContainsKey(Utils.GetPrefabName(((Component)allPiece).gameObject)))
				{
					num++;
				}
			}
			return $"Scaled {prefabLightCount} lights on {PrefabLights.Count} piece types; {num} placed pieces with them are loaded.";
		}
	}
	[BepInPlugin("Iteravio.BetterLighting", "BetterLighting", "1.0.0")]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	[NetworkCompatibility(/*Could not decode attribute arguments.*/)]
	[SynchronizationMode(/*Could not decode attribute arguments.*/)]
	public class Plugin : BaseUnityPlugin
	{
		public const string PluginGuid = "Iteravio.BetterLighting";

		public const string PluginName = "BetterLighting";

		public const string PluginVersion = "1.0.0";

		internal static ManualLogSource Log;

		internal static ConfigEntry<int> PercentLightRangeIncrease;

		internal static ConfigEntry<int> PercentLightIntensityIncrease;

		internal static ConfigEntry<int> PercentFlickerStrengthReduction;

		internal static ConfigEntry<int> PercentFlickerSpeedReduction;

		private void Awake()
		{
			Log = ((BaseUnityPlugin)this).Logger;
			PercentLightRangeIncrease = ((BaseUnityPlugin)this).Config.Bind<int>("Lights", "PercentLightRangeIncrease", 50, Describe("How much farther placed lights reach. Pieces from the hammer's Crafting tab keep their vanilla glow/brightness. [Synced with Server]", (AcceptableValueBase)(object)new AcceptableValueRange<int>(0, 200), synced: true, 4));
			PercentLightIntensityIncrease = ((BaseUnityPlugin)this).Config.Bind<int>("Lights", "PercentLightIntensityIncrease", 10, Describe("How much brighter placed lights shine. [Synced with Server]", (AcceptableValueBase)(object)new AcceptableValueRange<int>(0, 100), synced: true, 3));
			PercentFlickerStrengthReduction = ((BaseUnityPlugin)this).Config.Bind<int>("Flicker", "PercentFlickerStrengthReduction", 25, Describe("How much less placed lights flicker and sway. [Not synced with Server]", (AcceptableValueBase)(object)new AcceptableValueRange<int>(0, 100), synced: false, 2));
			PercentFlickerSpeedReduction = ((BaseUnityPlugin)this).Config.Bind<int>("Flicker", "PercentFlickerSpeedReduction", 25, Describe("How much slower placed lights flicker. [Not synced with Server]", (AcceptableValueBase)(object)new AcceptableValueRange<int>(0, 100), synced: false, 1));
			PercentLightRangeIncrease.SettingChanged += delegate
			{
				LightScaler.ApplyAll();
			};
			PercentLightIntensityIncrease.SettingChanged += delegate
			{
				LightScaler.ApplyAll();
			};
			PercentFlickerStrengthReduction.SettingChanged += delegate
			{
				LightScaler.ApplyAll();
			};
			PercentFlickerSpeedReduction.SettingChanged += delegate
			{
				LightScaler.ApplyAll();
			};
			PrefabManager.OnPrefabsRegistered += LightScaler.ScanPrefabs;
			CommandManager.Instance.AddConsoleCommand((ConsoleCommand)(object)new StatusCommand());
		}

		private static ConfigDescription Describe(string description, AcceptableValueBase range, bool synced, int order)
		{
			//IL_000a: Unknown result type (might be due to invalid IL or missing references)
			//IL_000f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0016: Unknown result type (might be due to invalid IL or missing references)
			//IL_0023: Expected O, but got Unknown
			//IL_0023: Unknown result type (might be due to invalid IL or missing references)
			//IL_0029: Expected O, but got Unknown
			return new ConfigDescription(description, range, new object[1] { (object)new ConfigurationManagerAttributes
			{
				IsAdminOnly = synced,
				Order = order
			} });
		}

		internal static string GetSettingsSummary()
		{
			return $"range +{PercentLightRangeIncrease.Value}%, intensity +{PercentLightIntensityIncrease.Value}%, " + $"flicker strength -{PercentFlickerStrengthReduction.Value}%, flicker speed -{PercentFlickerSpeedReduction.Value}%";
		}

		internal static string GetSettingsSource()
		{
			ZNet instance = ZNet.instance;
			if ((Object)(object)instance == (Object)null)
			{
				return "local config";
			}
			if (!instance.IsServer())
			{
				return "range and intensity synced from host";
			}
			return "this machine (host/solo)";
		}
	}
	internal class StatusCommand : ConsoleCommand
	{
		public override string Name => "betterlighting";

		public override string Help => "Show BetterLighting values in effect and how many lights it changed";

		public override void Run(string[] args, Terminal context)
		{
			if (!((Object)(object)context == (Object)null))
			{
				context.AddString("BetterLighting 1.0.0");
				context.AddString("Settings (" + Plugin.GetSettingsSource() + "): " + Plugin.GetSettingsSummary());
				context.AddString(LightScaler.GetStatus());
			}
		}
	}
}