|
| 1 | +/* |
| 2 | + * Hello Minecraft! Launcher |
| 3 | + * Copyright (C) 2020 huangyuhui <huanghongxun2008@126.com> and contributors |
| 4 | + * |
| 5 | + * This program is free software: you can redistribute it and/or modify |
| 6 | + * it under the terms of the GNU General Public License as published by |
| 7 | + * the Free Software Foundation, either version 3 of the License, or |
| 8 | + * (at your option) any later version. |
| 9 | + * |
| 10 | + * This program is distributed in the hope that it will be useful, |
| 11 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | + * GNU General Public License for more details. |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU General Public License |
| 16 | + * along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 17 | + */ |
| 18 | +package org.jackhuang.hmcl.download.optifine; |
| 19 | + |
| 20 | +import com.google.gson.annotations.SerializedName; |
| 21 | +import com.google.gson.reflect.TypeToken; |
| 22 | +import org.jackhuang.hmcl.download.VersionList; |
| 23 | +import org.jackhuang.hmcl.util.io.HttpRequest; |
| 24 | +import org.jackhuang.hmcl.util.versioning.VersionNumber; |
| 25 | + |
| 26 | +import java.util.List; |
| 27 | +import java.util.concurrent.CompletableFuture; |
| 28 | +import java.util.stream.Collectors; |
| 29 | + |
| 30 | +/** |
| 31 | + * @author huangyuhui |
| 32 | + */ |
| 33 | +public final class OptiFine302VersionList extends VersionList<OptiFineRemoteVersion> { |
| 34 | + private final String versionListURL; |
| 35 | + |
| 36 | + public OptiFine302VersionList(String versionListURL) { |
| 37 | + this.versionListURL = versionListURL; |
| 38 | + } |
| 39 | + |
| 40 | + @Override |
| 41 | + public boolean hasType() { |
| 42 | + return true; |
| 43 | + } |
| 44 | + |
| 45 | + @Override |
| 46 | + public CompletableFuture<?> refreshAsync() { |
| 47 | + return HttpRequest.GET(versionListURL).getJsonAsync(TypeToken.get(OptiFine302VersionList.VersionList.class)).thenAcceptAsync(root -> { |
| 48 | + lock.writeLock().lock(); |
| 49 | + |
| 50 | + try { |
| 51 | + versions.clear(); |
| 52 | + for (OptiFineVersion element : root.versions) { |
| 53 | + String gameVersion = VersionNumber.normalize(element.gameVersion); |
| 54 | + versions.put(gameVersion, new OptiFineRemoteVersion( |
| 55 | + gameVersion, element.version, |
| 56 | + root.downloadBases.stream().map(u -> u + element.fileName).collect(Collectors.toList()), |
| 57 | + element.fileName.startsWith("pre") |
| 58 | + )); |
| 59 | + } |
| 60 | + } finally { |
| 61 | + lock.writeLock().unlock(); |
| 62 | + } |
| 63 | + }); |
| 64 | + } |
| 65 | + |
| 66 | + private static final class VersionList { |
| 67 | + @SerializedName("file") |
| 68 | + private final List<OptiFineVersion> versions; |
| 69 | + |
| 70 | + @SerializedName("download") |
| 71 | + private final List<String> downloadBases; |
| 72 | + |
| 73 | + public VersionList(List<OptiFineVersion> versions, List<String> downloadBases) { |
| 74 | + this.versions = versions; |
| 75 | + this.downloadBases = downloadBases; |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + private static final class OptiFineVersion { |
| 80 | + @SerializedName("name") |
| 81 | + private final String version; |
| 82 | + @SerializedName("filename") |
| 83 | + private final String fileName; |
| 84 | + @SerializedName("mcversion") |
| 85 | + private final String gameVersion; |
| 86 | + |
| 87 | + public OptiFineVersion(String version, String fileName, String gameVersion) { |
| 88 | + this.version = version; |
| 89 | + this.fileName = fileName; |
| 90 | + this.gameVersion = gameVersion; |
| 91 | + } |
| 92 | + } |
| 93 | +} |
0 commit comments