-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cc3a78e
commit c410277
Showing
12 changed files
with
620 additions
and
3 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,187 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Diagnostics; | ||
using System.IO; | ||
using System.Threading.Tasks; | ||
using GameFrameX.Runtime; | ||
using Newtonsoft.Json; | ||
using Newtonsoft.Json.Linq; | ||
using UnityEditor; | ||
using UnityEditor.PackageManager; | ||
using UnityEditor.PackageManager.Requests; | ||
using UnityEngine; | ||
using UnityEngine.Serialization; | ||
using Debug = UnityEngine.Debug; | ||
using PackageInfo = UnityEditor.PackageManager.PackageInfo; | ||
using Version = System.Version; | ||
|
||
namespace GameFrameX.Editor | ||
{ | ||
public partial class PackageManagerWindow : EditorWindow | ||
{ | ||
/// <summary> | ||
/// 包信息 | ||
/// </summary> | ||
public sealed class PackageManagerInfo | ||
{ | ||
/// <summary> | ||
/// 包名称 | ||
/// </summary> | ||
public string Name { get; } | ||
|
||
/// <summary> | ||
/// 包 git 地址 | ||
/// </summary> | ||
public string GitUrl { get; } | ||
|
||
/// <summary> | ||
/// 包信息 | ||
/// </summary> | ||
public PackageInfo Package { get; } | ||
|
||
/// <summary> | ||
/// 版本列表,Tags | ||
/// </summary> | ||
public List<string> Versions { get; } | ||
|
||
public PackageManagerInfo(string name, PackageInfo package, string gitUrl) | ||
{ | ||
Name = name; | ||
Package = package; | ||
GitUrl = gitUrl; | ||
Versions = new List<string>(); | ||
} | ||
} | ||
|
||
private List<PackageManagerInfo> _packages; | ||
private ListRequest _listRequest; | ||
|
||
private PackagesManifest PackagesManifest { get; set; } | ||
|
||
private void OnPackageListUpdate() | ||
{ | ||
if (_listRequest.IsCompleted) | ||
{ | ||
if (_listRequest.Status == StatusCode.Success) | ||
{ | ||
_packages.Clear(); | ||
foreach (var package in _listRequest.Result) | ||
{ | ||
if (package.name.StartsWith("com.unity.")) | ||
{ | ||
// 过滤 Unity 内置包 | ||
continue; | ||
} | ||
|
||
if (package.source == PackageSource.Git) | ||
{ | ||
if (PackagesManifest.Dependencies.TryGetValue(package.name, out var gitUrl)) | ||
{ | ||
var item = new PackageManagerInfo(package.name, package, gitUrl); | ||
_packages.Add(item); | ||
} | ||
} | ||
} | ||
|
||
_packages.Sort((x, y) => x.Name.CompareTo(y.Name)); | ||
} | ||
else | ||
{ | ||
Debug.LogError($"Error retrieving packages: {_listRequest.Error.message}"); | ||
} | ||
|
||
isLoadingPackageList = false; | ||
EditorApplication.update -= OnPackageListUpdate; | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// 是否正在加载包标签列表 | ||
/// </summary> | ||
public bool isLoadingPackageTagList = false; | ||
|
||
/// <summary> | ||
/// 获取 git 标签 | ||
/// </summary> | ||
/// <param name="gitUrl">从 git 获取的地址</param> | ||
/// <param name="package">包信息对象</param> | ||
private void FetchGitTags(string gitUrl, PackageManagerInfo package) | ||
{ | ||
if (package.Versions.Count > 0) | ||
{ | ||
return; | ||
} | ||
|
||
isLoadingPackageTagList = true; | ||
Task.Run(() => | ||
{ | ||
ProcessStartInfo startInfo = new ProcessStartInfo | ||
{ | ||
FileName = "git", | ||
Arguments = $"ls-remote --tags {gitUrl}", | ||
RedirectStandardOutput = true, | ||
UseShellExecute = false, | ||
CreateNoWindow = true | ||
}; | ||
using (Process process = Process.Start(startInfo)) | ||
{ | ||
using (StreamReader reader = process.StandardOutput) | ||
{ | ||
string result = reader.ReadToEnd(); | ||
string[] tags = result.Split('\n'); | ||
|
||
foreach (var tag in tags) | ||
{ | ||
if (!string.IsNullOrEmpty(tag) && !tag.Contains("^{")) | ||
{ | ||
// 提取标签名 | ||
var tagName = tag.Split('\t')[1].Replace("refs/tags/", "").Trim(); | ||
package.Versions.Add(tagName); | ||
} | ||
} | ||
|
||
package.Versions.Sort((x, y) => -x.CompareTo(y)); | ||
} | ||
} | ||
|
||
isLoadingPackageTagList = false; | ||
}); | ||
} | ||
|
||
/// <summary> | ||
/// 加载已安装的包 | ||
/// </summary> | ||
void StartLoadPackages() | ||
{ | ||
_packages = new List<PackageManagerInfo>(); | ||
var manifestPath = Path.Combine(Application.dataPath, "..", "Packages", "manifest.json"); | ||
string jsonContent = File.ReadAllText(manifestPath); | ||
PackagesManifest = JsonConvert.DeserializeObject<PackagesManifest>(jsonContent); | ||
} | ||
|
||
/// <summary> | ||
/// 是否正在加载包列表 | ||
/// </summary> | ||
private bool isLoadingPackageList = false; | ||
|
||
/// <summary> | ||
/// 加载所有包信息 | ||
/// </summary> | ||
void LoadPackages() | ||
{ | ||
isLoadingPackageList = true; | ||
_listRequest = Client.List(); | ||
EditorApplication.update += OnPackageListUpdate; | ||
} | ||
|
||
/// <summary> | ||
/// 保存包信息 | ||
/// </summary> | ||
/// <param name="manifest"></param> | ||
void SavePackages(PackagesManifest manifest) | ||
{ | ||
var manifestPath = Path.Combine(Application.dataPath, "..", "Packages", "manifest.json"); | ||
File.WriteAllText(manifestPath, JsonConvert.SerializeObject(manifest, Formatting.Indented)); | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
using UnityEngine; | ||
|
||
namespace GameFrameX.Editor | ||
{ | ||
public partial class PackageManagerWindow | ||
{ | ||
private GUIStyle _labelGreenStyle; | ||
|
||
private GUIStyle LabelGreenStyle | ||
{ | ||
get | ||
{ | ||
if (_labelGreenStyle == null) | ||
{ | ||
_labelGreenStyle = new GUIStyle(GUI.skin.label) | ||
{ | ||
normal = | ||
{ | ||
textColor = Color.green, // 设置文本颜色为绿色 | ||
}, | ||
alignment = TextAnchor.MiddleCenter, | ||
}; | ||
} | ||
|
||
return _labelGreenStyle; | ||
} | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
using System.Collections.Generic; | ||
using UnityEditor; | ||
using UnityEngine; | ||
|
||
namespace GameFrameX.Editor | ||
{ | ||
public partial class PackageManagerWindow | ||
{ | ||
// 下拉列表的选项 | ||
private readonly string[] _dropdownOptions = new string[] { "gitlink.org.cn", "gitee.com", "atomgit.com", "gitcode.net", "github.com", "gitlab.com", "framagit.org", }; | ||
private int _selectedDropdownIndex = 0; | ||
|
||
private void OnGUIByToolBar() | ||
{ | ||
// 创建一个工具条 | ||
// 工具条 | ||
GUILayout.BeginHorizontal(EditorStyles.toolbar); | ||
GUILayout.Label("镜像列表:", EditorStyles.label, GUILayout.Width(100)); // 设置宽度以确保一致性 | ||
// 下拉列表 | ||
int newDropdownIndex = EditorGUILayout.Popup(_selectedDropdownIndex, _dropdownOptions, EditorStyles.toolbarDropDown); | ||
if (newDropdownIndex != _selectedDropdownIndex) | ||
{ | ||
// 弹出提示框,请求确认 | ||
var confirmed = EditorUtility.DisplayDialog("切换提示", $"您将要将所有的包的下载地址切换为镜像地址:\n{_dropdownOptions[newDropdownIndex]}\n可能会遇到部分库没有的情况。请提交Issue反馈", "确认", "取消"); | ||
|
||
if (confirmed) | ||
{ | ||
// 如果用户选择"Yes",则更新所选索引 | ||
_selectedDropdownIndex = newDropdownIndex; | ||
Dictionary<string, string> dependencies = new Dictionary<string, string>(PackagesManifest.Dependencies); | ||
foreach (var manifestDependency in PackagesManifest.Dependencies) | ||
{ | ||
if (manifestDependency.Value.StartsWith("https://")) | ||
{ | ||
var path = manifestDependency.Value.Replace("https://", string.Empty).Replace("\\", "/"); | ||
var index = path.IndexOf("/", System.StringComparison.OrdinalIgnoreCase); | ||
if (index >= 0) | ||
{ | ||
path = path.Substring(index); | ||
} | ||
|
||
dependencies[manifestDependency.Key] = $"https://{_dropdownOptions[_selectedDropdownIndex]}{path}"; | ||
} | ||
} | ||
|
||
PackagesManifest.Dependencies = dependencies; | ||
SavePackages(PackagesManifest); | ||
} | ||
} | ||
|
||
// 自动伸缩的中间部分 | ||
GUILayout.FlexibleSpace(); // 使中间部分可以自动伸缩 | ||
|
||
if (GUILayout.Button("Refresh(重新加载)", EditorStyles.toolbarButton)) | ||
{ | ||
StartLoadPackages(); | ||
LoadPackages(); | ||
} | ||
|
||
GUILayout.FlexibleSpace(); // 使中间部分可以自动伸缩 | ||
|
||
// 更新按钮 | ||
if (GUILayout.Button("Update All Packages(更新所有包到最新)", EditorStyles.toolbarButton)) | ||
{ | ||
UpdateAllPackageHelper.UpdatePackages(); | ||
} | ||
|
||
GUILayout.EndHorizontal(); | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.