| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495 |
- using System;
- using System.Linq;
- using UnityEditor;
- using UnityEditor.PackageManager;
- using UnityEditor.PackageManager.Requests;
- using UnityEngine;
- [InitializeOnLoad]
- public static class HybridCLRPackageSetup
- {
- public const string PackageName = "com.code-philosophy.hybridclr";
- public const string PackageUrl = "https://gitee.com/focus-creative-games/hybridclr_unity.git#v9.0.3";
- const string AttemptedKey = "AppRuntimes.HybridCLR.PackageAttempted";
- const string ErrorKey = "AppRuntimes.HybridCLR.PackageError";
- static AddRequest request;
- static bool scheduled;
- static double nextCheck;
- public static bool IsInstalled => UnityEditor.PackageManager.PackageInfo.GetAllRegisteredPackages().Any(package => package.name == PackageName);
- public static bool IsInstalling => request != null || scheduled && !IsInstalled;
- public static string LastError => SessionState.GetString(ErrorKey, "");
- static HybridCLRPackageSetup() => Schedule();
- public static void RetryInstallation()
- {
- if (request != null || IsInstalled) return;
- SessionState.EraseBool(AttemptedKey);
- SessionState.EraseString(ErrorKey);
- Schedule();
- }
- static void Schedule()
- {
- scheduled = true;
- nextCheck = EditorApplication.timeSinceStartup + 1;
- EditorApplication.update -= Update;
- EditorApplication.update += Update;
- }
- static void StopChecking()
- {
- scheduled = false;
- EditorApplication.update -= Update;
- }
- static void Update()
- {
- if (request != null)
- {
- if (!request.IsCompleted) return;
- var completed = request;
- request = null;
- StopChecking();
- if (completed.Status == StatusCode.Success)
- {
- SessionState.EraseString(ErrorKey);
- Debug.Log("[AppRuntimes] HybridCLR package installed. Wait for compilation, then configure AppSettings and build resources.");
- }
- else ReportFailure(completed.Error?.message ?? "Package Manager did not return an error message.");
- return;
- }
- if (EditorApplication.timeSinceStartup < nextCheck || EditorApplication.isCompiling || EditorApplication.isUpdating ||
- EditorApplication.isPlayingOrWillChangePlaymode || BuildPipeline.isBuildingPlayer) return;
- if (IsInstalled)
- {
- SessionState.EraseString(ErrorKey);
- StopChecking();
- return;
- }
- if (SessionState.GetBool(AttemptedKey, false))
- {
- StopChecking();
- return;
- }
- SessionState.SetBool(AttemptedKey, true);
- try
- {
- Debug.Log("[AppRuntimes] Installing the missing HybridCLR package: " + PackageUrl);
- request = Client.Add(PackageUrl);
- }
- catch (Exception exception)
- {
- StopChecking();
- ReportFailure(exception.Message);
- }
- }
- static void ReportFailure(string message)
- {
- SessionState.SetString(ErrorKey, message);
- Debug.LogWarning("[AppRuntimes] HybridCLR package installation failed: " + message +
- "\nOpen AppSettings and retry, or add this Git URL in Package Manager: " + PackageUrl);
- }
- }
|