Decompiled source of BlueFlame v1.0.0

BlueFlame.dll

Decompiled 15 hours ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using UnityEngine;
using UnityEngine.UI;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyVersion("0.0.0.0")]
namespace BlueFlame;

[BepInPlugin("com.parallel53.blueflame", "BlueFlame", "1.0.0")]
public class BlueFlamePlugin : BaseUnityPlugin
{
	private static ConfigEntry<bool> Fires;

	private static ConfigEntry<bool> HeldTorch;

	private static ConfigEntry<bool> Portals;

	private static ConfigEntry<bool> CraftingStations;

	private static ConfigEntry<bool> TeleportScreen;

	private static ManualLogSource Log;

	private const string BlueTorch = "piece_groundtorch_blue";

	private const string BlueBrazier = "piece_brazierfloor02";

	private static readonly Type[] EffectOwners = new Type[5]
	{
		typeof(Fireplace),
		typeof(CraftingStation),
		typeof(Smelter),
		typeof(CookingStation),
		typeof(TeleportWorld)
	};

	private static readonly HashSet<GameObject> effectsDone = new HashSet<GameObject>();

	private static Color blueLight = new Color(0.48f, 0.77f, 1f);

	private static ParticleSystem blueFlame;

	private static Material blueFlameMaterial;

	private static readonly Dictionary<string, Material> blueMaterials = new Dictionary<string, Material>();

	private static readonly Dictionary<Sprite, Sprite> blueSprites = new Dictionary<Sprite, Sprite>();

	private void Awake()
	{
		//IL_00b0: Unknown result type (might be due to invalid IL or missing references)
		Log = ((BaseUnityPlugin)this).Logger;
		Fires = ((BaseUnityPlugin)this).Config.Bind<bool>("Blue", "Fires", true, "Campfires, hearths, bonfires, standing and wall torches, braziers and other fires you refuel. On by default.");
		HeldTorch = ((BaseUnityPlugin)this).Config.Bind<bool>("Blue", "HeldTorch", true, "The torch you carry. On by default.");
		Portals = ((BaseUnityPlugin)this).Config.Bind<bool>("Blue", "Portals", true, "Portal fire and glow. On by default.");
		CraftingStations = ((BaseUnityPlugin)this).Config.Bind<bool>("Blue", "CraftingStations", true, "Forges, smelters, kilns, blast furnaces, ovens, cauldrons and other crafting and cooking stations, including the bursts of fire while you craft. On by default.");
		TeleportScreen = ((BaseUnityPlugin)this).Config.Bind<bool>("Blue", "TeleportScreen", true, "The swirl you see while teleporting. On by default.");
		new Harmony("com.parallel53.blueflame").PatchAll(typeof(BlueFlamePlugin));
	}

	[HarmonyPatch(typeof(ZNetScene), "Awake")]
	[HarmonyPostfix]
	private static void AfterSceneAwake(ZNetScene __instance)
	{
		ReadBlueTorch(__instance);
		effectsDone.Clear();
		int num = 0;
		if (Fires.Value || Portals.Value || CraftingStations.Value)
		{
			foreach (GameObject prefab2 in __instance.m_prefabs)
			{
				if (!((Object)(object)prefab2 == (Object)null) && ((Fires.Value && (Object)(object)prefab2.GetComponent<Fireplace>() != (Object)null) || (Portals.Value && (Object)(object)prefab2.GetComponent<TeleportWorld>() != (Object)null) || (CraftingStations.Value && ((Object)(object)prefab2.GetComponent<CraftingStation>() != (Object)null || (Object)(object)prefab2.GetComponent<Smelter>() != (Object)null || (Object)(object)prefab2.GetComponent<CookingStation>() != (Object)null))) && (Recolour(prefab2) | RecolourEffects(prefab2)))
				{
					num++;
				}
			}
		}
		if (HeldTorch.Value)
		{
			GameObject prefab = __instance.GetPrefab("Torch");
			if ((Object)(object)prefab != (Object)null && Recolour(prefab))
			{
				num++;
			}
		}
		Log.LogInfo((object)("Turned " + num + " kinds of fire blue"));
	}

	[HarmonyPatch(typeof(Hud), "Awake")]
	[HarmonyPostfix]
	private static void AfterHudAwake(Hud __instance)
	{
		if (!TeleportScreen.Value || (Object)(object)__instance.m_teleportingProgress == (Object)null)
		{
			return;
		}
		int num = 0;
		Image[] componentsInChildren = __instance.m_teleportingProgress.GetComponentsInChildren<Image>(true);
		foreach (Image val in componentsInChildren)
		{
			if (!((Object)(object)val.sprite == (Object)null))
			{
				if (!blueSprites.TryGetValue(val.sprite, out var value))
				{
					value = BlueCopy(val.sprite);
					blueSprites[val.sprite] = value;
				}
				if ((Object)(object)value != (Object)null)
				{
					val.sprite = value;
					num++;
				}
			}
		}
		Log.LogInfo((object)("Turned " + num + " layers of the teleport swirl blue"));
	}

	private static Sprite BlueCopy(Sprite sprite)
	{
		//IL_003c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0042: Expected O, but got Unknown
		//IL_005b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0087: 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_0093: Unknown result type (might be due to invalid IL or missing references)
		//IL_00d8: Unknown result type (might be due to invalid IL or missing references)
		//IL_00de: Unknown result type (might be due to invalid IL or missing references)
		//IL_00e9: Unknown result type (might be due to invalid IL or missing references)
		//IL_00ee: 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_0104: Unknown result type (might be due to invalid IL or missing references)
		//IL_0109: 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_0121: Unknown result type (might be due to invalid IL or missing references)
		Texture2D texture = sprite.texture;
		RenderTexture temporary = RenderTexture.GetTemporary(((Texture)texture).width, ((Texture)texture).height, 0, (RenderTextureFormat)0, (RenderTextureReadWrite)2);
		RenderTexture active = RenderTexture.active;
		Graphics.Blit((Texture)(object)texture, temporary);
		RenderTexture.active = temporary;
		Texture2D val = new Texture2D(((Texture)texture).width, ((Texture)texture).height, (TextureFormat)4, false);
		val.ReadPixels(new Rect(0f, 0f, (float)((Texture)texture).width, (float)((Texture)texture).height), 0, 0);
		RenderTexture.active = active;
		RenderTexture.ReleaseTemporary(temporary);
		Color[] pixels = val.GetPixels();
		bool changed = false;
		for (int i = 0; i < pixels.Length; i++)
		{
			pixels[i] = Shift(pixels[i], ref changed);
		}
		if (!changed)
		{
			Object.Destroy((Object)(object)val);
			return null;
		}
		val.SetPixels(pixels);
		val.Apply(false, true);
		((Object)val).name = ((Object)texture).name + "_blue";
		Rect rect = sprite.rect;
		float x = sprite.pivot.x;
		Rect rect2 = sprite.rect;
		float num = x / ((Rect)(ref rect2)).width;
		float y = sprite.pivot.y;
		rect2 = sprite.rect;
		Sprite obj = Sprite.Create(val, rect, new Vector2(num, y / ((Rect)(ref rect2)).height), sprite.pixelsPerUnit, 0u, (SpriteMeshType)0, sprite.border);
		((Object)obj).name = ((Object)sprite).name + "_blue";
		return obj;
	}

	private static void ReadBlueTorch(ZNetScene scene)
	{
		//IL_005d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0062: Unknown result type (might be due to invalid IL or missing references)
		//IL_0083: 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)
		GameObject prefab = scene.GetPrefab("piece_groundtorch_blue");
		Fireplace val = (((Object)(object)prefab != (Object)null) ? prefab.GetComponent<Fireplace>() : null);
		if ((Object)(object)val == (Object)null || (Object)(object)val.m_enabledObject == (Object)null)
		{
			Log.LogWarning((object)"No piece_groundtorch_blue; tinting flames instead of copying its colours");
			return;
		}
		Light componentInChildren = val.m_enabledObject.GetComponentInChildren<Light>(true);
		if ((Object)(object)componentInChildren != (Object)null)
		{
			blueLight = componentInChildren.color;
		}
		ParticleSystem[] componentsInChildren = val.m_enabledObject.GetComponentsInChildren<ParticleSystem>(true);
		foreach (ParticleSystem val2 in componentsInChildren)
		{
			CustomDataModule customData = val2.customData;
			if (((CustomDataModule)(ref customData)).enabled)
			{
				blueFlame = val2;
				ParticleSystemRenderer component = ((Component)val2).GetComponent<ParticleSystemRenderer>();
				blueFlameMaterial = (((Object)(object)component != (Object)null) ? ((Renderer)component).sharedMaterial : null);
				break;
			}
		}
		GameObject prefab2 = scene.GetPrefab("piece_brazierfloor02");
		if (!((Object)(object)prefab2 != (Object)null))
		{
			return;
		}
		Renderer[] componentsInChildren2 = prefab2.GetComponentsInChildren<Renderer>(true);
		for (int i = 0; i < componentsInChildren2.Length; i++)
		{
			Material[] sharedMaterials = componentsInChildren2[i].sharedMaterials;
			foreach (Material val3 in sharedMaterials)
			{
				if ((Object)(object)val3 != (Object)null && ((Object)val3).name.EndsWith("_blue"))
				{
					blueMaterials[((Object)val3).name] = val3;
				}
			}
		}
	}

	private static bool RecolourEffects(GameObject prefab)
	{
		//IL_0079: Unknown result type (might be due to invalid IL or missing references)
		//IL_0080: Expected O, but got Unknown
		bool flag = false;
		Type[] effectOwners = EffectOwners;
		foreach (Type type in effectOwners)
		{
			Component component = prefab.GetComponent(type);
			if ((Object)(object)component == (Object)null)
			{
				continue;
			}
			FieldInfo[] fields = type.GetFields(BindingFlags.Instance | BindingFlags.Public);
			foreach (FieldInfo fieldInfo in fields)
			{
				if (fieldInfo.FieldType != typeof(EffectList) || fieldInfo.Name == "m_fireworksEffects")
				{
					continue;
				}
				EffectList val = (EffectList)fieldInfo.GetValue(component);
				if (val == null || val.m_effectPrefabs == null)
				{
					continue;
				}
				EffectData[] effectPrefabs = val.m_effectPrefabs;
				foreach (EffectData val2 in effectPrefabs)
				{
					if (val2 != null && (Object)(object)val2.m_prefab != (Object)null && effectsDone.Add(val2.m_prefab))
					{
						flag |= Recolour(val2.m_prefab);
					}
				}
			}
		}
		return flag;
	}

	private static bool Recolour(GameObject prefab)
	{
		//IL_0028: 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_0034: Unknown result type (might be due to invalid IL or missing references)
		//IL_003b: 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_0047: 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_008d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0097: Unknown result type (might be due to invalid IL or missing references)
		if (((Object)prefab).name == "piece_groundtorch_blue")
		{
			return false;
		}
		bool changed = false;
		TeleportWorld component = prefab.GetComponent<TeleportWorld>();
		if ((Object)(object)component != (Object)null)
		{
			component.m_colorUnconnected = Shift(component.m_colorUnconnected, ref changed);
			component.m_colorTargetfound = Shift(component.m_colorTargetfound, ref changed);
		}
		Light[] componentsInChildren = prefab.GetComponentsInChildren<Light>(true);
		foreach (Light val in componentsInChildren)
		{
			if (IsWarm(val.color))
			{
				val.color = new Color(blueLight.r, blueLight.g, blueLight.b, val.color.a);
				changed = true;
			}
		}
		ParticleSystem[] componentsInChildren2 = prefab.GetComponentsInChildren<ParticleSystem>(true);
		foreach (ParticleSystem system in componentsInChildren2)
		{
			changed |= RecolourParticles(system);
		}
		Renderer[] componentsInChildren3 = prefab.GetComponentsInChildren<Renderer>(true);
		foreach (Renderer val2 in componentsInChildren3)
		{
			if (val2 is ParticleSystemRenderer)
			{
				continue;
			}
			Material[] sharedMaterials = val2.sharedMaterials;
			bool flag = false;
			for (int j = 0; j < sharedMaterials.Length; j++)
			{
				if ((Object)(object)sharedMaterials[j] != (Object)null && blueMaterials.TryGetValue(((Object)sharedMaterials[j]).name + "_blue", out var value))
				{
					sharedMaterials[j] = value;
					flag = true;
				}
			}
			if (flag)
			{
				val2.sharedMaterials = sharedMaterials;
				changed = true;
			}
		}
		return changed;
	}

	private static bool RecolourParticles(ParticleSystem system)
	{
		//IL_0003: Unknown result type (might be due to invalid IL or missing references)
		//IL_0008: Unknown result type (might be due to invalid IL or missing references)
		//IL_000b: 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_0029: Unknown result type (might be due to invalid IL or missing references)
		//IL_001b: 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_0035: 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_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_009c: Unknown result type (might be due to invalid IL or missing references)
		//IL_00a2: Invalid comparison between Unknown and I4
		//IL_00ab: Unknown result type (might be due to invalid IL or missing references)
		//IL_00bd: Unknown result type (might be due to invalid IL or missing references)
		//IL_00bf: 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_0181: Invalid comparison between Unknown and I4
		//IL_012b: 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_0139: Unknown result type (might be due to invalid IL or missing references)
		//IL_013f: Invalid comparison between Unknown and I4
		//IL_018e: Unknown result type (might be due to invalid IL or missing references)
		//IL_015a: Unknown result type (might be due to invalid IL or missing references)
		//IL_015f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0168: Unknown result type (might be due to invalid IL or missing references)
		bool result = false;
		MainModule main = system.main;
		if (Shift(((MainModule)(ref main)).startColor, out var result2))
		{
			((MainModule)(ref main)).startColor = result2;
			result = true;
		}
		ColorOverLifetimeModule colorOverLifetime = system.colorOverLifetime;
		if (((ColorOverLifetimeModule)(ref colorOverLifetime)).enabled && Shift(((ColorOverLifetimeModule)(ref colorOverLifetime)).color, out var result3))
		{
			((ColorOverLifetimeModule)(ref colorOverLifetime)).color = result3;
			result = true;
		}
		CustomDataModule customData = system.customData;
		if (!((CustomDataModule)(ref customData)).enabled)
		{
			return result;
		}
		ParticleSystemCustomData[] array = (ParticleSystemCustomData[])(object)new ParticleSystemCustomData[2]
		{
			default(ParticleSystemCustomData),
			(ParticleSystemCustomData)1
		};
		MinMaxGradient[] array2 = (MinMaxGradient[])(object)new MinMaxGradient[array.Length];
		bool flag = false;
		for (int i = 0; i < array.Length; i++)
		{
			array2[i] = ((CustomDataModule)(ref customData)).GetColor(array[i]);
			if ((int)((CustomDataModule)(ref customData)).GetMode(array[i]) == 2 && Shift(((CustomDataModule)(ref customData)).GetColor(array[i]), out var result4))
			{
				array2[i] = result4;
				flag = true;
			}
		}
		if (!flag)
		{
			return result;
		}
		ParticleSystemRenderer component = ((Component)system).GetComponent<ParticleSystemRenderer>();
		bool flag2 = (Object)(object)blueFlame != (Object)null && (Object)(object)blueFlameMaterial != (Object)null && (Object)(object)component != (Object)null && (Object)(object)((Renderer)component).sharedMaterial == (Object)(object)blueFlameMaterial;
		for (int j = 0; j < array.Length; j++)
		{
			if (flag2)
			{
				CustomDataModule customData2 = blueFlame.customData;
				if ((int)((CustomDataModule)(ref customData2)).GetMode(array[j]) == 2)
				{
					((CustomDataModule)(ref customData)).SetMode(array[j], (ParticleSystemCustomDataMode)2);
					int num = (int)array[j];
					customData2 = blueFlame.customData;
					((CustomDataModule)(ref customData)).SetColor((ParticleSystemCustomData)num, ((CustomDataModule)(ref customData2)).GetColor(array[j]));
					continue;
				}
			}
			if ((int)((CustomDataModule)(ref customData)).GetMode(array[j]) == 2)
			{
				((CustomDataModule)(ref customData)).SetColor(array[j], array2[j]);
			}
		}
		return true;
	}

	private static bool Shift(MinMaxGradient value, out MinMaxGradient result)
	{
		//IL_0001: Unknown result type (might be due to invalid IL or missing references)
		//IL_0002: Unknown result type (might be due to invalid IL or missing references)
		//IL_000b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0010: 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_002b: Expected I4, but got Unknown
		//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_003f: 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_0083: 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_0090: 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_0055: Unknown result type (might be due to invalid IL or missing references)
		//IL_005c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0063: Unknown result type (might be due to invalid IL or missing references)
		//IL_0068: 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)
		//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)
		result = value;
		bool changed = false;
		ParticleSystemGradientMode mode = ((MinMaxGradient)(ref value)).mode;
		switch ((int)mode)
		{
		case 0:
			result = new MinMaxGradient(Shift(((MinMaxGradient)(ref value)).color, ref changed));
			break;
		case 2:
			result = new MinMaxGradient(Shift(((MinMaxGradient)(ref value)).colorMin, ref changed), Shift(((MinMaxGradient)(ref value)).colorMax, ref changed));
			break;
		case 1:
		case 4:
			result = new MinMaxGradient(Shift(((MinMaxGradient)(ref value)).gradient, ref changed));
			((MinMaxGradient)(ref result)).mode = ((MinMaxGradient)(ref value)).mode;
			break;
		case 3:
			result = new MinMaxGradient(Shift(((MinMaxGradient)(ref value)).gradientMin, ref changed), Shift(((MinMaxGradient)(ref value)).gradientMax, ref changed));
			break;
		}
		return changed;
	}

	private static Gradient Shift(Gradient gradient, ref bool changed)
	{
		//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_0029: 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_003d: 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_0049: Unknown result type (might be due to invalid IL or missing references)
		//IL_0057: Expected O, but got Unknown
		if (gradient == null)
		{
			return null;
		}
		GradientColorKey[] colorKeys = gradient.colorKeys;
		for (int i = 0; i < colorKeys.Length; i++)
		{
			colorKeys[i].color = Shift(colorKeys[i].color, ref changed);
		}
		Gradient val = new Gradient
		{
			mode = gradient.mode
		};
		val.SetKeys(colorKeys, gradient.alphaKeys);
		return val;
	}

	private static Color Shift(Color colour, ref bool changed)
	{
		//IL_0000: 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_0008: 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_0050: Unknown result type (might be due to invalid IL or missing references)
		//IL_0053: 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)
		if (!IsWarm(colour))
		{
			return colour;
		}
		float num = default(float);
		float num2 = default(float);
		float num3 = default(float);
		Color.RGBToHSV(colour, ref num, ref num2, ref num3);
		if (num > 0.5f)
		{
			num -= 1f;
		}
		Color result = Color.HSVToRGB(Mathf.Clamp(0.555f - (num - 0.083f) * 0.6f, 0.5f, 0.68f), num2, num3, true);
		result.a = colour.a;
		changed = true;
		return result;
	}

	private static bool IsWarm(Color colour)
	{
		//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(colour, ref num, ref num2, ref num3);
		if (num2 > 0.15f && num3 > 0.05f)
		{
			if (!(num <= 0.19f))
			{
				return num >= 0.92f;
			}
			return true;
		}
		return false;
	}
}