LaunchApp.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. using System;
  2. using System.Threading;
  3. using Cysharp.Threading.Tasks;
  4. using HotPatch;
  5. using UnityEngine;
  6. using UnityEngine.SceneManagement;
  7. public sealed class LaunchApp
  8. {
  9. readonly Scene launchScene;
  10. readonly ILaunchView view;
  11. readonly ILaunchResources resources;
  12. readonly CancellationToken cancellationToken;
  13. public bool IsRunning { get; private set; }
  14. public bool IsCompleted { get; private set; }
  15. public LaunchApp(Scene launchScene, ILaunchView view, ILaunchResources resources, CancellationToken cancellationToken)
  16. {
  17. this.launchScene = launchScene;
  18. this.view = view ?? throw new ArgumentNullException(nameof(view));
  19. this.resources = resources ?? throw new ArgumentNullException(nameof(resources));
  20. this.cancellationToken = cancellationToken;
  21. }
  22. bool CanRun => !cancellationToken.IsCancellationRequested && view.IsAvailable;
  23. public async UniTask RunAsync()
  24. {
  25. if (IsRunning || IsCompleted || !CanRun) return;
  26. IsRunning = true;
  27. bool showRetryDialog = false;
  28. Scene gameScene = default;
  29. try
  30. {
  31. view.ResetProgress();
  32. var sceneName = resources.GameSceneName;
  33. if (SceneManager.GetSceneByName(sceneName).isLoaded)
  34. throw new InvalidOperationException(sceneName + " is already open. Restart the app.");
  35. HotPatchLog.Info("launch_start scene=" + sceneName);
  36. Report("Checking version", 0);
  37. await resources.LoadAsync(Report, cancellationToken);
  38. cancellationToken.ThrowIfCancellationRequested();
  39. HotPatchLog.Info("scene_load_start scene=" + sceneName + " version=" + resources.LoadedVersion);
  40. Report("Loading resources", .95f);
  41. var scenePath = resources.LoadedScenePath;
  42. AsyncOperation operation;
  43. #if UNITY_EDITOR
  44. if (resources.UseEditorSceneLoading)
  45. operation = UnityEditor.SceneManagement.EditorSceneManager.LoadSceneAsyncInPlayMode(scenePath, new LoadSceneParameters(LoadSceneMode.Additive));
  46. else
  47. #endif
  48. operation = SceneManager.LoadSceneAsync(scenePath, LoadSceneMode.Additive);
  49. if (operation == null) throw new InvalidOperationException("Unable to open game scene: " + sceneName);
  50. await operation.ToUniTask(progress: Progress.Create<float>(value => Report("Loading resources", .95f + .04f * value)));
  51. gameScene = SceneManager.GetSceneByPath(scenePath);
  52. cancellationToken.ThrowIfCancellationRequested();
  53. if (!gameScene.isLoaded || !SceneManager.SetActiveScene(gameScene))
  54. throw new InvalidOperationException("Failed to activate game scene: " + sceneName);
  55. HotPatchLog.Info("scene_load_complete scene=" + sceneName + " version=" + resources.LoadedVersion);
  56. Report("Loading resources", 1);
  57. await UniTask.Yield();
  58. cancellationToken.ThrowIfCancellationRequested();
  59. LaunchSceneInput.Transfer(launchScene, gameScene);
  60. #if UNITY_EDITOR
  61. await UniTask.NextFrame(cancellationToken: cancellationToken);
  62. HotPatchLog.Info("editor_initialization_start scene=" + gameScene.name);
  63. resources.InitializeEditor();
  64. HotPatchLog.Info("editor_initialization_complete scene=" + gameScene.name);
  65. #endif
  66. await SceneManager.UnloadSceneAsync(launchScene);
  67. IsCompleted = true;
  68. }
  69. catch (Exception exception)
  70. {
  71. HotPatchLog.Warning("launch_failed type=" + exception.GetType().Name + " message=" + exception.Message);
  72. if (gameScene.IsValid() && gameScene.isLoaded) await SceneManager.UnloadSceneAsync(gameScene);
  73. if (cancellationToken.IsCancellationRequested || exception is OperationCanceledException) return;
  74. if (resources.HasLoadedAssembly) resources.MarkEntryFailed();
  75. Debug.LogException(exception);
  76. showRetryDialog = true;
  77. }
  78. finally
  79. {
  80. IsRunning = false;
  81. }
  82. if (showRetryDialog && CanRun)
  83. {
  84. HotPatchLog.Info("retry_dialog_open requiresRestart=" + resources.RequiresRestart);
  85. view.ShowError(NativeDialog.NetworkErrorMessage, () =>
  86. {
  87. if (!CanRun) return;
  88. HotPatchLog.Info("retry_dialog_confirmed");
  89. RunAsync().Forget(Debug.LogException);
  90. });
  91. }
  92. }
  93. void Report(string stage, float progress)
  94. {
  95. if (CanRun) view.ReportProgress(stage, progress);
  96. }
  97. }