1
0
mirror of https://github.com/Siccity/xNode.git synced 2025-12-20 17:26:02 +08:00

Faster editor caching and less error prone

This commit is contained in:
Thor Brigsted 2018-06-30 16:16:11 +02:00
parent fb61577713
commit 2ee0e42668

View File

@ -9,7 +9,7 @@ namespace XNodeEditor.Internal {
/// <summary> Handles caching of custom editor classes and their target types. Accessible with GetEditor(Type type) </summary> /// <summary> Handles caching of custom editor classes and their target types. Accessible with GetEditor(Type type) </summary>
public class NodeEditorBase<T, A, K> where A : Attribute, NodeEditorBase<T, A, K>.INodeEditorAttrib where T : NodeEditorBase<T, A, K> where K : ScriptableObject { public class NodeEditorBase<T, A, K> where A : Attribute, NodeEditorBase<T, A, K>.INodeEditorAttrib where T : NodeEditorBase<T, A, K> where K : ScriptableObject {
/// <summary> Custom editors defined with [CustomNodeEditor] </summary> /// <summary> Custom editors defined with [CustomNodeEditor] </summary>
private static Dictionary<Type, T> editorTemplates; private static Dictionary<Type, Type> editorTypes;
private static Dictionary<K, T> editors = new Dictionary<K, T>(); private static Dictionary<K, T> editors = new Dictionary<K, T>();
public K target; public K target;
public SerializedObject serializedObject; public SerializedObject serializedObject;
@ -18,33 +18,36 @@ namespace XNodeEditor.Internal {
if (target == null) return null; if (target == null) return null;
if (!editors.ContainsKey(target)) { if (!editors.ContainsKey(target)) {
Type type = target.GetType(); Type type = target.GetType();
T editor = GetEditor(type); Type editorType = GetEditorType(type);
editors.Add(target, Activator.CreateInstance(editor.GetType()) as T); editors.Add(target, Activator.CreateInstance(editorType) as T);
editors[target].target = target; editors[target].target = target;
editors[target].serializedObject = new SerializedObject(target); editors[target].serializedObject = new SerializedObject(target);
} }
return editors[target]; T editor = editors[target];
if (editor.target == null) editor.target = target;
if (editor.serializedObject == null) editor.serializedObject = new SerializedObject(target);
return editor;
} }
private static T GetEditor(Type type) { private static Type GetEditorType(Type type) {
if (type == null) return null; if (type == null) return null;
if (editorTemplates == null) CacheCustomEditors(); if (editorTypes == null) CacheCustomEditors();
if (editorTemplates.ContainsKey(type)) return editorTemplates[type]; if (editorTypes.ContainsKey(type)) return editorTypes[type];
//If type isn't found, try base type //If type isn't found, try base type
return GetEditor(type.BaseType); return GetEditorType(type.BaseType);
} }
private static void CacheCustomEditors() { private static void CacheCustomEditors() {
editorTemplates = new Dictionary<Type, T>(); editorTypes = new Dictionary<Type, Type>();
//Get all classes deriving from NodeEditor via reflection //Get all classes deriving from NodeEditor via reflection
Type[] nodeEditors = XNodeEditor.NodeEditorWindow.GetDerivedTypes(typeof(T)); Type[] nodeEditors = XNodeEditor.NodeEditorWindow.GetDerivedTypes(typeof(T));
for (int i = 0; i < nodeEditors.Length; i++) { for (int i = 0; i < nodeEditors.Length; i++) {
if (nodeEditors[i].IsAbstract) continue;
var attribs = nodeEditors[i].GetCustomAttributes(typeof(A), false); var attribs = nodeEditors[i].GetCustomAttributes(typeof(A), false);
if (attribs == null || attribs.Length == 0) continue; if (attribs == null || attribs.Length == 0) continue;
if (nodeEditors[i].IsAbstract) continue;
A attrib = attribs[0] as A; A attrib = attribs[0] as A;
editorTemplates.Add(attrib.GetInspectedType(), Activator.CreateInstance(nodeEditors[i]) as T); editorTypes.Add(attrib.GetInspectedType(), nodeEditors[i]);
} }
} }