Decompiled source of TwitchChatAPI LethalCompany v1.0.0

TwitchChatAPI.LethalCompany.dll

Decompiled 2 weeks ago
using System;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Bootstrap;
using BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using TMPro;
using TwitchChatAPI.Enums;
using TwitchChatAPI.LethalCompany.Dependencies;
using TwitchChatAPI.LethalCompany.Helpers;
using TwitchChatAPI.LethalCompany.MonoBehaviours;
using TwitchChatAPI.LethalCompany.Patches;
using TwitchChatAPI.Objects;
using UnityEngine;
using UnityEngine.UI;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: IgnoresAccessChecksTo("TwitchChatAPI")]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("Zehs")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyCopyright("Copyright © 2025 Zehs")]
[assembly: AssemblyDescription("TwitchChatAPI wrapper for Lethal Company.")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0+c89e420a220228b54787d726eb06645ec5a3ef6d")]
[assembly: AssemblyProduct("TwitchChatAPI LethalCompany")]
[assembly: AssemblyTitle("TwitchChatAPI.LethalCompany")]
[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 TwitchChatAPI.LethalCompany
{
	internal static class Assets
	{
		public static GameObject PluginCanvasPrefab { get; private set; }

		public static void Load()
		{
			string directoryName = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
			string text = "twitchchatapilethalcompany_assets";
			string text2 = Path.Combine(directoryName, text);
			if (!File.Exists(text2))
			{
				Logger.LogFatal("Failed to load assets. AssetBundle file could not be found at path \"" + text2 + "\". Make sure the \"" + text + "\" file is in the same folder as the mod's DLL file.");
			}
			else
			{
				AssetBundle val = AssetBundle.LoadFromFile(text2);
				if ((Object)(object)val == (Object)null)
				{
					Logger.LogFatal("Failed to load assets. AssetBundle is null.");
				}
				else
				{
					OnAssetBundleLoaded(val);
				}
			}
		}

		private static void OnAssetBundleLoaded(AssetBundle assetBundle)
		{
			PluginCanvasPrefab = LoadAsset<GameObject>("PluginCanvas", assetBundle);
		}

		private static T LoadAsset<T>(string name, AssetBundle assetBundle) where T : Object
		{
			if (string.IsNullOrWhiteSpace(name))
			{
				Logger.LogError("Failed to load asset of type \"" + typeof(T).Name + "\" from AssetBundle. Name is null or whitespace.");
				return default(T);
			}
			if ((Object)(object)assetBundle == (Object)null)
			{
				Logger.LogError("Failed to load asset of type \"" + typeof(T).Name + "\" with name \"" + name + "\" from AssetBundle. AssetBundle is null.");
				return default(T);
			}
			T val = assetBundle.LoadAsset<T>(name);
			if ((Object)(object)val == (Object)null)
			{
				Logger.LogError("Failed to load asset of type \"" + typeof(T).Name + "\" with name \"" + name + "\" from AssetBundle. No asset found with that type and name.");
				return default(T);
			}
			return val;
		}

		private static bool TryLoadAsset<T>(string name, AssetBundle assetBundle, out T asset) where T : Object
		{
			asset = LoadAsset<T>(name, assetBundle);
			return (Object)(object)asset != (Object)null;
		}
	}
	internal static class Logger
	{
		public static ManualLogSource ManualLogSource { get; private set; }

		public static void Initialize(ManualLogSource manualLogSource)
		{
			ManualLogSource = manualLogSource;
		}

		public static void LogDebug(object data)
		{
			Log((LogLevel)32, data);
		}

		public static void LogInfo(object data, bool extended = false)
		{
			Log((LogLevel)16, data, extended);
		}

		public static void LogWarning(object data, bool extended = false)
		{
			Log((LogLevel)4, data, extended);
		}

		public static void LogError(object data, bool extended = false)
		{
			Log((LogLevel)2, data, extended);
		}

		public static void LogFatal(object data, bool extended = false)
		{
			Log((LogLevel)1, data, extended);
		}

		public static void Log(LogLevel logLevel, object data, bool extended = false)
		{
			//IL_0021: Unknown result type (might be due to invalid IL or missing references)
			if (!extended || IsExtendedLoggingEnabled())
			{
				ManualLogSource manualLogSource = ManualLogSource;
				if (manualLogSource != null)
				{
					manualLogSource.Log(logLevel, data);
				}
			}
		}

		public static bool IsExtendedLoggingEnabled()
		{
			return Logger.IsExtendedLoggingEnabled();
		}
	}
	[BepInPlugin("TwitchChatAPI.LethalCompany", "TwitchChatAPI LethalCompany", "1.0.0")]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	internal class Plugin : BaseUnityPlugin
	{
		private readonly Harmony _harmony = new Harmony("TwitchChatAPI.LethalCompany");

		internal static Plugin Instance { get; private set; }

		internal static JsonSave GlobalSave => Plugin.GlobalSave;

		private void Awake()
		{
			Instance = this;
			Logger.Initialize(Logger.CreateLogSource("TwitchChatAPI.LethalCompany"));
			Logger.LogInfo("TwitchChatAPI LethalCompany has awoken!");
			_harmony.PatchAll(typeof(MenuManagerPatch));
			_harmony.PatchAll(typeof(QuickMenuManagerPatch));
			Assets.Load();
		}
	}
	public static class MyPluginInfo
	{
		public const string PLUGIN_GUID = "TwitchChatAPI.LethalCompany";

		public const string PLUGIN_NAME = "TwitchChatAPI LethalCompany";

		public const string PLUGIN_VERSION = "1.0.0";
	}
}
namespace TwitchChatAPI.LethalCompany.Patches
{
	[HarmonyPatch(typeof(MenuManager))]
	internal static class MenuManagerPatch
	{
		[HarmonyPatch("Awake")]
		[HarmonyPrefix]
		private static void AwakePatch(MenuManager __instance)
		{
			MenuManagerHelper.Instance = __instance;
		}

		[HarmonyPatch("Start")]
		[HarmonyPostfix]
		private static void StartPatch(MenuManager __instance)
		{
			if (!__instance.isInitScene)
			{
				PluginCanvas.Spawn();
			}
		}
	}
	[HarmonyPatch(typeof(QuickMenuManager))]
	internal static class QuickMenuManagerPatch
	{
		[HarmonyPatch("Start")]
		[HarmonyPrefix]
		private static void StartPatch(QuickMenuManager __instance)
		{
			QuickMenuManagerHelper.Instance = __instance;
			PluginCanvas.Spawn();
		}

		[HarmonyPatch("OpenQuickMenu")]
		[HarmonyPostfix]
		private static void OpenQuickMenuPatch()
		{
			PluginCanvas.Instance?.UpdateSettingsButton();
		}

		[HarmonyPatch("CloseQuickMenu")]
		[HarmonyPostfix]
		private static void CloseQuickMenuPatch()
		{
			PluginCanvas.Instance?.CloseSettingsWindow();
		}
	}
}
namespace TwitchChatAPI.LethalCompany.MonoBehaviours
{
	public class PluginCanvas : MonoBehaviour
	{
		[Header("Windows")]
		[Space(5f)]
		public GameObject SettingsWindowObject;

		public GameObject MainMenuObject;

		public GameObject QuickMenuObject;

		[Header("Settings Window Properties")]
		[Space(5f)]
		public TextMeshProUGUI ConnectionStatusText;

		public Toggle EnabledToggle;

		public TMP_InputField ChannelInputField;

		[Header("MainMenu Properties")]
		[Space(5f)]
		public RectTransform MainMenuSettingsButtonTransform;

		[Header("QuickMenu Properties")]
		[Space(5f)]
		public RectTransform QuickMenuSettingsButtonTransform;

		public static PluginCanvas Instance { get; private set; }

		public bool IsSettingsWindowOpen { get; private set; }

		public static void Spawn()
		{
			if (!((Object)(object)Instance != (Object)null))
			{
				Object.Instantiate<GameObject>(Assets.PluginCanvasPrefab);
			}
		}

		private void Awake()
		{
			if ((Object)(object)Instance != (Object)null && (Object)(object)Instance != (Object)(object)this)
			{
				Object.Destroy((Object)(object)((Component)this).gameObject);
				return;
			}
			Instance = this;
			((Object)((Component)this).gameObject).name = "TwitchChatAPI LethalCompany Canvas";
			Logger.LogInfo("Instantiated \"" + ((Object)((Component)this).gameObject).name + "\"", extended: true);
		}

		private void Start()
		{
			CloseSettingsWindow();
			OpenSettingsWindowFirstTimeOnly();
			UpdateSettingsButton();
		}

		private void OnEnable()
		{
			API.OnConnectionStateChanged += HandleConnectionStateChanged;
		}

		private void OnDisable()
		{
			API.OnConnectionStateChanged -= HandleConnectionStateChanged;
		}

		private void OpenSettingsWindowFirstTimeOnly()
		{
			if (!Plugin.GlobalSave.Load<bool>("OpenedSettingsWindowFirstTime", false, false))
			{
				Plugin.GlobalSave.Save<bool>("OpenedSettingsWindowFirstTime", true);
				OpenSettingsWindow();
			}
		}

		public void OpenSettingsWindow()
		{
			if (!((Object)(object)SettingsWindowObject == (Object)null) && !IsSettingsWindowOpen && (!((Object)(object)QuickMenuManagerHelper.Instance != (Object)null) || QuickMenuManagerHelper.IsMenuOpen))
			{
				IsSettingsWindowOpen = true;
				SettingsWindowObject.SetActive(true);
				UpdateSettingsWindowUI();
				UpdateSettingsButton();
			}
		}

		public void CloseSettingsWindow()
		{
			if (!((Object)(object)SettingsWindowObject == (Object)null))
			{
				IsSettingsWindowOpen = false;
				SettingsWindowObject.SetActive(false);
				UpdateSettingsButton();
			}
		}

		public void OnApplyButtonClicked()
		{
			if ((Object)(object)EnabledToggle != (Object)null)
			{
				ConfigManager.TwitchChat_Enabled.Value = EnabledToggle.isOn;
			}
			if ((Object)(object)ChannelInputField != (Object)null)
			{
				ConfigManager.TwitchChat_Channel.Value = ChannelInputField.text;
			}
		}

		public void OnCloseButtonClicked()
		{
			CloseSettingsWindow();
		}

		private void UpdateSettingsWindowUI()
		{
			//IL_0052: Unknown result type (might be due to invalid IL or missing references)
			if ((Object)(object)EnabledToggle != (Object)null)
			{
				EnabledToggle.isOn = ConfigManager.TwitchChat_Enabled.Value;
			}
			if ((Object)(object)ChannelInputField != (Object)null)
			{
				ChannelInputField.text = ConfigManager.TwitchChat_Channel.Value;
			}
			HandleConnectionStateChanged(API.ConnectionState);
		}

		public void HandleConnectionStateChanged(ConnectionState connectionState)
		{
			//IL_001b: Unknown result type (might be due to invalid IL or missing references)
			//IL_001d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0033: Expected I4, but got Unknown
			//IL_0071: Unknown result type (might be due to invalid IL or missing references)
			if (!((Object)(object)ConnectionStatusText == (Object)null))
			{
				if (1 == 0)
				{
				}
				string text = (connectionState - 1) switch
				{
					0 => "#00FF00", 
					1 => "#00FF00", 
					2 => "#FF0000", 
					3 => "#FF0000", 
					_ => string.Empty, 
				};
				if (1 == 0)
				{
				}
				string text2 = text;
				string text3 = (string.IsNullOrEmpty(text2) ? ((object)(ConnectionState)(ref connectionState)).ToString() : $"<color={text2}>{connectionState}</color>");
				((TMP_Text)ConnectionStatusText).text = "Connection Status: " + text3;
			}
		}

		public void OnSettingsButtonClicked()
		{
			OpenSettingsWindow();
		}

		public void UpdateSettingsButton()
		{
			UpdateMainMenuUI();
			UpdateQuickMenuUI();
		}

		private void UpdateMainMenuUI()
		{
			//IL_0094: Unknown result type (might be due to invalid IL or missing references)
			//IL_0075: Unknown result type (might be due to invalid IL or missing references)
			if ((Object)(object)MainMenuObject == (Object)null)
			{
				return;
			}
			MainMenuObject.SetActive((Object)(object)MenuManagerHelper.Instance != (Object)null);
			if (!((Object)(object)MainMenuSettingsButtonTransform == (Object)null))
			{
				((Component)MainMenuSettingsButtonTransform).gameObject.SetActive(!IsSettingsWindowOpen);
				if (MoreCompanyProxy.Enabled)
				{
					MainMenuSettingsButtonTransform.anchoredPosition = new Vector2(-145f, 48f);
				}
				else
				{
					MainMenuSettingsButtonTransform.anchoredPosition = new Vector2(-24f, 48f);
				}
			}
		}

		private void UpdateQuickMenuUI()
		{
			//IL_008e: Unknown result type (might be due to invalid IL or missing references)
			//IL_006f: Unknown result type (might be due to invalid IL or missing references)
			if ((Object)(object)QuickMenuObject == (Object)null)
			{
				return;
			}
			QuickMenuObject.SetActive(QuickMenuManagerHelper.IsMenuOpen);
			if (!((Object)(object)QuickMenuSettingsButtonTransform == (Object)null))
			{
				((Component)QuickMenuSettingsButtonTransform).gameObject.SetActive(!IsSettingsWindowOpen);
				if (MoreCompanyProxy.Enabled)
				{
					QuickMenuSettingsButtonTransform.anchoredPosition = new Vector2(-169f, 63f);
				}
				else
				{
					QuickMenuSettingsButtonTransform.anchoredPosition = new Vector2(-52f, 63f);
				}
			}
		}
	}
}
namespace TwitchChatAPI.LethalCompany.Helpers
{
	internal static class MenuManagerHelper
	{
		public static MenuManager Instance { get; set; }
	}
	internal static class QuickMenuManagerHelper
	{
		public static QuickMenuManager Instance { get; set; }

		public static bool IsMenuOpen => Instance?.isMenuOpen ?? false;
	}
}
namespace TwitchChatAPI.LethalCompany.Dependencies
{
	internal static class MoreCompanyProxy
	{
		public const string PLUGIN_GUID = "me.swipez.melonloader.morecompany";

		private static bool? _enabled;

		public static bool Enabled
		{
			get
			{
				bool valueOrDefault = _enabled.GetValueOrDefault();
				if (!_enabled.HasValue)
				{
					valueOrDefault = Chainloader.PluginInfos.ContainsKey("me.swipez.melonloader.morecompany");
					_enabled = valueOrDefault;
				}
				return _enabled.Value;
			}
		}
	}
}
namespace System.Runtime.CompilerServices
{
	[AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)]
	internal sealed class IgnoresAccessChecksToAttribute : Attribute
	{
		public IgnoresAccessChecksToAttribute(string assemblyName)
		{
		}
	}
}