2using System.Collections.Generic;
4using System.Reflection;
5using System.Diagnostics;
6using System.Runtime.CompilerServices;
10using Microsoft.Xna.Framework;
11using Microsoft.Xna.Framework.Input;
12using Microsoft.Xna.Framework.Graphics;
20 public class TypeTreeNode
23 public TypeTreeNode Parent;
24 public List<TypeTreeNode> Children =
new();
25 public CUITypeMetaData Meta => CUITypeMetaData.Get(T);
26 public void Add(TypeTreeNode child)
31 public TypeTreeNode(Type t) => T = t;
32 public override string ToString() => T?.ToString() ??
"null";
33 public void RunRecursive(Action<TypeTreeNode> action)
36 foreach (TypeTreeNode child
in Children)
38 child.RunRecursive(action);
44 public class CUIReflection
46 internal static void InitStatic()
50 Stopwatch sw = Stopwatch.StartNew();
53 CUIDebug.Log($
"CUIReflection.Initialize took {sw.ElapsedMilliseconds}ms");
55 CUI.OnDispose += () =>
58 CUILayoutTypes.Clear();
65 public record TypePair(Type type, Type baseType);
67 public static Dictionary<Type, TypeTreeNode> CUITypeTree =
new();
69 public static Dictionary<string, Type> CUILayoutTypes =
new();
70 public static Dictionary<string, Type> CUITypes =
new Dictionary<string, Type>();
72 public static void FormCUITypeTree()
74 List<TypePair> Pustoe = CUITypes.Values.Select(t =>
new TypePair(t, t.BaseType)).ToList();
75 List<TypePair> Porojnee =
new List<TypePair>();
77 while (Pustoe.Count > 0)
79 Porojnee =
new List<TypePair>();
80 foreach (TypePair pair
in Pustoe)
83 if (pair.baseType == typeof(
object))
85 CUITypeTree[pair.type] =
new TypeTreeNode(pair.type);
90 if (CUITypeTree.ContainsKey(pair.baseType))
92 CUITypeTree[pair.type] =
new TypeTreeNode(pair.type);
93 CUITypeTree[pair.baseType].Add(CUITypeTree[pair.type]);
108 public static void FindCUITypes()
110 Assembly CUIAssembly = Assembly.GetAssembly(typeof(CUI));
111 Assembly CallingAssembly = Assembly.GetCallingAssembly();
113 CUITypes[
"CUIComponent"] = typeof(CUIComponent);
114 CUILayoutTypes[
"CUILayout"] = typeof(CUILayout);
122 foreach (Type t
in CallingAssembly.GetTypes())
124 if (t.IsSubclassOf(typeof(CUIComponent))) CUITypes[t.Name] = t;
125 if (t.IsSubclassOf(typeof(CUILayout))) CUILayoutTypes[t.Name] = t;
128 foreach (Type t
in CUIAssembly.GetTypes())
130 if (t.IsSubclassOf(typeof(CUIComponent))) CUITypes[t.Name] = t;
131 if (t.IsSubclassOf(typeof(CUILayout))) CUILayoutTypes[t.Name] = t;
135 public static Type GetComponentTypeByName(
string name)
137 return CUITypes.GetValueOrDefault(name);
140 public static object GetDefault(
object obj)
142 FieldInfo defField = obj.GetType().GetField(
"Default", BindingFlags.Static | BindingFlags.Public);
143 if (defField ==
null)
return null;
144 return defField.GetValue(
null);
147 public static object GetNestedValue(
object obj,
string nestedName)
149 string[] names = nestedName.Split(
'.');
151 foreach (
string name
in names)
153 FieldInfo fi = obj.GetType().GetField(name, AccessTools.all);
154 PropertyInfo pi = obj.GetType().GetProperty(name, AccessTools.all);
158 obj = fi.GetValue(obj);
164 obj = pi.GetValue(obj);