fix:fix a bug of runtime graph

main
taoria 3 years ago
parent e5fd18ef2d
commit e72d7fa691
  1. 2
      TNodeCore/DataWrapper.cs
  2. 6
      TNodeCore/Models/BlackboardDragNodeData.cs
  3. 4
      TNodeCore/Runtime/Interfaces/IPortTypeConversion.cs
  4. 10
      TNodeCore/Runtime/RuntimeGraph.cs
  5. 18
      TNodeCore/Runtime/RuntimeNode.cs
  6. 4
      TNodeCore/RuntimeCache/IModelPropertyAccessor.cs
  7. 35
      TNodeCore/RuntimeCache/RuntimeCache.cs
  8. 5
      TNodeGraphViewImpl/Editor/GraphBlackboard/BlackboardField.cs
  9. 2
      TNodeGraphViewImpl/Editor/GraphBlackboard/DefaultGraphBlackboardView.cs
  10. 6
      TNodeGraphViewImpl/Editor/NodeGraphView/DataGraphView.cs
  11. 2
      TNodeGraphViewImpl/Editor/NodeViews/NodeView.cs

@ -14,7 +14,7 @@ namespace TNodeCore{
return CreateInstance<TWrapper>();
}
if(Cache.ContainsKey(data)){
return Cache[data];
return Cache[data]==null?CreateInstance<TWrapper>():Cache[data];
}
var wrapper = CreateInstance<TWrapper>();
wrapper.data = data;

@ -7,9 +7,8 @@ using UnityEngine;
namespace TNodeCore.Models{
[Serializable]
[InternalUsage]
public class BlackboardDragNodeData:RuntimeNodeData{
public class BlackboardDragNodeData:SceneNodeData{
public string blackDragData;
/// <summary>
/// it's very hacky way to get blackboard data ,even when the value is null,type info is not null!
/// </summary>
@ -23,7 +22,8 @@ namespace TNodeCore.Models{
}
[Serializable]
public class RuntimeNodeData:NodeData{
public class SceneNodeData:NodeData{
public BlackboardData BlackboardData{ get; set; }

@ -1,7 +1,7 @@
namespace TNodeCore.Runtime.Interfaces{
public interface IPortTypeConversion<in TFrom, out TTo>{
public TTo Convert(TFrom tFrom);
public abstract class PortTypeConversion<TFrom, TTo>{
public abstract TTo Convert(TFrom tFrom);
}
}

@ -22,12 +22,11 @@ namespace TNodeCore.Runtime{
public readonly Dictionary<string, RuntimeNode> RuntimeNodes;
public void DirectlyTraversal(){
foreach (var node in TopologicalOrder){
var links = node.OutputLink;
var links = node.InputLink;
foreach (var link in links){
var targetNode = RuntimeNodes[link.inPort.nodeDataId];
HandlingLink(link);
targetNode.NodeData.Process();
}
node.NodeData.Process();
}
}
public void DependencyTraversal(RuntimeNode runtimeNode){
@ -47,6 +46,7 @@ namespace TNodeCore.Runtime{
//out node is node output data
//in node is node receive data
var outValue = outNode.GetOutput(nodeLink.outPort.portEntryName);
inNode.SetInput(nodeLink.inPort.portEntryName, outValue);
}
public GraphTool(List<RuntimeNode> list, Dictionary<string, RuntimeNode> graphNodes){
@ -101,6 +101,10 @@ namespace TNodeCore.Runtime{
Debug.Log("hi");
var nodeList = RuntimeNodes.Values;
_graphTool = new GraphTool(nodeList.ToList(),RuntimeNodes);
var sceneNodes = RuntimeNodes.Values.Where(x => x.NodeData is SceneNodeData).Select(x => x.NodeData as SceneNodeData);
foreach (var sceneNode in sceneNodes){
if (sceneNode != null) sceneNode.BlackboardData = runtimeBlackboardData;
}
_build = true;
}

@ -5,6 +5,7 @@ using Codice.Client.Common.TreeGrouper;
using TNodeCore.Attribute.Ports;
using TNodeCore.Models;
using TNodeCore.RuntimeCache;
using UnityEngine;
namespace TNodeCore.Runtime{
public class RuntimeNode{
@ -18,7 +19,22 @@ namespace TNodeCore.Runtime{
public Type NodeType => _type;
public void SetInput(string portName,object value){
_portAccessors[portName].SetValue(this.NodeData,value);
var valueType = value.GetType();
var portType = _portAccessors[portName].Type;
Debug.Log(valueType);
Debug.Log(portType);
if(portType!=valueType && !portType.IsAssignableFrom(valueType)){
var res =RuntimeCache.RuntimeCache.Instance.GetConvertedValue(valueType, portType, value);
_portAccessors[portName].SetValue(this.NodeData, res);
}
else{
_portAccessors[portName].SetValue(this.NodeData,value);
}
}
public object GetOutput(string portName){

@ -4,5 +4,9 @@ namespace TNodeCore.RuntimeCache{
public interface IModelPropertyAccessor{
object GetValue(object model);
void SetValue(object model, object value);
public Type Type{ get; set; }
}
}

@ -14,8 +14,10 @@ namespace TNodeCore.RuntimeCache{
public readonly Action<T1, T2> Set;
public PropAccessor(string propName){
Type t = typeof(T1);
MethodInfo getter = t.GetMethod("get_" + propName);
MethodInfo setter = t.GetMethod("set_" + propName);
Type = getter?.ReturnType??setter?.GetParameters()[0].ParameterType;
if(getter!=null)
Get = (Func<T1, T2>)Delegate.CreateDelegate(typeof(Func<T1, T2>), null, getter);
if(setter!=null)
@ -32,12 +34,14 @@ namespace TNodeCore.RuntimeCache{
public void SetValue(object model, object value){
Set((T1)model,(T2)value);
}
public Type Type{ get; set; }
}
internal class PortConverterHelper<T1,T2> : IPortConverterHelper{
private readonly IPortTypeConversion<T1, T2> _converter;
private readonly PortTypeConversion<T1, T2> _converter;
public PortConverterHelper(Type type){
_converter = Activator.CreateInstance(type) as IPortTypeConversion<T1, T2>;
_converter = Activator.CreateInstance(type) as PortTypeConversion<T1, T2>;
}
public object Convert(object value){
return _converter.Convert((T1)value);
@ -116,23 +120,31 @@ namespace TNodeCore.RuntimeCache{
CacheRuntimeNodeData(type);
}
//Check if the type is implementing IPortTypeConversion<T1,T2>
if(typeof(IPortTypeConversion<,>).IsAssignableFrom(type)){
if(type.BaseType is{IsGenericType: true} && type.BaseType.GetGenericTypeDefinition()==typeof(PortTypeConversion<,>)){
//if it is, add it to the cache
Debug.Log("find conversion");
CacheRuntimePortTypeConversion(type);
}
else{
Debug.Log(type);
}
}
private void CacheRuntimePortTypeConversion(Type type){
if (type.IsGenericType == false){
return;
if (type.BaseType != null){
var genericType = type.BaseType.GetGenericTypeDefinition();
if (genericType != typeof(PortTypeConversion<,>)){
return;
}
}
var genericType = type.GetGenericTypeDefinition();
if (genericType != typeof(IPortTypeConversion<,>)){
else{
return;
}
var type1 = type.GetGenericArguments()[0];
var type2 = type.GetGenericArguments()[1];
var type1 = type.BaseType.GetGenericArguments()[0];
var type2 = type.BaseType.GetGenericArguments()[1];
Debug.Log(type1);
Debug.Log(type2);
var specificType = typeof(PortConverterHelper<,>).MakeGenericType(type1, type2);
var instance = Activator.CreateInstance(specificType, type) as IPortConverterHelper;
if (instance == null){
@ -256,8 +268,9 @@ namespace TNodeCore.RuntimeCache{
return (T) method.Invoke(data);
}
public static object GetValue(this IModel data, string path,Type type=null){
var method = RuntimeCache.Instance.CachedDelegatesForGettingValue[type??data.GetType()][path];
return method.Invoke(data);
var dic = RuntimeCache.Instance.CachedDelegatesForGettingValue[type ?? data.GetType()];
var method = dic.ContainsKey(path) ? dic[path] : null;
return method?.Invoke(data);
}
public static void SetValue<T>(this IModel data,string path,T value,Type type=null){

@ -1,4 +1,5 @@
using UnityEditor.Experimental.GraphView;
using UnityEditor;
using UnityEditor.Experimental.GraphView;
namespace TNodeGraphViewImpl.Editor.GraphBlackboard{
public class BlackboardField:UnityEditor.Experimental.GraphView.BlackboardField{
@ -6,7 +7,5 @@ namespace TNodeGraphViewImpl.Editor.GraphBlackboard{
public BlackboardField(BlackboardProperty.BlackboardProperty blackboardProperty):base(null,blackboardProperty.PropertyName,null){
BlackboardProperty = blackboardProperty;
}
}
}

@ -30,7 +30,7 @@ namespace TNodeGraphViewImpl.Editor.GraphBlackboard{
//skip if the field is a list or Ilist
if (!typeof(IList).IsAssignableFrom(field.FieldType)){
VisualElement visualElement = new VisualElement();
var propertyField = new BlackboardField(new BlackboardProperty.BlackboardProperty(ObjectNames.NicifyVariableName(field.Name),field.FieldType));
var propertyField = new BlackboardField(new BlackboardProperty.BlackboardProperty(field.Name,field.FieldType));
var foldoutData = new Foldout{
text = ObjectNames.NicifyVariableName(field.Name)
};

@ -155,8 +155,9 @@ namespace TNodeGraphViewImpl.Editor.NodeGraphView{
private void UpdateRuntimeGraphBehaviour(){
if(_runtimeGraph != null){
if (_runtimeGraphUpdate){
_runtimeGraph.ResolveDependency();
_runtimeGraphUpdate = false;
_runtimeGraph.ResolveDependency();
AfterRuntimeGraphUpdate?.Invoke();
}
@ -283,7 +284,7 @@ namespace TNodeGraphViewImpl.Editor.NodeGraphView{
//Get the node type
var nodeType = dataNode.GetType();
//Get the derived type of NodeAttribute View from the node type
if (dataNode is RuntimeNodeData runtimeNodeData){
if (dataNode is SceneNodeData runtimeNodeData){
runtimeNodeData.BlackboardData = GetBlackboardData();
}
var nodePos = Owner.graphEditorData.graphElementsData.
@ -445,7 +446,6 @@ namespace TNodeGraphViewImpl.Editor.NodeGraphView{
if (supportedTypes != null){
compatiblePorts.AddRange(ports.Where(x => supportedTypes.Contains(x.portType)).ToList());
}
return compatiblePorts;

@ -20,7 +20,7 @@ namespace TNodeGraphViewImpl.Editor.NodeViews{
public IBaseDataGraphView BaseDataGraphView{
get{
var visualElement = this.GetFirstAncestorOfType<IBaseDataGraphView>() as IBaseDataGraphView;
var visualElement = this.GetFirstAncestorOfType<IBaseDataGraphView>();
return visualElement;
}
}

Loading…
Cancel
Save