From 8ce0d63903c16a0b4904c0473585f4973f120b2e Mon Sep 17 00:00:00 2001 From: Thor Brigsted Date: Thu, 12 Oct 2017 00:18:40 +0200 Subject: [PATCH] NodeDataCache changed from ScriptableObject to static class. Nodes still don't load properly, but we're getting there. --- Example/ExampleNodeGraph.asset | Bin 5424 -> 1617 bytes Example/ExampleNodeGraph.asset.meta | 6 +- Resources.meta | 9 --- Resources/New Node Data Cache.asset | Bin 4184 -> 0 bytes Resources/New Node Data Cache.asset.meta | 9 --- Scripts/Editor/NodeEditorUtilities.cs | 1 + Scripts/Node.cs | 2 +- Scripts/NodeDataCache.cs | 75 +++++++++++++---------- Scripts/NodeGraph.cs | 1 - Scripts/NodePort.cs | 6 +- 10 files changed, 51 insertions(+), 58 deletions(-) delete mode 100644 Resources.meta delete mode 100644 Resources/New Node Data Cache.asset delete mode 100644 Resources/New Node Data Cache.asset.meta diff --git a/Example/ExampleNodeGraph.asset b/Example/ExampleNodeGraph.asset index 2871ba7b5313da89a7d7aabe05d1ed1d8c8b822b..f2238520e17d218064f6942207a10fa8c192f0bb 100644 GIT binary patch literal 1617 zcmds%%Wm5+5JmU?ipe5fSpi8=)MJ-6u4|x412hOw6oo;VBb$XR2^5vaE%NUrr4&)@ z6!AJ*=*1!DlK0GPn~{PKrEJ@Ua+}tL zMw5XXt6W^`4=Z>&0e7NQq=cj_O(+j(!hM400g*Ez;Yr8@VMHVk`B3el>zGq z?j2yECmLXvf#_d;X=OJqz}18Qzd^m;IL3x=T{WG3bbIL#DAmBs-J>3WRb5psu}jj2 zdDlH5&miRo-up}dqXiANU1@s-OQro*df8r6-Pv9QeE;FT`pmv8_bPfX#oNP&F(eT( zNsCCTSnw!`h>94a%vGFV>5KSWO8Z(pABRmq;{g}PO?o^ygJ=I7boK=>#%SdFQ5@~{ h2;XG(IC=tejFIoEa_Hsx@E9@=?^}0BH+^3ulfU5n*G2#U literal 5424 zcmeI0Uu+ab9LIm#UVE)5v=%CYApK(j!D^*NtWf(Ou(cFo2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set.meta index 04eb901..51a01a0 100644 --- a/Example/ExampleNodeGraph.asset.meta +++ b/Example/ExampleNodeGraph.asset.meta @@ -1,7 +1,7 @@ fileFormatVersion: 2 -guid: b21c76861277d8445a55b4691205fd00 -timeCreated: 1507704049 -licenseType: Pro +guid: 2c7ed5ea36166484a8bcecda71ad8cc8 +timeCreated: 1507760054 +licenseType: Free NativeFormatImporter: mainObjectFileID: 11400000 userData: diff --git a/Resources.meta b/Resources.meta deleted file mode 100644 index f101f36..0000000 --- a/Resources.meta +++ /dev/null @@ -1,9 +0,0 @@ -fileFormatVersion: 2 -guid: 65ed9b337b7f0b74ab007799bb407a5e -folderAsset: yes -timeCreated: 1507567488 -licenseType: Free -DefaultImporter: - userData: - assetBundleName: - assetBundleVariant: diff --git a/Resources/New Node Data Cache.asset b/Resources/New Node Data Cache.asset deleted file mode 100644 index 98c7f5f59a4d35c557ef7fbc609f1d162e555f8d..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 4184 zcmeH~PiqrF7{;H;*49>2wQ98pHmA~4AXF=&NYtdX6hge|K_%GLL~5gP@y~59?M=`x zAfAFCR`BEpXg@#?h0=re;Hh{JJa~%nd3HC;sCz|<(mTiR1@cg!xY86 zNdFwysGc$LsCl@>+{bL1pGKi63H7fKvdYeAiZYO@uTYC-L0?Lpng)4rL` zQT+2cQ3UkkE8_D4d#}HwbIh%`C(qx>-gwj5+kH27@0or;7tR6afOEh(;2dxcI0u{q l&H?9ubKrnGp!bnNeeaE=_h7|;nX5FH{G1+Kx%kDUy#I7mhHd}= diff --git a/Resources/New Node Data Cache.asset.meta b/Resources/New Node Data Cache.asset.meta deleted file mode 100644 index 830cf51..0000000 --- a/Resources/New Node Data Cache.asset.meta +++ /dev/null @@ -1,9 +0,0 @@ -fileFormatVersion: 2 -guid: a05ee176e92214f48a94c321dad1614d -timeCreated: 1507703437 -licenseType: Pro -NativeFormatImporter: - mainObjectFileID: 11400000 - userData: - assetBundleName: - assetBundleVariant: diff --git a/Scripts/Editor/NodeEditorUtilities.cs b/Scripts/Editor/NodeEditorUtilities.cs index 9d683bd..78ee295 100644 --- a/Scripts/Editor/NodeEditorUtilities.cs +++ b/Scripts/Editor/NodeEditorUtilities.cs @@ -41,6 +41,7 @@ public static class NodeEditorUtilities { /// Return color based on type public static Color GetTypeColor(Type type) { + if (type == null) return Color.gray; UnityEngine.Random.InitState(type.Name.GetHashCode()); return new Color(UnityEngine.Random.value, UnityEngine.Random.value, UnityEngine.Random.value); } diff --git a/Scripts/Node.cs b/Scripts/Node.cs index 2f5dbf0..7759d6e 100644 --- a/Scripts/Node.cs +++ b/Scripts/Node.cs @@ -112,6 +112,6 @@ public abstract class Node : ScriptableObject { } private void GetPorts() { - NodeDataCache.GetPorts(this, ref inputs, ref outputs); + NodeDataCache.UpdatePorts(this, inputs, outputs); } } diff --git a/Scripts/NodeDataCache.cs b/Scripts/NodeDataCache.cs index 681eaa2..f14fb9f 100644 --- a/Scripts/NodeDataCache.cs +++ b/Scripts/NodeDataCache.cs @@ -6,40 +6,54 @@ using System.Linq; using UnityEditor; /// Precaches reflection data in editor so we won't have to do it runtime -public sealed class NodeDataCache : ScriptableObject { - public static NodeDataCache instance { get; private set;} - public static bool Initialized { get { return instance != null; } } +public static class NodeDataCache { + private static PortDataCache portDataCache; + private static bool Initialized { get { return portDataCache != null; } } - [SerializeField] private PortDataCache portDataCache; + /// Checks for invalid and removes them. + /// Checks for missing ports and adds them. + /// Checks for invalid connections and removes them. + public static void UpdatePorts(Node node, List inputs, List outputs) { + if (!Initialized) BuildCache(); -#if UNITY_EDITOR - [UnityEditor.Callbacks.DidReloadScripts] -#endif - public static void Initialize() { - if (!Initialized) { - instance = Resources.LoadAll("").FirstOrDefault(); -#if UNITY_EDITOR - instance.BuildCache(); -#endif + List inputPorts = new List(); + List outputPorts = new List(); + + System.Type nodeType = node.GetType(); + inputPorts = new List(); + outputPorts = new List(); + if (!portDataCache.ContainsKey(nodeType)) return; + for (int i = 0; i < portDataCache[nodeType].Count; i++) { + if (portDataCache[nodeType][i].direction == NodePort.IO.Input) inputPorts.Add(new NodePort(portDataCache[nodeType][i], node)); + else outputPorts.Add(new NodePort(portDataCache[nodeType][i], node)); + } + + for (int i = inputs.Count-1; i >= 0; i--) { + int index = inputPorts.FindIndex(x => inputs[i].fieldName == x.fieldName); + //If input nodeport does not exist, remove it + if (index == -1) inputs.RemoveAt(i); + //If input nodeport does exist, update it + else inputs[i].type = inputPorts[index].type; + } + for (int i = outputs.Count - 1; i >= 0; i--) { + int index = outputPorts.FindIndex(x => outputs[i].fieldName == x.fieldName); + //If output nodeport does not exist, remove it + if (index == -1) outputs.RemoveAt(i); + //If output nodeport does exist, update it + else outputs[i].type = outputPorts[index].type; + } + //Add + for (int i = 0; i < inputPorts.Count; i++) { + //If inputports contains a new port, add it + if (!inputs.Any(x => x.fieldName == inputPorts[i].fieldName)) inputs.Add(inputPorts[i]); + } + for (int i = 0; i < outputPorts.Count; i++) { + //If inputports contains a new port, add it + if (!outputs.Any(x => x.fieldName == outputPorts[i].fieldName)) outputs.Add(outputPorts[i]); } } - /// Return port data from cache - public static void GetPorts(Node node, ref List inputs, ref List outputs) { - Initialize(); - - System.Type nodeType = node.GetType(); - inputs = new List(); - outputs = new List(); - if (!instance.portDataCache.ContainsKey(nodeType)) return; - 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)); - else outputs.Add(new NodePort(instance.portDataCache[nodeType][i], node)); - } - } - -#if UNITY_EDITOR - private void BuildCache() { + private static void BuildCache() { portDataCache = new PortDataCache(); System.Type baseType = typeof(Node); Assembly assembly = Assembly.GetAssembly(baseType); @@ -53,7 +67,7 @@ public sealed class NodeDataCache : ScriptableObject { } } - private void CachePorts(System.Type nodeType) { + private static void CachePorts(System.Type nodeType) { System.Reflection.FieldInfo[] fieldInfo = nodeType.GetFields(); for (int i = 0; i < fieldInfo.Length; i++) { @@ -71,7 +85,6 @@ public sealed class NodeDataCache : ScriptableObject { } } } -#endif [System.Serializable] private class PortDataCache : Dictionary>, ISerializationCallbackReceiver { diff --git a/Scripts/NodeGraph.cs b/Scripts/NodeGraph.cs index 878a6f0..7c00faf 100644 --- a/Scripts/NodeGraph.cs +++ b/Scripts/NodeGraph.cs @@ -15,7 +15,6 @@ public abstract class NodeGraph : ScriptableObject, ISerializationCallbackReceiv } public virtual Node AddNode(Type type) { - if (!NodeDataCache.Initialized) NodeDataCache.Initialize(); Node node = ScriptableObject.CreateInstance(type) as Node; #if UNITY_EDITOR if (!Application.isPlaying) { diff --git a/Scripts/NodePort.cs b/Scripts/NodePort.cs index 54e55d9..ad51779 100644 --- a/Scripts/NodePort.cs +++ b/Scripts/NodePort.cs @@ -19,14 +19,12 @@ public class NodePort { public bool IsOutput { get { return direction == IO.Output; } } public Node node { get; private set; } - public bool enabled { get { return _enabled; } set { _enabled = value; } } public string fieldName { get { return _fieldName; } } - [SerializeField] private List connections = new List(); [SerializeField] private string _fieldName; [SerializeField] public Type type; - [SerializeField] private bool _enabled = true; + [SerializeField] private List connections = new List(); [SerializeField] private IO _direction; public NodePort(FieldInfo fieldInfo) { @@ -59,7 +57,7 @@ public class NodePort { } } - public object GetValue() { + public object GetValue() { return node.GetValue(this); }