Multi-Threading
This commit is contained in:
parent
17f56fd400
commit
0a941684f8
@ -6,6 +6,8 @@ import net.fabricmc.api.ModInitializer;
|
|||||||
import org.apache.logging.log4j.LogManager;
|
import org.apache.logging.log4j.LogManager;
|
||||||
import org.apache.logging.log4j.Logger;
|
import org.apache.logging.log4j.Logger;
|
||||||
|
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
public class ModUpdater implements ModInitializer {
|
public class ModUpdater implements ModInitializer {
|
||||||
public static final String NAMESPACE = "modupdater";
|
public static final String NAMESPACE = "modupdater";
|
||||||
|
|
||||||
@ -21,19 +23,28 @@ public class ModUpdater implements ModInitializer {
|
|||||||
|
|
||||||
private static ModUpdate[] updates;
|
private static ModUpdate[] updates;
|
||||||
|
|
||||||
|
private static Thread updateThread;
|
||||||
|
|
||||||
public static ModUpdate[] getUpdates() {
|
public static ModUpdate[] getUpdates() {
|
||||||
if (updates == null) {
|
if (updates == null) {
|
||||||
updates = ModUpdateStrategies.findAvailableUpdates();
|
if (Thread.currentThread() == updateThread) {
|
||||||
|
updates = ModUpdateStrategies.findAvailableUpdates();
|
||||||
|
} else {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return updates;
|
return updates;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onInitialize() {
|
public void onInitialize() {
|
||||||
getLogger().info("Checking For Mod Updates...");
|
updateThread = new Thread(() -> {
|
||||||
for (ModUpdate update : getUpdates()) {
|
getLogger().info("Checking For Mod Updates...");
|
||||||
getLogger().info(update.text + " (" + update.downloadURL + ')');
|
for (ModUpdate update : Objects.requireNonNull(getUpdates())) {
|
||||||
}
|
getLogger().info(update.text + " (" + update.downloadURL + ')');
|
||||||
getLogger().info(updates.length + " Mod Update(s) Found");
|
}
|
||||||
|
getLogger().info(updates.length + " Mod Update(s) Found");
|
||||||
|
});
|
||||||
|
//updateThread.start();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -14,9 +14,11 @@ import net.minecraft.client.util.math.MatrixStack;
|
|||||||
import net.minecraft.text.TranslatableText;
|
import net.minecraft.text.TranslatableText;
|
||||||
import net.minecraft.util.Util;
|
import net.minecraft.util.Util;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
|
||||||
@Environment(EnvType.CLIENT)
|
@Environment(EnvType.CLIENT)
|
||||||
public class ModUpdateScreen extends Screen {
|
public class ModUpdateScreen extends Screen {
|
||||||
private ModUpdateListWidget list;
|
public ModUpdateListWidget list;
|
||||||
private ButtonWidget download;
|
private ButtonWidget download;
|
||||||
private final Screen parent;
|
private final Screen parent;
|
||||||
|
|
||||||
@ -56,13 +58,25 @@ public class ModUpdateScreen extends Screen {
|
|||||||
@Environment(EnvType.CLIENT)
|
@Environment(EnvType.CLIENT)
|
||||||
private static class ModUpdateListWidget extends EntryListWidget<ModUpdateEntry> {
|
private static class ModUpdateListWidget extends EntryListWidget<ModUpdateEntry> {
|
||||||
private final ModUpdateScreen screen;
|
private final ModUpdateScreen screen;
|
||||||
|
private ModUpdate[] updates = null;
|
||||||
|
|
||||||
private ModUpdateListWidget(MinecraftClient client, ModUpdateScreen screen) {
|
private ModUpdateListWidget(MinecraftClient client, ModUpdateScreen screen) {
|
||||||
super(client, screen.width, screen.height, 32, screen.height - 40, 18);
|
super(client, screen.width, screen.height, 32, screen.height - 40, 18);
|
||||||
this.screen = screen;
|
this.screen = screen;
|
||||||
|
|
||||||
for (ModUpdate update : ModUpdater.getUpdates()) {
|
reload();
|
||||||
addEntry(new ModUpdateEntry(update, screen, this));
|
}
|
||||||
|
|
||||||
|
public void reload() {
|
||||||
|
ModUpdate[] newUpdates = ModUpdater.getUpdates();
|
||||||
|
if (!Arrays.equals(updates, newUpdates)) {
|
||||||
|
clearEntries();
|
||||||
|
if (newUpdates != null) {
|
||||||
|
for (ModUpdate update : newUpdates) {
|
||||||
|
addEntry(new ModUpdateEntry(update, screen, this));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
updates = newUpdates;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -100,6 +114,15 @@ public class ModUpdateScreen extends Screen {
|
|||||||
protected boolean isFocused() {
|
protected boolean isFocused() {
|
||||||
return screen.getFocused() == this;
|
return screen.getFocused() == this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void render(MatrixStack matrices, int mouseX, int mouseY, float delta) {
|
||||||
|
reload();
|
||||||
|
super.render(matrices, mouseX, mouseY, delta);
|
||||||
|
if (updates == null) {
|
||||||
|
drawCenteredText(matrices, screen.textRenderer, new TranslatableText("gui.modupdater.loading"), width / 2, height / 2 - screen.textRenderer.fontHeight, 16777215);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Environment(EnvType.CLIENT)
|
@Environment(EnvType.CLIENT)
|
||||||
|
@ -13,6 +13,7 @@ import java.util.ArrayList;
|
|||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.concurrent.atomic.AtomicInteger;
|
||||||
|
|
||||||
public class ModUpdateStrategies {
|
public class ModUpdateStrategies {
|
||||||
private static final Map<String, ModUpdateStrategy> data = new HashMap<>();
|
private static final Map<String, ModUpdateStrategy> data = new HashMap<>();
|
||||||
@ -40,26 +41,50 @@ public class ModUpdateStrategies {
|
|||||||
public static ModUpdate[] findAvailableUpdates() {
|
public static ModUpdate[] findAvailableUpdates() {
|
||||||
List<ModUpdate> updates = new ArrayList<>();
|
List<ModUpdate> updates = new ArrayList<>();
|
||||||
|
|
||||||
|
AtomicInteger remaining = new AtomicInteger(0);
|
||||||
|
|
||||||
for (ModContainer mod : FabricLoader.getInstance().getAllMods()) {
|
for (ModContainer mod : FabricLoader.getInstance().getAllMods()) {
|
||||||
ModMetadata metadata = mod.getMetadata();
|
Thread thread = new Thread(() -> {
|
||||||
String name = metadata.getName() + " (" + metadata.getId() + ')';
|
ModMetadata metadata = mod.getMetadata();
|
||||||
|
String name = metadata.getName() + " (" + metadata.getId() + ')';
|
||||||
|
|
||||||
ModUpdate update = null;
|
ModUpdate update = null;
|
||||||
if (metadata.containsCustomValue(ModUpdater.NAMESPACE)) {
|
if (metadata.containsCustomValue(ModUpdater.NAMESPACE)) {
|
||||||
try {
|
try {
|
||||||
update = checkForUpdate(metadata, new ConfigObject.ConfigObjectCustom(metadata.getCustomValue(ModUpdater.NAMESPACE).getAsObject()), name);
|
update = checkForUpdate(metadata, new ConfigObject.ConfigObjectCustom(metadata.getCustomValue(ModUpdater.NAMESPACE).getAsObject()), name);
|
||||||
} catch (ClassCastException e) {
|
} catch (ClassCastException e) {
|
||||||
ModUpdater.invalidModUpdaterConfig(name);
|
ModUpdater.invalidModUpdaterConfig(name);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
ConfigObject obj = HardcodedData.getData(metadata.getId());
|
||||||
|
if (obj != null) {
|
||||||
|
update = checkForUpdate(metadata, obj, name);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
ConfigObject obj = HardcodedData.getData(metadata.getId());
|
if (update != null) {
|
||||||
if (obj != null) {
|
synchronized (updates) {
|
||||||
update = checkForUpdate(metadata, obj, name);
|
updates.add(update);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
synchronized (remaining) {
|
||||||
|
remaining.decrementAndGet();
|
||||||
|
remaining.notifyAll();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
synchronized (remaining) {
|
||||||
|
remaining.incrementAndGet();
|
||||||
}
|
}
|
||||||
|
thread.start();
|
||||||
|
}
|
||||||
|
|
||||||
if (update != null) {
|
synchronized (remaining) {
|
||||||
updates.add(update);
|
while (remaining.get() > 0) {
|
||||||
|
try {
|
||||||
|
remaining.wait();
|
||||||
|
} catch (InterruptedException ignored) {
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -35,8 +35,10 @@ public class Util {
|
|||||||
public static final String JAR_EXTENSION = ".jar";
|
public static final String JAR_EXTENSION = ".jar";
|
||||||
|
|
||||||
public static String getVersionFromFileName(String fileName) {
|
public static String getVersionFromFileName(String fileName) {
|
||||||
int index = fileName.indexOf("-");
|
while (!Character.isDigit(fileName.charAt(0))) {
|
||||||
fileName = fileName.substring(index != -1 ? index + 1 : 0);
|
int index = fileName.indexOf("-");
|
||||||
|
fileName = fileName.substring(index != -1 ? index + 1 : 0);
|
||||||
|
}
|
||||||
if (fileName.endsWith(JAR_EXTENSION)) {
|
if (fileName.endsWith(JAR_EXTENSION)) {
|
||||||
fileName = fileName.substring(0, fileName.length() - JAR_EXTENSION.length());
|
fileName = fileName.substring(0, fileName.length() - JAR_EXTENSION.length());
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
{
|
{
|
||||||
"gui.modupdater.title": "Available Mod Updates",
|
"gui.modupdater.title": "Available Mod Updates",
|
||||||
"gui.modupdater.download": "Download"
|
"gui.modupdater.download": "Download",
|
||||||
|
"gui.modupdater.loading": "Loading..."
|
||||||
}
|
}
|
Reference in New Issue
Block a user