Decompiled source of SlipChat v0.1.0

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

Decompiled 3 months ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
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 RegionVo;
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("0.0.1.0")]
[assembly: AssemblyInformationalVersion("0.0.1+7ec3018117ff922180074ea2dcf8e2b1390c2cd6")]
[assembly: AssemblyProduct("SlipChat")]
[assembly: AssemblyTitle("com.mosadie.slipchat")]
[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 SlipChat
{
	[BepInPlugin("com.mosadie.slipchat", "SlipChat", "0.0.1")]
	[BepInProcess("Slipstream_Win.exe")]
	public class Plugin : BaseUnityPlugin
	{
		private static ConfigEntry<int> port;

		private static ConfigEntry<bool> debugMode;

		private static HttpListener listener = null;

		internal static ManualLogSource Log;

		public static readonly string COMPATIBLE_GAME_VERSION = "4.1556";

		private void Awake()
		{
			try
			{
				Log = ((BaseUnityPlugin)this).Logger;
				Log.LogInfo((object)("Game version: " + Application.version));
				if (Application.version != COMPATIBLE_GAME_VERSION)
				{
					Log.LogError((object)"This version of SlipChat is not compatible with the current game version. Please check for an updated version of the plugin.");
					return;
				}
				port = ((BaseUnityPlugin)this).Config.Bind<int>("Server Settings", "Port", 8002, "Port to listen on.");
				debugMode = ((BaseUnityPlugin)this).Config.Bind<bool>("Developer Settings", "Debug Mode", false, "Enable debug mode, preventing the game from actually sending the order.");
				if (!HttpListener.IsSupported)
				{
					Log.LogError((object)"HttpListener is not supported on this platform.");
					listener = null;
					return;
				}
				listener = new HttpListener();
				listener.Prefixes.Add($"http://127.0.0.1:{port.Value}/sendchat/");
				listener.Prefixes.Add($"http://localhost:{port.Value}/sendchat/");
				listener.Start();
				listener.BeginGetContext(HandleRequest, listener);
				((BaseUnityPlugin)this).Logger.LogInfo((object)"Plugin com.mosadie.slipchat is loaded!");
				Application.quitting += ApplicationQuitting;
			}
			catch (PlatformNotSupportedException ex)
			{
				Log.LogError((object)"HttpListener is not supported on this platform.");
				Log.LogError((object)ex.Message);
			}
			catch (Exception ex2)
			{
				Log.LogError((object)"An error occurred while starting the plugin.");
				Log.LogError((object)ex2.Message);
			}
		}

		private void HandleRequest(IAsyncResult result)
		{
			//IL_010f: Unknown result type (might be due to invalid IL or missing references)
			((BaseUnityPlugin)this).Logger.LogInfo((object)"Handling request");
			try
			{
				HttpListener httpListener = (HttpListener)result.AsyncState;
				HttpListenerContext httpListenerContext = httpListener.EndGetContext(result);
				HttpListenerRequest request = httpListenerContext.Request;
				HttpListenerResponse response = httpListenerContext.Response;
				_ = request.RawUrl.Split('?', 2)[0];
				HttpStatusCode statusCode;
				string s;
				if (!getIsCaptainAndOnHelm())
				{
					((BaseUnityPlugin)this).Logger.LogInfo((object)$"Captain Seat check failed. IsCaptain: {getIsCaptain()} AndOnHelm: {getIsCaptainAndOnHelm()}");
					statusCode = HttpStatusCode.Forbidden;
					s = "You are not the captain or are not seated on the helm.";
				}
				else
				{
					string text = request.QueryString["message"];
					((BaseUnityPlugin)this).Logger.LogInfo((object)("Pre-parsed Message: " + text));
					if (text != null)
					{
						text = VariableHandler.ParseVariables(text);
						if (EditableText.IsTextUnusable(text))
						{
							((BaseUnityPlugin)this).Logger.LogInfo((object)$"Message is not usable: Null/Whitespace: {string.IsNullOrWhiteSpace(text)}. Null/Empty: {string.IsNullOrEmpty(text)}");
							statusCode = HttpStatusCode.BadRequest;
							s = "Message is not usable.";
						}
						else
						{
							if (!debugMode.Value)
							{
								RequestCatalog.CaptainIssueOrder((OrderType)7, text, -1);
								((BaseUnityPlugin)this).Logger.LogInfo((object)("Message sent: " + text));
							}
							else
							{
								((BaseUnityPlugin)this).Logger.LogInfo((object)("Debug mode enabled, message not sent: " + text));
							}
							statusCode = HttpStatusCode.OK;
							s = "Message sent!";
						}
					}
					else
					{
						statusCode = HttpStatusCode.BadRequest;
						s = "No message provided.";
					}
				}
				response.StatusCode = (int)statusCode;
				response.Headers.Add("Access-Control-Allow-Origin", "*");
				byte[] bytes = Encoding.UTF8.GetBytes(s);
				response.ContentLength64 = bytes.Length;
				Stream outputStream = response.OutputStream;
				outputStream.Write(bytes, 0, bytes.Length);
				outputStream.Close();
				VariableHandler.Reset();
				httpListener.BeginGetContext(HandleRequest, httpListener);
			}
			catch (Exception ex)
			{
				Log.LogError((object)"An error occurred while handling the request.");
				Log.LogError((object)ex.Message);
			}
		}

		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 getIsCaptainAndOnHelm()
		{
			if (!getIsCaptain())
			{
				return false;
			}
			try
			{
				MpCaptainController captains = Svc.Get<MpSvc>().Captains;
				if (captains == null || captains.CaptainClient == null)
				{
					return false;
				}
				return captains.CaptainClient.IsLocal && captains.IsCaptainAtHelm();
			}
			catch (Exception ex)
			{
				Log.LogError((object)("An error occurred while checking if the crewmate is the captain and seated on the helm: " + ex.Message));
				return false;
			}
		}

		private void ApplicationQuitting()
		{
			((BaseUnityPlugin)this).Logger.LogInfo((object)"Stopping server");
			if (listener != null)
			{
				listener.Close();
			}
		}
	}
	internal class VariableHandler
	{
		private static Dictionary<string, string> crewMap = new Dictionary<string, string>();

		internal static string ParseVariables(string message)
		{
			string[] array = message.Split(' ');
			string text = "";
			string[] array2 = array;
			foreach (string text2 in array2)
			{
				if (text2.StartsWith("$"))
				{
					string variableValue = GetVariableValue(text2.Substring(1));
					text = ((variableValue != null) ? (text + variableValue + " ") : (text + text2 + " "));
				}
				else
				{
					text = text + text2 + " ";
				}
			}
			return text.Trim();
		}

		internal static string GetVariableValue(string variable)
		{
			//IL_04cd: Unknown result type (might be due to invalid IL or missing references)
			//IL_04d2: Unknown result type (might be due to invalid IL or missing references)
			//IL_04fe: Unknown result type (might be due to invalid IL or missing references)
			//IL_0503: Unknown result type (might be due to invalid IL or missing references)
			string text = "";
			while (variable.Length > 0 && !char.IsLetterOrDigit(variable[variable.Length - 1]))
			{
				text = variable[variable.Length - 1] + text;
				variable = variable.Substring(0, variable.Length - 1);
			}
			string text2 = "";
			if (variable.StartsWith("randomCrew"))
			{
				text2 = GetRandomCrewMember(variable.Substring(10, variable.Length - 11));
			}
			else if (variable.StartsWith("crew"))
			{
				text2 = GetCrewMember(variable.Substring(5, variable.Length - 6));
			}
			else
			{
				switch (variable)
				{
				case "version":
					text2 = "0.0.1";
					break;
				case "captain":
					text2 = Svc.Get<MpSvc>().Captains.CaptainClient.Player.DisplayName;
					break;
				case "enemyName":
				{
					MpScenarioController scenarios5 = Svc.Get<MpSvc>().Scenarios;
					text2 = ((scenarios5 != null && scenarios5.CurrentScenario != null && scenarios5.CurrentScenario.Battle != null && scenarios5.CurrentScenario.Battle.Metadata.EnemyName != null) ? scenarios5.CurrentScenario.Battle.Metadata.EnemyName : "");
					break;
				}
				case "enemyIntel":
				{
					MpScenarioController scenarios2 = Svc.Get<MpSvc>().Scenarios;
					text2 = ((scenarios2 != null && scenarios2.CurrentScenario != null && scenarios2.CurrentScenario.Battle != null && scenarios2.CurrentScenario.Battle.Metadata.IntelDescription != null) ? scenarios2.CurrentScenario.Battle.Metadata.IntelDescription : "");
					break;
				}
				case "enemyInvader":
				case "enemyInvaders":
				{
					MpScenarioController scenarios6 = Svc.Get<MpSvc>().Scenarios;
					text2 = ((scenarios6 != null && scenarios6.CurrentScenario != null && scenarios6.CurrentScenario.Battle != null && scenarios6.CurrentScenario.Battle.Metadata.InvaderDescription != null) ? scenarios6.CurrentScenario.Battle.Metadata.InvaderDescription : "");
					break;
				}
				case "enemyThreat":
				{
					MpScenarioController scenarios3 = Svc.Get<MpSvc>().Scenarios;
					text2 = ((scenarios3 != null && scenarios3.CurrentScenario != null && scenarios3.CurrentScenario.Battle != null) ? scenarios3.CurrentScenario.Battle.Metadata.ThreatLevel.ToString() : "");
					break;
				}
				case "enemySpeed":
				{
					MpScenarioController scenarios = Svc.Get<MpSvc>().Scenarios;
					text2 = ((scenarios != null && scenarios.CurrentScenario != null && scenarios.CurrentScenario.Battle != null) ? scenarios.CurrentScenario.Battle.Metadata.SpeedLevel.ToString() : "");
					break;
				}
				case "enemyCargo":
				{
					MpScenarioController scenarios4 = Svc.Get<MpSvc>().Scenarios;
					text2 = ((scenarios4 != null && scenarios4.CurrentScenario != null && scenarios4.CurrentScenario.Battle != null) ? scenarios4.CurrentScenario.Battle.Metadata.CargoLevel.ToString() : "");
					break;
				}
				case "campaignName":
				{
					MpCampaignController campaigns2 = Svc.Get<MpSvc>().Campaigns;
					if (campaigns2 != null && campaigns2.CurrentCampaign != null && campaigns2.CurrentCampaign.CaptainCampaign != null && campaigns2.CurrentCampaign.CaptainCampaign.CampaignVo != null)
					{
						RegionMetadataVo metadata = ((Root)(ref campaigns2.CurrentCampaign.CaptainCampaign.CampaignVo.RegionVo)).Metadata;
						if (((RegionMetadataVo)(ref metadata)).Name != null)
						{
							metadata = ((Root)(ref campaigns2.CurrentCampaign.CaptainCampaign.CampaignVo.RegionVo)).Metadata;
							text2 = ((RegionMetadataVo)(ref metadata)).Name;
							break;
						}
					}
					text2 = "";
					break;
				}
				case "sectorName":
				{
					MpCampaignController campaigns = Svc.Get<MpSvc>().Campaigns;
					text2 = ((campaigns != null && campaigns.CurrentCampaign != null && campaigns.CurrentCampaign.CaptainCampaign != null && campaigns.CurrentCampaign.CaptainCampaign.CampaignVo != null && campaigns.CurrentCampaign.CaptainCampaign.CampaignVo.CurrentSectorVo != null && ((SectorDefVo)(ref campaigns.CurrentCampaign.CaptainCampaign.CampaignVo.CurrentSectorVo.Definition)).Name != null) ? ((SectorDefVo)(ref campaigns.CurrentCampaign.CaptainCampaign.CampaignVo.CurrentSectorVo.Definition)).Name : "");
					break;
				}
				default:
					text2 = "";
					break;
				}
			}
			return text2 + text;
		}

		internal static string GetRandomCrewMember(string id)
		{
			if (crewMap.ContainsKey(id))
			{
				return crewMap[id];
			}
			Dictionary<int, Crewmate> dictionary = Svc.Get<MpSvc>().Crew.CrewMap;
			if (dictionary.Count == 0)
			{
				return "";
			}
			Random random = new Random();
			Crewmate val = dictionary[random.Next(dictionary.Count)];
			crewMap.Add(id, val.Client.Player.DisplayName);
			return val.Client.Player.DisplayName;
		}

		internal static string GetCrewMember(string id)
		{
			Dictionary<int, Crewmate> dictionary = Svc.Get<MpSvc>().Crew.CrewMap;
			if (dictionary.Count == 0)
			{
				return "";
			}
			int result = 0;
			if (!int.TryParse(id, out result))
			{
				return "";
			}
			if (!dictionary.ContainsKey(result))
			{
				return "";
			}
			return dictionary[result].Client.Player.DisplayName;
		}

		internal static void Reset()
		{
			crewMap.Clear();
		}
	}
	public static class PluginInfo
	{
		public const string PLUGIN_GUID = "com.mosadie.slipchat";

		public const string PLUGIN_NAME = "SlipChat";

		public const string PLUGIN_VERSION = "0.0.1";
	}
}