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

NodeDataCache initialization now works correctly

Still needs work on GetPorts, keep connections, disconnect invalid, etc.
This commit is contained in:
Thor 2017-10-11 09:03:20 +02:00
parent 963eb84edb
commit d608eaab9a
3 changed files with 25 additions and 33 deletions

Binary file not shown.

View File

@ -1,7 +1,7 @@
fileFormatVersion: 2 fileFormatVersion: 2
guid: 5398d565241ec2d489f41c368ca6cf24 guid: b21c76861277d8445a55b4691205fd00
timeCreated: 1507498811 timeCreated: 1507704049
licenseType: Free licenseType: Pro
NativeFormatImporter: NativeFormatImporter:
mainObjectFileID: 11400000 mainObjectFileID: 11400000
userData: userData:

View File

@ -7,52 +7,46 @@ using UnityEditor;
/// <summary> Precaches reflection data in editor so we won't have to do it runtime </summary> /// <summary> Precaches reflection data in editor so we won't have to do it runtime </summary>
public sealed class NodeDataCache : ScriptableObject { public sealed class NodeDataCache : ScriptableObject {
public static NodeDataCache instance { public static NodeDataCache instance { get; private set;}
get { public static bool Initialized { get { return instance != null; } }
if (_instance == null) _instance = Resources.FindObjectsOfTypeAll<NodeDataCache>().FirstOrDefault();
return _instance; [SerializeField] private PortDataCache portDataCache;
#if UNITY_EDITOR
[UnityEditor.Callbacks.DidReloadScripts]
#endif
public static void Initialize() {
if (!Initialized) {
instance = Resources.LoadAll<NodeDataCache>("").FirstOrDefault();
#if UNITY_EDITOR
instance.BuildCache();
#endif
} }
} }
public static NodeDataCache _instance;
public static bool Initialized { get { return _instance != null; } }
[SerializeField]
private PortDataCache portDataCache = new PortDataCache();
/// <summary> Return port data from cache </summary> /// <summary> Return port data from cache </summary>
public static void GetPorts(Node node, ref List<NodePort> inputs, ref List<NodePort> outputs) { public static void GetPorts(Node node, ref List<NodePort> inputs, ref List<NodePort> outputs) {
if (!Initialized) Initialize(); Initialize();
System.Type nodeType = node.GetType(); System.Type nodeType = node.GetType();
inputs = new List<NodePort>(); inputs = new List<NodePort>();
outputs = new List<NodePort>(); outputs = new List<NodePort>();
if (!_instance.portDataCache.ContainsKey(nodeType)) return; if (!instance.portDataCache.ContainsKey(nodeType)) return;
for (int i = 0; i < _instance.portDataCache[nodeType].Count; i++) { for (int i = 0; i < instance.portDataCache[nodeType].Count; i++) {
if (_instance.portDataCache[nodeType][i].direction == NodePort.IO.Input) inputs.Add(new NodePort(_instance.portDataCache[nodeType][i], node)); if (instance.portDataCache[nodeType][i].direction == NodePort.IO.Input) inputs.Add(new NodePort(instance.portDataCache[nodeType][i], node));
else outputs.Add(new NodePort(_instance.portDataCache[nodeType][i], node)); else outputs.Add(new NodePort(instance.portDataCache[nodeType][i], node));
} }
}
public static void Initialize() {
_instance = Resources.LoadAll<NodeDataCache>("").FirstOrDefault();
} }
#if UNITY_EDITOR #if UNITY_EDITOR
[UnityEditor.Callbacks.DidReloadScripts]
private static void Reload() {
Initialize();
instance.BuildCache();
}
#endif
private void BuildCache() { private void BuildCache() {
portDataCache = new PortDataCache();
System.Type baseType = typeof(Node); System.Type baseType = typeof(Node);
Assembly assembly = Assembly.GetAssembly(baseType); Assembly assembly = Assembly.GetAssembly(baseType);
System.Type[] nodeTypes = assembly.GetTypes().Where(t => System.Type[] nodeTypes = assembly.GetTypes().Where(t =>
!t.IsAbstract && !t.IsAbstract &&
baseType.IsAssignableFrom(t) baseType.IsAssignableFrom(t)
).ToArray(); ).ToArray();
portDataCache.Clear();
for (int i = 0; i < nodeTypes.Length; i++) { for (int i = 0; i < nodeTypes.Length; i++) {
CachePorts(nodeTypes[i]); CachePorts(nodeTypes[i]);
@ -60,9 +54,6 @@ public sealed class NodeDataCache : ScriptableObject {
} }
private void CachePorts(System.Type nodeType) { private void CachePorts(System.Type nodeType) {
List<NodePort> inputPorts = new List<NodePort>();
List<NodePort> outputPorts = new List<NodePort>();
System.Reflection.FieldInfo[] fieldInfo = nodeType.GetFields(); System.Reflection.FieldInfo[] fieldInfo = nodeType.GetFields();
for (int i = 0; i < fieldInfo.Length; i++) { for (int i = 0; i < fieldInfo.Length; i++) {
@ -80,6 +71,7 @@ public sealed class NodeDataCache : ScriptableObject {
} }
} }
} }
#endif
[System.Serializable] [System.Serializable]
private class PortDataCache : Dictionary<System.Type, List<NodePort>>, ISerializationCallbackReceiver { private class PortDataCache : Dictionary<System.Type, List<NodePort>>, ISerializationCallbackReceiver {