Tanoda
LeapProjectChecks.cs
Go to the documentation of this file.
1/******************************************************************************
2 * Copyright (C) Ultraleap, Inc. 2011-2020. *
3 * *
4 * Use subject to the terms of the Apache License 2.0 available at *
5 * http://www.apache.org/licenses/LICENSE-2.0, or another agreement *
6 * between Ultraleap and you, your company or other organization. *
7 ******************************************************************************/
8
9using Leap.Unity.Query;
10using System;
11using System.Collections.Generic;
12using System.Reflection;
13using UnityEngine;
14
15#if UNITY_EDITOR
16using UnityEditor;
17#endif
18
19namespace Leap.Unity {
20
35 [AttributeUsage(AttributeTargets.Method, AllowMultiple = false)]
36 public class LeapProjectCheckAttribute : Attribute {
37 public string header;
38 public int order;
39
41 this.header = header;
42 this.order = order;
43 }
44 }
45
50 public static class LeapProjectChecks {
51
52 private struct ProjectCheck {
53 public Func<bool> checkFunc;
54 public LeapProjectCheckAttribute attribute;
55 }
56
57 private static List<ProjectCheck> _projectChecks = null;
58
59 private static void ensureChecksLoaded() {
60 if (_projectChecks != null) {
61 return;
62 }
63
64 _projectChecks = new List<ProjectCheck>();
65
66 var assemblies = AppDomain.CurrentDomain.GetAssemblies();
67 foreach (var type in assemblies.Query().SelectMany(a => a.GetTypes())) {
68 foreach (var method in type.GetMethods(BindingFlags.Public
69 | BindingFlags.NonPublic
70 | BindingFlags.Static)) {
71 var attributes = method.GetCustomAttributes(typeof(LeapProjectCheckAttribute),
72 inherit: true);
73 if (attributes.Length == 0) {
74 continue;
75 }
76
77 var attribute = attributes[0] as LeapProjectCheckAttribute;
78 _projectChecks.Add(new ProjectCheck() {
79 checkFunc = () => {
80 if (!method.IsStatic) {
81 Debug.LogError("Invalid project check definition; project checks must "
82 + "be static methods.");
83 return true;
84 }
85 else if (method.ReturnType == typeof(bool)) {
86 return (bool)method.Invoke(null, null);
87 }
88 else {
89 return true;
90 }
91 },
92 attribute = attribute
93 });
94 }
95 }
96
97 _projectChecks.Sort((a, b) => a.attribute.order.CompareTo(b.attribute.order));
98 }
99
104 public static void DrawProjectChecksGUI() {
105 #if UNITY_EDITOR
106 ensureChecksLoaded();
107
108 bool allChecksPassed = true;
109 foreach (var projectCheck in _projectChecks) {
110
111 using (new EditorGUILayout.VerticalScope(EditorStyles.helpBox)) {
112 allChecksPassed &= projectCheck.checkFunc();
113 }
114
115 }
116
117 if (_ignoredKeys != null && _ignoredKeys.Count > 0) {
118 EditorGUILayout.Space();
119 using (new EditorGUILayout.HorizontalScope()) {
120 GUILayout.FlexibleSpace();
121
122 using (new EditorGUILayout.VerticalScope()) {
123 GUILayout.Space(4f);
124 GUILayout.Label("Some project checks have been ignored.");
125 }
126
127 if (GUILayout.Button(new GUIContent("Reset Ignore Flags",
128 "Un-ignore any project checks that have been ignored."))) {
129 ClearAllIgnoredKeys();
130 }
131
132 EditorGUILayout.Space();
133 }
134 }
135 #endif
136 }
137
138 #region Ignored Keys via Editor Prefs
139
140 private const string IGNORED_KEYS_PREF = "LeapUnityWindow_IgnoredKeys";
141
142 #if UNITY_EDITOR
143 private static HashSet<string> _backingIgnoredKeys = null;
144 #endif
146 private static HashSet<string> _ignoredKeys {
147 get {
148 #if UNITY_EDITOR
149 if (_backingIgnoredKeys == null) {
150 _backingIgnoredKeys
151 = splitBySemicolonToSet(EditorPrefs.GetString(IGNORED_KEYS_PREF));
152 }
153 return _backingIgnoredKeys;
154 #else
155 return null;
156 #endif
157 }
158 }
159
160 public static bool CheckIgnoredKey(string editorPrefKey) {
161 #if UNITY_EDITOR
162 return _ignoredKeys.Contains(editorPrefKey);
163 #else
164 return false;
165 #endif
166 }
167
168 public static void SetIgnoredKey(string editorPrefKey, bool ignore) {
169 #if UNITY_EDITOR
170 if (ignore) {
171 _ignoredKeys.Add(editorPrefKey);
172 }
173 else {
174 _ignoredKeys.Remove(editorPrefKey);
175 }
176
177 uploadignoredKeyChangesToEditorPrefs();
178 #endif
179 }
180
181 public static void ClearAllIgnoredKeys() {
182 #if UNITY_EDITOR
183 _ignoredKeys.Clear();
184
185 uploadignoredKeyChangesToEditorPrefs();
186 #endif
187 }
188
193 private static HashSet<string> splitBySemicolonToSet(string ignoredKeys_semicolonDelimited) {
194 var keys = ignoredKeys_semicolonDelimited;
195 var set = new HashSet<string>();
196 foreach (var key in keys.Split(new char[] { ';' },
197 StringSplitOptions.RemoveEmptyEntries)) {
198 set.Add(key);
199 }
200 return set;
201 }
202
203 private static string joinBySemicolon(HashSet<string> keys) {
204 return string.Join(";", keys.Query().ToArray());
205 }
206
207 private static void uploadignoredKeyChangesToEditorPrefs() {
208 #if UNITY_EDITOR
209 EditorPrefs.SetString(IGNORED_KEYS_PREF, joinBySemicolon(_ignoredKeys));
210 #endif
211 }
212
213#endregion
214
215 }
216
217}
UnityEngine.Debug Debug
Definition: TanodaServer.cs:19
Add this attribute to a settings check. This method will be called often while the Leap Motion Unity ...
LeapProjectCheckAttribute(string header, int order)