From 4a1ada67db39b814b2b79f49a1534fc273ce3eff Mon Sep 17 00:00:00 2001 From: taoria <445625470@qq.com> Date: Mon, 29 Aug 2022 22:23:48 +0800 Subject: [PATCH] feature:add a reset method for cached model for port reset --- Samples/New HelloGraph.asset | 24 +++++++++---------- Samples/Nodes/AddNode.cs | 11 +++++---- Samples/Nodes/CheckSizeNode.cs | 9 +++++-- .../RuntimeCache/IModelPortAccessor.cs | 2 ++ .../Runtime/RuntimeCache/RuntimeCache.cs | 16 +++++++++++++ .../Runtime/RuntimeModels/RuntimeNode.cs | 7 +++++- TNodeCore/Runtime/Tools/GraphTool.cs | 3 ++- 7 files changed, 52 insertions(+), 20 deletions(-) diff --git a/Samples/New HelloGraph.asset b/Samples/New HelloGraph.asset index 54974c3..27e241f 100644 --- a/Samples/New HelloGraph.asset +++ b/Samples/New HelloGraph.asset @@ -99,29 +99,29 @@ MonoBehaviour: entryPoint: 0 isTest: 0 00000003: - type: {class: AddNode, ns: Samples.Nodes, asm: Assembly-CSharp} + type: {class: CheckSizeNode, ns: Samples.Nodes, asm: Assembly-CSharp} data: positionInView: serializedVersion: 2 - x: 1121 - y: 187 + x: 917 + y: 228 width: 0 height: 0 - id: 926f2eea-3403-4663-88bd-7ed16ce029fa - nodeName: AddNode + id: f236a611-cc64-4fce-88d2-40baf7f4a490 + nodeName: CheckSizeNode entryPoint: 0 isTest: 0 00000004: - type: {class: CheckSizeNode, ns: Samples.Nodes, asm: Assembly-CSharp} + type: {class: AddNode, ns: Samples.Nodes, asm: Assembly-CSharp} data: positionInView: serializedVersion: 2 - x: 900 - y: 228 + x: 1145.4003 + y: 188.50003 width: 0 height: 0 - id: f236a611-cc64-4fce-88d2-40baf7f4a490 - nodeName: CheckSizeNode + id: 926f2eea-3403-4663-88bd-7ed16ce029fa + nodeName: AddNode entryPoint: 0 isTest: 0 00000005: @@ -137,7 +137,7 @@ MonoBehaviour: HelloString: HelloGameObject: {fileID: 0} Value: - - 11 + - 24.41 - 102.1 00000006: type: {class: GraphViewModel, ns: TNode.TNodeCore.Editor.Models, asm: Taoria.TNodeCore.Runtime} @@ -150,5 +150,5 @@ MonoBehaviour: height: 0 id: persistScale: 0.8695652 - persistOffset: {x: -76, y: 126.00001} + persistOffset: {x: -125, y: 184} isBlackboardOn: 1 diff --git a/Samples/Nodes/AddNode.cs b/Samples/Nodes/AddNode.cs index bbac497..e042987 100644 --- a/Samples/Nodes/AddNode.cs +++ b/Samples/Nodes/AddNode.cs @@ -1,15 +1,18 @@ -using TNodeCore.Runtime.Attributes; +using TNodeCore.Runtime; +using TNodeCore.Runtime.Attributes; using TNodeCore.Runtime.Attributes.Ports; using TNodeCore.Runtime.Models; namespace Samples.Nodes{ [GraphUsage(typeof(HelloGraph),"Math")] public class AddNode:NodeData{ + [Input] public float A{ get; set; } = default; [Input] - public float A{ get; set; } - [Input] - public float B{ get; set; } + public float B{ get; set; }= default; [Output] public float C => A + B; + public override void Process(){ + + } } } \ No newline at end of file diff --git a/Samples/Nodes/CheckSizeNode.cs b/Samples/Nodes/CheckSizeNode.cs index 22019e7..ad71b5a 100644 --- a/Samples/Nodes/CheckSizeNode.cs +++ b/Samples/Nodes/CheckSizeNode.cs @@ -1,6 +1,8 @@ -using TNodeCore.Runtime.Attributes; +using TNodeCore.Runtime; +using TNodeCore.Runtime.Attributes; using TNodeCore.Runtime.Attributes.Ports; using TNodeCore.Runtime.Models; +using UnityEngine; namespace Samples.Nodes{ [GraphUsage(typeof(HelloGraph),"Math")] @@ -24,6 +26,9 @@ namespace Samples.Nodes{ DataFunc = ()=>A }; } - + + public override void Process(){ + this.Log($"{A}"); + } } } \ No newline at end of file diff --git a/TNodeCore/Runtime/RuntimeCache/IModelPortAccessor.cs b/TNodeCore/Runtime/RuntimeCache/IModelPortAccessor.cs index b69ecf8..f7adc14 100644 --- a/TNodeCore/Runtime/RuntimeCache/IModelPortAccessor.cs +++ b/TNodeCore/Runtime/RuntimeCache/IModelPortAccessor.cs @@ -6,6 +6,8 @@ namespace TNodeCore.Runtime.RuntimeCache{ object GetValue(object model); void SetValue(object model, object value); + void Reset(object model); + public Type Type{ get; set; } diff --git a/TNodeCore/Runtime/RuntimeCache/RuntimeCache.cs b/TNodeCore/Runtime/RuntimeCache/RuntimeCache.cs index 33876aa..3a94817 100644 --- a/TNodeCore/Runtime/RuntimeCache/RuntimeCache.cs +++ b/TNodeCore/Runtime/RuntimeCache/RuntimeCache.cs @@ -13,6 +13,8 @@ namespace TNodeCore.Runtime.RuntimeCache{ public class PortAccessor:IModelPortAccessor{ public readonly Func Get; public readonly Action Set; + private readonly Action _resetFunc; + private readonly T2 _defaultValue; public PortAccessor(string name,bool property){ if (property){ Type t = typeof(T1); @@ -20,10 +22,18 @@ namespace TNodeCore.Runtime.RuntimeCache{ MethodInfo getter = t.GetMethod("get_" + name); MethodInfo setter = t.GetMethod("set_" + name); Type = getter?.ReturnType??setter?.GetParameters()[0].ParameterType; + if(getter!=null) Get = (Func)Delegate.CreateDelegate(typeof(Func), null, getter); if(setter!=null) Set = (Action)Delegate.CreateDelegate(typeof(Action), null, setter); + if (Set != null){ + var dummy = Activator.CreateInstance(); + if (Get != null) _defaultValue = Get(dummy); + _resetFunc = (obj) => { + Set(obj, _defaultValue); + }; + } } else{ Type t = typeof(T1); @@ -46,6 +56,12 @@ namespace TNodeCore.Runtime.RuntimeCache{ public void SetValue(object model, object value){ Set((T1)model,(T2)value); } + + public void Reset(object model){ + //Get + _resetFunc((T1)model); + } + public Type Type{ get; set; } } diff --git a/TNodeCore/Runtime/RuntimeModels/RuntimeNode.cs b/TNodeCore/Runtime/RuntimeModels/RuntimeNode.cs index 6eeddac..99930a1 100644 --- a/TNodeCore/Runtime/RuntimeModels/RuntimeNode.cs +++ b/TNodeCore/Runtime/RuntimeModels/RuntimeNode.cs @@ -121,6 +121,12 @@ namespace TNodeCore.Runtime{ _portAccessors = RuntimeCache.RuntimeCache.Instance.CachedPortAccessors[_type]; } + + public void ResetPortValue(){ + foreach (var modelPortAccessor in _portAccessors){ + modelPortAccessor.Value.Reset(this); + } + } public List GetInputNodesId(){ List dependencies = new List(); foreach (NodeLink link in InputLinks) @@ -129,7 +135,6 @@ namespace TNodeCore.Runtime{ } return dependencies; } - } public enum Direction{ Input, diff --git a/TNodeCore/Runtime/Tools/GraphTool.cs b/TNodeCore/Runtime/Tools/GraphTool.cs index b0a3269..8e9a293 100644 --- a/TNodeCore/Runtime/Tools/GraphTool.cs +++ b/TNodeCore/Runtime/Tools/GraphTool.cs @@ -297,7 +297,8 @@ namespace TNode.TNodeCore.Runtime.Tools{ //TODO looks like this string would be too long to make a cache var cachedKey = $"{outNode.NodeData.id}-{nodeLink.inPort.portEntryName}"; - var outValue = OutputCached.ContainsKey(cachedKey) ? OutputCached[cachedKey] : outNode.GetOutput(nodeLink.outPort.portEntryName);; + var outValue = OutputCached.ContainsKey(cachedKey) ? OutputCached[cachedKey] : outNode.GetOutput(nodeLink.outPort.portEntryName); + Debug.Log(outValue); if (_isCachingOutput){ OutputCached[cachedKey] = outValue; }