版本验证配置
This commit is contained in:
19
Assets/TcgEngine/Resources/ResourceDownloaderConfig.asset
Normal file
19
Assets/TcgEngine/Resources/ResourceDownloaderConfig.asset
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
%YAML 1.1
|
||||||
|
%TAG !u! tag:unity3d.com,2011:
|
||||||
|
--- !u!114 &11400000
|
||||||
|
MonoBehaviour:
|
||||||
|
m_ObjectHideFlags: 0
|
||||||
|
m_CorrespondingSourceObject: {fileID: 0}
|
||||||
|
m_PrefabInstance: {fileID: 0}
|
||||||
|
m_PrefabAsset: {fileID: 0}
|
||||||
|
m_GameObject: {fileID: 0}
|
||||||
|
m_Enabled: 1
|
||||||
|
m_EditorHideFlags: 0
|
||||||
|
m_Script: {fileID: 11500000, guid: cdc2f7b92625e448fb76f26558cd0779, type: 3}
|
||||||
|
m_Name: ResourceDownloaderConfig
|
||||||
|
m_EditorClassIdentifier:
|
||||||
|
cdnUrl: https://cardcdn.ambigrat.com
|
||||||
|
serverUrl: http://192.168.1.99:8080
|
||||||
|
resourcesEndpoint: /test/{version}.zip
|
||||||
|
versionEndpoint: /version
|
||||||
|
validateVersionedUrl: 1
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 5852c171f99424772952bce4e3303421
|
||||||
|
NativeFormatImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
mainObjectFileID: 11400000
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
||||||
@@ -467,7 +467,7 @@ MonoBehaviour:
|
|||||||
nextSceneName: LoginMenu
|
nextSceneName: LoginMenu
|
||||||
fadeInDuration: 1
|
fadeInDuration: 1
|
||||||
fadeOutDuration: 1
|
fadeOutDuration: 1
|
||||||
minimumDisplayTime: 3
|
minimumDisplayTime: 0
|
||||||
splashMusic: {fileID: 0}
|
splashMusic: {fileID: 0}
|
||||||
--- !u!114 &507621921
|
--- !u!114 &507621921
|
||||||
MonoBehaviour:
|
MonoBehaviour:
|
||||||
|
|||||||
@@ -14,14 +14,9 @@ namespace TcgEngine
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public class ResourceDownloader : MonoBehaviour
|
public class ResourceDownloader : MonoBehaviour
|
||||||
{
|
{
|
||||||
[Header("Download Settings")]
|
[Header("Configuration")]
|
||||||
public string serverUrl = "https://cardcdn.ambigrat.com";
|
[Tooltip("资源下载器配置文件")]
|
||||||
public string resourcesEndpoint = "/test/{version}.zip";
|
public ResourceDownloaderConfig config;
|
||||||
public string versionEndpoint = "/version";
|
|
||||||
|
|
||||||
[Header("URL Pattern Options")]
|
|
||||||
[Tooltip("检查版本化文件是否存在")]
|
|
||||||
public bool validateVersionedUrl = true;
|
|
||||||
private string currentVersion = "0.0.0";
|
private string currentVersion = "0.0.0";
|
||||||
private string currentMd5 = "";
|
private string currentMd5 = "";
|
||||||
private string targetVersion = "0.0.1";
|
private string targetVersion = "0.0.1";
|
||||||
@@ -211,8 +206,13 @@ namespace TcgEngine
|
|||||||
targetFileName = version;
|
targetFileName = version;
|
||||||
}
|
}
|
||||||
|
|
||||||
string endpoint = resourcesEndpoint.Replace("{version}", targetFileName);
|
if (config == null)
|
||||||
string url = serverUrl + endpoint;
|
{
|
||||||
|
Debug.LogError("ResourceDownloaderConfig is not assigned!");
|
||||||
|
return "0.0.0";
|
||||||
|
}
|
||||||
|
|
||||||
|
string url = config.GetResourceUrl(targetFileName);
|
||||||
|
|
||||||
Debug.Log($"Built download URL: {url} (version: {version}, filename: {targetFileName})");
|
Debug.Log($"Built download URL: {url} (version: {version}, filename: {targetFileName})");
|
||||||
return url;
|
return url;
|
||||||
@@ -223,7 +223,7 @@ namespace TcgEngine
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
private async Task<bool> ValidateVersionedUrl(string url)
|
private async Task<bool> ValidateVersionedUrl(string url)
|
||||||
{
|
{
|
||||||
if (!validateVersionedUrl)
|
if (config == null || !config.validateVersionedUrl)
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
try
|
try
|
||||||
@@ -261,8 +261,13 @@ namespace TcgEngine
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
private async Task<ResourceVersionResponse> GetServerVersionData()
|
private async Task<ResourceVersionResponse> GetServerVersionData()
|
||||||
{
|
{
|
||||||
|
if (config == null)
|
||||||
|
{
|
||||||
|
Debug.LogError("ResourceDownloaderConfig is not assigned!");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
string url = "http://192.168.1.99:8080" + versionEndpoint;
|
string url = config.GetVersionUrl();
|
||||||
Debug.Log($"Version check URL: {url}");
|
Debug.Log($"Version check URL: {url}");
|
||||||
using (UnityWebRequest request = UnityWebRequest.Get(url))
|
using (UnityWebRequest request = UnityWebRequest.Get(url))
|
||||||
{
|
{
|
||||||
@@ -307,7 +312,7 @@ namespace TcgEngine
|
|||||||
string downloadUrl = BuildVersionedDownloadUrl(targetVersion);
|
string downloadUrl = BuildVersionedDownloadUrl(targetVersion);
|
||||||
|
|
||||||
// 验证URL是否存在(可选)
|
// 验证URL是否存在(可选)
|
||||||
if (validateVersionedUrl)
|
if (config != null && config.validateVersionedUrl)
|
||||||
{
|
{
|
||||||
bool urlExists = await ValidateVersionedUrl(downloadUrl);
|
bool urlExists = await ValidateVersionedUrl(downloadUrl);
|
||||||
if (!urlExists)
|
if (!urlExists)
|
||||||
@@ -517,10 +522,32 @@ namespace TcgEngine
|
|||||||
GameObject go = new GameObject("ResourceDownloader");
|
GameObject go = new GameObject("ResourceDownloader");
|
||||||
instance = go.AddComponent<ResourceDownloader>();
|
instance = go.AddComponent<ResourceDownloader>();
|
||||||
DontDestroyOnLoad(go);
|
DontDestroyOnLoad(go);
|
||||||
|
|
||||||
|
// 加载配置文件
|
||||||
|
instance.LoadConfig();
|
||||||
|
|
||||||
Debug.Log("Created new ResourceDownloader instance");
|
Debug.Log("Created new ResourceDownloader instance");
|
||||||
}
|
}
|
||||||
return instance;
|
return instance;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 加载配置文件
|
||||||
|
/// </summary>
|
||||||
|
private void LoadConfig()
|
||||||
|
{
|
||||||
|
// 从Resources文件夹加载配置文件
|
||||||
|
config = Resources.Load<ResourceDownloaderConfig>("ResourceDownloaderConfig");
|
||||||
|
|
||||||
|
if (config == null)
|
||||||
|
{
|
||||||
|
Debug.LogError("ResourceDownloaderConfig not found in Resources folder! Please create a ResourceDownloaderConfig asset in the Resources folder.");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Debug.Log("ResourceDownloaderConfig loaded successfully");
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|||||||
49
Assets/TcgEngine/Scripts/Network/ResourceDownloaderConfig.cs
Normal file
49
Assets/TcgEngine/Scripts/Network/ResourceDownloaderConfig.cs
Normal file
@@ -0,0 +1,49 @@
|
|||||||
|
using UnityEngine;
|
||||||
|
|
||||||
|
namespace TcgEngine
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 资源下载器配置文件
|
||||||
|
/// 用于存储服务器URL、资源端点等配置信息
|
||||||
|
/// </summary>
|
||||||
|
[CreateAssetMenu(fileName = "ResourceDownloaderConfig", menuName = "TcgEngine/ResourceDownloaderConfig", order = 0)]
|
||||||
|
public class ResourceDownloaderConfig : ScriptableObject
|
||||||
|
{
|
||||||
|
[Header("CDN Configuration")]
|
||||||
|
[Tooltip("资源服务器的基础URL")]
|
||||||
|
public string cdnUrl = "";
|
||||||
|
|
||||||
|
[Tooltip("游戏服务器URL")]
|
||||||
|
public string serverUrl = "";
|
||||||
|
|
||||||
|
[Tooltip("资源文件下载路径,{version}会被替换为实际版本号")]
|
||||||
|
public string resourcesEndpoint = "";
|
||||||
|
|
||||||
|
[Tooltip("版本检查接口")]
|
||||||
|
public string versionEndpoint = "";
|
||||||
|
|
||||||
|
[Header("Download Settings")]
|
||||||
|
[Tooltip("检查版本化文件是否存在")]
|
||||||
|
public bool validateVersionedUrl = true;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取完整的资源下载URL
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="version">版本号</param>
|
||||||
|
/// <returns>完整的下载URL</returns>
|
||||||
|
public string GetResourceUrl(string version)
|
||||||
|
{
|
||||||
|
string endpoint = resourcesEndpoint.Replace("{version}", version);
|
||||||
|
return cdnUrl + endpoint;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取版本检查URL
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>版本检查URL</returns>
|
||||||
|
public string GetVersionUrl()
|
||||||
|
{
|
||||||
|
return serverUrl + versionEndpoint;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: cdc2f7b92625e448fb76f26558cd0779
|
||||||
|
MonoImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
serializedVersion: 2
|
||||||
|
defaultReferences: []
|
||||||
|
executionOrder: 0
|
||||||
|
icon: {instanceID: 0}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
||||||
Reference in New Issue
Block a user