/// <summary>
        /// Downloads a specific file.
        /// </summary>
        public async Task DownloadAndExtract(DownloadProgressChangedEventHandler progressChanged)
        {
            // Start the modification download.
            byte[] data;
            using (WebClient client = new WebClient())
            {
                client.DownloadProgressChanged += progressChanged;
                data = await client.DownloadDataTaskAsync(Uri);
            }

            /* Extract to Temp Directory */
            string temporaryDirectory = GetTemporaryDirectory();
            var    archiveExtractor   = new ArchiveExtractor();
            await archiveExtractor.ExtractPackageAsync(data, temporaryDirectory);

            /* Get name of package. */
            var configReader = new ConfigReader <ModConfig>();
            var configs      = configReader.ReadConfigurations(temporaryDirectory, ModConfig.ConfigFileName);
            var loaderConfig = LoaderConfigReader.ReadConfiguration();

            foreach (var config in configs)
            {
                string configId        = config.Object.ModId;
                string configDirectory = Path.GetDirectoryName(config.Path);
                string targetDirectory = Path.Combine(loaderConfig.ModConfigDirectory, configId);
                IOEx.MoveDirectory(configDirectory, targetDirectory);
            }

            Directory.Delete(temporaryDirectory, true);
        }
Esempio n. 2
0
        /// <summary>
        /// Extracts the content files of the NuGet package to a specified directory.
        /// </summary>
        /// <param name="stream">Stream containing the NuGet package.</param>
        /// <param name="targetDirectory">The directory to extract the package content to.</param>
        /// <param name="token">A cancellation token to allow cancellation of the task.</param>
        public static void ExtractPackage(Stream stream, string targetDirectory, CancellationToken token = default)
        {
            PackageReaderBase packageReader = new PackageArchiveReader(stream);
            var items         = packageReader.GetFiles();
            var tempDirectory = $"{Path.GetTempPath()}\\{packageReader.NuspecReader.GetId()}";

            // Remove all items ending with a front or backslash (directories)
            items = items.Where(x => !(x.EndsWith("\\") || x.EndsWith("/")));

            if (Directory.Exists(tempDirectory))
            {
                Directory.Delete(tempDirectory, true);
            }

            packageReader.CopyFiles(tempDirectory, items, ExtractFile, new NullLogger(), token);

            var fullTargetDirectory = Path.GetFullPath(targetDirectory);

            IOEx.MoveDirectory(tempDirectory, fullTargetDirectory);
        }