Merge remote-tracking branch 'origin/main'

This commit is contained in:
YiHan0621
2025-08-28 18:36:01 +08:00
13 changed files with 198 additions and 188 deletions

View File

@@ -34,10 +34,12 @@ MonoBehaviour:
probability: 90
- variant: {fileID: 11400000, guid: 3a827aeeaca139641a4b8ad0c3c74f52, type: 2}
probability: 10
title: 黄金卡包
title: "\u9EC4\u91D1\u5361\u5305"
pack_img: {fileID: 21300000, guid: e400c282187e5f54dab6aacfd8adae20, type: 3}
cardback_img: {fileID: 21300000, guid: 96d8dad709a2729419f19d1d4633a0ef, type: 3}
desc: 精英卡包内含5张卡牌。更有机会获得稀有卡牌和神话卡牌。.
pack_img_path: Cardbacks/pack_gold.png
cardback_img_path: Cardbacks/cardback_gold.png
desc: "\u7CBE\u82F1\u5361\u5305\uFF0C\u5185\u542B5\u5F20\u5361\u724C\u3002\u66F4\u6709\u673A\u4F1A\u83B7\u5F97\u7A00\u6709\u5361\u724C\u548C\u795E\u8BDD\u5361\u724C\u3002."
sort_order: 1
available: 1
cost: 250

View File

@@ -36,10 +36,12 @@ MonoBehaviour:
probability: 96
- variant: {fileID: 11400000, guid: 3a827aeeaca139641a4b8ad0c3c74f52, type: 2}
probability: 4
title: 白银卡包
pack_img: {fileID: 21300000, guid: b2c2a8efa1f7d7b4593a512e2c8cd61a, type: 3}
title: "\u767D\u94F6\u5361\u5305"
pack_img: {fileID: 0}
cardback_img: {fileID: 21300000, guid: 0e839bd6251bf9a4697b9cb18c1565a4, type: 3}
desc: 白银卡包内含5张卡牌.
pack_img_path: Cardbacks/pack_silver.png
cardback_img_path: Cardbacks/cardback_silver.png
desc: "\u767D\u94F6\u5361\u5305\uFF0C\u5185\u542B5\u5F20\u5361\u724C."
sort_order: 0
available: 1
cost: 100

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

@@ -24,6 +24,11 @@ namespace TcgEngine
public string title;
public Sprite pack_img;
public Sprite cardback_img;
[Header("Dynamic Art Paths")]
public string pack_img_path;
public string cardback_img_path;
[TextArea(5, 10)]
public string desc;
public int sort_order;
@@ -56,6 +61,54 @@ namespace TcgEngine
{
return desc;
}
/// <summary>
/// 获取Pack图片优先使用动态路径
/// </summary>
public Sprite GetPackImage()
{
if (!string.IsNullOrEmpty(pack_img_path))
{
Sprite dynamicSprite = SpriteLoader.Get()?.LoadSprite(pack_img_path);
if (dynamicSprite != null)
{
return dynamicSprite;
}
else
{
Debug.LogWarning($"Pack {id} 图片加载失败: {pack_img_path}");
}
}
else
{
Debug.LogWarning($"Pack {id} pack_img_path为空");
}
return pack_img;
}
/// <summary>
/// 获取卡背图片,优先使用动态路径
/// </summary>
public Sprite GetCardbackImage()
{
if (!string.IsNullOrEmpty(cardback_img_path))
{
Sprite dynamicSprite = SpriteLoader.Get()?.LoadSprite(cardback_img_path);
if (dynamicSprite != null)
{
return dynamicSprite;
}
else
{
Debug.LogWarning($"Pack {id} 卡背图片加载失败: {cardback_img_path}");
}
}
else
{
Debug.LogWarning($"Pack {id} cardback_img_path为空");
}
return cardback_img;
}
public static PackData Get(string id)
{

View File

@@ -127,7 +127,7 @@ namespace TcgEngine.Client
PackData ipack = PackData.Get(pack.tid);
if (ipack)
{
pack_sprite.sprite = ipack.pack_img;
pack_sprite.sprite = ipack.GetPackImage();
}
}

View File

@@ -70,7 +70,7 @@ namespace TcgEngine.Client
this.variant = variant;
if (cardback != null)
cardback.sprite = pack.cardback_img;
cardback.sprite = pack.GetCardbackImage();
card_ui.SetCard(card, variant);
new_card?.SetActive(false);

View File

@@ -181,19 +181,6 @@ namespace TcgEngine
downloadCompleted = true;
UpdateProgress(1f);
UpdateStatus("资源解压完成!");
// 打印解压后的文件夹位置
if (ResourceDownloader.Get() != null)
{
ResourceDownloader.Get().LogResourcePaths();
// 另外也可以直接打印路径
string spritesPath = ResourceDownloader.Get().GetSpritesDirectoryPath();
Debug.Log($"[ResourceInitializer] 资源解压到: {spritesPath}");
// 在macOS上可以直接用这个命令在终端中打开文件夹
Debug.Log($"[ResourceInitializer] 打开文件夹命令: open \"{spritesPath}\"");
}
}
/// <summary>

View File

@@ -14,22 +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 = "/api/version";
[Header("URL Pattern Options")]
[Tooltip("支持的占位符: {version} - 版本号")]
public bool useVersionInFilename = true;
[Tooltip("如果启用,会先检查版本化文件是否存在")]
public bool validateVersionedUrl = false;
[Tooltip("调试模式:当版本接口失败时,使用固定版本号")]
public bool debugMode = true;
[Tooltip("调试模式下使用的固定版本号")]
public string debugVersion = "0.0.1";
[Tooltip("调试模式下使用的固定MD5值")]
public string debugMd5 = "ceb24758054d6dcf1e23ddb41811a525";
[Header("Configuration")]
[Tooltip("资源下载器配置文件")]
public ResourceDownloaderConfig config;
private string currentVersion = "0.0.0";
private string currentMd5 = "";
private string targetVersion = "0.0.1";
@@ -86,19 +73,7 @@ namespace TcgEngine
Debug.Log($"Starting update check. Current version: {currentVersion}, Current MD5: {currentMd5}");
var serverVersionData = await GetServerVersionData();
if (serverVersionData == null || string.IsNullOrEmpty(serverVersionData.version))
{
Debug.LogWarning("Failed to get server version");
// 调试模式:版本接口失败时使用固定版本号
if (debugMode)
{
Debug.Log($"Debug mode enabled: using fixed version {debugVersion} and MD5 {debugMd5}");
targetVersion = debugVersion;
targetMd5 = debugMd5;
return await CheckVersionAndMd5();
}
{
return false;
}
@@ -112,16 +87,6 @@ namespace TcgEngine
{
Debug.LogError($"Error checking for updates: {e.Message}");
// 调试模式:出现异常时也尝试使用固定版本
if (debugMode)
{
Debug.Log($"Debug mode: attempting to use version {debugVersion} despite error");
targetVersion = debugVersion;
targetMd5 = debugMd5;
return await CheckVersionAndMd5();
}
return false;
}
}
@@ -152,20 +117,13 @@ namespace TcgEngine
return false;
}
Debug.Log($"版本匹配开始检查MD5校验...");
Debug.Log($"当前版本: {currentVersion}");
Debug.Log($"目标版本: {targetVersion}");
Debug.Log($"保存的MD5: {currentMd5}");
Debug.Log($"目标MD5: {targetMd5}");
Debug.Log($"版本匹配开始检查MD5校验... 当前版本: {currentVersion} 目标版本: {targetVersion} 当前MD5: {currentMd5} 目标MD5: {targetMd5}");
string localMd5 = await CalculateDirectoryMd5(spritesPath);
if (localMd5 != targetMd5)
{
Debug.LogWarning($"🚨 MD5校验失败");
Debug.LogWarning($"本地计算MD5: {localMd5}");
Debug.LogWarning($"期望的MD5: {targetMd5}");
Debug.Log("正在删除损坏的文件并重新下载...");
Debug.Log("MD5校验失败,正在删除损坏的文件并重新下载...");
// 删除损坏的文件
if (Directory.Exists(spritesPath))
@@ -176,8 +134,7 @@ namespace TcgEngine
return true; // 需要重新下载
}
Debug.Log($"✅ MD5校验通过: {localMd5}");
Debug.Log("文件完整性验证成功,无需下载");
Debug.Log($"✅ MD5校验通过");
return false; // 无需下载
}
@@ -195,25 +152,9 @@ namespace TcgEngine
if (files.Length == 0)
return "";
Debug.Log($"=== 开始计算目录MD5 ===");
Debug.Log($"目录路径: {directoryPath}");
Debug.Log($"文件数量: {files.Length}");
// 按路径排序确保一致性
Array.Sort(files);
// 打印前几个文件作为示例
Debug.Log("文件列表示例:");
for (int i = 0; i < Math.Min(5, files.Length); i++)
{
string relativePath = files[i].Substring(directoryPath.Length + 1).Replace('\\', '/');
Debug.Log($" [{i+1}] {relativePath}");
}
if (files.Length > 5)
{
Debug.Log($" ... 还有 {files.Length - 5} 个文件");
}
using (var md5 = System.Security.Cryptography.MD5.Create())
{
foreach (string file in files)
@@ -238,12 +179,6 @@ namespace TcgEngine
md5.TransformFinalBlock(new byte[0], 0, 0);
string result = BitConverter.ToString(md5.Hash).Replace("-", "").ToLowerInvariant();
Debug.Log($"=== MD5计算完成 ===");
Debug.Log($"本地计算的MD5: {result}");
Debug.Log($"目标MD5: {targetMd5}");
Debug.Log($"MD5匹配: {(result == targetMd5 ? " " : " ")}");
Debug.Log($"===================");
return result;
}
}
@@ -261,24 +196,23 @@ namespace TcgEngine
{
string targetFileName;
if (!useVersionInFilename)
if (string.IsNullOrEmpty(version))
{
// 不使用版本化文件名时,统一使用"sprites"
targetFileName = "sprites";
}
else if (string.IsNullOrEmpty(version))
{
// 如果版本为空,使用调试版本
targetFileName = debugVersion;
Debug.LogError("Version is null");
return "0.0.0";
}
else
{
// 使用实际版本号
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;
@@ -289,7 +223,7 @@ namespace TcgEngine
/// </summary>
private async Task<bool> ValidateVersionedUrl(string url)
{
if (!validateVersionedUrl)
if (config == null || !config.validateVersionedUrl)
return true;
try
@@ -327,17 +261,13 @@ namespace TcgEngine
/// </summary>
private async Task<ResourceVersionResponse> GetServerVersionData()
{
// 从NetworkData获取游戏服务器地址
string gameServerUrl = NetworkData.Get().api_url;
if (string.IsNullOrEmpty(gameServerUrl))
if (config == null)
{
Debug.LogError("Failed to get game server URL from NetworkData");
Debug.LogError("ResourceDownloaderConfig is not assigned!");
return null;
}
// 构建完整的版本检查URL
string protocol = NetworkData.Get().api_https ? "https://" : "http://";
string url = protocol + gameServerUrl + versionEndpoint;
string url = config.GetVersionUrl();
Debug.Log($"Version check URL: {url}");
using (UnityWebRequest request = UnityWebRequest.Get(url))
{
@@ -382,7 +312,7 @@ namespace TcgEngine
string downloadUrl = BuildVersionedDownloadUrl(targetVersion);
// 验证URL是否存在可选
if (validateVersionedUrl)
if (config != null && config.validateVersionedUrl)
{
bool urlExists = await ValidateVersionedUrl(downloadUrl);
if (!urlExists)
@@ -584,79 +514,6 @@ namespace TcgEngine
return currentVersion;
}
/// <summary>
/// 获取指定版本的下载URL用于调试
/// </summary>
public string GetDownloadUrlForVersion(string version)
{
return BuildVersionedDownloadUrl(version);
}
/// <summary>
/// 获取当前目标版本的下载URL
/// </summary>
public string GetCurrentDownloadUrl()
{
return BuildVersionedDownloadUrl(targetVersion);
}
/// <summary>
/// 获取本地Sprites目录的完整路径用于调试
/// </summary>
public string GetSpritesDirectoryPath()
{
return spritesPath;
}
/// <summary>
/// 手动计算并打印当前Sprites目录的MD5
/// </summary>
public async void CalculateAndLogCurrentMd5()
{
Debug.Log("=== 手动计算当前Sprites目录MD5 ===");
if (!Directory.Exists(spritesPath))
{
Debug.LogWarning("Sprites目录不存在");
return;
}
string calculatedMd5 = await CalculateDirectoryMd5(spritesPath);
Debug.Log($"手动计算结果: {calculatedMd5}");
Debug.Log($"当前保存的MD5: {currentMd5}");
Debug.Log($"调试MD5: {debugMd5}");
Debug.Log("================================");
}
/// <summary>
/// 打印本地资源路径信息
/// </summary>
public void LogResourcePaths()
{
Debug.Log($"=== Resource Paths ===");
Debug.Log($"persistentDataPath: {persistentDataPath}");
Debug.Log($"Sprites Directory: {spritesPath}");
Debug.Log($"Directory Exists: {Directory.Exists(spritesPath)}");
if (Directory.Exists(spritesPath))
{
var files = Directory.GetFiles(spritesPath, "*", SearchOption.AllDirectories);
Debug.Log($"Total files in Sprites: {files.Length}");
// 显示前10个文件作为示例
for (int i = 0; i < Math.Min(10, files.Length); i++)
{
string relativePath = files[i].Substring(spritesPath.Length + 1);
Debug.Log($" {relativePath}");
}
if (files.Length > 10)
{
Debug.Log($" ... and {files.Length - 10} more files");
}
}
Debug.Log($"======================");
}
public static ResourceDownloader Get()
{
if (instance == null)
@@ -665,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:

View File

@@ -41,7 +41,7 @@ namespace TcgEngine.UI
pack_title.text = pack.title;
}
pack_img.enabled = true;
pack_img.sprite = pack.pack_img;
pack_img.sprite = pack.GetPackImage();
}
if (pack_quantity != null)