Merge pull request #28 from taoria/working-in-process

refactor: add a package.json
main
taoria 3 years ago committed by GitHub
commit f7d61870e2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 1
      .gitignore
  2. 5
      TNode/TNodeCore/Attribute/Ports/BatchInputAttribute.cs
  3. 0
      TNode/TNodeCore/Editor/GraphEditor.cs
  4. 0
      TNode/TNodeCore/Editor/GraphEditor.cs.meta
  5. 0
      TNode/TNodeCore/Runtime/Attributes.meta
  6. 0
      TNode/TNodeCore/Runtime/Attributes/DisableOnInspector.cs
  7. 0
      TNode/TNodeCore/Runtime/Attributes/DisableOnInspector.cs.meta
  8. 0
      TNode/TNodeCore/Runtime/Attributes/GraphUsageAttribute.cs
  9. 0
      TNode/TNodeCore/Runtime/Attributes/GraphUsageAttribute.cs.meta
  10. 0
      TNode/TNodeCore/Runtime/Attributes/InternalUsageAttribute.cs
  11. 0
      TNode/TNodeCore/Runtime/Attributes/InternalUsageAttribute.cs.meta
  12. 0
      TNode/TNodeCore/Runtime/Attributes/NodeAttribute.cs
  13. 0
      TNode/TNodeCore/Runtime/Attributes/NodeAttribute.cs.meta
  14. 0
      TNode/TNodeCore/Runtime/Attributes/PortTypeConversion.cs
  15. 0
      TNode/TNodeCore/Runtime/Attributes/PortTypeConversion.cs.meta
  16. 0
      TNode/TNodeCore/Runtime/Attributes/Ports.meta
  17. 5
      TNode/TNodeCore/Runtime/Attributes/Ports/BatchInputAttribute.cs
  18. 0
      TNode/TNodeCore/Runtime/Attributes/Ports/BatchInputAttribute.cs.meta
  19. 0
      TNode/TNodeCore/Runtime/Attributes/Ports/BatchOutputAttribute.cs
  20. 0
      TNode/TNodeCore/Runtime/Attributes/Ports/BatchOutputAttribute.cs.meta
  21. 0
      TNode/TNodeCore/Runtime/Attributes/Ports/InputAttribute.cs
  22. 0
      TNode/TNodeCore/Runtime/Attributes/Ports/InputAttribute.cs.meta
  23. 0
      TNode/TNodeCore/Runtime/Attributes/Ports/OutputAttribute.cs
  24. 0
      TNode/TNodeCore/Runtime/Attributes/Ports/OutputAttribute.cs.meta
  25. 1
      TNode/TNodeCore/Runtime/Attributes/Ports/PortAttribute.cs
  26. 0
      TNode/TNodeCore/Runtime/Attributes/Ports/PortAttribute.cs.meta
  27. 0
      TNode/TNodeCore/Runtime/Attributes/RuntimeNodeAttribute.cs
  28. 0
      TNode/TNodeCore/Runtime/Attributes/RuntimeNodeAttribute.cs.meta
  29. 0
      TNode/TNodeCore/Runtime/Attributes/ShowInNodeViewAttribute.cs
  30. 0
      TNode/TNodeCore/Runtime/Attributes/ShowInNodeViewAttribute.cs.meta
  31. 0
      TNode/TNodeCore/Runtime/Attributes/ViewComponentAttribute.cs
  32. 0
      TNode/TNodeCore/Runtime/Attributes/ViewComponentAttribute.cs.meta
  33. 0
      TNode/TNodeCore/Runtime/Components.meta
  34. 0
      TNode/TNodeCore/Runtime/Components/RuntimeDataSaver.cs
  35. 0
      TNode/TNodeCore/Runtime/Components/RuntimeDataSaver.cs.meta
  36. 143
      TNode/TNodeCore/Runtime/Components/RuntimeGraph.cs
  37. 0
      TNode/TNodeCore/Runtime/Components/RuntimeGraph.cs.meta
  38. 0
      TNode/TNodeCore/Runtime/DataWrapper.cs
  39. 0
      TNode/TNodeCore/Runtime/DataWrapper.cs.meta
  40. 0
      TNode/TNodeCore/Runtime/Models.meta
  41. 0
      TNode/TNodeCore/Runtime/Models/BlackboardData.cs
  42. 0
      TNode/TNodeCore/Runtime/Models/BlackboardData.cs.meta
  43. 0
      TNode/TNodeCore/Runtime/Models/BlackboardDragNodeData.cs
  44. 0
      TNode/TNodeCore/Runtime/Models/BlackboardDragNodeData.cs.meta
  45. 0
      TNode/TNodeCore/Runtime/Models/GraphData.cs
  46. 0
      TNode/TNodeCore/Runtime/Models/GraphData.cs.meta
  47. 0
      TNode/TNodeCore/Runtime/Models/IModel.cs
  48. 0
      TNode/TNodeCore/Runtime/Models/IModel.cs.meta
  49. 0
      TNode/TNodeCore/Runtime/Models/NodeData.cs
  50. 0
      TNode/TNodeCore/Runtime/Models/NodeData.cs.meta
  51. 0
      TNode/TNodeCore/Runtime/Models/NodeLink.cs
  52. 0
      TNode/TNodeCore/Runtime/Models/NodeLink.cs.meta
  53. 0
      TNode/TNodeCore/Runtime/Models/PortInfo.cs
  54. 0
      TNode/TNodeCore/Runtime/Models/PortInfo.cs.meta
  55. 0
      TNode/TNodeCore/Runtime/Models/SceneObjectWrapper.cs
  56. 0
      TNode/TNodeCore/Runtime/Models/SceneObjectWrapper.cs.meta
  57. 0
      TNode/TNodeCore/Runtime/RuntimeCache.meta
  58. 0
      TNode/TNodeCore/Runtime/RuntimeCache/IModelPropertyAccessor.cs
  59. 0
      TNode/TNodeCore/Runtime/RuntimeCache/IModelPropertyAccessor.cs.meta
  60. 3
      TNode/TNodeCore/Runtime/RuntimeCache/RuntimeCache.cs
  61. 0
      TNode/TNodeCore/Runtime/RuntimeCache/RuntimeCache.cs.meta
  62. 2
      TNode/TNodeGraphViewImpl/Editor/Inspector/NodeInspectorInNode.cs
  63. 5
      TNode/TNodeGraphViewImpl/Editor/NodeViews/NodeView.cs
  64. 20
      TNode/package.json
  65. 7
      TNode/package.json.meta

1
.gitignore vendored

@ -1 +1,2 @@
/Dialogue/
/TextMesh Pro/

@ -1,5 +0,0 @@
namespace TNodeCore.Attribute.Ports{
public class BatchInputAttribute{
}
}

@ -0,0 +1,5 @@
namespace TNode.TNodeCore.Runtime.Attributes.Ports{
public class BatchInputAttribute{
}
}

@ -22,6 +22,7 @@ namespace TNodeCore.Attribute.Ports{
public readonly string Name;
public readonly PortNameHandling NameHandling;
public Type HandledType;
public bool Multiple = true;
public TypeHandling TypeHandling{ get; set; }
public PortAttribute(string name,PortNameHandling nameHandling=PortNameHandling.Auto,TypeHandling typeHandling=TypeHandling.Declared){
this.Name = name;

@ -7,20 +7,53 @@ using UnityEngine;
namespace TNodeCore.Components{
public class RuntimeGraph:MonoBehaviour{
/// <summary>
/// Graph data reference to be used in runtime
/// </summary>
public GraphData graphData;
/// <summary>
/// Runtime copy of scene node data to hold references to scene objects
/// </summary>
public List<SceneNodeData> sceneNodes;
/// <summary>
/// Map of node id to runtime node
/// </summary>
public readonly Dictionary<string, RuntimeNode> RuntimeNodes = new Dictionary<string, RuntimeNode>();
///<summary>
/// The graph tool the current runtime graph is using
/// </summary>
private GraphTool _graphTool;
/// <summary>
/// Inner graph tool to help with graph operations
/// </summary>
private class GraphTool{
/// <summary>
/// Topological order of the graph nodes
/// </summary>
[NonSerialized]
public readonly List<RuntimeNode> TopologicalOrder = new List<RuntimeNode>();
/// <summary>
/// Entry nodes of the graph. These are the nodes that has no input.
/// </summary>
public readonly List<RuntimeNode> EntryNodes = new List<RuntimeNode>();
/// <summary>
/// Cached data for Dependency traversal.
/// </summary>
public readonly Dictionary<string, object> OutputCached = new Dictionary<string, object>();
/// <summary>
/// Ssed to detect if the graph tool is caching the output data of the node
/// </summary>
private bool _isCachingOutput = false;
/// <summary>
/// elements are read only ,do not modify them
/// </summary>
public readonly Dictionary<string, RuntimeNode> RuntimeNodes;
//Traverse and process all nodes in a topological order,dependency of the node is already resolved.if you want to run specific node,you can use RunNodeDependently instead
public void DirectlyTraversal(){
foreach (var node in TopologicalOrder){
var links = node.InputLink;
@ -30,26 +63,76 @@ namespace TNodeCore.Components{
node.NodeData.Process();
}
}
public void DependencyTraversal(RuntimeNode runtimeNode){
/// <summary>
/// Cache out port data in the graph tool so that we can directly access the output.
/// The two function assume there will be no change happens in scene nodes or blackboard referenced data during the running,so in a dependency traversal for some
/// batch of nodes.the nodes could directly access the output data in the graph tool instead of waiting dependency traversal resolve the result of the output.
/// </summary>
public void StartCachingPort(){
_isCachingOutput = true;
}
public void EndCachingPort(){
_isCachingOutput = false;
OutputCached.Clear();
}
/// <summary>
/// Resolve dependencies by a deep first search,the depended nodes will be processed to satisfy the need of the the given runtime node
/// Note it's a recursive function.if you want directly traverse all nodes with dependency resolved ,use DirectlyTraversal() instead.
/// </summary>
/// <param name="runtimeNode">The node you want to resolve dependency</param>
/// <param name="dependencyLevel">search depth,no need provide a number when use outside</param>
public void RunNodeDependently(RuntimeNode runtimeNode,int dependencyLevel=0){
var links = runtimeNode.InputLink;
foreach (var link in links){
var outputNode = RuntimeNodes[link.outPort.nodeDataId];
DependencyTraversal(outputNode);
RunNodeDependently(outputNode,dependencyLevel+1);
HandlingLink(link);
}
if (dependencyLevel > DependencyLevelMax){
throw new Exception("Dependency anomaly detected,check if there is a loop in the graph");
}
//if the runtime node has no output ,it will not be processed
if (runtimeNode.OutputLink.Count == 0 && dependencyLevel != 0){
return;
}
runtimeNode.NodeData.Process();
}
/// <summary>
/// Max depth of dependency traversal,in case of some special situation. the dependency level bigger than this number will be considered as a loop.
/// </summary>
private const int DependencyLevelMax = 1145;
/// <summary>
/// Handling a node link to transfer data from it's output side to the input side
/// </summary>
/// <param name="nodeLink">Link you want to process</param>
public void HandlingLink(NodeLink nodeLink){
//out node is node output data
//in node is node receive data
var inNode = RuntimeNodes[nodeLink.inPort.nodeDataId];
var outNode = RuntimeNodes[nodeLink.outPort.nodeDataId];
//out node is node output data
//in node is node receive data
var outValue = outNode.GetOutput(nodeLink.outPort.portEntryName);
//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);;
if (_isCachingOutput){
OutputCached[cachedKey] = outValue;
}
inNode.SetInput(nodeLink.inPort.portEntryName, outValue);
}
/// <summary>
/// Constructor of the graph tool,it will traverse the graph and build the topological order of the graph.
/// </summary>
/// <param name="list">List of nodes you need to traversal to build graph tool</param>
/// <param name="graphNodes">Map stores the mapping of node data id to runtime node</param>
public GraphTool(List<RuntimeNode> list, Dictionary<string, RuntimeNode> graphNodes){
RuntimeNodes = graphNodes;
if (list == null) return;
@ -87,10 +170,20 @@ namespace TNodeCore.Components{
}
/// <summary>
/// Holding the reference of the blackboard ,but it will be override by the runtime graph
/// </summary>
[SerializeReference]
public BlackboardData runtimeBlackboardData;
/// <summary>
/// Check if the runtime graph is build .a built graph has a graph tool set up
/// </summary>
[NonSerialized]
private bool _build = false;
/// <summary>
/// Build the graph tool and other dependencies for the runtime graph
/// </summary>
public void Build(){
var link = graphData.NodeLinks;
@ -109,6 +202,11 @@ namespace TNodeCore.Components{
_build = true;
}
/// <summary>
/// Cast the node data to a runtime node
/// </summary>
/// <param name="nodeData">Node data you provided</param>
/// <returns></returns>
public RuntimeNode Get(NodeData nodeData){
if(!_build)
Build();
@ -117,20 +215,24 @@ namespace TNodeCore.Components{
}
return null;
}
/// <summary>
/// Get the runtime node from an id
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public RuntimeNode Get(string id){
if (RuntimeNodes.ContainsKey(id)){
return RuntimeNodes[id];
}
return null;
}
//DFS search for resolving dependency
public bool ResolveDependency(NodeData startNode){
//DFS search to run a node.
public bool RunOnDependency(NodeData startNode){
if(!_build)
Build();
if (_graphTool == null)
return false;
_graphTool.DependencyTraversal(Get(startNode));
_graphTool.RunNodeDependently(Get(startNode));
return true;
}
public bool ResolveDependency(){
@ -152,10 +254,20 @@ namespace TNodeCore.Components{
}
public List<RuntimeNode> GetRuntimeNodesOfType<T>(){
return RuntimeNodes.Values.Where(x => x.NodeType == typeof(T)).ToList();
return RuntimeNodes.Values.Where(x => typeof(T).IsAssignableFrom(x.NodeType)).ToList();
}
public List<RuntimeNode> GetRuntimeNodesOfType(Type type){
return RuntimeNodes.Values.Where(x => x.NodeType == type).ToList();
return RuntimeNodes.Values.Where(x => type.IsAssignableFrom(type)).ToList();
}
public void RunNodesOfType(Type t){
var nodes = GetRuntimeNodesOfType(t);
_graphTool.StartCachingPort();
foreach (var runtimeNode in nodes){
RunOnDependency(runtimeNode.NodeData);
}
_graphTool.EndCachingPort();
}
private void ModifyOrCreateOutNode(NodeLink linkData){
var outNodeId = linkData.outPort.nodeDataId;
@ -170,7 +282,7 @@ namespace TNodeCore.Components{
public void OnValidate(){
if(runtimeBlackboardData==null||runtimeBlackboardData.GetType()==typeof(BlackboardData)){
if (graphData != null)
runtimeBlackboardData = graphData.blackboardData.Clone() as BlackboardData;
runtimeBlackboardData = graphData.blackboardData?.Clone() as BlackboardData;
}
}
@ -186,6 +298,7 @@ namespace TNodeCore.Components{
public void Start(){
Build();
}
public virtual void RuntimeExecute(){
_graphTool.DirectlyTraversal();

@ -267,6 +267,9 @@ namespace TNodeCore.RuntimeCache{
return (T) method.Invoke(data);
}
public static object GetValue(this IModel data, string path,Type type=null){
if(!RuntimeCache.Instance.CachedDelegatesForGettingValue.ContainsKey(type??data.GetType())){
return null;
}
var dic = RuntimeCache.Instance.CachedDelegatesForGettingValue[type ?? data.GetType()];
var method = dic.ContainsKey(path) ? dic[path] : null;
return method?.Invoke(data);

@ -80,7 +80,7 @@ namespace TNode.Editor.Inspector{
if(!test.IsRuntimeGraph) return;
var runtimeGraph = test.GetRuntimeGraph();
if (runtimeGraph != null){
var res = runtimeGraph.ResolveDependency(_data);
var res = runtimeGraph.RunOnDependency(_data);
}
_data.OnTest();
}

@ -105,7 +105,8 @@ namespace TNodeGraphViewImpl.Editor.NodeViews{
foreach (var propertyInfo in propertyInfos){
if (propertyInfo.GetCustomAttributes(typeof(OutputAttribute),true).FirstOrDefault() is OutputAttribute attribute){
Port port = InstantiatePort(Orientation.Horizontal, Direction.Output,Port.Capacity.Multi,BuildPortType(attribute,propertyInfo));
Port port = InstantiatePort(Orientation.Horizontal, Direction.Output,attribute.Multiple?Port.Capacity.Multi:Port.Capacity.Single,BuildPortType(attribute,propertyInfo));
this.outputContainer.Add(port);
var portName = ObjectNames.NicifyVariableName(BuildPortName(attribute,propertyInfo));
port.portName = portName;
@ -115,7 +116,7 @@ namespace TNodeGraphViewImpl.Editor.NodeViews{
}
foreach (var propertyInfo in propertyInfos){
if(propertyInfo.GetCustomAttributes(typeof(InputAttribute),true).FirstOrDefault() is InputAttribute attribute){
Port port = InstantiatePort(Orientation.Horizontal, Direction.Input,Port.Capacity.Single,BuildPortType(attribute,propertyInfo));
Port port = InstantiatePort(Orientation.Horizontal, Direction.Input,attribute.Multiple?Port.Capacity.Multi:Port.Capacity.Single,BuildPortType(attribute,propertyInfo));
this.inputContainer.Add(port);
var portName = BuildPortName(attribute,propertyInfo);
port.portName = portName;

@ -0,0 +1,20 @@
{
"name": "com.taoria.t-node",
"version": "0.0.1",
"displayName": "T-Node",
"description": "A graph edition tool for unity",
"unity": "2022.1",
"unityRelease": "0b5",
"dependencies": {
},
"keywords": [
"graph",
"node",
"wrapper"
],
"author": {
"name": "taoria",
"email": "445625470@qq.com",
"url": "taoria@github.io"
}
}

@ -0,0 +1,7 @@
fileFormatVersion: 2
guid: 3838bc14242907547b72a73ef55ce0e0
PackageManifestImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
Loading…
Cancel
Save