Decompiled source of ValheimBathHearts v1.0.1

BepInEx/plugins/BathHearts/BathHearts.dll

Decompiled 2 weeks ago
using System;
using System.Collections;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("BathHearts")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("BathHearts")]
[assembly: AssemblyTitle("BathHearts")]
[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 ValheimBathHearts
{
	[BepInPlugin("com.custom.valheimbathhearts", "Valheim Bath Hearts", "1.0.0")]
	public sealed class BathHeartsPlugin : BaseUnityPlugin
	{
		public const string PluginGuid = "com.custom.valheimbathhearts";

		public const string PluginName = "Valheim Bath Hearts";

		public const string PluginVersion = "1.0.0";

		private static ManualLogSource Log;

		private static bool _prefabPatched;

		private static float _steamMultiplier = 2f;

		private static float _heartsPerSecond = 3.5f;

		internal static MethodInfo GetFuelMethod;

		private Harmony _harmony;

		private void Awake()
		{
			//IL_0081: Unknown result type (might be due to invalid IL or missing references)
			//IL_008b: Expected O, but got Unknown
			Log = ((BaseUnityPlugin)this).Logger;
			_steamMultiplier = Mathf.Clamp(((BaseUnityPlugin)this).Config.Bind<float>("Effects", "SteamMultiplier", 2f, "Множитель интенсивности оригинального пара ванны. Значение 2 = примерно вдвое больше.").Value, 1f, 4f);
			_heartsPerSecond = Mathf.Clamp(((BaseUnityPlugin)this).Config.Bind<float>("Effects", "HeartsPerSecond", 3.5f, "Частота появления розовых сердечек в секунду.").Value, 0.5f, 8f);
			_harmony = new Harmony("com.custom.valheimbathhearts");
			((MonoBehaviour)this).StartCoroutine(InstallPatchWhenReady());
			((BaseUnityPlugin)this).Logger.LogInfo((object)"Loaded. Waiting for ZNetScene to register prefabs.");
		}

		private IEnumerator InstallPatchWhenReady()
		{
			for (int i = 0; i < 1200; i++)
			{
				Type type = FindType("ZNetScene");
				if (type != null)
				{
					MethodInfo methodInfo = FindParameterlessMethod(type, "Awake");
					if (methodInfo != null)
					{
						try
						{
							_harmony.Patch((MethodBase)methodInfo, (HarmonyMethod)null, new HarmonyMethod(typeof(BathHeartsPlugin), "ZNetSceneAwakePostfix", (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
							((BaseUnityPlugin)this).Logger.LogInfo((object)"Hooked ZNetScene.Awake.");
							yield break;
						}
						catch (Exception ex)
						{
							((BaseUnityPlugin)this).Logger.LogError((object)("Could not hook ZNetScene.Awake: " + ex));
							yield break;
						}
					}
				}
				yield return null;
			}
			((BaseUnityPlugin)this).Logger.LogError((object)"ZNetScene.Awake was not found. The bathtub effects were not changed.");
		}

		internal static Type FindType(string fullName)
		{
			Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
			for (int i = 0; i < assemblies.Length; i++)
			{
				Type type = assemblies[i].GetType(fullName, throwOnError: false);
				if (type != null)
				{
					return type;
				}
			}
			return null;
		}

		internal static MethodInfo FindParameterlessMethod(Type type, string name)
		{
			Type type2 = type;
			while (type2 != null)
			{
				MethodInfo[] methods = type2.GetMethods(BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
				for (int i = 0; i < methods.Length; i++)
				{
					if (methods[i].Name == name && methods[i].GetParameters().Length == 0)
					{
						return methods[i];
					}
				}
				type2 = type2.BaseType;
			}
			return null;
		}

		private static void ZNetSceneAwakePostfix(object __instance)
		{
			if (_prefabPatched)
			{
				return;
			}
			try
			{
				GameObject val = FindPrefab(__instance, "piece_bathtub");
				if ((Object)(object)val == (Object)null)
				{
					Log.LogWarning((object)"Prefab piece_bathtub was not found in ZNetScene.m_prefabs.");
					return;
				}
				Transform val2 = FindChildRecursive(val.transform, "vfx_bath_mist");
				if ((Object)(object)val2 == (Object)null)
				{
					Log.LogWarning((object)"Child vfx_bath_mist was not found under piece_bathtub. No visual changes applied.");
					return;
				}
				int num = BoostSteam(((Component)val2).gameObject, _steamMultiplier);
				GameObject heartEmitter = AddHeartEmitter(val2, _heartsPerSecond);
				BathHeatGate bathHeatGate = val.GetComponent<BathHeatGate>();
				if ((Object)(object)bathHeatGate == (Object)null)
				{
					bathHeatGate = val.AddComponent<BathHeatGate>();
				}
				bathHeatGate.Initialize(((Component)val2).gameObject, heartEmitter);
				_prefabPatched = true;
				Log.LogInfo((object)("Bathtub prefab updated. Steam particle systems changed: " + num + "; hearts/sec: " + _heartsPerSecond.ToString("0.0") + ". Original bubbles were not modified."));
			}
			catch (Exception ex)
			{
				Log.LogError((object)("Could not modify bathtub effects: " + ex));
			}
		}

		private static GameObject FindPrefab(object znetScene, string prefabName)
		{
			if (znetScene == null)
			{
				return null;
			}
			FieldInfo fieldInfo = FindField(znetScene.GetType(), "m_prefabs");
			if (fieldInfo == null)
			{
				Log.LogWarning((object)"Field ZNetScene.m_prefabs was not found.");
				return null;
			}
			if (!(fieldInfo.GetValue(znetScene) is IEnumerable enumerable))
			{
				return null;
			}
			foreach (object item in enumerable)
			{
				GameObject val = (GameObject)((item is GameObject) ? item : null);
				if ((Object)(object)val == (Object)null && item != null)
				{
					PropertyInfo property = item.GetType().GetProperty("Value");
					if (property != null)
					{
						object? value = property.GetValue(item, null);
						val = (GameObject)((value is GameObject) ? value : null);
					}
				}
				if ((Object)(object)val != (Object)null && string.Equals(((Object)val).name, prefabName, StringComparison.Ordinal))
				{
					return val;
				}
			}
			return null;
		}

		private static FieldInfo FindField(Type type, string fieldName)
		{
			Type type2 = type;
			while (type2 != null)
			{
				FieldInfo field = type2.GetField(fieldName, BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
				if (field != null)
				{
					return field;
				}
				type2 = type2.BaseType;
			}
			return null;
		}

		internal static Transform FindChildRecursive(Transform root, string childName)
		{
			if ((Object)(object)root == (Object)null)
			{
				return null;
			}
			Transform[] componentsInChildren = ((Component)root).GetComponentsInChildren<Transform>(true);
			for (int i = 0; i < componentsInChildren.Length; i++)
			{
				if (string.Equals(((Object)componentsInChildren[i]).name, childName, StringComparison.Ordinal))
				{
					return componentsInChildren[i];
				}
			}
			return null;
		}

		private static int BoostSteam(GameObject steamRoot, float multiplier)
		{
			//IL_001c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0021: Unknown result type (might be due to invalid IL or missing references)
			//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)
			ParticleSystem[] componentsInChildren = steamRoot.GetComponentsInChildren<ParticleSystem>(true);
			int num = 0;
			foreach (ParticleSystem val in componentsInChildren)
			{
				if (!((Object)(object)val == (Object)null))
				{
					EmissionModule emission = val.emission;
					((EmissionModule)(ref emission)).rateOverTimeMultiplier = Mathf.Max(0f, ((EmissionModule)(ref emission)).rateOverTimeMultiplier * multiplier);
					((EmissionModule)(ref emission)).rateOverDistanceMultiplier = Mathf.Max(0f, ((EmissionModule)(ref emission)).rateOverDistanceMultiplier * multiplier);
					MainModule main = val.main;
					((MainModule)(ref main)).maxParticles = Mathf.Clamp(((MainModule)(ref main)).maxParticles * Mathf.CeilToInt(multiplier), 32, 2000);
					num++;
				}
			}
			return num;
		}

		private static GameObject AddHeartEmitter(Transform steamRoot, float rate)
		{
			//IL_0021: Unknown result type (might be due to invalid IL or missing references)
			//IL_0027: Expected O, but got Unknown
			//IL_0049: 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_0069: 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)
			//IL_0080: 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_00bb: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d1: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f1: 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_0110: Unknown result type (might be due to invalid IL or missing references)
			//IL_011b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0120: Unknown result type (might be due to invalid IL or missing references)
			//IL_012d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0138: 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_016d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0178: Unknown result type (might be due to invalid IL or missing references)
			//IL_017d: Unknown result type (might be due to invalid IL or missing references)
			//IL_019b: Unknown result type (might be due to invalid IL or missing references)
			//IL_01b1: Unknown result type (might be due to invalid IL or missing references)
			//IL_01c7: Unknown result type (might be due to invalid IL or missing references)
			//IL_01d2: Unknown result type (might be due to invalid IL or missing references)
			//IL_01d7: Unknown result type (might be due to invalid IL or missing references)
			//IL_01ed: Unknown result type (might be due to invalid IL or missing references)
			//IL_01f8: Unknown result type (might be due to invalid IL or missing references)
			//IL_01fd: Unknown result type (might be due to invalid IL or missing references)
			//IL_0207: Unknown result type (might be due to invalid IL or missing references)
			//IL_020e: Expected O, but got Unknown
			//IL_022c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0236: Unknown result type (might be due to invalid IL or missing references)
			//IL_023b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0256: Unknown result type (might be due to invalid IL or missing references)
			//IL_0260: Unknown result type (might be due to invalid IL or missing references)
			//IL_0265: Unknown result type (might be due to invalid IL or missing references)
			//IL_027c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0281: Unknown result type (might be due to invalid IL or missing references)
			//IL_0292: Unknown result type (might be due to invalid IL or missing references)
			//IL_0297: Unknown result type (might be due to invalid IL or missing references)
			//IL_02a8: Unknown result type (might be due to invalid IL or missing references)
			//IL_02ad: Unknown result type (might be due to invalid IL or missing references)
			//IL_02be: Unknown result type (might be due to invalid IL or missing references)
			//IL_02c3: Unknown result type (might be due to invalid IL or missing references)
			//IL_02d1: Unknown result type (might be due to invalid IL or missing references)
			//IL_0343: Unknown result type (might be due to invalid IL or missing references)
			//IL_034a: Expected O, but got Unknown
			//IL_036b: Unknown result type (might be due to invalid IL or missing references)
			Transform val = FindChildRecursive(steamRoot, "BathHearts_CustomEmitter");
			if ((Object)(object)val != (Object)null)
			{
				return ((Component)val).gameObject;
			}
			GameObject val2 = new GameObject("BathHearts_CustomEmitter");
			val2.transform.SetParent(steamRoot, false);
			val2.transform.localPosition = new Vector3(0f, 0.12f, 0f);
			val2.transform.localRotation = Quaternion.identity;
			val2.transform.localScale = Vector3.one;
			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(1.6f, 2.7f);
			((MainModule)(ref main)).startSpeed = new MinMaxCurve(0.1f, 0.24f);
			((MainModule)(ref main)).startSize = new MinMaxCurve(0.075f, 0.12f);
			((MainModule)(ref main)).startColor = new MinMaxGradient(new Color(1f, 0.36f, 0.62f, 0.92f));
			((MainModule)(ref main)).maxParticles = 24;
			((MainModule)(ref main)).gravityModifier = MinMaxCurve.op_Implicit(-0.015f);
			EmissionModule emission = val3.emission;
			((EmissionModule)(ref emission)).enabled = false;
			((EmissionModule)(ref emission)).rateOverTime = new MinMaxCurve(rate);
			ShapeModule shape = val3.shape;
			((ShapeModule)(ref shape)).enabled = true;
			((ShapeModule)(ref shape)).shapeType = (ParticleSystemShapeType)10;
			((ShapeModule)(ref shape)).radius = 0.3f;
			((ShapeModule)(ref shape)).rotation = new Vector3(90f, 0f, 0f);
			VelocityOverLifetimeModule velocityOverLifetime = val3.velocityOverLifetime;
			((VelocityOverLifetimeModule)(ref velocityOverLifetime)).enabled = true;
			((VelocityOverLifetimeModule)(ref velocityOverLifetime)).space = (ParticleSystemSimulationSpace)0;
			((VelocityOverLifetimeModule)(ref velocityOverLifetime)).x = new MinMaxCurve(-0.06f, 0.06f);
			((VelocityOverLifetimeModule)(ref velocityOverLifetime)).y = new MinMaxCurve(0.12f, 0.26f);
			((VelocityOverLifetimeModule)(ref velocityOverLifetime)).z = new MinMaxCurve(-0.06f, 0.06f);
			RotationOverLifetimeModule rotationOverLifetime = val3.rotationOverLifetime;
			((RotationOverLifetimeModule)(ref rotationOverLifetime)).enabled = true;
			((RotationOverLifetimeModule)(ref rotationOverLifetime)).z = new MinMaxCurve(-0.8f, 0.8f);
			ColorOverLifetimeModule colorOverLifetime = val3.colorOverLifetime;
			((ColorOverLifetimeModule)(ref colorOverLifetime)).enabled = true;
			Gradient val4 = new Gradient();
			val4.SetKeys((GradientColorKey[])(object)new GradientColorKey[2]
			{
				new GradientColorKey(new Color(1f, 0.4f, 0.66f, 1f), 0f),
				new GradientColorKey(new Color(1f, 0.66f, 0.79f, 1f), 1f)
			}, (GradientAlphaKey[])(object)new GradientAlphaKey[4]
			{
				new GradientAlphaKey(0f, 0f),
				new GradientAlphaKey(0.9f, 0.12f),
				new GradientAlphaKey(0.7f, 0.72f),
				new GradientAlphaKey(0f, 1f)
			});
			((ColorOverLifetimeModule)(ref colorOverLifetime)).color = new MinMaxGradient(val4);
			ParticleSystemRenderer component = val2.GetComponent<ParticleSystemRenderer>();
			component.renderMode = (ParticleSystemRenderMode)4;
			component.alignment = (ParticleSystemRenderSpace)0;
			component.mesh = CreateHeartMesh();
			Shader val5 = Shader.Find("Particles/Standard Unlit");
			if ((Object)(object)val5 == (Object)null)
			{
				val5 = Shader.Find("Sprites/Default");
			}
			if ((Object)(object)val5 == (Object)null)
			{
				val5 = Shader.Find("Standard");
			}
			if ((Object)(object)val5 != (Object)null)
			{
				Material val6 = new Material(val5);
				((Object)val6).name = "BathHearts_PinkMaterial";
				if (val6.HasProperty("_Color"))
				{
					val6.SetColor("_Color", Color.white);
				}
				((Renderer)component).material = val6;
			}
			else
			{
				Log.LogWarning((object)"Could not find a compatible particle shader. Heart particles may be invisible.");
			}
			val3.Stop(true, (ParticleSystemStopBehavior)0);
			return val2;
		}

		private static Mesh CreateHeartMesh()
		{
			//IL_0018: 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_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_004e: 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_0069: Unknown result type (might be due to invalid IL or missing references)
			//IL_006e: 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_0089: Unknown result type (might be due to invalid IL or missing references)
			//IL_009f: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a4: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ba: 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_00d5: Unknown result type (might be due to invalid IL or missing references)
			//IL_00da: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f0: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f5: Unknown result type (might be due to invalid IL or missing references)
			//IL_010c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0111: Unknown result type (might be due to invalid IL or missing references)
			//IL_0128: Unknown result type (might be due to invalid IL or missing references)
			//IL_012d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0144: Unknown result type (might be due to invalid IL or missing references)
			//IL_0149: Unknown result type (might be due to invalid IL or missing references)
			//IL_0160: 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_017c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0181: Unknown result type (might be due to invalid IL or missing references)
			//IL_01b1: Unknown result type (might be due to invalid IL or missing references)
			//IL_01b6: Unknown result type (might be due to invalid IL or missing references)
			//IL_01c9: Unknown result type (might be due to invalid IL or missing references)
			//IL_01ce: Unknown result type (might be due to invalid IL or missing references)
			//IL_01d9: Unknown result type (might be due to invalid IL or missing references)
			//IL_01de: Unknown result type (might be due to invalid IL or missing references)
			//IL_01e1: Unknown result type (might be due to invalid IL or missing references)
			//IL_01e8: Unknown result type (might be due to invalid IL or missing references)
			//IL_01f0: Unknown result type (might be due to invalid IL or missing references)
			//IL_01f7: Unknown result type (might be due to invalid IL or missing references)
			//IL_02a6: Unknown result type (might be due to invalid IL or missing references)
			//IL_02ab: Unknown result type (might be due to invalid IL or missing references)
			//IL_02ca: 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_02d4: 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_02e4: Unknown result type (might be due to invalid IL or missing references)
			//IL_02eb: Unknown result type (might be due to invalid IL or missing references)
			//IL_02f3: Unknown result type (might be due to invalid IL or missing references)
			//IL_02fb: Unknown result type (might be due to invalid IL or missing references)
			//IL_0301: Unknown result type (might be due to invalid IL or missing references)
			//IL_0308: Expected O, but got Unknown
			Vector3[] array = (Vector3[])(object)new Vector3[14]
			{
				new Vector3(0f, 0.2f, 0f),
				new Vector3(-0.15f, 0.43f, 0f),
				new Vector3(-0.34f, 0.43f, 0f),
				new Vector3(-0.48f, 0.28f, 0f),
				new Vector3(-0.48f, 0.1f, 0f),
				new Vector3(-0.36f, -0.08f, 0f),
				new Vector3(-0.2f, -0.25f, 0f),
				new Vector3(0f, -0.45f, 0f),
				new Vector3(0.2f, -0.25f, 0f),
				new Vector3(0.36f, -0.08f, 0f),
				new Vector3(0.48f, 0.1f, 0f),
				new Vector3(0.48f, 0.28f, 0f),
				new Vector3(0.34f, 0.43f, 0f),
				new Vector3(0.15f, 0.43f, 0f)
			};
			Vector3[] array2 = (Vector3[])(object)new Vector3[array.Length + 1];
			Array.Copy(array, array2, array.Length);
			int num = array.Length;
			array2[num] = new Vector3(0f, 0.02f, 0f);
			float num2 = 0f;
			for (int i = 0; i < array.Length; i++)
			{
				Vector3 val = array[i];
				Vector3 val2 = array[(i + 1) % array.Length];
				num2 += val.x * val2.y - val2.x * val.y;
			}
			int[] array3 = new int[array.Length * 3];
			for (int j = 0; j < array.Length; j++)
			{
				int num3 = (j + 1) % array.Length;
				int num4 = j * 3;
				array3[num4] = num;
				array3[num4 + 1] = ((num2 >= 0f) ? j : num3);
				array3[num4 + 2] = ((num2 >= 0f) ? num3 : j);
			}
			Vector2[] array4 = (Vector2[])(object)new Vector2[array2.Length];
			for (int k = 0; k < array.Length; k++)
			{
				array4[k] = new Vector2(array[k].x + 0.5f, array[k].y + 0.5f);
			}
			array4[num] = new Vector2(0.5f, 0.5f);
			Mesh val3 = new Mesh
			{
				name = "BathHearts_RuntimeHeartMesh",
				vertices = array2,
				triangles = array3,
				uv = array4
			};
			val3.RecalculateBounds();
			val3.RecalculateNormals();
			return val3;
		}

		private void OnDestroy()
		{
			if (_harmony != null)
			{
				_harmony.UnpatchSelf();
			}
		}
	}
	public sealed class BathHeatGate : MonoBehaviour
	{
		private GameObject _steamRoot;

		private GameObject _heartEmitter;

		private ParticleSystem _heartParticles;

		private Component _smelter;

		private MethodInfo _getFuel;

		private float _nextCheck;

		private bool _lastShouldEmit;

		private bool _hasState;

		public void Initialize(GameObject steamRoot, GameObject heartEmitter)
		{
			_steamRoot = steamRoot;
			_heartEmitter = heartEmitter;
			ResolveReferences();
			ApplyState(shouldEmit: false);
		}

		private void Awake()
		{
			ResolveReferences();
			ApplyState(shouldEmit: false);
		}

		private void ResolveReferences()
		{
			if ((Object)(object)_steamRoot == (Object)null)
			{
				Transform val = BathHeartsPlugin.FindChildRecursive(((Component)this).transform, "vfx_bath_mist");
				if ((Object)(object)val != (Object)null)
				{
					_steamRoot = ((Component)val).gameObject;
				}
			}
			if ((Object)(object)_heartEmitter == (Object)null && (Object)(object)_steamRoot != (Object)null)
			{
				Transform val2 = BathHeartsPlugin.FindChildRecursive(_steamRoot.transform, "BathHearts_CustomEmitter");
				if ((Object)(object)val2 != (Object)null)
				{
					_heartEmitter = ((Component)val2).gameObject;
				}
			}
			if ((Object)(object)_heartParticles == (Object)null && (Object)(object)_heartEmitter != (Object)null)
			{
				_heartParticles = _heartEmitter.GetComponent<ParticleSystem>();
			}
			Type type = BathHeartsPlugin.FindType("Smelter");
			if (!(type != null))
			{
				return;
			}
			if ((Object)(object)_smelter == (Object)null)
			{
				_smelter = ((Component)this).GetComponent(type);
			}
			if (_getFuel == null)
			{
				if (BathHeartsPlugin.GetFuelMethod == null)
				{
					BathHeartsPlugin.GetFuelMethod = BathHeartsPlugin.FindParameterlessMethod(type, "GetFuel");
				}
				_getFuel = BathHeartsPlugin.GetFuelMethod;
			}
		}

		private void Update()
		{
			if (Time.unscaledTime < _nextCheck)
			{
				return;
			}
			_nextCheck = Time.unscaledTime + 0.3f;
			if ((Object)(object)_heartParticles == (Object)null || (Object)(object)_steamRoot == (Object)null || (Object)(object)_heartEmitter == (Object)null || _getFuel == null || (Object)(object)_smelter == (Object)null)
			{
				ResolveReferences();
			}
			bool flag = false;
			if ((Object)(object)_smelter != (Object)null && _getFuel != null)
			{
				try
				{
					object obj = _getFuel.Invoke(_smelter, null);
					flag = obj != null && Convert.ToSingle(obj) > 0.001f;
				}
				catch
				{
					flag = false;
				}
			}
			else
			{
				flag = (Object)(object)_steamRoot != (Object)null && _steamRoot.activeInHierarchy;
			}
			bool flag2 = flag && (Object)(object)_steamRoot != (Object)null && _steamRoot.activeInHierarchy;
			if (!_hasState || flag2 != _lastShouldEmit)
			{
				ApplyState(flag2);
			}
		}

		private void ApplyState(bool shouldEmit)
		{
			//IL_0014: Unknown result type (might be due to invalid IL or missing references)
			//IL_0019: Unknown result type (might be due to invalid IL or missing references)
			if ((Object)(object)_heartParticles != (Object)null)
			{
				EmissionModule emission = _heartParticles.emission;
				((EmissionModule)(ref emission)).enabled = shouldEmit;
				if (shouldEmit)
				{
					_heartParticles.Play(true);
				}
				else
				{
					_heartParticles.Stop(true, (ParticleSystemStopBehavior)0);
				}
			}
			_lastShouldEmit = shouldEmit;
			_hasState = true;
		}
	}
}