Decompiled source of CollapseableComponents v1.0.2

plugins/CollapseableComponents/CollapseableComponents.dll

Decompiled a month ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using System.Threading;
using System.Threading.Tasks;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Core.Logging.Interpolation;
using BepInEx.Logging;
using BepInEx.NET.Common;
using BepInExResoniteShim;
using Elements.Core;
using FrooxEngine;
using FrooxEngine.UIX;
using HarmonyLib;
using Renderite.Shared;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(/*Could not decode attribute arguments.*/)]
[assembly: TargetFramework(".NETCoreApp,Version=v9.0", FrameworkDisplayName = ".NET 9.0")]
[assembly: AssemblyCompany("NepuShiro")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.2.0")]
[assembly: AssemblyInformationalVersion("1.0.2+fc2697b69a7aff9816131fe07805d0257eef8074")]
[assembly: AssemblyProduct("CollapseableComponents")]
[assembly: AssemblyTitle("CollapseableComponents")]
[assembly: AssemblyMetadata("RepositoryUrl", "https://github.com/NepuShiro/CollapseableComponents")]
[assembly: AssemblyVersion("1.0.2.0")]
[module: RefSafetyRules(11)]
namespace CollapseableComponents;

[ResonitePlugin("NepuShiro.CollapseableComponents", "CollapseableComponents", "1.0.2", "NepuShiro", "https://github.com/NepuShiro/CollapseableComponents")]
[BepInDependency(/*Could not decode attribute arguments.*/)]
public class Plugin : BasePlugin
{
	[HarmonyPatch(typeof(WorkerInspector), "BuildUIForComponent")]
	public class CollapseButtonPatch
	{
		[CompilerGenerated]
		private sealed class <>c__DisplayClass0_0
		{
			[StructLayout(3)]
			private struct <<Postfix>b__0>d : IAsyncStateMachine
			{
				public int <>1__state;

				public AsyncTaskMethodBuilder <>t__builder;

				private <>c__DisplayClass0_1 <>8__1;

				public <>c__DisplayClass0_0 <>4__this;

				private TaskAwaiter <>u__1;

				private void MoveNext()
				{
					//IL_005c: Unknown result type (might be due to invalid IL or missing references)
					//IL_0061: Unknown result type (might be due to invalid IL or missing references)
					//IL_0068: Unknown result type (might be due to invalid IL or missing references)
					//IL_001f: 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_0042: 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)
					//IL_00b9: Unknown result type (might be due to invalid IL or missing references)
					//IL_00c6: Expected O, but got Unknown
					int num = <>1__state;
					<>c__DisplayClass0_0 <>c__DisplayClass0_ = <>4__this;
					try
					{
						TaskAwaiter awaiter;
						if (num != 0)
						{
							<>8__1 = new <>c__DisplayClass0_1();
							awaiter = global::System.Threading.Tasks.Task.Delay(TimeSpan.FromSeconds(10L)).GetAwaiter();
							if (!((TaskAwaiter)(ref awaiter)).IsCompleted)
							{
								num = (<>1__state = 0);
								<>u__1 = awaiter;
								((AsyncTaskMethodBuilder)(ref <>t__builder)).AwaitUnsafeOnCompleted<TaskAwaiter, <<Postfix>b__0>d>(ref awaiter, ref this);
								return;
							}
						}
						else
						{
							awaiter = <>u__1;
							<>u__1 = default(TaskAwaiter);
							num = (<>1__state = -1);
						}
						((TaskAwaiter)(ref awaiter)).GetResult();
						if (Enabled.Value)
						{
							<>8__1.world = <>c__DisplayClass0_.worker.World;
							<>8__1.world.RunSynchronously((Action)delegate
							{
								//IL_0040: Unknown result type (might be due to invalid IL or missing references)
								//IL_0045: Unknown result type (might be due to invalid IL or missing references)
								//IL_0097: Unknown result type (might be due to invalid IL or missing references)
								//IL_009e: Expected O, but got Unknown
								List<Slot> obj = new List<Slot>(Enumerable.Where<Slot>((global::System.Collections.Generic.IEnumerable<Slot>)<>8__1.world.RootSlot.GetChildrenWithTag("CollapseableComponentsTag"), (Func<Slot, bool>)((Slot x) => x.Parent == <>8__1.world.RootSlot && <>8__1.world.LocalUser.IsAllocatingUser((IWorldElement)(object)x))));
								int num2 = 0;
								Enumerator<Slot> enumerator = obj.GetEnumerator();
								try
								{
									while (enumerator.MoveNext())
									{
										Slot current = enumerator.Current;
										if (current != null && <>8__1.world.LocalUser.IsAllocatingUser((IWorldElement)(object)current))
										{
											current.Destroy();
											num2++;
										}
									}
								}
								finally
								{
									((global::System.IDisposable)enumerator).Dispose();
								}
								if (num2 > 0)
								{
									ManualLogSource log = Log;
									bool flag = default(bool);
									BepInExDebugLogInterpolatedStringHandler val = new BepInExDebugLogInterpolatedStringHandler(27, 1, ref flag);
									if (flag)
									{
										((BepInExLogInterpolatedStringHandler)val).AppendLiteral("Removed ");
										((BepInExLogInterpolatedStringHandler)val).AppendFormatted<int>(num2);
										((BepInExLogInterpolatedStringHandler)val).AppendLiteral(" escaped Expanders.");
									}
									log.LogDebug(val);
								}
							}, false, (IUpdatable)null, false);
						}
					}
					catch (global::System.Exception exception)
					{
						<>1__state = -2;
						<>8__1 = null;
						((AsyncTaskMethodBuilder)(ref <>t__builder)).SetException(exception);
						return;
					}
					<>1__state = -2;
					<>8__1 = null;
					((AsyncTaskMethodBuilder)(ref <>t__builder)).SetResult();
				}

				[DebuggerHidden]
				private void SetStateMachine(IAsyncStateMachine stateMachine)
				{
					((AsyncTaskMethodBuilder)(ref <>t__builder)).SetStateMachine(stateMachine);
				}
			}

			public Worker worker;

			[AsyncStateMachine(typeof(<<Postfix>b__0>d))]
			internal global::System.Threading.Tasks.Task <Postfix>b__0()
			{
				//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)
				<<Postfix>b__0>d <<Postfix>b__0>d = default(<<Postfix>b__0>d);
				<<Postfix>b__0>d.<>t__builder = AsyncTaskMethodBuilder.Create();
				<<Postfix>b__0>d.<>4__this = this;
				<<Postfix>b__0>d.<>1__state = -1;
				((AsyncTaskMethodBuilder)(ref <<Postfix>b__0>d.<>t__builder)).Start<<<Postfix>b__0>d>(ref <<Postfix>b__0>d);
				return ((AsyncTaskMethodBuilder)(ref <<Postfix>b__0>d.<>t__builder)).Task;
			}
		}

		[CompilerGenerated]
		private sealed class <>c__DisplayClass0_1
		{
			public World world;

			public Func<Slot, bool> <>9__2;

			internal void <Postfix>b__1()
			{
				//IL_0040: Unknown result type (might be due to invalid IL or missing references)
				//IL_0045: Unknown result type (might be due to invalid IL or missing references)
				//IL_0097: Unknown result type (might be due to invalid IL or missing references)
				//IL_009e: Expected O, but got Unknown
				List<Slot> obj = new List<Slot>(Enumerable.Where<Slot>((global::System.Collections.Generic.IEnumerable<Slot>)world.RootSlot.GetChildrenWithTag("CollapseableComponentsTag"), (Func<Slot, bool>)((Slot x) => x.Parent == world.RootSlot && world.LocalUser.IsAllocatingUser((IWorldElement)(object)x))));
				int num = 0;
				Enumerator<Slot> enumerator = obj.GetEnumerator();
				try
				{
					while (enumerator.MoveNext())
					{
						Slot current = enumerator.Current;
						if (current != null && world.LocalUser.IsAllocatingUser((IWorldElement)(object)current))
						{
							current.Destroy();
							num++;
						}
					}
				}
				finally
				{
					((global::System.IDisposable)enumerator).Dispose();
				}
				if (num > 0)
				{
					ManualLogSource log = Log;
					bool flag = default(bool);
					BepInExDebugLogInterpolatedStringHandler val = new BepInExDebugLogInterpolatedStringHandler(27, 1, ref flag);
					if (flag)
					{
						((BepInExLogInterpolatedStringHandler)val).AppendLiteral("Removed ");
						((BepInExLogInterpolatedStringHandler)val).AppendFormatted<int>(num);
						((BepInExLogInterpolatedStringHandler)val).AppendLiteral(" escaped Expanders.");
					}
					log.LogDebug(val);
				}
			}

			internal bool <Postfix>b__2(Slot x)
			{
				if (x.Parent == world.RootSlot)
				{
					return world.LocalUser.IsAllocatingUser((IWorldElement)(object)x);
				}
				return false;
			}
		}

		public static void Postfix(Worker worker, WorkerInspector __instance, bool allowContainer)
		{
			//IL_0059: Unknown result type (might be due to invalid IL or missing references)
			//IL_0070: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a0: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a7: Expected O, but got Unknown
			//IL_00c0: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d5: Unknown result type (might be due to invalid IL or missing references)
			//IL_00da: Unknown result type (might be due to invalid IL or missing references)
			//IL_0125: Unknown result type (might be due to invalid IL or missing references)
			//IL_012a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0142: Expected O, but got Unknown
			//IL_0269: Unknown result type (might be due to invalid IL or missing references)
			//IL_027a: Unknown result type (might be due to invalid IL or missing references)
			<>c__DisplayClass0_0 CS$<>8__locals0 = new <>c__DisplayClass0_0();
			CS$<>8__locals0.worker = worker;
			if (!Enabled.Value || CS$<>8__locals0.worker is Slot || allowContainer)
			{
				return;
			}
			SceneInspector componentInParents = ((Component)__instance).Slot.GetComponentInParents<SceneInspector>((Predicate<SceneInspector>)null, true, false);
			if (componentInParents == null || !((Worker)componentInParents).World.LocalUser.IsAllocatingUser((IWorldElement)(object)componentInParents))
			{
				return;
			}
			Slot val = Enumerable.LastOrDefault<Slot>((global::System.Collections.Generic.IEnumerable<Slot>)(object)((Component)__instance).Slot.Children);
			Slot val2 = ((val != null) ? Enumerable.FirstOrDefault<Slot>((global::System.Collections.Generic.IEnumerable<Slot>)(object)val.Children) : null);
			if (val2 == null)
			{
				return;
			}
			Slot val3 = val.AddSlot("ExpanderFolder", true);
			val3.Tag = "CollapseableComponentsTag";
			UIBuilder val4 = new UIBuilder(val2, (Slot)null);
			((SyncField<float>)(object)((DirectionalLayout)((ContainerWorker<Component>)(object)val3).AttachComponent<VerticalLayout>(true, (Action<VerticalLayout>)null)).Spacing).Value = 4f;
			Enumerator<Slot> enumerator = new List<Slot>(Enumerable.Skip<Slot>((global::System.Collections.Generic.IEnumerable<Slot>)(object)val.Children, 1)).GetEnumerator();
			try
			{
				while (enumerator.MoveNext())
				{
					enumerator.Current.Parent = val3;
				}
			}
			finally
			{
				((global::System.IDisposable)enumerator).Dispose();
			}
			RadiantUI_Constants.SetupEditorStyle(val4, false);
			val4.Style.MinWidth = 80f;
			Uri val5 = new Uri("resdb:///dc53547406616593fd9601fae527aa450e82a1ff3606096161dd2038b7e219f3");
			colorX? val6 = Sub.PURPLE;
			Button val7 = val4.Button(val5, ref val6, ref Hero.PURPLE);
			((Component)val7).Slot.OrderOffset = -1L;
			Expander obj = ((ContainerWorker<Component>)(object)((Component)val7).Slot).AttachComponent<Expander>(true, (Action<Expander>)null);
			obj.SectionRoot.Target = val3;
			obj.IsExpanded = GetCollapseDefault(CS$<>8__locals0.worker.LocalUser);
			Slot val8 = ((Component)val7).Slot[0];
			if (val8 != null)
			{
				((ComponentBase<Component>)(object)((ContainerWorker<Component>)(object)val8).GetComponent<Image>((Predicate<Image>)null, false))?.Destroy();
				((ComponentBase<Component>)(object)((ContainerWorker<Component>)(object)val8).GetComponent<SpriteProvider>((Predicate<SpriteProvider>)null, false))?.Destroy();
				RawImage val9 = ((ContainerWorker<Component>)(object)val8).AttachComponent<RawImage>(true, (Action<RawImage>)null);
				((SyncRef<IAssetProvider<ITexture2D>>)(object)val9.Texture).Target = (IAssetProvider<ITexture2D>)(object)((ContainerWorker<Component>)(object)val8).GetComponent<StaticTexture2D>((Predicate<StaticTexture2D>)null, false);
				((SyncField<bool>)(object)val9.PreserveAspect).Value = true;
				BooleanValueDriver<RectOrientation> obj2 = ((ContainerWorker<Component>)(object)val8).AttachComponent<BooleanValueDriver<RectOrientation>>(true, (Action<BooleanValueDriver<RectOrientation>>)null);
				((SyncField<RectOrientation>)(object)obj2.FalseValue).Value = (RectOrientation)3;
				((SyncField<RectOrientation>)(object)obj2.TrueValue).Value = (RectOrientation)0;
				((SyncRef<IField<RectOrientation>>)(object)obj2.TargetField).Target = (IField<RectOrientation>)(object)val9.Orientation;
				ValueCopyExtensions.DriveFrom<bool>((IField<bool>)(object)obj2.State, (IField<bool>)(object)val3.ActiveSelf_Field, false, false, true);
				((SyncRef<IField<colorX>>)(object)((SyncElementList<ColorDriver>)(object)((InteractionElement)val7).ColorDrivers)[1].ColorDrive).Target = (IField<colorX>)(object)val9.Tint;
				RectTransform component = ((ContainerWorker<Component>)(object)val8).GetComponent<RectTransform>((Predicate<RectTransform>)null, false);
				if (component != null)
				{
					((SyncField<float2>)(object)component.OffsetMin).Value = float2.Zero;
					((SyncField<float2>)(object)component.OffsetMax).Value = float2.Zero;
				}
			}
			if (RunCleanUp.Value)
			{
				RunOnce([AsyncStateMachine(typeof(<>c__DisplayClass0_0.<<Postfix>b__0>d))] () =>
				{
					//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)
					<>c__DisplayClass0_0.<<Postfix>b__0>d <<Postfix>b__0>d = default(<>c__DisplayClass0_0.<<Postfix>b__0>d);
					<<Postfix>b__0>d.<>t__builder = AsyncTaskMethodBuilder.Create();
					<<Postfix>b__0>d.<>4__this = CS$<>8__locals0;
					<<Postfix>b__0>d.<>1__state = -1;
					((AsyncTaskMethodBuilder)(ref <<Postfix>b__0>d.<>t__builder)).Start<<>c__DisplayClass0_0.<<Postfix>b__0>d>(ref <<Postfix>b__0>d);
					return ((AsyncTaskMethodBuilder)(ref <<Postfix>b__0>d.<>t__builder)).Task;
				});
			}
		}
	}

	[StructLayout(3)]
	[CompilerGenerated]
	private struct <RunOnce>d__9 : IAsyncStateMachine
	{
		public int <>1__state;

		public AsyncTaskMethodBuilder <>t__builder;

		public Func<global::System.Threading.Tasks.Task> action;

		private TaskAwaiter <>u__1;

		private void MoveNext()
		{
			//IL_00a1: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a6: 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_0014: Unknown result type (might be due to invalid IL or missing references)
			//IL_0074: Unknown result type (might be due to invalid IL or missing references)
			//IL_0079: Unknown result type (might be due to invalid IL or missing references)
			//IL_0080: Unknown result type (might be due to invalid IL or missing references)
			//IL_0044: Unknown result type (might be due to invalid IL or missing references)
			//IL_0049: Unknown result type (might be due to invalid IL or missing references)
			//IL_005d: Unknown result type (might be due to invalid IL or missing references)
			//IL_005e: Unknown result type (might be due to invalid IL or missing references)
			int num = <>1__state;
			try
			{
				if (num == 0)
				{
					goto IL_0036;
				}
				Scope val = Sync.EnterScope();
				try
				{
					if (!_isRunning)
					{
						_isRunning = true;
						goto IL_0036;
					}
				}
				finally
				{
					if (num < 0)
					{
						((Scope)(ref val)).Dispose();
					}
				}
				goto end_IL_0007;
				IL_0036:
				try
				{
					TaskAwaiter awaiter;
					if (num != 0)
					{
						awaiter = action.Invoke().GetAwaiter();
						if (!((TaskAwaiter)(ref awaiter)).IsCompleted)
						{
							num = (<>1__state = 0);
							<>u__1 = awaiter;
							((AsyncTaskMethodBuilder)(ref <>t__builder)).AwaitUnsafeOnCompleted<TaskAwaiter, <RunOnce>d__9>(ref awaiter, ref this);
							return;
						}
					}
					else
					{
						awaiter = <>u__1;
						<>u__1 = default(TaskAwaiter);
						num = (<>1__state = -1);
					}
					((TaskAwaiter)(ref awaiter)).GetResult();
				}
				finally
				{
					if (num < 0)
					{
						val = Sync.EnterScope();
						try
						{
							_isRunning = false;
						}
						finally
						{
							if (num < 0)
							{
								((Scope)(ref val)).Dispose();
							}
						}
					}
				}
				end_IL_0007:;
			}
			catch (global::System.Exception exception)
			{
				<>1__state = -2;
				((AsyncTaskMethodBuilder)(ref <>t__builder)).SetException(exception);
				return;
			}
			<>1__state = -2;
			((AsyncTaskMethodBuilder)(ref <>t__builder)).SetResult();
		}

		[DebuggerHidden]
		private void SetStateMachine(IAsyncStateMachine stateMachine)
		{
			((AsyncTaskMethodBuilder)(ref <>t__builder)).SetStateMachine(stateMachine);
		}
	}

	internal static ManualLogSource Log = null;

	internal static ConfigEntry<bool> Enabled;

	internal static ConfigEntry<bool> DefaultExpanded;

	internal static ConfigEntry<bool> RunCleanUp;

	private static bool _isRunning;

	private static readonly Lock Sync = new Lock();

	public override void Load()
	{
		//IL_0080: Unknown result type (might be due to invalid IL or missing references)
		//IL_0086: Expected O, but got Unknown
		Log = ((BasePlugin)this).Log;
		Enabled = ((BasePlugin)this).Config.Bind<bool>("General", "Enabled", true, "Enables/Disables the plugin.");
		DefaultExpanded = ((BasePlugin)this).Config.Bind<bool>("General", "DefaultExpanded", true, "Whether the components are expanded by default, You can control this with a user variable 'User/Inspector_Collapse_Default' (bool) as well.");
		RunCleanUp = ((BasePlugin)this).Config.Bind<bool>("General", "RunCleanUp", true, "Whether the plugin should cleanup escaped Expanders after 10 seconds.");
		((BasePlugin)this).HarmonyInstance.PatchAll();
		ManualLogSource log = Log;
		bool flag = default(bool);
		BepInExInfoLogInterpolatedStringHandler val = new BepInExInfoLogInterpolatedStringHandler(18, 1, ref flag);
		if (flag)
		{
			((BepInExLogInterpolatedStringHandler)val).AppendLiteral("Plugin ");
			((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>("NepuShiro.CollapseableComponents");
			((BepInExLogInterpolatedStringHandler)val).AppendLiteral(" is loaded!");
		}
		log.LogInfo(val);
	}

	private static bool GetCollapseDefault(User localUser)
	{
		if (localUser != null)
		{
			UserRoot root = localUser.Root;
			bool? obj;
			bool flag = default(bool);
			if (root == null)
			{
				obj = null;
			}
			else
			{
				Slot slot = ((Component)root).Slot;
				if (slot == null)
				{
					obj = null;
				}
				else
				{
					DynamicVariableSpace component = ((ContainerWorker<Component>)(object)slot).GetComponent<DynamicVariableSpace>((Predicate<DynamicVariableSpace>)((DynamicVariableSpace x) => ((SyncField<string>)(object)x.SpaceName).Value == "User"), false);
					obj = ((component != null) ? new bool?(component.TryReadValue<bool>("Inspector_Collapse_Default", ref flag)) : null);
				}
			}
			bool? flag2 = obj;
			if (flag2.GetValueOrDefault())
			{
				return !flag;
			}
		}
		return DefaultExpanded.Value;
	}

	[AsyncStateMachine(typeof(<RunOnce>d__9))]
	private static global::System.Threading.Tasks.Task RunOnce(Func<global::System.Threading.Tasks.Task> action)
	{
		//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)
		<RunOnce>d__9 <RunOnce>d__ = default(<RunOnce>d__9);
		<RunOnce>d__.<>t__builder = AsyncTaskMethodBuilder.Create();
		<RunOnce>d__.action = action;
		<RunOnce>d__.<>1__state = -1;
		((AsyncTaskMethodBuilder)(ref <RunOnce>d__.<>t__builder)).Start<<RunOnce>d__9>(ref <RunOnce>d__);
		return ((AsyncTaskMethodBuilder)(ref <RunOnce>d__.<>t__builder)).Task;
	}
}
public static class Helper
{
	public static bool IsAllocatingUser(this User initUser, IWorldElement element)
	{
		//IL_0009: Unknown result type (might be due to invalid IL or missing references)
		//IL_000e: Unknown result type (might be due to invalid IL or missing references)
		if (element == null || initUser == null)
		{
			return false;
		}
		RefID referenceID = element.ReferenceID;
		ulong num = default(ulong);
		byte b = default(byte);
		((RefID)(ref referenceID)).ExtractIDs(ref num, ref b);
		User userByAllocationID = element.World.GetUserByAllocationID(b);
		if (userByAllocationID == null || num < userByAllocationID.AllocationIDStart)
		{
			return false;
		}
		return userByAllocationID == initUser;
	}
}
public static class PluginMetadata
{
	public const string GUID = "NepuShiro.CollapseableComponents";

	public const string NAME = "CollapseableComponents";

	public const string VERSION = "1.0.2";

	public const string AUTHORS = "NepuShiro";

	public const string REPOSITORY_URL = "https://github.com/NepuShiro/CollapseableComponents";
}