-
Notifications
You must be signed in to change notification settings - Fork 342
Expand file tree
/
Copy pathBootstrap.Common.cs
More file actions
158 lines (139 loc) · 5.8 KB
/
Bootstrap.Common.cs
File metadata and controls
158 lines (139 loc) · 5.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
// Bootstrap.cs
//
// Author:
// JasonXuDeveloper <jason@xgamedev.net>
//
// Copyright (c) 2025 JEngine
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
using System;
using Cysharp.Threading.Tasks;
using JEngine.Core.Encrypt;
using JEngine.Core.Update;
using UnityEngine.SceneManagement;
using YooAsset;
using SceneHandle = YooAsset.SceneHandle;
namespace JEngine.Core
{
public partial class Bootstrap
{
/// <summary>
/// Common function for loading hot update scenes
/// </summary>
/// <param name="package"></param>
/// <param name="sceneName"></param>
/// <param name="callbacks"></param>
/// <param name="loadMode"></param>
/// <exception cref="Exception"></exception>
public static async UniTask<SceneHandle> LoadHotUpdateScene(ResourcePackage package, string sceneName,
SceneLoadCallbacks callbacks,
LoadSceneMode loadMode = LoadSceneMode.Single)
{
// Call status update callback
callbacks.OnStatusUpdate?.Invoke(SceneLoadStatus.Loading);
callbacks.OnProgressUpdate?.Invoke(0f);
// Save current active scene for later unloading
var previousSceneName = SceneManager.GetActiveScene().name;
try
{
// Load new scene
var handle = package.LoadSceneAsync(sceneName, loadMode, suspendLoad: true);
// Update loading progress
while (handle.Progress < 0.9f)
{
float progress = handle.Progress;
// Call progress update callback
callbacks.OnProgressUpdate?.Invoke(progress);
await UniTask.DelayFrame(1);
}
// Call completion status callback
callbacks.OnStatusUpdate?.Invoke(SceneLoadStatus.Completed);
callbacks.OnProgressUpdate?.Invoke(1f);
await UniTask.DelayFrame(1); // Wait one frame to ensure scene transition is complete
handle.UnSuspend();
await handle.Task;
if (handle.Status != EOperationStatus.Succeed)
{
throw new Exception(handle.LastError);
}
// Call completion callback
return handle;
}
catch (Exception e)
{
callbacks.OnStatusUpdate?.Invoke(SceneLoadStatus.Failed);
// Call error callback
await callbacks.OnError(e);
// Switch back to previous scene
await SceneManager.LoadSceneAsync(previousSceneName).ToUniTask();
return null;
}
}
/// <summary>
/// Create or get a resource package
/// </summary>
/// <param name="packageName"></param>
/// <returns></returns>
public static ResourcePackage CreateOrGetPackage(string packageName)
{
return YooAssets.TryGetPackage(packageName) ?? YooAssets.CreatePackage(packageName);
}
/// <summary>
/// Common resource package update function
/// </summary>
/// <param name="package">Resource package to initialize</param>
/// <param name="callbacks">Various callback functions</param>
/// <param name="encryptionOption">Package encryption method</param>
/// <returns>Whether initialization was successful</returns>
public static async UniTask<bool> UpdatePackage(ResourcePackage package,
PackageInitializationCallbacks callbacks, EncryptionOption encryptionOption)
{
if (_instance == null)
{
throw new Exception("Bootstrap instance not found in the scene.");
}
var ret = await _instance.UpdatePackageImpl(package, callbacks, encryptionOption);
if (!ret)
{
await package.DestroyAsync();
}
return ret;
}
/// <summary>
/// Clear resource package cache
/// </summary>
/// <param name="package"></param>
/// <param name="onError"></param>
/// <returns></returns>
public static async UniTask<bool> DeletePackageCache(ResourcePackage package,
Func<Exception, UniTask> onError = null)
{
var operation = package.ClearCacheFilesAsync(EFileClearMode.ClearAllBundleFiles);
await operation.ToUniTask();
if (operation.Status == EOperationStatus.Succeed)
{
// Cleanup successful
return true;
}
// Cleanup failed
if (onError != null) await onError(new Exception(operation.Error));
return false;
}
}
}