Decompiled source of SlipChat v1.0.1

BepInEx/plugins/MoSadie-SlipChat/com.mosadie.slipchat.dll

Decompiled 2 months ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Net;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using System.Text;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using Microsoft.CodeAnalysis;
using MoCore;
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("com.mosadie.slipchat")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyDescription("Local API for sending in-game announcements.")]
[assembly: AssemblyFileVersion("1.0.1.0")]
[assembly: AssemblyInformationalVersion("1.0.1-alpha.0.4+555acf22d8a5b74688d65ccb844790b628411fd2")]
[assembly: AssemblyProduct("SlipChat")]
[assembly: AssemblyTitle("com.mosadie.slipchat")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.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.Module, AllowMultiple = false, Inherited = false)]
	internal sealed class RefSafetyRulesAttribute : Attribute
	{
		public readonly int Version;

		public RefSafetyRulesAttribute(int P_0)
		{
			Version = P_0;
		}
	}
}
namespace SlipChat
{
	[BepInPlugin("com.mosadie.slipchat", "SlipChat", "1.0.0")]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	[BepInProcess("Slipstream_Win.exe")]
	public class SlipChat : BaseUnityPlugin, IMoPlugin, IMoHttpHandler
	{
		private static ConfigEntry<bool> debugMode;

		internal static ManualLogSource Log;

		public static readonly string HTTP_PREFIX = "slipchat";

		public static readonly string COMPATIBLE_GAME_VERSION = "4.1595";

		public static readonly string GAME_VERSION_URL = "https://raw.githubusercontent.com/MoSadie/SlipChat/refs/heads/main/versions.json";

		private void Awake()
		{
			try
			{
				Log = ((BaseUnityPlugin)this).Logger;
				if (!MoCore.RegisterPlugin((IMoPlugin)(object)this))
				{
					Log.LogError((object)"Failed to register plugin with MoCore. Please check the logs for more information.");
					return;
				}
				debugMode = ((BaseUnityPlugin)this).Config.Bind<bool>("Developer Settings", "Debug Mode", false, "Enable debug mode, preventing the game from actually sending the order.");
				((BaseUnityPlugin)this).Logger.LogInfo((object)"Plugin com.mosadie.slipchat is loaded!");
			}
			catch (Exception ex)
			{
				Log.LogError((object)"An error occurred while starting the plugin.");
				Log.LogError((object)ex.Message);
			}
		}

		public HttpListenerResponse HandleRequest(HttpListenerRequest request, HttpListenerResponse response)
		{
			//IL_0229: Unknown result type (might be due to invalid IL or missing references)
			((BaseUnityPlugin)this).Logger.LogInfo((object)"Handling request");
			try
			{
				string[] array = request.Url.AbsolutePath.Trim('/').Split('/');
				if (array.Length < 2 || array[0] != HTTP_PREFIX)
				{
					((BaseUnityPlugin)this).Logger.LogInfo((object)"Invalid request path.");
					response.StatusCode = 404;
					response.Headers.Add("Access-Control-Allow-Origin", "*");
					return response;
				}
				if (array[1] != "sendchat")
				{
					((BaseUnityPlugin)this).Logger.LogInfo((object)"Unknown request path.");
					response.StatusCode = 404;
					response.Headers.Add("Access-Control-Allow-Origin", "*");
					return response;
				}
				bool flag = CanUseAndOnHelm();
				if (!flag)
				{
					((BaseUnityPlugin)this).Logger.LogInfo((object)$"Captain Seat check failed. IsCaptain: {GetIsCaptain()} IsFirstMate: {GetIsFirstMate()} AndOnHelm: {flag}");
					response.StatusCode = 403;
					response.Headers.Add("Access-Control-Allow-Origin", "*");
					string s = "You are not the captain/first mate or are not seated on the helm.";
					byte[] bytes = Encoding.UTF8.GetBytes(s);
					response.ContentLength64 = bytes.Length;
					response.OutputStream.Write(bytes, 0, bytes.Length);
					return response;
				}
				string text = request.QueryString["message"];
				((BaseUnityPlugin)this).Logger.LogInfo((object)("Pre-parsed Message: " + text));
				if (text != null)
				{
					text = VariableHandler.ParseVariables(text);
					if (!EditableText.IsTextUsable(text))
					{
						((BaseUnityPlugin)this).Logger.LogInfo((object)$"Message is not usable: Null/Whitespace: {string.IsNullOrWhiteSpace(text)}. Null/Empty: {string.IsNullOrEmpty(text)}");
						response.StatusCode = 400;
						response.Headers.Add("Access-Control-Allow-Origin", "*");
						string s2 = "Message is not usable.";
						byte[] bytes2 = Encoding.UTF8.GetBytes(s2);
						response.ContentLength64 = bytes2.Length;
						response.OutputStream.Write(bytes2, 0, bytes2.Length);
						return response;
					}
					if (!debugMode.Value)
					{
						RequestCatalog.CaptainIssueOrderAll((OrderType)7, text);
						((BaseUnityPlugin)this).Logger.LogInfo((object)("Message sent: " + text));
					}
					else
					{
						((BaseUnityPlugin)this).Logger.LogInfo((object)("Debug mode enabled, message not sent: " + text));
					}
					response.StatusCode = 200;
					response.Headers.Add("Access-Control-Allow-Origin", "*");
					string s3 = "Message sent!";
					byte[] bytes3 = Encoding.UTF8.GetBytes(s3);
					response.ContentLength64 = bytes3.Length;
					response.OutputStream.Write(bytes3, 0, bytes3.Length);
					return response;
				}
				response.StatusCode = 400;
				response.Headers.Add("Access-Control-Allow-Origin", "*");
				string s4 = "No message provided.";
				byte[] bytes4 = Encoding.UTF8.GetBytes(s4);
				response.ContentLength64 = bytes4.Length;
				response.OutputStream.Write(bytes4, 0, bytes4.Length);
				return response;
			}
			catch (Exception ex)
			{
				Log.LogError((object)("An error occurred while handling the request. " + ex.Message));
				Log.LogError((object)ex.StackTrace);
				return response;
			}
		}

		private static bool GetIsCaptain()
		{
			try
			{
				if (Svc.Get<MpSvc>() == null)
				{
					Log.LogError((object)"An error occurred handling self crew. null MpSvc.");
					return false;
				}
				MpCaptainController captains = Svc.Get<MpSvc>().Captains;
				if (captains == null || captains.CaptainClient == null)
				{
					return false;
				}
				return captains.CaptainClient.IsLocal;
			}
			catch (Exception ex)
			{
				Log.LogError((object)("An error occurred while checking if the crewmate is the captain: " + ex.Message));
				return false;
			}
		}

		private static bool GetIsFirstMate()
		{
			try
			{
				if (Svc.Get<MpSvc>() == null)
				{
					Log.LogError((object)"An error occurred handling self crew. null MpSvc.");
					return false;
				}
				MpClientController clients = Svc.Get<MpSvc>().Clients;
				if (clients == null || clients.LocalClient == null)
				{
					return false;
				}
				return ((SlipClient)clients.LocalClient).Roles.Has(Roles.FirstMate);
			}
			catch (Exception ex)
			{
				Log.LogError((object)("An error occurred while checking if the crewmate is the first mate: " + ex.Message));
				return false;
			}
		}

		private static bool CanUseAndOnHelm()
		{
			//IL_00fd: Unknown result type (might be due to invalid IL or missing references)
			if (!GetIsCaptain() && !GetIsFirstMate())
			{
				Log.LogInfo((object)"Not captain or first mate.");
				return false;
			}
			try
			{
				MpSvc val = Svc.Get<MpSvc>();
				if (val == null)
				{
					Log.LogError((object)"An error occurred handling helm check. null MpSvc.");
					return false;
				}
				MpClientController clients = val.Clients;
				if (clients == null)
				{
					Log.LogWarning((object)"An error occurred handling helm check. null Clients.");
					return false;
				}
				LocalSlipClient localClient = clients.LocalClient;
				if (localClient == null)
				{
					Log.LogWarning((object)"An error occurred handling helm check. null LocalClient.");
					return false;
				}
				List<Crewmate> crew = ((SlipClient)localClient).Crew;
				if (crew == null)
				{
					Log.LogWarning((object)"An error occurred handling helm check. null Crew list.");
					return false;
				}
				for (int i = 0; i < crew.Count; i++)
				{
					try
					{
						Log.LogInfo((object)string.Format("Checking crewmate {0}: {1} {2}", i, crew[i].Client.Player.DisplayName, ((Object)(object)crew[i].CurrentStation != (Object)null) ? ((object)crew[i].CurrentStation.StationType) : "No Station"));
						if ((Object)(object)crew[i] != (Object)null && (Object)(object)crew[i].CurrentStation != (Object)null && ((object)(StationType)(ref crew[i].CurrentStation.StationType)).Equals((object)(StationType)3))
						{
							Log.LogInfo((object)"Found valid crew on helm.");
							return true;
						}
					}
					catch (Exception ex)
					{
						Log.LogError((object)$"An error occurred while checking crew member {i}: {ex.Message}");
						Log.LogError((object)ex.StackTrace);
					}
				}
				Log.LogInfo((object)"No valid crew on helm.");
				return false;
			}
			catch (Exception ex2)
			{
				Log.LogError((object)("An error occurred while checking if the crewmate is the captain/first mate and seated on the helm: " + ex2.Message));
				Log.LogError((object)ex2.StackTrace);
				return false;
			}
		}

		public string GetCompatibleGameVersion()
		{
			return COMPATIBLE_GAME_VERSION;
		}

		public string GetVersionCheckUrl()
		{
			return GAME_VERSION_URL;
		}

		public BaseUnityPlugin GetPluginObject()
		{
			return (BaseUnityPlugin)(object)this;
		}

		public IMoHttpHandler GetHttpHandler()
		{
			return (IMoHttpHandler)(object)this;
		}

		public string GetPrefix()
		{
			return HTTP_PREFIX;
		}
	}
	public static class PluginInfo
	{
		public const string PLUGIN_GUID = "com.mosadie.slipchat";

		public const string PLUGIN_NAME = "SlipChat";

		public const string PLUGIN_VERSION = "1.0.0";
	}
}