/// <summary>
        /// Executes CoroutineProcessor  immediately
        /// </summary>
        /// <param name="target">object, for tracking coroutine</param>
        /// <param name="processor">CoroutineProcessor to execute</param>
        /// <param name="handlerError">>event, which is gonna call on any exception in coroutine execution process</param>
        /// <exception cref="ArgumentNullException"></exception>
        public virtual void StartProcessorImmediate(object target, ICoroutineProcessor processor, Action <object, Exception> handlerError = null)
        {
            if (target == null)
            {
                throw new ArgumentNullException(nameof(target));
            }

            if (processor == null)
            {
                throw new ArgumentNullException(nameof(processor));
            }

            try
            {
                while (!processor.IsCompleted)
                {
                    processor.Update();
                }
            }
            catch (Exception exception)
            {
                if (handlerError != null)
                {
                    handlerError(target, exception);
                    return;
                }

                throw;
            }
        }
示例#2
0
        public static CoroutineBuilder BuildDelayed(this ICoroutineProcessor processor, float delaySeconds)
        {
            var result = new CoroutineBuilder(processor);

            result.WaitForSeconds(delaySeconds);

            return(result);
        }
示例#3
0
        public static CoroutineBuilder Build(this ICoroutineProcessor processor, Action logic)
        {
            var result = new CoroutineBuilder(processor);

            result.Then(logic);

            return(result);
        }
示例#4
0
        /// <summary>
        /// Updates coroutine execution process
        /// </summary>
        /// <exception cref="UnityYieldInstructionNotSupportedException"></exception>
        /// <exception cref="UsingCoroutineProcessorInYieldingException"></exception>
        public virtual void Update()
        {
            int loopCount = 0;

            while (true)
            {
                if (IsCompleted || _state == CoroutineState.Paused)
                {
                    break;
                }

                if (_state == CoroutineState.NotStarted)
                {
                    _state = CoroutineState.InProgress;
                }

                if (_innerCoroutineProcessor != null)
                {
                    _innerCoroutineProcessor.Update();

                    if (_innerCoroutineProcessor.IsCompleted)
                    {
                        _innerCoroutineProcessor = null;
                        loopCount++;
                        continue;
                    }
                    else
                    {
                        break;
                    }
                }

                if (_enumerator.MoveNext())
                {
                    _innerCoroutineProcessor = CoroutineProcessorFactory(_enumerator.Current, loopCount);

                    if (_innerCoroutineProcessor != null)
                    {
                        _innerCoroutineProcessor.Update();

                        if (_innerCoroutineProcessor.IsCompleted)
                        {
                            _innerCoroutineProcessor = null;
                            loopCount++;
                            continue;
                        }
                    }
                }
                else
                {
                    State = CoroutineState.Completed;
                }

                break;
            }
        }
示例#5
0
        public static CoroutineBuilder BuildDelayed <TContext>(this ICoroutineProcessor processor, float delaySeconds,
                                                               TContext context = null) where TContext : class, new()
        {
            if (context == null)
            {
                context = new TContext();
            }
            var result = new CoroutineBuilder <TContext>(processor, context);

            result.WaitForSeconds(delaySeconds);

            return(result);
        }
示例#6
0
        public static CoroutineBuilder Build <TContext>(this ICoroutineProcessor processor, Action logic,
                                                        TContext context = null) where TContext : class, new()
        {
            if (context == null)
            {
                context = new TContext();
            }

            var result = new CoroutineBuilder <TContext>(processor, context);

            result.Then(logic);

            return(result);
        }
示例#7
0
        public void StartProcessorImmediate(object target, ICoroutineProcessor processor, Action <object, Exception> handlerError = null)
        {
            try
            {
                while (!processor.IsCompleted)
                {
                    processor.Update();
                }
            }
            catch (Exception exception)
            {
                if (handlerError != null)
                {
                    handlerError(target, exception);
                    return;
                }

                throw;
            }
        }
示例#8
0
        protected virtual ICoroutineProcessor CoroutineProcessorFactory(object current, int loopCount)
        {
            ICoroutineProcessor res = null;

            if (current is CoroutineSystemWaitForSeconds)
            {
                float a = loopCount > 0 ? _timeProvider.deltaTime : 0f;
                res = new WaitForSecondsCoroutineProcessor(_timeProvider, (( CoroutineSystemWaitForSeconds )current).Seconds + a);
            }
            else if (current is CustomYieldInstruction)
            {
                res = new UnityCustomYieldCoroutineProcessor(( CustomYieldInstruction )current);
            }
            else if (current is IEnumerator)
            {
                res = new EnumeratorCoroutineProcessor(_timeProvider, ( IEnumerator )current);
            }
            else if (current is AsyncOperation)
            {
                res = new AsyncOperationProcessor(( AsyncOperation )current);
            }
            else if (current is WaitForSeconds)
            {
                throw new UnityYieldInstructionNotSupportedException(( YieldInstruction )current, typeof(CoroutineSystemWaitForSeconds));
            }
            else if (current is YieldInstruction)
            {
                throw new UnityYieldInstructionNotSupportedException(( YieldInstruction )current);
            }
            else if (current is ICoroutineProcessor)
            {
                throw new UsingCoroutineProcessorInYieldingException();
            }

            return(res);
        }
 public SceneTransitorController(ICoroutineProcessor coroutineProcessor)
 {
     _coroutineProcessor = coroutineProcessor;
 }
示例#10
0
        public ICoroutineProcessorPausable StartCoroutineBefore(object target, IEnumerator enumerator, ICoroutineProcessor before, Action <object, Exception> handlerError = null)
        {
            if (_timeProvider == null)
            {
                throw new NullReferenceException("TimeProvider");
            }

            if (target == null)
            {
                throw new NullReferenceException("parameter 'target'");
            }

            var extendedCoroutine = CreateEnumeratorCoroutine(enumerator);

            LinkedListNode <Entry> beforeNode = null;

            foreach (var entry in _coroutines)
            {
                if (entry.CoroutineProcessor == before)
                {
                    beforeNode = _coroutines.Find(entry);
                    break;
                }
            }

            if (beforeNode == null)
            {
                throw new ArgumentException("There's no such element in coroutines list", "before");
            }

            _coroutines.AddBefore(beforeNode, new Entry(target, enumerator, extendedCoroutine, handlerError));
            return(extendedCoroutine);
        }
        /// <summary>
        ///  Starts coroutine before any another coroutine
        /// </summary>
        /// <param name="target">object, for tracking coroutine</param>
        /// <param name="enumerator">coroutine to start</param>
        /// <param name="before">starts enumerator before it</param>
        /// <param name="handlerError">event, which is gonna call on any exception in coroutine execution process</param>
        /// <returns>returns created CoroutineProcessor</returns>
        /// <exception cref="ArgumentNullException"></exception>
        public virtual ICoroutineProcessorPausable StartCoroutineBefore(object target, IEnumerator enumerator, ICoroutineProcessor before, Action <object, Exception> handlerError = null)
        {
            CheckCommonValuesForNullState(target);

            var extendedCoroutine = CreateEnumeratorCoroutine(enumerator);

            LinkedListNode <Entry> beforeNode = null;

            foreach (var entry in _coroutines)
            {
                if (entry.CoroutineProcessor == before)
                {
                    beforeNode = _coroutines.Find(entry);
                    break;
                }
            }

            if (beforeNode == null)
            {
                throw new ArgumentNullException(nameof(beforeNode));
            }

            _coroutines.AddBefore(beforeNode, new Entry(target, enumerator, extendedCoroutine, handlerError));
            return(extendedCoroutine);
        }
示例#12
0
 public CoroutineBuilder(ICoroutineProcessor processor, TContext context) : base(processor)
 {
     _context = context;
 }
示例#13
0
        public void Update()
        {
            int loopCount = 0;

            while (true)
            {
                if (IsCompleted || _state == CoroutineState.Paused)
                {
                    break;
                }

                if (_state == CoroutineState.NotStarted)
                {
                    _state = CoroutineState.InProgress;
                }

                if (_innerCoroutineProcessor != null)
                {
                    _innerCoroutineProcessor.Update();

                    if (_innerCoroutineProcessor.IsCompleted)
                    {
                        _innerCoroutineProcessor = null;
                        loopCount++;
                        continue;
                    }
                    else
                    {
                        break;
                    }
                }

                if (_enumerator.MoveNext())
                {
                    var current = _enumerator.Current;
                    if (current is CoroutineSystemWaitForSeconds)
                    {
                        float a = loopCount > 0 ? _timeProvider.deltaTime : 0f;
                        _innerCoroutineProcessor = new WaitForSecondsCoroutineProcessor(_timeProvider, (( CoroutineSystemWaitForSeconds )current).Seconds + a);
                    }
                    else if (current is CustomYieldInstruction)
                    {
                        _innerCoroutineProcessor = new UnityCustomYieldCoroutineProcessor(( CustomYieldInstruction )current);
                    }
                    else if (current is IEnumerator)
                    {
                        _innerCoroutineProcessor = new EnumeratorCoroutineProcessor(_timeProvider, ( IEnumerator )current);
                    }
                    else if (current is AsyncOperation)
                    {
                        _innerCoroutineProcessor = new AsyncOperationProcessor(( AsyncOperation )current);
                    }
                    else if (current is WaitForSeconds)
                    {
                        throw new UnityYieldInstructionNotSupportedException(( YieldInstruction )current, typeof(CoroutineSystemWaitForSeconds));
                    }
                    else if (current is YieldInstruction)
                    {
                        throw new UnityYieldInstructionNotSupportedException(( YieldInstruction )current);
                    }
                    else if (current is ICoroutineProcessor)
                    {
                        throw new UsingCoroutineProcessorInYieldingException();
                    }

                    if (_innerCoroutineProcessor != null)
                    {
                        _innerCoroutineProcessor.Update();

                        if (_innerCoroutineProcessor.IsCompleted)
                        {
                            _innerCoroutineProcessor = null;
                            loopCount++;
                            continue;
                        }
                    }
                }
                else
                {
                    State = CoroutineState.Completed;
                }

                break;
            }
        }