using System; using System.Collections; using UnityEngine; using UnityEngine.Networking; namespace SUISS.Scheduling { public class CoYield : IYieldStatement { protected CoYield() { this._type = CoYield.YieldType.NullYield; } protected CoYield(CoYield.YieldType type, object yieldable) { this._type = type; this._yieldable = yieldable; } public static IYieldStatement On(UnityWebRequest webRequest) { return new CoYield(CoYield.YieldType.WWW, webRequest); } public static IYieldStatement On(double suissYield) { return new CoYield(CoYield.YieldType.SUISSYield, suissYield); } public static IYieldStatement On(float waitSeconds) { return new CoYield(CoYield.YieldType.UnityYield, new WaitForSeconds(waitSeconds)); } public static IYieldStatement On(YieldInstruction unityYield) { return new CoYield(CoYield.YieldType.UnityYield, unityYield); } public static IYieldStatement On(IEnumerator busyEnumerator) { return new CoYield(CoYield.YieldType.Enumerator, busyEnumerator); } public static IYieldStatement NextFrame() { return new CoYield(); } public object UnityYieldable(MonoBehaviour runner) { switch (this._type) { case CoYield.YieldType.Enumerator: { if (runner == null) { UnityEngine.Debug.LogError("Error! Cannot use busy IEnumerator a unity yield statement!"); throw new Exception("Error! Cannot use busy IEnumerator a unity yield statement!"); } IEnumerator enumerator = this._yieldable as IEnumerator; if (enumerator == null) { UnityEngine.Debug.LogError("Busy IEnumerator was null. This is unexpected!"); throw new Exception("Busy IEnumerator was null. This is unexpected!"); } return runner.StartCoroutine(enumerator); } case CoYield.YieldType.WWW: case CoYield.YieldType.UnityYield: if (this._yieldable == null) { UnityEngine.Debug.LogError("A unity yield object was null. This is unexpected!"); throw new Exception("A unity yield object was null. This is unexpected!"); } return this._yieldable; case CoYield.YieldType.SUISSYield: UnityEngine.Debug.LogWarning("A SUISS yield instruction is being passed to the Unity Scheduler. You probably don't want this!"); return null; default: return null; } } public object UnityYieldable() { return this.UnityYieldable(null); } public object SUISSYieldable() { switch (this._type) { case CoYield.YieldType.Enumerator: if (!(this._yieldable is IEnumerator)) { UnityEngine.Debug.LogError("Busy IEnumerator was null. This is unexpected!"); throw new Exception("Busy IEnumerator was null. This is unexpected!"); } return this._yieldable; case CoYield.YieldType.WWW: case CoYield.YieldType.UnityYield: case CoYield.YieldType.SUISSYield: if (this._yieldable == null) { UnityEngine.Debug.LogError("A yield object was null. This is unexpected!"); throw new Exception("A yield object was null. This is unexpected!"); } return this._yieldable; default: return null; } } private object _yieldable; private CoYield.YieldType _type; protected enum YieldType { Enumerator, WWW, UnityYield, SUISSYield, NullYield } } }