public async Task <bool> UninstallPackageAsync(string packageName)
        {
            var package = await m_NuGetPackageManager.GetLatestPackageIdentityAsync(packageName);

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

            return(await m_NuGetPackageManager.RemoveAsync(package));
        }
Exemplo n.º 2
0
        public async Task BootstrapAsync(
            string rocketFolder,
            IEnumerable <string> packageIds,
            bool allowPrereleaseVersions = false,
            string repository            = DefaultNugetRepository,
            ILogger logger = null)
        {
            logger = logger ?? new NuGetConsoleLogger();
            logger.LogInformation("Bootstrap has started.");

            rocketFolder = Path.GetFullPath(rocketFolder);

            if (string.IsNullOrEmpty(repository))
            {
                await InitializeRuntimeAsync();

                return;
            }

            var packagesDirectory = Path.Combine(rocketFolder, "Packages");

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

            var nugetInstaller = new NuGetPackageManager(logger, packagesDirectory, new[] { repository });

            foreach (var packageId in packageIds)
            {
                PackageIdentity packageIdentity;
                if (!await nugetInstaller.PackageExistsAsync(packageId))
                {
                    logger.LogInformation("Searching for: " + packageId);
                    var rocketPackage = await nugetInstaller.QueryPackageExactAsync(packageId, null, allowPrereleaseVersions);

                    logger.LogInformation($"Downloading {rocketPackage.Identity.Id} v{rocketPackage.Identity.Version} via NuGet, this might take a while...");
                    var installResult = await nugetInstaller.InstallAsync(rocketPackage.Identity, allowPrereleaseVersions);

                    if (installResult.Code != NuGetInstallCode.Success)
                    {
                        logger.LogInformation($"Downloading has failed for {rocketPackage.Identity.Id}: " + installResult.Code);
                        return;
                    }

                    packageIdentity = installResult.Identity;
                    logger.LogInformation($"Finished downloading \"{packageId}\"");
                }
                else
                {
                    packageIdentity = await nugetInstaller.GetLatestPackageIdentityAsync(packageId);
                }

                logger.LogInformation($"Loading {packageId}.");
                await LoadPackageAsync(nugetInstaller, packageIdentity);
            }

            await InitializeRuntimeAsync();
        }
Exemplo n.º 3
0
        protected virtual async Task <NuGetInstallResult> InstallOrUpdateAsync(string packageId, string version = null, bool isPreRelease = false, bool isUpdate = false)
        {
            PackageIdentity previousVersion = await nugetPackageManager.GetLatestPackageIdentityAsync(packageId);

            if (isUpdate && previousVersion == null)
            {
                return(new NuGetInstallResult(NuGetInstallCode.PackageOrVersionNotFound));
            }

            var package = (await nugetPackageManager.QueryPackageExactAsync(packageId, version, isPreRelease));

            if (package == null)
            {
                return(new NuGetInstallResult(NuGetInstallCode.PackageOrVersionNotFound));
            }

            if (version == null)
            {
                version = package.Identity.Version.OriginalVersion; // set to latest version
            }

            var packageIdentity = new PackageIdentity(package.Identity.Id, new NuGetVersion(version));

            var result = await nugetPackageManager.InstallAsync(packageIdentity, isPreRelease);

            if (result.Code != NuGetInstallCode.Success)
            {
                return(result);
            }

            if (isUpdate)
            {
                await nugetPackageManager.RemoveAsync(previousVersion);
            }
            return(result);
        }
Exemplo n.º 4
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));
        }
        protected override async Task OnLoadAsync()
        {
            var scriptsDir = Path.Combine(m_Runtime.WorkingDirectory, "scripts");
            var nativeDir  = Path.Combine(m_Runtime.WorkingDirectory, "native");

            if (!s_Initialized)
            {
                if (!Directory.Exists(nativeDir))
                {
                    Directory.CreateDirectory(nativeDir);
                }

                var latestPackageId = await m_NuGetPackageManager.GetLatestPackageIdentityAsync("V8.NET");

                var nupkgFile     = m_NuGetPackageManager.GetNugetPackageFile(latestPackageId);
                var packageReader = new PackageArchiveReader(nupkgFile);
                foreach (var file in packageReader.GetFiles("contentFiles"))
                {
                    if (!file.Contains("netstandard2.0"))
                    {
                        continue;
                    }

                    var fileName = Path.GetFileName(file);
                    var entry    = packageReader.GetEntry(file);
                    using var stream = entry.Open();
                    var ms = new FileStream(Path.Combine(nativeDir, fileName), FileMode.Create);
                    await stream.CopyToAsync(ms);

                    ms.Close();
                    stream.Close();
                }

                Loader.AlternateRootSubPath = nativeDir;
                s_Initialized = true;
            }

            if (!Directory.Exists(scriptsDir))
            {
                Directory.CreateDirectory(scriptsDir);
            }
            else
            {
                foreach (var directory in Directory.GetDirectories(scriptsDir))
                {
                    m_Logger.LogInformation($"[loading] Script: {directory}");

                    var filePath = Path.Combine(directory, "startup.js");
                    if (!File.Exists(filePath))
                    {
                        continue;
                    }

                    var scriptId = new DirectoryInfo(directory).Name;

                    m_Engine = new V8Engine(true);
                    var globalContext = m_Engine.GetContext();
                    var v8Context     = m_Engine.CreateContext();

                    var scriptLifeTime = m_LifetimeScope.BeginLifetimeScope(builder =>
                    {
                        builder.Register(ctx => m_Engine)
                        .As <V8Engine>()
                        .SingleInstance()
                        .OwnedByLifetimeScope();

                        builder.Register(ctx => v8Context)
                        .As <Context>()
                        .SingleInstance()
                        .OwnedByLifetimeScope();
                    });

                    try
                    {
                        var serviceProvider = scriptLifeTime.Resolve <IServiceProvider>();
                        m_Engine.SetContext(v8Context);
                        m_Engine.GlobalObject.SetProperty("logger", ActivatorUtilities.CreateInstance <ScriptLogger>(serviceProvider, scriptId), memberSecurity: ScriptMemberSecurity.Locked);
                        m_Engine.GlobalObject.SetProperty("openmod", ActivatorUtilities.CreateInstance <OpenModFunctions>(serviceProvider), memberSecurity: ScriptMemberSecurity.Locked);
                        var script = m_Engine.LoadScript(filePath, throwExceptionOnError: true);
                        m_Engine.Execute(script, throwExceptionOnError: true, trackReturn: false);
                        m_Engine.SetContext(globalContext);
                        GC.KeepAlive(script);
                    }
                    catch (Exception ex)
                    {
                        m_Logger.LogError(ex.GetBaseException(), $"Script error in script \"{scriptId}\"");
                    }
                }
            }
        }
        public async Task BootstrapAsync(
            string openModFolder,
            string[] commandLineArgs,
            IEnumerable <string> packageIds,
            bool allowPrereleaseVersions = false,
            ILogger logger = null)
        {
            var shouldAutoUpdate = false;

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


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

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

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

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

            var hostAssemblies = new List <Assembly>();

            foreach (var packageId in packageIds)
            {
                PackageIdentity packageIdentity;
                var             installedPackage = await nugetInstaller.GetLatestPackageIdentityAsync(packageId);

                var shouldInstallOrUpdate = installedPackage == null;

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

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

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

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

                    if (installResult.Code != NuGetInstallCode.Success)
                    {
                        logger.LogError($"Downloading has failed for {openModPackage.Identity.Id} v{openModPackage.Identity.Version.OriginalVersion}: " + installResult.Code);
                        return;
                    }

                    packageIdentity = installResult.Identity;
                    logger.LogInformation($"Finished downloading \"{packageId}\".");
                }
                else
                {
                    packageIdentity = await nugetInstaller.GetLatestPackageIdentityAsync(packageId);
                }

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

                hostAssemblies.AddRange(packageAssemblies);
            }

            await InitializeRuntimeAsync(hostAssemblies, openModFolder, commandLineArgs);
        }