Decompiled source of TurnCountersPlugin v1.1.0

TurnCountersPlugin.dll

Decompiled 2 months ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using System.Security.Cryptography;
using System.Text;
using BepInEx;
using BepInEx.Configuration;
using Bounce.Singletons;
using Newtonsoft.Json;
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("TurnCountersPlugin")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("TurnCountersPlugin")]
[assembly: AssemblyCopyright("Copyright ©  2024")]
[assembly: AssemblyTrademark("TurnCountersPlugin")]
[assembly: ComVisible(false)]
[assembly: Guid("c303405d-e66c-4316-9cdb-4e3ca15c6360")]
[assembly: AssemblyFileVersion("1.1.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyVersion("1.1.0.0")]
namespace LordAshes;

[BepInPlugin("org.lordashes.plugins.turncounters", "Turn Counters Plugin", "1.1.0.0")]
[BepInDependency(/*Could not decode attribute arguments.*/)]
[BepInDependency(/*Could not decode attribute arguments.*/)]
public class TurnCountersPlugin : BaseUnityPlugin
{
	public static class Utility
	{
		public static bool isBoardLoaded()
		{
			return SimpleSingletonBehaviour<CameraController>.HasInstance && SingletonStateMBehaviour<BoardSessionManager, State<BoardSessionManager>>.HasInstance && !BoardSessionManager.IsLoading;
		}

		public static Guid GuidFromString(string input)
		{
			using MD5 mD = MD5.Create();
			byte[] b = mD.ComputeHash(Encoding.Default.GetBytes(input));
			return new Guid(b);
		}

		public static GameObject GetBaseLoader(CreatureGuid cid)
		{
			//IL_0004: Unknown result type (might be due to invalid IL or missing references)
			try
			{
				CreatureBoardAsset val = null;
				CreaturePresenter.TryGetAsset(cid, ref val);
				if ((Object)(object)val != (Object)null)
				{
					Transform match = null;
					Traverse(((Component)val).transform, "BaseLoader", 0, 10, ref match);
					if ((Object)(object)match != (Object)null)
					{
						Debug.Log((object)("Turn Counters Plugin: Base Loader '" + ((Object)match.GetChild(0)).name + "' Found"));
						return ((Component)match.GetChild(0)).gameObject;
					}
					Debug.LogWarning((object)"Turn Counters Plugin: Could Not Find Base Loader");
					return null;
				}
				return null;
			}
			catch
			{
				return null;
			}
		}

		public static GameObject GetAssetLoader(CreatureGuid cid)
		{
			//IL_0004: Unknown result type (might be due to invalid IL or missing references)
			try
			{
				CreatureBoardAsset val = null;
				CreaturePresenter.TryGetAsset(cid, ref val);
				if ((Object)(object)val != (Object)null)
				{
					Transform match = null;
					Traverse(((Component)val).transform, "AssetLoader", 0, 10, ref match);
					if ((Object)(object)match != (Object)null)
					{
						Debug.Log((object)("Turn Counters Plugin: Asset Loader '" + ((Object)match.GetChild(0)).name + "' Found"));
						return ((Component)match.GetChild(0)).gameObject;
					}
					Debug.LogWarning((object)"Turn Counters Plugin: Could Not Find Asset Loader");
					return null;
				}
				return null;
			}
			catch
			{
				return null;
			}
		}

		public static void Traverse(Transform root, string seek, int depth, int depthMax, ref Transform match)
		{
			try
			{
				if ((Object)(object)match != (Object)null)
				{
					return;
				}
				if (((Object)root).name == seek)
				{
					match = root;
					return;
				}
				foreach (Transform item in ExtensionMethods.Children(root))
				{
					if (depth < depthMax)
					{
						Traverse(item, seek, depth + 1, depthMax, ref match);
					}
				}
			}
			catch
			{
			}
		}

		public static float ParseFloat(string value)
		{
			return float.Parse(value, CultureInfo.InvariantCulture);
		}

		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();
		}

		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);
				}
			};
		}

		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 enum DiagnosticLevel
	{
		none,
		error,
		warning,
		info,
		debug,
		ultra
	}

	public class Counter
	{
		public string name { get; set; } = "Counter";


		public int value { get; set; } = 1;


		public int step { get; set; } = 1;


		public bool reset { get; set; } = false;

	}

	public const string Name = "Turn Counters Plugin";

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

	public const string Version = "1.1.0.0";

	public const string Author = "Lord Ashes";

	private List<Counter> counters = new List<Counter>();

	private Vector2 position = Vector2.zero;

	private CreatureGuid creatureReference = default(CreatureGuid);

	private ConfigEntry<KeyboardShortcut> trigger;

	private static GUIStyle styleLabel = new GUIStyle();

	private static GUIStyle styleEntry = new GUIStyle();

	private static int spacing = 30;

	private static int selectedCounter = 0;

	private ConfigEntry<DiagnosticLevel> diagnostics { get; set; }

	private void Awake()
	{
		//IL_008d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0092: Unknown result type (might be due to invalid IL or missing references)
		//IL_0132: Unknown result type (might be due to invalid IL or missing references)
		//IL_01f4: Unknown result type (might be due to invalid IL or missing references)
		//IL_024e: Unknown result type (might be due to invalid IL or missing references)
		diagnostics = ((BaseUnityPlugin)this).Config.Bind<DiagnosticLevel>("Settings", "Diagnostic Level", DiagnosticLevel.info, (ConfigDescription)null);
		Debug.Log((object)("Turn Counters Plugin: Active. (Diagnostic Mode = " + diagnostics.Value.ToString() + ")"));
		position = new Vector2((float)((BaseUnityPlugin)this).Config.Bind<int>("Settings", "X Position Of Counters", 10, (ConfigDescription)null).Value, (float)((BaseUnityPlugin)this).Config.Bind<int>("Settings", "Y Position Of Counters", 900, (ConfigDescription)null).Value);
		spacing = ((BaseUnityPlugin)this).Config.Bind<int>("Settings", "Vertical Spacing", 30, (ConfigDescription)null).Value;
		string[] array = null;
		array = ((BaseUnityPlugin)this).Config.Bind<string>("Settings", "Entry Color RGBA Byte Values Separated By Pipes", "0|0|0|255", (ConfigDescription)null).Value.Split(new char[1] { '|' });
		styleEntry.normal.textColor = new Color((float)(int)byte.Parse(array[0]) / 255f, (float)(int)byte.Parse(array[1]) / 255f, (float)(int)byte.Parse(array[2]) / 255f, (float)(int)byte.Parse(array[3]) / 255f);
		styleEntry.fontSize = ((BaseUnityPlugin)this).Config.Bind<int>("Settings", "Text Size", 16, (ConfigDescription)null).Value;
		styleEntry.fontStyle = (FontStyle)1;
		styleEntry.alignment = (TextAnchor)1;
		array = ((BaseUnityPlugin)this).Config.Bind<string>("Settings", "Label Color RGBA Byte Values Separated By Pipes", "219|55|0|255", (ConfigDescription)null).Value.Split(new char[1] { '|' });
		styleLabel.normal.textColor = new Color((float)(int)byte.Parse(array[0]) / 255f, (float)(int)byte.Parse(array[1]) / 255f, (float)(int)byte.Parse(array[2]) / 255f, (float)(int)byte.Parse(array[3]) / 255f);
		styleLabel.fontSize = ((BaseUnityPlugin)this).Config.Bind<int>("Settings", "Text Size", 16, (ConfigDescription)null).Value;
		styleLabel.alignment = (TextAnchor)0;
		trigger = ((BaseUnityPlugin)this).Config.Bind<KeyboardShortcut>("Settings", "Trigger Sync Of Turns To Selected Character", new KeyboardShortcut((KeyCode)278, Array.Empty<KeyCode>()), (ConfigDescription)null);
		styleEntry.normal.background = Image.LoadTexture("org.lordashes.plugins.turncounters.png", (CacheType)999);
		InitiativeManager.OnTurnSwitch += TurnSwitch;
		Utility.PostOnMainPage(((object)this).GetType());
	}

	private void OnGUI()
	{
		//IL_03c6: Unknown result type (might be due to invalid IL or missing references)
		//IL_000f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0016: Unknown result type (might be due to invalid IL or missing references)
		//IL_001c: Unknown result type (might be due to invalid IL or missing references)
		//IL_00bb: Unknown result type (might be due to invalid IL or missing references)
		//IL_0112: Unknown result type (might be due to invalid IL or missing references)
		//IL_019d: Unknown result type (might be due to invalid IL or missing references)
		//IL_02f1: Unknown result type (might be due to invalid IL or missing references)
		//IL_02f6: Unknown result type (might be due to invalid IL or missing references)
		//IL_0302: Unknown result type (might be due to invalid IL or missing references)
		//IL_0307: Unknown result type (might be due to invalid IL or missing references)
		//IL_0321: Unknown result type (might be due to invalid IL or missing references)
		//IL_033e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0343: Unknown result type (might be due to invalid IL or missing references)
		if (Utility.isBoardLoaded())
		{
			if (creatureReference == default(CreatureGuid))
			{
				GetCreatureReference();
			}
			int[] array = new int[counters.Count];
			for (int i = 0; i < counters.Count; i++)
			{
				array[i] = int.Parse(counters.ElementAt(i).value.ToString());
				GUI.DrawTexture(new Rect(position.x, position.y - (float)((counters.Count - i) * spacing) - 2f, 32f, (float)styleEntry.fontSize * 1.2f + 4f), (Texture)(object)Texture2D.blackTexture, (ScaleMode)0, true);
				int result = 0;
				if (int.TryParse(GUI.TextField(new Rect(position.x, position.y - (float)((counters.Count - i) * spacing), 32f, (float)styleEntry.fontSize * 1.2f), counters.ElementAt(i).value.ToString(), styleEntry), out result))
				{
					counters.ElementAt(i).value = result;
				}
				if (GUI.Button(new Rect(position.x + 40f, position.y - (float)((counters.Count - i) * spacing), 1920f, (float)spacing), "(" + ((counters.ElementAt(i).step > 0) ? "+" : "-") + ") " + counters.ElementAt(i).name.ToString(), styleLabel))
				{
					selectedCounter = i;
					SystemMessage.AskForTextInput("Turn Counters Plugin:", "Enter Counter Name:", "Apply", (Action<string>)delegate(string S)
					{
						counters.ElementAt(selectedCounter).name = S;
						AssetDataPlugin.SetInfo(LocalPlayer.Name, "org.lordashes.plugins.turncounters.name." + (selectedCounter + 1), counters.ElementAt(selectedCounter).name, false);
						selectedCounter = 0;
					}, (Action)null, "Cancel", (Action)null, counters.ElementAt(i).name);
				}
			}
			for (int j = 0; j < counters.Count; j++)
			{
				if (array[j] != int.Parse(counters.ElementAt(j).value.ToString()))
				{
					AssetDataPlugin.SetInfo(LocalPlayer.Name, "org.lordashes.plugins.turncounters.value." + (j + 1), (object)counters.ElementAt(j).value, false);
				}
			}
			KeyboardShortcut value = trigger.Value;
			if (((KeyboardShortcut)(ref value)).IsUp() && creatureReference != LocalClient.SelectedCreatureId)
			{
				CreatureBoardAsset val = null;
				CreaturePresenter.TryGetAsset(LocalClient.SelectedCreatureId, ref val);
				if ((Object)(object)val != (Object)null)
				{
					creatureReference = LocalClient.SelectedCreatureId;
					Debug.Log((object)("Turn Counters Plugin: [Manual] Adjusting Counters When " + Utility.GetCreatureName(val.Name) + " Starts His/Her/Its Turn (" + ((object)(CreatureGuid)(ref creatureReference)).ToString() + ")"));
					SystemMessage.DisplayInfoText("Turn Counters Plugin:\r\nAdjusting Counters When " + Utility.GetCreatureName(val.Name) + " Starts His/Her/Its Turn", 2.5f);
				}
			}
		}
		else
		{
			creatureReference = default(CreatureGuid);
		}
	}

	private void GetCreatureReference()
	{
		//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)
		//IL_0021: Unknown result type (might be due to invalid IL or missing references)
		//IL_0032: Unknown result type (might be due to invalid IL or missing references)
		//IL_0037: Unknown result type (might be due to invalid IL or missing references)
		foreach (CreatureBoardAsset item in (IEnumerable<CreatureBoardAsset>)CreaturePresenter.GetTempReadOnlyViewOfAllCreatureAssets())
		{
			if (LocalClient.CanControlCreature(item.CreatureId))
			{
				creatureReference = item.CreatureId;
				Debug.Log((object)("Turn Counters Plugin: [Auto] Adjusting Counters When " + Utility.GetCreatureName(item.Name) + " Starts His/Her/Its Turn (" + ((object)(CreatureGuid)(ref creatureReference)).ToString() + ")"));
				break;
			}
		}
		Debug.Log((object)"Turn Counters Plugin: Loading Counters Configuraton");
		counters = JsonConvert.DeserializeObject<List<Counter>>(File.ReadAllText("org.lordashes.plugins.turncounters.json", (CacheType)999));
		for (int i = 1; i <= counters.Count; i++)
		{
			if (counters[i - 1].reset)
			{
				continue;
			}
			int value = counters[i - 1].value;
			try
			{
				if (AssetDataPlugin.ReadInfo(LocalPlayer.Name, "org.lordashes.plugins.turncounters.name." + i).Trim() != "")
				{
					counters[i - 1].name = AssetDataPlugin.ReadInfo(LocalPlayer.Name, "org.lordashes.plugins.turncounters.name." + i);
					counters[i - 1].value = int.Parse(AssetDataPlugin.ReadInfo(LocalPlayer.Name, "org.lordashes.plugins.turncounters.value." + i));
				}
			}
			catch (Exception ex)
			{
				Debug.Log((object)("Turn Counters Plugin: Error Reading Counter " + i + " Information: " + ex));
				counters[i - 1].value = value;
			}
		}
	}

	private void TurnSwitch(CreatureGuid cid)
	{
		//IL_0001: Unknown result type (might be due to invalid IL or missing references)
		//IL_0003: Unknown result type (might be due to invalid IL or missing references)
		if (!(cid == creatureReference))
		{
			return;
		}
		Debug.Log((object)"Turn Counters Plugin: Adjusting Counters");
		for (int i = 0; i < counters.Count; i++)
		{
			counters[i].value += counters[i].step;
			if (counters[i].value < 0)
			{
				counters[i].value = 0;
			}
			AssetDataPlugin.SetInfo(LocalPlayer.Name, "org.lordashes.plugins.turncounters.value." + (i + 1), (object)counters[i].value, false);
		}
	}
}