mirror of
https://github.com/FalsePattern/FalsePatternLib.git
synced 2026-11-17 15:45:01 +00:00
added update checks and separate internet library
This commit is contained in:
7 files changed
+207
-101
No files matched your search
@@ -2,6 +2,8 @@ package com.falsepattern.lib.internal;
|
||||
|
||||
import com.falsepattern.lib.config.ConfigException;
|
||||
import com.falsepattern.lib.config.ConfigurationManager;
|
||||
import com.falsepattern.lib.updates.ModUpdateInfo;
|
||||
import com.falsepattern.lib.updates.UpdateChecker;
|
||||
import com.falsepattern.lib.util.ResourceUtil;
|
||||
import com.google.common.eventbus.EventBus;
|
||||
import com.google.common.eventbus.Subscribe;
|
||||
@@ -15,10 +17,15 @@ import cpw.mods.fml.common.event.FMLPostInitializationEvent;
|
||||
import cpw.mods.fml.common.event.FMLPreInitializationEvent;
|
||||
import lombok.Getter;
|
||||
import lombok.SneakyThrows;
|
||||
import lombok.val;
|
||||
import net.minecraft.launchwrapper.Launch;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.Future;
|
||||
|
||||
/**
|
||||
* Utility class used by FalsePatternLib's internal code. This can change between versions without notice, so do not use
|
||||
* this in your code!
|
||||
@@ -30,6 +37,8 @@ public class FalsePatternLib extends DummyModContainer {
|
||||
@Getter private static final boolean developerEnvironment =
|
||||
(boolean) Launch.blackboard.get("fml.deobfuscatedEnvironment");
|
||||
|
||||
private Future<List<ModUpdateInfo>> updateCheckFuture;
|
||||
|
||||
public FalsePatternLib() {
|
||||
super(MetadataCollection.from(ResourceUtil.getResourceFromJar("/mcmod.info", FalsePatternLib.class), Tags.MODID)
|
||||
.getMetadataForId(Tags.MODID, null));
|
||||
@@ -48,8 +57,26 @@ public class FalsePatternLib extends DummyModContainer {
|
||||
}
|
||||
|
||||
@Subscribe
|
||||
public void postInit(FMLPostInitializationEvent e) {
|
||||
public void preInit(FMLPreInitializationEvent e) {
|
||||
if (LibraryConfig.ENABLE_UPDATE_CHECKER) {
|
||||
getLog().info("Launching asynchronous update check. I'll check back on it during postInit.");
|
||||
updateCheckFuture = UpdateChecker.fetchUpdatesAsync("https://falsepattern.com/mc/versions.json");
|
||||
}
|
||||
}
|
||||
|
||||
@Subscribe
|
||||
public void postInit(FMLPostInitializationEvent e) {
|
||||
if (updateCheckFuture != null && !updateCheckFuture.isCancelled()) {
|
||||
try {
|
||||
val updates = updateCheckFuture.get();
|
||||
if (updates != null)
|
||||
for (val update: updates)
|
||||
update.log(getLog());
|
||||
} catch (InterruptedException | ExecutionException ex) {
|
||||
getLog().warn("Error while checking updates", ex);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("UnstableApiUsage")
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
package com.falsepattern.lib.internal;
|
||||
|
||||
import lombok.val;
|
||||
import lombok.var;
|
||||
|
||||
import java.io.*;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URL;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
public class Internet {
|
||||
public static void connect(URL URL, Consumer<Exception> onError, Consumer<InputStream> onSuccess) {
|
||||
try {
|
||||
val connection = (HttpURLConnection) URL.openConnection();
|
||||
connection.setConnectTimeout(3500);
|
||||
connection.setReadTimeout(5000);
|
||||
connection.setRequestProperty("User-Agent", "FalsePatternLib Internet Connector");
|
||||
if (connection.getResponseCode() != 200) {
|
||||
onError.accept(new Exception("HTTP response code " + connection.getResponseCode()));
|
||||
} else {
|
||||
onSuccess.accept(connection.getInputStream());
|
||||
}
|
||||
connection.disconnect();
|
||||
} catch (Exception e) {
|
||||
onError.accept(e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user