private void Run(TimerAction timerAction) { switch (timerAction.TimerClass) { case TimerClass.OnceTimer: { ETTask <bool> tcs = timerAction.Object as ETTask <bool>; this.Remove(timerAction.Id); tcs.SetResult(true); break; } case TimerClass.RepeatedTimer: { int type = timerAction.Type; long tillTime = TimeHelper.ServerNow() + timerAction.Time; this.AddTimer(tillTime, timerAction); ITimer timer = this.timerActions[type]; if (timer == null) { Log.Error($"not found timer action: {type}"); return; } timer.Handle(timerAction.Object); break; } } }
public async ETTask <bool> WaitTillAsync(long tillTime, ETCancellationToken cancellationToken = null) { if (TimeHelper.ServerNow() >= tillTime) { return(true); } ETTask <bool> tcs = ETTask <bool> .Create(true); TimerAction timer = this.AddChild <TimerAction, TimerClass, long, object>(TimerClass.OnceWaitTimer, 0, tcs, true); this.AddTimer(tillTime, timer); long timerId = timer.Id; void CancelAction() { if (this.Remove(timerId)) { tcs.SetResult(false); } } bool ret; try { cancellationToken?.Add(CancelAction); ret = await tcs; } finally { cancellationToken?.Remove(CancelAction); } return(ret); }
async static ETTask <int> ShowMsgBoxView(this UIUpdateView self, string content, string confirmBtnText, string cancelBtnText) { ETTask <int> tcs = ETTask <int> .Create(); Action confirmBtnFunc = () => { tcs.SetResult(self.BTN_CONFIRM); }; Action cancelBtnFunc = () => { tcs.SetResult(self.BTN_CANCEL); }; I18NComponent.Instance.I18NTryGetText(content, out self.para.Content); I18NComponent.Instance.I18NTryGetText(confirmBtnText, out self.para.ConfirmText); I18NComponent.Instance.I18NTryGetText(cancelBtnText, out self.para.CancelText); self.para.ConfirmCallback = confirmBtnFunc; self.para.CancelCallback = cancelBtnFunc; await UIManagerComponent.Instance.OpenWindow <UIMsgBoxWin, UIMsgBoxWin.MsgBoxPara>(UIMsgBoxWin.PrefabPath, self.para, UILayerNames.TipLayer); var result = await tcs; await UIManagerComponent.Instance.CloseWindow <UIMsgBoxWin>(); return(result); }
private void Run(TimerAction timerAction) { switch (timerAction.TimerClass) { case TimerClass.OnceWaitTimer: { ETTask <bool> tcs = timerAction.Callback as ETTask <bool>; this.Remove(timerAction.Id); tcs.SetResult(true); break; } case TimerClass.OnceTimer: { Action action = timerAction.Callback as Action; this.Remove(timerAction.Id); action?.Invoke(); break; } case TimerClass.RepeatedTimer: { Action action = timerAction.Callback as Action; long tillTime = TimeHelper.ServerNow() + timerAction.Time; this.AddTimer(tillTime, timerAction); action?.Invoke(); break; } } }
public static async ETTask <IActorResponse> Call( this ActorMessageSenderComponent self, long actorId, int rpcId, MemoryStream memoryStream, bool needException = true ) { if (actorId == 0) { throw new Exception($"actor id is 0: {memoryStream.ToActorMessage()}"); } var tcs = ETTask <IActorResponse> .Create(true); self.requestCallback.Add(rpcId, new ActorMessageSender(actorId, memoryStream, tcs, needException)); self.Send(actorId, memoryStream); long beginTime = TimeHelper.ServerFrameTime(); IActorResponse response = await tcs; long endTime = TimeHelper.ServerFrameTime(); long costTime = endTime - beginTime; if (costTime > 200) { Log.Warning("actor rpc time > 200: {0} {1}", costTime, memoryStream.ToActorMessage()); } return(response); }
public void Add(ETTask <CoroutineLock> tcs, int time) { this.queue.Enqueue(new CoroutineLockInfo() { Tcs = tcs, Time = time }); }
public static void Add(this CoroutineLockQueue self, ETTask <CoroutineLock> tcs, int time) { self.queue.Enqueue(new CoroutineLockInfo() { Tcs = tcs, Time = time }); }
// 有了这个方法,就可以直接await Unity的AsyncOperation了 public static async ETTask GetAwaiter(this AsyncOperation asyncOperation) { ETTask task = ETTask.Create(true); asyncOperation.completed += _ => { task.SetResult(); }; await task; }
public async ETTask WaitAsync() { --this.count; if (this.count < 0) { return; } if (this.count == 0) { List <ETTask> t = this.tcss; this.tcss = null; foreach (ETTask ttcs in t) { ttcs.SetResult(); } return; } ETTask tcs = ETTask.Create(true); tcss.Add(tcs); await tcs; }
private static void Run(this TimerComponent self, TimerAction timerAction) { switch (timerAction.TimerClass) { case TimerClass.OnceTimer: { int type = timerAction.Type; Game.EventSystem.Callback(type, timerAction.Object); break; } case TimerClass.OnceWaitTimer: { ETTask <bool> tcs = timerAction.Object as ETTask <bool>; self.Remove(timerAction.Id); tcs.SetResult(true); break; } case TimerClass.RepeatedTimer: { int type = timerAction.Type; long tillTime = TimeHelper.ServerNow() + timerAction.Time; self.AddTimer(tillTime, timerAction); Game.EventSystem.Callback(type, timerAction.Object); break; } } }
static CanceledETTaskCache() { ETTaskCompletionSource tcs = new ETTaskCompletionSource(); tcs.TrySetCanceled(); Task = tcs.Task; }
public void SetResult(K k) { var t = tcs; this.tcs = null; t.SetResult(k); }
public static async ETTask <bool> WaitTillAsync(this TimerComponent self, long tillTime, ETCancellationToken cancellationToken = null) { if (self.timeNow >= tillTime) { return(true); } ETTask <bool> tcs = ETTask <bool> .Create(true); TimerAction timer = self.AddChild <TimerAction, TimerClass, long, int, object>(TimerClass.OnceWaitTimer, tillTime - self.timeNow, 0, tcs, true); self.AddTimer(tillTime, timer); long timerId = timer.Id; void CancelAction() { if (self.Remove(timerId)) { tcs.SetResult(false); } } bool ret; try { cancellationToken?.Add(CancelAction); ret = await tcs; } finally { cancellationToken?.Remove(CancelAction); } return(ret); }
public override void Awake(GalGameEngineComponent self) { GalGameEngineComponent.Instance = self; self.CancelToken = new ETCancellationToken(); self.Speed = GalGameEngineComponent.NormalSpeed; self.StageRoleMap = new Dictionary <string, string>(); self.RoleExpressionMap = new Dictionary <string, string>(); self.RoleExpressionPathMap = new Dictionary <string, Dictionary <string, string> >(); self.WaitInput = ETTask <KeyCode> .Create(); self.ReviewItems = new List <GalGameEngineComponent.ReviewItem>(); for (int i = 0; i < RoleExpressionConfigCategory.Instance.GetAllList().Count; i++) { var item = RoleExpressionConfigCategory.Instance.GetAllList()[i]; if (!self.RoleExpressionPathMap.ContainsKey(item.NameKey)) { self.RoleExpressionPathMap.Add(item.NameKey, new Dictionary <string, string>()); } self.RoleExpressionPathMap[item.NameKey].Add(item.Expression, item.Path); } self.StagePosMap = new Dictionary <string, Vector3>(); for (int i = 0; i < StagePosConfigCategory.Instance.GetAllList().Count; i++) { var item = StagePosConfigCategory.Instance.GetAllList()[i]; self.StagePosMap.Add(item.NameKey, new Vector3(item.Position[0], item.Position[1], 0)); } self.FSM = self.AddComponent <FSMComponent>(); self.FSM.AddState <GalGameEngineReadyState>(); self.FSM.AddState <GalGameEngineFastForwardState>(); self.FSM.AddState <GalGameEngineRunningState>(); self.FSM.AddState <GalGameEngineSuspendedState>(); self.FSM.ChangeState <GalGameEngineReadyState>().Coroutine(); }
public static async ETTask ChangeSceneAsync(this SceneChangeComponent self, string sceneName) { self.tcs = ETTask.Create(true); // 加载map self.loadMapOperation = SceneManager.LoadSceneAsync(sceneName); await self.tcs; }
public ActorMessageSender(long actorId, MemoryStream memoryStream, ETTask <IActorResponse> tcs, bool needException) { this.ActorId = actorId; this.MemoryStream = memoryStream; this.CreateTime = TimeHelper.ServerNow(); this.Tcs = tcs; this.NeedException = needException; }
public static ETAsyncTaskMethodBuilder Create() { ETAsyncTaskMethodBuilder builder = new ETAsyncTaskMethodBuilder() { tcs = ETTask.Create(true) }; return(builder); }
public static async ETTask <UnityEngine.Object[]> UnityLoadAssetAsync(AssetBundle assetBundle) { var tcs = ETTask <UnityEngine.Object[]> .Create(true); AssetBundleRequest request = assetBundle.LoadAllAssetsAsync(); request.completed += operation => { tcs.SetResult(request.allAssets); }; return(await tcs); }
public static async ETTask <GObject> CreateGObjectAsync(Scene scene, string packageName, string resName) { await scene.GetComponent <FGUIPackageComponent>().AddPackageAsync(packageName); ETTask <GObject> tcs = ETTask <GObject> .Create(true); UIPackage.CreateObjectAsync(packageName, resName, (go) => { tcs.SetResult(go); }); return(await tcs); }
public static async ETTask <AssetBundle> UnityLoadBundleAsync(string path) { var tcs = ETTask <AssetBundle> .Create(true); AssetBundleCreateRequest request = AssetBundle.LoadFromFileAsync(path); request.completed += operation => { tcs.SetResult(request.assetBundle); }; return(await tcs); }
public void SetResult() { var t = tcs; this.tcs = null; t.SetResult(new K() { Error = WaitTypeError.Destroy }); }
public override void Awake(TargetSelectComponent self) { //CursorImage = GetComponent<Image>(); self.CursorColor = Color.white; self.waiter = ETTask <GameObject> .Create(); self.Init().Coroutine(); self.HeroObj = UnitHelper.GetMyUnitFromZoneScene(self.ZoneScene()).GetComponent <GameObjectComponent>().GameObject; }
// 创建机器人,生命周期是RobotCase public static async ETTask NewRobot(this RobotCase self, int count, List <Scene> scenes) { ETTask[] tasks = new ETTask[count]; for (int i = 0; i < count; ++i) { tasks[i] = self.NewRobot(scenes); } await ETTaskHelper.WaitAll(tasks); }
public ETTask StartUpdate() { ETTask etTask = ETTask.Create(true); Updater.ResPreparedCompleted += () => { etTask.SetResult(); }; Updater.StartUpdate(); return(etTask); }
public async ETTask DownloadAsync(string url) { this.tcs = ETTask.Create(true); url = url.Replace(" ", "%20"); this.Request = UnityWebRequest.Get(url); this.Request.certificateHandler = certificateHandler; this.Request.SendWebRequest(); await this.tcs; }
public static ETTask <FUI_BottomBtn> CreateInstanceAsync(Entity domain) { ETTask <FUI_BottomBtn> tcs = ETTask <FUI_BottomBtn> .Create(true); CreateGObjectAsync((go) => { tcs.SetResult(Entity.Create <FUI_BottomBtn, GObject>(domain, go)); }); return(tcs); }
public static async ETTask <bool> MoveToAsync(this DMoveComponent self, Vector3 target, float speedValue, ETCancellationToken cancellationToken = null) { await ETTask.CompletedTask; DUnit unit = self.GetParent <DUnit>(); if ((target - self.Target).magnitude < 0.1f) { return(true); } self.Target = target; self.StartPos = unit.Position; self.StartTime = TimeHelper.ClientNow(); float distance = (self.Target - self.StartPos).magnitude; if (Math.Abs(distance) < 0.1f) { return(true); } self.needTime = (long)(distance / speedValue * 1000); ETTask <bool> moveTcs = ETTask <bool> .Create(); self.Callback = (ret) => { moveTcs.SetResult(ret); }; void CancelAction() { if (self.Callback != null) { Action <bool> callback = self.Callback; self.Callback = null; callback.Invoke(false); } } bool moveRet; try { cancellationToken?.Add(CancelAction); moveRet = await moveTcs; } finally { cancellationToken?.Remove(CancelAction); } return(moveRet); }
//预加载prefab private static ETTask StartPreloadGameObject(this SceneLoadComponent self, string path, int count) { ETTask task = ETTask.Create(); GameObjectPoolComponent.Instance.PreLoadGameObjectAsync(path, count, () => { self.FinishCount++; self.ProgressCallback?.Invoke((float)self.FinishCount / self.Total); task.SetResult(); }).Coroutine(); return(task); }
//预加载图集 private static ETTask StartPreloadImage(this SceneLoadComponent self, string path) { ETTask task = ETTask.Create(); ImageLoaderComponent.Instance.LoadImageAsync(path, (go) => { self.FinishCount++; self.ProgressCallback?.Invoke((float)self.FinishCount / self.Total); task.SetResult(); }).Coroutine(); return(task); }
/// <summary> /// 加载场景,path需要是全路径 /// </summary> /// <param name="path"></param> /// <returns></returns> public static ETTask <SceneAssetRequest> LoadSceneAsync(string path) { ETTask <SceneAssetRequest> tcs = ETTask <SceneAssetRequest> .Create(true); SceneAssetRequest sceneAssetRequest = Assets.LoadSceneAsync(path, false); sceneAssetRequest.completed += (arq) => { tcs.SetResult(sceneAssetRequest); }; return(tcs); }