Decompiled source of SlipPronouns v1.0.0

BepInEx/plugins/MoSadie-SlipPronouns/com.mosadie.slippronouns.dll

Decompiled 7 hours ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Net;
using System.Net.Http;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using MoCore;
using Newtonsoft.Json;
using SlipPronouns.pronouns;
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.slippronouns")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyDescription("Adds automatic pronouns to crew members")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0+37536e3d67e12fca99972eacb05c97c59a304d78")]
[assembly: AssemblyProduct("SlipPronouns")]
[assembly: AssemblyTitle("com.mosadie.slippronouns")]
[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 SlipPronouns
{
	[BepInPlugin("com.mosadie.slippronouns", "SlipPronouns", "1.0.0")]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	[BepInProcess("Slipstream_Win.exe")]
	public class SlipPronouns : BaseUnityPlugin, MoPlugin
	{
		private static ConfigEntry<string> url;

		private static ConfigEntry<bool> debugLogs;

		internal static ManualLogSource Log;

		public static PronounFetcher pronounFetcher = new PronounFetcher();

		public static readonly string COMPATIBLE_GAME_VERSION = "4.1579";

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

		private void Awake()
		{
			try
			{
				Log = ((BaseUnityPlugin)this).Logger;
				if (!MoCore.RegisterPlugin((MoPlugin)(object)this))
				{
					Log.LogError((object)"Failed to register plugin with MoCore. Please check the logs for more information.");
					return;
				}
				url = ((BaseUnityPlugin)this).Config.Bind<string>("API Settings", "API URL", "https://api.pronouns.alejo.io/v1", "Url for Alejo's Twitch Pronouns API");
				debugLogs = ((BaseUnityPlugin)this).Config.Bind<bool>("Debug", "DebugLogs", false, "Enable additional logging for debugging");
				pronounFetcher.init(url.Value);
				Harmony.CreateAndPatchAll(typeof(SlipPronouns), (string)null);
				((BaseUnityPlugin)this).Logger.LogInfo((object)"Plugin com.mosadie.slippronouns is loaded!");
			}
			catch (Exception ex)
			{
				Log.LogError((object)"An error occurred while starting the plugin.");
				Log.LogError((object)ex.Message);
			}
		}

		internal static void debugLogInfo(string message)
		{
			if (debugLogs.Value)
			{
				Log.LogInfo((object)message);
			}
		}

		internal static void debugLogWarn(string message)
		{
			if (debugLogs.Value)
			{
				Log.LogWarning((object)message);
			}
		}

		internal static void debugLogError(string message)
		{
			if (debugLogs.Value)
			{
				Log.LogError((object)message);
			}
		}

		internal static void debugLogDebug(string message)
		{
			if (debugLogs.Value)
			{
				Log.LogDebug((object)message);
			}
		}

		internal void crewTest(CrewmateCreatedEvent e)
		{
			try
			{
				if ((Object)(object)e.Crewmate != (Object)null && e.Crewmate.Client != null && e.Crewmate.Client.Player != null)
				{
					string text = ((e.Crewmate.Client.Player.TwitchUserName != null) ? e.Crewmate.Client.Player.TwitchUserName : "null");
					string text2 = ((e.Crewmate.Client.Player.TwitchUserId != null) ? e.Crewmate.Client.Player.TwitchUserId : "null");
					Log.LogInfo((object)("Crewmate created: " + e.Crewmate.Client.Player.DisplayName + " Twitch Info: name:" + text + " id:" + text2));
				}
			}
			catch (Exception ex)
			{
				Log.LogError((object)"An error occurred while doing the crewmate created event test.");
				Log.LogError((object)ex.Message);
			}
		}

		public string GetCompatibleGameVersion()
		{
			return COMPATIBLE_GAME_VERSION;
		}

		public string GetVersionCheckUrl()
		{
			return GAME_VERSION_URL;
		}

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

		[HarmonyPatch(/*Could not decode attribute arguments.*/)]
		[HarmonyPostfix]
		[HarmonyWrapSafe]
		private static void addToDisplayName(ref Player __instance, ref string __result)
		{
			if (__instance.TwitchUserName != null)
			{
				__result += pronounFetcher.getSuffix(__result);
			}
		}
	}
	public static class PluginInfo
	{
		public const string PLUGIN_GUID = "com.mosadie.slippronouns";

		public const string PLUGIN_NAME = "SlipPronouns";

		public const string PLUGIN_VERSION = "1.0.0";
	}
}
namespace SlipPronouns.pronouns
{
	public class PronounFetcher
	{
		private string url;

		private Dictionary<string, PronounResponse> pronounsCache;

		private Dictionary<string, PronounInfo> pronounInfo;

		private static HttpClient client;

		public PronounFetcher()
		{
			url = null;
			pronounsCache = new Dictionary<string, PronounResponse>();
			pronounInfo = new Dictionary<string, PronounInfo>();
			client = new HttpClient();
			client.DefaultRequestHeaders.Add("User-Agent", "SlipPronouns/MoSadie 1.0.0");
		}

		public void init(string url)
		{
			SlipPronouns.Log.LogInfo((object)("Initializing PronounFetcher with URL: " + url));
			this.url = url;
			try
			{
				string requestUri = url + "/pronouns";
				HttpResponseMessage result = client.GetAsync(requestUri).Result;
				if (result.IsSuccessStatusCode)
				{
					Dictionary<string, PronounInfo> dictionary = JsonConvert.DeserializeObject<Dictionary<string, PronounInfo>>(result.Content.ReadAsStringAsync().Result);
					pronounInfo = dictionary;
					SlipPronouns.Log.LogInfo((object)"Pronouns list fetched successfully");
				}
				else
				{
					SlipPronouns.Log.LogError((object)"An unknown error occurred while fetching pronouns");
					SlipPronouns.Log.LogError((object)$"Status code: {result.StatusCode}");
					this.url = null;
				}
			}
			catch (Exception ex)
			{
				SlipPronouns.Log.LogError((object)"An error occurred while fetching pronouns");
				SlipPronouns.Log.LogError((object)ex.Message);
				this.url = null;
			}
		}

		public string getSuffix(string user)
		{
			if (pronounsCache.ContainsKey(user))
			{
				return getSuffix(pronounsCache[user]);
			}
			PronounResponse pronounResponse = fetchUserPronouns(user);
			pronounsCache[user] = pronounResponse;
			return getSuffix(pronounResponse);
		}

		private PronounResponse fetchUserPronouns(string user)
		{
			if (url == null || url == "" || url == " ")
			{
				SlipPronouns.Log.LogWarning((object)"API URL is not set. Please set the API URL before attempting to fetch pronouns.");
				return null;
			}
			try
			{
				string requestUri = url + "/users/" + user;
				HttpResponseMessage result = client.GetAsync(requestUri).Result;
				if (result.IsSuccessStatusCode)
				{
					PronounResponse result2 = JsonConvert.DeserializeObject<PronounResponse>(result.Content.ReadAsStringAsync().Result);
					SlipPronouns.debugLogInfo("Pronouns fetched successfully for " + user);
					return result2;
				}
				if (result.StatusCode == HttpStatusCode.NotFound)
				{
					SlipPronouns.debugLogWarn("No pronouns found for " + user);
					return null;
				}
				SlipPronouns.Log.LogError((object)("An unknown error occurred while fetching pronouns for " + user));
				SlipPronouns.Log.LogError((object)$"Status code: {result.StatusCode}");
				return null;
			}
			catch (Exception ex)
			{
				SlipPronouns.Log.LogError((object)("An error occurred while fetching pronouns for " + user));
				SlipPronouns.Log.LogError((object)ex.Message);
				return null;
			}
		}

		public string getSuffix(PronounResponse userPronouns)
		{
			if (userPronouns == null)
			{
				return "";
			}
			if (this.pronounInfo.ContainsKey(userPronouns.pronoun_id))
			{
				if (userPronouns.alt_pronoun_id != null && this.pronounInfo.ContainsKey(userPronouns.alt_pronoun_id))
				{
					return " (" + this.pronounInfo[userPronouns.pronoun_id].subject + "/" + this.pronounInfo[userPronouns.alt_pronoun_id].subject + ")";
				}
				PronounInfo pronounInfo = this.pronounInfo[userPronouns.pronoun_id];
				if (pronounInfo.singular)
				{
					return " (" + pronounInfo.subject + ")";
				}
				return " (" + pronounInfo.subject + "/" + pronounInfo.obj + ")";
			}
			return "";
		}
	}
	public class PronounInfo
	{
		public string name;

		public string subject;

		[JsonProperty("object")]
		public string obj;

		public bool singular;
	}
	public class PronounResponse
	{
		internal string channel_id;

		internal string channel_login;

		internal string pronoun_id;

		internal string alt_pronoun_id;

		public PronounResponse(string channel_id, string channel_login, string pronoun_id, string alt_pronoun_id)
		{
			this.channel_id = channel_id;
			this.channel_login = channel_login;
			this.pronoun_id = pronoun_id;
			this.alt_pronoun_id = alt_pronoun_id;
		}
	}
}