示例#1
0
        public static async Task <string> DetectVersion(StorageFile resourcePackFile, bool onlyReleases = true)
        {
            if (resourcePackFile == null)
            {
                throw new ArgumentNullException(nameof(resourcePackFile));
            }

            Logger.WriteLine($"Detecting resource pack version of file {resourcePackFile.Name}.");

            var relations = await ResourceRelationService.GetOrCreateAsync();

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

            if (!MinecraftService.IsInitialized)
            {
                await MinecraftService.InitializeAsync();
            }
            var versionCount = onlyReleases ? MinecraftAPI.Releases.ToDictionary(x => x.Id, x => 0) : MinecraftAPI.Versions.ToDictionary(x => x.Id, x => 0);

            using (var stream = await resourcePackFile.OpenStreamForReadAsync())
                using (var archive = new ZipArchive(stream, ZipArchiveMode.Read))
                {
                    foreach (var entry in archive.Entries.ToList())
                    {
                        var entryPath = entry.FullName.Replace("/", "\\");
                        foreach (var relation in relations)
                        {
                            foreach (var versionPathPair in relation)
                            {
                                if (versionPathPair.Value.Equals(entryPath) && versionCount.ContainsKey(versionPathPair.Key))
                                {
                                    versionCount[versionPathPair.Key]++;
                                }
                            }
                        }
                    }
                }

            var    maxCount = versionCount.Max(vp => vp.Value);
            var    mostNominatedVersions = versionCount.Where(r => r.Value == maxCount).Select(vp => vp.Key);
            string maxVersion            = null;

            foreach (var nominatedVersion in mostNominatedVersions)
            {
                if (maxVersion == null)
                {
                    maxVersion = nominatedVersion;
                }
                else
                {
                    maxVersion = MinecraftAPI.MaxVersion(maxVersion, nominatedVersion);
                }
            }
            return(maxVersion);
        }
示例#2
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();
        }