From ef6cfa785f7c47d358ffb0a0dc535019a2da7354 Mon Sep 17 00:00:00 2001 From: huanghongxun Date: Mon, 26 Nov 2018 00:39:52 +0800 Subject: [PATCH] Query cursemeta for removed mods --- .../hmcl/mod/CurseCompletionTask.java | 14 ++++- .../jackhuang/hmcl/mod/CurseManifestFile.java | 19 ++++-- .../org/jackhuang/hmcl/mod/CurseMetaMod.java | 63 +++++++++++++++++++ 3 files changed, 88 insertions(+), 8 deletions(-) create mode 100644 HMCLCore/src/main/java/org/jackhuang/hmcl/mod/CurseMetaMod.java diff --git a/HMCLCore/src/main/java/org/jackhuang/hmcl/mod/CurseCompletionTask.java b/HMCLCore/src/main/java/org/jackhuang/hmcl/mod/CurseCompletionTask.java index c04415c46..458a21c39 100644 --- a/HMCLCore/src/main/java/org/jackhuang/hmcl/mod/CurseCompletionTask.java +++ b/HMCLCore/src/main/java/org/jackhuang/hmcl/mod/CurseCompletionTask.java @@ -17,6 +17,7 @@ */ package org.jackhuang.hmcl.mod; +import com.google.gson.JsonParseException; import org.jackhuang.hmcl.download.DefaultDependencyManager; import org.jackhuang.hmcl.game.GameRepository; import org.jackhuang.hmcl.task.FileDownloadTask; @@ -32,7 +33,6 @@ import java.io.IOException; import java.util.Collection; import java.util.LinkedList; import java.util.List; -import java.util.concurrent.ConcurrentSkipListSet; import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicInteger; import java.util.logging.Level; @@ -117,8 +117,16 @@ public final class CurseCompletionTask extends Task { try { return file.withFileName(NetworkUtils.detectFileName(file.getUrl())); } catch (FileNotFoundException e) { - notFound.set(true); - return file; + try { + String result = NetworkUtils.doGet(NetworkUtils.toURL(String.format("https://cursemeta.dries007.net/%d/%d.json", file.getProjectID(), file.getFileID()))); + CurseMetaMod mod = JsonUtils.fromNonNullJson(result, CurseMetaMod.class); + return file.withFileName(mod.getFileNameOnDisk()).withURL(mod.getDownloadURL()); + } catch (IOException | JsonParseException e2) { + Logging.LOG.log(Level.WARNING, "Could not query cursemeta for deleted mods: " + file.getUrl(), e2); + notFound.set(true); + return file; + } + } catch (IOException ioe) { Logging.LOG.log(Level.WARNING, "Unable to fetch the file name of URL: " + file.getUrl(), ioe); flag.set(false); diff --git a/HMCLCore/src/main/java/org/jackhuang/hmcl/mod/CurseManifestFile.java b/HMCLCore/src/main/java/org/jackhuang/hmcl/mod/CurseManifestFile.java index c9a3962bf..01292ca52 100644 --- a/HMCLCore/src/main/java/org/jackhuang/hmcl/mod/CurseManifestFile.java +++ b/HMCLCore/src/main/java/org/jackhuang/hmcl/mod/CurseManifestFile.java @@ -41,18 +41,22 @@ public final class CurseManifestFile implements Validation { @SerializedName("fileName") private final String fileName; + + @SerializedName("url") + private final String url; @SerializedName("required") private final boolean required; public CurseManifestFile() { - this(0, 0, "", true); + this(0, 0, null, null, true); } - public CurseManifestFile(int projectID, int fileID, String fileName, boolean required) { + public CurseManifestFile(int projectID, int fileID, String fileName, String url, boolean required) { this.projectID = projectID; this.fileID = fileID; this.fileName = fileName; + this.url = url; this.required = required; } @@ -79,11 +83,16 @@ public final class CurseManifestFile implements Validation { } public URL getUrl() { - return NetworkUtils.toURL("https://minecraft.curseforge.com/projects/" + projectID + "/files/" + fileID + "/download"); + return url == null ? NetworkUtils.toURL("https://minecraft.curseforge.com/projects/" + projectID + "/files/" + fileID + "/download") + : NetworkUtils.toURL(url); } - + public CurseManifestFile withFileName(String fileName) { - return new CurseManifestFile(projectID, fileID, fileName, required); + return new CurseManifestFile(projectID, fileID, fileName, url, required); + } + + public CurseManifestFile withURL(String url) { + return new CurseManifestFile(projectID, fileID, fileName, url, required); } @Override diff --git a/HMCLCore/src/main/java/org/jackhuang/hmcl/mod/CurseMetaMod.java b/HMCLCore/src/main/java/org/jackhuang/hmcl/mod/CurseMetaMod.java new file mode 100644 index 000000000..5fc9b91ed --- /dev/null +++ b/HMCLCore/src/main/java/org/jackhuang/hmcl/mod/CurseMetaMod.java @@ -0,0 +1,63 @@ +/* + * Hello Minecraft! Launcher. + * Copyright (C) 2018 huangyuhui + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see {http://www.gnu.org/licenses/}. + */ +package org.jackhuang.hmcl.mod; + +import com.google.gson.annotations.SerializedName; +import org.jackhuang.hmcl.util.Immutable; + +@Immutable +public final class CurseMetaMod { + @SerializedName("Id") + private final int id; + + @SerializedName("FileName") + private final String fileName; + + @SerializedName("FileNameOnDisk") + private final String fileNameOnDisk; + + @SerializedName("DownloadURL") + private final String downloadURL; + + public CurseMetaMod() { + this(0, "", "", ""); + } + + public CurseMetaMod(int id, String fileName, String fileNameOnDisk, String downloadURL) { + this.id = id; + this.fileName = fileName; + this.fileNameOnDisk = fileNameOnDisk; + this.downloadURL = downloadURL; + } + + public int getId() { + return id; + } + + public String getFileName() { + return fileName; + } + + public String getFileNameOnDisk() { + return fileNameOnDisk; + } + + public String getDownloadURL() { + return downloadURL; + } +}