Skip to content

Commit

Permalink
noname commit
Browse files Browse the repository at this point in the history
  • Loading branch information
adam committed Mar 20, 2024
1 parent 97fb007 commit dfcd7aa
Show file tree
Hide file tree
Showing 13 changed files with 107 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<Pack> packs = new ArrayList<>();
Expand All @@ -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!");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public static boolean isHTTPTrafficAllowed() {

// DebugScreen allowed
public static boolean isDebugScreenAllowed() {
return true;
return false;
}

public static void debugNetwork() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -140,14 +151,15 @@ 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()) {
JSONObject j = files.getJSONObject(i);
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++;
}
Expand All @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Original file line number Diff line number Diff line change
@@ -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<SyncingTask> taskSupplier;

public SyncThread(Supplier<SyncingTask> 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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
Expand All @@ -39,6 +44,7 @@ public void run() {
}
onSyncDone(reloadRequired);
Out.println("SyncTask ended!");
isSyncing = false;
}

public void onPackDoneSuccess(Pack pack) {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,8 @@ public static void securityWarning(String s) {
System.out.println("[dynamicpack] " + s);
}
}

public static void debug(String s) {
println("DEBUG: " + s);
}
}
13 changes: 13 additions & 0 deletions src/client/java/com/adamcalculator/dynamicpack/Compat.java
Original file line number Diff line number Diff line change
@@ -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 extends Element & Drawable & Selectable> 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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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()));
Expand All @@ -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
Expand Down

0 comments on commit dfcd7aa

Please sign in to comment.