Exemple #1
0
        /// <summary>
        /// Try to enable the package GUID.
        /// If this GUID is not installed or this method fails it will return false,
        /// otherwise it will return true and enable/install all dependencies.
        /// </summary>
        /// <param name="guid">The guid to try to enable</param>
        /// <param name="tryEnableDependencies">If any dependencies are disabled, should we try to automatically install/enable them?</param>
        /// <returns><see langword="true"/> if successful, otherwise <see langword="false"/></returns>
        public static bool TryEnablePackage(string guid, bool tryEnableDependencies = true)
        {
            if (OutwardHelper.IsOutwardRunning())
            {
                MessageBox.Show("You need to close Outward to do that.");
                return(false);
            }

            var package = TryGetInstalledPackage(guid);

            if (package == null)
            {
                return(false);
            }

            if (!PreEnableConflictCheck(package))
            {
                //Console.WriteLine("PreEnableConflictCheck returned false");
                return(false);
            }

            if (!package.AreAllDependenciesEnabled(out List <string> missing))
            {
                if (!tryEnableDependencies || !PreEnableDependencyCheck(package, missing))
                {
                    return(false);
                }
            }

            if (s_enabledPackages.ContainsKey(guid))
            {
                OnPackageEnabled?.Invoke(package);
                return(true);
            }

            string toDir   = Folders.OUTWARD_PLUGINS + $@"\{package.GUID}";
            string fromDir = Folders.MEFINO_DISABLED_FOLDER + $@"\{package.GUID}";

            if (IOHelper.TryMoveDirectory(fromDir, toDir))
            {
                RefreshInstalledPackages();

                OnPackageEnabled?.Invoke(package);

                return(true);
            }

            return(false);
        }
        /// <summary>
        /// Adds a package to the Package Manager.
        /// </summary>
        /// <param name="package">A string representing the package to be added.</param>
        /// <param name="version">If provided, the package will be loaded with this specific version.</param>
        public static async void LoadPackage(string package, string version = null)
        {
            if (string.IsNullOrEmpty(package) || IsPackageLoaded(package, version) || EditorApplication.isPlayingOrWillChangePlaymode)
            {
                return;
            }

            if (IsPackageLoaded(package) && string.IsNullOrEmpty(version) == false)
            {
                PackageInfo installedPackage = Packages.First(packageInfo => packageInfo.name == package);
                EditorUtility.DisplayDialog($"{installedPackage.displayName} Upgrade", $"{installedPackage.displayName} will be upgraded from v{installedPackage.version} to v{version}.", "Continue");
            }

            if (package.Contains("@") == false && string.IsNullOrEmpty(version) == false)
            {
                package = $"{package}@{version}";
            }

            AddRequest addRequest = Client.Add(package);

            Debug.Log($"Enabling package: {package.Split('@').First()}, Version: {(string.IsNullOrEmpty(version) ? "latest" : version)}.");

            while (addRequest.IsCompleted == false)
            {
                await Task.Delay(100);
            }

            if (addRequest.Status == StatusCode.Failure)
            {
                Debug.LogError($"There was an error trying to enable '{package}' - Error Code: [{addRequest.Error.errorCode}] .\n{addRequest.Error.message}");
            }
            else
            {
                OnPackageEnabled?.Invoke(null, new PackageEnabledEventArgs(addRequest.Result));
                Debug.Log($"The package '{addRequest.Result.displayName}' version '{addRequest.Result.version}' has been automatically added");
            }
        }