Decompiled source of RelayPlugin v1.5.0

RelayPlugin.dll

Decompiled 7 months ago
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using Bounce.Singletons;
using TMPro;
using UnityEngine;
using UnityEngine.SceneManagement;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: AssemblyTitle("RelayPlugin")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("RelayPlugin")]
[assembly: AssemblyCopyright("Copyright ©  2023")]
[assembly: AssemblyTrademark("RelayPlugin")]
[assembly: ComVisible(false)]
[assembly: Guid("c303405d-e66c-4316-9cdb-4e3ca15c6360")]
[assembly: AssemblyFileVersion("1.5.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyVersion("1.5.0.0")]
namespace LordAshes;

[BepInPlugin("org.lordashes.plugins.relay", "Replay Plug-In", "1.5.0.0")]
[BepInDependency(/*Could not decode attribute arguments.*/)]
[BepInDependency(/*Could not decode attribute arguments.*/)]
public class ReplayPlugin : BaseUnityPlugin
{
	public static class Utility
	{
		public static void PostOnMainPage(MemberInfo plugin)
		{
			SceneManager.sceneLoaded += delegate(Scene scene, LoadSceneMode mode)
			{
				//IL_0072: Unknown result type (might be due to invalid IL or missing references)
				//IL_0079: Expected O, but got Unknown
				try
				{
					if (((Scene)(ref scene)).name == "UI")
					{
						TextMeshProUGUI uITextByName = GetUITextByName("BETA");
						if (Object.op_Implicit((Object)(object)uITextByName))
						{
							((TMP_Text)uITextByName).text = "INJECTED BUILD - unstable mods";
						}
					}
					else
					{
						TextMeshProUGUI uITextByName2 = GetUITextByName("TextMeshPro Text");
						if (Object.op_Implicit((Object)(object)uITextByName2))
						{
							BepInPlugin val = (BepInPlugin)Attribute.GetCustomAttribute(plugin, typeof(BepInPlugin));
							if (((TMP_Text)uITextByName2).text.EndsWith("</size>"))
							{
								((TMP_Text)uITextByName2).text = ((TMP_Text)uITextByName2).text + "\n\nMods Currently Installed:\n";
							}
							TextMeshProUGUI val2 = uITextByName2;
							((TMP_Text)val2).text = ((TMP_Text)val2).text + "\nLord Ashes' " + val.Name + " - " + val.Version;
						}
					}
				}
				catch (Exception ex)
				{
					Debug.Log((object)ex);
				}
			};
		}

		public static bool isBoardLoaded()
		{
			return SimpleSingletonBehaviour<CameraController>.HasInstance && SingletonStateMBehaviour<BoardSessionManager, State<BoardSessionManager>>.HasInstance && !BoardSessionManager.IsLoading;
		}

		public static string GetCreatureName(string nameBlock)
		{
			if (nameBlock == null)
			{
				return "(Unknown)";
			}
			if (!nameBlock.Contains("<size=0>"))
			{
				return nameBlock;
			}
			return nameBlock.Substring(0, nameBlock.IndexOf("<size=0>")).Trim();
		}

		private static TextMeshProUGUI GetUITextByName(string name)
		{
			TextMeshProUGUI[] array = Object.FindObjectsOfType<TextMeshProUGUI>();
			for (int i = 0; i < array.Length; i++)
			{
				if (((Object)array[i]).name == name)
				{
					return array[i];
				}
			}
			return null;
		}
	}

	public const string Name = "Replay Plug-In";

	public const string Guid = "org.lordashes.plugins.relay";

	public const string Version = "1.5.0.0";

	public List<Guid> subscriptionIds = new List<Guid>();

	public Dictionary<string, string> relays = new Dictionary<string, string>();

	public KeyboardShortcut toggle;

	public float suspensionInterval = 2f;

	public bool processRelays = true;

	public static ReplayPlugin _self;

	public void RelayHandler(DatumChange change)
	{
		//IL_0070: 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)
		//IL_009e: Unknown result type (might be due to invalid IL or missing references)
		//IL_02b0: Unknown result type (might be due to invalid IL or missing references)
		//IL_02b5: Unknown result type (might be due to invalid IL or missing references)
		//IL_0308: Unknown result type (might be due to invalid IL or missing references)
		//IL_030d: Unknown result type (might be due to invalid IL or missing references)
		//IL_03c7: Unknown result type (might be due to invalid IL or missing references)
		//IL_03cc: Unknown result type (might be due to invalid IL or missing references)
		string[] obj = new string[11]
		{
			"Relay Plugin: Received ",
			change.source,
			"->",
			change.key,
			"->",
			change.previous?.ToString(),
			"->",
			change.value?.ToString(),
			" (",
			null,
			null
		};
		ChangeAction action = change.action;
		obj[9] = ((object)(ChangeAction)(ref action)).ToString();
		obj[10] = ")";
		Debug.Log((object)string.Concat(obj));
		CreatureBoardAsset val = default(CreatureBoardAsset);
		CreaturePresenter.TryGetAsset(new CreatureGuid(change.source), ref val);
		string text = (((Object)(object)val != (Object)null) ? Utility.GetCreatureName(val.Name) : change.source);
		Debug.Log((object)("Relay Plugin: Translated to " + change.key + "->" + text + "->" + change.value));
		if (processRelays)
		{
			foreach (KeyValuePair<string, string> relay in relays)
			{
				if (change.key + ":" + text + ":" + change.value == relay.Key || change.key + ":*:" + change.value == relay.Key)
				{
					Debug.Log((object)("Relay Plugin: Found Relay. Actions: '" + relay.Value + "'"));
					string[] array = relay.Value.Split(new char[1] { '|' });
					List<object[]> list = new List<object[]>();
					for (int i = 0; i < array.Length; i++)
					{
						Debug.Log((object)("Relay Plugin: Queuing Relay Action: '" + array[i] + "'"));
						string[] array2 = array[i].Split(new char[1] { ':' });
						float num = float.Parse(array2[0]);
						string[] array3 = new string[3]
						{
							array2[1],
							array2[2],
							array2[3]
						};
						if (array3[1] == "*")
						{
							array3[1] = change.source;
						}
						CreatureBoardAsset val2 = FindCreature(array3[1]);
						string[] obj2 = new string[6] { "Relay Plugin: Queuing Request ", null, null, null, null, null };
						object obj3;
						CreatureGuid creatureId;
						if (!((Object)(object)val2 != (Object)null))
						{
							obj3 = array3[1];
						}
						else
						{
							creatureId = val2.CreatureId;
							obj3 = ((object)(CreatureGuid)(ref creatureId)).ToString();
						}
						obj2[1] = (string)obj3;
						obj2[2] = "->";
						obj2[3] = array3[0];
						obj2[4] = "->";
						obj2[5] = array3[2];
						Debug.Log((object)string.Concat(obj2));
						if (array3[2].StartsWith("~"))
						{
							creatureId = val2.CreatureId;
							string text2 = AssetDataPlugin.ReadInfo(((object)(CreatureGuid)(ref creatureId)).ToString(), array3[0]);
							int result = 1;
							if (!int.TryParse(text2, out result))
							{
								result = 1;
							}
							Debug.Log((object)("Relay Plugin: Toggle State From " + text2));
							result++;
							if (result > int.Parse(array3[2].Substring(1)))
							{
								result = 1;
							}
							array3[2] = result.ToString();
							Debug.Log((object)("Relay Plugin: Toggle State To " + array3[2]));
						}
						if ((Object)(object)val2 != (Object)null)
						{
							Debug.Log((object)"Relay Plugin: Queuing Creature Request");
							object[] obj4 = new object[4] { num, null, null, null };
							creatureId = val2.CreatureId;
							obj4[1] = ((object)(CreatureGuid)(ref creatureId)).ToString();
							obj4[2] = array3[0];
							obj4[3] = array3[2];
							list.Add(obj4);
						}
						else
						{
							Debug.Log((object)"Relay Plugin: Queuing Asset Request");
							list.Add(new object[4]
							{
								num,
								array3[1],
								array3[0],
								array3[2]
							});
						}
					}
					Debug.Log((object)("Relay Plugin: " + (processRelays ? "Start Processing Reactions" : "Ignore Processing Reactions")));
					if (processRelays)
					{
						((MonoBehaviour)this).StartCoroutine("DelayRequest", (object)new object[2] { change.source, list });
					}
				}
			}
			return;
		}
		Debug.Log((object)"Relay Plugin: Relay Processing Suspended");
	}

	public CreatureBoardAsset FindCreature(string creatureName)
	{
		//IL_0002: Unknown result type (might be due to invalid IL or missing references)
		//IL_0007: Unknown result type (might be due to invalid IL or missing references)
		foreach (CreatureBoardAsset item in (IEnumerable<CreatureBoardAsset>)CreaturePresenter.GetTempReadOnlyViewOfAllCreatureAssets())
		{
			if (Utility.GetCreatureName(item.Name).ToUpper() == creatureName.ToUpper())
			{
				return item;
			}
		}
		return null;
	}

	public IEnumerator DelayRequest(object[] input)
	{
		if (suspensionInterval > 0f)
		{
			processRelays = false;
		}
		Debug.Log((object)"Relay Plugin: Obtaining Tripping Creature");
		CreatureGuid cid = new CreatureGuid((string)input[0]);
		Debug.Log((object)"Relay Plugin: Obtaining Request List");
		List<object[]> reactions = (List<object[]>)input[1];
		Debug.Log((object)("Relay Plugin: Request List Has " + reactions.Count + " Requests"));
		CreatureBoardAsset asset = default(CreatureBoardAsset);
		foreach (object[] inputs in reactions)
		{
			float delay = (float)inputs[0];
			Debug.Log((object)("Relay Plugin: Delaying " + delay + " s"));
			yield return (object)new WaitForSeconds(delay);
			Debug.Log((object)"Relay Plugin: Checking Legacy Status");
			bool legacy = inputs[2].ToString().StartsWith("*");
			if (legacy)
			{
				inputs[2] = inputs[2].ToString().Substring(1);
			}
			string target = ((inputs[1].ToString() != "*") ? inputs[1].ToString() : ((object)(CreatureGuid)(ref cid)).ToString());
			Debug.Log((object)("Relay Plugin: Processing Request " + target + "->" + inputs[2]?.ToString() + "->" + inputs[3]?.ToString() + " (Legacy: " + legacy + ")"));
			if (inputs[2].ToString().ToUpper() == "LIFT")
			{
				CreaturePresenter.TryGetAsset(new CreatureGuid(target), ref asset);
				if ((Object)(object)asset != (Object)null)
				{
					Debug.Log((object)"Relay Plugin: Lifting Asset");
					asset.Pickup();
					asset.DropAtCurrentLocation();
				}
				asset = null;
			}
			else if (((string)inputs[3]).Trim() != "")
			{
				AssetDataPlugin.SetInfo((string)inputs[1], (string)inputs[2], (string)inputs[3], legacy);
			}
			else
			{
				AssetDataPlugin.ClearInfo((string)inputs[1], (string)inputs[2], legacy);
			}
		}
		if (suspensionInterval > 0f)
		{
			((MonoBehaviour)this).StartCoroutine("SuspendRelays");
		}
	}

	public IEnumerator SuspendRelays()
	{
		processRelays = false;
		Debug.Log((object)"Relay Plugin: Relays Suspended");
		yield return (object)new WaitForSeconds(suspensionInterval);
		Debug.Log((object)"Relay Plugin: Relays Activated");
		processRelays = true;
	}

	private void Awake()
	{
		//IL_0033: Unknown result type (might be due to invalid IL or missing references)
		//IL_003e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0043: Unknown result type (might be due to invalid IL or missing references)
		Debug.Log((object)"Relay Plugin: Active.");
		_self = this;
		toggle = ((BaseUnityPlugin)this).Config.Bind<KeyboardShortcut>("Setting", "Toggle Logic", new KeyboardShortcut((KeyCode)120, (KeyCode[])(object)new KeyCode[1] { (KeyCode)305 }), (ConfigDescription)null).Value;
		suspensionInterval = ((BaseUnityPlugin)this).Config.Bind<float>("Setting", "Relay Suspension Interval", 2f, (ConfigDescription)null).Value;
		List<string> list = new List<string>();
		string[] array = (from f in File.Catalog(false)
			where Path.GetExtension(f).ToUpper() == ".SUB"
			select f).ToArray();
		foreach (string text in array)
		{
			Debug.Log((object)("Relay Plugin: Reading Relays From '" + text + "'..."));
			string[] array2 = File.ReadAllLines(text.ToString(), (CacheType)999);
			foreach (string text2 in array2)
			{
				if (text2.Trim() != "")
				{
					string[] array3 = text2.Split(new char[1] { '|' })[0].Split(new char[1] { ':' });
					Debug.Log((object)("Relay Plugin: Adding Relay " + array3[0] + " for " + array3[1] + " state " + array3[2]));
					if (!list.Contains(array3[0]))
					{
						Debug.Log((object)("Relay Plugin: Adding subscription to " + array3[0]));
						subscriptionIds.Add(AssetDataPlugin.Subscribe(array3[0], (Action<DatumChange>)RelayHandler));
						list.Add(array3[0]);
					}
					relays.Add(array3[0] + ":" + array3[1] + ":" + array3[2], text2.Split(new char[1] { '|' })[1]);
				}
			}
		}
		Utility.PostOnMainPage(((object)this).GetType());
	}

	private void Update()
	{
		if (((KeyboardShortcut)(ref toggle)).IsUp())
		{
			processRelays = !processRelays;
			SystemMessage.DisplayInfoText("Processing of Relays Logic is " + (processRelays ? "On" : "Off"), 2.5f);
		}
	}

	private static ReplayPlugin Instance()
	{
		return _self;
	}
}