Decompiled source of NoSaloonDoors v1.1.0

NoSaloonDoors.dll

Decompiled 6 days ago
using System;
using System.Diagnostics;
using System.Linq;
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(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("Rory Durrant")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.1.0.0")]
[assembly: AssemblyInformationalVersion("1.1.0+d9f150f9dcb6135c9c96a2546ec39694181755da")]
[assembly: AssemblyProduct("NoSaloonDoors")]
[assembly: AssemblyTitle("NoSaloonDoors")]
[assembly: AssemblyMetadata("RepositoryUrl", "https://github.com/RoryDotGG/NoSaloonDoors")]
[assembly: AssemblyVersion("1.1.0.0")]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Embedded]
	[AttributeUsage(AttributeTargets.Class | AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Event | AttributeTargets.Parameter | AttributeTargets.ReturnValue | AttributeTargets.GenericParameter, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableAttribute : Attribute
	{
		public readonly byte[] NullableFlags;

		public NullableAttribute(byte P_0)
		{
			NullableFlags = new byte[1] { P_0 };
		}

		public NullableAttribute(byte[] P_0)
		{
			NullableFlags = P_0;
		}
	}
	[CompilerGenerated]
	[Embedded]
	[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Method | AttributeTargets.Interface | AttributeTargets.Delegate, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableContextAttribute : Attribute
	{
		public readonly byte Flag;

		public NullableContextAttribute(byte P_0)
		{
			Flag = P_0;
		}
	}
	[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 NoSaloonDoors
{
	[HarmonyPatch]
	internal static class BakedDoorPatch
	{
		private static readonly MethodInfo? DestroyObject = AccessTools.Method(typeof(PhysGrabObjectImpactDetector), "DestroyObject", (Type[])null, (Type[])null);

		private static MethodBase? Target => AccessTools.Method("LevelGenerator:GenerateDoneApply", (Type[])null, (Type[])null);

		private static bool Prepare()
		{
			if (Target != null)
			{
				return true;
			}
			Plugin.Log.LogWarning((object)"LevelGenerator.GenerateDoneApply isn't in this game version, so saloon doors built into rooms stay. NoSaloonDoors needs the Sunstone update (v0.5.0) or later.");
			return false;
		}

		private static MethodBase TargetMethod()
		{
			return Target;
		}

		private static void Postfix()
		{
			if (!Plugin.Enabled.Value || Plugin.Names.Length == 0 || !SemiFunc.IsMasterClientOrSingleplayer())
			{
				return;
			}
			int num = 0;
			PhysGrabHinge[] array = Object.FindObjectsOfType<PhysGrabHinge>();
			foreach (PhysGrabHinge val in array)
			{
				if (IsLeftOut(val))
				{
					val.DestroyHinge();
					DestroyQuietly(((Component)val).GetComponent<PhysGrabObjectImpactDetector>());
					num++;
				}
			}
			if (num > 0)
			{
				Plugin.Log.LogInfo((object)$"Took out {num} saloon door hinge(s) built into the level");
			}
		}

		internal static bool IsLeftOut(PhysGrabHinge hinge)
		{
			Transform parent = ((Component)hinge).transform.parent;
			if ((Object)(object)parent != (Object)null)
			{
				return DoorFilter.MatchesObjectName(((Object)parent).name, Plugin.Names);
			}
			return false;
		}

		private static void DestroyQuietly(PhysGrabObjectImpactDetector? detector)
		{
			if ((Object)(object)detector == (Object)null || DestroyObject == null)
			{
				return;
			}
			object[] parameters = DestroyObject.GetParameters().Select(delegate(ParameterInfo p)
			{
				string name = p.Name;
				if (name == "effects")
				{
					return false;
				}
				return (name == "forced") ? ((object)true) : p.DefaultValue;
			}).ToArray();
			DestroyObject.Invoke(detector, parameters);
		}
	}
	[HarmonyPatch(typeof(PhysGrabObjectImpactDetector), "DestroyObjectRPC")]
	internal static class QuietDoorBreakPatch
	{
		private static void Prefix(PhysGrabObjectImpactDetector __instance, ref PhysObjectParticles ___particles)
		{
			if (Plugin.Enabled.Value && !((Object)(object)___particles == (Object)null))
			{
				PhysGrabHinge component = ((Component)__instance).GetComponent<PhysGrabHinge>();
				if ((Object)(object)component != (Object)null && BakedDoorPatch.IsLeftOut(component))
				{
					___particles = null;
				}
			}
		}
	}
	internal static class DoorFilter
	{
		public const string DefaultNames = "Saloon Doors";

		public static string[] ParseNames(string? setting)
		{
			return (from n in (setting ?? "").Split(',')
				select n.Trim() into n
				where n.Length > 0
				select n).ToArray();
		}

		public static bool Matches(string? prefabName, string? resourcePath, string[] names)
		{
			string text = resourcePath ?? "";
			string leaf = text.Substring(text.LastIndexOf('/') + 1);
			return names.Any((string n) => string.Equals(prefabName, n, StringComparison.OrdinalIgnoreCase) || string.Equals(leaf, n, StringComparison.OrdinalIgnoreCase));
		}

		public static bool MatchesObjectName(string? objectName, string[] names)
		{
			string name = (objectName ?? "").Trim();
			if (name.EndsWith("(Clone)", StringComparison.Ordinal))
			{
				name = name.Substring(0, name.Length - "(Clone)".Length).TrimEnd();
			}
			if (name.Length > 0)
			{
				return names.Any((string n) => string.Equals(name, n, StringComparison.OrdinalIgnoreCase));
			}
			return false;
		}
	}
	[BepInPlugin("RoryDotGG.NoSaloonDoors", "NoSaloonDoors", "1.1.0")]
	public class Plugin : BaseUnityPlugin
	{
		public const string Guid = "RoryDotGG.NoSaloonDoors";

		public const string Name = "NoSaloonDoors";

		public const string Version = "1.1.0";

		internal static ManualLogSource Log = null;

		internal static ConfigEntry<bool> Enabled = null;

		internal static ConfigEntry<string> PrefabNames = null;

		internal static string[] Names { get; private set; } = Array.Empty<string>();

		private void Awake()
		{
			//IL_0091: Unknown result type (might be due to invalid IL or missing references)
			//IL_0096: 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)
			Log = ((BaseUnityPlugin)this).Logger;
			Enabled = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Enabled", true, "Leave the saloon doorways in Sunstone Valley Resort open. Only the host needs the mod: the doors are never spawned, so nobody in the lobby gets them. Applies from the next level.");
			PrefabNames = ((BaseUnityPlugin)this).Config.Bind<string>("General", "PrefabNames", "Saloon Doors", "The doorway props to leave out, as a comma-separated list of prefab names, in any case. Saloon Doors is the only one this mod is meant for, but any door a level places with CustomConnectionObject can go here. Applies from the next level.");
			Names = DoorFilter.ParseNames(PrefabNames.Value);
			PrefabNames.SettingChanged += delegate
			{
				Names = DoorFilter.ParseNames(PrefabNames.Value);
			};
			Harmony val = new Harmony("RoryDotGG.NoSaloonDoors");
			val.PatchAll(typeof(SaloonDoorPatch));
			val.PatchAll(typeof(BakedDoorPatch));
			val.PatchAll(typeof(QuietDoorBreakPatch));
			Log.LogInfo((object)"NoSaloonDoors 1.1.0 loaded");
		}
	}
	[HarmonyPatch]
	internal static class SaloonDoorPatch
	{
		private static MethodBase? Target => AccessTools.Method("CustomConnectionObject:Setup", (Type[])null, (Type[])null);

		private static bool Prepare()
		{
			if (Target != null)
			{
				return true;
			}
			Plugin.Log.LogWarning((object)"CustomConnectionObject.Setup isn't in this game version, so the saloon doors stay. NoSaloonDoors needs the Sunstone update (v0.5.0) or later.");
			return false;
		}

		private static MethodBase TargetMethod()
		{
			return Target;
		}

		private static bool Prefix(Component __instance, PrefabRef ___spawnObject, ref bool ___spawned)
		{
			//IL_004c: Unknown result type (might be due to invalid IL or missing references)
			if (!Plugin.Enabled.Value || ___spawnObject == null || !((PrefabRef<GameObject>)(object)___spawnObject).IsValid())
			{
				return true;
			}
			if (!DoorFilter.Matches(((PrefabRef<GameObject>)(object)___spawnObject).PrefabName, ((PrefabRef<GameObject>)(object)___spawnObject).ResourcePath, Plugin.Names))
			{
				return true;
			}
			___spawned = true;
			Plugin.Log.LogDebug((object)$"Left out {((PrefabRef<GameObject>)(object)___spawnObject).PrefabName} at {__instance.transform.position}");
			return false;
		}
	}
	internal static class BuildInfo
	{
		public const string Version = "1.1.0";
	}
}