Decompiled source of Flees AtlyssDedicatedServer v0.0.2

AtlyssDedicatedServer.dll

Decompiled 2 weeks ago
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using System.Text.RegularExpressions;
using BepInEx;
using BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using Mirror;
using Steamworks;
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(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("AtlyssDedicatedServer")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("0.0.1.0")]
[assembly: AssemblyInformationalVersion("0.0.1+efcaf37e02ea816ac94170c296b5b5b753570214")]
[assembly: AssemblyProduct("AtlyssDedicatedServer")]
[assembly: AssemblyTitle("AtlyssDedicatedServer")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("0.0.1.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.Module, AllowMultiple = false, Inherited = false)]
	internal sealed class RefSafetyRulesAttribute : Attribute
	{
		public readonly int Version;

		public RefSafetyRulesAttribute(int P_0)
		{
			Version = P_0;
		}
	}
}
namespace AtlyssDedicatedServer
{
	[BepInPlugin("FleesDedicatedServer", "Atlyss_Server", "0.0.2")]
	[BepInProcess("ATLYSS.exe")]
	public class Plugin : BaseUnityPlugin
	{
		public enum LobbyTypeTag : byte
		{
			PUBLIC,
			FRIENDS,
			PRIVATE
		}

		public enum LobbyFocusTag : byte
		{
			PVE_LOBBY,
			PVP_LOBBY,
			SOCIAL
		}

		[HarmonyPatch(typeof(ChatBehaviour), "New_ChatMessage")]
		private class ChatMirrorPatch
		{
			private static void Postfix(string _message)
			{
				string text = StripUnityRichText(_message);
				Console.WriteLine("[Chat] " + text);
			}

			private static string StripUnityRichText(string input)
			{
				return Regex.Replace(input, "<.*?>", string.Empty);
			}
		}

		[HarmonyPatch(typeof(SettingsManager), "Handle_AudioParameters")]
		private class AudioParamsPatch
		{
			private static bool Prefix(SettingsManager __instance)
			{
				AudioListener.volume = 0f;
				return false;
			}
		}

		[HarmonyPatch(typeof(NetworkManager), "StopHost")]
		public class StopHostPatch
		{
			private static void Postfix(NetworkManager __instance)
			{
				//IL_0011: Unknown result type (might be due to invalid IL or missing references)
				//IL_0017: Expected O, but got Unknown
				Debug.Log((object)"[StopHostPatch] Host stopped. Scheduling shutdown in 5 seconds.");
				GameObject val = new GameObject("ShutdownScheduler");
				Object.DontDestroyOnLoad((Object)(object)val);
				val.AddComponent<ShutdownDelay>();
			}
		}

		public class ShutdownDelay : MonoBehaviour
		{
			[CompilerGenerated]
			private sealed class <DelayedShutdown>d__1 : IEnumerator<object>, IEnumerator, IDisposable
			{
				private int <>1__state;

				private object <>2__current;

				public ShutdownDelay <>4__this;

				object IEnumerator<object>.Current
				{
					[DebuggerHidden]
					get
					{
						return <>2__current;
					}
				}

				object IEnumerator.Current
				{
					[DebuggerHidden]
					get
					{
						return <>2__current;
					}
				}

				[DebuggerHidden]
				public <DelayedShutdown>d__1(int <>1__state)
				{
					this.<>1__state = <>1__state;
				}

				[DebuggerHidden]
				void IDisposable.Dispose()
				{
					<>1__state = -2;
				}

				private bool MoveNext()
				{
					//IL_0026: Unknown result type (might be due to invalid IL or missing references)
					//IL_0030: Expected O, but got Unknown
					switch (<>1__state)
					{
					default:
						return false;
					case 0:
						<>1__state = -1;
						<>2__current = (object)new WaitForSeconds(5f);
						<>1__state = 1;
						return true;
					case 1:
						<>1__state = -1;
						Debug.Log((object)"[ShutdownDelay] Shutting down game...");
						Application.Quit();
						return false;
					}
				}

				bool IEnumerator.MoveNext()
				{
					//ILSpy generated this explicit interface implementation from .override directive in MoveNext
					return this.MoveNext();
				}

				[DebuggerHidden]
				void IEnumerator.Reset()
				{
					throw new NotSupportedException();
				}
			}

			private void Start()
			{
				((MonoBehaviour)this).StartCoroutine(DelayedShutdown());
			}

			[IteratorStateMachine(typeof(<DelayedShutdown>d__1))]
			private IEnumerator DelayedShutdown()
			{
				//yield-return decompiler failed: Unexpected instruction in Iterator.Dispose()
				return new <DelayedShutdown>d__1(0)
				{
					<>4__this = this
				};
			}
		}

		private string serverName = "ATLYSS Server";

		private LobbyTypeTag serverType = LobbyTypeTag.PUBLIC;

		private LobbyFocusTag serverFocus = LobbyFocusTag.PVE_LOBBY;

		private string serverPassword = string.Empty;

		private string serverMOTD = string.Empty;

		private int serverMaxPlayers = 16;

		private int hostCharSaveSlot = 0;

		private bool shouldHostServer = false;

		private bool hostSpawned = false;

		private bool actionTriggered = false;

		private float timeSinceSpawn = 0f;

		internal static ManualLogSource Logger;

		private bool detected = false;

		private string GetArgValue(string[] args, string key)
		{
			int num = Array.IndexOf(args, key);
			return (num >= 0 && num + 1 < args.Length) ? args[num + 1] : null;
		}

		private void LogServerConfig()
		{
			Logger.LogInfo((object)"=== Server Configuration ===");
			Logger.LogInfo((object)("Server Name      : " + serverName));
			Logger.LogInfo((object)$"Server Type      : {serverType}");
			Logger.LogInfo((object)$"Server Focus     : {serverFocus}");
			Logger.LogInfo((object)("Server Password  : " + (string.IsNullOrEmpty(serverPassword) ? "[None]" : serverPassword)));
			Logger.LogInfo((object)("Server MOTD      : " + (string.IsNullOrEmpty(serverMOTD) ? "[None]" : serverMOTD)));
			Logger.LogInfo((object)$"Max Players      : {serverMaxPlayers}");
			Logger.LogInfo((object)"============================");
		}

		private void Awake()
		{
			Logger = ((BaseUnityPlugin)this).Logger;
			Logger.LogInfo((object)"Plugin FleesDedicatedServer is loaded!");
			if (!Application.isBatchMode)
			{
				Logger.LogWarning((object)"Not running in batchmode, DedicatedServer plugin exiting.");
				return;
			}
			string[] commandLineArgs = Environment.GetCommandLineArgs();
			if (!commandLineArgs.Contains("-server"))
			{
				return;
			}
			Logger.LogInfo((object)"Starting in dedicated server mode.");
			shouldHostServer = true;
			Harmony.CreateAndPatchAll(Assembly.GetExecutingAssembly(), (string)null);
			if (int.TryParse(GetArgValue(commandLineArgs, "-hostsave"), out var result))
			{
				if (result >= 0 && result <= 6)
				{
					hostCharSaveSlot = result;
				}
				else
				{
					hostCharSaveSlot = 0;
					Logger.LogWarning((object)"HostSave must be between 0 and 6. Defaulting to 0.");
				}
			}
			else
			{
				hostCharSaveSlot = 0;
			}
			serverName = GetArgValue(commandLineArgs, "-name") ?? "ATLYSS Server";
			if (serverName.Length > 20)
			{
				Logger.LogWarning((object)$"Server name \"{serverName}\" is too long ({serverName.Length}/20). Defaulting to \"ATLYSS Server\".");
				serverName = "ATLYSS Server";
			}
			serverPassword = GetArgValue(commandLineArgs, "-password") ?? "";
			serverMOTD = GetArgValue(commandLineArgs, "-motd") ?? "Welcome to the server!";
			List<string> list = new string[3] { "-public", "-private", "-friends" }.Where(((IEnumerable<string>)commandLineArgs).Contains<string>).ToList();
			if (list.Count > 1)
			{
				Logger.LogWarning((object)("Multiple server type flags detected (" + string.Join(", ", list) + "). Defaulting to PUBLIC."));
				serverType = LobbyTypeTag.PUBLIC;
			}
			else if (list.Count == 1)
			{
				string text = list[0];
				if (1 == 0)
				{
				}
				LobbyTypeTag lobbyTypeTag = text switch
				{
					"-public" => LobbyTypeTag.PUBLIC, 
					"-private" => LobbyTypeTag.PRIVATE, 
					"-friends" => LobbyTypeTag.FRIENDS, 
					_ => LobbyTypeTag.PUBLIC, 
				};
				if (1 == 0)
				{
				}
				serverType = lobbyTypeTag;
			}
			else
			{
				serverType = LobbyTypeTag.PUBLIC;
			}
			List<string> list2 = new string[3] { "-pve", "-pvp", "-social" }.Where(((IEnumerable<string>)commandLineArgs).Contains<string>).ToList();
			if (list2.Count > 1)
			{
				Logger.LogWarning((object)("Multiple lobby focus flags detected (" + string.Join(", ", list2) + "). Defaulting to PVE."));
				serverFocus = LobbyFocusTag.PVE_LOBBY;
			}
			else if (list2.Count == 1)
			{
				string text2 = list2[0];
				if (1 == 0)
				{
				}
				LobbyFocusTag lobbyFocusTag = text2 switch
				{
					"-pve" => LobbyFocusTag.PVE_LOBBY, 
					"-pvp" => LobbyFocusTag.PVP_LOBBY, 
					"-social" => LobbyFocusTag.SOCIAL, 
					_ => LobbyFocusTag.PVE_LOBBY, 
				};
				if (1 == 0)
				{
				}
				serverFocus = lobbyFocusTag;
			}
			else
			{
				serverFocus = LobbyFocusTag.PVE_LOBBY;
			}
			if (int.TryParse(GetArgValue(commandLineArgs, "-maxplayers"), out var result2))
			{
				if (result2 >= 2 && result2 <= 250)
				{
					serverMaxPlayers = result2;
				}
				else
				{
					serverMaxPlayers = 16;
					Logger.LogWarning((object)"MaxPlayers must be between 2 and 250. Defaulting to 16.");
				}
			}
			else
			{
				serverMaxPlayers = 16;
			}
			LogServerConfig();
		}

		private void Update()
		{
			if (!shouldHostServer)
			{
				return;
			}
			if (!hostSpawned)
			{
				if ((Object)(object)NetworkClient.localPlayer != (Object)null)
				{
					hostSpawned = true;
					Logger.LogInfo((object)"[HostSpawnDetector] Host player spawned. Starting delay...");
				}
			}
			else if (!actionTriggered)
			{
				timeSinceSpawn += Time.deltaTime;
				if (timeSinceSpawn >= 30f)
				{
					actionTriggered = true;
					OnHostReady();
				}
			}
			if (!detected && (Object)(object)Object.FindObjectOfType<MainMenuManager>() != (Object)null)
			{
				detected = true;
				HostServer();
			}
		}

		private void OnHostReady()
		{
			//IL_0050: Unknown result type (might be due to invalid IL or missing references)
			//IL_0069: Unknown result type (might be due to invalid IL or missing references)
			Logger.LogInfo((object)"[HostSpawnDetector] 30 seconds passed since host spawned — teleporting!");
			Player component = ((Component)AtlyssNetworkManager._current._connectedPlayers[0]).gameObject.GetComponent<Player>();
			CharacterController component2 = ((Component)component).GetComponent<CharacterController>();
			((Collider)component2).enabled = false;
			((Component)component).transform.SetPositionAndRotation(new Vector3(500f, 50f, 510f), new Quaternion(0f, 0f, 0f, 0f));
			((Collider)component2).enabled = true;
		}

		private void HostServer()
		{
			//IL_0114: Unknown result type (might be due to invalid IL or missing references)
			//IL_0138: Unknown result type (might be due to invalid IL or missing references)
			//IL_013d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0142: Unknown result type (might be due to invalid IL or missing references)
			//IL_015e: Unknown result type (might be due to invalid IL or missing references)
			Logger.LogInfo((object)"[DedicatedServer] Hosting Server");
			ServerHostSettings_Profile hostSettingsProfile = ProfileDataManager._current._hostSettingsProfile;
			AtlyssNetworkManager current = AtlyssNetworkManager._current;
			ProfileDataManager current2 = ProfileDataManager._current;
			LobbyListManager current3 = LobbyListManager._current;
			current._steamworksMode = true;
			current._soloMode = false;
			current._serverMode = false;
			current._serverName = serverName;
			current._serverPassword = serverPassword;
			current._sentPassword = serverPassword;
			current._serverMotd = serverMOTD;
			((NetworkManager)current).maxConnections = serverMaxPlayers;
			current3._lobbyPasswordInput.text = current._serverPassword;
			current3._lobbyTypeDropdown.value = (int)serverType;
			current3._lobbyFocusDropdown.value = (int)serverFocus;
			current._bannedClientList.Clear();
			current._mutedClientList.Clear();
			if (hostSettingsProfile._banList != null)
			{
				current._bannedClientList.AddRange(hostSettingsProfile._banList);
			}
			if (hostSettingsProfile._mutedList != null)
			{
				current._mutedClientList.AddRange(hostSettingsProfile._mutedList);
			}
			ELobbyType val = (ELobbyType)2;
			switch ((int)serverType)
			{
			case 0:
				val = (ELobbyType)2;
				break;
			case 1:
				val = (ELobbyType)1;
				break;
			case 2:
				val = (ELobbyType)0;
				break;
			}
			current2._characterFile = current2._characterFiles[hostCharSaveSlot];
			SteamLobby._current.HostLobby(val);
			MainMenuManager._current.Connect_ToServer();
		}
	}
	public static class PluginInfo
	{
		public const string PLUGIN_GUID = "FleesDedicatedServer";

		public const string PLUGIN_NAME = "Atlyss_Server";

		public const string PLUGIN_VERSION = "0.0.2";
	}
	public static class MyPluginInfo
	{
		public const string PLUGIN_GUID = "AtlyssDedicatedServer";

		public const string PLUGIN_NAME = "AtlyssDedicatedServer";

		public const string PLUGIN_VERSION = "0.0.1";
	}
}