Decompiled source of TypePicker v1.0.0

plugins/TypePicker/TypePicker.dll

Decompiled a month ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Reflection.Emit;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Logging;
using BepInEx.NET.Common;
using BepInExResoniteShim;
using Elements.Core;
using FrooxEngine;
using FrooxEngine.UIX;
using HarmonyLib;

[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("TumnusB")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0+3116292cde73e3e66cbcf11d7acc7ecba3de056f")]
[assembly: AssemblyProduct("TypePicker")]
[assembly: AssemblyTitle("TypePicker")]
[assembly: AssemblyMetadata("RepositoryUrl", "https://github.com/TumnusB/TypePicker")]
[assembly: AssemblyVersion("1.0.0.0")]
[module: RefSafetyRules(11)]
namespace TypePicker;

[ResonitePlugin("TumnusB.TypePicker", "TypePicker", "1.0.0", "TumnusB", "https://github.com/TumnusB/TypePicker")]
[BepInDependency(/*Could not decode attribute arguments.*/)]
public class TypePicker : BasePlugin
{
	[HarmonyPatch(typeof(ComponentSelector), "BuildUI")]
	private class ComponentSelector_BuildUI_Patch
	{
		private static global::System.Collections.Generic.IEnumerable<CodeInstruction> Transpiler(global::System.Collections.Generic.IEnumerable<CodeInstruction> instructions)
		{
			//IL_0016: Unknown result type (might be due to invalid IL or missing references)
			//IL_001b: 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_0098: Unknown result type (might be due to invalid IL or missing references)
			//IL_009e: Expected O, but got Unknown
			//IL_00a0: 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_00ac: Expected O, but got Unknown
			//IL_00ae: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b5: Unknown result type (might be due to invalid IL or missing references)
			//IL_00bb: Expected O, but got Unknown
			int num = -1;
			List<CodeInstruction> val = new List<CodeInstruction>(instructions);
			for (int i = 0; i < val.Count; i++)
			{
				CodeInstruction val2 = val[i];
				if (!(val2.opcode != OpCodes.Ldstr) && ((string)val2.operand).Contains("ComponentSelector.CustomGenericArguments"))
				{
					Log.LogInfo((object)("Found code at index " + i));
					num = i - 1;
					break;
				}
			}
			if (num > -1)
			{
				MethodInfo method = typeof(TypePicker).GetMethod("BuildComponentSelectorUI", (BindingFlags)24);
				val.InsertRange(num, (global::System.Collections.Generic.IEnumerable<CodeInstruction>)(object)new CodeInstruction[3]
				{
					new CodeInstruction(OpCodes.Ldarg_0, (object)null),
					new CodeInstruction(OpCodes.Ldloc_0, (object)null),
					new CodeInstruction(OpCodes.Call, (object)method)
				});
				Log.LogInfo((object)"Patched");
			}
			else
			{
				Log.LogInfo((object)"Could not find patch target! This means the mod won't do anything.");
			}
			return Enumerable.AsEnumerable<CodeInstruction>((global::System.Collections.Generic.IEnumerable<CodeInstruction>)val);
		}
	}

	internal static ManualLogSource Log;

	public override void Load()
	{
		Log = ((BasePlugin)this).Log;
		((BasePlugin)this).HarmonyInstance.PatchAll();
	}

	public static void BuildComponentSelectorUI(ComponentSelector selector, object uiDisplayClass)
	{
		//IL_0072: Unknown result type (might be due to invalid IL or missing references)
		//IL_007c: Expected O, but got Unknown
		//IL_0077: 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_00be: Unknown result type (might be due to invalid IL or missing references)
		//IL_00cd: Unknown result type (might be due to invalid IL or missing references)
		//IL_00d7: Expected O, but got Unknown
		//IL_00dd: Unknown result type (might be due to invalid IL or missing references)
		//IL_00e2: Unknown result type (might be due to invalid IL or missing references)
		//IL_00f1: Unknown result type (might be due to invalid IL or missing references)
		//IL_00fb: Expected O, but got Unknown
		//IL_0107: Unknown result type (might be due to invalid IL or missing references)
		//IL_010c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0141: Unknown result type (might be due to invalid IL or missing references)
		//IL_0146: Unknown result type (might be due to invalid IL or missing references)
		//IL_0155: Unknown result type (might be due to invalid IL or missing references)
		//IL_015f: Expected O, but got Unknown
		//IL_0165: Unknown result type (might be due to invalid IL or missing references)
		//IL_016a: Unknown result type (might be due to invalid IL or missing references)
		//IL_0179: Unknown result type (might be due to invalid IL or missing references)
		//IL_0183: Expected O, but got Unknown
		//IL_0189: Unknown result type (might be due to invalid IL or missing references)
		//IL_018e: 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_01a7: Expected O, but got Unknown
		//IL_01ad: Unknown result type (might be due to invalid IL or missing references)
		//IL_01b2: Unknown result type (might be due to invalid IL or missing references)
		//IL_01c1: Unknown result type (might be due to invalid IL or missing references)
		//IL_01cb: Expected O, but got Unknown
		UIBuilder value = Traverse.Create(uiDisplayClass).Field<UIBuilder>("ui").Value;
		SyncRefList<TextField> fields = (SyncRefList<TextField>)(object)((Worker)selector).GetSyncMember("_customGenericArguments");
		ReferenceField<IWorldElement> refField = ((ContainerWorker<Component>)(object)WorldElementExtensions.FindNearestParent<Slot>((IWorldElement)(object)selector, (Predicate<Slot>)null)).GetComponentOrAttach<ReferenceField<IWorldElement>>((Predicate<ReferenceField<IWorldElement>>)null);
		Sync<ushort> paramIndex = ((ContainerWorker<Component>)(object)WorldElementExtensions.FindNearestParent<Slot>((IWorldElement)(object)selector, (Predicate<Slot>)null)).GetComponentOrAttach<ValueField<ushort>>((Predicate<ValueField<ushort>>)null).Value;
		((SyncField<ushort>)(object)paramIndex).Value = 0;
		((ComponentBase<Component>)(object)selector).RunInUpdates(1, (Action)delegate
		{
			//IL_006b: Unknown result type (might be due to invalid IL or missing references)
			if (((SyncElementList<SyncRef<TextField>>)(object)fields).Count > 1)
			{
				for (int i = 0; i < ((SyncElementList<SyncRef<TextField>>)(object)fields).Count; i++)
				{
					Slot obj = ((Component)fields[i]).Slot.Parent.Parent[0][0];
					Button val2 = ((ContainerWorker<Component>)(object)obj).AttachComponent<Button>(true, (Action<Button>)null);
					Text component = ((ContainerWorker<Component>)(object)obj).GetComponent<Text>((Predicate<Text>)null, false);
					val2.SetupBackgroundColor(component.Color);
					((SyncField<colorX>)(object)Enumerable.FirstOrDefault<ColorDriver>((global::System.Collections.Generic.IEnumerable<ColorDriver>)((InteractionElement)val2).ColorDrivers).DisabledColor).Value = Hero.GREEN;
					ValueRadio<ushort> val3 = ((ContainerWorker<Component>)(object)obj).AttachComponent<ValueRadio<ushort>>(true, (Action<ValueRadio<ushort>>)null);
					((SyncField<ushort>)(object)val3.OptionValue).Value = (ushort)i;
					((SyncRef<IField<ushort>>)(object)val3.TargetValue).Target = (IField<ushort>)(object)paramIndex;
					BooleanValueDriver<bool> val4 = ((ContainerWorker<Component>)(object)obj).AttachComponent<BooleanValueDriver<bool>>(true, (Action<BooleanValueDriver<bool>>)null);
					((SyncField<bool>)(object)val4.FalseValue).Value = true;
					((SyncField<bool>)(object)val4.TrueValue).Value = false;
					((SyncRef<IField<bool>>)(object)val4.TargetField).Target = (IField<bool>)(object)((ComponentBase<Component>)(object)val2).EnabledField;
					((SyncRef<IField<bool>>)(object)((Radio)val3).CheckVisual).Target = (IField<bool>)(object)val4.State;
				}
			}
		});
		SyncMemberEditorBuilder.Build((ISyncMember)(object)refField.Reference, "Type picker", (FieldInfo)null, value, 0.3f);
		value.HorizontalLayout(8f, 0f, (Alignment?)null);
		LocaleString val = LocaleString.op_Implicit("Base type");
		value.Button(ref val).LocalPressed += (ButtonEventHandler)delegate
		{
			SetType(fields[(int)((SyncField<ushort>)(object)paramIndex).Value], FindBaseType(refField));
		};
		val = LocaleString.op_Implicit("Inner type");
		value.Button(ref val).LocalPressed += (ButtonEventHandler)delegate
		{
			SetType(fields[(int)((SyncField<ushort>)(object)paramIndex).Value], FindInnerType(refField));
		};
		value.NestOut();
		val = LocaleString.op_Implicit("Cast to:");
		value.Text(ref val, true, (Alignment?)null, true, (string)null);
		value.HorizontalLayout(8f, 0f, (Alignment?)null);
		val = LocaleString.op_Implicit("SyncRef");
		value.Button(ref val).LocalPressed += (ButtonEventHandler)delegate
		{
			SetType(fields[(int)((SyncField<ushort>)(object)paramIndex).Value], CastToSyncRef(refField));
		};
		val = LocaleString.op_Implicit("SyncRef Inner");
		value.Button(ref val).LocalPressed += (ButtonEventHandler)delegate
		{
			SetType(fields[(int)((SyncField<ushort>)(object)paramIndex).Value], CastToSyncRefInner(refField));
		};
		val = LocaleString.op_Implicit("IField");
		value.Button(ref val).LocalPressed += (ButtonEventHandler)delegate
		{
			SetType(fields[(int)((SyncField<ushort>)(object)paramIndex).Value], CastToIField(refField));
		};
		val = LocaleString.op_Implicit("IField Inner");
		value.Button(ref val).LocalPressed += (ButtonEventHandler)delegate
		{
			SetType(fields[(int)((SyncField<ushort>)(object)paramIndex).Value], CastToIFieldInner(refField));
		};
		value.NestOut();
	}

	private static global::System.Type? CastToIField(ReferenceField<IWorldElement> refField)
	{
		IWorldElement target = refField.Reference.Target;
		IField val = (IField)(object)((target is IField) ? target : null);
		if (val != null)
		{
			return typeof(IField<>).MakeGenericType(new global::System.Type[1] { val.ValueType });
		}
		return null;
	}

	private static global::System.Type? CastToIFieldInner(ReferenceField<IWorldElement> refField)
	{
		IWorldElement target = refField.Reference.Target;
		IField val = (IField)(object)((target is IField) ? target : null);
		if (val == null)
		{
			return null;
		}
		return val.ValueType;
	}

	private static global::System.Type? CastToSyncRef(ReferenceField<IWorldElement> refField)
	{
		global::System.Type type = FindBaseType(refField);
		if (type == null)
		{
			return null;
		}
		IWorldElement target = refField.Reference.Target;
		ISyncRef val = (ISyncRef)(object)((target is ISyncRef) ? target : null);
		if (val != null)
		{
			return typeof(ISyncRef<>).MakeGenericType(new global::System.Type[1] { val.TargetType });
		}
		global::System.Type type2 = typeof(ISyncRef<>).MakeGenericType(new global::System.Type[1] { type });
		if (ReflectionExtensions.IsValidGenericType(type2, true))
		{
			return type2;
		}
		return null;
	}

	private static global::System.Type? CastToSyncRefInner(ReferenceField<IWorldElement> refField)
	{
		IWorldElement target = refField.Reference.Target;
		ISyncRef val = (ISyncRef)(object)((target is ISyncRef) ? target : null);
		if (val == null)
		{
			return null;
		}
		return val.TargetType;
	}

	private static global::System.Type? FindBaseType(ReferenceField<IWorldElement> refField)
	{
		return ((object)refField.Reference.Target)?.GetType();
	}

	private static global::System.Type? FindInnerType(ReferenceField<IWorldElement> refField)
	{
		global::System.Type type = FindBaseType(refField);
		if (type == null || !type.IsConstructedGenericType)
		{
			return null;
		}
		if (type == null)
		{
			return null;
		}
		return type.GenericTypeArguments[0];
	}

	private static void SetType(TextField field, global::System.Type? type)
	{
		if (type == null)
		{
			field.Editor.Target.Text.Target.Text = null;
			return;
		}
		try
		{
			field.Editor.Target.Text.Target.Text = ((Worker)field).World.Types.EncodeType(type);
		}
		catch (global::System.Exception ex)
		{
			Log.LogError((object)ex);
			field.Editor.Target.Text.Target.Text = null;
		}
	}
}
public static class PluginMetadata
{
	public const string GUID = "TumnusB.TypePicker";

	public const string NAME = "TypePicker";

	public const string VERSION = "1.0.0";

	public const string AUTHORS = "TumnusB";

	public const string REPOSITORY_URL = "https://github.com/TumnusB/TypePicker";
}