using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Logging;
using HarmonyLib;
using TMPro;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: AssemblyVersion("0.0.0.0")]
namespace ChatTerminal;
[BepInPlugin("bmatusiask.ChatTerminal", "ChatTerminal", "1.0.1")]
public class ChatTerminalPlugin : BaseUnityPlugin
{
private const string modGUID = "bmatusiask.ChatTerminal";
private const string modName = "ChatTerminal";
private const string modVersion = "1.0.1";
private readonly Harmony harmony = new Harmony("bmatusiask.ChatTerminal");
public static BaseUnityPlugin Instance { get; private set; }
internal ManualLogSource Log { get; set; }
private void Awake()
{
if ((Object)(object)Instance == (Object)null)
{
Instance = (BaseUnityPlugin)(object)this;
}
Log = Logger.CreateLogSource("ChatTerminal");
Log.LogInfo((object)"Patching");
harmony.PatchAll(typeof(PluginPatch));
}
}
internal class PluginPatch
{
private static Terminal terminal
{
get
{
//IL_000f: Unknown result type (might be due to invalid IL or missing references)
//IL_0015: Expected O, but got Unknown
return (Terminal)Object.FindObjectOfType(typeof(Terminal));
}
set
{
}
}
private static int totalCostOfItems
{
get
{
return (int)((object)terminal).GetType().GetField("totalCostOfItems", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(terminal);
}
set
{
((object)terminal).GetType().GetField("totalCostOfItems", BindingFlags.Instance | BindingFlags.NonPublic).SetValue(terminal, value);
}
}
private static bool usedTerminalThisSession
{
get
{
return (bool)((object)terminal).GetType().GetField("usedTerminalThisSession", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(terminal);
}
set
{
((object)terminal).GetType().GetField("usedTerminalThisSession", BindingFlags.Instance | BindingFlags.NonPublic).SetValue(terminal, value);
}
}
[HarmonyPrefix]
[HarmonyPatch(typeof(Terminal), "PlayTerminalAudioServerRpc")]
public static void TerminalPlayTerminalAudioServerRpc_Pre(ref Terminal __instance, ref int clipIndex)
{
if (!__instance.terminalInUse)
{
clipIndex = -1;
}
}
[HarmonyPostfix]
[HarmonyPatch(typeof(HUDManager), "Update")]
private static void HUDManagerUpdate_Post(ref TMP_InputField ___chatTextField, ref HUDElement ___Chat)
{
//IL_0031: Unknown result type (might be due to invalid IL or missing references)
//IL_001e: Unknown result type (might be due to invalid IL or missing references)
//IL_0042: Unknown result type (might be due to invalid IL or missing references)
if (((Behaviour)___chatTextField).isActiveAndEnabled)
{
if (___Chat.targetAlpha == 1f)
{
___chatTextField.placeholder.color = Color.white;
}
else
{
___chatTextField.placeholder.color = Color.blue;
}
((Graphic)___chatTextField.textComponent).color = Color.white;
if (!usedTerminalThisSession)
{
OnSubmit("");
}
}
}
[HarmonyPatch(typeof(HUDManager), "SubmitChat_performed")]
[HarmonyPrefix]
private static void HUDManagerSubmitChat_performed_Pre()
{
if (HUDManager.Instance.localPlayer.isTypingChat)
{
string text = HUDManager.Instance.chatTextField.text;
if (text.StartsWith("/"))
{
HUDManager.Instance.chatTextField.text = text.Substring(1);
return;
}
HUDManager.Instance.localPlayer.isTypingChat = false;
HUDManager.Instance.chatTextField.text = "";
EventSystem.current.SetSelectedGameObject((GameObject)null);
((Behaviour)HUDManager.Instance.typingIndicator).enabled = false;
OnSubmit(text);
}
}
public static bool DetectBang(Terminal terminal)
{
if (terminal.textAdded == 0)
{
return false;
}
string text = terminal.screenText.text;
int textAdded = terminal.textAdded;
int length = text.Length;
int num = length - textAdded;
string text2 = text.Substring(num, length - num);
return text2[text2.Length - 1] == '!';
}
private static void OnSubmit(string cmd_text = null)
{
string text = "";
bool flag = false;
bool flag2 = false;
string[] array = new string[0];
if (cmd_text != null)
{
array = cmd_text.Split(new char[1] { ' ' });
}
FieldInfo field = ((object)terminal).GetType().GetField("usedTerminalThisSession", BindingFlags.Instance | BindingFlags.NonPublic);
bool num = (bool)field.GetValue(terminal);
if (!num)
{
LoadNewNode(terminal.terminalNodes.specialNodes[1]);
text = ExtractTerminalOutput("", terminal.screenText.text);
AddMessage(text.Trim());
}
if (!num)
{
field.SetValue(terminal, true);
if (!(bool)((object)terminal).GetType().GetField("usedTerminalThisSession", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(terminal))
{
terminal.SyncTerminalValuesServerRpc();
}
LoadNewNode(terminal.terminalNodes.specialNodes[13]);
text = ExtractTerminalOutput("", terminal.screenText.text);
if (cmd_text != null && cmd_text.Length > 0)
{
flag2 = true;
}
}
else if (cmd_text != null)
{
if (array[0] == "clear")
{
HUDManager.Instance.ChatMessageHistory.Clear();
((TMP_Text)HUDManager.Instance.chatText).text = "";
HUDManager.Instance.lastChatMessage = "";
return;
}
if (array[0] == "?")
{
string text2 = terminal.screenText.text;
LoadNewNode(terminal.terminalNodes.specialNodes[13]);
text = ExtractTerminalOutput(text2, terminal.screenText.text);
}
else
{
TMP_InputField screenText = terminal.screenText;
screenText.text += cmd_text;
flag = DetectBang(terminal);
if ((Object)(object)terminal.currentNode != (Object)null && terminal.currentNode.acceptAnything)
{
LoadNewNode(terminal.currentNode.terminalOptions[0].result);
}
else
{
TerminalNode val = ParsePlayerSentence();
if ((Object)(object)val != (Object)null)
{
string text3 = terminal.screenText.text;
if (val.buyRerouteToMoon == -2)
{
totalCostOfItems = val.itemCost;
}
else if (val.itemCost != 0)
{
totalCostOfItems = val.itemCost * terminal.playerDefinedAmount;
}
if (val.buyItemIndex != -1 || (val.buyRerouteToMoon != -1 && val.buyRerouteToMoon != -2) || val.shipUnlockableID != -1)
{
LoadNewNodeIfAffordable(val);
}
else if (val.creatureFileID != -1)
{
AttemptLoadCreatureFileNode(val);
}
else if (val.storyLogFileID != -1)
{
AttemptLoadStoryLogFileNode(val);
}
else
{
LoadNewNode(val, cmd_text);
}
text = ExtractTerminalOutput(text3, terminal.screenText.text);
}
else
{
terminal.screenText.text = terminal.screenText.text.Substring(0, terminal.screenText.text.Length - terminal.textAdded);
terminal.currentText = terminal.screenText.text;
terminal.textAdded = 0;
}
}
}
}
if (text != "")
{
AddMessage(text.Trim());
if (flag)
{
OnSubmit("CONFIRM");
}
}
if (flag2)
{
OnSubmit(cmd_text);
}
}
private static string ExtractTerminalOutput(string previous, string output)
{
if (output.StartsWith(previous))
{
output = output.Substring(previous.Length);
}
return output;
}
private static void LoadNewNode(TerminalNode terminalNode, string name = "PROGRAMED")
{
terminal.LoadNewNode(terminalNode);
}
private static TerminalNode ParsePlayerSentence()
{
//IL_0021: Unknown result type (might be due to invalid IL or missing references)
//IL_0027: Expected O, but got Unknown
return (TerminalNode)((object)terminal).GetType().GetMethod("ParsePlayerSentence", BindingFlags.Instance | BindingFlags.NonPublic).Invoke(terminal, null);
}
private static void LoadNewNodeIfAffordable(TerminalNode terminalNode)
{
((object)terminal).GetType().GetMethod("LoadNewNodeIfAffordable", BindingFlags.Instance | BindingFlags.NonPublic).Invoke(terminal, new object[1] { terminalNode });
}
private static void AttemptLoadCreatureFileNode(TerminalNode terminalNode)
{
((object)terminal).GetType().GetMethod("AttemptLoadCreatureFileNode", BindingFlags.Instance | BindingFlags.NonPublic).Invoke(terminal, new object[1] { terminalNode });
}
private static void AttemptLoadStoryLogFileNode(TerminalNode terminalNode)
{
((object)terminal).GetType().GetMethod("AttemptLoadStoryLogFileNode", BindingFlags.Instance | BindingFlags.NonPublic).Invoke(terminal, new object[1] { terminalNode });
}
private static void AddMessage(string chatMessage)
{
if (!(HUDManager.Instance.lastChatMessage == chatMessage))
{
HUDManager.Instance.lastChatMessage = chatMessage;
HUDManager.Instance.PingHUDElement(HUDManager.Instance.Chat, 10f, 1f, 0.2f);
string item = "<color=#FFFF00>" + chatMessage + "</color>";
HUDManager.Instance.ChatMessageHistory.Add(item);
((TMP_Text)HUDManager.Instance.chatText).text = "";
for (int i = 0; i < HUDManager.Instance.ChatMessageHistory.Count; i++)
{
TextMeshProUGUI chatText = HUDManager.Instance.chatText;
((TMP_Text)chatText).text = ((TMP_Text)chatText).text + "\n" + HUDManager.Instance.ChatMessageHistory[i];
}
}
}
}