Example #1
0
 public override void Eval(GraphScope scope)
 {
     foreach (var invoke in evalInvokes)
     {
         Invoke(invoke);
     }
 }
Example #2
0
        /// <summary>
        /// Creates a deep copy of another scope.
        /// </summary>
        public GraphScope(GraphScope other)
        {
            Values       = new Dictionary <Socket, object>(other.Values);
            evaluatedIDs = new HashSet <int>(other.evaluatedIDs);

            foreach (var v in other.Vars)
            {
                Vars.Add(v.Key, v.Value);
            }
        }
Example #3
0
        private IEnumerator <Yield> Exec(Socket from, Socket to, GraphScope scope)
        {
            var source      = to.GetNode(Graph);
            var destination = to.GetNode(Graph);

            // Store from socket outputs in the scope
            foreach (var output in source.GetOutputSockets())
            {
                scope.Values[output] = source.GetSocketValue(output);
            }

            // Evaluate the next node
            Eval(destination, scope);

            // Execute the next node
            var yield = destination.Exec(from, to, scope);

            while (yield.MoveNext())
            {
                yield return(yield.Current);
            }

            // Check if execution is finished
            if (yield.Current == null)
            {
                yield break;
            }

            var signal = yield.Current as SignalSocket;

            if (signal == null)
            {
                yield break;
            }

            // Continue execution
            var newFrom = signal.Socket;

            foreach (var newTo in Graph.Links.HasSocketAsSource(newFrom))
            {
                yield = Exec(newFrom, newTo, scope);
                while (yield.MoveNext())
                {
                    yield return(yield.Current);
                }
            }
        }
Example #4
0
        private IEnumerator <Yield> Exec(Socket from, Socket to, GraphScope scope)
        {
            Node source, destination;

            try
            {
                source      = to.GetNode(Graph);
                destination = to.GetNode(Graph);
            }
            catch (InvalidOperationException)
            {
                yield break;
            }

            // Store from socket outputs in the scope
            foreach (var output in source.GetOutputSockets())
            {
                scope.Values[output] = source.GetSocketValue(output);
            }

            // Evaluate the next node
            Eval(destination, scope);

            // Execute the next node
            var yield = destination.Exec(from, to, scope);

            while (yield.MoveNext())
            {
                // Check if we should signal the next socket
                var signal = yield.Current as SignalSocket;
                if (signal != null)
                {
                    var newFrom = signal.Socket;
                    foreach (var newTo in Graph.Links.HasSocketAsSource(newFrom))
                    {
                        var newYield = Exec(newFrom, newTo, scope);
                        while (newYield.MoveNext())
                        {
                            yield return(newYield.Current);
                        }
                    }
                }

                // Wait for this yield to return
                yield return(yield.Current);
            }
        }
Example #5
0
        private void Eval(Node toEval, GraphScope scope)
        {
            var sockets = toEval.GetInputSockets();

            foreach (var socket in sockets)
            {
                var inputs = Graph.Links.HasSocketAsDestination(socket);
                foreach (var input in inputs)
                {
                    // Evaluate the node
                    Node node;
                    try
                    {
                        node = input.GetNode(Graph);
                    }
                    catch (Exception e)
                    {
                        Debug.LogWarningFormat(
                            "Could not evaluate {0} in {1}:\n{2}",
                            input, Graph, e);
                        continue;
                    }

                    if (scope.ShouldEvaluate(node.ID))
                    {
                        Eval(input.GetNode(Graph), scope);

                        // Store source socket outputs in the scope
                        foreach (var output in node.GetOutputSockets())
                        {
                            scope.Values[output] = node.GetSocketValue(output);
                        }
                    }

                    // Set the input destination socket values
                    toEval.SetSocketValue(socket, scope.Values[input]);
                }
            }

            toEval.Eval(scope);
        }
Example #6
0
        public override IEnumerator <Yield> Exec
            (Socket from, Socket to, GraphScope scope)
        {
            foreach (var invoke in execInvokes)
            {
                if (invoke.Trigger != to.FieldName)
                {
                    continue;
                }

                Invoke(invoke);

                var next = GetSocket(invoke.Next);
                if (!Util.IsNull(next))
                {
                    yield return(new SignalSocket(scope, next));
                }

                break;
            }
        }
Example #7
0
        public void Exec(ExecType type)
        {
            if (type == ExecType.OnDestroy)
            {
                destroyWhenDone = true;
            }

            if (Util.IsNull(Graph))
            {
                return;
            }

            if (yields == null)
            {
                yields = new List <IEnumerator <Yield> >();
            }

            foreach (var from in entryPoints[type])
            {
                // Initialize the scope
                var scope = new GraphScope();
                Eval(from.GetNode(Graph), scope);

                // Start Execution
                foreach (var to in Graph.Links.HasSocketAsSource(from))
                {
                    var yield = Exec(from, to, scope);
                    while (yield.MoveNext())
                    {
                        // Check if execution is delayed
                        if (!yield.Current.Finished)
                        {
                            yields.Add(yield);
                            break;
                        }
                    }
                }
            }
        }
Example #8
0
 /// <summary>
 /// Executes this node.
 /// </summary>
 /// <param name="from">The socket the signal comes from.</param>
 /// <param name="to">The socket the signal is sent to.</param>
 /// <param name="scope">The in-scope variables.</param>
 /// <returns>A yield.</returns>
 public virtual IEnumerator <Yield> Exec
     (Socket from, Socket to, GraphScope scope)
 {
     yield return(null);
 }
Example #9
0
 /// <summary>
 /// Evaluates this node.
 /// </summary>
 /// <param name="scope">The in-scope variables.</param>
 /// <returns>The execution socket to trigger.</returns>
 public virtual void Eval(GraphScope scope)
 {
 }
Example #10
0
        private void Invoke(GraphScope scope, IInvoke invoke)
        {
            var targetSocket = GetSocket(invoke.Target);

            if (Util.IsNull(targetSocket))
            {
                return;
            }

            var type   = GetSocketType(targetSocket);
            var target = GetSocketValue(targetSocket);
            var flags  = BindingFlags.Instance | BindingFlags.NonPublic
                         | BindingFlags.FlattenHierarchy | BindingFlags.Public;

            object value;

            object[] args;
            switch (invoke.InvokeType)
            {
            case InvokeType.GetField:
                var gField = type.GetField(invoke.MemberName, flags);
                value = gField.GetValue(target);
                SetSocketValue(invoke.Result, value);
                break;

            case InvokeType.SetField:
                var sField = type.GetField(invoke.MemberName, flags);
                value = GetSocketValue(invoke.Args[0]);
                sField.SetValue(target, value);
                break;

            case InvokeType.GetProperty:
                var gProperty = type.GetProperty(invoke.MemberName, flags);
                args  = BuildArgs(0, invoke.Args);
                value = gProperty.GetValue(target, args);
                SetSocketValue(invoke.Result, value);
                break;

            case InvokeType.SetProperty:
                var sProperty = type.GetProperty(invoke.MemberName, flags);
                args  = BuildArgs(1, invoke.Args);
                value = GetSocketValue(invoke.Args[0]);
                sProperty.SetValue(target, value, args);
                break;

            case InvokeType.CallMethod:
                var argTypes = BuildArgTypes(0, invoke.Args);
                var method   = type.GetMethod(invoke.MemberName, flags, null, argTypes, null);
                args  = BuildArgs(0, invoke.Args);
                value = method.Invoke(target, args);
                if (method.ReturnType != typeof(void))
                {
                    SetSocketValue(invoke.Result, value);
                }
                break;

            case InvokeType.SetVar:
                value = GetSocketValue(invoke.Target);
                scope.Vars[invoke.MemberName].Value = value;
                SetSocketValue(invoke.Result, value);
                break;

            case InvokeType.GetVar:
                value = scope.Vars[invoke.Target].Value;
                SetSocketValue(invoke.Result, value);
                break;

            default:
                var msg = string.Format(
                    "Invoke type {0} not supported!",
                    invoke.InvokeType);
                throw new InvalidOperationException(msg);
            }
        }