| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using UnityEditor;
- using UnityEditor.PackageManager;
- using UnityEditor.PackageManager.Requests;
- using UnityEngine;
- using PackageInfo = UnityEditor.PackageManager.PackageInfo;
- // All AppRuntimes package requests share one queue: UPM operations must be sequential.
- internal sealed class EditorPackageInstaller
- {
- static readonly List<EditorPackageInstaller> Pending = new List<EditorPackageInstaller>();
- static EditorPackageInstaller active;
- static AddRequest request;
- static double nextCheck;
- readonly string name, url, label, key, retryHint;
- internal EditorPackageInstaller(string name, string url, string label, string key, string retryHint)
- {
- this.name = name;
- this.url = url;
- this.label = label;
- this.key = key;
- this.retryHint = retryHint;
- }
- internal bool IsInstalled => PackageInfo.GetAllRegisteredPackages().Any(package => package.name == name);
- internal bool IsInstalling => active == this || Pending.Contains(this) && !IsInstalled;
- internal string LastError => SessionState.GetString(key + ".PackageError", "");
- internal void Retry()
- {
- if (active == this || IsInstalled) return;
- SessionState.EraseBool(key + ".PackageAttempted");
- SessionState.EraseString(key + ".PackageError");
- Schedule();
- }
- internal void Schedule()
- {
- if (!Pending.Contains(this)) Pending.Add(this);
- nextCheck = EditorApplication.timeSinceStartup + 1;
- EditorApplication.update -= Update;
- EditorApplication.update += Update;
- }
- static void Update()
- {
- if (request != null)
- {
- if (!request.IsCompleted) return;
- var completed = request;
- var installer = active;
- request = null;
- active = null;
- if (completed.Status == StatusCode.Success)
- {
- SessionState.EraseString(installer.key + ".PackageError");
- Debug.Log("[AppRuntimes] " + installer.label + " package installed. Wait for compilation to finish.");
- }
- else installer.ReportFailure(completed.Error?.message ?? "Package Manager did not return an error message.");
- nextCheck = EditorApplication.timeSinceStartup + 1;
- return;
- }
- if (Pending.Count == 0)
- {
- EditorApplication.update -= Update;
- return;
- }
- if (EditorApplication.timeSinceStartup < nextCheck || EditorApplication.isCompiling || EditorApplication.isUpdating ||
- EditorApplication.isPlayingOrWillChangePlaymode || BuildPipeline.isBuildingPlayer) return;
- var next = Pending[0];
- Pending.RemoveAt(0);
- if (next.IsInstalled)
- {
- SessionState.EraseString(next.key + ".PackageError");
- return;
- }
- if (SessionState.GetBool(next.key + ".PackageAttempted", false)) return;
- SessionState.SetBool(next.key + ".PackageAttempted", true);
- try
- {
- Debug.Log("[AppRuntimes] Installing the missing " + next.label + " package: " + next.url);
- active = next;
- request = Client.Add(next.url);
- }
- catch (Exception exception)
- {
- active = null;
- next.ReportFailure(exception.Message);
- }
- }
- void ReportFailure(string message)
- {
- SessionState.SetString(key + ".PackageError", message);
- Debug.LogWarning("[AppRuntimes] " + label + " package installation failed: " + message +
- "\n" + retryHint + " Or add this Git URL in Package Manager: " + url);
- }
- }
|