Skip to content

Commit

Permalink
Support 2020.3.41f1, 2021.3.12f1 or later
Browse files Browse the repository at this point in the history
Close #124, Close #126, Close #128
  • Loading branch information
mob-sakai committed Feb 21, 2023
1 parent e949d3e commit 80dc0a3
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 18 deletions.
27 changes: 27 additions & 0 deletions Editor/Coffee.UpmGitExtension/Extensions/PackageExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Collections.Generic;
using System.Text.RegularExpressions;
#if UNITY_2021_1_OR_NEWER
using UnityEditor.PackageManager.UI.Internal;
Expand All @@ -8,6 +9,32 @@

namespace Coffee.UpmGitExtension
{
internal static class IPackageVersionExtensions
{
public static PackageInfo GetPackageInfo(this IPackageVersion self)
{
return self is UpmPackageVersionEx ex
? ex.packageInfo
: PackageInfo.FindForAssetPath(self.localPath);
}
}

internal static class UpmPackageExtensions
{
public static void UpdateVersionsSafety(this UpmPackage self, IEnumerable<UpmPackageVersion> updatedVersions,
int numUnloadedVersions)
{
if (self.Has("UpdateVersions", new object[] { updatedVersions, numUnloadedVersions }))
{
self.Call("UpdateVersions", updatedVersions, numUnloadedVersions);
}
else
{
self.Call("UpdateVersions", updatedVersions);
}
}
}

internal static class PackageExtensions
{
static readonly Regex kRegexPackageId = new Regex("^([^@]+)@([^#]+)(#(.+))?$", RegexOptions.Compiled);
Expand Down
26 changes: 20 additions & 6 deletions Editor/Coffee.UpmGitExtension/Extensions/UpmPackageVersionEx.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
// IPackageVersion.packageInfo has been removed. (2020.3.41f1-, 2021.3.12f1-)
#if UNITY_2020_3_0 || UNITY_2020_3_1 || UNITY_2020_3_2 || UNITY_2020_3_3 || UNITY_2020_3_4 || UNITY_2020_3_5 || UNITY_2020_3_6 || UNITY_2020_3_7 || UNITY_2020_3_8 || UNITY_2020_3_9
#elif UNITY_2020_3_10 || UNITY_2020_3_11 || UNITY_2020_3_12 || UNITY_2020_3_13 || UNITY_2020_3_14 || UNITY_2020_3_15 || UNITY_2020_3_16 || UNITY_2020_3_17 || UNITY_2020_3_18 || UNITY_2020_3_19
#elif UNITY_2020_3_20 || UNITY_2020_3_21 || UNITY_2020_3_22 || UNITY_2020_3_23 || UNITY_2020_3_24 || UNITY_2020_3_25 || UNITY_2020_3_26 || UNITY_2020_3_27 || UNITY_2020_3_28 || UNITY_2020_3_29
#elif UNITY_2020_3_30 || UNITY_2020_3_31 || UNITY_2020_3_32 || UNITY_2020_3_33 || UNITY_2020_3_34 || UNITY_2020_3_35 || UNITY_2020_3_36 || UNITY_2020_3_37 || UNITY_2020_3_38 || UNITY_2020_3_39 || UNITY_2020_3_40
#elif UNITY_2021_3_0 || UNITY_2021_3_1 || UNITY_2021_3_2 || UNITY_2021_3_3 || UNITY_2021_3_4 || UNITY_2021_3_5 || UNITY_2021_3_6 || UNITY_2021_3_7 || UNITY_2021_3_8 || UNITY_2021_3_9 || UNITY_2021_3_10 || UNITY_2021_3_11
#elif UNITY_2020_3 || UNITY_2021_3
#define PACKAGE_INFO_HAS_BEEN_REMOVED
#endif

using System;
using System.Collections.Generic;
using System.IO;
using System.Text.RegularExpressions;
using System.Linq;
using UnityEngine;
using UnityEditor;
using UnityEditorInternal;
using UnityEditor.Scripting.ScriptCompilation;
#if UNITY_2021_1_OR_NEWER
using UnityEditor.PackageManager.UI.Internal;
Expand All @@ -21,6 +26,12 @@ internal class UpmPackageVersionEx : UpmPackageVersion
private static readonly Regex regex = new Regex("^(\\d +)\\.(\\d +)\\.(\\d +)(.*)$", RegexOptions.Compiled);
private static SemVersion? unityVersion;

#if PACKAGE_INFO_HAS_BEEN_REMOVED
[SerializeField]
private UnityEditor.PackageManager.PackageInfo m_PackageInfo;
public UnityEditor.PackageManager.PackageInfo packageInfo => m_PackageInfo;
#endif

public UpmPackageVersionEx(UnityEditor.PackageManager.PackageInfo packageInfo, bool isInstalled, bool isUnityPackage) : base(packageInfo, isInstalled, isUnityPackage)
{
}
Expand All @@ -29,8 +40,11 @@ public UpmPackageVersionEx(UnityEditor.PackageManager.PackageInfo packageInfo, b
{
}

public UpmPackageVersionEx(UpmPackageVersion packageVersion) : base(packageVersion.packageInfo, packageVersion.isInstalled, packageVersion.isUnityPackage)
public UpmPackageVersionEx(UpmPackageVersion packageVersion) : base(packageVersion.GetPackageInfo(), packageVersion.isInstalled, packageVersion.isUnityPackage)
{
#if PACKAGE_INFO_HAS_BEEN_REMOVED
m_PackageInfo = packageVersion.GetPackageInfo();
#endif
m_MinimumUnityVersion = UnityVersionToSemver(Application.unityVersion).ToString();
OnAfterDeserialize();
}
Expand Down
8 changes: 4 additions & 4 deletions Editor/Coffee.UpmGitExtension/GitPackageDataBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ internal static IPackageVersion GetPackageVersion(string packageUniqueId, string
public static void Fetch()
{
GetInstalledGitPackages()
.Select(p => p?.versions?.primary?.packageInfo?.GetSourceUrl())
.Select(p => p?.versions?.primary?.GetPackageInfo()?.GetSourceUrl())
.Where(url => !string.IsNullOrEmpty(url))
.Concat(GetCachedRepositoryUrls())
.Distinct()
Expand Down Expand Up @@ -228,7 +228,7 @@ private static void UpdateGitPackageVersions()
// Git mode: Register all installable package versions.
var upmPackage = _packageDatabase.GetPackage(versions.Key) as UpmPackage;
var installedVersion = upmPackage.versions.installed as UpmPackageVersion;
if (installedVersion.packageInfo.source != UnityEditor.PackageManager.PackageSource.Git)
if (installedVersion.GetPackageInfo().source != UnityEditor.PackageManager.PackageSource.Git)
return upmPackage;

// Unlock.
Expand All @@ -240,15 +240,15 @@ private static void UpdateGitPackageVersions()
.ThenBy(v => v.isInstalled)
.ToArray();

upmPackage.UpdateVersions(newVersions);
upmPackage.UpdateVersionsSafety(newVersions, 0);

return upmPackage;
}
else
{
// Registory mode: Register as installable package.
var upmPackage = new UpmPackage(versions.Key + " (git)", true, PackageType.ScopedRegistry);
upmPackage.UpdateVersions(versions.OrderBy(v => v.version));
upmPackage.UpdateVersionsSafety(versions.OrderBy(v => v.version), 0);
upmPackage.Set("m_Type", PackageType.MainNotUnity | PackageType.Installable);
return upmPackage;
}
Expand Down
16 changes: 8 additions & 8 deletions Editor/Coffee.UpmGitExtension/UI/PackageDetailsExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,11 @@ public void Setup(VisualElement root)
EditorApplication.delayCall += () =>
{
#if UNITY_2021_2_OR_NEWER || UNITY_2021_1_20 || UNITY_2021_1_21 || UNITY_2021_1_22 || UNITY_2021_1_23 || UNITY_2021_1_24 || UNITY_2021_1_25 || UNITY_2021_1_26 || UNITY_2021_1_27 || UNITY_2021_1_28
_pageManager.onVisualStateChange += _ => EditorApplication.delayCall += RefleshVersionItems;
_pageManager.onListUpdate += _ => EditorApplication.delayCall += RefleshVersionItems;
_pageManager.onVisualStateChange += _ => EditorApplication.delayCall += RefreshVersionItems;
_pageManager.onListUpdate += _ => EditorApplication.delayCall += RefreshVersionItems;
#else
_pageManager.onVisualStateChange += _ => RefleshVersionItems();
_pageManager.onListUpdate += (_, __, ___, ____) => RefleshVersionItems();
_pageManager.onVisualStateChange += _ => RefreshVersionItems();
_pageManager.onListUpdate += (_, __, ___, ____) => RefreshVersionItems();
#endif
};
}
Expand Down Expand Up @@ -183,19 +183,19 @@ private void ViewRepoOnBrowser()
{
if (_targetVersion != null)
{
Application.OpenURL(_targetVersion.packageInfo.GetRepositoryUrlForBrowser());
Application.OpenURL(_targetVersion.GetPackageInfo().GetRepositoryUrlForBrowser());
}
}

private void UpdatePackage()
{
if (_targetVersion?.packageInfo?.source == PackageSource.Git)
GitPackageDatabase.Install(_targetVersion.packageInfo.packageId);
if (_targetVersion?.GetPackageInfo()?.source == PackageSource.Git)
GitPackageDatabase.Install(_targetVersion.GetPackageInfo().packageId);
else
_clickableToUpdate?.Call("Invoke", new MouseDownEvent());
}

private void RefleshVersionItems()
private void RefreshVersionItems()
{
#if UNITY_2022_2_OR_NEWER
var items = _root.Query<PackageDetailsVersionHistoryItem>().Build()
Expand Down
3 changes: 3 additions & 0 deletions Editor/Commands/fetch-packages.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ const parseRef = text => {
m_MinimumUnityVersion: `${unity}.${unityRelease}`,
m_DisplayName: p.displayName,
m_Description: p.description,
m_Name: p.name,
m_PackageUniqueId: p.name,
m_PackageId: `${p.name}@${repositoryUrlWithPath}#${refName}`,
m_IsUnityPackage: false,
Expand All @@ -89,13 +90,15 @@ const parseRef = text => {
m_Author: author,
m_VersionString: p.version,
m_Tag: 4,
m_UpmErrors: [],
m_PackageInfo: {
m_PackageId: `${p.name}@${repositoryUrlWithPath}#${refName}`,
m_Name: p.name,
m_DisplayName: p.displayName,
m_Description: p.description,
m_Version: p.version,
m_Source: 5,
m_Errors: [],
m_Dependencies,
m_Git: {
m_Hash: hash,
Expand Down

0 comments on commit 80dc0a3

Please sign in to comment.