Decompiled source of LatchLink v0.2.0

LatchLink.dll

Decompiled 14 hours ago
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using HarmonyLib;
using Jotunn.Configs;
using Jotunn.Entities;
using Jotunn.Managers;
using Jotunn.Utils;
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("LatchLink")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("0.2.0.0")]
[assembly: AssemblyInformationalVersion("0.2.0")]
[assembly: AssemblyProduct("LatchLink")]
[assembly: AssemblyTitle("LatchLink")]
[assembly: AssemblyVersion("0.2.0.0")]
namespace DoorSwitch;

[HarmonyPatch(typeof(Door), "Interact")]
[HarmonyPriority(0)]
[HarmonyAfter(new string[] { "Harmony.Azumatt.WardIsLove" })]
internal static class DoorInteractionPatch
{
	private static bool Prefix(Door __instance, Humanoid character, bool hold, ref bool __result)
	{
		if (hold || !LinkingSession.ShouldHandleDoorInteraction)
		{
			return true;
		}
		__result = LinkingSession.TryHandleDoor(__instance, character);
		return false;
	}
}
public sealed class DoorSwitchController : MonoBehaviour, Hoverable, Interactable
{
	private const float StatePollInterval = 0.1f;

	private const float PositionAnimationSpeed = 0.8f;

	private const float RotationAnimationSpeed = 240f;

	public string PieceNameToken;

	public Transform Actuator;

	public Vector3 ReleasedPosition;

	public Vector3 ActivePosition;

	public Vector3 ReleasedRotation;

	public Vector3 ActiveRotation;

	private ZNetView networkView;

	private bool targetActive;

	private float nextStatePoll;

	private void Awake()
	{
		networkView = ((Component)this).GetComponent<ZNetView>();
		RefreshVisualState();
		ApplyVisualState(immediate: true);
	}

	private void Update()
	{
		if (!((Object)(object)networkView == (Object)null) && networkView.IsValid())
		{
			if (Time.time >= nextStatePoll)
			{
				nextStatePoll = Time.time + 0.1f;
				RefreshVisualState();
			}
			ApplyVisualState(immediate: false);
		}
	}

	public bool Interact(Humanoid character, bool hold, bool alt)
	{
		//IL_0026: Unknown result type (might be due to invalid IL or missing references)
		//IL_0081: Unknown result type (might be due to invalid IL or missing references)
		//IL_0086: Unknown result type (might be due to invalid IL or missing references)
		//IL_008c: 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)
		//IL_00aa: Unknown result type (might be due to invalid IL or missing references)
		//IL_00a2: Unknown result type (might be due to invalid IL or missing references)
		if (hold || (Object)(object)networkView == (Object)null || !networkView.IsValid())
		{
			return false;
		}
		if (!PrivateArea.CheckAccess(((Component)this).transform.position, 0f, true, false))
		{
			return true;
		}
		bool flag = alt || ZInput.GetKey((KeyCode)304, false) || ZInput.GetKey((KeyCode)303, false);
		bool flag2 = ZInput.GetKey((KeyCode)308, false) || ZInput.GetKey((KeyCode)307, false);
		ZDOID uid = networkView.GetZDO().m_uid;
		if (flag && flag2)
		{
			SwitchRpc.RequestClearLinks(uid);
		}
		else if (flag)
		{
			LinkingSession.BeginOrCancel(uid);
		}
		else if (flag2)
		{
			SwitchRpc.RequestCycleMode(uid);
		}
		else
		{
			SwitchRpc.RequestActivate(uid);
		}
		return true;
	}

	public bool UseItem(Humanoid user, ItemData item)
	{
		return false;
	}

	public string GetHoverName()
	{
		return Localization.instance.Localize(PieceNameToken);
	}

	public string GetHoverText()
	{
		if ((Object)(object)networkView == (Object)null || !networkView.IsValid())
		{
			return GetHoverName();
		}
		ZDO zDO = networkView.GetZDO();
		int count = SwitchData.ReadLinks(zDO).Count;
		string modeName = SwitchData.GetModeName(SwitchData.ReadMode(zDO));
		string text = GetHoverName() + $" ({modeName}, {count} linked)\n" + "[<color=yellow><b>$KEY_Use</b></color>] Activate\n[<color=yellow><b>Shift + $KEY_Use</b></color>] Link  [<color=yellow><b>Alt + $KEY_Use</b></color>] Mode";
		return Localization.instance.Localize(text);
	}

	private void RefreshVisualState()
	{
		//IL_005c: 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_006f: Unknown result type (might be due to invalid IL or missing references)
		ZDO val = (((Object)(object)networkView != (Object)null && networkView.IsValid()) ? networkView.GetZDO() : null);
		if (val == null)
		{
			targetActive = false;
			return;
		}
		List<ZDOID> list = SwitchData.ReadLinks(val);
		if (list.Count == 0)
		{
			targetActive = false;
			return;
		}
		bool flag = false;
		bool flag2 = false;
		foreach (ZDOID item in list)
		{
			ZDOMan instance = ZDOMan.instance;
			ZDO val2 = ((instance != null) ? instance.GetZDO(item) : null);
			if (val2 != null)
			{
				flag = true;
				if (val2.GetInt(ZDOVars.s_state, 0) != 0)
				{
					flag2 = true;
					break;
				}
			}
		}
		targetActive = (flag ? flag2 : val.GetBool("doorswitch_last_state", false));
	}

	private void ApplyVisualState(bool immediate)
	{
		//IL_0020: Unknown result type (might be due to invalid IL or missing references)
		//IL_0018: Unknown result type (might be due to invalid IL or missing references)
		//IL_0025: 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_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_0041: Unknown result type (might be due to invalid IL or missing references)
		//IL_006a: Unknown result type (might be due to invalid IL or missing references)
		//IL_006f: 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_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_00a2: 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_0057: Unknown result type (might be due to invalid IL or missing references)
		if (!((Object)(object)Actuator == (Object)null))
		{
			Vector3 val = (targetActive ? ActivePosition : ReleasedPosition);
			Quaternion val2 = Quaternion.Euler(targetActive ? ActiveRotation : ReleasedRotation);
			if (immediate)
			{
				Actuator.localPosition = val;
				Actuator.localRotation = val2;
			}
			else
			{
				Actuator.localPosition = Vector3.MoveTowards(Actuator.localPosition, val, 0.8f * Time.deltaTime);
				Actuator.localRotation = Quaternion.RotateTowards(Actuator.localRotation, val2, 240f * Time.deltaTime);
			}
		}
	}
}
[BepInPlugin("com.tomtom.latchlink", "LatchLink", "0.2.0")]
[BepInDependency(/*Could not decode attribute arguments.*/)]
[NetworkCompatibility(/*Could not decode attribute arguments.*/)]
public sealed class DoorSwitchPlugin : BaseUnityPlugin
{
	public const string ModGuid = "com.tomtom.latchlink";

	public const string ModName = "LatchLink";

	public const string ModVersion = "0.2.0";

	private Harmony harmony;

	internal static ConfigEntry<int> MaximumLinks { get; private set; }

	internal static ConfigEntry<float> MaximumLinkDistance { get; private set; }

	private void Awake()
	{
		//IL_0027: Unknown result type (might be due to invalid IL or missing references)
		//IL_0031: Expected O, but got Unknown
		//IL_0064: Unknown result type (might be due to invalid IL or missing references)
		//IL_006e: Expected O, but got Unknown
		//IL_0094: Unknown result type (might be due to invalid IL or missing references)
		//IL_009e: Expected O, but got Unknown
		MaximumLinks = ((BaseUnityPlugin)this).Config.Bind<int>("General", "Maximum links per switch", 32, new ConfigDescription("Maximum number of doors controlled by one switch.", (AcceptableValueBase)(object)new AcceptableValueRange<int>(1, 256), Array.Empty<object>()));
		MaximumLinkDistance = ((BaseUnityPlugin)this).Config.Bind<float>("General", "Maximum link distance", 100f, new ConfigDescription("Maximum distance in metres between a switch and a linked target.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(2f, 1000f), Array.Empty<object>()));
		AddLocalizations();
		SwitchRpc.Initialize();
		PrefabManager.OnVanillaPrefabsAvailable += RegisterPieces;
		harmony = new Harmony("com.tomtom.latchlink");
		harmony.PatchAll();
		((BaseUnityPlugin)this).Logger.LogInfo((object)"LatchLink 0.2.0 loaded");
	}

	private void Update()
	{
		LinkingSession.Update();
	}

	private void OnDestroy()
	{
		PrefabManager.OnVanillaPrefabsAvailable -= RegisterPieces;
		Harmony obj = harmony;
		if (obj != null)
		{
			obj.UnpatchSelf();
		}
	}

	private static void AddLocalizations()
	{
		CustomLocalization localization = LocalizationManager.Instance.GetLocalization();
		string text = "English";
		localization.AddTranslation(ref text, new Dictionary<string, string>
		{
			["doorswitch_wood_name"] = "Wooden Lever",
			["doorswitch_wood_description"] = "A simple lever for remotely operating linked doors and gates.",
			["doorswitch_iron_name"] = "Iron Button",
			["doorswitch_iron_description"] = "A compact mechanical control for linked doors and gates.",
			["doorswitch_rune_name"] = "Rune Switch",
			["doorswitch_rune_description"] = "A glowing runic control for linked doors and gates."
		});
	}

	private static void RegisterPieces()
	{
		SwitchPieceFactory.RegisterAll();
		PrefabManager.OnVanillaPrefabsAvailable -= RegisterPieces;
	}
}
internal static class LinkingSession
{
	private const float SessionDuration = 90f;

	private static ZDOID switchId = ZDOID.None;

	private static float expiresAt;

	private static bool suppressDoorHandling;

	internal static bool IsActive
	{
		get
		{
			if (!((ZDOID)(ref switchId)).IsNone())
			{
				return Time.unscaledTime < expiresAt;
			}
			return false;
		}
	}

	internal static bool ShouldHandleDoorInteraction
	{
		get
		{
			if (IsActive)
			{
				return !suppressDoorHandling;
			}
			return false;
		}
	}

	internal static void BeginOrCancel(ZDOID id)
	{
		//IL_001f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0020: 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)
		//IL_000c: Unknown result type (might be due to invalid IL or missing references)
		if (IsActive && switchId == id)
		{
			Cancel("Link mode cancelled");
			return;
		}
		switchId = id;
		expiresAt = Time.unscaledTime + 90f;
		ShowMessage("Link mode: use a door or gate to add/remove it; Escape cancels");
	}

	internal static void Update()
	{
		//IL_0007: Unknown result type (might be due to invalid IL or missing references)
		//IL_000c: Unknown result type (might be due to invalid IL or missing references)
		if (!IsActive)
		{
			switchId = ZDOID.None;
		}
		else if (ZInput.GetKeyDown((KeyCode)27, false))
		{
			Cancel("Link mode finished");
		}
	}

	internal static bool TryHandleDoor(Door door, Humanoid user)
	{
		//IL_001f: 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_006d: Unknown result type (might be due to invalid IL or missing references)
		if (!IsActive)
		{
			return false;
		}
		expiresAt = Time.unscaledTime + 90f;
		if (!PrivateArea.CheckAccess(((Component)door).transform.position, 0f, true, false))
		{
			End();
			return true;
		}
		ZNetView componentInParent = ((Component)door).GetComponentInParent<ZNetView>();
		if ((Object)(object)componentInParent == (Object)null || !componentInParent.IsValid())
		{
			ShowMessage("That target is not networked and cannot be linked");
			End();
			return true;
		}
		SwitchRpc.RequestToggleLink(switchId, componentInParent.GetZDO().m_uid);
		End();
		return true;
	}

	internal static bool InteractWithoutLinking(Door door, Humanoid user)
	{
		bool flag = suppressDoorHandling;
		suppressDoorHandling = true;
		try
		{
			return door.Interact(user, false, false);
		}
		finally
		{
			suppressDoorHandling = flag;
		}
	}

	private static void Cancel(string message)
	{
		End();
		ShowMessage(message);
	}

	private static void End()
	{
		//IL_0000: Unknown result type (might be due to invalid IL or missing references)
		//IL_0005: Unknown result type (might be due to invalid IL or missing references)
		switchId = ZDOID.None;
		expiresAt = 0f;
	}

	internal static void ShowMessage(string message)
	{
		if ((Object)(object)Player.m_localPlayer != (Object)null)
		{
			((Character)Player.m_localPlayer).Message((MessageType)2, message, 0, (Sprite)null);
		}
	}
}
internal static class SwitchData
{
	internal const string LinksKey = "doorswitch_links";

	internal const string ModeKey = "doorswitch_mode";

	internal const string LastStateKey = "doorswitch_last_state";

	internal const string ActivationKey = "doorswitch_activation";

	internal static List<ZDOID> ReadLinks(ZDO zdo)
	{
		//IL_0084: Unknown result type (might be due to invalid IL or missing references)
		List<ZDOID> list = new List<ZDOID>();
		if (zdo == null)
		{
			return list;
		}
		string text = zdo.GetString("doorswitch_links", string.Empty);
		if (string.IsNullOrWhiteSpace(text))
		{
			return list;
		}
		string[] array = text.Split(new char[1] { ';' }, StringSplitOptions.RemoveEmptyEntries);
		for (int i = 0; i < array.Length; i++)
		{
			string[] array2 = array[i].Split(':');
			if (array2.Length == 2 && long.TryParse(array2[0], NumberStyles.Integer, CultureInfo.InvariantCulture, out var result) && uint.TryParse(array2[1], NumberStyles.Integer, CultureInfo.InvariantCulture, out var result2) && result != 0L && result2 != 0)
			{
				list.Add(new ZDOID(result, result2));
			}
		}
		return list;
	}

	internal static void WriteLinks(ZDO zdo, IEnumerable<ZDOID> links)
	{
		//IL_0010: Unknown result type (might be due to invalid IL or missing references)
		//IL_0015: Unknown result type (might be due to invalid IL or missing references)
		List<string> list = new List<string>();
		foreach (ZDOID link in links)
		{
			ZDOID current = link;
			if (!((ZDOID)(ref current)).IsNone())
			{
				list.Add(((ZDOID)(ref current)).UserID.ToString(CultureInfo.InvariantCulture) + ":" + ((ZDOID)(ref current)).ID.ToString(CultureInfo.InvariantCulture));
			}
		}
		zdo.Set("doorswitch_links", string.Join(";", list));
	}

	internal static SwitchMode ReadMode(ZDO zdo)
	{
		int num = ((zdo != null) ? zdo.GetInt("doorswitch_mode", 0) : 0);
		if (num < 0 || num > 2)
		{
			return SwitchMode.Toggle;
		}
		return (SwitchMode)num;
	}

	internal static string GetModeName(SwitchMode mode)
	{
		return mode switch
		{
			SwitchMode.Open => "Open", 
			SwitchMode.Close => "Close", 
			_ => "Toggle", 
		};
	}
}
[HarmonyPatch(typeof(Player), "HaveRequirements", new Type[]
{
	typeof(Piece),
	typeof(RequirementMode)
})]
internal static class SwitchDiscoveryPatch
{
	private static bool Prefix(Player __instance, Piece piece, RequirementMode mode, ref bool __result)
	{
		//IL_0000: Unknown result type (might be due to invalid IL or missing references)
		//IL_0002: Invalid comparison between Unknown and I4
		if ((int)mode != 1 || !SwitchPieceFactory.IsSwitchPiece(piece))
		{
			return true;
		}
		if (piece.m_dlc.Length > 0 && !DLCMan.instance.IsDLCInstalled(piece.m_dlc))
		{
			__result = false;
			return false;
		}
		Requirement[] resources = piece.m_resources;
		foreach (Requirement val in resources)
		{
			if ((Object)(object)val.m_resItem != (Object)null && val.m_amount > 0 && !__instance.IsKnownMaterial(val.m_resItem.m_itemData.m_shared.m_name))
			{
				__result = false;
				return false;
			}
		}
		__result = true;
		return false;
	}
}
internal static class SwitchIconFactory
{
	private const int Size = 64;

	internal static Sprite Create(SwitchVisualStyle style)
	{
		//IL_001c: 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_0052: Unknown result type (might be due to invalid IL or missing references)
		//IL_0057: 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_0074: Unknown result type (might be due to invalid IL or missing references)
		//IL_007c: Expected O, but got Unknown
		//IL_00a0: Unknown result type (might be due to invalid IL or missing references)
		//IL_00af: Unknown result type (might be due to invalid IL or missing references)
		Color32[] array = (Color32[])(object)new Color32[4096];
		Color32 val = default(Color32);
		((Color32)(ref val))..ctor((byte)0, (byte)0, (byte)0, (byte)0);
		for (int i = 0; i < array.Length; i++)
		{
			array[i] = val;
		}
		switch (style)
		{
		case SwitchVisualStyle.IronButton:
			DrawIronButton(array);
			break;
		case SwitchVisualStyle.RuneSwitch:
			DrawRuneSwitch(array);
			break;
		default:
			DrawWoodLever(array);
			break;
		}
		Texture2D val2 = new Texture2D(64, 64, (TextureFormat)4, false)
		{
			name = $"DoorSwitch_{style}_Icon",
			filterMode = (FilterMode)0,
			wrapMode = (TextureWrapMode)1
		};
		val2.SetPixels32(array);
		val2.Apply(false, false);
		Sprite obj = Sprite.Create(val2, new Rect(0f, 0f, 64f, 64f), new Vector2(0.5f, 0.5f), 64f);
		((Object)obj).name = ((Object)val2).name;
		return obj;
	}

	private static void DrawWoodLever(Color32[] pixels)
	{
		//IL_000c: Unknown result type (might be due to invalid IL or missing references)
		//IL_001f: 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_0051: Unknown result type (might be due to invalid IL or missing references)
		//IL_0076: Unknown result type (might be due to invalid IL or missing references)
		//IL_0091: Unknown result type (might be due to invalid IL or missing references)
		//IL_00b2: Unknown result type (might be due to invalid IL or missing references)
		//IL_00cd: 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)
		DrawPlate(pixels, new Color32((byte)47, (byte)29, (byte)17, byte.MaxValue), new Color32((byte)131, (byte)76, (byte)31, byte.MaxValue), new Color32((byte)188, (byte)116, (byte)48, byte.MaxValue));
		DrawLine(pixels, 30, 27, 41, 47, 7, new Color32((byte)48, (byte)31, (byte)18, byte.MaxValue));
		DrawLine(pixels, 30, 27, 41, 47, 4, new Color32((byte)222, (byte)153, (byte)54, byte.MaxValue));
		FillCircle(pixels, 30, 27, 8, new Color32((byte)48, (byte)31, (byte)18, byte.MaxValue));
		FillCircle(pixels, 30, 27, 5, new Color32((byte)230, (byte)165, (byte)65, byte.MaxValue));
		FillCircle(pixels, 42, 49, 7, new Color32((byte)48, (byte)31, (byte)18, byte.MaxValue));
		FillCircle(pixels, 42, 49, 4, new Color32((byte)244, (byte)183, (byte)72, byte.MaxValue));
	}

	private static void DrawIronButton(Color32[] pixels)
	{
		//IL_000c: Unknown result type (might be due to invalid IL or missing references)
		//IL_001c: 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_0051: Unknown result type (might be due to invalid IL or missing references)
		//IL_0070: 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_00af: Unknown result type (might be due to invalid IL or missing references)
		//IL_00ce: Unknown result type (might be due to invalid IL or missing references)
		DrawPlate(pixels, new Color32((byte)25, (byte)29, (byte)32, byte.MaxValue), new Color32((byte)91, (byte)103, (byte)112, byte.MaxValue), new Color32((byte)153, (byte)165, (byte)171, byte.MaxValue));
		FillCircle(pixels, 32, 31, 15, new Color32((byte)35, (byte)20, (byte)18, byte.MaxValue));
		FillCircle(pixels, 32, 31, 12, new Color32((byte)191, (byte)35, (byte)25, byte.MaxValue));
		FillCircle(pixels, 30, 34, 7, new Color32((byte)239, (byte)61, (byte)37, byte.MaxValue));
		FillCircle(pixels, 28, 37, 3, new Color32(byte.MaxValue, (byte)146, (byte)100, byte.MaxValue));
		DrawRivets(pixels, new Color32((byte)206, (byte)213, (byte)215, byte.MaxValue));
	}

	private static void DrawRuneSwitch(Color32[] pixels)
	{
		//IL_000c: Unknown result type (might be due to invalid IL or missing references)
		//IL_001c: 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_0072: 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)
		//IL_0092: Unknown result type (might be due to invalid IL or missing references)
		//IL_00a2: Unknown result type (might be due to invalid IL or missing references)
		//IL_00b2: Unknown result type (might be due to invalid IL or missing references)
		//IL_00c2: Unknown result type (might be due to invalid IL or missing references)
		DrawPlate(pixels, new Color32((byte)28, (byte)34, (byte)37, byte.MaxValue), new Color32((byte)75, (byte)85, (byte)89, byte.MaxValue), new Color32((byte)130, (byte)141, (byte)145, byte.MaxValue));
		Color32 color = default(Color32);
		((Color32)(ref color))..ctor((byte)7, (byte)91, (byte)119, byte.MaxValue);
		Color32 color2 = default(Color32);
		((Color32)(ref color2))..ctor((byte)46, (byte)224, byte.MaxValue, byte.MaxValue);
		DrawLine(pixels, 29, 17, 35, 48, 8, color);
		DrawLine(pixels, 32, 33, 45, 44, 8, color);
		DrawLine(pixels, 32, 33, 43, 22, 8, color);
		DrawLine(pixels, 29, 17, 35, 48, 4, color2);
		DrawLine(pixels, 32, 33, 45, 44, 4, color2);
		DrawLine(pixels, 32, 33, 43, 22, 4, color2);
	}

	private static void DrawPlate(Color32[] pixels, Color32 border, Color32 face, Color32 highlight)
	{
		//IL_0008: 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_0026: 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)
		FillRect(pixels, 10, 6, 53, 57, border);
		FillRect(pixels, 14, 10, 49, 53, face);
		FillRect(pixels, 16, 12, 47, 15, highlight);
		FillRect(pixels, 16, 16, 19, 50, highlight);
	}

	private static void DrawRivets(Color32[] pixels, Color32 color)
	{
		//IL_0006: Unknown result type (might be due to invalid IL or missing references)
		//IL_0012: 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_002a: Unknown result type (might be due to invalid IL or missing references)
		FillCircle(pixels, 18, 15, 2, color);
		FillCircle(pixels, 46, 15, 2, color);
		FillCircle(pixels, 18, 49, 2, color);
		FillCircle(pixels, 46, 49, 2, color);
	}

	private static void DrawLine(Color32[] pixels, int x0, int y0, int x1, int y1, int thickness, Color32 color)
	{
		//IL_0033: Unknown result type (might be due to invalid IL or missing references)
		int num = Mathf.Abs(x1 - x0);
		int num2 = ((x0 < x1) ? 1 : (-1));
		int num3 = -Mathf.Abs(y1 - y0);
		int num4 = ((y0 < y1) ? 1 : (-1));
		int num5 = num + num3;
		while (true)
		{
			FillCircle(pixels, x0, y0, thickness / 2, color);
			if (x0 != x1 || y0 != y1)
			{
				int num6 = 2 * num5;
				if (num6 >= num3)
				{
					num5 += num3;
					x0 += num2;
				}
				if (num6 <= num)
				{
					num5 += num;
					y0 += num4;
				}
				continue;
			}
			break;
		}
	}

	private static void FillCircle(Color32[] pixels, int centerX, int centerY, int radius, Color32 color)
	{
		//IL_0023: Unknown result type (might be due to invalid IL or missing references)
		int num = radius * radius;
		for (int i = centerY - radius; i <= centerY + radius; i++)
		{
			for (int j = centerX - radius; j <= centerX + radius; j++)
			{
				int num2 = j - centerX;
				int num3 = i - centerY;
				if (num2 * num2 + num3 * num3 <= num)
				{
					SetPixel(pixels, j, i, color);
				}
			}
		}
	}

	private static void FillRect(Color32[] pixels, int minX, int minY, int maxX, int maxY, Color32 color)
	{
		//IL_000b: Unknown result type (might be due to invalid IL or missing references)
		for (int i = minY; i <= maxY; i++)
		{
			for (int j = minX; j <= maxX; j++)
			{
				SetPixel(pixels, j, i, color);
			}
		}
	}

	private static void SetPixel(Color32[] pixels, int x, int y, Color32 color)
	{
		//IL_0019: 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)
		if (x >= 0 && x < 64 && y >= 0 && y < 64)
		{
			pixels[y * 64 + x] = color;
		}
	}
}
internal enum SwitchMode
{
	Toggle,
	Open,
	Close
}
internal static class SwitchPieceFactory
{
	private readonly struct Requirement
	{
		internal string Item { get; }

		internal int Amount { get; }

		internal Requirement(string item, int amount)
		{
			Item = item;
			Amount = amount;
		}
	}

	private const string BasePrefab = "sign";

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

	internal static void RegisterAll()
	{
		Register("doorswitch_wood", "$doorswitch_wood_name", "$doorswitch_wood_description", SwitchVisualStyle.WoodLever, new Requirement[2]
		{
			new Requirement("Wood", 4),
			new Requirement("Resin", 1)
		});
		Register("doorswitch_iron", "$doorswitch_iron_name", "$doorswitch_iron_description", SwitchVisualStyle.IronButton, new Requirement[2]
		{
			new Requirement("FineWood", 2),
			new Requirement("Iron", 2)
		});
		Register("doorswitch_rune", "$doorswitch_rune_name", "$doorswitch_rune_description", SwitchVisualStyle.RuneSwitch, new Requirement[3]
		{
			new Requirement("Stone", 4),
			new Requirement("SurtlingCore", 1),
			new Requirement("GreydwarfEye", 2)
		});
	}

	internal static bool IsSwitchPrefab(int prefabHash)
	{
		return SwitchPrefabHashes.Contains(prefabHash);
	}

	private static void Register(string prefabName, string displayName, string description, SwitchVisualStyle style, IEnumerable<Requirement> requirements)
	{
		//IL_0000: Unknown result type (might be due to invalid IL or missing references)
		//IL_0005: Unknown result type (might be due to invalid IL or missing references)
		//IL_000c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0013: 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_0029: 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_0041: Expected O, but got Unknown
		//IL_0082: Unknown result type (might be due to invalid IL or missing references)
		//IL_0088: Expected O, but got Unknown
		PieceConfig val = new PieceConfig
		{
			Name = displayName,
			Description = description,
			PieceTable = PieceTables.Hammer,
			Category = PieceCategories.Misc,
			CraftingStation = CraftingStations.Workbench,
			Icon = SwitchIconFactory.Create(style)
		};
		foreach (Requirement requirement in requirements)
		{
			val.AddRequirement(requirement.Item, requirement.Amount, true);
		}
		CustomPiece val2 = new CustomPiece(prefabName, "sign", val);
		PreparePrefab(val2.PiecePrefab, displayName, style);
		PieceManager.Instance.AddPiece(val2);
		SwitchPrefabHashes.Add(StringExtensionMethods.GetStableHashCode(prefabName));
	}

	internal static bool IsSwitchPiece(Piece piece)
	{
		if ((Object)(object)piece != (Object)null)
		{
			return IsSwitchPrefab(StringExtensionMethods.GetStableHashCode(Utils.GetPrefabName(((Component)piece).gameObject)));
		}
		return false;
	}

	private static void PreparePrefab(GameObject prefab, string displayName, SwitchVisualStyle style)
	{
		Material sourceMaterial = (from renderer in prefab.GetComponentsInChildren<Renderer>(true)
			select renderer.sharedMaterial).FirstOrDefault((Func<Material, bool>)((Material material) => (Object)(object)material != (Object)null));
		Renderer[] componentsInChildren = prefab.GetComponentsInChildren<Renderer>(true);
		for (int num = 0; num < componentsInChildren.Length; num++)
		{
			componentsInChildren[num].enabled = false;
		}
		Sign[] componentsInChildren2 = prefab.GetComponentsInChildren<Sign>(true);
		for (int num = 0; num < componentsInChildren2.Length; num++)
		{
			Object.DestroyImmediate((Object)(object)componentsInChildren2[num]);
		}
		DoorSwitchController doorSwitchController = prefab.AddComponent<DoorSwitchController>();
		doorSwitchController.PieceNameToken = displayName;
		BuildVisual(prefab, doorSwitchController, style, sourceMaterial);
	}

	private static void BuildVisual(GameObject prefab, DoorSwitchController controller, SwitchVisualStyle style, Material sourceMaterial)
	{
		//IL_0005: Unknown result type (might be due to invalid IL or missing references)
		//IL_000b: Expected O, but got Unknown
		//IL_0039: 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)
		//IL_006e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0078: 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_00ac: Unknown result type (might be due to invalid IL or missing references)
		//IL_00d2: Unknown result type (might be due to invalid IL or missing references)
		//IL_00e6: Unknown result type (might be due to invalid IL or missing references)
		//IL_00eb: 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_0127: Unknown result type (might be due to invalid IL or missing references)
		//IL_013b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0157: Unknown result type (might be due to invalid IL or missing references)
		//IL_015c: 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_0183: Unknown result type (might be due to invalid IL or missing references)
		//IL_0188: Unknown result type (might be due to invalid IL or missing references)
		//IL_0189: Unknown result type (might be due to invalid IL or missing references)
		//IL_018b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0190: Unknown result type (might be due to invalid IL or missing references)
		//IL_0192: Unknown result type (might be due to invalid IL or missing references)
		//IL_01a8: Unknown result type (might be due to invalid IL or missing references)
		//IL_01b2: Unknown result type (might be due to invalid IL or missing references)
		//IL_01cd: Unknown result type (might be due to invalid IL or missing references)
		//IL_01e6: 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_0211: Unknown result type (might be due to invalid IL or missing references)
		//IL_0216: Unknown result type (might be due to invalid IL or missing references)
		//IL_0227: Unknown result type (might be due to invalid IL or missing references)
		//IL_0255: Unknown result type (might be due to invalid IL or missing references)
		//IL_0269: Unknown result type (might be due to invalid IL or missing references)
		//IL_027d: Unknown result type (might be due to invalid IL or missing references)
		//IL_029f: Unknown result type (might be due to invalid IL or missing references)
		//IL_02b3: Unknown result type (might be due to invalid IL or missing references)
		//IL_02c7: Unknown result type (might be due to invalid IL or missing references)
		//IL_02e9: Unknown result type (might be due to invalid IL or missing references)
		//IL_02fd: Unknown result type (might be due to invalid IL or missing references)
		//IL_0311: Unknown result type (might be due to invalid IL or missing references)
		//IL_0325: Unknown result type (might be due to invalid IL or missing references)
		//IL_032a: Unknown result type (might be due to invalid IL or missing references)
		//IL_033f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0344: 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_0350: Unknown result type (might be due to invalid IL or missing references)
		//IL_0351: Unknown result type (might be due to invalid IL or missing references)
		//IL_0353: Unknown result type (might be due to invalid IL or missing references)
		//IL_0358: Unknown result type (might be due to invalid IL or missing references)
		//IL_035a: Unknown result type (might be due to invalid IL or missing references)
		//IL_0370: Unknown result type (might be due to invalid IL or missing references)
		//IL_0389: Unknown result type (might be due to invalid IL or missing references)
		//IL_03a0: Unknown result type (might be due to invalid IL or missing references)
		//IL_03b4: Unknown result type (might be due to invalid IL or missing references)
		//IL_03b9: Unknown result type (might be due to invalid IL or missing references)
		//IL_03ca: Unknown result type (might be due to invalid IL or missing references)
		//IL_03fb: Unknown result type (might be due to invalid IL or missing references)
		//IL_040f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0423: Unknown result type (might be due to invalid IL or missing references)
		//IL_0446: Unknown result type (might be due to invalid IL or missing references)
		//IL_045a: Unknown result type (might be due to invalid IL or missing references)
		//IL_045f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0482: Unknown result type (might be due to invalid IL or missing references)
		//IL_0496: Unknown result type (might be due to invalid IL or missing references)
		//IL_049b: Unknown result type (might be due to invalid IL or missing references)
		//IL_04b1: Unknown result type (might be due to invalid IL or missing references)
		//IL_04b6: Unknown result type (might be due to invalid IL or missing references)
		//IL_04b7: Unknown result type (might be due to invalid IL or missing references)
		//IL_04b9: Unknown result type (might be due to invalid IL or missing references)
		//IL_04be: Unknown result type (might be due to invalid IL or missing references)
		//IL_04c0: Unknown result type (might be due to invalid IL or missing references)
		//IL_04d5: Unknown result type (might be due to invalid IL or missing references)
		//IL_04da: Unknown result type (might be due to invalid IL or missing references)
		//IL_04ef: Unknown result type (might be due to invalid IL or missing references)
		//IL_04f4: Unknown result type (might be due to invalid IL or missing references)
		GameObject val = new GameObject("DoorSwitchVisual");
		val.transform.SetParent(prefab.transform, false);
		val.layer = prefab.layer;
		Material material = CreateMaterial(sourceMaterial, new Color(0.58f, 0.32f, 0.12f), 0f, Color.black);
		switch (style)
		{
		case SwitchVisualStyle.IronButton:
		{
			material = CreateMaterial(sourceMaterial, new Color(0.48f, 0.52f, 0.56f), 0.75f, Color.black);
			Material material2 = CreateMaterial(sourceMaterial, new Color(0.95f, 0.07f, 0.025f), 0.35f, new Color(1.4f, 0.06f, 0.015f));
			CreatePrimitive((PrimitiveType)3, "Plate", val.transform, new Vector3(0f, 0f, 0f), new Vector3(0.38f, 0.42f, 0.09f), Vector3.zero, material);
			controller.Actuator = CreatePrimitive((PrimitiveType)2, "Actuator", val.transform, new Vector3(0f, 0f, 0.1f), new Vector3(0.12f, 0.055f, 0.12f), new Vector3(90f, 0f, 0f), material2).transform;
			controller.ReleasedPosition = controller.Actuator.localPosition;
			controller.ActivePosition = new Vector3(0f, 0f, 0.065f);
			controller.ReleasedRotation = (controller.ActiveRotation = controller.Actuator.localEulerAngles);
			break;
		}
		case SwitchVisualStyle.RuneSwitch:
		{
			material = CreateMaterial(sourceMaterial, new Color(0.42f, 0.44f, 0.46f), 0.05f, Color.black);
			Material material2 = CreateMaterial(sourceMaterial, new Color(0.02f, 0.82f, 1f), 0.1f, new Color(0.04f, 1.6f, 2.8f));
			CreatePrimitive((PrimitiveType)3, "Stone", val.transform, Vector3.zero, new Vector3(0.4f, 0.46f, 0.1f), Vector3.zero, material);
			Transform transform2 = new GameObject("Actuator").transform;
			transform2.SetParent(val.transform, false);
			CreatePrimitive((PrimitiveType)3, "RuneVertical", transform2, new Vector3(0f, 0f, 0.064f), new Vector3(0.045f, 0.27f, 0.025f), new Vector3(0f, 0f, 18f), material2);
			CreatePrimitive((PrimitiveType)3, "RuneBranchTop", transform2, new Vector3(0.055f, 0.07f, 0.064f), new Vector3(0.035f, 0.15f, 0.025f), new Vector3(0f, 0f, -42f), material2);
			CreatePrimitive((PrimitiveType)3, "RuneBranchBottom", transform2, new Vector3(0.055f, -0.07f, 0.064f), new Vector3(0.035f, 0.15f, 0.025f), new Vector3(0f, 0f, 42f), material2);
			controller.Actuator = transform2;
			controller.ReleasedPosition = Vector3.zero;
			controller.ActivePosition = new Vector3(0f, 0f, -0.018f);
			controller.ReleasedRotation = (controller.ActiveRotation = Vector3.zero);
			break;
		}
		default:
		{
			Material material2 = CreateMaterial(sourceMaterial, new Color(0.92f, 0.56f, 0.13f), 0.35f, new Color(0.18f, 0.07f, 0.01f));
			CreatePrimitive((PrimitiveType)3, "Plate", val.transform, Vector3.zero, new Vector3(0.34f, 0.42f, 0.09f), Vector3.zero, material);
			Transform transform = new GameObject("Actuator").transform;
			transform.SetParent(val.transform, false);
			CreatePrimitive((PrimitiveType)2, "Pivot", transform, new Vector3(0f, 0f, 0.075f), new Vector3(0.09f, 0.045f, 0.09f), new Vector3(90f, 0f, 0f), material2);
			CreatePrimitive((PrimitiveType)3, "Handle", transform, new Vector3(0f, 0.13f, 0.12f), new Vector3(0.055f, 0.28f, 0.055f), Vector3.zero, material2);
			CreatePrimitive((PrimitiveType)0, "Knob", transform, new Vector3(0f, 0.27f, 0.12f), new Vector3(0.11f, 0.11f, 0.11f), Vector3.zero, material2);
			controller.Actuator = transform;
			controller.ReleasedPosition = (controller.ActivePosition = Vector3.zero);
			controller.ReleasedRotation = new Vector3(0f, 0f, -25f);
			controller.ActiveRotation = new Vector3(0f, 0f, 25f);
			break;
		}
		}
	}

	private static GameObject CreatePrimitive(PrimitiveType type, string name, Transform parent, Vector3 position, Vector3 scale, Vector3 rotation, Material material)
	{
		//IL_0000: Unknown result type (might be due to invalid IL or missing references)
		//IL_0020: 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_0039: Unknown result type (might be due to invalid IL or missing references)
		GameObject obj = GameObject.CreatePrimitive(type);
		((Object)obj).name = name;
		obj.transform.SetParent(parent, false);
		obj.transform.localPosition = position;
		obj.transform.localScale = scale;
		obj.transform.localEulerAngles = rotation;
		obj.layer = ((Component)parent).gameObject.layer;
		Collider component = obj.GetComponent<Collider>();
		if ((Object)(object)component != (Object)null)
		{
			Object.DestroyImmediate((Object)(object)component);
		}
		obj.GetComponent<Renderer>().sharedMaterial = material;
		return obj;
	}

	private static Material CreateMaterial(Material source, Color color, float metallic, Color emission)
	{
		//IL_0015: Unknown result type (might be due to invalid IL or missing references)
		//IL_001b: Expected O, but got Unknown
		//IL_0068: Unknown result type (might be due to invalid IL or missing references)
		//IL_0081: Unknown result type (might be due to invalid IL or missing references)
		//IL_00a0: Unknown result type (might be due to invalid IL or missing references)
		//IL_00a1: Unknown result type (might be due to invalid IL or missing references)
		//IL_00cb: Unknown result type (might be due to invalid IL or missing references)
		//IL_00e4: Unknown result type (might be due to invalid IL or missing references)
		if ((Object)(object)source == (Object)null)
		{
			throw new InvalidOperationException("No source material was available for the switch visual.");
		}
		Material val = new Material(source);
		if (val.HasProperty("_MainTex"))
		{
			val.SetTexture("_MainTex", (Texture)(object)Texture2D.whiteTexture);
		}
		if (val.HasProperty("_BaseMap"))
		{
			val.SetTexture("_BaseMap", (Texture)(object)Texture2D.whiteTexture);
		}
		if (val.HasProperty("_Color"))
		{
			val.SetColor("_Color", color);
		}
		if (val.HasProperty("_BaseColor"))
		{
			val.SetColor("_BaseColor", color);
		}
		if (val.HasProperty("_Metallic"))
		{
			val.SetFloat("_Metallic", metallic);
		}
		if (emission != Color.black)
		{
			val.EnableKeyword("_EMISSION");
			if (val.HasProperty("_EmissionColor"))
			{
				val.SetColor("_EmissionColor", emission);
			}
			if (val.HasProperty("_EmissiveColor"))
			{
				val.SetColor("_EmissiveColor", emission);
			}
		}
		return val;
	}
}
internal enum SwitchVisualStyle
{
	WoodLever,
	IronButton,
	RuneSwitch
}
internal static class SwitchRpc
{
	private enum Command : byte
	{
		Activate,
		ToggleLink,
		ClearLinks,
		CycleMode,
		Result,
		OperateTargets
	}

	private static CustomRPC rpc;

	internal static void Initialize()
	{
		//IL_0011: 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_0027: Expected O, but got Unknown
		//IL_0027: Expected O, but got Unknown
		rpc = NetworkManager.Instance.AddRPC("DoorSwitch", new CoroutineHandler(OnServerReceive), new CoroutineHandler(OnClientReceive));
	}

	internal static void RequestActivate(ZDOID switchId)
	{
		//IL_0007: 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)
		SendToServer(Command.Activate, delegate(ZPackage package)
		{
			//IL_0002: Unknown result type (might be due to invalid IL or missing references)
			package.Write(switchId);
		});
	}

	internal static void RequestToggleLink(ZDOID switchId, ZDOID targetId)
	{
		//IL_0007: 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_000e: Unknown result type (might be due to invalid IL or missing references)
		//IL_000f: Unknown result type (might be due to invalid IL or missing references)
		SendToServer(Command.ToggleLink, delegate(ZPackage package)
		{
			//IL_0002: 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)
			package.Write(switchId);
			package.Write(targetId);
		});
	}

	internal static void RequestClearLinks(ZDOID switchId)
	{
		//IL_0007: 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)
		SendToServer(Command.ClearLinks, delegate(ZPackage package)
		{
			//IL_0002: Unknown result type (might be due to invalid IL or missing references)
			package.Write(switchId);
		});
	}

	internal static void RequestCycleMode(ZDOID switchId)
	{
		//IL_0007: 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)
		SendToServer(Command.CycleMode, delegate(ZPackage package)
		{
			//IL_0002: Unknown result type (might be due to invalid IL or missing references)
			package.Write(switchId);
		});
	}

	private static void SendToServer(Command command, Action<ZPackage> writePayload)
	{
		//IL_0053: Unknown result type (might be due to invalid IL or missing references)
		//IL_0059: Expected O, but got Unknown
		if (rpc == null || (Object)(object)ZNet.instance == (Object)null)
		{
			LinkingSession.ShowMessage("The network is not ready");
			return;
		}
		long num;
		if (ZNet.instance.IsServer())
		{
			num = ZNet.GetUID();
		}
		else
		{
			ZNetPeer serverPeer = ZNet.instance.GetServerPeer();
			if (serverPeer == null)
			{
				LinkingSession.ShowMessage("The server connection is not ready");
				return;
			}
			num = serverPeer.m_uid;
		}
		ZPackage val = new ZPackage();
		val.Write((byte)command);
		writePayload(val);
		rpc.SendPackage(num, val);
	}

	private static IEnumerator OnServerReceive(long sender, ZPackage package)
	{
		switch ((Command)package.ReadByte())
		{
		case Command.Activate:
			OnActivate(sender, package.ReadZDOID());
			break;
		case Command.ToggleLink:
			OnToggleLink(sender, package.ReadZDOID(), package.ReadZDOID());
			break;
		case Command.ClearLinks:
			OnClearLinks(sender, package.ReadZDOID());
			break;
		case Command.CycleMode:
			OnCycleMode(sender, package.ReadZDOID());
			break;
		case Command.Result:
			OnResult(package.ReadString());
			break;
		case Command.OperateTargets:
			if (sender == ZNet.GetUID())
			{
				OnOperateTargets(package);
			}
			break;
		}
		yield break;
	}

	private static IEnumerator OnClientReceive(long sender, ZPackage package)
	{
		ZNet instance = ZNet.instance;
		ZNetPeer val = ((instance != null) ? instance.GetServerPeer() : null);
		if (val != null && sender == val.m_uid)
		{
			switch ((Command)package.ReadByte())
			{
			case Command.Result:
				OnResult(package.ReadString());
				break;
			case Command.OperateTargets:
				OnOperateTargets(package);
				break;
			}
		}
		yield break;
	}

	private static void OnActivate(long sender, ZDOID switchId)
	{
		//IL_0001: Unknown result type (might be due to invalid IL or missing references)
		if (TryGetServerSwitch(sender, switchId, out var switchZdo))
		{
			List<ZDO> validTargets = GetValidTargets(switchZdo);
			if (validTargets.Count == 0)
			{
				Reply(sender, "This switch has no linked doors");
				return;
			}
			SwitchMode switchMode = SwitchData.ReadMode(switchZdo);
			bool flag = (byte)(((switchMode != SwitchMode.Close) ? 1u : 0u) & (uint)(switchMode switch
			{
				SwitchMode.Toggle => validTargets.Any((ZDO target) => target.GetInt(ZDOVars.s_state, 0) == 0) ? 1 : 0, 
				SwitchMode.Open => 1, 
				_ => 0, 
			})) != 0;
			SendOperation(sender, flag, validTargets);
			switchZdo.Set("doorswitch_last_state", flag);
			switchZdo.Set("doorswitch_activation", switchZdo.GetInt("doorswitch_activation", 0) + 1);
		}
	}

	private static void OnToggleLink(long sender, ZDOID switchId, ZDOID targetId)
	{
		//IL_0007: 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_000e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0026: Unknown result type (might be due to invalid IL or missing references)
		//IL_0046: 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_0110: Unknown result type (might be due to invalid IL or missing references)
		if (!TryGetServerSwitch(sender, switchId, out var switchZdo))
		{
			return;
		}
		ZDOMan instance = ZDOMan.instance;
		ZDO val = ((instance != null) ? instance.GetZDO(targetId) : null);
		if (!IsDoorTarget(val))
		{
			Reply(sender, "That object is not a supported door, gate, or drawbridge");
			return;
		}
		float num = Vector3.Distance(switchZdo.GetPosition(), val.GetPosition());
		if (num > DoorSwitchPlugin.MaximumLinkDistance.Value)
		{
			Reply(sender, $"Target is too far away ({num:0} m; limit {DoorSwitchPlugin.MaximumLinkDistance.Value:0} m)");
			return;
		}
		List<ZDOID> list = SwitchData.ReadLinks(switchZdo);
		int num2 = list.FindIndex((ZDOID link) => link == targetId);
		if (num2 >= 0)
		{
			list.RemoveAt(num2);
			SwitchData.WriteLinks(switchZdo, list);
			Reply(sender, $"Target unlinked ({list.Count} remaining)");
		}
		else if (list.Count >= DoorSwitchPlugin.MaximumLinks.Value)
		{
			Reply(sender, $"This switch already has the maximum of {DoorSwitchPlugin.MaximumLinks.Value} links");
		}
		else
		{
			list.Add(targetId);
			SwitchData.WriteLinks(switchZdo, list);
			Reply(sender, $"Target linked ({list.Count} total)");
		}
	}

	private static void OnClearLinks(long sender, ZDOID switchId)
	{
		//IL_0001: Unknown result type (might be due to invalid IL or missing references)
		if (TryGetServerSwitch(sender, switchId, out var switchZdo))
		{
			SwitchData.WriteLinks(switchZdo, (IEnumerable<ZDOID>)(object)new ZDOID[0]);
			Reply(sender, "All links removed");
		}
	}

	private static void OnCycleMode(long sender, ZDOID switchId)
	{
		//IL_0001: Unknown result type (might be due to invalid IL or missing references)
		if (TryGetServerSwitch(sender, switchId, out var switchZdo))
		{
			SwitchMode switchMode = (SwitchMode)((int)(SwitchData.ReadMode(switchZdo) + 1) % 3);
			switchZdo.Set("doorswitch_mode", (int)switchMode);
			Reply(sender, "Switch mode: " + SwitchData.GetModeName(switchMode));
		}
	}

	private static bool TryGetServerSwitch(long sender, ZDOID switchId, out ZDO switchZdo)
	{
		//IL_002b: Unknown result type (might be due to invalid IL or missing references)
		switchZdo = null;
		if ((Object)(object)ZNet.instance == (Object)null || !ZNet.instance.IsServer() || ZDOMan.instance == null)
		{
			return false;
		}
		switchZdo = ZDOMan.instance.GetZDO(switchId);
		if (switchZdo == null || !SwitchPieceFactory.IsSwitchPrefab(switchZdo.GetPrefab()))
		{
			Reply(sender, "The selected switch no longer exists");
			switchZdo = null;
			return false;
		}
		return true;
	}

	private static List<ZDO> GetValidTargets(ZDO switchZdo)
	{
		//IL_0016: 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_0021: Unknown result type (might be due to invalid IL or missing references)
		List<ZDO> list = new List<ZDO>();
		foreach (ZDOID item in SwitchData.ReadLinks(switchZdo))
		{
			ZDO zDO = ZDOMan.instance.GetZDO(item);
			if (IsDoorTarget(zDO))
			{
				list.Add(zDO);
			}
		}
		return list;
	}

	private static bool IsDoorTarget(ZDO target)
	{
		if (target == null || (Object)(object)ZNetScene.instance == (Object)null)
		{
			return false;
		}
		GameObject prefab = ZNetScene.instance.GetPrefab(target.GetPrefab());
		if ((Object)(object)prefab != (Object)null)
		{
			return (Object)(object)prefab.GetComponentInChildren<Door>(true) != (Object)null;
		}
		return false;
	}

	private static void SendOperation(long peerId, bool desiredOpen, List<ZDO> targets)
	{
		//IL_0000: Unknown result type (might be due to invalid IL or missing references)
		//IL_0006: Expected O, but got Unknown
		//IL_0033: Unknown result type (might be due to invalid IL or missing references)
		ZPackage val = new ZPackage();
		val.Write((byte)5);
		val.Write(desiredOpen);
		val.Write(targets.Count);
		foreach (ZDO target in targets)
		{
			val.Write(target.m_uid);
		}
		rpc.SendPackage(peerId, val);
	}

	private static void OnOperateTargets(ZPackage package)
	{
		//IL_0030: 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_0043: Unknown result type (might be due to invalid IL or missing references)
		bool flag = package.ReadBool();
		int num = Mathf.Clamp(package.ReadInt(), 0, 256);
		int num2 = 0;
		int num3 = 0;
		int num4 = 0;
		Humanoid localPlayer = (Humanoid)(object)Player.m_localPlayer;
		for (int i = 0; i < num; i++)
		{
			ZDOID val = package.ReadZDOID();
			ZNetScene instance = ZNetScene.instance;
			GameObject val2 = ((instance != null) ? instance.FindInstance(val) : null);
			Door val3 = (((Object)(object)val2 != (Object)null) ? val2.GetComponentInChildren<Door>(true) : null);
			ZNetView val4 = (((Object)(object)val3 != (Object)null) ? ((Component)val3).GetComponentInParent<ZNetView>() : null);
			if ((Object)(object)val3 == (Object)null || (Object)(object)val4 == (Object)null || !val4.IsValid() || (Object)(object)localPlayer == (Object)null)
			{
				continue;
			}
			num2++;
			if (val4.GetZDO().GetInt(ZDOVars.s_state, 0) != 0 != flag)
			{
				num3++;
				if (LinkingSession.InteractWithoutLinking(val3, localPlayer))
				{
					num4++;
				}
			}
		}
		if (num2 == 0)
		{
			OnResult("Linked doors are not loaded; move closer and try again");
		}
		else if (num4 > 0)
		{
			OnResult(string.Format("{0} {1} linked target{2}", flag ? "Opened" : "Closed", num4, (num4 == 1) ? string.Empty : "s"));
		}
		else if (num3 == 0)
		{
			OnResult("All loaded linked targets are already " + (flag ? "open" : "closed"));
		}
	}

	private static void Reply(long peerId, string message)
	{
		//IL_0008: Unknown result type (might be due to invalid IL or missing references)
		//IL_000e: Expected O, but got Unknown
		if (rpc != null)
		{
			ZPackage val = new ZPackage();
			val.Write((byte)4);
			val.Write(message);
			rpc.SendPackage(peerId, val);
		}
	}

	private static void OnResult(string message)
	{
		LinkingSession.ShowMessage(message);
	}
}