Decompiled source of RememberPortalNames v1.2.1

RememberPortalNames.dll

Decompiled 3 days ago
using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using HarmonyLib;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: AssemblyCompany("RememberPortalNames")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0+4ab8437df522ee3218f0017db84609d94e550b17")]
[assembly: AssemblyProduct("RememberPortalNames")]
[assembly: AssemblyTitle("RememberPortalNames")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace RememberPortalName;

[BepInPlugin("com.Matuh.rememberportalname", "Remember Portal Name", "1.2.0")]
public class RememberPortalMod : BaseUnityPlugin
{
	[HarmonyPatch(typeof(WearNTear), "Remove")]
	private class WearNTear_Remove_Patch
	{
		private static void Prefix(WearNTear __instance)
		{
			object networkView = GetNetworkView(__instance);
			if (networkView == null || !IsNetworkOwner(networkView))
			{
				return;
			}
			TeleportWorld component = ((Component)__instance).GetComponent<TeleportWorld>();
			if ((Object)(object)component != (Object)null)
			{
				string text = component.GetText();
				if (!string.IsNullOrEmpty(text))
				{
					LastPortalName = text;
					Debug.Log((object)("[RememberPortalMod] Saved portal tag: " + LastPortalName));
				}
			}
		}
	}

	[HarmonyPatch(typeof(Player), "PlacePiece")]
	private class Player_PlacePiece_Patch
	{
		private static void Prefix(Piece piece)
		{
			if ((Object)(object)((Component)piece).GetComponent<TeleportWorld>() != (Object)null)
			{
				IsPlacing = true;
			}
		}

		private static void Postfix()
		{
			IsPlacing = false;
		}
	}

	[HarmonyPatch(typeof(TeleportWorld), "Awake")]
	private class TeleportWorld_Awake_Patch
	{
		private static void Postfix(TeleportWorld __instance)
		{
			if (!IsPlacing)
			{
				return;
			}
			object networkView = GetNetworkView(__instance);
			if (networkView != null && IsNetworkValid(networkView) && IsNetworkOwner(networkView))
			{
				string text = "";
				if (!string.IsNullOrEmpty(LastPortalName))
				{
					text = LastPortalName;
					LastPortalName = "";
				}
				else if (!string.IsNullOrEmpty(DefaultPortalName.Value))
				{
					text = DefaultPortalName.Value;
				}
				if (!string.IsNullOrEmpty(text))
				{
					__instance.SetText(text);
					Debug.Log((object)("[RememberPortalMod] Applied portal tag: " + text));
				}
			}
		}
	}

	[Serializable]
	[CompilerGenerated]
	private sealed class <>c
	{
		public static readonly <>c <>9 = new <>c();

		public static ConsoleEvent <>9__8_0;

		public static ConsoleEvent <>9__8_1;

		internal void <RegisterConsoleCommands>b__8_0(ConsoleEventArgs args)
		{
			if (args.Length >= 2)
			{
				string text = (LastPortalName = string.Join(" ", args.Args, 1, args.Length - 1));
				args.Context.AddString("[RememberPortalMod] Next portal will be named: '" + text + "'");
			}
			else
			{
				args.Context.AddString("Usage: setportalname [name]");
			}
		}

		internal void <RegisterConsoleCommands>b__8_1(ConsoleEventArgs args)
		{
			if (args.Length >= 2)
			{
				string text = string.Join(" ", args.Args, 1, args.Length - 1);
				DefaultPortalName.Value = text;
				args.Context.AddString("[RememberPortalMod] Persistent default portal name updated to: '" + text + "'");
			}
			else
			{
				args.Context.AddString("[RememberPortalMod] Current persistent default: '" + DefaultPortalName.Value + "'. Usage: setdefaultportalname [name]");
			}
		}
	}

	public static string LastPortalName = "";

	public static bool IsPlacing = false;

	public static ConfigEntry<string> DefaultPortalName;

	private readonly Harmony harmony = new Harmony("com.Matuh.rememberportalname");

	private static object GetNetworkView(object instance)
	{
		try
		{
			Type type = instance.GetType();
			string[] array = new string[3] { "nview", "m_nview", "networkView" };
			foreach (string name in array)
			{
				FieldInfo field = type.GetField(name, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
				if (field != null)
				{
					return field.GetValue(instance);
				}
			}
		}
		catch
		{
		}
		return null;
	}

	private static bool IsNetworkOwner(object nview)
	{
		if (nview == null)
		{
			return false;
		}
		try
		{
			MethodInfo method = nview.GetType().GetMethod("IsOwner", BindingFlags.Instance | BindingFlags.Public);
			if (method != null)
			{
				return (bool)method.Invoke(nview, null);
			}
		}
		catch
		{
		}
		return true;
	}

	private static bool IsNetworkValid(object nview)
	{
		if (nview == null)
		{
			return false;
		}
		try
		{
			MethodInfo method = nview.GetType().GetMethod("IsValid", BindingFlags.Instance | BindingFlags.Public);
			if (method != null)
			{
				return (bool)method.Invoke(nview, null);
			}
		}
		catch
		{
		}
		return true;
	}

	private void Awake()
	{
		DefaultPortalName = ((BaseUnityPlugin)this).Config.Bind<string>("General", "DefaultPortalName", "", "Persistent default name applied to new portals when no deconstructed name exists.");
		harmony.PatchAll();
		RegisterConsoleCommands();
	}

	private void RegisterConsoleCommands()
	{
		//IL_0035: 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_0025: Unknown result type (might be due to invalid IL or missing references)
		//IL_002b: Expected O, but got Unknown
		//IL_006e: 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_005e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0064: Expected O, but got Unknown
		try
		{
			object obj = <>c.<>9__8_0;
			if (obj == null)
			{
				ConsoleEvent val = delegate(ConsoleEventArgs args)
				{
					if (args.Length >= 2)
					{
						string text = (LastPortalName = string.Join(" ", args.Args, 1, args.Length - 1));
						args.Context.AddString("[RememberPortalMod] Next portal will be named: '" + text + "'");
					}
					else
					{
						args.Context.AddString("Usage: setportalname [name]");
					}
				};
				<>c.<>9__8_0 = val;
				obj = (object)val;
			}
			new ConsoleCommand("setportalname", "Sets a one-time name for the next portal.", (ConsoleEvent)obj, false, false, false, false, false, false, (ConsoleOptionsFetcher)null, false, false, false);
			object obj2 = <>c.<>9__8_1;
			if (obj2 == null)
			{
				ConsoleEvent val2 = delegate(ConsoleEventArgs args)
				{
					if (args.Length >= 2)
					{
						string text = string.Join(" ", args.Args, 1, args.Length - 1);
						DefaultPortalName.Value = text;
						args.Context.AddString("[RememberPortalMod] Persistent default portal name updated to: '" + text + "'");
					}
					else
					{
						args.Context.AddString("[RememberPortalMod] Current persistent default: '" + DefaultPortalName.Value + "'. Usage: setdefaultportalname [name]");
					}
				};
				<>c.<>9__8_1 = val2;
				obj2 = (object)val2;
			}
			new ConsoleCommand("setdefaultportalname", "Sets a persistent default portal name that survives logouts.", (ConsoleEvent)obj2, false, false, false, false, false, false, (ConsoleOptionsFetcher)null, false, false, false);
		}
		catch (Exception ex)
		{
			Debug.LogError((object)("[RememberPortalMod] Failed to register console commands: " + ex.Message));
		}
	}
}