2022-07-31 20:41:56 +02:00
|
|
|
/*
|
2023-07-16 12:36:11 +02:00
|
|
|
* Copyright (C) 2022-2023 FalsePattern
|
2022-07-30 15:21:11 +02:00
|
|
|
* All Rights Reserved
|
|
|
|
|
*
|
2023-07-16 12:36:11 +02:00
|
|
|
* The above copyright notice and this permission notice
|
2022-07-30 15:21:11 +02:00
|
|
|
* shall be included in all copies or substantial portions of the Software.
|
|
|
|
|
*
|
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU Lesser 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 Lesser General Public License
|
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
*/
|
2022-06-10 00:09:13 +02:00
|
|
|
package com.falsepattern.lib.internal;
|
|
|
|
|
|
2023-01-14 20:18:48 +01:00
|
|
|
import lombok.Cleanup;
|
2022-06-10 00:09:13 +02:00
|
|
|
import lombok.val;
|
|
|
|
|
import lombok.var;
|
|
|
|
|
|
2023-02-12 09:34:12 +01:00
|
|
|
import net.minecraft.launchwrapper.Launch;
|
|
|
|
|
|
2022-07-31 20:41:56 +02:00
|
|
|
import java.io.ByteArrayOutputStream;
|
2023-01-14 20:18:48 +01:00
|
|
|
import java.io.File;
|
2022-07-31 20:41:56 +02:00
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.io.InputStream;
|
|
|
|
|
import java.io.OutputStream;
|
2022-06-10 00:09:13 +02:00
|
|
|
import java.net.HttpURLConnection;
|
|
|
|
|
import java.net.URL;
|
2023-01-14 20:18:48 +01:00
|
|
|
import java.nio.file.Files;
|
2023-01-17 16:54:04 +01:00
|
|
|
import java.util.Collections;
|
|
|
|
|
import java.util.Map;
|
2022-06-19 22:42:52 +02:00
|
|
|
import java.util.concurrent.CompletableFuture;
|
|
|
|
|
import java.util.concurrent.CompletionException;
|
2023-01-14 20:18:48 +01:00
|
|
|
import java.util.concurrent.atomic.AtomicBoolean;
|
2022-06-19 22:42:52 +02:00
|
|
|
import java.util.concurrent.atomic.AtomicReference;
|
2022-06-10 00:09:13 +02:00
|
|
|
import java.util.function.Consumer;
|
|
|
|
|
|
|
|
|
|
public class Internet {
|
2023-01-17 16:54:04 +01:00
|
|
|
private static final Map<String, String> NO_HEADERS = Collections.emptyMap();
|
2023-07-16 13:09:05 +02:00
|
|
|
|
2022-06-10 00:09:13 +02:00
|
|
|
public static void connect(URL URL, Consumer<Exception> onError, Consumer<InputStream> onSuccess) {
|
2023-01-17 16:54:04 +01:00
|
|
|
connect(URL, NO_HEADERS, onError, onSuccess);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static Map<String, String> constructHeaders(String... entries) {
|
|
|
|
|
if (entries.length % 2 != 0) {
|
|
|
|
|
throw new IllegalArgumentException("Entries must be in pairs");
|
|
|
|
|
}
|
|
|
|
|
val map = new java.util.HashMap<String, String>();
|
|
|
|
|
for (int i = 0; i < entries.length; i += 2) {
|
|
|
|
|
map.put(entries[i], entries[i + 1]);
|
|
|
|
|
}
|
|
|
|
|
return map;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void connect(URL URL, Map<String, String> headers, Consumer<Exception> onError, Consumer<InputStream> onSuccess) {
|
2022-06-10 00:09:13 +02:00
|
|
|
try {
|
|
|
|
|
val connection = (HttpURLConnection) URL.openConnection();
|
|
|
|
|
connection.setConnectTimeout(3500);
|
|
|
|
|
connection.setReadTimeout(5000);
|
2023-07-16 13:09:05 +02:00
|
|
|
connection.setRequestProperty("User-Agent",
|
|
|
|
|
Tags.MODNAME
|
|
|
|
|
+ " "
|
|
|
|
|
+ Tags.VERSION
|
|
|
|
|
+ " Internet Connector"
|
|
|
|
|
+ " (https://github.com/FalsePattern/FalsePatternLib)");
|
|
|
|
|
for (val header : headers.entrySet()) {
|
2023-01-17 16:54:04 +01:00
|
|
|
val key = header.getKey();
|
|
|
|
|
val value = header.getValue();
|
|
|
|
|
if (key == null || value == null) {
|
|
|
|
|
throw new IllegalArgumentException("Null key or value");
|
|
|
|
|
}
|
|
|
|
|
if (key.isEmpty()) {
|
|
|
|
|
throw new IllegalArgumentException("Empty key");
|
|
|
|
|
}
|
|
|
|
|
connection.setRequestProperty(key, value);
|
|
|
|
|
}
|
2022-06-10 00:09:13 +02:00
|
|
|
if (connection.getResponseCode() != 200) {
|
|
|
|
|
onError.accept(new Exception("HTTP response code " + connection.getResponseCode()));
|
|
|
|
|
} else {
|
|
|
|
|
onSuccess.accept(connection.getInputStream());
|
|
|
|
|
}
|
|
|
|
|
connection.disconnect();
|
|
|
|
|
} catch (Exception e) {
|
2023-02-12 09:34:12 +01:00
|
|
|
//Check if NonUpdate is present
|
|
|
|
|
try {
|
|
|
|
|
if (Launch.classLoader.getClassBytes("moe.mickey.forge.nonupdate.NonUpdate") != null) {
|
2023-07-16 13:09:05 +02:00
|
|
|
e.addSuppressed(new Exception(
|
|
|
|
|
"NonUpdate is present, it's possible that it's blocking a library download."
|
|
|
|
|
+ " Please disable it for a single run to allow mod dependencies to download."));
|
2023-02-12 09:34:12 +01:00
|
|
|
}
|
|
|
|
|
} catch (IOException ex) {
|
|
|
|
|
e.addSuppressed(ex);
|
|
|
|
|
}
|
2022-06-10 00:09:13 +02:00
|
|
|
onError.accept(e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-14 20:18:48 +01:00
|
|
|
public static CompletableFuture<byte[]> download(URL... urls) {
|
2022-06-19 22:42:52 +02:00
|
|
|
return CompletableFuture.supplyAsync(() -> {
|
|
|
|
|
val result = new ByteArrayOutputStream();
|
2023-01-14 20:18:48 +01:00
|
|
|
val caught = new AtomicReference<Exception>();
|
|
|
|
|
val success = new AtomicBoolean(false);
|
|
|
|
|
for (val url : urls) {
|
|
|
|
|
connect(url, caught::set, (input) -> {
|
|
|
|
|
try {
|
|
|
|
|
transferAndClose(input, result);
|
|
|
|
|
success.set(true);
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
throw new CompletionException(e);
|
|
|
|
|
}
|
|
|
|
|
});
|
2023-07-16 13:09:05 +02:00
|
|
|
if (success.get()) {
|
|
|
|
|
return result.toByteArray();
|
|
|
|
|
}
|
2023-01-14 20:18:48 +01:00
|
|
|
}
|
|
|
|
|
throw new CompletionException(caught.get());
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static CompletableFuture<Void> downloadFile(File file, URL... urls) {
|
|
|
|
|
return CompletableFuture.runAsync(() -> {
|
|
|
|
|
val caught = new AtomicReference<Exception>();
|
|
|
|
|
val success = new AtomicBoolean(false);
|
|
|
|
|
for (val url : urls) {
|
|
|
|
|
connect(url, caught::set, (input) -> {
|
|
|
|
|
try {
|
|
|
|
|
@Cleanup val outputStream = Files.newOutputStream(file.toPath());
|
|
|
|
|
transferAndClose(input, outputStream);
|
|
|
|
|
success.set(true);
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
throw new CompletionException(e);
|
|
|
|
|
}
|
|
|
|
|
});
|
2023-07-16 13:09:05 +02:00
|
|
|
if (success.get()) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2023-01-14 20:18:48 +01:00
|
|
|
}
|
|
|
|
|
throw new CompletionException(caught.get());
|
2022-06-19 22:42:52 +02:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-10 00:09:13 +02:00
|
|
|
|
|
|
|
|
public static void transferAndClose(InputStream is, OutputStream target) throws IOException {
|
|
|
|
|
var bytesRead = 0;
|
|
|
|
|
|
|
|
|
|
byte[] smallBuffer = new byte[4096];
|
|
|
|
|
while ((bytesRead = is.read(smallBuffer)) >= 0) {
|
|
|
|
|
target.write(smallBuffer, 0, bytesRead);
|
|
|
|
|
}
|
|
|
|
|
target.close();
|
|
|
|
|
is.close();
|
|
|
|
|
}
|
|
|
|
|
}
|