From bc113c5c6ed9abbd453cbfadf2af4579b244503e Mon Sep 17 00:00:00 2001
From: ALEXTANG <574809918@qq.com>
Date: Tue, 4 Jul 2023 20:57:26 +0800
Subject: [PATCH] =?UTF-8?q?[+]=20Update=E7=89=88=E6=9C=AC=E6=9B=B4?=
=?UTF-8?q?=E6=96=B0=E6=97=B6CDN=E5=9C=B0=E5=9D=80=E4=B9=9F=E5=8F=AF?=
=?UTF-8?q?=E6=9B=B4=E6=96=B0?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
[+] Update版本更新时CDN地址也可更新
---
.../Resources/AssetLoad/UpdateData.json | 5 ++-
.../Procedure/ProcedureCreateDownloader.cs | 26 +----------
.../Main/Procedure/ProcedureInitPackage.cs | 45 +++++++++++++++++++
.../GameFramework/Utility/Utility.Http.cs | 41 +++++++++++++++++
.../Utility/Utility.Http.cs.meta | 3 ++
.../Framework/FrameworkGlobalSettings.cs | 15 +++++++
6 files changed, 109 insertions(+), 26 deletions(-)
create mode 100644 Assets/TEngine/Runtime/GameFramework/Utility/Utility.Http.cs
create mode 100644 Assets/TEngine/Runtime/GameFramework/Utility/Utility.Http.cs.meta
diff --git a/Assets/GameScripts/Main/Launcher/Resources/AssetLoad/UpdateData.json b/Assets/GameScripts/Main/Launcher/Resources/AssetLoad/UpdateData.json
index 45d439cd..5b225e79 100644
--- a/Assets/GameScripts/Main/Launcher/Resources/AssetLoad/UpdateData.json
+++ b/Assets/GameScripts/Main/Launcher/Resources/AssetLoad/UpdateData.json
@@ -1,5 +1,8 @@
{
+ "CurrentVersion" : "",
"UpdateType": 1,
"UpdateStyle": 1,
- "UpdateNotice": 1
+ "UpdateNotice": 1,
+ "HostServerURL": "",
+ "FallbackHostServerURL" : ""
}
\ No newline at end of file
diff --git a/Assets/GameScripts/Main/Procedure/ProcedureCreateDownloader.cs b/Assets/GameScripts/Main/Procedure/ProcedureCreateDownloader.cs
index daa23f5c..14114a49 100644
--- a/Assets/GameScripts/Main/Procedure/ProcedureCreateDownloader.cs
+++ b/Assets/GameScripts/Main/Procedure/ProcedureCreateDownloader.cs
@@ -114,7 +114,7 @@ namespace GameMain
}
Log.Info("RequestUpdateData, proxy:" + checkVersionUrl);
- var updateDataStr = await HttpGet(checkVersionUrl);
+ var updateDataStr = await Utility.Http.Get(checkVersionUrl);
try
{
@@ -128,30 +128,6 @@ namespace GameMain
}
}
- ///
- /// GET请求与获取结果.
- ///
- public async UniTask HttpGet(string url,float timeout = 5f)
- {
- var cts = new CancellationTokenSource();
- cts.CancelAfterSlim(TimeSpan.FromSeconds(timeout));
-
- UnityWebRequest unityWebRequest = UnityWebRequest.Get(url);
- try
- {
- await unityWebRequest.SendWebRequest().WithCancellation(cts.Token);
- }
- catch (OperationCanceledException ex)
- {
- if (ex.CancellationToken == cts.Token)
- {
- Debug.Log("HttpGet Timeout");
- return string.Empty;
- }
- }
- return unityWebRequest.downloadHandler.text;
- }
-
///
/// 显示更新方式
///
diff --git a/Assets/GameScripts/Main/Procedure/ProcedureInitPackage.cs b/Assets/GameScripts/Main/Procedure/ProcedureInitPackage.cs
index 1177f66f..970a09c8 100644
--- a/Assets/GameScripts/Main/Procedure/ProcedureInitPackage.cs
+++ b/Assets/GameScripts/Main/Procedure/ProcedureInitPackage.cs
@@ -23,6 +23,23 @@ namespace GameMain
private async UniTaskVoid InitPackage(ProcedureOwner procedureOwner)
{
+ if (GameModule.Resource.PlayMode == EPlayMode.HostPlayMode)
+ {
+ UpdateData updateData = await RequestUpdateData();
+
+ if (updateData!=null)
+ {
+ if (!string.IsNullOrEmpty(updateData.HostServerURL))
+ {
+ SettingsUtils.FrameworkGlobalSettings.HostServerURL = updateData.HostServerURL;
+ }
+ if (!string.IsNullOrEmpty(updateData.FallbackHostServerURL))
+ {
+ SettingsUtils.FrameworkGlobalSettings.FallbackHostServerURL = updateData.FallbackHostServerURL;
+ }
+ }
+ }
+
var initializationOperation = GameModule.Resource.InitPackage();
await UniTask.Delay(TimeSpan.FromSeconds(1f));
@@ -82,5 +99,33 @@ namespace GameMain
InitPackage(procedureOwner).Forget();
}
+
+ ///
+ /// 请求更新配置数据。
+ ///
+ private async UniTask RequestUpdateData()
+ {
+ var checkVersionUrl = SettingsUtils.GetUpdateDataUrl();
+
+ if (string.IsNullOrEmpty(checkVersionUrl))
+ {
+ Log.Error("LoadMgr.RequestVersion, remote url is empty or null");
+ return null;
+ }
+ Log.Info("RequestUpdateData, proxy:" + checkVersionUrl);
+
+ var updateDataStr = await Utility.Http.Get(checkVersionUrl);
+
+ try
+ {
+ UpdateData updateData = Utility.Json.ToObject(updateDataStr);
+ return updateData;
+ }
+ catch (Exception e)
+ {
+ Log.Fatal(e);
+ return null;
+ }
+ }
}
}
\ No newline at end of file
diff --git a/Assets/TEngine/Runtime/GameFramework/Utility/Utility.Http.cs b/Assets/TEngine/Runtime/GameFramework/Utility/Utility.Http.cs
new file mode 100644
index 00000000..8f5a32b3
--- /dev/null
+++ b/Assets/TEngine/Runtime/GameFramework/Utility/Utility.Http.cs
@@ -0,0 +1,41 @@
+using System;
+using System.Threading;
+using Cysharp.Threading.Tasks;
+using UnityEngine;
+using UnityEngine.Networking;
+
+namespace TEngine
+{
+ public static partial class Utility
+ {
+ ///
+ /// Http 相关的实用函数。
+ ///
+ public static partial class Http
+ {
+ ///
+ /// GET请求与获取结果。
+ ///
+ public static async UniTask Get(string url,float timeout = 5f)
+ {
+ var cts = new CancellationTokenSource();
+ cts.CancelAfterSlim(TimeSpan.FromSeconds(timeout));
+
+ UnityWebRequest unityWebRequest = UnityWebRequest.Get(url);
+ try
+ {
+ await unityWebRequest.SendWebRequest().WithCancellation(cts.Token);
+ }
+ catch (OperationCanceledException ex)
+ {
+ if (ex.CancellationToken == cts.Token)
+ {
+ Debug.Log("HttpGet Timeout");
+ return string.Empty;
+ }
+ }
+ return unityWebRequest.downloadHandler.text;
+ }
+ }
+ }
+}
\ No newline at end of file
diff --git a/Assets/TEngine/Runtime/GameFramework/Utility/Utility.Http.cs.meta b/Assets/TEngine/Runtime/GameFramework/Utility/Utility.Http.cs.meta
new file mode 100644
index 00000000..3e155e28
--- /dev/null
+++ b/Assets/TEngine/Runtime/GameFramework/Utility/Utility.Http.cs.meta
@@ -0,0 +1,3 @@
+fileFormatVersion: 2
+guid: da0e9fec46234724b7f73d9c49a92603
+timeCreated: 1688475202
\ No newline at end of file
diff --git a/Assets/TEngine/Runtime/GameSettings/Framework/FrameworkGlobalSettings.cs b/Assets/TEngine/Runtime/GameSettings/Framework/FrameworkGlobalSettings.cs
index 74828714..81e20911 100644
--- a/Assets/TEngine/Runtime/GameSettings/Framework/FrameworkGlobalSettings.cs
+++ b/Assets/TEngine/Runtime/GameSettings/Framework/FrameworkGlobalSettings.cs
@@ -52,6 +52,11 @@ public enum GameStatus
///
public class UpdateData
{
+ ///
+ /// 当前版本信息。
+ ///
+ public string CurrentVersion;
+
///
/// 是否底包更新。
///
@@ -66,6 +71,16 @@ public class UpdateData
/// 是否提示。
///
public UpdateNotice UpdateNotice;
+
+ ///
+ /// 热更资源地址。
+ ///
+ public string HostServerURL;
+
+ ///
+ /// 备用热更资源地址。
+ ///
+ public string FallbackHostServerURL;
}
///