Decompiled source of Snow Lantern On Ice v1.4.2

BepInEx/plugins/SnowLanternOnIce/SnowLanternOnIce.dll

Decompiled a week ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using BepInEx;
using BepInEx.Logging;
using HarmonyLib;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyVersion("0.0.0.0")]
namespace SnowLanternOnIce;

[BepInPlugin("com.jaybird.snowlanternonice", "Snow Lantern * On Ice", "1.4.1")]
public sealed class SnowLanternOnIcePlugin : BaseUnityPlugin
{
	public const string PluginGuid = "com.jaybird.snowlanternonice";

	public const string PluginName = "Snow Lantern * On Ice";

	public const string PluginVersion = "1.4.1";

	private Harmony _harmony;

	internal static ManualLogSource ModLog;

	private void Awake()
	{
		//IL_0011: Unknown result type (might be due to invalid IL or missing references)
		//IL_001b: Expected O, but got Unknown
		ModLog = ((BaseUnityPlugin)this).Logger;
		_harmony = new Harmony("com.jaybird.snowlanternonice");
		_harmony.PatchAll();
		((BaseUnityPlugin)this).Logger.LogInfo((object)"Snow Lantern * On Ice 1.4.1 loaded.");
	}

	private void OnDestroy()
	{
		if (_harmony != null)
		{
			_harmony.UnpatchSelf();
		}
	}
}
[HarmonyPatch(typeof(WearNTear), "UpdateWear")]
internal static class SnowLanternWetPatch
{
	private const string SnowLanternPrefab = "piece_snowlantern";

	private const float RayStartHeight = 0.45f;

	private const float RayDistance = 2.75f;

	private static readonly HashSet<int> LoggedLanterns = new HashSet<int>();

	private static bool Prefix(WearNTear __instance)
	{
		if (IsPrefab(((Component)__instance).gameObject, "piece_snowlantern") && IsSupportedByIce(__instance))
		{
			return false;
		}
		return true;
	}

	private static bool IsSupportedByIce(WearNTear lantern)
	{
		//IL_0006: 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)
		RaycastHit[] array = Physics.SphereCastAll(((Component)lantern).transform.position, 0.75f, Vector3.down, 2.75f, -1, (QueryTriggerInteraction)1);
		for (int i = 0; i < array.Length; i++)
		{
			Collider collider = ((RaycastHit)(ref array[i])).collider;
			if ((Object)(object)collider == (Object)null)
			{
				continue;
			}
			Piece componentInParent = ((Component)collider).GetComponentInParent<Piece>();
			if ((Object)(object)componentInParent == (Object)null || (Object)(object)((Component)componentInParent).gameObject == (Object)(object)((Component)lantern).gameObject)
			{
				continue;
			}
			string text = NormalizePrefabName(((Object)((Component)componentInParent).gameObject).name);
			if (IsIceBuildingPiece(text))
			{
				int instanceID = ((Object)lantern).GetInstanceID();
				if (LoggedLanterns.Add(instanceID) && SnowLanternOnIcePlugin.ModLog != null)
				{
					SnowLanternOnIcePlugin.ModLog.LogInfo((object)("Snow Lantern supported by ice piece '" + text + "'; wear bypass enabled."));
				}
				return true;
			}
		}
		return false;
	}

	private static bool IsIceBuildingPiece(string prefabName)
	{
		string text = (prefabName.StartsWith("piece_", StringComparison.OrdinalIgnoreCase) ? prefabName.Substring(6) : prefabName);
		if (text.IndexOf("ice", StringComparison.OrdinalIgnoreCase) < 0)
		{
			return text.IndexOf("frozen", StringComparison.OrdinalIgnoreCase) >= 0;
		}
		return true;
	}

	private static bool IsPrefab(GameObject gameObject, string prefabName)
	{
		if ((Object)(object)gameObject == (Object)null)
		{
			return false;
		}
		return string.Equals(NormalizePrefabName(((Object)gameObject).name), prefabName, StringComparison.Ordinal);
	}

	private static string NormalizePrefabName(string objectName)
	{
		if (objectName.EndsWith("(Clone)", StringComparison.Ordinal))
		{
			objectName = objectName.Substring(0, objectName.Length - "(Clone)".Length);
		}
		return objectName;
	}
}