版本验证配置

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

@@ -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>