using System; using System.Collections; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Reflection; using System.Reflection.Emit; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using System.Runtime.Versioning; 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 HarmonyLib; using Renderite.Shared; [assembly: CompilationRelaxations(8)] [assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)] [assembly: Debuggable(/*Could not decode attribute arguments.*/)] [assembly: TargetFramework(".NETCoreApp,Version=v10.0", FrameworkDisplayName = ".NET 10.0")] [assembly: AssemblyCompany("art0007i")] [assembly: AssemblyConfiguration("Release")] [assembly: AssemblyFileVersion("1.0.6.0")] [assembly: AssemblyInformationalVersion("1.0.6+4ed676e363837bc3774ccae84dd6ce9a61c162e6")] [assembly: AssemblyProduct("DefaultToolOverride")] [assembly: AssemblyTitle("DefaultToolOverride")] [assembly: AssemblyMetadata("RepositoryUrl", "https://github.com/art0007i/DefaultToolOverride")] [assembly: AssemblyVersion("1.0.6.0")] [module: RefSafetyRules(11)] namespace DefaultToolOverride; [ResonitePlugin("art0007i.DefaultToolOverride", "DefaultToolOverride", "1.0.6", "art0007i", "https://github.com/art0007i/DefaultToolOverride")] [BepInDependency(/*Could not decode attribute arguments.*/)] public class Plugin : BasePlugin { public enum OverrideType { Fallback, None, Dequip, URL, ClassName } public enum ModifierKey { None, Shift, Alt, Control, Any } public struct ToolReplacement { [CompilerGenerated] private global::System.Type k__BackingField; public OverrideType Type => P.Value; public string Str => P.Value; public global::System.Type ToolType { get { string str = Str; if (k__BackingField == (global::System.Type)null || !str.Contains(((MemberInfo)k__BackingField).Name)) { k__BackingField = TypeHelper.FindType(str); } return k__BackingField; } } public ToolReplacement(ConfigEntry type, ConfigEntry str) { P = type; P = str; k__BackingField = null; } } [HarmonyPatch(typeof(InteractionHandler), "OnInputUpdate")] private class DefaultToolOverridePatch { [CompilerGenerated] private sealed class <>c__DisplayClass0_0 { [StructLayout((LayoutKind)3)] private struct <b__0>d : IAsyncStateMachine { public int <>1__state; public AsyncTaskMethodBuilder <>t__builder; public <>c__DisplayClass0_0 <>4__this; private TaskAwaiter <>u__1; private void MoveNext() { //IL_0052: Unknown result type (might be due to invalid IL or missing references) //IL_0057: 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) //IL_0022: Unknown result type (might be due to invalid IL or missing references) //IL_0027: Unknown result type (might be due to invalid IL or missing references) //IL_003b: Unknown result type (might be due to invalid IL or missing references) //IL_003c: Unknown result type (might be due to invalid IL or missing references) int num = <>1__state; <>c__DisplayClass0_0 <>c__DisplayClass0_ = <>4__this; try { TaskAwaiter val; if (num != 0) { val = <>c__DisplayClass0_.instance.SpawnAndEquip(<>c__DisplayClass0_.uri).GetAwaiter(); if (!((TaskAwaiter)(ref val)).IsCompleted) { num = (<>1__state = 0); <>u__1 = val; ((AsyncTaskMethodBuilder)(ref <>t__builder)).AwaitUnsafeOnCompletedb__0>d>(ref val, ref this); return; } } else { val = <>u__1; <>u__1 = default(TaskAwaiter); num = (<>1__state = -1); } ((TaskAwaiter)(ref val)).GetResult(); } 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); } } public InteractionHandler instance; public Uri uri; [AsyncStateMachine(typeof(<b__0>d))] internal global::System.Threading.Tasks.Task 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) <b__0>d <b__0>d = default(<b__0>d); <b__0>d.<>t__builder = AsyncTaskMethodBuilder.Create(); <b__0>d.<>4__this = this; <b__0>d.<>1__state = -1; ((AsyncTaskMethodBuilder)(ref <b__0>d.<>t__builder)).Start<<b__0>d>(ref <b__0>d); return ((AsyncTaskMethodBuilder)(ref <b__0>d.<>t__builder)).Task; } } [CompilerGenerated] private sealed class d__1 : global::System.Collections.Generic.IEnumerable, global::System.Collections.IEnumerable, global::System.Collections.Generic.IEnumerator, global::System.Collections.IEnumerator, global::System.IDisposable { private int <>1__state; private CodeInstruction <>2__current; private int <>l__initialThreadId; private MethodBase method; public MethodBase <>3__method; private global::System.Collections.Generic.IEnumerable codes; public global::System.Collections.Generic.IEnumerable <>3__codes; private int 5__2; private Label 5__3; private global::System.Collections.Generic.IEnumerator <>7__wrap3; private CodeInstruction 5__5; CodeInstruction global::System.Collections.Generic.IEnumerator.Current { [DebuggerHidden] get { return <>2__current; } } object global::System.Collections.IEnumerator.Current { [DebuggerHidden] get { return <>2__current; } } [DebuggerHidden] public d__1(int <>1__state) { this.<>1__state = <>1__state; <>l__initialThreadId = Environment.CurrentManagedThreadId; } [DebuggerHidden] void global::System.IDisposable.Dispose() { int num = <>1__state; if (num == -3 || (uint)(num - 1) <= 4u) { try { } finally { <>m__Finally1(); } } <>7__wrap3 = null; 5__5 = null; <>1__state = -2; } private bool MoveNext() { //IL_0167: Unknown result type (might be due to invalid IL or missing references) //IL_0180: Unknown result type (might be due to invalid IL or missing references) //IL_018a: Expected O, but got Unknown //IL_01a1: Unknown result type (might be due to invalid IL or missing references) //IL_01a7: Unknown result type (might be due to invalid IL or missing references) //IL_01b1: Unknown result type (might be due to invalid IL or missing references) //IL_01bb: Expected O, but got Unknown //IL_01d2: Unknown result type (might be due to invalid IL or missing references) //IL_01e3: Unknown result type (might be due to invalid IL or missing references) //IL_01ed: Expected O, but got Unknown //IL_007c: Unknown result type (might be due to invalid IL or missing references) //IL_0082: Expected O, but got Unknown //IL_00a8: Unknown result type (might be due to invalid IL or missing references) //IL_0108: Unknown result type (might be due to invalid IL or missing references) //IL_010d: Unknown result type (might be due to invalid IL or missing references) //IL_00f8: Unknown result type (might be due to invalid IL or missing references) //IL_00fd: Unknown result type (might be due to invalid IL or missing references) //IL_0140: 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_0150: Expected O, but got Unknown try { switch (<>1__state) { default: return false; case 0: { <>1__state = -1; 5__2 = Enumerable.Last((global::System.Collections.Generic.IEnumerable)method.GetMethodBody().LocalVariables, (Func)((LocalVariableInfo x) => x.LocalType == typeof(int))).LocalIndex; ManualLogSource log = Log; bool flag = default(bool); BepInExDebugLogInterpolatedStringHandler val = new BepInExDebugLogInterpolatedStringHandler(21, 1, ref flag); if (flag) { ((BepInExLogInterpolatedStringHandler)val).AppendLiteral("Found local at index "); ((BepInExLogInterpolatedStringHandler)val).AppendFormatted(5__2); } log.LogDebug(val); 5__3 = default(Label); <>7__wrap3 = codes.GetEnumerator(); <>1__state = -3; goto IL_0226; } case 1: <>1__state = -3; <>2__current = new CodeInstruction(OpCodes.Call, (object)typeof(DefaultToolOverridePatch).GetMethod("ToolIntercept")); <>1__state = 2; return true; case 2: <>1__state = -3; <>2__current = new CodeInstruction(OpCodes.Brfalse, (object)5__3); <>1__state = 3; return true; case 3: <>1__state = -3; <>2__current = new CodeInstruction(OpCodes.Ldloc_S, (object)(byte)5__2); <>1__state = 4; return true; case 4: <>1__state = -3; goto IL_0200; case 5: { <>1__state = -3; 5__5 = null; goto IL_0226; } IL_0226: if (((global::System.Collections.IEnumerator)<>7__wrap3).MoveNext()) { 5__5 = <>7__wrap3.Current; Label? val2 = default(Label?); if (CodeInstructionExtensions.Branches(5__5, ref val2) && val2.HasValue) { 5__3 = val2.Value; } if (5__5.opcode == OpCodes.Switch && 5__5.operand is Label[] array && array.Length > 6) { <>2__current = new CodeInstruction(OpCodes.Ldarg_0, (object)null); <>1__state = 1; return true; } goto IL_0200; } <>m__Finally1(); <>7__wrap3 = null; return false; IL_0200: <>2__current = 5__5; <>1__state = 5; return true; } } catch { //try-fault ((global::System.IDisposable)this).Dispose(); throw; } } bool global::System.Collections.IEnumerator.MoveNext() { //ILSpy generated this explicit interface implementation from .override directive in MoveNext return this.MoveNext(); } private void <>m__Finally1() { <>1__state = -1; if (<>7__wrap3 != null) { ((global::System.IDisposable)<>7__wrap3).Dispose(); } } [DebuggerHidden] void global::System.Collections.IEnumerator.Reset() { //IL_0000: Unknown result type (might be due to invalid IL or missing references) throw new NotSupportedException(); } [DebuggerHidden] global::System.Collections.Generic.IEnumerator global::System.Collections.Generic.IEnumerable.GetEnumerator() { d__1 d__; if (<>1__state == -2 && <>l__initialThreadId == Environment.CurrentManagedThreadId) { <>1__state = 0; d__ = this; } else { d__ = new d__1(0); } d__.codes = <>3__codes; d__.method = <>3__method; return d__; } [DebuggerHidden] global::System.Collections.IEnumerator global::System.Collections.IEnumerable.GetEnumerator() { return (global::System.Collections.IEnumerator)((global::System.Collections.Generic.IEnumerable)this).GetEnumerator(); } } public static bool ToolIntercept(int toolNum, InteractionHandler instance) { //IL_0153: Unknown result type (might be due to invalid IL or missing references) //IL_015a: Expected O, but got Unknown //IL_01f6: Unknown result type (might be due to invalid IL or missing references) //IL_01fd: Expected O, but got Unknown //IL_01de: Unknown result type (might be due to invalid IL or missing references) //IL_01ea: Expected O, but got Unknown <>c__DisplayClass0_0 CS$<>8__locals9 = new <>c__DisplayClass0_0(); CS$<>8__locals9.instance = instance; bool flag = true; InputInterface inputInterface = ((Worker)CS$<>8__locals9.instance).InputInterface; Key[] array = new Key[3]; RuntimeHelpers.InitializeArray((global::System.Array)array, (RuntimeFieldHandle)/*OpCode not supported: LdMemberToken*/); bool anyKey = inputInterface.GetAnyKey((Key[])(object)array); InputInterface inputInterface2 = ((Worker)CS$<>8__locals9.instance).InputInterface; Key[] array2 = new Key[3]; RuntimeHelpers.InitializeArray((global::System.Array)array2, (RuntimeFieldHandle)/*OpCode not supported: LdMemberToken*/); bool anyKey2 = inputInterface2.GetAnyKey((Key[])(object)array2); InputInterface inputInterface3 = ((Worker)CS$<>8__locals9.instance).InputInterface; Key[] array3 = new Key[3]; RuntimeHelpers.InitializeArray((global::System.Array)array3, (RuntimeFieldHandle)/*OpCode not supported: LdMemberToken*/); bool anyKey3 = inputInterface3.GetAnyKey((Key[])(object)array3); switch (Modifier.Value) { case ModifierKey.Shift: if (!anyKey) { flag = false; } break; case ModifierKey.Alt: if (!anyKey2) { flag = false; } break; case ModifierKey.Control: if (!anyKey3) { flag = false; } break; case ModifierKey.Any: if (!(anyKey || anyKey2 || anyKey3)) { flag = false; } break; } if (flag == InvertModifier.Value) { return false; } ToolReplacement toolReplacement = default(ToolReplacement); if (ConfigKeys.TryGetValue(toolNum, ref toolReplacement)) { bool flag2 = default(bool); switch (toolReplacement.Type) { case OverrideType.Fallback: return true; case OverrideType.None: return false; case OverrideType.Dequip: CS$<>8__locals9.instance.StashCurrentToolOrDequip(); return false; case OverrideType.URL: { if (Uri.TryCreate(toolReplacement.Str, (UriKind)1, ref CS$<>8__locals9.uri)) { ((Worker)CS$<>8__locals9.instance).StartTask((Func)([AsyncStateMachine(typeof(<>c__DisplayClass0_0.<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.<b__0>d <b__0>d = default(<>c__DisplayClass0_0.<b__0>d); <b__0>d.<>t__builder = AsyncTaskMethodBuilder.Create(); <b__0>d.<>4__this = CS$<>8__locals9; <b__0>d.<>1__state = -1; ((AsyncTaskMethodBuilder)(ref <b__0>d.<>t__builder)).Start<<>c__DisplayClass0_0.<b__0>d>(ref <b__0>d); return ((AsyncTaskMethodBuilder)(ref <b__0>d.<>t__builder)).Task; })); return false; } ManualLogSource log2 = Log; BepInExWarningLogInterpolatedStringHandler val = new BepInExWarningLogInterpolatedStringHandler(29, 2, ref flag2); if (flag2) { ((BepInExLogInterpolatedStringHandler)val).AppendLiteral("Invalid tool url '"); ((BepInExLogInterpolatedStringHandler)val).AppendFormatted(toolReplacement.Str); ((BepInExLogInterpolatedStringHandler)val).AppendLiteral("' on tool "); ((BepInExLogInterpolatedStringHandler)val).AppendFormatted(toolNum); ((BepInExLogInterpolatedStringHandler)val).AppendLiteral("."); } log2.LogWarning(val); return true; } case OverrideType.ClassName: { global::System.Type toolType = toolReplacement.ToolType; if (toolType != (global::System.Type)null && typeof(ITool).IsAssignableFrom(toolType)) { SpawnEquipFunc(CS$<>8__locals9.instance, new Uri("tool://tool" + toolNum), toolType); return false; } ManualLogSource log = Log; BepInExWarningLogInterpolatedStringHandler val = new BepInExWarningLogInterpolatedStringHandler(34, 2, ref flag2); if (flag2) { ((BepInExLogInterpolatedStringHandler)val).AppendLiteral("Invalid tool TypeName '"); ((BepInExLogInterpolatedStringHandler)val).AppendFormatted(toolReplacement.Str); ((BepInExLogInterpolatedStringHandler)val).AppendLiteral("' on tool "); ((BepInExLogInterpolatedStringHandler)val).AppendFormatted(toolNum); ((BepInExLogInterpolatedStringHandler)val).AppendLiteral("."); } log.LogWarning(val); return true; } } } return true; } [IteratorStateMachine(typeof(d__1))] public static global::System.Collections.Generic.IEnumerable Transpiler(global::System.Collections.Generic.IEnumerable codes, MethodBase method) { int localIndex = Enumerable.Last((global::System.Collections.Generic.IEnumerable)method.GetMethodBody().LocalVariables, (Func)((LocalVariableInfo x) => x.LocalType == typeof(int))).LocalIndex; ManualLogSource log = Log; bool flag = default(bool); BepInExDebugLogInterpolatedStringHandler val = new BepInExDebugLogInterpolatedStringHandler(21, 1, ref flag); if (flag) { ((BepInExLogInterpolatedStringHandler)val).AppendLiteral("Found local at index "); ((BepInExLogInterpolatedStringHandler)val).AppendFormatted(localIndex); } log.LogDebug(val); Label lastLabel = default(Label); global::System.Collections.Generic.IEnumerator enumerator = codes.GetEnumerator(); try { Label? val2 = default(Label?); while (((global::System.Collections.IEnumerator)enumerator).MoveNext()) { CodeInstruction code = enumerator.Current; if (CodeInstructionExtensions.Branches(code, ref val2) && val2.HasValue) { lastLabel = val2.Value; } if (code.opcode == OpCodes.Switch && code.operand is Label[] array && array.Length > 6) { yield return new CodeInstruction(OpCodes.Ldarg_0, (object)null); yield return new CodeInstruction(OpCodes.Call, (object)typeof(DefaultToolOverridePatch).GetMethod("ToolIntercept")); yield return new CodeInstruction(OpCodes.Brfalse, (object)lastLabel); yield return new CodeInstruction(OpCodes.Ldloc_S, (object)(byte)localIndex); } yield return code; } } finally { ((global::System.IDisposable)enumerator)?.Dispose(); } } public static void SpawnEquipFunc(InteractionHandler instance, Uri uri, global::System.Type toolType) { //IL_00b5: Unknown result type (might be due to invalid IL or missing references) //IL_00ba: Unknown result type (might be due to invalid IL or missing references) Traverse val = Traverse.Create((object)instance); Traverse val2 = val.Field("_currentToolUri"); if (val2.Value == uri) { return; } Slot val3 = null; bool flag = false; try { Dictionary value = val.Field>("_stashedTools").Value; ITool componentInChildren = default(ITool); if (value.TryGetValue(uri, ref componentInChildren) && !((IWorldElement)componentInChildren).IsRemoved) { ((IComponent)componentInChildren).Slot.ActiveSelf = true; val3 = ((IComponent)componentInChildren).Slot; } else { val3 = ((Component)((Worker)instance).LocalUserRoot).Slot.AddSlot(((MemberInfo)toolType).Name, true); ((ContainerWorker)(object)val3).AttachComponent(toolType, true, (Action)null); componentInChildren = val3.GetComponentInChildren((Predicate)null, false, false); } value.Remove(uri); if (componentInChildren == null) { flag = true; } else if (!instance.CanEquip(componentInChildren)) { flag = true; NotificationMessage.SpawnTextMessage(((IComponent)componentInChildren).Slot, LocaleString.op_Implicit("You don't have permission to equip this."), colorX.Red, 3f, 0.15f, 0.5f, 0.1f, 0.2f, false); } else { instance.StashCurrentToolOrDequip(); instance.Equip(componentInChildren, true); val2.Value = uri; } } catch (global::System.Exception ex) { Log.LogError((object)("Exception trying to spawn and equip tooltip:\n" + (object)ex)); flag = true; } if (flag && val3 != null) { val3.Destroy(); } } } internal static ManualLogSource Log; public static ConfigEntry InvertModifier; public static ConfigEntry Modifier; public static Dictionary ConfigKeys = new Dictionary(); private void BuildToolKey(string name, int index) { ConfigEntry type = ((BasePlugin)this).Config.Bind("Tools", name + "_type", OverrideType.Fallback, "Tool " + name + " will use this mode."); ConfigEntry str = ((BasePlugin)this).Config.Bind("Tools", name + "_string", "", "String for Tool " + name + ". (Only for URL / ClassName)"); ConfigKeys.Add(index, new ToolReplacement(type, str)); } public override void Load() { Log = ((BasePlugin)this).Log; InvertModifier = ((BasePlugin)this).Config.Bind("General", "InvertModifier", false, "Reverses the behavior of modifier key. (If you hold the modifier tools will not be spawned)"); Modifier = ((BasePlugin)this).Config.Bind("General", "Modifier", ModifierKey.None, "Tool spawning will require holding this modifier key."); for (int i = 1; i < 10; i++) { BuildToolKey(i.ToString(), i); } BuildToolKey("0", 0); BuildToolKey("minus", 10); ((BasePlugin)this).HarmonyInstance.PatchAll(); } } public static class PluginMetadata { public const string GUID = "art0007i.DefaultToolOverride"; public const string NAME = "DefaultToolOverride"; public const string VERSION = "1.0.6"; public const string AUTHORS = "art0007i"; public const string REPOSITORY_URL = "https://github.com/art0007i/DefaultToolOverride"; }