public static RadicalCoroutine InvokeRepeatingRadical(this MonoBehaviour behaviour, System.Action method, float delay, float repeatRate, ITimeSupplier time = null, RadicalCoroutineDisableMode disableMode = RadicalCoroutineDisableMode.CancelOnDisable)
        {
            if (behaviour == null) throw new System.ArgumentNullException("behaviour");
            if (method == null) throw new System.ArgumentNullException("method");

            return StartRadicalCoroutine(behaviour, RadicalInvokeRedirect(method, delay, repeatRate, time), disableMode);
        }
        public static RadicalCoroutine InvokeAfterYield(this MonoBehaviour behaviour, System.Action method, object yieldInstruction, RadicalCoroutineDisableMode disableMode = RadicalCoroutineDisableMode.CancelOnDisable)
        {
            if (behaviour == null) throw new System.ArgumentNullException("behaviour");
            if (method == null) throw new System.ArgumentNullException("method");

            return StartRadicalCoroutine(behaviour, InvokeAfterYieldRedirect(method, yieldInstruction));
        }
        public RadicalCoroutine StartRadicalCoroutine(System.Collections.IEnumerator routine, RadicalCoroutineDisableMode disableMode = RadicalCoroutineDisableMode.Default)
        {
            if (routine == null) throw new System.ArgumentNullException("routine");

            var co = new RadicalCoroutine(routine);
            co.Start(this, disableMode);
            return co;
        }
 public RadicalState(MonoBehaviour handle, RadicalCoroutineDisableMode disableMode = RadicalCoroutineDisableMode.Default)
 {
     if (object.ReferenceEquals(handle, null))
     {
         throw new System.ArgumentNullException("handle");
     }
     _handle      = handle;
     _disableMode = disableMode;
 }
        public static RadicalCoroutine StartRadicalCoroutine(this MonoBehaviour behaviour, System.Func<System.Collections.IEnumerator> method, RadicalCoroutineDisableMode disableMode = RadicalCoroutineDisableMode.Default)
        {
            if (behaviour == null) throw new System.ArgumentNullException("behaviour");
            if (method == null) throw new System.ArgumentNullException("routine");

            var co = new RadicalCoroutine(method());
            co.Start(behaviour, disableMode);
            return co;
        }
        public static RadicalCoroutine StartRadicalCoroutine(this MonoBehaviour behaviour, System.Delegate method, object[] args = null, RadicalCoroutineDisableMode disableMode = RadicalCoroutineDisableMode.Default)
        {
            if (behaviour == null) throw new System.ArgumentNullException("behaviour");
            if (method == null) throw new System.ArgumentNullException("method");

            System.Collections.IEnumerator e;
            if (com.spacepuppy.Utils.TypeUtil.IsType(method.Method.ReturnType, typeof(System.Collections.IEnumerable)))
            {
                e = (method.DynamicInvoke(args) as System.Collections.IEnumerable).GetEnumerator();
            }
            else if (com.spacepuppy.Utils.TypeUtil.IsType(method.Method.ReturnType, typeof(System.Collections.IEnumerator)))
            {
                e = (method.DynamicInvoke(args) as System.Collections.IEnumerator);
            }
            else
            {
                throw new System.ArgumentException("Delegate must have a return type of IEnumerable or IEnumerator.", "method");
            }

            var co = new RadicalCoroutine(e);
            co.Start(behaviour, disableMode);
            return co;
        }
        /// <summary>
        /// Starts the coroutine, one should always call this method or the StartRadicalCoroutine extension method. Never pass the RadicalCoroutine into the 'StartCoroutine' method.
        /// </summary>
        /// <param name="behaviour">A reference to the MonoBehaviour that should be handling the coroutine.</param>
        /// <param name="disableMode">A disableMode other than Default is only supported if the behaviour is an SPComponent.</param>
        /// <remarks>
        /// Disable modes allow you to decide how the coroutine is dealt with when the component/gameobject are disabled. Note that 'CancelOnDeactivate' is a specical case flag, 
        /// it only takes effect if NO OTHER flag is set (it's a 0 flag actually). What this results in is that Deactivate and Disable are pausible... but a routine can only play 
        /// through Disable, not Deactivate. This is due to the default behaviour of coroutine in unity. In default mode, coroutines continue playing when a component gets disabled, 
        /// but when deactivated the coroutine gets cancelled. This means we can not play through a deactivation.
        /// </remarks>
        public void Start(MonoBehaviour behaviour, RadicalCoroutineDisableMode disableMode = RadicalCoroutineDisableMode.Default)
        {
            if (_state != RadicalCoroutineOperatingState.Inactive) throw new System.InvalidOperationException("Failed to start RadicalCoroutine. The Coroutine is already being processed.");
            if (behaviour == null) throw new System.ArgumentNullException("behaviour");

            _state = RadicalCoroutineOperatingState.Active;
            _owner = behaviour;
            _token = behaviour.StartCoroutine(this);

            _disableMode = disableMode;
            if (_disableMode > RadicalCoroutineDisableMode.Default && _disableMode != RadicalCoroutineDisableMode.ResumeOnEnable)
            {
                //no point in managing the routine if it acts in default mode... a flag of 'ResumeOnEnable' is a redundant mode to default
                var manager = behaviour.AddOrGetComponent<RadicalCoroutineManager>();
                manager.RegisterCoroutine(behaviour, this);
            }

            if (_stack.Count > 0 && _stack.Peek() is IPausibleYieldInstruction) (_stack.Peek() as IPausibleYieldInstruction).OnResume();
        }
        public void Schedule(MonoBehaviour behaviour, RadicalCoroutine routine, RadicalCoroutineDisableMode disableMode = RadicalCoroutineDisableMode.Default)
        {
            if (behaviour == null) throw new System.ArgumentNullException("behaviour");
            if (routine == null) throw new System.ArgumentNullException("routine");

            this.OnComplete += (s, e) =>
            {
                if (routine._state == RadicalCoroutineOperatingState.Inactive) routine.Start(behaviour, disableMode);
            };
        }
        public RadicalCoroutine Schedule(MonoBehaviour behaviour, CoroutineMethod routine, RadicalCoroutineDisableMode disableMode = RadicalCoroutineDisableMode.Default)
        {
            if (behaviour == null) throw new System.ArgumentNullException("behaviour");
            if (routine == null) throw new System.ArgumentNullException("routine");

            var co = new RadicalCoroutine(routine().GetEnumerator());
            this.OnComplete += (s, e) =>
            {
                if (co._state == RadicalCoroutineOperatingState.Inactive) co.Start(behaviour, disableMode);
            };
            return co;
        }
        public void Dispose()
        {
            _state = RadicalCoroutineOperatingState.Inactive;
            _disableMode = RadicalCoroutineDisableMode.Default;
            if (_stack != null) _stack.Clear();
            _currentIEnumeratorYieldValue = null;
            _forcedTick = false;
            this.OnComplete = null;
            this.OnCancelled = null;
            this.OnFinished = null;

            //TODO - #100 - allow releasing when we've fully implemented coroutine object caching 
            //_pool.Release(this);
        }
        public static RadicalCoroutine InvokeAfterYield(this MonoBehaviour behaviour, System.Action method, object yieldInstruction, RadicalCoroutineDisableMode disableMode = RadicalCoroutineDisableMode.CancelOnDisable)
        {
            if (behaviour == null)
            {
                throw new System.ArgumentNullException("behaviour");
            }
            if (method == null)
            {
                throw new System.ArgumentNullException("method");
            }

            return(StartRadicalCoroutine(behaviour, InvokeAfterYieldRedirect(method, yieldInstruction)));
        }
        public static RadicalCoroutine StartRadicalCoroutineAsync(this MonoBehaviour behaviour, System.Delegate method, object[] args = null, RadicalCoroutineDisableMode disableMode = RadicalCoroutineDisableMode.Default)
        {
            if (behaviour == null)
            {
                throw new System.ArgumentNullException("behaviour");
            }
            if (method == null)
            {
                throw new System.ArgumentNullException("method");
            }

            System.Collections.IEnumerator e;
            if (com.spacepuppy.Utils.TypeUtil.IsType(method.Method.ReturnType, typeof(System.Collections.IEnumerable)))
            {
                e = (method.DynamicInvoke(args) as System.Collections.IEnumerable).GetEnumerator();
            }
            else if (com.spacepuppy.Utils.TypeUtil.IsType(method.Method.ReturnType, typeof(System.Collections.IEnumerator)))
            {
                e = (method.DynamicInvoke(args) as System.Collections.IEnumerator);
            }
            else
            {
                throw new System.ArgumentException("Delegate must have a return type of IEnumerable or IEnumerator.", "method");
            }

            var co = new RadicalCoroutine(e);

            co.StartAsync(behaviour, disableMode);
            return(co);
        }
        public RadicalCoroutine InvokeAfterYield(System.Action method, object yieldInstruction, RadicalCoroutineDisableMode disableMode = RadicalCoroutineDisableMode.CancelOnDisable)
        {
            if (method == null)
            {
                throw new System.ArgumentNullException("method");
            }

            return(this.StartRadicalCoroutine(CoroutineUtil.InvokeAfterYieldRedirect(method, yieldInstruction), disableMode));
        }
        public RadicalCoroutine StartRadicalCoroutine(CoroutineMethod routine, RadicalCoroutineDisableMode disableMode = RadicalCoroutineDisableMode.Default)
        {
            if (routine == null) throw new System.ArgumentNullException("routine");

            var co = new RadicalCoroutine(routine().GetEnumerator());
            co.Start(this, disableMode);
            return co;
        }
        public static RadicalCoroutine StartValidatedRadicalCoroutine(this MonoBehaviour behaviour, System.Collections.IEnumerator routine, System.Func <bool> validator, RadicalCoroutineDisableMode disableMode = RadicalCoroutineDisableMode.Default)
        {
            if (behaviour == null)
            {
                throw new System.ArgumentNullException("behaviour");
            }
            if (routine == null)
            {
                throw new System.ArgumentNullException("routine");
            }
            if (validator == null)
            {
                throw new System.ArgumentNullException("validator");
            }

            var co = new RadicalCoroutine(ValidatedRoutine(routine, validator));

            co.Start(behaviour, disableMode);
            return(co);
        }
        public static RadicalCoroutine StartRadicalCoroutineAsync(this MonoBehaviour behaviour, System.Collections.IEnumerable routine, RadicalCoroutineDisableMode disableMode = RadicalCoroutineDisableMode.Default)
        {
            if (behaviour == null) throw new System.ArgumentNullException("behaviour");
            if (routine == null) throw new System.ArgumentNullException("routine");

            var co = new RadicalCoroutine(routine.GetEnumerator());
            co.StartAsync(behaviour, disableMode);
            return co;
        }
        public RadicalCoroutine StartRadicalCoroutineAsync(System.Func <System.Collections.IEnumerator> routine, RadicalCoroutineDisableMode disableMode = RadicalCoroutineDisableMode.Default)
        {
            if (routine == null)
            {
                throw new System.ArgumentNullException("routine");
            }

            var co = new RadicalCoroutine(routine());

            co.StartAsync(this, disableMode);
            return(co);
        }
        public RadicalCoroutine InvokeAfterYield(System.Action method, object yieldInstruction, RadicalCoroutineDisableMode disableMode = RadicalCoroutineDisableMode.CancelOnDisable)
        {
            if (method == null) throw new System.ArgumentNullException("method");

            return this.StartRadicalCoroutine(CoroutineUtil.InvokeAfterYieldRedirect(method, yieldInstruction), disableMode);
        }
        public RadicalCoroutine InvokeRepeatingRadical(System.Action method, float delay, float repeatRate, ITimeSupplier time = null, RadicalCoroutineDisableMode disableMode = RadicalCoroutineDisableMode.Default)
        {
            if (method == null)
            {
                throw new System.ArgumentNullException("method");
            }

            return(this.StartRadicalCoroutine(CoroutineUtil.RadicalInvokeRedirect(method, delay, repeatRate, time), disableMode));
        }
        public RadicalCoroutine InvokeRepeatingRadical(System.Action method, float delay, float repeatRate, RadicalCoroutineDisableMode disableMode = RadicalCoroutineDisableMode.Default)
        {
            if (method == null) throw new System.ArgumentNullException("method");

            return this.StartRadicalCoroutine(CoroutineUtil.RadicalInvokeRedirect(method, delay), disableMode);
        }
        public static RadicalCoroutine StartRadicalCoroutineAsync(this MonoBehaviour behaviour, System.Collections.IEnumerable routine, RadicalCoroutineDisableMode disableMode = RadicalCoroutineDisableMode.Default)
        {
            if (behaviour == null)
            {
                throw new System.ArgumentNullException("behaviour");
            }
            if (routine == null)
            {
                throw new System.ArgumentNullException("routine");
            }

            var co = new RadicalCoroutine(routine.GetEnumerator());

            co.StartAsync(behaviour, disableMode);
            return(co);
        }
        public RadicalCoroutine InvokeRadical(System.Action method, float delay, ITimeSupplier time = null, RadicalCoroutineDisableMode disableMode = RadicalCoroutineDisableMode.Default)
        {
            if (method == null) throw new System.ArgumentNullException("method");

            return this.StartRadicalCoroutine(CoroutineUtil.RadicalInvokeRedirect(method, delay, -1f, time), disableMode);
        }
        public static RadicalCoroutine InvokeRepeatingRadical(this MonoBehaviour behaviour, System.Action method, float delay, float repeatRate, ITimeSupplier time = null, RadicalCoroutineDisableMode disableMode = RadicalCoroutineDisableMode.CancelOnDisable)
        {
            if (behaviour == null)
            {
                throw new System.ArgumentNullException("behaviour");
            }
            if (method == null)
            {
                throw new System.ArgumentNullException("method");
            }

            return(StartRadicalCoroutine(behaviour, RadicalInvokeRedirect(method, delay, repeatRate, time), disableMode));
        }
Esempio n. 24
0
        public static RadicalCoroutine StartAutoKillRadicalCoroutine(this MonoBehaviour behaviour, System.Collections.IEnumerator routine, object autoKillToken, RadicalCoroutineDisableMode disableMode = RadicalCoroutineDisableMode.Default)
        {
            if (behaviour == null)
            {
                throw new System.ArgumentNullException("behaviour");
            }
            if (routine == null)
            {
                throw new System.ArgumentNullException("routine");
            }

            var co = new RadicalCoroutine(routine);

            co.StartAutoKill(behaviour, autoKillToken, disableMode);
            return(co);
        }
        public static RadicalCoroutine StartRadicalCoroutine(this MonoBehaviour behaviour, System.Func <System.Collections.IEnumerator> method, RadicalCoroutineDisableMode disableMode = RadicalCoroutineDisableMode.Default)
        {
            if (behaviour == null)
            {
                throw new System.ArgumentNullException("behaviour");
            }
            if (method == null)
            {
                throw new System.ArgumentNullException("routine");
            }

            var co = new RadicalCoroutine(method());

            co.Start(behaviour, disableMode);
            return(co);
        }
        public void StartAsync(MonoBehaviour behaviour, RadicalCoroutineDisableMode disableMode = RadicalCoroutineDisableMode.Default)
        {
            if (_state != RadicalCoroutineOperatingState.Inactive) throw new System.InvalidOperationException("Failed to start RadicalCoroutine. The Coroutine is already being processed.");
            if (behaviour == null) throw new System.ArgumentNullException("behaviour");

            _state = RadicalCoroutineOperatingState.Active;
            _owner = behaviour;
            _disableMode = disableMode;

            if (_stack.CurrentOperation is IPausibleYieldInstruction) (_stack.CurrentOperation as IPausibleYieldInstruction).OnResume();
            _stack.Push(com.spacepuppy.Async.RadicalTask.Create(this)); //we start the task as an async operation

#if SP_LIB
            var manager = behaviour.AddOrGetComponent<RadicalCoroutineManager>();
#else
            var manager = behaviour.GetComponent<RadicalCoroutineManager>();
            if (manager == null) manager = behaviour.gameObject.AddComponent<RadicalCoroutineManager>();
#endif
            _manager = manager;
            _manager.RegisterCoroutine(this);
            _token = behaviour.StartCoroutine(this);

        }