Decompiled source of ColliderPlugin v1.2.0

ColliderPlugin.dll

Decompiled 7 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 HarmonyLib;
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("ColliderPlugin")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("ColliderPlugin")]
[assembly: AssemblyCopyright("Copyright ©  2023")]
[assembly: AssemblyTrademark("ColliderPlugin")]
[assembly: ComVisible(false)]
[assembly: Guid("c303405d-e66c-4316-9cdb-4e3ca15c6360")]
[assembly: AssemblyFileVersion("1.2.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyVersion("1.2.0.0")]
namespace LordAshes;

[BepInPlugin("org.lordashes.plugins.collider", "Collider Plug-In", "1.2.0.0")]
[BepInDependency(/*Could not decode attribute arguments.*/)]
public class ColliderPlugin : BaseUnityPlugin
{
	public static class Utility
	{
		public static bool isBoardLoaded()
		{
			return SimpleSingletonBehaviour<CameraController>.HasInstance && SingletonStateMBehaviour<BoardSessionManager, State<BoardSessionManager>>.HasInstance && !BoardSessionManager.IsLoading;
		}

		public static bool StrictKeyCheck(KeyboardShortcut check)
		{
			//IL_002e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0031: Unknown result type (might be due to invalid IL or missing references)
			//IL_003f: Unknown result type (might be due to invalid IL or missing references)
			if (!((KeyboardShortcut)(ref check)).IsUp())
			{
				return false;
			}
			KeyCode[] array = new KeyCode[6];
			RuntimeHelpers.InitializeArray(array, (RuntimeFieldHandle)/*OpCode not supported: LdMemberToken*/);
			KeyCode[] array2 = (KeyCode[])(object)array;
			foreach (KeyCode val in array2)
			{
				if (Input.GetKey(val) != ((KeyboardShortcut)(ref check)).Modifiers.Contains(val))
				{
					return false;
				}
			}
			return true;
		}

		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)("Share Assets Plugin: Base Loader '" + ((Object)match.GetChild(0)).name + "' Found"));
						return ((Component)match.GetChild(0)).gameObject;
					}
					Debug.LogWarning((object)"Share Assets 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)("Share Assets Plugin: Asset Loader '" + ((Object)match.GetChild(0)).name + "' Found"));
						return ((Component)match.GetChild(0)).gameObject;
					}
					Debug.LogWarning((object)"Share Assets 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 + "\nPlugin Masters' " + 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;
		}
	}

	[HarmonyPatch(typeof(CreatureBoardAsset), "DropCommon")]
	public static class PatchesCreatureBoardAssetDropCommon
	{
		public static bool Prefix(ref CreatureBoardAsset __instance)
		{
			//IL_001f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0024: Unknown result type (might be due to invalid IL or missing references)
			Debug.Log((object)("Collision Plugin: Drop Started For " + Utility.GetCreatureName(__instance.Name)));
			PatchesMovableBoardAssetSetPosition.dropCreature = __instance.CreatureId;
			return true;
		}
	}

	[HarmonyPatch(typeof(MovableBoardAsset), "SetPosition")]
	public static class PatchesMovableBoardAssetSetPosition
	{
		public static CreatureGuid dropCreature = CreatureGuid.Empty;

		public static bool Prefix(ref MovableBoardAsset __instance, Vector3 pos, ref State ____currentState)
		{
			//IL_0017: Unknown result type (might be due to invalid IL or missing references)
			//IL_0486: Unknown result type (might be due to invalid IL or missing references)
			//IL_0029: Unknown result type (might be due to invalid IL or missing references)
			//IL_002e: Unknown result type (might be due to invalid IL or missing references)
			//IL_04b1: Unknown result type (might be due to invalid IL or missing references)
			//IL_04b6: Unknown result type (might be due to invalid IL or missing references)
			//IL_009a: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a1: Unknown result type (might be due to invalid IL or missing references)
			//IL_007a: Unknown result type (might be due to invalid IL or missing references)
			//IL_007f: Unknown result type (might be due to invalid IL or missing references)
			//IL_00dc: Unknown result type (might be due to invalid IL or missing references)
			//IL_015d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0220: Unknown result type (might be due to invalid IL or missing references)
			//IL_0225: Unknown result type (might be due to invalid IL or missing references)
			//IL_0231: Unknown result type (might be due to invalid IL or missing references)
			//IL_0243: Unknown result type (might be due to invalid IL or missing references)
			//IL_024d: Unknown result type (might be due to invalid IL or missing references)
			//IL_019c: Unknown result type (might be due to invalid IL or missing references)
			//IL_029d: Unknown result type (might be due to invalid IL or missing references)
			//IL_02af: Unknown result type (might be due to invalid IL or missing references)
			//IL_02b9: Unknown result type (might be due to invalid IL or missing references)
			//IL_030b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0310: Unknown result type (might be due to invalid IL or missing references)
			//IL_0330: Unknown result type (might be due to invalid IL or missing references)
			//IL_0335: Unknown result type (might be due to invalid IL or missing references)
			//IL_0355: Unknown result type (might be due to invalid IL or missing references)
			//IL_035a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0398: Unknown result type (might be due to invalid IL or missing references)
			//IL_039d: Unknown result type (might be due to invalid IL or missing references)
			//IL_03bd: Unknown result type (might be due to invalid IL or missing references)
			//IL_03c2: Unknown result type (might be due to invalid IL or missing references)
			//IL_03e2: Unknown result type (might be due to invalid IL or missing references)
			//IL_03e7: Unknown result type (might be due to invalid IL or missing references)
			if ((int)____currentState == 1)
			{
				bool flag = true;
				Vector3 position;
				for (float num = ((Component)__instance).transform.position.y; num >= pos.y; num -= 0.01f)
				{
					foreach (CreatureBoardAsset item in (IEnumerable<CreatureBoardAsset>)CreaturePresenter.GetTempReadOnlyViewOfAllCreatureAssets())
					{
						if (logDisgnostics)
						{
							string creatureName = Utility.GetCreatureName(item.Name);
							position = ((Component)item).gameObject.transform.position;
							Debug.Log((object)("Checking Collision With " + creatureName + " At " + ((object)(Vector3)(ref position)).ToString()));
						}
						if (!(dropCreature != item.CreatureId))
						{
							continue;
						}
						List<Bounds> list = new List<Bounds>();
						Collider[] componentsInChildren = ((Component)item).gameObject.GetComponentsInChildren<Collider>();
						foreach (Collider val in componentsInChildren)
						{
							list.Add(val.bounds);
						}
						if (list.Count == 0)
						{
							if (logDisgnostics)
							{
								Debug.Log((object)("Checking Collision With " + Utility.GetCreatureName(item.Name) + ": Creating default bounds based on mesh"));
							}
							MeshFilter[] componentsInChildren2 = ((Component)item).gameObject.GetComponentsInChildren<MeshFilter>();
							foreach (MeshFilter val2 in componentsInChildren2)
							{
								list.Add(val2.mesh.bounds);
							}
							SkinnedMeshRenderer[] componentsInChildren3 = ((Component)item).gameObject.GetComponentsInChildren<SkinnedMeshRenderer>();
							foreach (SkinnedMeshRenderer val3 in componentsInChildren3)
							{
								list.Add(val3.sharedMesh.bounds);
							}
						}
						if (logDisgnostics)
						{
							Debug.Log((object)("Checking Collision With " + Utility.GetCreatureName(item.Name) + ": Asset has " + list.Count + " colliders"));
						}
						foreach (Bounds item2 in list)
						{
							Bounds current2 = item2;
							if (((Bounds)(ref current2)).Contains(new Vector3(((Component)__instance).transform.position.x, num, ((Component)__instance).transform.position.z)))
							{
								if (logDisgnostics)
								{
									Debug.Log((object)("Checking Collision With " + Utility.GetCreatureName(item.Name) + ": Asset is blocking falling asset"));
								}
								((Component)__instance).transform.position = new Vector3(((Component)__instance).transform.position.x, num, ((Component)__instance).transform.position.z);
								____currentState = (State)0;
								if (!supressDropUpdates)
								{
									string[] obj = new string[8]
									{
										"Writing Key org.lordashes.plugins.collider Source ",
										((object)(CreatureGuid)(ref dropCreature)).ToString(),
										" To ",
										null,
										null,
										null,
										null,
										null
									};
									position = ((Component)__instance).transform.position;
									obj[3] = position.x.ToString();
									obj[4] = ",";
									position = ((Component)__instance).transform.position;
									obj[5] = position.y.ToString();
									obj[6] = ",";
									position = ((Component)__instance).transform.position;
									obj[7] = position.z.ToString();
									Debug.Log((object)string.Concat(obj));
									string? text = ((object)(CreatureGuid)(ref dropCreature)).ToString();
									string[] array = new string[5];
									position = ((Component)__instance).transform.position;
									array[0] = position.x.ToString();
									array[1] = ",";
									position = ((Component)__instance).transform.position;
									array[2] = position.y.ToString();
									array[3] = ",";
									position = ((Component)__instance).transform.position;
									array[4] = position.z.ToString();
									AssetDataPlugin.SetInfo(text, "org.lordashes.plugins.collider", string.Concat(array), false);
								}
								flag = false;
								break;
							}
							if (logDisgnostics)
							{
								Debug.Log((object)("Checking Collision With " + Utility.GetCreatureName(item.Name) + ": Asset is not blocking falling asset"));
							}
						}
					}
					if (!flag)
					{
						break;
					}
				}
				if (!flag)
				{
					position = ((Component)__instance).transform.position;
					Debug.Log((object)("Collision Plugin: Suspending Asset At " + ((object)(Vector3)(ref position)).ToString()));
					____currentState = (State)0;
					return false;
				}
			}
			return true;
		}
	}

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

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

	public const string Version = "1.2.0.0";

	private static bool logDisgnostics = true;

	private static bool supressDropUpdates = false;

	private void Awake()
	{
		//IL_0011: Unknown result type (might be due to invalid IL or missing references)
		//IL_0017: Expected O, but got Unknown
		Debug.Log((object)"Collider Plugin: Active.");
		Harmony val = new Harmony("org.lordashes.plugins.collider");
		val.PatchAll();
		logDisgnostics = ((BaseUnityPlugin)this).Config.Bind<bool>("Setting", "Log Diagnostics", true, (ConfigDescription)null).Value;
		AssetDataPlugin.Subscribe("org.lordashes.plugins.collider", (Action<DatumChange>)FixPositionOnLoad, (Func<DatumChange, bool>)Checker.CheckSourceAsCreature);
		Utility.PostOnMainPage(((object)this).GetType());
	}

	private void FixPositionOnLoad(DatumChange datum)
	{
		//IL_0018: Unknown result type (might be due to invalid IL or missing references)
		//IL_001e: Invalid comparison between Unknown and I4
		//IL_005b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0117: Unknown result type (might be due to invalid IL or missing references)
		Debug.Log((object)("Collision Plugin: Request " + JsonConvert.SerializeObject((object)datum)));
		if ((int)datum.action != 1 && datum.source.ToString() != ((object)(CreatureGuid)(ref PatchesMovableBoardAssetSetPosition.dropCreature)).ToString())
		{
			CreatureBoardAsset val = null;
			CreaturePresenter.TryGetAsset(new CreatureGuid(datum.source), ref val);
			if ((Object)(object)val != (Object)null)
			{
				Debug.Log((object)("Collision Plugin: Moving Asset " + Utility.GetCreatureName(val.Name) + " (" + datum.source + ") To Position " + datum.value.ToString()));
				string[] array = datum.value.ToString().Split(new char[1] { ',' });
				((Component)val).gameObject.transform.position = new Vector3(float.Parse(array[0], CultureInfo.InvariantCulture), float.Parse(array[1], CultureInfo.InvariantCulture), float.Parse(array[2], CultureInfo.InvariantCulture));
			}
		}
	}
}