Decompiled source of RainbowGear v1.15.1

RainbowGear.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 BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: AssemblyCompany("RainbowGear")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("RainbowGear")]
[assembly: AssemblyTitle("RainbowGear")]
[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 RainbowGear
{
	public enum ShaderMode
	{
		Vanilla,
		Alpha,
		Additive,
		Unlit
	}
	[BepInPlugin("snowfarm.valheim.rainbowgear", "RainbowGear", "1.15.1")]
	public class RainbowGearPlugin : BaseUnityPlugin
	{
		private sealed class DualGlow
		{
			public SpriteRenderer Head;

			public SpriteRenderer Grip;
		}

		public const string Guid = "snowfarm.valheim.rainbowgear";

		public const string Name = "RainbowGear";

		public const string Version = "1.15.1";

		private static ManualLogSource _log;

		private static ConfigEntry<bool> _enabled;

		private static ConfigEntry<ShaderMode> _shaderMode;

		private static ConfigEntry<float> _glowRadius;

		private static ConfigEntry<bool> _dualGlow;

		private static ConfigEntry<float> _dualScale;

		private static ConfigEntry<float> _dualAlpha;

		private static ConfigEntry<KeyCode> _reloadKey;

		private static ConfigEntry<int> _minQuality;

		private static ConfigEntry<float> _cycleSpeed;

		private static ConfigEntry<float> _saturation;

		private static ConfigEntry<float> _mapRed;

		private static ConfigEntry<float> _mapGreen;

		private static ConfigEntry<float> _mapBlue;

		private static ConfigEntry<float> _targetLuminance;

		private static ConfigEntry<float> _maxEmission;

		private static ConfigEntry<bool> _hueCompensation;

		private static ConfigEntry<float> _hueStrength;

		private static bool _mapSampled;

		private static bool _mapSampleLogged;

		private static readonly Dictionary<int, Shader> OriginalShaders = new Dictionary<int, Shader>();

		private static Texture2D _whiteGlow;

		private static Sprite _glowSprite;

		private static readonly Dictionary<int, DualGlow> _dualGlows = new Dictionary<int, DualGlow>();

		private static float _whiteGlowRadius = -1f;

		private static Color _sampledMap = Color.white;

		private static ConfigEntry<float> _brightness;

		private static readonly FieldRef<VisEquipment, GameObject> RightInstance = AccessTools.FieldRefAccess<VisEquipment, GameObject>("m_rightItemInstance");

		private static readonly FieldRef<VisEquipment, GameObject> LeftInstance = AccessTools.FieldRefAccess<VisEquipment, GameObject>("m_leftItemInstance");

		private static readonly FieldRef<VisEquipment, int> RightQuality = AccessTools.FieldRefAccess<VisEquipment, int>("m_rightItemQuality");

		private static readonly FieldRef<VisEquipment, int> LeftQuality = AccessTools.FieldRefAccess<VisEquipment, int>("m_leftItemQuality");

		private static readonly FieldRef<VisEquipment, GameObject> RightBackInstance = AccessTools.FieldRefAccess<VisEquipment, GameObject>("m_rightBackItemInstance");

		private static readonly FieldRef<VisEquipment, GameObject> LeftBackInstance = AccessTools.FieldRefAccess<VisEquipment, GameObject>("m_leftBackItemInstance");

		private static readonly FieldRef<VisEquipment, int> RightBackQuality = AccessTools.FieldRefAccess<VisEquipment, int>("m_rightBackItemQuality");

		private static readonly FieldRef<VisEquipment, int> LeftBackQuality = AccessTools.FieldRefAccess<VisEquipment, int>("m_leftBackItemQuality");

		private static readonly FieldRef<MeleeWeaponTrail, Color[]> TrailColors = AccessTools.FieldRefAccess<MeleeWeaponTrail, Color[]>("_colors");

		private static readonly List<GameObject> ExtraItems = new List<GameObject>();

		private static readonly Color[] Palette = (Color[])(object)new Color[21]
		{
			new Color(1f, 0.15f, 0.18f),
			new Color(1f, 0.24f, 0.1f),
			new Color(1f, 0.32f, 0.12f),
			new Color(1f, 0.42f, 0.1f),
			new Color(1f, 0.52f, 0.1f),
			new Color(1f, 0.62f, 0.14f),
			new Color(1f, 0.72f, 0.18f),
			new Color(1f, 0.8f, 0.22f),
			new Color(1f, 0.9f, 0.3f),
			new Color(0.85f, 0.98f, 0.3f),
			new Color(0.62f, 1f, 0.34f),
			new Color(0.42f, 1f, 0.42f),
			new Color(0.22f, 0.95f, 0.68f),
			new Color(0.1f, 0.9f, 0.92f),
			new Color(0.15f, 0.68f, 1f),
			new Color(0.2f, 0.46f, 1f),
			new Color(0.3f, 0.32f, 1f),
			new Color(0.5f, 0.25f, 1f),
			new Color(0.72f, 0.26f, 0.98f),
			new Color(0.9f, 0.28f, 0.9f),
			new Color(1f, 0.32f, 0.72f)
		};

		private static readonly float[] HueGainTable = new float[12]
		{
			1.5f, 1.45f, 1.2f, 0.85f, 0.9f, 1.3f, 1.45f, 1.4f, 1.3f, 1.25f,
			1.3f, 1.45f
		};

		private static Gradient _whiteGradient;

		private static Gradient WhiteGradient
		{
			get
			{
				//IL_000d: Unknown result type (might be due to invalid IL or missing references)
				//IL_0017: Expected O, but got Unknown
				//IL_0024: Unknown result type (might be due to invalid IL or missing references)
				//IL_002e: Unknown result type (might be due to invalid IL or missing references)
				//IL_0033: Unknown result type (might be due to invalid IL or missing references)
				//IL_003a: Unknown result type (might be due to invalid IL or missing references)
				//IL_0044: Unknown result type (might be due to invalid IL or missing references)
				//IL_0049: Unknown result type (might be due to invalid IL or missing references)
				//IL_0060: 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_0076: Unknown result type (might be due to invalid IL or missing references)
				//IL_007b: Unknown result type (might be due to invalid IL or missing references)
				if (_whiteGradient != null)
				{
					return _whiteGradient;
				}
				_whiteGradient = new Gradient();
				_whiteGradient.SetKeys((GradientColorKey[])(object)new GradientColorKey[2]
				{
					new GradientColorKey(Color.white, 0f),
					new GradientColorKey(Color.white, 1f)
				}, (GradientAlphaKey[])(object)new GradientAlphaKey[2]
				{
					new GradientAlphaKey(1f, 0f),
					new GradientAlphaKey(0f, 1f)
				});
				return _whiteGradient;
			}
		}

		private void Awake()
		{
			//IL_009d: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a7: Expected O, but got Unknown
			//IL_00da: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e4: Expected O, but got Unknown
			//IL_0117: Unknown result type (might be due to invalid IL or missing references)
			//IL_0121: Expected O, but got Unknown
			//IL_016a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0174: Expected O, but got Unknown
			//IL_01a7: Unknown result type (might be due to invalid IL or missing references)
			//IL_01b1: Expected O, but got Unknown
			//IL_01e4: Unknown result type (might be due to invalid IL or missing references)
			//IL_01ee: Expected O, but got Unknown
			//IL_0221: Unknown result type (might be due to invalid IL or missing references)
			//IL_022b: Expected O, but got Unknown
			//IL_025e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0268: Expected O, but got Unknown
			//IL_029b: Unknown result type (might be due to invalid IL or missing references)
			//IL_02a5: Expected O, but got Unknown
			//IL_02d8: Unknown result type (might be due to invalid IL or missing references)
			//IL_02e2: Expected O, but got Unknown
			//IL_0315: Unknown result type (might be due to invalid IL or missing references)
			//IL_031f: Expected O, but got Unknown
			//IL_0372: Unknown result type (might be due to invalid IL or missing references)
			//IL_037c: Expected O, but got Unknown
			//IL_03af: Unknown result type (might be due to invalid IL or missing references)
			//IL_03b9: Expected O, but got Unknown
			_log = ((BaseUnityPlugin)this).Logger;
			_enabled = ((BaseUnityPlugin)this).Config.Bind<bool>("1 - General", "Enabled", true, "Turn the plugin on or off.");
			_reloadKey = ((BaseUnityPlugin)this).Config.Bind<KeyCode>("1 - General", "ReloadConfigKey", (KeyCode)291, "Reload this file in game.");
			_dualGlow = ((BaseUnityPlugin)this).Config.Bind<bool>("1 - General", "DualGlow", false, "Add a second glow on the grip with a colour half a cycle apart, so the colour flows along the weapon.");
			_dualScale = ((BaseUnityPlugin)this).Config.Bind<float>("1 - General", "DualGlowScale", 1.1f, new ConfigDescription("Size of the head/grip glows relative to the weapon part.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0.2f, 3f), Array.Empty<object>()));
			_dualAlpha = ((BaseUnityPlugin)this).Config.Bind<float>("1 - General", "DualGlowAlpha", 0.55f, new ConfigDescription("Opacity of the head/grip glows.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0f, 1f), Array.Empty<object>()));
			_glowRadius = ((BaseUnityPlugin)this).Config.Bind<float>("1 - General", "GlowRadius", 0.35f, new ConfigDescription("Fraction of the particle quad that actually glows; lower = smaller halo.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0.1f, 1f), Array.Empty<object>()));
			_shaderMode = ((BaseUnityPlugin)this).Config.Bind<ShaderMode>("1 - General", "ShaderMode", ShaderMode.Alpha, "Particle shader: Vanilla keeps the dark ring, Alpha removes it with soft transparency.");
			_minQuality = ((BaseUnityPlugin)this).Config.Bind<int>("1 - General", "MinQuality", 26, new ConfigDescription("Only items with quality >= this value get the effect (26 = above 25).", (AcceptableValueBase)(object)new AcceptableValueRange<int>(1, 100), Array.Empty<object>()));
			_cycleSpeed = ((BaseUnityPlugin)this).Config.Bind<float>("2 - Rainbow", "CycleSpeed", 0.5f, new ConfigDescription("Full colour cycles per second.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0.02f, 3f), Array.Empty<object>()));
			_saturation = ((BaseUnityPlugin)this).Config.Bind<float>("2 - Rainbow", "Saturation", 0.65f, new ConfigDescription("0 = white, 1 = vivid rainbow.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0f, 1f), Array.Empty<object>()));
			_mapRed = ((BaseUnityPlugin)this).Config.Bind<float>("2 - Rainbow", "MapRed", 0.08f, new ConfigDescription("Approximate red content of the glow's emission map, used for luminance normalisation.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0f, 1f), Array.Empty<object>()));
			_mapGreen = ((BaseUnityPlugin)this).Config.Bind<float>("2 - Rainbow", "MapGreen", 1.1f, new ConfigDescription("Approximate green content of the emission map.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0f, 1f), Array.Empty<object>()));
			_mapBlue = ((BaseUnityPlugin)this).Config.Bind<float>("2 - Rainbow", "MapBlue", 0.12f, new ConfigDescription("Approximate blue content of the emission map.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0f, 1f), Array.Empty<object>()));
			_targetLuminance = ((BaseUnityPlugin)this).Config.Bind<float>("2 - Rainbow", "TargetLuminance", 0.55f, new ConfigDescription("Perceived brightness every hue is normalised to.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0.1f, 2f), Array.Empty<object>()));
			_maxEmission = ((BaseUnityPlugin)this).Config.Bind<float>("2 - Rainbow", "MaxEmission", 20f, new ConfigDescription("Upper limit for a single emission channel (HDR).", (AcceptableValueBase)(object)new AcceptableValueRange<float>(1f, 64f), Array.Empty<object>()));
			_hueCompensation = ((BaseUnityPlugin)this).Config.Bind<bool>("2 - Rainbow", "HueCompensation", true, "Lift the hues that tone mapping renders dimmer (measured: red-orange and cyan-blue).");
			_hueStrength = ((BaseUnityPlugin)this).Config.Bind<float>("2 - Rainbow", "HueCompensationStrength", 1f, new ConfigDescription("How strongly the hue compensation is applied.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0f, 2f), Array.Empty<object>()));
			_brightness = ((BaseUnityPlugin)this).Config.Bind<float>("2 - Rainbow", "Brightness", 1f, new ConfigDescription("Overall emission multiplier. Keep at 1 to preserve the vanilla halo.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0.3f, 2f), Array.Empty<object>()));
			((BaseUnityPlugin)this).Logger.LogInfo((object)"RainbowGear 1.15.1 loaded.");
		}

		private void Update()
		{
			//IL_0012: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a5: Unknown result type (might be due to invalid IL or missing references)
			//IL_00aa: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c3: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e1: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ff: Unknown result type (might be due to invalid IL or missing references)
			//IL_011d: Unknown result type (might be due to invalid IL or missing references)
			//IL_015d: Unknown result type (might be due to invalid IL or missing references)
			if (!_enabled.Value)
			{
				return;
			}
			if (Input.GetKeyDown(_reloadKey.Value))
			{
				((BaseUnityPlugin)this).Config.Reload();
				_log.LogInfo((object)$"[RainbowGear] config reloaded: ShaderMode={_shaderMode.Value} Saturation={_saturation.Value} Brightness={_brightness.Value}");
			}
			Player localPlayer = Player.m_localPlayer;
			if ((Object)(object)localPlayer == (Object)null)
			{
				return;
			}
			VisEquipment component = ((Component)localPlayer).GetComponent<VisEquipment>();
			if ((Object)(object)component == (Object)null)
			{
				return;
			}
			Color color = SamplePalette(Mathf.Repeat(Time.time * _cycleSpeed.Value, 1f));
			Apply(RightInstance.Invoke(component), RightQuality.Invoke(component), color);
			Apply(LeftInstance.Invoke(component), LeftQuality.Invoke(component), color);
			Apply(RightBackInstance.Invoke(component), RightBackQuality.Invoke(component), color);
			Apply(LeftBackInstance.Invoke(component), LeftBackQuality.Invoke(component), color);
			for (int num = ExtraItems.Count - 1; num >= 0; num--)
			{
				GameObject val = ExtraItems[num];
				if ((Object)(object)val == (Object)null)
				{
					ExtraItems.RemoveAt(num);
				}
				else
				{
					Apply(val, int.MaxValue, color);
				}
			}
		}

		public static void RegisterExtra(GameObject item)
		{
			if ((Object)(object)item != (Object)null && !ExtraItems.Contains(item))
			{
				ExtraItems.Add(item);
			}
		}

		private static Color SamplePalette(float t)
		{
			//IL_0037: Unknown result type (might be due to invalid IL or missing references)
			//IL_0042: Unknown result type (might be due to invalid IL or missing references)
			//IL_004f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0054: Unknown result type (might be due to invalid IL or missing references)
			//IL_0055: Unknown result type (might be due to invalid IL or missing references)
			//IL_005a: 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)
			t = Mathf.Repeat(t, 1f) * (float)Palette.Length;
			int num = Mathf.FloorToInt(t) % Palette.Length;
			int num2 = (num + 1) % Palette.Length;
			Color val = Color.Lerp(Palette[num], Palette[num2], t - Mathf.Floor(t));
			return Color.Lerp(Color.white, val, _saturation.Value);
		}

		private static float HueGain(Color c)
		{
			//IL_0000: Unknown result type (might be due to invalid IL or missing references)
			float num = default(float);
			float num2 = default(float);
			float num3 = default(float);
			Color.RGBToHSV(c, ref num, ref num2, ref num3);
			float num4 = num * (float)HueGainTable.Length;
			int num5 = Mathf.FloorToInt(num4) % HueGainTable.Length;
			int num6 = (num5 + 1) % HueGainTable.Length;
			float num7 = Mathf.Lerp(HueGainTable[num5], HueGainTable[num6], num4 - Mathf.Floor(num4));
			return Mathf.Lerp(1f, num7, _hueStrength.Value);
		}

		private static void TrySampleMap(Material material)
		{
			//IL_0072: Unknown result type (might be due to invalid IL or missing references)
			//IL_0078: Expected O, but got Unknown
			//IL_008d: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c9: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ce: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d2: Unknown result type (might be due to invalid IL or missing references)
			//IL_00df: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ec: Unknown result type (might be due to invalid IL or missing references)
			//IL_0165: Unknown result type (might be due to invalid IL or missing references)
			//IL_016a: Unknown result type (might be due to invalid IL or missing references)
			if (_mapSampled)
			{
				return;
			}
			Texture texture = material.GetTexture("_EmissionMap");
			if ((Object)(object)texture == (Object)null)
			{
				if (!_mapSampleLogged)
				{
					_mapSampleLogged = true;
					_log.LogInfo((object)("[RainbowGear] material '" + ((Object)material).name + "' has no _EmissionMap; using configured map values."));
				}
				return;
			}
			RenderTexture val = null;
			RenderTexture active = RenderTexture.active;
			try
			{
				val = RenderTexture.GetTemporary(32, 32, 0, (RenderTextureFormat)0, (RenderTextureReadWrite)2);
				Graphics.Blit(texture, val);
				RenderTexture.active = val;
				Texture2D val2 = new Texture2D(32, 32, (TextureFormat)4, false);
				val2.ReadPixels(new Rect(0f, 0f, 32f, 32f), 0, 0);
				val2.Apply();
				Color32[] pixels = val2.GetPixels32();
				float num = 0f;
				float num2 = 0f;
				float num3 = 0f;
				Color32[] array = pixels;
				foreach (Color32 val3 in array)
				{
					num += (float)(int)val3.r;
					num2 += (float)(int)val3.g;
					num3 += (float)(int)val3.b;
				}
				int num4 = Mathf.Max(1, pixels.Length);
				num /= (float)num4 * 255f;
				num2 /= (float)num4 * 255f;
				num3 /= (float)num4 * 255f;
				float num5 = Mathf.Max(num, Mathf.Max(num2, num3));
				if (num5 > 0.02f)
				{
					_sampledMap = new Color(num / num5, num2 / num5, num3 / num5);
					_mapSampled = true;
					_log.LogInfo((object)$"[RainbowGear] emission map sampled: R={_sampledMap.r:F2} G={_sampledMap.g:F2} B={_sampledMap.b:F2}");
				}
				Object.Destroy((Object)(object)val2);
			}
			catch (Exception ex)
			{
				_log.LogDebug((object)("[RainbowGear] emission map sampling failed: " + ex.Message));
			}
			finally
			{
				RenderTexture.active = active;
				if ((Object)(object)val != (Object)null)
				{
					RenderTexture.ReleaseTemporary(val);
				}
			}
		}

		private static Color ResolveMap(Material material)
		{
			//IL_0007: Unknown result type (might be due to invalid IL or missing references)
			//IL_0054: Unknown result type (might be due to invalid IL or missing references)
			//IL_0030: Unknown result type (might be due to invalid IL or missing references)
			if (_mapSampled)
			{
				return _sampledMap;
			}
			if ((Object)(object)(material.HasProperty("_EmissionMap") ? material.GetTexture("_EmissionMap") : null) == (Object)null)
			{
				return Color.white;
			}
			return new Color(_mapRed.Value, _mapGreen.Value, _mapBlue.Value);
		}

		private static Color ComputeEmission(Color color, Color map)
		{
			//IL_0005: Unknown result type (might be due to invalid IL or missing references)
			//IL_0011: Unknown result type (might be due to invalid IL or missing references)
			//IL_001e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0040: Unknown result type (might be due to invalid IL or missing references)
			//IL_0046: Unknown result type (might be due to invalid IL or missing references)
			//IL_0073: Unknown result type (might be due to invalid IL or missing references)
			//IL_0079: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a6: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ac: Unknown result type (might be due to invalid IL or missing references)
			//IL_011a: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ef: 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_0100: Unknown result type (might be due to invalid IL or missing references)
			//IL_0108: Unknown result type (might be due to invalid IL or missing references)
			float num = 0.2126f * color.r + 0.7152f * color.g + 0.0722f * color.b;
			float num2 = _targetLuminance.Value / Mathf.Max(num, 0.05f);
			Color val = default(Color);
			((Color)(ref val))..ctor(Mathf.Min(color.r / Mathf.Max(map.r, 0.02f) * num2, _maxEmission.Value) * _brightness.Value, Mathf.Min(color.g / Mathf.Max(map.g, 0.02f) * num2, _maxEmission.Value) * _brightness.Value, Mathf.Min(color.b / Mathf.Max(map.b, 0.02f) * num2, _maxEmission.Value) * _brightness.Value, 1f);
			if (_hueCompensation.Value)
			{
				float num3 = HueGain(color);
				((Color)(ref val))..ctor(val.r * num3, val.g * num3, val.b * num3, 1f);
			}
			return val;
		}

		private static void NeutraliseParticles(GameObject item)
		{
			//IL_001a: Unknown result type (might be due to invalid IL or missing references)
			//IL_001f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0033: Unknown result type (might be due to invalid IL or missing references)
			//IL_0038: Unknown result type (might be due to invalid IL or missing references)
			//IL_003c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0046: Unknown result type (might be due to invalid IL or missing references)
			//IL_004b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0056: Unknown result type (might be due to invalid IL or missing references)
			//IL_005b: Unknown result type (might be due to invalid IL or missing references)
			//IL_006d: Unknown result type (might be due to invalid IL or missing references)
			ParticleSystem[] componentsInChildren = item.GetComponentsInChildren<ParticleSystem>(true);
			foreach (ParticleSystem val in componentsInChildren)
			{
				if (!((Object)(object)val == (Object)null))
				{
					MainModule main = val.main;
					MinMaxGradient startColor = ((MainModule)(ref main)).startColor;
					((MainModule)(ref main)).startColor = new MinMaxGradient(new Color(1f, 1f, 1f, ((MinMaxGradient)(ref startColor)).color.a));
					ColorOverLifetimeModule colorOverLifetime = val.colorOverLifetime;
					if (((ColorOverLifetimeModule)(ref colorOverLifetime)).enabled)
					{
						((ColorOverLifetimeModule)(ref colorOverLifetime)).color = new MinMaxGradient(WhiteGradient);
					}
				}
			}
		}

		private static void EnsureNeutralMainTex(Material material)
		{
			if (!material.HasProperty("_MainTex"))
			{
				return;
			}
			if ((Object)(object)_whiteGlow == (Object)null || Mathf.Abs(_whiteGlowRadius - _glowRadius.Value) > 0.001f)
			{
				_whiteGlow = MakeWhiteGlow();
				_whiteGlowRadius = _glowRadius.Value;
			}
			if ((Object)(object)_whiteGlow != (Object)null)
			{
				material.SetTexture("_MainTex", (Texture)(object)_whiteGlow);
			}
			ShaderMode value = _shaderMode.Value;
			if (value == ShaderMode.Vanilla)
			{
				if (OriginalShaders.TryGetValue(((Object)material).GetInstanceID(), out var value2) && (Object)(object)value2 != (Object)null)
				{
					material.shader = value2;
				}
				return;
			}
			if (!OriginalShaders.ContainsKey(((Object)material).GetInstanceID()))
			{
				OriginalShaders[((Object)material).GetInstanceID()] = material.shader;
			}
			Shader val = FindLoadedShader(value switch
			{
				ShaderMode.Additive => "Legacy Shaders/Particles/Additive", 
				ShaderMode.Alpha => "Custom/AlphaParticle", 
				_ => "Particles/Standard Unlit", 
			});
			if ((Object)(object)val != (Object)null && (Object)(object)material.shader != (Object)(object)val)
			{
				material.shader = val;
			}
		}

		private static Shader FindLoadedShader(string name)
		{
			try
			{
				Shader[] array = Resources.FindObjectsOfTypeAll<Shader>();
				foreach (Shader val in array)
				{
					if ((Object)(object)val != (Object)null && ((Object)val).name == name)
					{
						return val;
					}
				}
			}
			catch (Exception ex)
			{
				_log.LogDebug((object)("[RainbowGear] shader lookup failed: " + ex.Message));
			}
			return null;
		}

		private static Texture2D MakeWhiteGlow()
		{
			//IL_000c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0012: Expected O, but got Unknown
			//IL_0024: Unknown result type (might be due to invalid IL or missing references)
			//IL_002b: 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)
			Texture2D val = new Texture2D(128, 128, (TextureFormat)4, false);
			float num = 63.5f;
			for (int i = 0; i < 128; i++)
			{
				for (int j = 0; j < 128; j++)
				{
					float num2 = Vector2.Distance(new Vector2((float)j, (float)i), new Vector2(num, num)) / num;
					float num3 = Mathf.Clamp(_glowRadius.Value, 0.1f, 1f);
					float num4 = Mathf.Clamp01((num3 - num2) / num3);
					num4 = Mathf.Pow(num4, 1.5f);
					val.SetPixel(j, i, new Color(num4, num4, num4, num4));
				}
			}
			val.Apply();
			return val;
		}

		private static void UpdateDualGlow(GameObject item, Color headColor)
		{
			//IL_0078: Unknown result type (might be due to invalid IL or missing references)
			//IL_007d: Unknown result type (might be due to invalid IL or missing references)
			//IL_007f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0084: Unknown result type (might be due to invalid IL or missing references)
			//IL_0088: Unknown result type (might be due to invalid IL or missing references)
			//IL_0093: Unknown result type (might be due to invalid IL or missing references)
			//IL_0098: Unknown result type (might be due to invalid IL or missing references)
			//IL_009c: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a7: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ac: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b0: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c7: 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_00d0: Unknown result type (might be due to invalid IL or missing references)
			//IL_00db: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e0: 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_00ef: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f4: 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_0115: Unknown result type (might be due to invalid IL or missing references)
			//IL_011a: Unknown result type (might be due to invalid IL or missing references)
			//IL_011e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0130: Unknown result type (might be due to invalid IL or missing references)
			//IL_013d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0142: Unknown result type (might be due to invalid IL or missing references)
			//IL_0146: Unknown result type (might be due to invalid IL or missing references)
			//IL_0162: Unknown result type (might be due to invalid IL or missing references)
			if (!_dualGlow.Value)
			{
				RemoveDualGlow(item);
				return;
			}
			DualGlow dualGlow = EnsureDualGlow(item);
			if (!((Object)(object)dualGlow.Head == (Object)null) && !((Object)(object)dualGlow.Grip == (Object)null))
			{
				Renderer val = LargestRenderer(item);
				Renderer val2 = SmallestRenderer(item);
				if (!((Object)(object)val == (Object)null) && !((Object)(object)val2 == (Object)null))
				{
					Color color = SamplePalette(Mathf.Repeat(Time.time * _cycleSpeed.Value + 0.5f, 1f));
					Bounds bounds = val.bounds;
					float x = ((Bounds)(ref bounds)).size.x;
					bounds = val.bounds;
					float y = ((Bounds)(ref bounds)).size.y;
					bounds = val.bounds;
					float num = Mathf.Max(x, Mathf.Max(y, ((Bounds)(ref bounds)).size.z));
					bounds = val2.bounds;
					float x2 = ((Bounds)(ref bounds)).size.x;
					bounds = val2.bounds;
					float y2 = ((Bounds)(ref bounds)).size.y;
					bounds = val2.bounds;
					float num2 = Mathf.Max(x2, Mathf.Max(y2, ((Bounds)(ref bounds)).size.z));
					SpriteRenderer head = dualGlow.Head;
					bounds = val.bounds;
					PlaceGlow(head, ((Bounds)(ref bounds)).center, num * _dualScale.Value, headColor);
					SpriteRenderer grip = dualGlow.Grip;
					bounds = val2.bounds;
					PlaceGlow(grip, ((Bounds)(ref bounds)).center, Mathf.Max(num2 * _dualScale.Value, 0.25f), color);
				}
			}
		}

		private static void PlaceGlow(SpriteRenderer renderer, Vector3 position, float size, Color color)
		{
			//IL_0008: 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_001f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0059: Unknown result type (might be due to invalid IL or missing references)
			//IL_0064: Unknown result type (might be due to invalid IL or missing references)
			//IL_0069: 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_0039: Unknown result type (might be due to invalid IL or missing references)
			//IL_0040: Unknown result type (might be due to invalid IL or missing references)
			//IL_0045: Unknown result type (might be due to invalid IL or missing references)
			//IL_004a: Unknown result type (might be due to invalid IL or missing references)
			//IL_004f: Unknown result type (might be due to invalid IL or missing references)
			Transform transform = ((Component)renderer).transform;
			transform.position = position;
			transform.localScale = Vector3.one * Mathf.Max(0.1f, size);
			Camera main = Camera.main;
			if ((Object)(object)main != (Object)null)
			{
				transform.rotation = Quaternion.LookRotation(position - ((Component)main).transform.position, Vector3.up);
			}
			Color color2 = color * _brightness.Value;
			color2.a = _dualAlpha.Value;
			renderer.color = color2;
		}

		private static DualGlow EnsureDualGlow(GameObject item)
		{
			//IL_008a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0099: Unknown result type (might be due to invalid IL or missing references)
			int instanceID = ((Object)item).GetInstanceID();
			if (_dualGlows.TryGetValue(instanceID, out var value) && (Object)(object)value.Head != (Object)null && (Object)(object)value.Grip != (Object)null)
			{
				return value;
			}
			if ((Object)(object)_whiteGlow == (Object)null)
			{
				_whiteGlow = MakeWhiteGlow();
			}
			if ((Object)(object)_glowSprite == (Object)null && (Object)(object)_whiteGlow != (Object)null)
			{
				_glowSprite = Sprite.Create(_whiteGlow, new Rect(0f, 0f, (float)((Texture)_whiteGlow).width, (float)((Texture)_whiteGlow).height), new Vector2(0.5f, 0.5f), (float)((Texture)_whiteGlow).width);
			}
			value = new DualGlow
			{
				Head = CreateGlowObject(item, "RainbowGear_HeadGlow"),
				Grip = CreateGlowObject(item, "RainbowGear_GripGlow")
			};
			_dualGlows[instanceID] = value;
			return value;
		}

		private static SpriteRenderer CreateGlowObject(GameObject item, string name)
		{
			//IL_0001: Unknown result type (might be due to invalid IL or missing references)
			//IL_0006: Unknown result type (might be due to invalid IL or missing references)
			//IL_005f: Unknown result type (might be due to invalid IL or missing references)
			//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_005e: Expected O, but got Unknown
			GameObject val = new GameObject(name);
			val.transform.SetParent(item.transform, false);
			SpriteRenderer val2 = val.AddComponent<SpriteRenderer>();
			val2.sprite = _glowSprite;
			Shader val3 = FindLoadedShader("Legacy Shaders/Particles/Additive") ?? FindLoadedShader("Sprites/Default");
			if ((Object)(object)val3 != (Object)null)
			{
				((Renderer)val2).material = new Material(val3)
				{
					name = name
				};
			}
			val2.color = Color.white;
			return val2;
		}

		private static void RemoveDualGlow(GameObject item)
		{
			if (_dualGlows.TryGetValue(((Object)item).GetInstanceID(), out var value))
			{
				if ((Object)(object)value.Head != (Object)null)
				{
					Object.Destroy((Object)(object)((Component)value.Head).gameObject);
				}
				if ((Object)(object)value.Grip != (Object)null)
				{
					Object.Destroy((Object)(object)((Component)value.Grip).gameObject);
				}
				_dualGlows.Remove(((Object)item).GetInstanceID());
			}
		}

		private static Renderer LargestRenderer(GameObject item)
		{
			//IL_0037: Unknown result type (might be due to invalid IL or missing references)
			//IL_003c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0040: Unknown result type (might be due to invalid IL or missing references)
			//IL_0045: Unknown result type (might be due to invalid IL or missing references)
			//IL_0047: Unknown result type (might be due to invalid IL or missing references)
			//IL_004e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0056: Unknown result type (might be due to invalid IL or missing references)
			Renderer result = null;
			float num = 0f;
			Renderer[] componentsInChildren = item.GetComponentsInChildren<Renderer>(true);
			foreach (Renderer val in componentsInChildren)
			{
				if (!((Object)(object)val == (Object)null) && !(val is SpriteRenderer) && !(val is ParticleSystemRenderer))
				{
					Bounds bounds = val.bounds;
					Vector3 size = ((Bounds)(ref bounds)).size;
					float num2 = size.x * size.y * size.z;
					if (num2 > num)
					{
						num = num2;
						result = val;
					}
				}
			}
			return result;
		}

		private static Renderer SmallestRenderer(GameObject item)
		{
			//IL_0037: Unknown result type (might be due to invalid IL or missing references)
			//IL_003c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0040: Unknown result type (might be due to invalid IL or missing references)
			//IL_0045: Unknown result type (might be due to invalid IL or missing references)
			//IL_0047: Unknown result type (might be due to invalid IL or missing references)
			//IL_004e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0056: Unknown result type (might be due to invalid IL or missing references)
			Renderer result = null;
			float num = float.MaxValue;
			Renderer[] componentsInChildren = item.GetComponentsInChildren<Renderer>(true);
			foreach (Renderer val in componentsInChildren)
			{
				if (!((Object)(object)val == (Object)null) && !(val is SpriteRenderer) && !(val is ParticleSystemRenderer))
				{
					Bounds bounds = val.bounds;
					Vector3 size = ((Bounds)(ref bounds)).size;
					float num2 = size.x * size.y * size.z;
					if (num2 > 1E-05f && num2 < num)
					{
						num = num2;
						result = val;
					}
				}
			}
			return result;
		}

		private static void Apply(GameObject item, int quality, Color color)
		{
			//IL_001e: Unknown result type (might be due to invalid IL or missing references)
			//IL_006a: Unknown result type (might be due to invalid IL or missing references)
			//IL_006f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0071: Unknown result type (might be due to invalid IL or missing references)
			//IL_0072: Unknown result type (might be due to invalid IL or missing references)
			//IL_0074: Unknown result type (might be due to invalid IL or missing references)
			//IL_0079: 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_008e: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a8: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c1: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d2: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e3: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f9: Unknown result type (might be due to invalid IL or missing references)
			//IL_0179: Unknown result type (might be due to invalid IL or missing references)
			//IL_017f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0185: Unknown result type (might be due to invalid IL or missing references)
			//IL_0199: Unknown result type (might be due to invalid IL or missing references)
			//IL_019e: Unknown result type (might be due to invalid IL or missing references)
			if ((Object)(object)item == (Object)null || quality < _minQuality.Value)
			{
				return;
			}
			NeutraliseParticles(item);
			UpdateDualGlow(item, color);
			ParticleSystemRenderer[] componentsInChildren = item.GetComponentsInChildren<ParticleSystemRenderer>(true);
			foreach (ParticleSystemRenderer val in componentsInChildren)
			{
				if ((Object)(object)val == (Object)null)
				{
					continue;
				}
				Material material = ((Renderer)val).material;
				if (!((Object)(object)material == (Object)null))
				{
					EnsureNeutralMainTex(material);
					if (!_mapSampled)
					{
						TrySampleMap(material);
					}
					Color map = ResolveMap(material);
					Color val2 = ComputeEmission(color, map);
					if (material.HasProperty("_EmissionColor"))
					{
						material.SetColor("_EmissionColor", val2);
					}
					if (material.HasProperty("_Color"))
					{
						material.SetColor("_Color", color);
					}
					if (material.HasProperty("_TintColor"))
					{
						material.SetColor("_TintColor", new Color(color.r * _brightness.Value, color.g * _brightness.Value, color.b * _brightness.Value, 1f));
					}
				}
			}
			Light[] componentsInChildren2 = item.GetComponentsInChildren<Light>(true);
			foreach (Light val3 in componentsInChildren2)
			{
				if ((Object)(object)val3 != (Object)null)
				{
					val3.color = color;
				}
			}
			MeleeWeaponTrail[] componentsInChildren3 = item.GetComponentsInChildren<MeleeWeaponTrail>(true);
			foreach (MeleeWeaponTrail val4 in componentsInChildren3)
			{
				if ((Object)(object)val4 == (Object)null)
				{
					continue;
				}
				Color[] array = TrailColors.Invoke(val4);
				if (array != null)
				{
					for (int j = 0; j < array.Length; j++)
					{
						array[j] = new Color(color.r, color.g, color.b, array[j].a);
					}
				}
			}
		}
	}
}