Ejemplo n.º 1
0
        public static NuGetPackageManager GetNuGetPackageManager()
        {
            if (s_NuGetPackageManager != null)
            {
                return(s_NuGetPackageManager);
            }

            var workingDirectory = OpenModInstallerPlugin.Instance.OpenModManager.WorkingDirectory;
            var packagesPath     = Path.Combine(workingDirectory, "packages");

            if (!Directory.Exists(packagesPath))
            {
                Directory.CreateDirectory(packagesPath);
            }

            Environment.SetEnvironmentVariable("NUGET_COMMON_APPLICATION_DATA", packagesPath);

            s_NuGetPackageManager = new NuGetPackageManager(packagesPath)
            {
                Logger = new NuGetConsoleLogger()
            };

            s_NuGetPackageManager.IgnoreDependencies(
                "Microsoft.NETCore.Platforms",
                "Microsoft.Packaging.Tools",
                "NETStandard.Library",

                /*"OpenMod.Unturned.Redist",
                 * "OpenMod.UnityEngine.Redist",*/// todo
                "System.IO.FileSystem.Watcher");

            return(s_NuGetPackageManager);
        }
Ejemplo n.º 2
0
        public async Task <object> BootstrapAsync(
            string openModFolder,
            string[] commandLineArgs,
            IEnumerable <string> packageIds,
            IEnumerable <string> ignoredDependencies,
            bool allowPrereleaseVersions = false,
            ILogger logger = null)
        {
            var shouldAutoUpdate = false;

            if (!commandLineArgs.Any(arg => arg.Equals("-NoOpenModAutoUpdate", StringComparison.InvariantCultureIgnoreCase)))
            {
                if (!bool.TryParse(Environment.GetEnvironmentVariable("OpenMod_EnableAutoUpdate"), out shouldAutoUpdate))
                {
                    shouldAutoUpdate = true;
                }
            }


            logger ??= new NuGetConsoleLogger();
            openModFolder = Path.GetFullPath(openModFolder);

            var packagesDirectory = Path.Combine(openModFolder, "packages");

            if (!Directory.Exists(packagesDirectory))
            {
                Directory.CreateDirectory(packagesDirectory);
            }

            Environment.SetEnvironmentVariable("NUGET_COMMON_APPLICATION_DATA", Path.GetFullPath(packagesDirectory));

            var nugetPackageManager = new NuGetPackageManager(packagesDirectory)
            {
                Logger = logger
            };

            // these dependencies do not exist on NuGet and create warnings
            // they are not required
            nugetPackageManager.IgnoreDependencies(
                "Microsoft.NETCore.Platforms",
                "Microsoft.Packaging.Tools",
                "NETStandard.Library",
                "System.IO.FileSystem.Watcher");

            nugetPackageManager.IgnoreDependencies(ignoredDependencies.ToArray());

            var hostAssemblies = new List <Assembly>();

            foreach (var packageId in packageIds)
            {
                var packageIdentity = await nugetPackageManager.GetLatestPackageIdentityAsync(packageId);

                var shouldInstallOrUpdate = packageIdentity == null;

                IPackageSearchMetadata openModPackage = null;
                if (packageIdentity == null || shouldAutoUpdate)
                {
                    openModPackage = await nugetPackageManager.QueryPackageExactAsync(packageId, null, allowPrereleaseVersions);
                }

                if (packageIdentity != null && shouldAutoUpdate)
                {
                    var availableVersions = await openModPackage.GetVersionsAsync();

                    shouldInstallOrUpdate = availableVersions.Any(d => d.Version > packageIdentity.Version);
                }

                if (shouldInstallOrUpdate)
                {
                    logger.LogInformation($"Downloading {openModPackage.Identity.Id} v{openModPackage.Identity.Version} via NuGet");
                    var installResult = await nugetPackageManager.InstallAsync(openModPackage.Identity, allowPrereleaseVersions);

                    if (installResult.Code == NuGetInstallCode.Success)
                    {
                        packageIdentity = installResult.Identity;
                        logger.LogInformation($"Finished downloading \"{packageId}\".");
                    }
                    else
                    {
                        logger.LogError($"Downloading has failed for {openModPackage.Identity.Id} v{openModPackage.Identity.Version.OriginalVersion}: {installResult.Code}");
                        if (packageIdentity == null)
                        {
                            return(null);
                        }
                    }
                }

                logger.LogInformation($"Loading {packageIdentity.Id} v{packageIdentity.Version}");
                var packageAssemblies = await LoadPackageAsync(nugetPackageManager, packageIdentity);

                hostAssemblies.AddRange(packageAssemblies);
            }

            return(await InitializeRuntimeAsync(nugetPackageManager, hostAssemblies, openModFolder, commandLineArgs));
        }