版本验证配置

This commit is contained in:
xianyi
2025-08-28 17:44:28 +08:00
parent c458b0c46e
commit 565e2ddf46
6 changed files with 128 additions and 14 deletions

View 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

View File

@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 5852c171f99424772952bce4e3303421
NativeFormatImporter:
externalObjects: {}
mainObjectFileID: 11400000
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -467,7 +467,7 @@ MonoBehaviour:
nextSceneName: LoginMenu
fadeInDuration: 1
fadeOutDuration: 1
minimumDisplayTime: 3
minimumDisplayTime: 0
splashMusic: {fileID: 0}
--- !u!114 &507621921
MonoBehaviour:

View File

@@ -14,14 +14,9 @@ namespace TcgEngine
/// </summary>
public class ResourceDownloader : MonoBehaviour
{
[Header("Download Settings")]
public string serverUrl = "https://cardcdn.ambigrat.com";
public string resourcesEndpoint = "/test/{version}.zip";
public string versionEndpoint = "/version";
[Header("URL Pattern Options")]
[Tooltip("检查版本化文件是否存在")]
public bool validateVersionedUrl = true;
[Header("Configuration")]
[Tooltip("资源下载器配置文件")]
public ResourceDownloaderConfig config;
private string currentVersion = "0.0.0";
private string currentMd5 = "";
private string targetVersion = "0.0.1";
@@ -211,8 +206,13 @@ namespace TcgEngine
targetFileName = version;
}
string endpoint = resourcesEndpoint.Replace("{version}", targetFileName);
string url = serverUrl + endpoint;
if (config == null)
{
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})");
return url;
@@ -223,7 +223,7 @@ namespace TcgEngine
/// </summary>
private async Task<bool> ValidateVersionedUrl(string url)
{
if (!validateVersionedUrl)
if (config == null || !config.validateVersionedUrl)
return true;
try
@@ -261,8 +261,13 @@ namespace TcgEngine
/// </summary>
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}");
using (UnityWebRequest request = UnityWebRequest.Get(url))
{
@@ -307,7 +312,7 @@ namespace TcgEngine
string downloadUrl = BuildVersionedDownloadUrl(targetVersion);
// 验证URL是否存在可选
if (validateVersionedUrl)
if (config != null && config.validateVersionedUrl)
{
bool urlExists = await ValidateVersionedUrl(downloadUrl);
if (!urlExists)
@@ -517,10 +522,32 @@ namespace TcgEngine
GameObject go = new GameObject("ResourceDownloader");
instance = go.AddComponent<ResourceDownloader>();
DontDestroyOnLoad(go);
// 加载配置文件
instance.LoadConfig();
Debug.Log("Created new ResourceDownloader 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>

View 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;
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: cdc2f7b92625e448fb76f26558cd0779
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: