com.alicizax.unity.packagem.../Editor/PackageManager/UpdateAllPackageHelper.cs

145 lines
4.3 KiB
C#
Raw Normal View History

2025-02-20 13:15:18 +08:00
using System.Collections.Generic;
2026-04-17 14:21:41 +08:00
using UnityEditor;
2025-02-20 13:15:18 +08:00
namespace AlicizaX.PackageManager.Editor
{
internal static class UpdateAllPackageHelper
{
private static readonly Queue<RepositoryPackageData> PackagesToUpdate = new Queue<RepositoryPackageData>();
2026-04-17 14:21:41 +08:00
private static FrameworkPackageManagerWindow s_window;
private static int s_totalCount;
private static int s_currentIndex;
2025-02-20 13:15:18 +08:00
2026-04-17 14:21:41 +08:00
public static void UpdatePackages(FrameworkPackageManagerWindow window)
2025-02-20 13:15:18 +08:00
{
2026-04-17 14:21:41 +08:00
if (window == null)
2025-02-20 13:15:18 +08:00
{
2026-04-17 14:21:41 +08:00
return;
2025-02-20 13:15:18 +08:00
}
2026-04-17 14:21:41 +08:00
if (PackageOperationRunner.IsBusy)
{
EditorUtility.DisplayDialog("更新包", "当前有其他包操作正在执行。", "确定");
return;
}
var updatablePackages = CollectUpdatablePackages(window.RepositoryPackageDatas);
if (updatablePackages.Count == 0)
{
EditorUtility.DisplayDialog("更新包", "当前没有可更新的包。", "确定");
return;
}
var confirm = EditorUtility.DisplayDialog(
"更新包提示",
$"检测到 {updatablePackages.Count} 个包可更新,是否继续?\n更新完成后建议重新打开 Unity。",
"是",
"否");
if (!confirm)
{
return;
}
s_window = window;
PackagesToUpdate.Clear();
foreach (var package in updatablePackages)
{
PackagesToUpdate.Enqueue(package);
}
s_totalCount = PackagesToUpdate.Count;
s_currentIndex = 0;
UpdateNextPackage();
2025-02-20 13:15:18 +08:00
}
2026-04-17 14:21:41 +08:00
public static void UpdatePackage(FrameworkPackageManagerWindow window, RepositoryPackageData package)
2025-02-20 13:15:18 +08:00
{
2026-04-17 14:21:41 +08:00
if (window == null || package == null)
2025-02-20 13:15:18 +08:00
{
2026-04-17 14:21:41 +08:00
return;
}
if (PackageOperationRunner.IsBusy)
{
EditorUtility.DisplayDialog("更新包", "当前有其他包操作正在执行。", "确定");
return;
}
if (package.PackageState != PackageState.Update)
{
EditorUtility.DisplayDialog("更新包", "当前包没有检测到远端更新。", "确定");
return;
2025-02-20 13:15:18 +08:00
}
2026-04-17 14:21:41 +08:00
s_window = window;
PackagesToUpdate.Clear();
PackagesToUpdate.Enqueue(package);
s_totalCount = 1;
s_currentIndex = 0;
UpdateNextPackage();
2025-02-20 13:15:18 +08:00
}
2026-04-17 14:21:41 +08:00
private static List<RepositoryPackageData> CollectUpdatablePackages(List<RepositoryPackageData> repositoryPackageDatas)
2025-02-20 13:15:18 +08:00
{
2026-04-17 14:21:41 +08:00
var result = new List<RepositoryPackageData>();
if (repositoryPackageDatas == null)
2025-02-20 13:15:18 +08:00
{
2026-04-17 14:21:41 +08:00
return result;
2025-02-20 13:15:18 +08:00
}
2026-04-17 14:21:41 +08:00
foreach (var package in repositoryPackageDatas)
2025-02-20 13:15:18 +08:00
{
2026-04-17 14:21:41 +08:00
if (package == null || package.PackageState != PackageState.Update)
{
continue;
}
if (string.IsNullOrEmpty(package.cloneUrl))
{
continue;
}
result.Add(package);
2025-02-20 13:15:18 +08:00
}
2026-04-17 14:21:41 +08:00
return result;
2025-02-20 13:15:18 +08:00
}
private static void UpdateNextPackage()
{
2026-04-17 14:21:41 +08:00
if (PackagesToUpdate.Count == 0)
2025-02-20 13:15:18 +08:00
{
2026-04-17 14:21:41 +08:00
EditorUtility.ClearProgressBar();
s_window?.Refresh();
return;
2025-02-20 13:15:18 +08:00
}
2026-04-17 14:21:41 +08:00
s_currentIndex++;
var package = PackagesToUpdate.Dequeue();
var canceled = EditorUtility.DisplayCancelableProgressBar(
"正在更新包",
$"{s_currentIndex}/{s_totalCount} {package.name}",
(float)s_currentIndex / s_totalCount);
if (canceled)
2025-02-20 13:15:18 +08:00
{
2026-04-17 14:21:41 +08:00
PackagesToUpdate.Clear();
2025-02-20 13:15:18 +08:00
EditorUtility.ClearProgressBar();
2026-04-17 14:21:41 +08:00
return;
2025-02-20 13:15:18 +08:00
}
2026-04-17 14:21:41 +08:00
PackageManagerCheckTool.UpdatePackage(package, (success, _) =>
2025-02-20 13:15:18 +08:00
{
2026-04-17 14:21:41 +08:00
if (!success)
2025-02-20 13:15:18 +08:00
{
2026-04-17 14:21:41 +08:00
EditorUtility.ClearProgressBar();
return;
2025-02-20 13:15:18 +08:00
}
UpdateNextPackage();
2026-04-17 14:21:41 +08:00
});
2025-02-20 13:15:18 +08:00
}
}
}