add log percent

This commit is contained in:
CuongNV
2026-05-21 10:42:51 +07:00
parent 8f45a7f3ae
commit 84b84567c6
2 changed files with 65 additions and 11 deletions
@@ -165,28 +165,32 @@ namespace BrewMonster.Scripts
/// <summary>
/// Ensures dependencies for <paramref name="key"/> exist on disk; uses cache when CRC/catalog match (no redundant full download).
/// Logs download percent (and MB when known) every <paramref name="progressLogStepPercent"/> percent via <c>[Cuong]</c>.
/// </summary>
public static async UniTask<bool> DownloadDependenciesAsync(object key, bool autoReleaseHandle = true)
/// <param name="progressLogStepPercent">Log when percent crosses each step (1100). Default 5.</param>
public static async UniTask<bool> DownloadDependenciesAsync(
object key,
bool autoReleaseHandle = true,
int progressLogStepPercent = 5)
{
try
{
await EnsureInitializedAsync();
var keyLabel = key?.ToString() ?? "(null)";
Debug.Log($"[Cuong] AddressablesCatalogUpdater: Đang tải dependencies (key={keyLabel})...");
var step = Mathf.Clamp(progressLogStepPercent, 1, 100);
Debug.Log($"[Cuong] AddressablesCatalogUpdater: Bắt đầu tải dependencies (key={keyLabel})...");
var handle = Addressables.DownloadDependenciesAsync(key, autoReleaseHandle);
var lastLoggedMilestone = -1;
var lastLoggedPercent = -1;
LogDownloadProgress(keyLabel, handle, ref lastLoggedPercent, step, force: true);
while (!handle.IsDone)
{
var milestone = (int)(handle.PercentComplete * 10f);
if (milestone > lastLoggedMilestone)
{
lastLoggedMilestone = milestone;
Debug.Log($"[Cuong] AddressablesCatalogUpdater: Đang tải '{keyLabel}'... {handle.PercentComplete * 100f:F0}%");
}
LogDownloadProgress(keyLabel, handle, ref lastLoggedPercent, step);
await UniTask.Yield();
}
LogDownloadProgress(keyLabel, handle, ref lastLoggedPercent, step, force: true);
await handle.ToUniTask();
if (handle.Status != AsyncOperationStatus.Succeeded)
{
@@ -206,6 +210,39 @@ namespace BrewMonster.Scripts
}
}
static void LogDownloadProgress(
string keyLabel,
AsyncOperationHandle handle,
ref int lastLoggedPercent,
int stepPercent,
bool force = false)
{
var status = handle.GetDownloadStatus();
var percent = Mathf.Clamp(status.Percent * 100f, 0f, 100f);
var percentRounded = Mathf.RoundToInt(percent);
if (!force)
{
if (percentRounded <= lastLoggedPercent)
return;
if (percentRounded < 100 && percentRounded - lastLoggedPercent < stepPercent)
return;
}
lastLoggedPercent = percentRounded;
if (status.TotalBytes > 0)
{
var downloadedMb = status.DownloadedBytes / (1024f * 1024f);
var totalMb = status.TotalBytes / (1024f * 1024f);
Debug.Log(
$"[Cuong] AddressablesCatalogUpdater: {percent:F0}% ({downloadedMb:F1}/{totalMb:F1} MB) — key={keyLabel}");
}
else
{
Debug.Log($"[Cuong] AddressablesCatalogUpdater: {percent:F0}% — key={keyLabel}");
}
}
/// <summary>
/// Removes bundles not referenced by the given catalogs (optional maintenance / “clear old cache”).
/// </summary>
@@ -183,8 +183,25 @@ namespace BrewMonster.Scripts
return new BootstrapResult(false, true, "remoteBulkDownloadLabel empty", fetch.ContentVersion);
}
Debug.Log($"[Cuong] GameContentBootstrap: Đang tải toàn bộ remote content (label={_remoteBulkDownloadLabel})...");
bool dl = await AddressablesCatalogUpdater.DownloadDependenciesAsync(_remoteBulkDownloadLabel.Trim(), true);
var bulkLabel = _remoteBulkDownloadLabel.Trim();
var downloadBytes = await AddressablesCatalogUpdater.GetDownloadSizeBytesAsync(bulkLabel);
if (downloadBytes > 0)
{
Debug.Log(
$"[Cuong] GameContentBootstrap: Đang tải remote content (label={bulkLabel}), dung lượng cần tải ~{downloadBytes / (1024f * 1024f):F1} MB...");
}
else if (downloadBytes == 0)
{
Debug.Log(
$"[Cuong] GameContentBootstrap: Remote content (label={bulkLabel}) đã có trong cache — vẫn chạy DownloadDependencies để đồng bộ.");
}
else
{
Debug.Log(
$"[Cuong] GameContentBootstrap: Đang tải remote content (label={bulkLabel}) — không lấy được dung lượng trước khi tải.");
}
bool dl = await AddressablesCatalogUpdater.DownloadDependenciesAsync(bulkLabel, true);
if (!dl)
{
Debug.LogError("[Cuong] GameContentBootstrap: Tải remote content thất bại.");