Пример #1
0
        /// <summary>
        /// Executes, returning the coroutine state. You may call <see cref="Execute(object[])"/> if you do not care whether it is a coroutine or not
        /// </summary>
        /// <param name="args"></param>
        /// <returns></returns>
        public CoroutineState ExecuteAsCoroutine(params object[] args)
        {
            Coroutine co = Coroutine.Coroutine;

            if (co.State == CoroutineState.Dead)
            {
                return(CoroutineState.Dead);
            }
            else if (!CheckYieldStatus())
            {
                return(CoroutineState.Suspended);
            }
            else
            {
                //Do coroutine
                DynValue ret = co.Resume(args);
                switch (co.State)
                {
                case CoroutineState.Suspended:

                    if (ret.IsNotNil())
                    {
                        try
                        {
                            Yielder yielder = ret.ToObject <Yielder>();
                            CurYielder = yielder;
                        }
                        catch     //TODO: specific catches to ignore
                        {
                            //Moonsharp does not have a good way of testing the user data type without throwing errors so we have to use try/catch
                        }
                    }
                    return(CoroutineState.Suspended);

                case CoroutineState.Dead:
                    if (AutoResetCoroutine)
                    {
                        ResetCoroutine();
                    }
                    return(CoroutineState.Dead);

                default:
                    //ForceSuspended, Running, etc
                    return(co.State);
                }
            }
        }
        protected virtual CoroutineState ExecuteAsRepeatableCoroutineForUnity(Coroutine co, Action callback, object[] args)
        {
            try
            {
                if (!CheckYieldStatus())
                {
                    return(CoroutineState.Suspended);
                }
                else
                {
                    //Do coroutine
                    DynValue ret;
                    if (!initialized)
                    {
                        initialized = true;
                        ret         = co.Resume(args);
                    }
                    else if (!started)
                    {
                        started = true;
                        //For some reason this is how it needs to be... very cool...
                        ret = co.Resume(new object[] { args });
                    }
                    else
                    {
                        ret = co.Resume();
                    }

                    bool unityCoroutineState = (bool)ScriptRef.Globals[$"{ID}_UNITY_COROUTINE_STATE"];

                    if (unityCoroutineState)
                    {
                        if (ret.IsNotNil())
                        {
                            try
                            {
                                Yielder yielder = ret.ToObject <Yielder>();
                                CurYielder = yielder;
                            }
                            catch
                            {
                                //Throw exception?
                            }
                        }
                        return(CoroutineState.Suspended);
                    }
                    else
                    {
                        callback?.Invoke();
                        started = false;
                        return(CoroutineState.Dead);
                    }
                }
            }
            catch (Exception ex)
            {
                if (ex is InterpreterException ex2)
                {
                    //For unity
                    throw new Exception(ex2.DecoratedMessage, ex);
                }
                else
                {
                    throw ex;
                }
            }
        }
Пример #3
0
        protected virtual CoroutineState ExecuteAsCoroutineForUnity(Coroutine co, Action callback, object[] args)
        {
            try
            {
                if (co.State == CoroutineState.Dead)
                {
                    return(CoroutineState.Dead);
                }
                else if (!CheckYieldStatus())
                {
                    return(CoroutineState.Suspended);
                }
                else
                {
                    //Do coroutine
                    DynValue ret;
                    if (co.State == CoroutineState.NotStarted)
                    {
                        ret = co.Resume(args);
                    }
                    else
                    {
                        ret = co.Resume();
                    }

                    switch (co.State)
                    {
                    case CoroutineState.Suspended:

                        if (ret.IsNotNil())
                        {
                            try
                            {
                                Yielder yielder = ret.ToObject <Yielder>();
                                CurYielder = yielder;
                            }
                            catch
                            {
                            }
                        }
                        return(CoroutineState.Suspended);

                    case CoroutineState.Dead:
                        callback?.Invoke();
                        if (AutoResetCoroutine)
                        {
                            ResetCoroutine();
                        }
                        return(CoroutineState.Dead);

                    default:
                        //ForceSuspended, Running, etc
                        return(co.State);
                    }
                }
            }
            catch (Exception ex)
            {
                if (ex is InterpreterException ex2)
                {
                    //For unity
                    throw new Exception(ex2.DecoratedMessage, ex);
                }
                else
                {
                    throw ex;
                }
            }
        }
Пример #4
0
 public void SetYielder(Yielder yielder)
 {
     CurYielder = yielder;
 }