Decompiled source of ConsoleOnly v1.0.0

plugin/ConsoleOnly.dll

Decompiled 7 months ago
using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Logging;
using HarmonyLib;
using JetBrains.Annotations;
using Microsoft.CodeAnalysis;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("0.0.0.0")]
[module: UnverifiableCode]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.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]
	[Microsoft.CodeAnalysis.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]
	[Microsoft.CodeAnalysis.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 ConsoleOnly
{
	[BepInPlugin("patricnox.valheim.ConsoleOnly", "ConsoleOnly", "1.0.0")]
	public class ConsoleOnlyPlugin : BaseUnityPlugin
	{
		[HarmonyPatch(typeof(ZDOMan), "AddPeer")]
		internal class OnPlayerJoin
		{
			internal class Client
			{
				public static string PlayerConnecting(ZDOID player, string name, string id, string platform)
				{
					if (name == "" || id == "")
					{
						p.LogInfo((object)("Could not fire PlayerConnecting, missing params. Name: " + name + " | Id: " + id + " | Platform: " + platform));
						return "NotAllowed";
					}
					if (platform != "xbox")
					{
						return "NotAllowed";
					}
					return "OK";
				}
			}

			private static bool Prefix(ZNetPeer netPeer)
			{
				//IL_0047: Unknown result type (might be due to invalid IL or missing references)
				if (!netPeer.IsReady() || netPeer.m_socket == null)
				{
					p.LogInfo((object)$"[ERROR] COULD NOT LOGIN PLAYER CUS INVALID NETPEER: {netPeer}");
					return false;
				}
				string hostName = netPeer.m_socket.GetHostName();
				(string id, string? platform) tuple = NormalizeSteamID(hostName);
				string item = tuple.id;
				string item2 = tuple.platform;
				string text = Client.PlayerConnecting(netPeer.m_characterID, netPeer.m_playerName, item, item2);
				if (!(text == "NotAllowed"))
				{
					if (text == "OK")
					{
						return true;
					}
					return true;
				}
				netPeer.m_rpc.Invoke("Error", new object[1] { 4 });
				p.LogInfo((object)("NotAllowed: " + item + " on " + item2));
				return false;
			}
		}

		public const string PluginId = "patricnox.valheim.ConsoleOnly";

		private Harmony _harmony;

		private static ConsoleOnlyPlugin _instance;

		public static ManualLogSource p = Logger.CreateLogSource("patricnox.valheim.ConsoleOnly");

		[UsedImplicitly]
		private void Awake()
		{
			_instance = this;
			_harmony = Harmony.CreateAndPatchAll(typeof(ConsoleOnlyPlugin).Assembly, "patricnox.valheim.ConsoleOnly");
			p.LogInfo((object)"[ConsoleOnly] Started");
		}

		private static (string id, string? platform) NormalizeSteamID(string steamID)
		{
			if (steamID.Contains("_"))
			{
				string[] array = steamID.Split(new char[1] { '_' });
				if (array.Length <= 1)
				{
					return (steamID, null);
				}
				return (array[1], array[0]);
			}
			return (steamID, null);
		}

		[UsedImplicitly]
		private void OnDestroy()
		{
			Harmony harmony = _harmony;
			if (harmony != null)
			{
				harmony.UnpatchSelf();
			}
		}
	}
	public static class MyPluginInfo
	{
		public const string PLUGIN_GUID = "ConsoleOnly";

		public const string PLUGIN_NAME = "ConsoleOnly";

		public const string PLUGIN_VERSION = "1.0.0";
	}
}