2022-07-31 20:41:56 +02:00
|
|
|
/*
|
2024-04-17 17:23:43 +02:00
|
|
|
* This file is part of FalsePatternLib.
|
|
|
|
|
*
|
2025-05-24 19:29:12 +02:00
|
|
|
* Copyright (C) 2022-2025 FalsePattern
|
2022-07-30 15:21:11 +02:00
|
|
|
* All Rights Reserved
|
|
|
|
|
*
|
2024-04-17 17:23:43 +02:00
|
|
|
* The above copyright notice and this permission notice shall be included
|
|
|
|
|
* in all copies or substantial portions of the Software.
|
2022-07-30 15:21:11 +02:00
|
|
|
*
|
2024-04-17 17:23:43 +02:00
|
|
|
* FalsePatternLib is free software: you can redistribute it and/or modify
|
2022-07-30 15:21:11 +02:00
|
|
|
* it under the terms of the GNU Lesser General Public License as published by
|
2025-05-24 19:29:12 +02:00
|
|
|
* the Free Software Foundation, only version 3 of the License.
|
2022-07-30 15:21:11 +02:00
|
|
|
*
|
2024-04-17 17:23:43 +02:00
|
|
|
* FalsePatternLib is distributed in the hope that it will be useful,
|
2022-07-30 15:21:11 +02:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2024-04-17 17:23:43 +02:00
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU Lesser General Public License for more details.
|
2022-07-30 15:21:11 +02:00
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU Lesser General Public License
|
2024-04-17 17:23:43 +02:00
|
|
|
* along with FalsePatternLib. If not, see <https://www.gnu.org/licenses/>.
|
2022-07-30 15:21:11 +02:00
|
|
|
*/
|
2022-06-10 00:09:13 +02:00
|
|
|
package com.falsepattern.lib.internal;
|
|
|
|
|
|
|
|
|
|
import lombok.val;
|
|
|
|
|
|
2023-02-12 09:34:12 +01:00
|
|
|
import net.minecraft.launchwrapper.Launch;
|
|
|
|
|
|
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;
|
2025-05-18 13:27:23 +02:00
|
|
|
import java.net.URLConnection;
|
2023-01-17 16:54:04 +01:00
|
|
|
import java.util.Collections;
|
|
|
|
|
import java.util.Map;
|
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
|
|
|
|
2024-04-17 17:17:44 +02:00
|
|
|
public static void connect(URL URL, Consumer<Exception> onError, Consumer<InputStream> onSuccess, Consumer<Long> contentLengthCallback) {
|
|
|
|
|
connect(URL, NO_HEADERS, onError, onSuccess, contentLengthCallback);
|
2023-01-17 16:54:04 +01:00
|
|
|
}
|
|
|
|
|
|
2024-04-17 17:17:44 +02:00
|
|
|
public static void connect(URL URL, Map<String, String> headers, Consumer<Exception> onError, Consumer<InputStream> onSuccess, Consumer<Long> contentLengthCallback) {
|
2022-06-10 00:09:13 +02:00
|
|
|
try {
|
2025-05-18 13:27:23 +02:00
|
|
|
val urlConnection = URL.openConnection();
|
|
|
|
|
if (urlConnection instanceof HttpURLConnection) {
|
|
|
|
|
connectHTTP((HttpURLConnection) urlConnection, headers, onError, onSuccess, contentLengthCallback);
|
2022-06-10 00:09:13 +02:00
|
|
|
} else {
|
2025-05-18 13:27:23 +02:00
|
|
|
executeTransfer(urlConnection, onSuccess, contentLengthCallback);
|
2022-06-10 00:09:13 +02:00
|
|
|
}
|
|
|
|
|
} 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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-18 13:27:23 +02:00
|
|
|
private static void connectHTTP(HttpURLConnection connection, Map<String, String> headers, Consumer<Exception> onError, Consumer<InputStream> onSuccess, Consumer<Long> contentLengthCallback) throws IOException {
|
|
|
|
|
connection.setConnectTimeout(3500);
|
|
|
|
|
connection.setReadTimeout(5000);
|
|
|
|
|
connection.setRequestProperty("User-Agent",
|
|
|
|
|
Tags.MODNAME
|
|
|
|
|
+ " "
|
|
|
|
|
+ Tags.VERSION
|
|
|
|
|
+ " Internet Connector"
|
|
|
|
|
+ " (https://github.com/FalsePattern/FalsePatternLib)");
|
|
|
|
|
for (val header : headers.entrySet()) {
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
if (connection.getResponseCode() != 200) {
|
|
|
|
|
onError.accept(new Exception("HTTP response code " + connection.getResponseCode()));
|
|
|
|
|
} else {
|
|
|
|
|
executeTransfer(connection, onSuccess, contentLengthCallback);
|
|
|
|
|
}
|
|
|
|
|
connection.disconnect();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static void executeTransfer(URLConnection connection, Consumer<InputStream> onSuccess, Consumer<Long> contentLengthCallback) throws IOException {
|
|
|
|
|
contentLengthCallback.accept(connection.getContentLengthLong());
|
|
|
|
|
onSuccess.accept(connection.getInputStream());
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-17 17:17:44 +02:00
|
|
|
public static void transferAndClose(InputStream is, OutputStream target, Consumer<Integer> downloadSizeCallback)
|
|
|
|
|
throws IOException {
|
2022-06-10 00:09:13 +02:00
|
|
|
var bytesRead = 0;
|
|
|
|
|
|
2024-04-17 17:17:44 +02:00
|
|
|
byte[] smallBuffer = new byte[256 * 1024];
|
2022-06-10 00:09:13 +02:00
|
|
|
while ((bytesRead = is.read(smallBuffer)) >= 0) {
|
|
|
|
|
target.write(smallBuffer, 0, bytesRead);
|
2024-04-17 17:17:44 +02:00
|
|
|
downloadSizeCallback.accept(bytesRead);
|
2022-06-10 00:09:13 +02:00
|
|
|
}
|
|
|
|
|
target.close();
|
|
|
|
|
is.close();
|
|
|
|
|
}
|
|
|
|
|
}
|