Decompiled source of Blackfyre Sword v1.2.2

plugins/BlackfyreRecolor.dll

Decompiled 3 days ago
using System;
using System.Collections;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using Microsoft.CodeAnalysis;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyCompany("BlackfyreRecolor")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("BlackfyreRecolor")]
[assembly: AssemblyTitle("BlackfyreRecolor")]
[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 BlackfyreRecolor
{
	[BepInPlugin("zoey.blackfyre.recolor", "Blackfyre Blue Flame V2", "1.2.1")]
	public class BlackfyreRecolorPlugin : BaseUnityPlugin
	{
		public const string ModGuid = "zoey.blackfyre.recolor";

		public const string ModName = "Blackfyre Blue Flame V2";

		public const string ModVersion = "1.2.1";

		private const string TargetPrefabName = "BlackfyreSword";

		private const string FlameDonorPrefab = "vfx_Burning_blue";

		private bool _prefabPatched;

		private Material _cachedBlueFlameMaterial;

		private ParticleSystem _cachedDonorParticle;

		private static readonly Color CoreBlue = new Color(0.78f, 0.94f, 1f, 1f);

		private static readonly Color MidBlue = new Color(0.08f, 0.45f, 1f, 0.85f);

		private static readonly Color DeepBlue = new Color(0.01f, 0.08f, 0.42f, 0f);

		private void Awake()
		{
			((BaseUnityPlugin)this).Logger.LogInfo((object)"Blackfyre Blue Flame V2 loaded.");
			((MonoBehaviour)this).StartCoroutine(PatchLoop());
		}

		private IEnumerator PatchLoop()
		{
			yield return (object)new WaitForSeconds(2f);
			while (true)
			{
				try
				{
					CacheVanillaBlueFlame();
					PatchPrefab();
				}
				catch (Exception ex)
				{
					((BaseUnityPlugin)this).Logger.LogError((object)("Blackfyre Blue Flame V2 failed: " + ex));
				}
				yield return (object)new WaitForSeconds(_prefabPatched ? 10f : 2f);
			}
		}

		private void CacheVanillaBlueFlame()
		{
			//IL_00aa: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b4: Expected O, but got Unknown
			if ((Object)(object)_cachedBlueFlameMaterial != (Object)null)
			{
				return;
			}
			GameObject val = FindPrefabByName("vfx_Burning_blue");
			if ((Object)(object)val == (Object)null)
			{
				return;
			}
			ParticleSystemRenderer[] componentsInChildren = val.GetComponentsInChildren<ParticleSystemRenderer>(true);
			foreach (ParticleSystemRenderer val2 in componentsInChildren)
			{
				if ((Object)(object)val2 == (Object)null || (Object)(object)((Renderer)val2).sharedMaterial == (Object)null)
				{
					continue;
				}
				Material sharedMaterial = ((Renderer)val2).sharedMaterial;
				Texture val3 = null;
				if (sharedMaterial.HasProperty("_MainTex"))
				{
					val3 = sharedMaterial.GetTexture("_MainTex");
				}
				if ((Object)(object)val3 == (Object)null)
				{
					val3 = sharedMaterial.mainTexture;
				}
				if (!((Object)(object)val3 == (Object)null) || !((Object)(object)_cachedBlueFlameMaterial != (Object)null))
				{
					_cachedBlueFlameMaterial = new Material(sharedMaterial);
					((Object)_cachedBlueFlameMaterial).name = "Blackfyre_BlueFlame_FromVanilla";
					ParticleSystem component = ((Component)val2).GetComponent<ParticleSystem>();
					if ((Object)(object)component != (Object)null)
					{
						_cachedDonorParticle = component;
					}
					((BaseUnityPlugin)this).Logger.LogInfo((object)("Borrowed blue flame particle material from vfx_Burning_blue: " + ((Object)sharedMaterial).name));
					if ((Object)(object)val3 != (Object)null)
					{
						break;
					}
				}
			}
		}

		private void PatchPrefab()
		{
			GameObject val = FindPrefabByName("BlackfyreSword");
			if ((Object)(object)val == (Object)null)
			{
				return;
			}
			if ((Object)(object)val.GetComponent<BlackfyreBlueFlameMarker>() != (Object)null)
			{
				_prefabPatched = true;
				return;
			}
			Transform val2 = FindBestSwordVisual(val);
			if ((Object)(object)val2 == (Object)null)
			{
				((BaseUnityPlugin)this).Logger.LogWarning((object)"BlackfyreSword found, but no suitable mesh visual was found.");
				return;
			}
			AddBlueFlameSystem(val2);
			AddBlueBladeLight(val2);
			val.AddComponent<BlackfyreBlueFlameMarker>();
			_prefabPatched = true;
			((BaseUnityPlugin)this).Logger.LogInfo((object)"Applied textured blue flame aura to BlackfyreSword.");
		}

		private GameObject FindPrefabByName(string name)
		{
			if ((Object)(object)ObjectDB.instance != (Object)null)
			{
				GameObject itemPrefab = ObjectDB.instance.GetItemPrefab(name);
				if ((Object)(object)itemPrefab != (Object)null)
				{
					return itemPrefab;
				}
			}
			if ((Object)(object)ZNetScene.instance != (Object)null)
			{
				GameObject prefab = ZNetScene.instance.GetPrefab(name);
				if ((Object)(object)prefab != (Object)null)
				{
					return prefab;
				}
			}
			GameObject[] array = Resources.FindObjectsOfTypeAll<GameObject>();
			foreach (GameObject val in array)
			{
				if ((Object)(object)val != (Object)null && string.Equals(((Object)val).name, name, StringComparison.OrdinalIgnoreCase))
				{
					return val;
				}
			}
			return null;
		}

		private Transform FindBestSwordVisual(GameObject root)
		{
			//IL_0058: Unknown result type (might be due to invalid IL or missing references)
			//IL_005d: 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)
			//IL_0066: Unknown result type (might be due to invalid IL or missing references)
			MeshFilter[] componentsInChildren = root.GetComponentsInChildren<MeshFilter>(true);
			MeshFilter val = null;
			float num = -1f;
			MeshFilter[] array = componentsInChildren;
			foreach (MeshFilter val2 in array)
			{
				if ((Object)(object)val2 == (Object)null || (Object)(object)val2.sharedMesh == (Object)null)
				{
					continue;
				}
				Renderer component = ((Component)val2).GetComponent<Renderer>();
				if ((Object)(object)component == (Object)null)
				{
					continue;
				}
				Bounds bounds = val2.sharedMesh.bounds;
				Vector3 size = ((Bounds)(ref bounds)).size;
				float num2 = ((Vector3)(ref size)).magnitude;
				Material[] sharedMaterials = component.sharedMaterials;
				foreach (Material val3 in sharedMaterials)
				{
					if (!((Object)(object)val3 == (Object)null) && ((Object)val3).name != null && (((Object)val3).name.IndexOf("Niedhogg", StringComparison.OrdinalIgnoreCase) >= 0 || ((Object)val3).name.IndexOf("Nidhogg", StringComparison.OrdinalIgnoreCase) >= 0))
					{
						num2 += 1000f;
						break;
					}
				}
				if (num2 > num)
				{
					num = num2;
					val = val2;
				}
			}
			if (!((Object)(object)val != (Object)null))
			{
				return root.transform;
			}
			return ((Component)val).transform;
		}

		private void AddBlueFlameSystem(Transform target)
		{
			//IL_001e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0032: 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)
			//IL_0049: Unknown result type (might be due to invalid IL or missing references)
			//IL_004c: 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_0067: 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_00b9: Unknown result type (might be due to invalid IL or missing references)
			//IL_00be: Unknown result type (might be due to invalid IL or missing references)
			//IL_00cf: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d6: Expected O, but got Unknown
			//IL_00eb: 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_010e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0113: Unknown result type (might be due to invalid IL or missing references)
			//IL_0139: Unknown result type (might be due to invalid IL or missing references)
			//IL_014f: 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_0171: Unknown result type (might be due to invalid IL or missing references)
			//IL_0176: Unknown result type (might be due to invalid IL or missing references)
			//IL_017b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0193: Unknown result type (might be due to invalid IL or missing references)
			//IL_0198: Unknown result type (might be due to invalid IL or missing references)
			//IL_01a9: Unknown result type (might be due to invalid IL or missing references)
			//IL_01b5: Unknown result type (might be due to invalid IL or missing references)
			//IL_01ba: Unknown result type (might be due to invalid IL or missing references)
			//IL_01d5: Unknown result type (might be due to invalid IL or missing references)
			//IL_01f1: Unknown result type (might be due to invalid IL or missing references)
			//IL_020d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0233: Unknown result type (might be due to invalid IL or missing references)
			//IL_023c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0241: Unknown result type (might be due to invalid IL or missing references)
			//IL_0252: Unknown result type (might be due to invalid IL or missing references)
			//IL_026f: Unknown result type (might be due to invalid IL or missing references)
			//IL_027b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0280: Unknown result type (might be due to invalid IL or missing references)
			//IL_028a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0291: Expected O, but got Unknown
			//IL_02aa: Unknown result type (might be due to invalid IL or missing references)
			//IL_02b4: Unknown result type (might be due to invalid IL or missing references)
			//IL_02b9: Unknown result type (might be due to invalid IL or missing references)
			//IL_02cf: Unknown result type (might be due to invalid IL or missing references)
			//IL_02d9: Unknown result type (might be due to invalid IL or missing references)
			//IL_02de: Unknown result type (might be due to invalid IL or missing references)
			//IL_02f4: Unknown result type (might be due to invalid IL or missing references)
			//IL_02fe: Unknown result type (might be due to invalid IL or missing references)
			//IL_0303: Unknown result type (might be due to invalid IL or missing references)
			//IL_031a: Unknown result type (might be due to invalid IL or missing references)
			//IL_031f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0330: Unknown result type (might be due to invalid IL or missing references)
			//IL_0335: Unknown result type (might be due to invalid IL or missing references)
			//IL_0346: Unknown result type (might be due to invalid IL or missing references)
			//IL_034b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0359: Unknown result type (might be due to invalid IL or missing references)
			//IL_0382: Unknown result type (might be due to invalid IL or missing references)
			//IL_038c: Expected O, but got Unknown
			//IL_03d0: Unknown result type (might be due to invalid IL or missing references)
			//IL_03d7: Expected O, but got Unknown
			//IL_03f8: Unknown result type (might be due to invalid IL or missing references)
			//IL_0417: Unknown result type (might be due to invalid IL or missing references)
			MeshFilter component = ((Component)target).GetComponent<MeshFilter>();
			Bounds val = (Bounds)(((Object)(object)component != (Object)null && (Object)(object)component.sharedMesh != (Object)null) ? component.sharedMesh.bounds : new Bounds(Vector3.zero, new Vector3(0.12f, 1.8f, 0.12f)));
			int axis = LongestAxis(((Bounds)(ref val)).size);
			float axis2 = GetAxis(((Bounds)(ref val)).min, axis);
			float axis3 = GetAxis(((Bounds)(ref val)).max, axis);
			float num = ((Mathf.Abs(axis3) >= Mathf.Abs(axis2)) ? axis3 : axis2);
			float num2 = num * 0.16f;
			float num3 = num * 0.97f;
			float value = (num2 + num3) * 0.5f;
			float value2 = Mathf.Abs(num3 - num2);
			Vector3 v = ((Bounds)(ref val)).center;
			SetAxis(ref v, axis, value);
			GameObject val2 = new GameObject("Blackfyre_BlueFlameAura_V2");
			val2.transform.SetParent(target, false);
			val2.transform.localPosition = v;
			val2.transform.localRotation = Quaternion.identity;
			ParticleSystem val3 = val2.AddComponent<ParticleSystem>();
			MainModule main = val3.main;
			((MainModule)(ref main)).loop = true;
			((MainModule)(ref main)).playOnAwake = true;
			((MainModule)(ref main)).simulationSpace = (ParticleSystemSimulationSpace)0;
			((MainModule)(ref main)).startLifetime = new MinMaxCurve(0.28f, 0.62f);
			((MainModule)(ref main)).startSpeed = new MinMaxCurve(0.015f, 0.075f);
			((MainModule)(ref main)).startSize = new MinMaxCurve(0.055f, 0.14f);
			((MainModule)(ref main)).startColor = new MinMaxGradient(CoreBlue, MidBlue);
			((MainModule)(ref main)).maxParticles = 150;
			EmissionModule emission = val3.emission;
			((EmissionModule)(ref emission)).enabled = true;
			((EmissionModule)(ref emission)).rateOverTime = MinMaxCurve.op_Implicit(46f);
			ShapeModule shape = val3.shape;
			((ShapeModule)(ref shape)).enabled = true;
			((ShapeModule)(ref shape)).shapeType = (ParticleSystemShapeType)5;
			Vector3 v2 = default(Vector3);
			((Vector3)(ref v2))..ctor(Mathf.Max(0.045f, ((Bounds)(ref val)).size.x * 0.18f), Mathf.Max(0.045f, ((Bounds)(ref val)).size.y * 0.18f), Mathf.Max(0.045f, ((Bounds)(ref val)).size.z * 0.18f));
			SetAxis(ref v2, axis, value2);
			((ShapeModule)(ref shape)).scale = v2;
			NoiseModule noise = val3.noise;
			((NoiseModule)(ref noise)).enabled = true;
			((NoiseModule)(ref noise)).strength = MinMaxCurve.op_Implicit(0.055f);
			((NoiseModule)(ref noise)).frequency = 0.9f;
			((NoiseModule)(ref noise)).scrollSpeed = MinMaxCurve.op_Implicit(0.24f);
			ColorOverLifetimeModule colorOverLifetime = val3.colorOverLifetime;
			((ColorOverLifetimeModule)(ref colorOverLifetime)).enabled = true;
			Gradient val4 = new Gradient();
			val4.SetKeys((GradientColorKey[])(object)new GradientColorKey[3]
			{
				new GradientColorKey(new Color(0.9f, 0.98f, 1f), 0f),
				new GradientColorKey(new Color(0.1f, 0.55f, 1f), 0.32f),
				new GradientColorKey(new Color(0.01f, 0.12f, 0.62f), 1f)
			}, (GradientAlphaKey[])(object)new GradientAlphaKey[3]
			{
				new GradientAlphaKey(0.8f, 0f),
				new GradientAlphaKey(0.6f, 0.35f),
				new GradientAlphaKey(0f, 1f)
			});
			((ColorOverLifetimeModule)(ref colorOverLifetime)).color = new MinMaxGradient(val4);
			ParticleSystemRenderer component2 = val2.GetComponent<ParticleSystemRenderer>();
			if ((Object)(object)_cachedBlueFlameMaterial != (Object)null)
			{
				((Renderer)component2).material = new Material(_cachedBlueFlameMaterial);
				((Object)((Renderer)component2).material).name = "Blackfyre_BlueFlame_RuntimeClone";
			}
			else
			{
				Shader val5 = Shader.Find("Legacy Shaders/Particles/Additive");
				if ((Object)(object)val5 == (Object)null)
				{
					val5 = Shader.Find("Particles/Standard Unlit");
				}
				if ((Object)(object)val5 != (Object)null)
				{
					Material val6 = new Material(val5);
					((Object)val6).name = "Blackfyre_BlueFlame_Fallback";
					if (val6.HasProperty("_TintColor"))
					{
						val6.SetColor("_TintColor", CoreBlue);
					}
					if (val6.HasProperty("_Color"))
					{
						val6.SetColor("_Color", CoreBlue);
					}
					((Renderer)component2).material = val6;
				}
			}
			CopyTextureSheetAnimation(_cachedDonorParticle, val3);
			val3.Play(true);
		}

		private void CopyTextureSheetAnimation(ParticleSystem donor, ParticleSystem target)
		{
			//IL_0015: Unknown result type (might be due to invalid IL or missing references)
			//IL_001a: Unknown result type (might be due to invalid IL or missing references)
			//IL_002a: Unknown result type (might be due to invalid IL or missing references)
			//IL_002f: 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_0066: 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_0082: Unknown result type (might be due to invalid IL or missing references)
			if ((Object)(object)donor == (Object)null || (Object)(object)target == (Object)null)
			{
				return;
			}
			try
			{
				TextureSheetAnimationModule textureSheetAnimation = donor.textureSheetAnimation;
				if (((TextureSheetAnimationModule)(ref textureSheetAnimation)).enabled)
				{
					TextureSheetAnimationModule textureSheetAnimation2 = target.textureSheetAnimation;
					((TextureSheetAnimationModule)(ref textureSheetAnimation2)).enabled = true;
					((TextureSheetAnimationModule)(ref textureSheetAnimation2)).mode = ((TextureSheetAnimationModule)(ref textureSheetAnimation)).mode;
					((TextureSheetAnimationModule)(ref textureSheetAnimation2)).numTilesX = ((TextureSheetAnimationModule)(ref textureSheetAnimation)).numTilesX;
					((TextureSheetAnimationModule)(ref textureSheetAnimation2)).numTilesY = ((TextureSheetAnimationModule)(ref textureSheetAnimation)).numTilesY;
					((TextureSheetAnimationModule)(ref textureSheetAnimation2)).animation = ((TextureSheetAnimationModule)(ref textureSheetAnimation)).animation;
					((TextureSheetAnimationModule)(ref textureSheetAnimation2)).frameOverTime = ((TextureSheetAnimationModule)(ref textureSheetAnimation)).frameOverTime;
					((TextureSheetAnimationModule)(ref textureSheetAnimation2)).startFrame = ((TextureSheetAnimationModule)(ref textureSheetAnimation)).startFrame;
					((TextureSheetAnimationModule)(ref textureSheetAnimation2)).cycleCount = ((TextureSheetAnimationModule)(ref textureSheetAnimation)).cycleCount;
					((TextureSheetAnimationModule)(ref textureSheetAnimation2)).useRandomRow = ((TextureSheetAnimationModule)(ref textureSheetAnimation)).useRandomRow;
					((TextureSheetAnimationModule)(ref textureSheetAnimation2)).rowIndex = ((TextureSheetAnimationModule)(ref textureSheetAnimation)).rowIndex;
					((BaseUnityPlugin)this).Logger.LogInfo((object)"Copied vanilla blue-flame texture-sheet animation.");
				}
			}
			catch (Exception ex)
			{
				((BaseUnityPlugin)this).Logger.LogWarning((object)("Could not copy texture-sheet animation: " + ex.Message));
			}
		}

		private void AddBlueBladeLight(Transform target)
		{
			//IL_0005: Unknown result type (might be due to invalid IL or missing references)
			//IL_000a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0017: Unknown result type (might be due to invalid IL or missing references)
			//IL_001d: 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)
			GameObject val = new GameObject("Blackfyre_BlueBladeLight_V2");
			val.transform.SetParent(target, false);
			val.transform.localPosition = Vector3.zero;
			Light obj = val.AddComponent<Light>();
			obj.type = (LightType)2;
			obj.color = new Color(0.08f, 0.42f, 1f);
			obj.range = 3.2f;
			obj.intensity = 0.95f;
			obj.shadows = (LightShadows)0;
		}

		private static int LongestAxis(Vector3 size)
		{
			//IL_0000: 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_001e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0024: Unknown result type (might be due to invalid IL or missing references)
			//IL_000e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0014: Unknown result type (might be due to invalid IL or missing references)
			//IL_002c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0032: Unknown result type (might be due to invalid IL or missing references)
			if (size.y > size.x && size.y >= size.z)
			{
				return 1;
			}
			if (size.z > size.x && size.z > size.y)
			{
				return 2;
			}
			return 0;
		}

		private static float GetAxis(Vector3 v, int axis)
		{
			//IL_0015: Unknown result type (might be due to invalid IL or missing references)
			//IL_000e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0007: Unknown result type (might be due to invalid IL or missing references)
			return axis switch
			{
				1 => v.y, 
				0 => v.x, 
				_ => v.z, 
			};
		}

		private static void SetAxis(ref Vector3 v, int axis, float value)
		{
			switch (axis)
			{
			case 0:
				v.x = value;
				break;
			case 1:
				v.y = value;
				break;
			default:
				v.z = value;
				break;
			}
		}
	}
	internal class BlackfyreBlueFlameMarker : MonoBehaviour
	{
	}
}