// Coroutines /-/-/-/-/-/-/-/-/-/-/-/-/ // RUN AUTH MODULE private IEnumerator I_RunModule(AuthModule module) { ModuleRunning = true; int retrys = 0; bool module_retrying = false; module.OnEnter(); IAsyncModule asyncModule = module as IAsyncModule; if (asyncModule != null) { ProcessResult ares = ProcessResult.None; do { if (module_retrying) { module.OnEnter(); module_retrying = false; } ares = asyncModule.AsyncResult(); if (ares == ProcessResult.Failure) { Logger.LogWarning("Got Error during process of AsyncModule " + module.GetException(), this); if (retrys < MaximunErrorRetrys) { yield return(new WaitForSeconds(0.5f)); Logger.LogWarning("Retrying... " + retrys, this); retrys++; module_retrying = true; } } yield return(new WaitForEndOfFrame()); } while (ares != ProcessResult.Completed); yield return(new WaitUntil(() => ares == ProcessResult.Completed || retrys >= MaximunErrorRetrys)); } retrys = 0; if (module.IsValid(WasRequestedByGuest, auth.CurrentUser)) { module.Execute(this); ProcessResult tres = ProcessResult.None; float time_enter = Time.time; do { if (module_retrying) { module.OnEnter(); module.Execute(this); time_enter = Time.time; module_retrying = false; } tres = module.GetResult(); //Debug.Log("MODULE " + module.GetType().Name + " RESULT: " + tres); if (Input.GetKey(KeyCode.Escape) && Time.time - time_enter >= 0.15f) { RequestExit(); } //Interrumpt the module when you're about to upgrade the acocount float diff = Time.time - ExitTime; if (IsAuthenticated && (module.IsInterruptible() && ExitRequest && diff > 0.2f && diff < 0.5f)) { fsm.MoveNext(AuthCheckCommand.GoBack); yield break; } if (diff >= 0.55f) { ExitTime = 0; } if (tres == ProcessResult.Failure) { Logger.LogWarning("Got Error during process of module " + module.GetException(), this); if (retrys < MaximunErrorRetrys) { yield return(new WaitForSeconds(0.5f)); Logger.LogWarning("Retrying... " + retrys, this); retrys++; module_retrying = true; } } yield return(new WaitForEndOfFrame()); } while (tres != ProcessResult.Completed); if (tres == ProcessResult.Completed) { Logger.Log("Module completed: " + module.GetType().Name, this, true); module.OnFinish(this, ExitRequest); ICustomCommand cmd = module as ICustomCommand; fsm.MoveNext(cmd != null ? cmd.GetNextCommand() : AuthCheckCommand.Next); ExitRequest = false; } } else { fsm.MoveNext(AuthCheckCommand.Next); yield break; } ModuleRunning = false; yield return(0); }
// PRIVATE API /-/-/-/-/-/-/-/-/-/-/-/ private void InitFSM() { fsm.AddTransition(AuthCheckState.None, AuthCheckCommand.Next, () => { return(AuthCheckState.InternetCheck); }); fsm.AddTransition(AuthCheckState.InternetCheck, AuthCheckCommand.Next, () => AuthCheckState.MainMenu); fsm.AddTransition(AuthCheckState.InternetCheck, AuthCheckCommand.Exit, () => AuthCheckState.None); fsm.AddTransition(AuthCheckState.MainMenu, AuthCheckCommand.Next, () => { return(AuthCheckState.AuthMethod); }); fsm.AddTransition(AuthCheckState.AuthMethod, AuthCheckCommand.Exit, () => { if (!AlwaysAskForAccount) { PlayerPrefs.SetInt(AccountSateKey, 1); } else { PlayerPrefs.SetInt(AccountSateKey, 0); } return(AuthCheckState.None); }); fsm.AddTransition(AuthCheckState.AuthMethod, AuthCheckCommand.Next, () => AuthCheckState.DatabaseCheck); fsm.AddTransition(AuthCheckState.DatabaseCheck, AuthCheckCommand.Next, () => AuthCheckState.SetupProfile); fsm.AddTransition(AuthCheckState.AuthMethod, AuthCheckCommand.GoBack, () => AuthCheckState.MainMenu); fsm.AddTransition(AuthCheckState.MainMenu, AuthCheckCommand.GoBack, () => AuthCheckState.None); fsm.AddTransition(AuthCheckState.SetupProfile, AuthCheckCommand.Next, () => AuthCheckState.None); fsm.AddTransition(AuthCheckState.DatabaseCheck, AuthCheckCommand.Exit, () => AuthCheckState.None); fsm.OnStateChanged = (AuthCheckState state) => { AuthModule module = null; Logger.Log($"<color=blue>State changed:: { state } </color>", this, true); DisplayLayout(true); switch (state) { case AuthCheckState.InternetCheck: module = internetcheck; break; case AuthCheckState.MainMenu: module = menuModule; break; case AuthCheckState.AuthMethod: if (SelectedMethod == null) { fsm.MoveNext(AuthCheckCommand.Next); } else if (!AuthRunning) { StartCoroutine(I_ExecuteMethod()); } break; case AuthCheckState.DatabaseCheck: if (auth.CurrentUser != null) { module = databaseCheck; } else { fsm.MoveNext(AuthCheckCommand.Exit); } break; case AuthCheckState.SetupProfile: module = profileModule; PlayerPrefs.SetInt(AccountSateKey, 0); break; case AuthCheckState.None: if (BaseLayout.activeInHierarchy) { DisplayLayout(false); if (OnVerifyCompleted != null) { OnVerifyCompleted.Invoke(); } } ModuleRunning = false; break; } if (state != AuthCheckState.None && module != null) { StartCoroutine(I_RunModule(module)); } }; }