This commit is contained in:
huangyuhui
2018-07-01 13:34:52 +08:00
parent c887d7e649
commit 1f30c299a5
7 changed files with 79 additions and 15 deletions

View File

@@ -0,0 +1,35 @@
/*
* Hello Minecraft! Launcher.
* Copyright (C) 2017 huangyuhui <huanghongxun2008@126.com>
*
* 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;
public class CurseCompletionException extends Exception {
public CurseCompletionException() {
}
public CurseCompletionException(String message) {
super(message);
}
public CurseCompletionException(String message, Throwable cause) {
super(message, cause);
}
public CurseCompletionException(Throwable cause) {
super(cause);
}
}

View File

@@ -24,9 +24,11 @@ import org.jackhuang.hmcl.task.Task;
import org.jackhuang.hmcl.util.*;
import java.io.File;
import java.io.IOException;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.logging.Level;
import java.util.stream.Collectors;
@@ -49,7 +51,7 @@ public final class CurseCompletionTask extends Task {
* Constructor.
*
* @param dependencyManager the dependency manager.
* @param version the existent and physical version.
* @param version the existent and physical version.
*/
public CurseCompletionTask(DefaultDependencyManager dependencyManager, String version) {
this(dependencyManager, version, null);
@@ -59,8 +61,8 @@ public final class CurseCompletionTask extends Task {
* Constructor.
*
* @param dependencyManager the dependency manager.
* @param version the existent and physical version.
* @param manifest the CurseForgeModpack manifest.
* @param version the existent and physical version.
* @param manifest the CurseForgeModpack manifest.
*/
public CurseCompletionTask(DefaultDependencyManager dependencyManager, String version, CurseManifest manifest) {
this.dependencyManager = dependencyManager;
@@ -96,6 +98,7 @@ public final class CurseCompletionTask extends Task {
File root = repository.getVersionRoot(version);
File run = repository.getRunDirectory(version);
AtomicBoolean flag = new AtomicBoolean(true);
AtomicInteger finished = new AtomicInteger(0);
// Because in China, Curse is too difficult to visit,
@@ -104,14 +107,32 @@ public final class CurseCompletionTask extends Task {
manifest.getFiles().parallelStream()
.map(file -> {
updateProgress(finished.incrementAndGet(), manifest.getFiles().size());
return Lang.ignoringException(() -> file.setFileName(NetworkUtils.detectFileName(file.getUrl(), dependencyManager.getProxy())), file);
if (StringUtils.isBlank(file.getFileName())) {
try {
return file.withFileName(NetworkUtils.detectFileName(file.getUrl(), dependencyManager.getProxy()));
} catch (IOException ioe) {
Logging.LOG.log(Level.WARNING, "Unable to fetch the file name of URL: " + file.getUrl(), ioe);
flag.set(false);
return file;
}
} else
return file;
})
.collect(Collectors.toList()));
FileUtils.writeText(new File(root, "manifest.json"), Constants.GSON.toJson(newManifest));
for (CurseManifestFile file : newManifest.getFiles())
if (StringUtils.isNotBlank(file.getFileName()))
dependencies.add(new FileDownloadTask(file.getUrl(), new File(run, "mods/" + file.getFileName()), dependencyManager.getProxy()));
if (StringUtils.isNotBlank(file.getFileName())) {
File dest = new File(run, "mods/" + file.getFileName());
if (!dest.exists())
dependencies.add(new FileDownloadTask(file.getUrl(), dest, dependencyManager.getProxy()));
}
// Let this task fail if the curse manifest has not been completed.
if (!flag.get())
dependencies.add(Task.of(() -> {
throw new CurseCompletionException();
}));
}
}

View File

@@ -82,7 +82,7 @@ public final class CurseManifestFile implements Validation {
return NetworkUtils.toURL("https://minecraft.curseforge.com/projects/" + projectID + "/files/" + fileID + "/download");
}
public CurseManifestFile setFileName(String fileName) {
public CurseManifestFile withFileName(String fileName) {
return new CurseManifestFile(projectID, fileID, fileName, required);
}

View File

@@ -20,10 +20,8 @@ package org.jackhuang.hmcl.util;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.Proxy;
import java.net.URL;
import java.net.*;
import java.nio.charset.StandardCharsets;
import java.util.Map;
import java.util.Objects;
import java.util.function.Supplier;
@@ -138,9 +136,9 @@ public final class NetworkUtils {
String disposition = conn.getHeaderField("Content-Disposition");
if (disposition == null || !disposition.contains("filename=")) {
String u = conn.getURL().toString();
return substringAfterLast(u, '/');
return Lang.invoke(() -> URLDecoder.decode(substringAfterLast(u, '/'), StandardCharsets.UTF_8.name()));
} else
return removeSurrounding(substringAfter(disposition, "filename="), "\"");
return Lang.invoke(() -> URLDecoder.decode(removeSurrounding(substringAfter(disposition, "filename="), "\""), StandardCharsets.UTF_8.name()));
}
public static URL toURL(String str) {