Tanoda
Scripts/BepinRUE/Utils/Extensions.cs
Go to the documentation of this file.
1using System;
2using System.Collections;
3using System.Collections.Generic;
4using System.Linq;
5using System.Reflection;
7using UnityEngine;
8
10{
11 public static class Extensions
12 {
13 public static bool Contains(this string s, string searchText, StringComparison sc)
14 {
15 return s.IndexOf(searchText, sc) >= 0;
16 }
17
18 public static T Next<T>(this T src) where T : struct
19 {
20 if (!typeof(T).IsEnum) throw new ArgumentException(string.Format("Argumnent {0} is not an Enum",
21 typeof(T).FullName));
22
23 var arr = (T[])Enum.GetValues(src.GetType());
24 var j = Array.IndexOf(arr, src) + 1;
25 return (arr.Length == j) ? arr[0] : arr[j];
26 }
27
28 public static object GetPrivateExplicit<T>(this T self, string name)
29 {
30 return typeof(T).GetField(name, BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public | BindingFlags.FlattenHierarchy).GetValue(self);
31 }
32 public static object GetPrivate(this object self, string name)
33 {
34 return self.GetType().GetField(name, BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public | BindingFlags.FlattenHierarchy).GetValue(self);
35 }
36 public static void SetPrivateExplicit<T>(this T self, string name, object value)
37 {
38 typeof(T).GetField(name, BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public | BindingFlags.FlattenHierarchy).SetValue(self, value);
39 }
40 public static void SetPrivate(this object self, string name, object value)
41 {
42 self.GetType().GetField(name, BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public | BindingFlags.FlattenHierarchy).SetValue(self, value);
43 }
44 public static object CallPrivateExplicit<T>(this T self, string name, params object[] p)
45 {
46 return typeof(T).GetMethod(name, BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public | BindingFlags.FlattenHierarchy)?.Invoke(self, p);
47 }
48 public static object CallPrivate(this object self, string name, params object[] p)
49 {
50 return self.GetType().GetMethod(name, BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public | BindingFlags.FlattenHierarchy)?.Invoke(self, p);
51 }
52
53 public static void ExecuteDelayed(this MonoBehaviour self, Action action, int waitCount = 1)
54 {
55 self.StartCoroutine(ExecuteDelayed_Routine(action, waitCount));
56 }
57
58 private static IEnumerator ExecuteDelayed_Routine(Action action, int waitCount)
59 {
60 for (var i = 0; i < waitCount; ++i)
61 yield return null;
62 action();
63 }
64
65 public static string GetPathFrom(this Transform self, Transform root)
66 {
67 var self2 = self;
68 var path = self2.name;
69 self2 = self2.parent;
70 while (self2 != root)
71 {
72 path = self2.name + "/" + path;
73 self2 = self2.parent;
74 }
75 return path;
76 }
77
78 public static Transform FindDescendant(this Transform self, string name)
79 {
80 if (self.name.Equals(name))
81 return self;
82 foreach (Transform t in self)
83 {
84 var res = t.FindDescendant(name);
85 if (res != null)
86 return res;
87 }
88 return null;
89 }
90
91 public static MemberInfo GetMemberInfo(this ICacheEntry centry, bool throwOnError)
92 {
93 if (centry == null)
94 {
95 if (!throwOnError) return null;
96 throw new ArgumentNullException();
97 }
98
99 var m = centry as MethodCacheEntry;
100 var p = centry as PropertyCacheEntry;
101 var f = centry as FieldCacheEntry;
102
103 if (m != null) return m.MethodInfo;
104 if (p != null) return p.PropertyInfo;
105 if (f != null) return f.FieldInfo;
106
107 if (throwOnError)
108 throw new Exception("Cannot open items of type " + centry.GetType().FullName);
109 return null;
110 }
111
112 public static IEnumerable<Type> GetTypesSafe(this Assembly ass)
113 {
114 try { return ass.GetTypes(); }
115 catch (ReflectionTypeLoadException e) { return e.Types.Where(x => x != null); }
116 catch { return Enumerable.Empty<Type>(); }
117 }
118
119 public static string GetFullTransfromPath(this Transform target)
120 {
121 var name = target.name;
122 var parent = target.parent;
123 while (parent != null)
124 {
125 name = string.Format("{0}/{1}", parent.name, name);
126 parent = parent.parent;
127 }
128 return name;
129 }
130
131 internal static string IsNullOrDestroyed(this object value)
132 {
133 if (ReferenceEquals(value, null)) return "NULL";
134 var uobj = value as UnityEngine.Object;
135 if (uobj)
136 {
137 // This is necessary because the is operator ignores the == override that makes Objects look like null
138 if (uobj.Equals(null)) return "NULL (Destroyed)";
139 }
140
141 return null;
142 }
143 }
144}
FieldInfo FieldInfo
Definition: ICacheEntry.cs:6