Decompiled source of Colorable CozyLights v2.0.0

BepInEx/Plugins/Colorable_Cozy_Lights.dll

Decompiled 2 months ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
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: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("Colorable_Cozy_Lights")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0+e9ab6d7f6418a74077eb5aecfe6ae6f91f887f01")]
[assembly: AssemblyProduct("Adds a config to change the color of the cozy lights")]
[assembly: AssemblyTitle("Colorable_Cozy_Lights")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.0.0")]
[module: UnverifiableCode]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.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 Colorable_Cozy_Lights
{
	[BepInPlugin("com.atomic.colorcozylights", "Color Cozy Lights", "1.9.0")]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	public class Plugin : BaseUnityPlugin
	{
		[HarmonyPatch(typeof(CozyLights), "Update")]
		public class CozyLightsPatch
		{
			private static bool lastAnimatorState;

			private static void Postfix(CozyLights __instance)
			{
				((Component)__instance).GetComponent<Animator>().runtimeAnimatorController = lightsAnimator;
				CozyLightsManager cozyLightsManager = Object.FindObjectOfType<CozyLightsManager>();
				if (__instance.cozyLightsAnimator.GetBool("on") && !lastAnimatorState && (Object)(object)cozyLightsManager != (Object)null)
				{
					cozyLightsManager.Initialize();
				}
				lastAnimatorState = __instance.cozyLightsAnimator.GetBool("on");
			}
		}

		internal static ConfigEntry<int> colorConfigR;

		internal static ConfigEntry<int> colorConfigG;

		internal static ConfigEntry<int> colorConfigB;

		internal static ConfigEntry<bool> Rainbow;

		internal static ConfigEntry<float> RainbowSpeed;

		internal static ConfigEntry<float> Brightness;

		internal static ConfigEntry<bool> AnimationActive;

		internal static ConfigEntry<string> AnimationValue;

		internal static RuntimeAnimatorController lightsAnimator;

		internal static ManualLogSource Logger;

		private Harmony harmony;

		private void Awake()
		{
			//IL_0137: Unknown result type (might be due to invalid IL or missing references)
			//IL_0141: Expected O, but got Unknown
			Logger = ((BaseUnityPlugin)this).Logger;
			Logger.LogInfo((object)"Color Cozy Lights has loaded! Checking and setting configs.");
			colorConfigR = ((BaseUnityPlugin)this).Config.Bind<int>("Cozy Light Color", "Cozy Lights Color - R", 53, "RGB value (0-255) for Red. YOU DONT NEED TO RESET THE GAME");
			colorConfigG = ((BaseUnityPlugin)this).Config.Bind<int>("Cozy Light Color", "Cozy Lights Color - G", 135, "RGB value (0-255) for Green. YOU DONT NEED TO RESET THE GAME");
			colorConfigB = ((BaseUnityPlugin)this).Config.Bind<int>("Cozy Light Color", "Cozy Lights Color - B", 255, "RGB value (0-255) for Blue. YOU DONT NEED TO RESET THE GAME");
			Brightness = ((BaseUnityPlugin)this).Config.Bind<float>("Cozy Light Color", "Cozy Lights Brightness", 138f, "Decimals allowed. Brightness off the light. Please don't surpass 1000, its too bright. YOU DONT NEED TO RESET THE GAME");
			Rainbow = ((BaseUnityPlugin)this).Config.Bind<bool>("Cozy Light Color", "Rainbow", false, "Cycles through colors!. YOU DONT NEED TO RESET THE GAME");
			RainbowSpeed = ((BaseUnityPlugin)this).Config.Bind<float>("Cozy Light Color", "Rainbow Speed", 0.05f, "Sets the speed of the rainbow, I am not responsible for any seizures people have for high amounts. YOU DONT NEED TO RESET THE GAME");
			AnimationActive = ((BaseUnityPlugin)this).Config.Bind<bool>("Animations", "Animation Enabled", false, "Make light shows! Go to atomictyler.dev/tools/lights for easy creation!");
			AnimationValue = ((BaseUnityPlugin)this).Config.Bind<string>("Animations", "Animations", "{'255,255,255,1','5000'},{'0,255,144,0.5','2500'}", "atomictyler.dev/tools/lights recommended. It works by having each animation in {} and seperated with commas. you need RGBA and how long it will stay on that anim in ms.");
			harmony = new Harmony("com.atomic.colorcozylights.patch");
			harmony.PatchAll();
			try
			{
				AssetBundle val = AssetBundle.LoadFromFile(Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "lightanimator"));
				Object obj = val.LoadAsset("ShipCozyLightsClone", typeof(RuntimeAnimatorController));
				lightsAnimator = (RuntimeAnimatorController)(object)((obj is RuntimeAnimatorController) ? obj : null);
			}
			catch
			{
				Logger.LogError((object)"Encountered some error loading asset bundle. Did you install the plugin correctly?");
				return;
			}
			SceneManager.sceneLoaded += OnSceneLoaded;
		}

		private void OnSceneLoaded(Scene scene, LoadSceneMode mode)
		{
			if (((Scene)(ref scene)).name == "SampleSceneRelay")
			{
				GameObject val = GameObject.Find("Environment");
				val.AddComponent<CozyLightsManager>();
			}
		}
	}
	public class CozyLightsManager : MonoBehaviour
	{
		private float normalizedR;

		private float normalizedG;

		private float normalizedB;

		private float hue = 0f;

		private float rainbowSpeed = Plugin.RainbowSpeed.Value;

		private float brightness = 0f;

		private List<(Color color, float duration)> animationSteps;

		private int currentStepIndex = 0;

		private float animationTimer = 0f;

		private bool animationInitialized = false;

		internal bool LightStatus = false;

		public bool MaterialMade = false;

		public Material cozyLightsNewMat;

		public Material cozyLightsOldMat;

		public GameObject cozyLights;

		public RuntimeAnimatorController lightsAnimator = Plugin.lightsAnimator;

		private List<(Color color, float duration)> ParseAnimationConfig(string configString)
		{
			//IL_0112: Unknown result type (might be due to invalid IL or missing references)
			List<(Color, float)> list = new List<(Color, float)>();
			string[] array = configString.Trim('{', '}').Split(new string[1] { "},{" }, StringSplitOptions.RemoveEmptyEntries);
			string[] array2 = array;
			Color item = default(Color);
			foreach (string text in array2)
			{
				string text2 = text.Trim('{', '}');
				string[] array3 = text2.Split('\'');
				if (array3.Length >= 2)
				{
					string text3 = array3[1];
					string s = array3[3];
					string[] array4 = text3.Split(',');
					if (array4.Length == 4 && float.TryParse(s, out var result))
					{
						float num = (float)int.Parse(array4[0].Trim()) / 255f;
						float num2 = (float)int.Parse(array4[1].Trim()) / 255f;
						float num3 = (float)int.Parse(array4[2].Trim()) / 255f;
						float num4 = float.Parse(array4[3].Trim());
						((Color)(ref item))..ctor(num, num2, num3, num4);
						list.Add((item, result / 1000f));
					}
				}
			}
			return list;
		}

		private void Awake()
		{
			Initialize();
		}

		private void Update()
		{
			//IL_008b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0090: 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_00f6: Unknown result type (might be due to invalid IL or missing references)
			//IL_0102: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e2: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e7: Unknown result type (might be due to invalid IL or missing references)
			//IL_0197: Unknown result type (might be due to invalid IL or missing references)
			//IL_019c: Unknown result type (might be due to invalid IL or missing references)
			//IL_019f: Unknown result type (might be due to invalid IL or missing references)
			//IL_01ac: Unknown result type (might be due to invalid IL or missing references)
			//IL_01b9: Unknown result type (might be due to invalid IL or missing references)
			if (Plugin.AnimationActive.Value)
			{
				if (!animationInitialized)
				{
					animationSteps = ParseAnimationConfig(Plugin.AnimationValue.Value);
					currentStepIndex = 0;
					animationTimer = 0f;
					animationInitialized = true;
				}
				if (animationSteps.Count > 0)
				{
					animationTimer += Time.deltaTime;
					var (val, num) = animationSteps[currentStepIndex];
					if (animationTimer >= num)
					{
						animationTimer = 0f;
						currentStepIndex = (currentStepIndex + 1) % animationSteps.Count;
						val = animationSteps[currentStepIndex].color;
					}
					normalizedR = val.r;
					normalizedG = val.g;
					normalizedB = val.b;
					SetCozyLights();
					return;
				}
			}
			if (Plugin.Rainbow.Value && !Plugin.AnimationActive.Value)
			{
				rainbowSpeed = Plugin.RainbowSpeed.Value;
				hue += rainbowSpeed * Time.deltaTime;
				if (hue > 1f)
				{
					hue = 0f;
				}
				Color val2 = Color.HSVToRGB(hue, 0.8f, 0.8f);
				normalizedR = val2.r;
				normalizedG = val2.g;
				normalizedB = val2.b;
				SetCozyLights();
			}
			else
			{
				SetCozyLights();
			}
		}

		public void MaterialHandler(MeshRenderer cozyLights)
		{
			//IL_0031: Unknown result type (might be due to invalid IL or missing references)
			//IL_003b: Expected O, but got Unknown
			if (MaterialMade)
			{
				MaterialSwapper(cozyLights);
				return;
			}
			Material[] materials = ((Renderer)cozyLights).materials;
			cozyLightsOldMat = materials[1];
			cozyLightsNewMat = new Material(Shader.Find("HDRP/Lit"));
			MaterialMade = true;
			MaterialSwapper(cozyLights);
		}

		public void MaterialSwapper(MeshRenderer cozyLights)
		{
			Material[] materials = ((Renderer)cozyLights).materials;
			materials[1] = cozyLightsNewMat;
			((Renderer)cozyLights).materials = materials;
		}

		public void MaterialColourChanger(float R, float G, float B)
		{
			//IL_004d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0066: Unknown result type (might be due to invalid IL or missing references)
			//IL_0037: Unknown result type (might be due to invalid IL or missing references)
			if (!((Object)(object)cozyLightsNewMat == (Object)null))
			{
				if (!LightStatus)
				{
					cozyLightsNewMat.color = new Color(0f, 0f, 0f);
					return;
				}
				cozyLightsNewMat.color = new Color(R, G, B);
				cozyLightsNewMat.SetColor("_BaseColor", new Color(R, G, B));
			}
		}

		public void Initialize()
		{
			normalizedR = (float)Plugin.colorConfigR.Value / 100f;
			normalizedG = (float)Plugin.colorConfigG.Value / 100f;
			normalizedB = (float)Plugin.colorConfigB.Value / 100f;
			SetCozyLights();
		}

		public void SetCozyLights()
		{
			//IL_0043: Unknown result type (might be due to invalid IL or missing references)
			//IL_0049: Expected O, but got Unknown
			//IL_0067: Unknown result type (might be due to invalid IL or missing references)
			//IL_006e: Expected O, but got Unknown
			//IL_010a: Unknown result type (might be due to invalid IL or missing references)
			brightness = Plugin.Brightness.Value;
			GameObject val = GameObject.Find("ShipCozyLights(Clone)");
			if (!((Object)(object)val != (Object)null))
			{
				return;
			}
			foreach (Transform item in val.transform)
			{
				Transform val2 = item;
				MeshRenderer component = ((Component)val2).GetComponent<MeshRenderer>();
				foreach (Transform item2 in val2)
				{
					Transform val3 = item2;
					if (((Object)val3).name == "Light")
					{
						Light component2 = ((Component)val3).GetComponent<Light>();
						if ((Object)(object)component2 != (Object)null)
						{
							if (!((Behaviour)component2).isActiveAndEnabled)
							{
								LightStatus = false;
							}
							else
							{
								LightStatus = true;
							}
							MaterialHandler(component);
							MaterialColourChanger(normalizedR, normalizedG, normalizedB);
							component2.intensity = brightness;
							component2.color = new Color(normalizedR, normalizedG, normalizedB);
						}
					}
					else
					{
						MeshRenderer component3 = ((Component)val3).GetComponent<MeshRenderer>();
						MaterialHandler(component3);
					}
				}
			}
		}
	}
	public static class MyPluginInfo
	{
		public const string PLUGIN_GUID = "Colorable_Cozy_Lights";

		public const string PLUGIN_NAME = "Adds a config to change the color of the cozy lights";

		public const string PLUGIN_VERSION = "1.0.0";
	}
}