mirror of
https://github.com/FalsePattern/FalsePatternLib.git
synced 2026-11-18 16:09:15 +00:00
port update checker to CompletableFuture
This commit is contained in:
5 files changed
+144
-137
No files matched your search
@@ -6,6 +6,9 @@ import lombok.var;
|
||||
import java.io.*;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URL;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.CompletionException;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
public class Internet {
|
||||
@@ -27,6 +30,21 @@ public class Internet {
|
||||
}
|
||||
}
|
||||
|
||||
public static CompletableFuture<byte[]> download(URL URL) {
|
||||
return CompletableFuture.supplyAsync(() -> {
|
||||
val result = new ByteArrayOutputStream();
|
||||
AtomicReference<Exception> caught = new AtomicReference<>();
|
||||
connect(URL, caught::set, (input) -> {
|
||||
try {
|
||||
transferAndClose(input, result);
|
||||
} catch (IOException e) {
|
||||
throw new CompletionException(e);
|
||||
}
|
||||
});
|
||||
return result.toByteArray();
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
public static void transferAndClose(InputStream is, OutputStream target) throws IOException {
|
||||
var bytesRead = 0;
|
||||
|
||||
@@ -3,7 +3,7 @@ package com.falsepattern.lib.internal.proxy;
|
||||
import com.falsepattern.lib.internal.FalsePatternLib;
|
||||
import com.falsepattern.lib.internal.Tags;
|
||||
import com.falsepattern.lib.updates.UpdateChecker;
|
||||
import com.falsepattern.lib.util.AsyncUtil;
|
||||
import cpw.mods.fml.client.IModGuiFactory;
|
||||
import cpw.mods.fml.common.event.FMLPostInitializationEvent;
|
||||
import cpw.mods.fml.common.event.FMLPreInitializationEvent;
|
||||
import cpw.mods.fml.common.eventhandler.SubscribeEvent;
|
||||
@@ -15,14 +15,13 @@ import net.minecraft.util.IChatComponent;
|
||||
import net.minecraftforge.common.MinecraftForge;
|
||||
import net.minecraftforge.event.entity.EntityJoinWorldEvent;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.Callable;
|
||||
import java.util.concurrent.Future;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
@SideOnly(Side.CLIENT)
|
||||
public class ClientProxy extends CommonProxy {
|
||||
private Future<List<IChatComponent>> chatFuture;
|
||||
private CompletableFuture<List<IChatComponent>> chatFuture;
|
||||
|
||||
@Override
|
||||
public void preInit(FMLPreInitializationEvent e) {
|
||||
@@ -33,21 +32,11 @@ public class ClientProxy extends CommonProxy {
|
||||
@Override
|
||||
public void postInit(FMLPostInitializationEvent e) {
|
||||
super.postInit(e);
|
||||
chatFuture = AsyncUtil.asyncWorker.submit(new Callable<List<IChatComponent>>() {
|
||||
@Override
|
||||
public List<IChatComponent> call() throws Exception {
|
||||
//Deadlock avoidance
|
||||
if (updatesFuture == null || updatesFuture.isCancelled()) {
|
||||
chatFuture = null;
|
||||
return null;
|
||||
}
|
||||
if (!updatesFuture.isDone()) {
|
||||
chatFuture = AsyncUtil.asyncWorker.submit(this);
|
||||
return null;
|
||||
}
|
||||
val updates = updatesFuture.get();
|
||||
return UpdateChecker.updateListToChatMessages(Tags.MODNAME, updates);
|
||||
chatFuture = updatesFuture.handleAsync((updates, exception) -> {
|
||||
if (exception != null || updates.isEmpty()) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
return UpdateChecker.updateListToChatMessages(Tags.MODNAME, updates);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -57,7 +46,7 @@ public class ClientProxy extends CommonProxy {
|
||||
!(e.entity instanceof EntityPlayerSP)) return;
|
||||
val player = (EntityPlayerSP) e.entity;
|
||||
try {
|
||||
for (val line: chatFuture.get(1, TimeUnit.SECONDS)) {
|
||||
for (val line: chatFuture.get()) {
|
||||
player.addChatMessage(line);
|
||||
}
|
||||
chatFuture = null;
|
||||
|
||||
@@ -6,21 +6,17 @@ import com.falsepattern.lib.internal.FalsePatternLib;
|
||||
import com.falsepattern.lib.internal.LibraryConfig;
|
||||
import com.falsepattern.lib.updates.ModUpdateInfo;
|
||||
import com.falsepattern.lib.updates.UpdateChecker;
|
||||
import com.falsepattern.lib.util.AsyncUtil;
|
||||
import cpw.mods.fml.common.event.FMLConstructionEvent;
|
||||
import cpw.mods.fml.common.event.FMLPostInitializationEvent;
|
||||
import cpw.mods.fml.common.event.FMLPreInitializationEvent;
|
||||
import lombok.val;
|
||||
import lombok.var;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.Callable;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.Future;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
public class CommonProxy {
|
||||
|
||||
protected Future<List<ModUpdateInfo>> updatesFuture;
|
||||
protected CompletableFuture<List<ModUpdateInfo>> updatesFuture;
|
||||
|
||||
public void preInit(FMLPreInitializationEvent e) {
|
||||
ConfigurationManager.registerBus();
|
||||
@@ -31,37 +27,22 @@ public class CommonProxy {
|
||||
}
|
||||
if (LibraryConfig.ENABLE_UPDATE_CHECKER) {
|
||||
FalsePatternLib.getLog().info("Launching asynchronous update check.");
|
||||
val updateCheckFuture = UpdateChecker.fetchUpdatesAsync(FalsePatternLib.UPDATE_URL);
|
||||
updatesFuture = AsyncUtil.asyncWorker.submit(new Callable<List<ModUpdateInfo>>() {
|
||||
@Override
|
||||
public List<ModUpdateInfo> call() {
|
||||
//Deadlock avoidance
|
||||
if (updateCheckFuture.isCancelled()) {
|
||||
updatesFuture = null;
|
||||
return null;
|
||||
}
|
||||
if (!updateCheckFuture.isDone()) {
|
||||
updatesFuture = AsyncUtil.asyncWorker.submit(this);
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
var updates = updateCheckFuture.get();
|
||||
if (updates != null && updates.size() > 0) {
|
||||
for (val update : updates) {
|
||||
update.log(FalsePatternLib.getLog());
|
||||
}
|
||||
} else if (updates == null) {
|
||||
FalsePatternLib.getLog().warn("Unknown error while checking updates.");
|
||||
} else {
|
||||
FalsePatternLib.getLog().info("All checked mods up to date!");
|
||||
updates = null;
|
||||
}
|
||||
return updates;
|
||||
} catch (InterruptedException | ExecutionException ex) {
|
||||
FalsePatternLib.getLog().warn("Error while checking updates", ex);
|
||||
}
|
||||
return null;
|
||||
updatesFuture = UpdateChecker.fetchUpdatesAsync(FalsePatternLib.UPDATE_URL).thenApplyAsync(updates -> {
|
||||
if (updates == null) {
|
||||
updates = Collections.emptyList();
|
||||
}
|
||||
if (updates.isEmpty()) {
|
||||
FalsePatternLib.getLog().info("No updates found.");
|
||||
} else {
|
||||
FalsePatternLib.getLog().info("Found {} updates.", updates.size());
|
||||
for (val update : updates) {
|
||||
update.log(FalsePatternLib.getLog());
|
||||
}
|
||||
}
|
||||
return updates;
|
||||
}).exceptionally(ex -> {
|
||||
FalsePatternLib.getLog().error("Failed to check for updates!", ex);
|
||||
return Collections.emptyList();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user