Skip to content

Commit fea1ae2

Browse files
authored
Merge pull request #59 from AlanWeekend/main
优化分包资源下载逻辑,统一WebGL平台与其他平台的远程热更资源引用方式
2 parents f186d6b + 866c440 commit fea1ae2

File tree

4 files changed

+54
-31
lines changed

4 files changed

+54
-31
lines changed

UnityProject/Assets/GameScripts/Main/Procedure/ProcedureInitPackage.cs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ protected override void OnEnter(ProcedureOwner procedureOwner)
2929

3030
private async UniTaskVoid InitPackage(ProcedureOwner procedureOwner)
3131
{
32-
if (GameModule.Resource.PlayMode == EPlayMode.HostPlayMode)
32+
if (GameModule.Resource.PlayMode == EPlayMode.HostPlayMode ||
33+
GameModule.Resource.PlayMode == EPlayMode.WebPlayMode)
3334
{
3435
if (SettingsUtils.EnableUpdateData())
3536
{
@@ -44,7 +45,8 @@ private async UniTaskVoid InitPackage(ProcedureOwner procedureOwner)
4445

4546
if (!string.IsNullOrEmpty(updateData.FallbackHostServerURL))
4647
{
47-
SettingsUtils.FrameworkGlobalSettings.FallbackHostServerURL = updateData.FallbackHostServerURL;
48+
SettingsUtils.FrameworkGlobalSettings.FallbackHostServerURL =
49+
updateData.FallbackHostServerURL;
4850
}
4951
}
5052
}
@@ -60,9 +62,9 @@ private async UniTaskVoid InitPackage(ProcedureOwner procedureOwner)
6062
{
6163
//热更新阶段文本初始化
6264
LoadText.Instance.InitConfigData(null);
63-
65+
6466
GameEvent.Send(RuntimeId.ToRuntimeId("RefreshVersion"));
65-
67+
6668
EPlayMode playMode = GameModule.Resource.PlayMode;
6769

6870
// 编辑器模式。
@@ -107,7 +109,9 @@ private async UniTaskVoid InitPackage(ProcedureOwner procedureOwner)
107109
// 打开启动UI。
108110
UILoadMgr.Show(UIDefine.UILoadUpdate, $"资源初始化失败!");
109111

110-
UILoadTip.ShowMessageBox($"资源初始化失败!点击确认重试 \n \n <color=#FF0000>原因{initializationOperation.Error}</color>", MessageShowType.TwoButton,
112+
UILoadTip.ShowMessageBox(
113+
$"资源初始化失败!点击确认重试 \n \n <color=#FF0000>原因{initializationOperation.Error}</color>",
114+
MessageShowType.TwoButton,
111115
LoadStyle.StyleEnum.Style_Retry
112116
, () => { Retry(procedureOwner); }, UnityEngine.Application.Quit);
113117
}

UnityProject/Assets/TEngine/Runtime/Modules/ResourceModule/BuiltinQueryMgr.cs

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,6 @@ public class StreamingAssetsDefine
1010
public const string RootFolderName = "yoo";
1111
}
1212

13-
/// <summary>
14-
/// 内置文件查询服务类。
15-
/// </summary>
16-
public class BuiltinQueryServices : IBuildinQueryServices
17-
{
18-
public bool QueryStreamingAssets(string packageName, string fileName)
19-
{
20-
// 注意:fileName包含文件格式
21-
return BuiltinQueryMgr.FileExists(packageName, fileName);
22-
}
23-
}
24-
2513
#if UNITY_EDITOR
2614
/// <summary>
2715
/// 内置资源资源查询帮助类。

UnityProject/Assets/TEngine/Runtime/Modules/ResourceModule/ResourceManager.Services.cs

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ internal partial class ResourceManager
1212
private class GameDecryptionServices : IDecryptionServices
1313
{
1414
private const byte OffSet = 32;
15-
15+
1616
public ulong LoadFromFileOffset(DecryptFileInfo fileInfo)
1717
{
1818
return OffSet;
@@ -25,7 +25,8 @@ public byte[] LoadFromMemory(DecryptFileInfo fileInfo)
2525

2626
public Stream LoadFromStream(DecryptFileInfo fileInfo)
2727
{
28-
BundleStream bundleStream = new BundleStream(fileInfo.FilePath, FileMode.Open, FileAccess.Read, FileShare.Read);
28+
BundleStream bundleStream =
29+
new BundleStream(fileInfo.FilePath, FileMode.Open, FileAccess.Read, FileShare.Read);
2930
return bundleStream;
3031
}
3132

@@ -34,7 +35,7 @@ public uint GetManagedReadBufferSize()
3435
return 1024;
3536
}
3637
}
37-
38+
3839
/// <summary>
3940
/// 默认的分发资源查询服务类
4041
/// </summary>
@@ -44,40 +45,66 @@ public DeliveryFileInfo GetDeliveryFileInfo(string packageName, string fileName)
4445
{
4546
throw new NotImplementedException();
4647
}
48+
4749
public bool QueryDeliveryFiles(string packageName, string fileName)
4850
{
4951
return false;
5052
}
5153
}
52-
54+
5355
/// <summary>
5456
/// 远程文件查询服务类。
5557
/// </summary>
56-
private class RemoteServices: IRemoteServices
58+
private class RemoteServices : IRemoteServices
5759
{
5860
private readonly string _defaultHostServer;
5961
private readonly string _fallbackHostServer;
62+
private string _packageName;
6063

61-
public RemoteServices()
64+
public RemoteServices(string packageName)
6265
{
6366
_defaultHostServer = SettingsUtils.FrameworkGlobalSettings.HostServerURL;
6467
_fallbackHostServer = SettingsUtils.FrameworkGlobalSettings.FallbackHostServerURL;
68+
_packageName = packageName;
6569
}
66-
70+
6771
public RemoteServices(string defaultHostServer, string fallbackHostServer)
6872
{
6973
_defaultHostServer = defaultHostServer;
7074
_fallbackHostServer = fallbackHostServer;
7175
}
72-
76+
7377
public string GetRemoteMainURL(string fileName)
7478
{
75-
return $"{_defaultHostServer}/{fileName}";
79+
return $"{_defaultHostServer}/{_packageName}/{fileName}";
7680
}
7781

7882
public string GetRemoteFallbackURL(string fileName)
7983
{
80-
return $"{_fallbackHostServer}/{fileName}";
84+
return $"{_defaultHostServer}/{_packageName}/{fileName}";
85+
}
86+
}
87+
88+
/// <summary>
89+
/// 内置文件查询服务类。
90+
/// </summary>
91+
public class BuiltinQueryServices : IBuildinQueryServices
92+
{
93+
public bool QueryStreamingAssets(string packageName, string fileName)
94+
{
95+
// 注意:fileName包含文件格式
96+
return BuiltinQueryMgr.FileExists(packageName, fileName);
97+
}
98+
}
99+
100+
/// <summary>
101+
/// WebGL内置文件查询服务类。WebGL平台不需要内置查询,直接使用远程热更资源。
102+
/// </summary>
103+
public class WebGLBuiltinQueryServices : IBuildinQueryServices
104+
{
105+
public bool QueryStreamingAssets(string packageName, string fileName)
106+
{
107+
return true;
81108
}
82109
}
83110
}
@@ -86,7 +113,8 @@ public class BundleStream : FileStream
86113
{
87114
public const byte KEY = 128;
88115

89-
public BundleStream(string path, FileMode mode, FileAccess access, FileShare share) : base(path, mode, access, share)
116+
public BundleStream(string path, FileMode mode, FileAccess access, FileShare share) : base(path, mode, access,
117+
share)
90118
{
91119
}
92120

UnityProject/Assets/TEngine/Runtime/Modules/ResourceModule/ResourceManager.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,7 @@ public InitializationOperation InitPackage(string customPackageName = "")
384384
createParameters.DecryptionServices = new GameDecryptionServices();
385385
createParameters.BuildinQueryServices = new BuiltinQueryServices();
386386
createParameters.DeliveryQueryServices = new DefaultDeliveryQueryServices();
387-
createParameters.RemoteServices = new RemoteServices();
387+
createParameters.RemoteServices = new RemoteServices(targetPackageName);
388388
initializationOperation = package.InitializeAsync(createParameters);
389389
}
390390

@@ -394,8 +394,11 @@ public InitializationOperation InitPackage(string customPackageName = "")
394394
YooAssets.SetCacheSystemDisableCacheOnWebGL();
395395
var createParameters = new WebPlayModeParameters();
396396
createParameters.DecryptionServices = new GameDecryptionServices();
397-
createParameters.BuildinQueryServices = new BuiltinQueryServices();
398-
createParameters.RemoteServices = new RemoteServices();
397+
createParameters.BuildinQueryServices = new WebGLBuiltinQueryServices();
398+
createParameters.RemoteServices = new RemoteServices(targetPackageName);
399+
// WebGL运行模式下,直接使用远程热更资源。
400+
createParameters.BuildinRootDirectory = SettingsUtils.FrameworkGlobalSettings.HostServerURL;
401+
createParameters.SandboxRootDirectory = SettingsUtils.FrameworkGlobalSettings.HostServerURL;
399402
initializationOperation = package.InitializeAsync(createParameters);
400403
}
401404

0 commit comments

Comments
 (0)