Пример #1
0
        public static async Task Convert(ResourcePack resourcePack, string version, bool quiet = false)
        {
            if (resourcePack == null)
            {
                throw new ArgumentNullException(nameof(resourcePack));
            }
            if (string.IsNullOrWhiteSpace(version))
            {
                throw new ArgumentNullException(nameof(version));
            }

            var outputFolder = await StorageUtilities.PickFolderDestination();

            if (outputFolder == null)
            {
                // User decided to cancel. Return.
                return;
            }

            Logger.WriteLine($"Converting {resourcePack.Name} ({resourcePack.Version}) to Minecraft version {version}.");

            var temporaryOutputFolder = await ApplicationData.Current.LocalFolder.CreateFolderAsync($"{resourcePack.Name}_converted_temp", CreationCollisionOption.ReplaceExisting);

            var resourceFileUrl = await StorageSource.GetFileUrlAsync($"{RESOURCE_PACKS_FOLDER_NAME}/{resourcePack.FileName}");

            if (resourceFileUrl == null)
            {
                throw new InvalidOperationException("Could not retrieve resource pack file url.");
            }

            var relations = await ResourceRelationService.GetOrCreateAsync();

            if (relations?.Count == 0)
            {
                throw new InvalidOperationException("Retrieved relation set is empty.");
            }

            using (var webClient = new WebClient())
                using (var stream = new MemoryStream(webClient.DownloadData(resourceFileUrl)))
                    using (var archive = new ZipArchive(stream, ZipArchiveMode.Read))
                    {
                        var index = 0;
                        foreach (var entry in archive.Entries.ToList())
                        {
                            var entryPath = entry.FullName.Replace("/", "\\");
                            foreach (var relation in relations)
                            {
                                // Does it contain both versions?
                                if (!(relation.ContainsKey(resourcePack.Version) && relation.ContainsKey(version)))
                                {
                                    continue;
                                }
                                // Does path match?
                                if (!relation[resourcePack.Version].Equals(entryPath))
                                {
                                    continue;
                                }

                                await ExtractZipEntry(temporaryOutputFolder, entry, relation[version]);

                                break;
                            }

                            if (!quiet)
                            {
                                Logger.WriteLine($"{++index} of {archive.Entries.Count} converted.");
                            }
                        }
                    }

            // Move and rename temporary zip file.
            var temporaryMoveFilePAth = Path.Combine(ApplicationData.Current.LocalFolder.Path, $"{resourcePack.Name}_{version}.zip");

            ZipFile.CreateFromDirectory(temporaryOutputFolder.Path, temporaryMoveFilePAth);
            var temporaryMoveFile = await StorageFile.GetFileFromPathAsync(temporaryMoveFilePAth);

            await temporaryMoveFile.MoveAsync(outputFolder, $"{resourcePack.Name}_{version}.zip", NameCollisionOption.GenerateUniqueName);

            // Delete temporary files
            await temporaryOutputFolder.DeleteAsync();
        }