Esempio n. 1
0
 public override void OnExecute()
 {
     if (!body.isAssigned)
     {
         throw new System.Exception("body is unassigned");
     }
     if (HasCoroutineInFlow(body))
     {
         owner.StartCoroutine(OnUpdate(), this);
     }
     else
     {
         using (var val = target.Get <System.IDisposable>()) {
             value = val;
             Node      n;
             WaitUntil w;
             if (!body.ActivateFlowNode(out n, out w))
             {
                 throw new System.Exception("body is not coroutine but body is not finished.");
             }
             if (n != null)
             {
                 jumpState = n.GetJumpState();
                 if (jumpState != null)
                 {
                     Finish();
                     return;
                 }
             }
         }
         Finish(onFinished);
     }
 }
Esempio n. 2
0
 public override void OnExecute()
 {
     if (!HasCoroutineInFlow(body))
     {
         IEnumerable lObj = target.Get() as IEnumerable;
         if (lObj != null)
         {
             foreach (object obj in lObj)
             {
                 if (body == null || !body.isAssigned)
                 {
                     continue;
                 }
                 loopObject = obj;
                 Node      n;
                 WaitUntil w;
                 if (!body.ActivateFlowNode(out n, out w))
                 {
                     throw new System.Exception("body is not coroutine but body is not finished.");
                 }
                 if (n == null)                       //Skip on executing flow input pin.
                 {
                     continue;
                 }
                 JumpStatement js = n.GetJumpState();
                 if (js != null)
                 {
                     if (js.jumpType == JumpStatementType.Continue)
                     {
                         continue;
                     }
                     else
                     {
                         if (js.jumpType == JumpStatementType.Return)
                         {
                             jumpState = js;
                         }
                         break;
                     }
                 }
             }
         }
         else
         {
             Debug.LogError("The target must be IEnumerable");
         }
         Finish(onFinished);
     }
     else
     {
         owner.StartCoroutine(OnUpdate(), this);
     }
 }
Esempio n. 3
0
 public override void OnExecute()
 {
     if (!body.isAssigned)
     {
         throw new System.Exception("body is unassigned");
     }
     if (HasCoroutineInFlow(body))
     {
         owner.StartCoroutine(OnUpdate(), this);
     }
     else
     {
         do
         {
             if (body == null || !body.isAssigned)
             {
                 continue;
             }
             Node      n;
             WaitUntil w;
             if (!body.ActivateFlowNode(out n, out w))
             {
                 throw new System.Exception("body is not coroutine but body is not finished.");
             }
             if (n == null)                   //Skip on executing flow input pin.
             {
                 continue;
             }
             JumpStatement js = n.GetJumpState();
             if (js != null)
             {
                 if (js.jumpType == JumpStatementType.Continue)
                 {
                     continue;
                 }
                 else
                 {
                     if (js.jumpType == JumpStatementType.Return)
                     {
                         jumpState = js;
                     }
                     break;
                 }
             }
         } while(condition.GetValue <bool>());
         Finish(onFinished);
     }
 }
Esempio n. 4
0
 /// <summary>
 /// Execute flow node and handle jump state and coroutine nodes.
 /// </summary>
 /// <param name="flowNodes"></param>
 /// <param name="onFinish"></param>
 /// <param name="handleCoroutine"></param>
 protected void ExecuteFlow(IList <MemberData> flowNodes, System.Action onFinish, bool handleCoroutine = true)
 {
     for (int i = 0; i < flowNodes.Count; i++)
     {
         MemberData flow = flowNodes[i];
         if (flow == null || !flow.isAssigned)
         {
             continue;
         }
         Node n;
         if (!flow.ActivateFlowNode(out n))           //Activate flow node and check if the node is not finished.
         {
             if (handleCoroutine)                     //Wait when handleCoroutine is true
             {
                 owner.StartCoroutine(ExecuteFlowCoroutine(flowNodes, i, onFinish), this);
                 return;
             }
         }
         if (n != null)
         {
             jumpState = n.GetJumpState();
             if (jumpState != null)
             {
                 break;
             }
         }
     }
     if (onFinish != null)
     {
         onFinish();
     }
 }
Esempio n. 5
0
        IEnumerator Do()
        {
            if (!targetNode.isAssigned)
            {
                Debug.LogError("Unassigned target node", this);
                Finish();
                yield break;
            }
            Node      n;
            WaitUntil w;

            if (!targetNode.ActivateFlowNode(out n, out w))
            {
                yield return(w);
            }
            if (n == null)
            {
                throw new System.Exception("targetNode must be FlowNode");
            }
            JumpStatement js = n.GetJumpState();

            if (js != null)
            {
                jumpState = js;
            }
            state = StateType.Failure;
            Finish();
        }
Esempio n. 6
0
 public override void OnExecute()
 {
     if (target.isAssigned)
     {
         target.ActivateFlowNode();
     }
     Finish();
 }
Esempio n. 7
0
 public override void OnExecute()
 {
     if (!HasCoroutineInFlow(body))
     {
         for (index = startIndex.Get(); uNodeHelper.OperatorComparison(index, compareNumber.Get(), compareType);
              uNodeHelper.SetObject(ref index, iteratorSetValue.Get(), iteratorSetType))
         {
             if (!body.isAssigned)
             {
                 continue;
             }
             Node      n;
             WaitUntil w;
             if (!body.ActivateFlowNode(out n, out w))
             {
                 throw new System.Exception("body is not coroutine but body is not finished.");
             }
             if (n == null)                   //Skip on executing flow input pin.
             {
                 continue;
             }
             JumpStatement js = n.GetJumpState();
             if (js != null)
             {
                 if (js.jumpType == JumpStatementType.Continue)
                 {
                     continue;
                 }
                 else
                 {
                     if (js.jumpType == JumpStatementType.Return)
                     {
                         jumpState = js;
                     }
                     break;
                 }
             }
         }
         Finish(onFinished);
     }
     else
     {
         owner.StartCoroutine(OnUpdate(), this);
     }
 }
Esempio n. 8
0
 protected override object Value()
 {
     System.Type type = returnType.Get <System.Type>();
     if (type != null)
     {
         if (type == typeof(void))
         {
             return(CustomDelegate.CreateActionDelegate((obj) => {
                 if (owner == null)
                 {
                     return;
                 }
                 for (int i = 0; i < parameterValues.Count; i++)
                 {
                     parameterValues[i] = obj[i];
                 }
                 body.InvokeFlow();
             }, parameterTypes.Select((item) => item.Get <System.Type>()).ToArray()));
         }
         else
         {
             System.Type[] types = new System.Type[parameterTypes.Count + 1];
             for (int x = 0; x < parameterTypes.Count; x++)
             {
                 types[x] = parameterTypes[x].Get <System.Type>();
             }
             types[types.Length - 1] = type;
             return(CustomDelegate.CreateFuncDelegate((obj) => {
                 if (owner == null)
                 {
                     return null;
                 }
                 for (int i = 0; i < parameterValues.Count; i++)
                 {
                     parameterValues[i] = obj[i];
                 }
                 Node n;
                 WaitUntil w;
                 if (!body.ActivateFlowNode(out n, out w))
                 {
                     throw new System.Exception("Coroutine aren't supported by anonymous function in runtime.");
                 }
                 if (n == null)
                 {
                     throw new System.Exception("No return value");
                 }
                 JumpStatement js = n.GetJumpState();
                 if (js == null || js.jumpType != JumpStatementType.Return || !(js.from is NodeReturn))
                 {
                     throw new System.Exception("No return value");
                 }
                 return (js.from as NodeReturn).GetReturnValue();
             }, types));
         }
     }
     return(null);
 }
Esempio n. 9
0
 public IEnumerator OnUpdate()
 {
     while (state == StateType.Running)
     {
         if (!targetNode.isAssigned)
         {
             Debug.LogError("Unassigned target node", this);
             Finish();
             yield break;
         }
         Node n;
         if (canExecuteEvent && (RepeatForever || RepeatCount > repeatNumber))
         {
             WaitUntil w;
             if (!targetNode.ActivateFlowNode(out n, out w))
             {
                 yield return(w);
             }
             repeatNumber++;
             canExecuteEvent = false;
         }
         else
         {
             n = targetNode.GetTargetNode();
         }
         if (n.IsFinished())
         {
             JumpStatement js = n.GetJumpState();
             if (js != null)
             {
                 if (js.jumpType == JumpStatementType.Continue)
                 {
                     continue;
                 }
                 else if (js.jumpType == JumpStatementType.Break)
                 {
                     Finish();
                     yield break;
                 }
                 jumpState = js;
                 Finish();
                 yield break;
             }
             if (StopEventOnFailure && n.currentState == StateType.Failure)
             {
                 Finish();
                 yield break;
             }
             if (!RepeatForever && RepeatCount <= repeatNumber)
             {
                 Finish();
             }
             canExecuteEvent = true;
         }
         yield return(null);
     }
 }
Esempio n. 10
0
 public IEnumerator OnUpdate()
 {
     while (state == StateType.Running)
     {
         if (!targetNode.isAssigned)
         {
             Debug.LogError("No Target Event", this);
             Finish();
             yield break;
         }
         Node      n;
         WaitUntil w;
         if (!targetNode.ActivateFlowNode(out n, out w))
         {
             yield return(w);
         }
         if (n == null)
         {
             throw new System.Exception("targetNode must be FlowNode");
         }
         JumpStatement js = n.GetJumpState();
         if (js != null)
         {
             if (js.jumpType == JumpStatementType.Continue)
             {
                 continue;
             }
             else if (js.jumpType == JumpStatementType.Break)
             {
                 Finish();
                 yield break;
             }
             jumpState = js;
             Finish();
             yield break;
         }
         if (n.currentState == StateType.Failure)
         {
             Finish();
             yield break;
         }
         yield return(null);
     }
 }
Esempio n. 11
0
 /// <summary>
 /// Execute flow node and finish this node.
 /// </summary>
 /// <param name="flowNode"></param>
 protected void Finish(MemberData flowNode)
 {
     if (flowNode != null && flowNode.isAssigned)
     {
         Node      n;
         WaitUntil w;
         if (!flowNode.ActivateFlowNode(out n, out w))
         {
             WaitAndExecute(w, delegate() {
                 if (n != null)
                 {
                     jumpState = n.GetJumpState();
                 }
                 Finish();
             });
             return;
         }
         if (n != null)
         {
             jumpState = n.GetJumpState();
         }
     }
     Finish();
 }
Esempio n. 12
0
        public override void RegisterPort()
        {
            register.onExecute = () => {
                if (target.isAssigned)
                {
                    object val = target.Get();
                    if (val == null)
                    {
                        val = new MemberData.Event(target.CreateRuntimeEvent(), null);
                    }
                    if (val is MemberData.Event)
                    {
                        MemberData.Event e = val as MemberData.Event;
                        if (e.eventInfo != null)
                        {
                            if (m_Delegate == null)
                            {
                                if (e.eventInfo is RuntimeEvent)
                                {
                                    var returnType = target.type.GetMethod("Invoke").ReturnType;
                                    m_Delegate = new MemberData.EventCallback((obj => {
                                        if (owner == null)
                                        {
                                            return(null);
                                        }
                                        if (obj != null && parameters.Count == obj.Length)
                                        {
                                            for (int i = 0; i < obj.Length; i++)
                                            {
                                                parameters[i] = obj[i];
                                            }
                                        }
                                        if (returnType == typeof(void))
                                        {
                                            body.InvokeFlow();
                                            return(null);
                                        }
                                        else
                                        {
                                            Node n;
                                            WaitUntil w;
                                            if (!body.ActivateFlowNode(out n, out w))
                                            {
                                                throw new uNodeException("Coroutine aren't supported by EventHook node in runtime.", this);
                                            }
                                            if (n == null)
                                            {
                                                throw new uNodeException("No return value", this);
                                            }
                                            JumpStatement js = n.GetJumpState();
                                            if (js == null || js.jumpType != JumpStatementType.Return || !(js.from is NodeReturn))
                                            {
                                                throw new uNodeException("No return value", this);
                                            }
                                            return((js.from as NodeReturn).GetReturnValue());
                                        }
                                    }));
                                }
                                else
                                {
                                    var method = e.eventInfo.EventHandlerType.GetMethod("Invoke");
                                    var type   = method.ReturnType;
                                    if (type == typeof(void))
                                    {
                                        m_Delegate = CustomDelegate.CreateActionDelegate((obj) => {
                                            if (owner == null)
                                            {
                                                return;
                                            }
                                            if (obj != null && parameters.Count == obj.Length)
                                            {
                                                for (int i = 0; i < obj.Length; i++)
                                                {
                                                    parameters[i] = obj[i];
                                                }
                                            }
                                            body.InvokeFlow();
                                        }, method.GetParameters().Select(i => i.ParameterType).ToArray());
                                    }
                                    else
                                    {
                                        var types = method.GetParameters().Select(i => i.ParameterType).ToList();
                                        types.Add(type);
                                        m_Delegate = CustomDelegate.CreateFuncDelegate((obj) => {
                                            if (owner == null)
                                            {
                                                return(null);
                                            }
                                            if (obj != null && parameters.Count == obj.Length)
                                            {
                                                for (int i = 0; i < obj.Length; i++)
                                                {
                                                    parameters[i] = obj[i];
                                                }
                                            }
                                            Node n;
                                            WaitUntil w;
                                            if (!body.ActivateFlowNode(out n, out w))
                                            {
                                                throw new uNodeException("Coroutine aren't supported by EventHook node in runtime.", this);
                                            }
                                            if (n == null)
                                            {
                                                throw new uNodeException("No return value", this);
                                            }
                                            JumpStatement js = n.GetJumpState();
                                            if (js == null || js.jumpType != JumpStatementType.Return || !(js.from is NodeReturn))
                                            {
                                                throw new uNodeException("No return value", this);
                                            }
                                            return((js.from as NodeReturn).GetReturnValue());
                                        }, types.ToArray());
                                    }
                                    m_Delegate = ReflectionUtils.ConvertDelegate(m_Delegate, e.eventInfo.EventHandlerType);
                                }
                            }
                            e.eventInfo.AddEventHandler(e.instance, m_Delegate);
                        }
                    }
                    else if (val is UnityEventBase)
                    {
                        var method = val.GetType().GetMethod("AddListener");
                        if (m_Delegate == null)
                        {
                            var param = method.GetParameters()[0].ParameterType;
                            var gType = param.GetGenericArguments();
                            m_Delegate = CustomDelegate.CreateActionDelegate((obj) => {
                                if (owner == null)
                                {
                                    return;
                                }
                                if (obj != null && parameters.Count == obj.Length)
                                {
                                    for (int i = 0; i < obj.Length; i++)
                                    {
                                        parameters[i] = obj[i];
                                    }
                                }
                                body.InvokeFlow();
                            }, gType);
                            m_Delegate = System.Delegate.CreateDelegate(param, m_Delegate.Target, m_Delegate.Method);
                        }
                        method.InvokeOptimized(val, new object[] { m_Delegate });
                    }
                    else
                    {
                        if (val == null)
                        {
                            throw new uNodeException("The target event is null", this);
                        }
                        throw new uNodeException("Invalid target value: " + val, this);
                    }
                }
            };
            unregister.onExecute = () => {
                if (m_Delegate != null && target.isAssigned)
                {
                    object val = target.Get();
                    if (val is MemberData.Event)
                    {
                        MemberData.Event e = val as MemberData.Event;
                        if (e.eventInfo != null)
                        {
                            e.eventInfo.RemoveEventHandler(e.instance, m_Delegate);
                        }
                    }
                    else if (val is UnityEventBase)
                    {
                        var method = val.GetType().GetMethod("RemoveListener");
                        method.InvokeOptimized(val, new object[] { m_Delegate });
                    }
                }
            };

            if (CodeGenerator.isGenerating)
            {
                CodeGenerator.RegisterFlowNode(this);
                register.codeGeneration = () => {
                    if (target.type.IsCastableTo(typeof(UnityEventBase)))
                    {
                        return(target.ParseValue().AddFunction("AddListener", GenerateEventCodes()).AddSemicolon());
                    }
                    return(CodeGenerator.GenerateSetCode(target, GenerateEventCodes(), SetType.Add, target.type));
                };
                unregister.codeGeneration = () => {
                    if (target.type.IsCastableTo(typeof(UnityEventBase)))
                    {
                        return(target.ParseValue().AddFunction("RemoveListener", GenerateEventCodes()).AddSemicolon());
                    }
                    return(CodeGenerator.GenerateSetCode(target, GenerateEventCodes(), SetType.Subtract, target.type));
                };
            }
        }