From dfcd7aafb2e9e69b062cbbac3572d1149671d4b2 Mon Sep 17 00:00:00 2001 From: adam Date: Wed, 20 Mar 2024 19:08:24 +0700 Subject: [PATCH] noname commit --- .../dynamicpack/DynamicPackModBase.java | 6 ++++ .../com/adamcalculator/dynamicpack/Mod.java | 2 +- .../dynamicpack/pack/DynamicRepoRemote.java | 2 +- .../dynamicpack/pack/ModrinthRemote.java | 20 ++++++++++-- .../adamcalculator/dynamicpack/pack/Pack.java | 2 +- .../dynamicpack/pack/Remote.java | 2 +- .../dynamicpack/sync/SyncThread.java | 32 +++++++++++++++++++ .../dynamicpack/sync/SyncingTask.java | 8 ++++- .../adamcalculator/dynamicpack/util/Out.java | 4 +++ .../adamcalculator/dynamicpack/Compat.java | 13 ++++++++ .../dynamicpack/DebugScreen.java | 2 +- .../dynamicpack/DynamicPackScreen.java | 7 ++-- .../dynamicpack/FabricDynamicMod.java | 25 +++++++++++---- 13 files changed, 107 insertions(+), 18 deletions(-) create mode 100644 common/src/main/java/com/adamcalculator/dynamicpack/sync/SyncThread.java create mode 100644 src/client/java/com/adamcalculator/dynamicpack/Compat.java diff --git a/common/src/main/java/com/adamcalculator/dynamicpack/DynamicPackModBase.java b/common/src/main/java/com/adamcalculator/dynamicpack/DynamicPackModBase.java index 19dc2ee..ecdca4b 100644 --- a/common/src/main/java/com/adamcalculator/dynamicpack/DynamicPackModBase.java +++ b/common/src/main/java/com/adamcalculator/dynamicpack/DynamicPackModBase.java @@ -19,6 +19,7 @@ public abstract class DynamicPackModBase { public static final String MINECRAFT_META = "pack.mcmeta"; public static DynamicPackModBase INSTANCE; + protected static int manuallySyncThreadCounter = 0; private boolean isPacksScanning = false; private List packs = new ArrayList<>(); @@ -40,8 +41,13 @@ public void init(File gameDir) { startSyncThread(); } + /** + * ONLY FOR FIRST INIT RUN! FOR MANUALLY USE startManuallySync!!!!! + */ public abstract void startSyncThread(); + public abstract void startManuallySync(); + public void rescanPacks() { if (isPacksScanning) { Out.warn("rescanPacks already in scanning!"); diff --git a/common/src/main/java/com/adamcalculator/dynamicpack/Mod.java b/common/src/main/java/com/adamcalculator/dynamicpack/Mod.java index b523e9e..830d46e 100644 --- a/common/src/main/java/com/adamcalculator/dynamicpack/Mod.java +++ b/common/src/main/java/com/adamcalculator/dynamicpack/Mod.java @@ -101,7 +101,7 @@ public static boolean isHTTPTrafficAllowed() { // DebugScreen allowed public static boolean isDebugScreenAllowed() { - return true; + return false; } public static void debugNetwork() { diff --git a/common/src/main/java/com/adamcalculator/dynamicpack/pack/DynamicRepoRemote.java b/common/src/main/java/com/adamcalculator/dynamicpack/pack/DynamicRepoRemote.java index 01623ff..fb74540 100644 --- a/common/src/main/java/com/adamcalculator/dynamicpack/pack/DynamicRepoRemote.java +++ b/common/src/main/java/com/adamcalculator/dynamicpack/pack/DynamicRepoRemote.java @@ -107,7 +107,7 @@ public String getCurrentPackContentHash(String id) { @Override - public boolean sync(PackSyncProgress progress) throws IOException, NoSuchAlgorithmException { + public boolean sync(PackSyncProgress progress, boolean manually) throws IOException, NoSuchAlgorithmException { PackUtil.openPackFileSystem(parent.getLocation(), path -> { try { sync0(progress, path); diff --git a/common/src/main/java/com/adamcalculator/dynamicpack/pack/ModrinthRemote.java b/common/src/main/java/com/adamcalculator/dynamicpack/pack/ModrinthRemote.java index 127beed..1235d31 100644 --- a/common/src/main/java/com/adamcalculator/dynamicpack/pack/ModrinthRemote.java +++ b/common/src/main/java/com/adamcalculator/dynamicpack/pack/ModrinthRemote.java @@ -85,16 +85,27 @@ public boolean checkUpdateAvailable() throws IOException { return false; } if (latest.optString("version_number", "").equals(getCurrentVersionNumber())) { + Out.debug("version number equal"); return false; } + Out.debug("version rem.id="+latest.getString("id") + "; curr=" + getCurrentUnique()); + return !getCurrentUnique().equals(latest.getString("id")); } @Override - public boolean sync(PackSyncProgress progress) throws IOException { + public boolean sync(PackSyncProgress progress, boolean manually) throws IOException { progress.textLog("getting latest version on modrinth..."); ModrinthRemote.LatestModrinthVersion latest = getLatest(); + if (manually) { + if (latest.latestId.equals(getCurrentUnique())) { + progress.textLog("Manually & version ids equal. skipping."); + + return false; + } + } + progress.textLog("downloading..."); File tempFile = null; int attempts = 3; @@ -140,6 +151,7 @@ public void onUpdate(FileDownloadConsumer it) { public LatestModrinthVersion getLatest() throws IOException { JSONObject latest = parseLatestVersionJson(); String latestId = latest.getString("id"); + String latestVersionNumber = latest.getString("version_number"); JSONArray files = latest.getJSONArray("files"); int i = 0; while (i < files.length()) { @@ -147,7 +159,7 @@ public LatestModrinthVersion getLatest() throws IOException { if (j.getBoolean("primary")) { String url = j.getString("url"); JSONObject hashes = j.getJSONObject("hashes"); - return new LatestModrinthVersion(latestId, url, hashes.getString("sha1")); + return new LatestModrinthVersion(latestId, latestVersionNumber, url, hashes.getString("sha1")); } i++; } @@ -157,11 +169,13 @@ public LatestModrinthVersion getLatest() throws IOException { public static class LatestModrinthVersion { public final String latestId; + public final String latestVersionNumber; public final String url; public final String fileHash; - public LatestModrinthVersion(String latestId, String url, String fileHash) { + public LatestModrinthVersion(String latestId, String latestVersionNumber, String url, String fileHash) { this.latestId = latestId; + this.latestVersionNumber = latestVersionNumber; this.url = url; this.fileHash = fileHash; } diff --git a/common/src/main/java/com/adamcalculator/dynamicpack/pack/Pack.java b/common/src/main/java/com/adamcalculator/dynamicpack/pack/Pack.java index 5a2bbca..c7485bd 100644 --- a/common/src/main/java/com/adamcalculator/dynamicpack/pack/Pack.java +++ b/common/src/main/java/com/adamcalculator/dynamicpack/pack/Pack.java @@ -112,7 +112,7 @@ private void sync0(PackSyncProgress progress, boolean manually) throws Exception progress.start(); progress.textLog("start syncing..."); - boolean reloadRequired = remote.sync(progress); + boolean reloadRequired = remote.sync(progress, manually); isSyncing = false; progress.done(reloadRequired); diff --git a/common/src/main/java/com/adamcalculator/dynamicpack/pack/Remote.java b/common/src/main/java/com/adamcalculator/dynamicpack/pack/Remote.java index b155df5..1435582 100644 --- a/common/src/main/java/com/adamcalculator/dynamicpack/pack/Remote.java +++ b/common/src/main/java/com/adamcalculator/dynamicpack/pack/Remote.java @@ -28,5 +28,5 @@ public static void initRemoteTypes() { public abstract boolean checkUpdateAvailable() throws IOException; - public abstract boolean sync(PackSyncProgress progress) throws IOException, NoSuchAlgorithmException; + public abstract boolean sync(PackSyncProgress progress, boolean manually) throws IOException, NoSuchAlgorithmException; } diff --git a/common/src/main/java/com/adamcalculator/dynamicpack/sync/SyncThread.java b/common/src/main/java/com/adamcalculator/dynamicpack/sync/SyncThread.java new file mode 100644 index 0000000..d8e2894 --- /dev/null +++ b/common/src/main/java/com/adamcalculator/dynamicpack/sync/SyncThread.java @@ -0,0 +1,32 @@ +package com.adamcalculator.dynamicpack.sync; + +import java.util.function.Supplier; + +public class SyncThread extends Thread { + private static int counter = 0; + private static final long SLEEP_DELAY = 1000 * 60 * 60 * 24; // 24 hours + + private final Supplier taskSupplier; + + public SyncThread(Supplier taskSupplier) { + super("SyncThread" + (counter++)); + this.taskSupplier = taskSupplier; + } + + @Override + public void run() { + while (true) { + startSync(); + try { + Thread.sleep(SLEEP_DELAY); + } catch (InterruptedException e) { + throw new RuntimeException(e); + } + } + } + + + private void startSync() { + taskSupplier.get().run(); + } +} diff --git a/common/src/main/java/com/adamcalculator/dynamicpack/sync/SyncingTask.java b/common/src/main/java/com/adamcalculator/dynamicpack/sync/SyncingTask.java index a5e7f1c..a9e3234 100644 --- a/common/src/main/java/com/adamcalculator/dynamicpack/sync/SyncingTask.java +++ b/common/src/main/java/com/adamcalculator/dynamicpack/sync/SyncingTask.java @@ -12,7 +12,7 @@ * Re-check all packs and update packs with update available */ public class SyncingTask implements Runnable { - public static long threadCounter = 0; + public static boolean isSyncing = false; private final boolean manually; // skip checkUpdateAvailable(). private boolean reloadRequired = false; @@ -24,6 +24,11 @@ public SyncingTask(boolean manually) { @Override public void run() { + if (isSyncing) { + Out.warn("SyncTask already syncing...."); + return; + } + isSyncing = true; Out.println("SyncTask started!"); onSyncStart(); DynamicPackModBase.INSTANCE.rescanPacks(); @@ -39,6 +44,7 @@ public void run() { } onSyncDone(reloadRequired); Out.println("SyncTask ended!"); + isSyncing = false; } public void onPackDoneSuccess(Pack pack) {} diff --git a/common/src/main/java/com/adamcalculator/dynamicpack/util/Out.java b/common/src/main/java/com/adamcalculator/dynamicpack/util/Out.java index 401130b..cf627f6 100644 --- a/common/src/main/java/com/adamcalculator/dynamicpack/util/Out.java +++ b/common/src/main/java/com/adamcalculator/dynamicpack/util/Out.java @@ -62,4 +62,8 @@ public static void securityWarning(String s) { System.out.println("[dynamicpack] " + s); } } + + public static void debug(String s) { + println("DEBUG: " + s); + } } diff --git a/src/client/java/com/adamcalculator/dynamicpack/Compat.java b/src/client/java/com/adamcalculator/dynamicpack/Compat.java new file mode 100644 index 0000000..f24e674 --- /dev/null +++ b/src/client/java/com/adamcalculator/dynamicpack/Compat.java @@ -0,0 +1,13 @@ +package com.adamcalculator.dynamicpack; + +import net.minecraft.client.gui.Drawable; +import net.minecraft.client.gui.Element; +import net.minecraft.client.gui.Selectable; +import net.minecraft.client.gui.widget.ButtonWidget; +import net.minecraft.text.Text; + +public class Compat { + public static T createButton(Text text, Runnable press, int w, int h, int x, int y) { + return (T) ButtonWidget.builder(text, button -> press.run()).size(w, h).position(x, y).build(); + } +} diff --git a/src/client/java/com/adamcalculator/dynamicpack/DebugScreen.java b/src/client/java/com/adamcalculator/dynamicpack/DebugScreen.java index f5c744b..ebe975c 100644 --- a/src/client/java/com/adamcalculator/dynamicpack/DebugScreen.java +++ b/src/client/java/com/adamcalculator/dynamicpack/DebugScreen.java @@ -18,6 +18,6 @@ public void render(DrawContext context, int mouseX, int mouseY, float delta) { @Override protected void init() { - addDrawableChild(ButtonWidget.builder(Text.of("Re-Scan & Re-sync normally"), button -> DynamicPackModBase.INSTANCE.startSyncThread()).size(120, 20).position(this.width / 2, this.height / 2).build()); + addDrawableChild(ButtonWidget.builder(Text.of("Re-Scan & Re-sync normally"), button -> DynamicPackModBase.INSTANCE.startManuallySync()).size(120, 20).position(this.width / 2, this.height / 2).build()); } } diff --git a/src/client/java/com/adamcalculator/dynamicpack/DynamicPackScreen.java b/src/client/java/com/adamcalculator/dynamicpack/DynamicPackScreen.java index 9c26ed8..5cde355 100644 --- a/src/client/java/com/adamcalculator/dynamicpack/DynamicPackScreen.java +++ b/src/client/java/com/adamcalculator/dynamicpack/DynamicPackScreen.java @@ -4,7 +4,6 @@ import net.minecraft.client.MinecraftClient; import net.minecraft.client.gui.DrawContext; import net.minecraft.client.gui.screen.Screen; -import net.minecraft.client.gui.widget.ButtonWidget; import net.minecraft.text.Text; public class DynamicPackScreen extends Screen { @@ -24,7 +23,11 @@ public void render(DrawContext context, int mouseX, int mouseY, float delta) { @Override protected void init() { - addDrawableChild(ButtonWidget.builder(Text.of("Manually sync"), button -> DynamicPackModBase.INSTANCE.startSyncThread()).size(120, 20).position(this.width / 2, this.height / 2).build()); + addDrawableChild(Compat.createButton( + Text.of("Manually sync"), + () -> DynamicPackModBase.INSTANCE.startManuallySync(), + 100, 20, width - 120, 10 + )); } @Override diff --git a/src/client/java/com/adamcalculator/dynamicpack/FabricDynamicMod.java b/src/client/java/com/adamcalculator/dynamicpack/FabricDynamicMod.java index f278200..d253bd6 100644 --- a/src/client/java/com/adamcalculator/dynamicpack/FabricDynamicMod.java +++ b/src/client/java/com/adamcalculator/dynamicpack/FabricDynamicMod.java @@ -1,6 +1,7 @@ package com.adamcalculator.dynamicpack; import com.adamcalculator.dynamicpack.pack.Pack; +import com.adamcalculator.dynamicpack.sync.SyncThread; import com.adamcalculator.dynamicpack.sync.SyncingTask; import com.adamcalculator.dynamicpack.sync.state.StateDownloading; import com.adamcalculator.dynamicpack.sync.state.StateFileDeleted; @@ -43,23 +44,36 @@ public void onInitializeClient() { @Override public void startSyncThread() { - SyncingTask syncingTask = new SyncingTask(false) { + new SyncThread(() -> createSyncTask(false)).start(); + } + + @Override + public void startManuallySync() { + Thread thread = new Thread(() -> createSyncTask(true).run()); + thread.setName("DynamicPack-ManuallySyncThread" + (DynamicPackModBase.manuallySyncThreadCounter++)); + thread.start(); + } + + private SyncingTask createSyncTask(boolean manually) { + return new SyncingTask(manually) { @Override public void onSyncStart() { - setToastContent(Text.literal("DynamicPack"), Text.translatable("dynamicpack.toast.syncStarted")); + if (manually) setToastContent(Text.literal("DynamicPack"), Text.translatable("dynamicpack.toast.syncStarted")); } @Override public void onSyncDone(boolean reloadRequired) { - if (reloadRequired) { + if (manually || reloadRequired) { setToastContent(Text.literal("DynamicPack"), Text.translatable("dynamicpack.toast.done")); + } + if (reloadRequired) { tryToReloadResources(); } } @Override public void onStateChanged(Pack pack, SyncProgressState state) { - if (!FabricDynamicMod.SHOW_STATE) return; + if (!manually) return; if (state instanceof StateDownloading downloading) { setToastContent(Text.translatable("dynamicpack.toast.pack.state.downloading.title", pack.getName()), Text.translatable("dynamicpack.toast.pack.state.downloading.description", downloading.getPercentage(), downloading.getName())); @@ -72,9 +86,6 @@ public void onStateChanged(Pack pack, SyncProgressState state) { } } }; - Thread thread = new Thread(syncingTask); - thread.setName("DynamicPack-SyncTask" + (SyncingTask.threadCounter++)); - thread.start(); } @Override