From 5e4c012a5ecf82378d936f9b983f5909ec963252 Mon Sep 17 00:00:00 2001 From: taoria <445625470@qq.com> Date: Tue, 16 Aug 2022 10:32:52 +0800 Subject: [PATCH] fix:fix port conversion bug --- Samples/HandWriterConverter.cs | 12 +++ Samples/HandWriterConverter.cs.meta | 3 + Samples/New HelloGraph.asset | 102 ++++++++++++------ .../Runtime/Models/BlackboardDragNode.cs | 26 ++++- .../Runtime/RuntimeCache/RuntimeCache.cs | 14 ++- 5 files changed, 118 insertions(+), 39 deletions(-) create mode 100644 Samples/HandWriterConverter.cs create mode 100644 Samples/HandWriterConverter.cs.meta diff --git a/Samples/HandWriterConverter.cs b/Samples/HandWriterConverter.cs new file mode 100644 index 0000000..c8be7ff --- /dev/null +++ b/Samples/HandWriterConverter.cs @@ -0,0 +1,12 @@ +using TNodeCore.Runtime.Attributes; +using TNodeCore.Runtime.Interfaces; +using UnityEngine; + +namespace Samples{ + [GraphUsage(typeof(HelloGraph))] + public class HandWriterConverter:PortTypeConversion{ + public override Vector3 Convert(GameObject tFrom){ + return tFrom.gameObject.transform.position; + } + } +} \ No newline at end of file diff --git a/Samples/HandWriterConverter.cs.meta b/Samples/HandWriterConverter.cs.meta new file mode 100644 index 0000000..0adc9b2 --- /dev/null +++ b/Samples/HandWriterConverter.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 052c65cd0a43406ca7bb9dbb3cf13542 +timeCreated: 1660616016 \ No newline at end of file diff --git a/Samples/New HelloGraph.asset b/Samples/New HelloGraph.asset index 7c6e75d..304ba7e 100644 --- a/Samples/New HelloGraph.asset +++ b/Samples/New HelloGraph.asset @@ -17,92 +17,117 @@ MonoBehaviour: - id: 1 - id: 2 - id: 3 + - id: 4 nodeLinks: - inPort: portEntryName: A - nodeDataId: 1cf6c28a-a878-4ca6-9170-423975702f0f + nodeDataId: f33d73da-40c5-4b3a-ace7-40105e3489b6 outPort: portEntryName: Value - nodeDataId: a8a42372-3106-4028-ba4a-b1e92df29396 + nodeDataId: ae519ad8-4bc8-46c5-a98a-dc95bc85b58a - inPort: - portEntryName: B - nodeDataId: 1cf6c28a-a878-4ca6-9170-423975702f0f + portEntryName: A + nodeDataId: f33d73da-40c5-4b3a-ace7-40105e3489b6 outPort: portEntryName: Value - nodeDataId: e211ba2b-6ea0-46e8-a319-aa05602eaf46 + nodeDataId: 23526615-f15c-4bdf-96c2-38a57d30b10d - inPort: portEntryName: A - nodeDataId: 1cf6c28a-a878-4ca6-9170-423975702f0f + nodeDataId: f33d73da-40c5-4b3a-ace7-40105e3489b6 outPort: portEntryName: Value - nodeDataId: e211ba2b-6ea0-46e8-a319-aa05602eaf46 + nodeDataId: 06f5acb9-58d5-41b3-b7f5-bd90f7f9ab94 blackboardData: - id: 4 + id: 5 sceneReference: - editorModels: [] + editorModels: + - id: 6 graphViewModel: - id: 5 + id: 7 references: version: 1 00000000: - type: {class: AddNode, ns: Samples, asm: Assembly-CSharp} + type: {class: BlackboardDragNode, ns: TNodeCore.Runtime.Models, asm: TNodeCore} data: positionInView: serializedVersion: 2 - x: 292 - y: -32 + x: -345 + y: 21 width: 0 height: 0 - id: 1cf6c28a-a878-4ca6-9170-423975702f0f - nodeName: AddNode + id: 23526615-f15c-4bdf-96c2-38a57d30b10d + nodeName: entryPoint: 0 isTest: 0 + blackboardDragTypeString: UnityEngine.GameObject, UnityEngine.CoreModule, + Version=0.0.0.0, Culture=neutral, PublicKeyToken=null + blackDragData: HelloGameObject + isListElement: 0 00000001: - type: {class: BlackboardDragNode, ns: TNodeCore.Runtime.Models, asm: TNodeCore} + type: {class: AddNode, ns: Samples, asm: Assembly-CSharp} data: positionInView: serializedVersion: 2 - x: 29 - y: -10 + x: 0 + y: 0 width: 0 height: 0 - id: a8a42372-3106-4028-ba4a-b1e92df29396 - nodeName: + id: f33d73da-40c5-4b3a-ace7-40105e3489b6 + nodeName: AddNode entryPoint: 0 isTest: 0 - blackDragData: V3S.0 - isListElement: 1 00000002: type: {class: BlackboardDragNode, ns: TNodeCore.Runtime.Models, asm: TNodeCore} data: positionInView: serializedVersion: 2 - x: 38 - y: 70 + x: -345 + y: 60 width: 0 height: 0 - id: e211ba2b-6ea0-46e8-a319-aa05602eaf46 + id: ae519ad8-4bc8-46c5-a98a-dc95bc85b58a nodeName: entryPoint: 0 isTest: 0 - blackDragData: V2S.0 - isListElement: 1 + blackboardDragTypeString: UnityEngine.GameObject, UnityEngine.CoreModule, + Version=0.0.0.0, Culture=neutral, PublicKeyToken=null + blackDragData: HelloGameObject + isListElement: 0 00000003: type: {class: BlackboardDragNode, ns: TNodeCore.Runtime.Models, asm: TNodeCore} data: positionInView: serializedVersion: 2 - x: -10 - y: -95 + x: -345 + y: -21 width: 0 height: 0 - id: 33d100b8-692c-445f-99e0-329727670e93 + id: 06f5acb9-58d5-41b3-b7f5-bd90f7f9ab94 nodeName: entryPoint: 0 isTest: 0 + blackboardDragTypeString: UnityEngine.GameObject, UnityEngine.CoreModule, + Version=0.0.0.0, Culture=neutral, PublicKeyToken=null blackDragData: HelloGameObject isListElement: 0 00000004: + type: {class: BlackboardDragNode, ns: TNodeCore.Runtime.Models, asm: TNodeCore} + data: + positionInView: + serializedVersion: 2 + x: 0 + y: 0 + width: 0 + height: 0 + id: a705abd9-5c22-4410-812f-d824ddfa60d8 + nodeName: + entryPoint: 0 + isTest: 0 + blackboardDragTypeString: System.String, mscorlib, Version=4.0.0.0, Culture=neutral, + PublicKeyToken=b77a5c561934e089 + blackDragData: HelloString + isListElement: 0 + 00000005: type: {class: HelloBlackboard, ns: TNode.Samples, asm: Assembly-CSharp} data: positionInView: @@ -118,7 +143,20 @@ MonoBehaviour: - {x: 0, y: 0, z: 0} V2S: - {x: 0, y: 0} - 00000005: + 00000006: + type: {class: PlacematModel, ns: TNode.TNodeCore.Editor.Models, asm: TNodeCore} + data: + positionInView: + serializedVersion: 2 + x: 465 + y: -21 + width: 500 + height: 500 + id: + hostModels: [] + zOrder: 0 + title: Title + 00000007: type: {class: GraphViewModel, ns: TNode.TNodeCore.Editor.Models, asm: TNodeCore} data: positionInView: @@ -129,5 +167,5 @@ MonoBehaviour: height: 0 id: persistScale: 1 - persistOffset: {x: 487, y: 320} + persistOffset: {x: 519, y: 347} isBlackboardOn: 1 diff --git a/TNodeCore/Runtime/Models/BlackboardDragNode.cs b/TNodeCore/Runtime/Models/BlackboardDragNode.cs index 216c9cd..cd75c77 100644 --- a/TNodeCore/Runtime/Models/BlackboardDragNode.cs +++ b/TNodeCore/Runtime/Models/BlackboardDragNode.cs @@ -2,11 +2,13 @@ using TNodeCore.Runtime.Attributes; using TNodeCore.Runtime.Attributes.Ports; using TNodeCore.Runtime.RuntimeCache; +using UnityEngine; +using UnityEngine.Serialization; namespace TNodeCore.Runtime.Models{ [Serializable] [InternalUsage] - public class BlackboardDragNode:SceneNode{ + public class BlackboardDragNode:SceneNode,ISerializationCallbackReceiver{ public string BlackDragData{ get => blackDragData; set{ @@ -16,11 +18,16 @@ namespace TNodeCore.Runtime.Models{ } } } + /// /// /// - public Type BlackboardDragType{ get; set; } - + + public Type BlackboardDragType; + + [SerializeField] + private string blackboardDragTypeString; + public string blackDragData; /// TODO : The type handling in a safer way in the future @@ -40,5 +47,18 @@ namespace TNodeCore.Runtime.Models{ } public bool isListElement=false; + public void OnBeforeSerialize(){ + if (BlackboardDragType != null){ + blackboardDragTypeString = BlackboardDragType.AssemblyQualifiedName; + } + + } + + public void OnAfterDeserialize(){ + if(blackboardDragTypeString != null){ + BlackboardDragType = Type.GetType(blackboardDragTypeString); + } + + } } } \ No newline at end of file diff --git a/TNodeCore/Runtime/RuntimeCache/RuntimeCache.cs b/TNodeCore/Runtime/RuntimeCache/RuntimeCache.cs index ebe71ed..ba806da 100644 --- a/TNodeCore/Runtime/RuntimeCache/RuntimeCache.cs +++ b/TNodeCore/Runtime/RuntimeCache/RuntimeCache.cs @@ -6,6 +6,7 @@ using System.Reflection; using TNodeCore.Runtime.Attributes; using TNodeCore.Runtime.Interfaces; using TNodeCore.Runtime.Models; +using UnityEngine; namespace TNodeCore.Runtime.RuntimeCache{ public class PropAccessor:IModelPropertyAccessor{ @@ -134,20 +135,23 @@ namespace TNodeCore.Runtime.RuntimeCache{ if(type.BaseType is{IsGenericType: true} && type.BaseType.GetGenericTypeDefinition()==typeof(PortTypeConversion<,>)){ //if it is, add it to the cache CacheRuntimePortTypeConversion(type); - } - else{ + } } private void CacheRuntimePortTypeConversion(Type type){ + + if (type.BaseType == null) return; if (type.BaseType != null){ + var genericType = type.BaseType.GetGenericTypeDefinition(); - if (genericType != typeof(PortTypeConversion<,>)|| genericType != typeof(TwoWayPortTypeConversion<,>)){ + if (genericType != typeof(PortTypeConversion<,>) && genericType != typeof(TwoWayPortTypeConversion<,>)){ + return; } } - + //Forward direction var type1 = type.BaseType.GetGenericArguments()[0]; var type2 = type.BaseType.GetGenericArguments()[1]; @@ -229,9 +233,11 @@ namespace TNodeCore.Runtime.RuntimeCache{ } public List GetSupportedTypes(Type type){ + if(!CachedPortConverters.ContainsKey(type)){ return new List(); } + return CachedPortConverters[type].Keys.ToList(); }