Skip to content

Commit

Permalink
6
Browse files Browse the repository at this point in the history
  • Loading branch information
adam committed Mar 16, 2024
1 parent e83aebf commit 06c310a
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 14 deletions.
11 changes: 8 additions & 3 deletions src/client/java/com/adamcalculator/dynamicpack/DebugScreen.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@ protected void init() {
addDrawableChild(ButtonWidget.builder(Text.of("Re-Scan"), button -> {
DynamicPackMod.rescanPacks();
MinecraftClient.getInstance().setScreen(new DebugScreen());
}).size(50, 20).position(500, 10).build());
}).size(50, 20).position(this.width-60, 10).build());

int height = 10;
for (Pack pack : DynamicPackMod.packs) {
Out.println("gui pack: " + pack);
try {
addDrawableChild(ButtonWidget.builder(Text.of(pack.getLocation().getName() + ":"+pack.checkIsUpdateAvailable()), button -> {

}).size(100, 20).position(10, height).build());
}).size(160, 20).position(10, height).build());

addDrawableChild(ButtonWidget.builder(Text.of("Sync!"), button -> {
SystemToast toast = new SystemToast(SystemToast.Type.NARRATOR_TOGGLE, Text.literal("T"), Text.literal("d"));
Expand All @@ -49,12 +49,17 @@ public void textLog(String s) {

@Override
public void done(boolean b) {
if (b) {
toast.setContent(Text.literal("Done. Reload!"), Text.literal("Reload required!!!"));
} else {
toast.setContent(Text.literal("Done!"), Text.literal(""));
}
}
}, true);
} catch (Exception e) {
Out.e(e);
}
}).size(50, 20).position(100, height).build());
}).size(50, 20).position(190, height).build());
} catch (IOException e) {
addDrawableChild(ButtonWidget.builder(Text.of(e + ""), button -> {
}).size(500, 20).position(10, height).build());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ public LatestModrinthVersion getLatest() throws IOException {
JSONObject j = files.getJSONObject(i);
if (j.getBoolean("primary")) {
String url = j.getString("url");
return new LatestModrinthVersion(latestId, url);
JSONObject hashes = j.getJSONObject("hashes");
return new LatestModrinthVersion(latestId, url, hashes.getString("sha1"));
}
i++;
}
Expand All @@ -73,11 +74,12 @@ public static class LatestModrinthVersion {

public final String latestId;
public final String url;
public final String fileHash;

public LatestModrinthVersion(String latestId, String url) {
public LatestModrinthVersion(String latestId, String url, String fileHash) {
this.latestId = latestId;

this.url = url;
this.fileHash = fileHash;
}
}
}
31 changes: 24 additions & 7 deletions src/client/java/com/adamcalculator/dynamicpack/pack/Pack.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import java.io.File;
import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.util.Objects;
import java.util.zip.ZipFile;

Expand Down Expand Up @@ -54,6 +55,11 @@ public String getCurrentUnique() {
return cachedJson.getJSONObject("current").optString("version", "");
}


public String getCurrentVersionNumber() {
return cachedJson.getJSONObject("current").optString("version_number", "");
}

public boolean checkIsUpdateAvailable() throws IOException {
return remote.checkUpdateAvailable();
}
Expand Down Expand Up @@ -139,18 +145,33 @@ public boolean isContentActive(String id) {
return true; // todo
}

private boolean modrinthSync(ModrinthRemote modrinthRemote, SyncProgress progress) throws IOException {
private boolean modrinthSync(ModrinthRemote modrinthRemote, SyncProgress progress) throws IOException, NoSuchAlgorithmException {
progress.textLog("getting latest version on modrinth...");
ModrinthRemote.LatestModrinthVersion latest = modrinthRemote.getLatest();

progress.textLog("downloading...");
File file = Urls.downloadFileToTemp(latest.url, "dynamicpack_download", ".zip");
File file = null;
int attempts = 3;
while (attempts > 0) {
file = Urls.downloadFileToTemp(latest.url, "dynamicpack_download", ".zip");

if (Hashes.calcHashForFile(file).equals(latest.fileHash)) {
progress.textLog("Download done! Hashes is equals.");
break;
}
attempts--;
}
if (attempts == 0) {
throw new RuntimeException("Failed to download correct file from modrinth.");
}

ZipFile zipFile = new ZipFile(file);
boolean isDynamicPack = zipFile.getEntry(DynamicPackMod.CLIENT_FILE) != null;

cachedJson.getJSONObject("current").put("version", latest.latestId);
cachedJson.getJSONObject("current").remove("version_number");


if (!isDynamicPack) {
PackUtil.addFileToZip(file, DynamicPackMod.CLIENT_FILE, cachedJson.toString(2));
}
Expand All @@ -161,13 +182,9 @@ private boolean modrinthSync(ModrinthRemote modrinthRemote, SyncProgress progres
AFiles.deleteDirectory(this.location);
AFiles.unzip(file, this.location);
}

progress.textLog("dynamicmcpack.json is updated.");

progress.textLog("done!");
return true;
}

public String getCurrentVersionNumber() {
return cachedJson.getJSONObject("current").optString("version_number", "");
}
}
2 changes: 1 addition & 1 deletion src/main/java/com/adamcalculator/dynamicpack/Hashes.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import java.util.zip.ZipFile;

public class Hashes {
public static String calcHashFor(File file) throws IOException, NoSuchAlgorithmException {
public static String calcHashForFile(File file) throws IOException, NoSuchAlgorithmException {
byte[] data = Files.readAllBytes(file.toPath());
byte[] hash = MessageDigest.getInstance("SHA-1").digest(data);
return new BigInteger(1, hash).toString(16);
Expand Down

0 comments on commit 06c310a

Please sign in to comment.