This commit is contained in:
@@ -0,0 +1,175 @@
|
||||
#if UNITY_EDITOR
|
||||
#nullable enable
|
||||
|
||||
using System;
|
||||
using System.Linq;
|
||||
using UnityEditor;
|
||||
using UnityEditor.PackageManager;
|
||||
using UnityEditor.PackageManager.Requests;
|
||||
using UnityEngine;
|
||||
|
||||
namespace ShrinkSDK.Installer
|
||||
{
|
||||
internal sealed class ShrinkSdkInstallerWindow : EditorWindow
|
||||
{
|
||||
private AddRequest? _addRequest;
|
||||
private ListRequest? _listRequest;
|
||||
private ResolveRequest? _resolveRequest;
|
||||
private ShrinkSdkPackageEntry? _pendingInstall;
|
||||
private int _selectedPackageIndex;
|
||||
private string _status = "Ready";
|
||||
private MessageType _statusType = MessageType.Info;
|
||||
|
||||
[MenuItem("ShrinkSDK/Packages")]
|
||||
private static void Open()
|
||||
{
|
||||
GetWindow<ShrinkSdkInstallerWindow>("ShrinkSDK Packages");
|
||||
}
|
||||
|
||||
private void OnEnable()
|
||||
{
|
||||
RefreshDiagnostics();
|
||||
}
|
||||
|
||||
private void OnGUI()
|
||||
{
|
||||
EditorGUILayout.LabelField("ShrinkSDK", EditorStyles.boldLabel);
|
||||
EditorGUILayout.Space(4f);
|
||||
EditorGUILayout.LabelField("Registry", ShrinkSdkManifestStore.HasShrinkSdkRegistry()
|
||||
? ShrinkSdkPackageCatalog.RegistryUrl
|
||||
: "Not configured");
|
||||
|
||||
using (new EditorGUI.DisabledScope(IsBusy))
|
||||
{
|
||||
if (GUILayout.Button("Configure Registry"))
|
||||
{
|
||||
ConfigureRegistry();
|
||||
}
|
||||
|
||||
EditorGUILayout.Space(8f);
|
||||
if (GUILayout.Button("Install Starter Basic"))
|
||||
{
|
||||
BeginInstall(ShrinkSdkPackageCatalog.StarterBasic);
|
||||
}
|
||||
|
||||
var labels = ShrinkSdkPackageCatalog.Packages.Select(entry =>
|
||||
entry.DisplayName + " " + entry.Version).ToArray();
|
||||
_selectedPackageIndex = EditorGUILayout.Popup("Package", _selectedPackageIndex, labels);
|
||||
if (GUILayout.Button("Install Selected Package"))
|
||||
{
|
||||
BeginInstall(ShrinkSdkPackageCatalog.Packages[_selectedPackageIndex]);
|
||||
}
|
||||
|
||||
if (GUILayout.Button("Refresh Diagnostics"))
|
||||
{
|
||||
RefreshDiagnostics();
|
||||
}
|
||||
}
|
||||
|
||||
EditorGUILayout.Space(8f);
|
||||
EditorGUILayout.HelpBox(_status, _statusType);
|
||||
}
|
||||
|
||||
private bool IsBusy => _resolveRequest != null || _addRequest != null || _listRequest != null;
|
||||
|
||||
private void Update()
|
||||
{
|
||||
if (_resolveRequest != null && _resolveRequest.IsCompleted)
|
||||
{
|
||||
var request = _resolveRequest;
|
||||
_resolveRequest = null;
|
||||
if (request.Status != StatusCode.Success)
|
||||
{
|
||||
SetStatus("Registry resolve failed: " + request.Error.message, MessageType.Error);
|
||||
return;
|
||||
}
|
||||
|
||||
if (_pendingInstall != null)
|
||||
{
|
||||
_addRequest = Client.Add(_pendingInstall.PackageSpecifier);
|
||||
SetStatus("Installing " + _pendingInstall.PackageSpecifier, MessageType.Info);
|
||||
}
|
||||
}
|
||||
|
||||
if (_addRequest != null && _addRequest.IsCompleted)
|
||||
{
|
||||
var request = _addRequest;
|
||||
_addRequest = null;
|
||||
if (request.Status == StatusCode.Success)
|
||||
{
|
||||
SetStatus("Installed " + request.Result.name + " " + request.Result.version, MessageType.Info);
|
||||
}
|
||||
else
|
||||
{
|
||||
SetStatus("Package install failed: " + request.Error.message, MessageType.Error);
|
||||
}
|
||||
|
||||
_pendingInstall = null;
|
||||
}
|
||||
|
||||
if (_listRequest != null && _listRequest.IsCompleted)
|
||||
{
|
||||
var request = _listRequest;
|
||||
_listRequest = null;
|
||||
if (request.Status == StatusCode.Success)
|
||||
{
|
||||
var installed = request.Result.Count(package => package.name.StartsWith("com.cneicy.", StringComparison.Ordinal));
|
||||
SetStatus("Registry is configured. Installed ShrinkSDK packages: " + installed, MessageType.Info);
|
||||
}
|
||||
else
|
||||
{
|
||||
SetStatus("Package diagnostics failed: " + request.Error.message, MessageType.Warning);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void ConfigureRegistry()
|
||||
{
|
||||
try
|
||||
{
|
||||
ShrinkSdkManifestStore.EnsureShrinkSdkRegistry();
|
||||
_pendingInstall = null;
|
||||
_resolveRequest = Client.Resolve();
|
||||
SetStatus("Registry configuration written. Resolving packages.", MessageType.Info);
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
SetStatus(exception.Message, MessageType.Error);
|
||||
}
|
||||
}
|
||||
|
||||
private void BeginInstall(ShrinkSdkPackageEntry entry)
|
||||
{
|
||||
try
|
||||
{
|
||||
ShrinkSdkManifestStore.EnsureShrinkSdkRegistry();
|
||||
_pendingInstall = entry;
|
||||
_resolveRequest = Client.Resolve();
|
||||
SetStatus("Resolving registry for " + entry.PackageSpecifier, MessageType.Info);
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
SetStatus(exception.Message, MessageType.Error);
|
||||
}
|
||||
}
|
||||
|
||||
private void RefreshDiagnostics()
|
||||
{
|
||||
if (IsBusy)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
_listRequest = Client.List(true);
|
||||
SetStatus("Refreshing installed package diagnostics.", MessageType.Info);
|
||||
}
|
||||
|
||||
private void SetStatus(string message, MessageType messageType)
|
||||
{
|
||||
_status = message;
|
||||
_statusType = messageType;
|
||||
Repaint();
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
Reference in New Issue
Block a user