CrabUI
Loading...
Searching...
No Matches
CUIMultiModResolver.cs
1using System;
2using System.Reflection;
3using System.Runtime.CompilerServices;
4using System.Collections.Generic;
5using System.Collections.Immutable;
6using System.Linq;
7
8using Barotrauma;
9using HarmonyLib;
10using Microsoft.Xna.Framework;
11using System.IO;
12
13namespace CrabUI
14{
15 public class CUIMultiModResolver
16 {
17 internal static void InitStatic()
18 {
19 CUI.OnInit += () =>
20 {
21 //FindOtherInputs();
22 };
23 CUI.OnDispose += () =>
24 {
25 CUIInputs.Clear();
26 CUIs.Clear();
27 MouseInputHandledMethods.Clear();
28 };
29 }
30
31 public static List<object> CUIInputs = new();
32 public static List<object> CUIs = new();
33 public static List<Action<bool>> MouseInputHandledMethods = new();
34
35 public static void MarkOtherInputsAsHandled()
36 {
37 //MouseInputHandledMethods.ForEach(action => action(true));
38
39 foreach (object input in CUIInputs)
40 {
41 try
42 {
43 PropertyInfo setAsHandled = input.GetType().GetProperty("MouseInputHandled");
44 setAsHandled.SetValue(input, true);
45 CUI.Log($"setAsHandled.SetValue(input, true) for {input}");
46 }
47 catch (Exception e)
48 {
49 CUI.Warning($"Couldn't find MouseInputHandled in CUIInput in CUI from other mod ({input.GetType()})");
50 continue;
51 }
52 }
53 }
54
55 public static void FindOtherInputs()
56 {
57 AppDomain currentDomain = AppDomain.CurrentDomain;
58
59 foreach (Assembly asm in currentDomain.GetAssemblies())
60 {
61 foreach (Type T in asm.GetTypes())
62 {
63 if (T.Name == "CUI")
64 {
65 try
66 {
67 FieldInfo InstanceField = T.GetField("Instance", BindingFlags.Static | BindingFlags.Public);
68 object CUIInstance = InstanceField.GetValue(null);
69 if (CUIInstance != null && CUIInstance != CUI.Instance)
70 {
71 CUIs.Add(CUIInstance);
72 FieldInfo inputField = T.GetField("input", AccessTools.all);
73
74 object input = inputField.GetValue(CUIInstance);
75 if (input != null) CUIInputs.Add(input);
76 }
77 }
78 catch (Exception e)
79 {
80 CUI.Warning($"Couldn't find CUIInputs in CUI from other mod ({T})");
81 continue;
82 }
83 }
84 }
85 }
86
87 foreach (object input in CUIInputs)
88 {
89 try
90 {
91 PropertyInfo setAsHandled = input.GetType().GetProperty("MouseInputHandled");
92 MouseInputHandledMethods.Add(setAsHandled.SetMethod.CreateDelegate<Action<bool>>(input));
93 }
94 catch (Exception e)
95 {
96 CUI.Warning($"Couldn't find MouseInputHandled in CUIInput in CUI from other mod ({input.GetType()})");
97 continue;
98 }
99 }
100 }
101
102
103 }
104
105}