/// <summary>
        /// Attempts to install the sample associated with the currently active Content Pack.
        /// </summary>
        static void ProcessSampleEvent()
        {
            var targetPack = s_CurrentEvent.targetPack;
            var samples    = Sample.FindByPackage(targetPack.PackageName, targetPack.Version);

            // We manually set the event null this time because we have no asynchronous request to process
            s_CurrentEvent = null;

            if (!samples.Any())
            {
                targetPack.InstallStatus &= ~InstallationStatus.Installing;
                OnInstallContent?.Invoke(false, $"Can't install content - no sample available.");
                OnContentChanged?.Invoke();
                return;
            }

            var targetSample = samples.First();

            if (!targetSample.Import())
            {
                targetPack.InstallStatus &= ~InstallationStatus.Installing;
                OnInstallContent?.Invoke(false, $"Failed to import associated content sample.");
                OnContentChanged?.Invoke();
                return;
            }

            FinishInstallation(targetPack);
        }
        /// <summary>
        /// Attempts to remove the package that the currently active event contains.
        /// </summary>
        static void StartUninstallEvent()
        {
            // If the package is not installed, do nothing.
            if (!s_CurrentEvent.targetPack.InstallStatus.HasFlag(InstallationStatus.Installed))
            {
                OnUninstallContent?.Invoke(true, $"{s_CurrentEvent.targetPack.PackageName} not installed");
                s_CurrentEvent = null;
                return;
            }

            s_CurrentEvent.targetPack.InstallStatus |= InstallationStatus.Locked;
            s_CurrentEvent.request = Client.Remove(s_CurrentEvent.targetPack.PackageName);
            OnContentChanged?.Invoke();
        }
        static void Initialize()
        {
            var result = "Initialize: ";

            // Cache the path to the library
            s_CachedPackagePath = $"{Path.GetDirectoryName(Application.dataPath)?.ToLower()}{Path.DirectorySeparatorChar}library";

            if (s_Instance != null)
            {
                result += "Duplicate";
                ConditionalLog(result);
                return;
            }

            if (s_Instance == null)
            {
                result    += " Instance not set, searching.";
                s_Instance = Resources.FindObjectsOfTypeAll <ContentManagerDriver>().FirstOrDefault();
            }

            if (s_Instance == null)
            {
                result    += " Instance not found, creating.";
                s_Instance = CreateInstance <ContentManagerDriver>();
            }

            s_CurrentEvent      = null;
            s_StatusRequested   = false;
            s_StatusInitialized = false;

            SubscribeStatusCallback(LogStatusResult);
            SubscribeInstallCallback(LogInstallResult);
            SubscribeUninstallCallback(LogUninstallResult);

            // Is there any queued events?  Start a status update which will kick off events again
            if (s_Instance.m_QueuedEvents.Count > 0)
            {
                result += " Events already enqueue - updated status.";
                UpdateContentStatus();
            }

            ConditionalLog(result);
        }
        /// <summary>
        /// Checks the status of currently running Content Manager actions and queues up new ones if current actions have finished.
        /// Runs every frame while a Content Manager action is available or running.
        /// </summary>
        static void ContentRequestUpdate()
        {
            ConditionalLog("Request update loop");
            var eventQueue = QueuedEvents;

            // Queue up an event if we're open for one
            // If an event is running, see if it has finished
            if (s_CurrentEvent == null)
            {
                if (eventQueue.Count > 0 || s_StatusRequested)
                {
                    if (s_StatusRequested || !s_StatusInitialized)
                    {
                        s_CurrentEvent = new ContentEvent {
                            action = PackageAction.Status
                        };
                    }
                    else
                    {
                        s_CurrentEvent = eventQueue[0];
                        eventQueue.RemoveAt(0);
                    }

                    // Start the next action
                    switch (s_CurrentEvent.action)
                    {
                    case PackageAction.Status:
                        StartStatusEvent();
                        break;

                    case PackageAction.Add:
                        StartAddEvent();
                        break;

                    case PackageAction.Remove:
                        StartUninstallEvent();
                        break;

                    case PackageAction.Embed:
                        StartEmbedEvent();
                        break;

                    case PackageAction.SampleInstall:
                        ProcessSampleEvent();
                        break;
                    }
                }
                else
                {
                    StopEditorUpdates();
                }
            }
            else
            {
                // Check the request status
                if (s_CurrentEvent.request != null && s_CurrentEvent.request.IsCompleted)
                {
                    // Complete the action
                    switch (s_CurrentEvent.action)
                    {
                    case PackageAction.Status:
                        CompleteStatusEvent();
                        break;

                    case PackageAction.Add:
                        CompleteAddEvent();
                        break;

                    case PackageAction.Remove:
                        CompleteUninstallEvent();
                        break;

                    case PackageAction.Embed:
                        CompleteEmbedEvent();
                        break;
                    }

                    s_CurrentEvent = null;
                }
            }
        }
        /// <summary>
        /// Attempts to install a package specified within the currently active Content Pack.
        /// </summary>
        static void StartAddEvent()
        {
            // Is the package already installed? Do nothing
            // Otherwise, start an add action
            if (s_CurrentEvent.targetPack.InstallStatus.HasFlag(InstallationStatus.Installed) && !s_CurrentEvent.targetPack.InstallStatus.HasFlag(InstallationStatus.DifferentVersion))
            {
                OnInstallContent?.Invoke(true, $"{s_CurrentEvent.targetPack.PackageName} already installed");
                s_CurrentEvent = null;
                return;
            }

            s_CurrentEvent.targetPack.InstallStatus |= InstallationStatus.Locked;
            var currentUrl = s_CurrentEvent.targetPack.Url.ToLower();

            if (currentUrl.StartsWith(k_PackageReferenceKey))
            {
                // Local content packs can be in embedded packages, which is fine
                // Or they can be in the library folder, in which case we need to copy them to a temporary folder for installation
                currentUrl = Path.GetFullPath(currentUrl).ToLower();

                if (currentUrl.StartsWith(s_CachedPackagePath))
                {
                    try
                    {
                        // The content pack is located within the library folder - we must copy it to a temporary folder
                        var localPackageInfo = new DirectoryInfo(currentUrl);
                        if (localPackageInfo.Exists)
                        {
                            var localFolder = localPackageInfo.Name;
                            var targetPath  = $"{Path.GetDirectoryName(Application.dataPath)}{Path.DirectorySeparatorChar}{k_LocalPackagePath}{Path.DirectorySeparatorChar}{localFolder}";

                            Directory.CreateDirectory(targetPath);
                            FileUtil.ReplaceDirectory(currentUrl.Replace(Path.DirectorySeparatorChar, '/'), targetPath.Replace(Path.DirectorySeparatorChar, '/'));
                            currentUrl = $"file:{ToRelativeUnityPath(targetPath)}";
                        }
                        else
                        {
                            OnInstallContent?.Invoke(false, $"{s_CurrentEvent.targetPack.PackageName} cannot be found");
                            s_CurrentEvent = null;
                            return;
                        }
                    }
                    catch (Exception ex)
                    {
                        OnInstallContent?.Invoke(false, $"Exception installing {s_CurrentEvent?.targetPack.PackageName}:{ex.Message}");
                        s_CurrentEvent = null;
                        return;
                    }
                }
                else
                {
                    // Convert the packages path to a relative path for version control niceness
                    currentUrl = $"file:{ToRelativeUnityPath(Path.GetFullPath(currentUrl))}";
                }
            }

            if (!string.IsNullOrEmpty(s_CurrentEvent.targetPack.AutoVersion))
            {
                currentUrl += $"@{s_CurrentEvent.targetPack.AutoVersion}";
            }

            s_CurrentEvent.request = Client.Add(currentUrl);
            OnContentChanged?.Invoke();
        }