Skip to content

Commit

Permalink
fix: support 2021.3.26 or later
Browse files Browse the repository at this point in the history
  • Loading branch information
mob-sakai committed Nov 20, 2023
1 parent 9bf490f commit 062454f
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 15 deletions.
39 changes: 39 additions & 0 deletions Assets/Tests/ReflectionTests.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
#if UNITY_2020_1_OR_NEWER
using System;
using System.Collections;
using System.Linq;
using NUnit.Framework;
using UnityEngine;
using UnityEngine.UIElements;
using UnityEngine.TestTools;
#if UNITY_2021_1_OR_NEWER
using UnityEditor.PackageManager.UI.Internal;

#else
using UnityEditor.PackageManager.UI;
#endif
#if UNITY_2023_1_OR_NEWER
using UpmPackage = UnityEditor.PackageManager.UI.Internal.Package;
#endif

namespace Coffee.UpmGitExtension
{
Expand All @@ -34,6 +40,39 @@ public void UpmPackageVersion_Tag()
);
}

[UnityTest]
public IEnumerator UpmPackage_UpdateVersions()
{
if (Application.isBatchMode) yield break; // Skip in batch mode.
#if UNITY_2020_1
var upmClient = UpmClient.instance;
var database = PackageDatabase.instance;
#else
var upmClient = UnityEditor.ScriptableSingleton<ServicesContainer>.instance.Resolve<UpmClient>();
var database = UnityEditor.ScriptableSingleton<ServicesContainer>.instance.Resolve<PackageDatabase>();
#endif

IOperation current = null;
void onListOperation(IOperation op)
{
current = op;
upmClient.onListOperation -= onListOperation;
}

upmClient.onListOperation += onListOperation;
upmClient.List(true);

while (current == null || current.isInProgress)
{
yield return null;
}

var package = database.GetPackage("com.coffee.softmask-for-ugui") as UpmPackage;
Console.WriteLine(JsonUtility.ToJson(package, true));
Debug.Log(JsonUtility.ToJson(package, true));
package.UpdateVersionsSafety(new[] { package.GetInstalledVersion() });
}

#if UNITY_2023_1_OR_NEWER
#elif UNITY_2021_2_OR_NEWER
[Test]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
// UpmPackage.UpdateVersions is removed from Unity 2021.3.26

#if 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
#elif UNITY_2021_3_10 || UNITY_2021_3_11 || UNITY_2021_3_12 || UNITY_2021_3_13 || UNITY_2021_3_14 || UNITY_2021_3_15 || UNITY_2021_3_16 || UNITY_2021_3_17 || UNITY_2021_3_18 || UNITY_2021_3_19
#elif UNITY_2021_3_20 || UNITY_2021_3_21 || UNITY_2021_3_22 || UNITY_2021_3_23 || UNITY_2021_3_24 || UNITY_2021_3_25
#elif UNITY_2021_3
#define UNITY_2021_3_26_OR_NEWER
#endif

using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using UnityEditor.PackageManager;
#if UNITY_2021_1_OR_NEWER
Expand All @@ -24,16 +34,30 @@ public static PackageInfo GetPackageInfo(this IPackageVersion self)

internal static class UpmPackageExtensions
{
public static void UpdateVersionsSafety(this UpmPackage self, IEnumerable<UpmPackageVersion> updatedVersions)
public static UpmPackage UpdateVersionsSafety(this UpmPackage self, IEnumerable<UpmPackageVersion> versions)
{
if (self.Has("UpdateVersions", updatedVersions, 0))
#if UNITY_2023_1_OR_NEWER
var factory = UnityEditor.ScriptableSingleton<ServicesContainer>.instance.Resolve<UpmPackageFactory>();
self = factory.CreatePackage(self.name, new UpmVersionList(versions.OrderBy(v => v.version)));
#elif UNITY_2022_2_OR_NEWER || UNITY_2021_3_26_OR_NEWER
self = new UpmPackage(self.uniqueId, true, new UpmVersionList(versions.OrderBy(v => v.version)));
#else
if (self.Has("UpdateVersions", versions, 0))
{
self.Call("UpdateVersions", updatedVersions, 0);
self.Call("UpdateVersions", versions, 0);
}
else if (self.Has("UpdateVersions", versions))
{
self.Call("UpdateVersions", versions);
}
else
{
self.Call("UpdateVersions", updatedVersions);
throw new System.NotImplementedException(
"void UpmPackage.UpdateVersions(IEnumerable<UpmPackageVersion>, int) or void UpmPackage.UpdateVersions(IEnumerable<UpmPackageVersion>) is not found");
}
#endif

return self;
}
}

Expand Down
12 changes: 1 addition & 11 deletions Packages/src/Editor/Coffee.UpmGitExtension/GitPackageDataBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -243,17 +243,7 @@ private static void UpdateGitPackageVersions()
.OrderBy(v => v.semVersion)
.ThenBy(v => v.isInstalled)
.ToArray();

#if UNITY_2023_1_OR_NEWER
var factory = ScriptableSingleton<ServicesContainer>.instance.Resolve<UpmPackageFactory>();
var versionList = new UpmVersionList(newVersions.OrderBy(v => v.version));
upmPackage = factory.CreatePackage(upmPackage.name, versionList);
#elif UNITY_2022_2_OR_NEWER
var versionList = new UpmVersionList(newVersions.OrderBy(v => v.version));
upmPackage = new UpmPackage(upmPackage.uniqueId, true, versionList);
#else
upmPackage.UpdateVersionsSafety(newVersions);
#endif
upmPackage = upmPackage.UpdateVersionsSafety(newVersions);

return upmPackage;
}
Expand Down

0 comments on commit 062454f

Please sign in to comment.