Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

1.0.9 Backport to 1.19.4 #13

Merged
merged 8 commits into from
Mar 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion common/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ plugins {
}

group = "com.adamcalculator"
version = "1.0.8"
version = "1.0.9"


repositories {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@

public abstract class DynamicPackModBase {
public static final String CLIENT_FILE = "dynamicmcpack.json";
public static final String MINECRAFT_META = "pack.mcmeta";

public static DynamicPackModBase INSTANCE;

public static List<Pack> packs = new ArrayList<>();
private File gameDir;
private File resourcePacks;
private boolean minecraftInitialized = false;


public void init(File gameDir) {
Expand All @@ -30,8 +32,8 @@ public void init(File gameDir) {
}
INSTANCE = this;
this.gameDir = gameDir;
resourcePacks = new File(gameDir, "resourcepacks");
resourcePacks.mkdirs();
this.resourcePacks = new File(gameDir, "resourcepacks");
this.resourcePacks.mkdirs();

startSyncThread();
}
Expand Down Expand Up @@ -66,18 +68,14 @@ public void rescanPacks() {


private void processPack(File location, JSONObject json) {
Out.println("pack " + location + ": " + json);

long formatVersion = json.getLong("formatVersion");
if (formatVersion == 1) {

Pack pack = new Pack(location, json);
packs.add(pack);

} else {
throw new RuntimeException("Unsupported formatVersion!");
}

}

public boolean isResourcePackActive(Pack pack) throws IOException {
Expand All @@ -95,4 +93,12 @@ public boolean isResourcePackActive(Pack pack) throws IOException {
public File getGameDir() {
return gameDir;
}

public void minecraftInitialized() {
this.minecraftInitialized = true;
}

public boolean isMinecraftInitialized() {
return minecraftInitialized;
}
}
10 changes: 10 additions & 0 deletions common/src/main/java/com/adamcalculator/dynamicpack/Mod.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,14 @@ public static boolean isHTTPTrafficAllowed() {
public static boolean isDebugScreenAllowed() {
return true;
}

public static void debugNetwork() {
if (isRelease()) return;

try {
Thread.sleep(13);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,12 @@
public class PackUtil {

public static JSONObject readJson(InputStream inputStream) throws IOException {
return new JSONObject(readString(inputStream));
}

public static String readString(InputStream inputStream) throws IOException {
byte[] bytes = inputStream.readAllBytes();
return new JSONObject(new String(bytes, StandardCharsets.UTF_8));
return new String(bytes, StandardCharsets.UTF_8);
}


Expand All @@ -44,4 +48,11 @@ public static void walkScan(Set<String> buffer, Path path) throws IOException {
}
});
}

public static boolean isPathFileExists(Path path) throws IOException {
if (Files.exists(path)) {
return !Files.isDirectory(path);
}
return false;
}
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
package com.adamcalculator.dynamicpack.pack;

import com.adamcalculator.dynamicpack.DynamicPackModBase;
import com.adamcalculator.dynamicpack.Mod;
import com.adamcalculator.dynamicpack.PackUtil;
import com.adamcalculator.dynamicpack.sync.PackSyncProgress;
import com.adamcalculator.dynamicpack.util.AFiles;
import com.adamcalculator.dynamicpack.util.Out;
import com.adamcalculator.dynamicpack.util.Urls;
import org.json.JSONObject;

import java.io.File;
import java.io.IOException;
import java.security.NoSuchAlgorithmException;

public class DynamicRepoRemote extends Remote {
private final Pack parent;
Expand Down Expand Up @@ -36,6 +44,48 @@ public boolean checkUpdateAvailable() throws IOException {
return parent.getCurrentBuild() != Long.parseLong(content);
}

@Override
public boolean sync(PackSyncProgress progress) throws IOException, NoSuchAlgorithmException {
String packUrlContent;
progress.downloading("dynamicmcpack.repo.json", 0);
if (skipSign) {
packUrlContent = Urls.parseContent(packUrl, Mod.MOD_FILES_LIMIT);
Out.warn("Dynamic pack " + parent.getLocation().getName() + " is skipping signing.");
progress.textLog("File parsed, verify skipped.");

} else {
packUrlContent = Urls.parseContentAndVerify(packSigUrl, packUrl, publicKey, Mod.MOD_FILES_LIMIT);
progress.textLog("Success parse and verify file.");
}
progress.downloading("dynamicmcpack.repo.json", 100);

JSONObject j = new JSONObject(packUrlContent);
if (j.getLong("formatVersion") != 1) {
throw new RuntimeException("Incompatible formatVersion!");
}


DynamicRepoSyncProcessV1 dynamicRepoSyncProcessV1 = new DynamicRepoSyncProcessV1(parent, this, progress, j);
try {
dynamicRepoSyncProcessV1.run();
dynamicRepoSyncProcessV1.close();

} catch (Exception e) {
dynamicRepoSyncProcessV1.close();
throw e;
}

parent.current_build = j.getLong("build");
parent.cachedJson.getJSONObject("current").put("build", parent.current_build);

if (parent.isZip()) {
PackUtil.addFileToZip(parent.getLocation(), DynamicPackModBase.CLIENT_FILE, parent.cachedJson.toString(2));
} else {
AFiles.write(new File(parent.getLocation(), DynamicPackModBase.CLIENT_FILE), parent.cachedJson.toString(2));
}
return true;
}

public String getUrl() {
return url;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.adamcalculator.dynamicpack.pack;

import com.adamcalculator.dynamicpack.*;
import com.adamcalculator.dynamicpack.sync.PackSyncProgress;
import com.adamcalculator.dynamicpack.sync.state.StateFileDeleted;
import com.adamcalculator.dynamicpack.util.*;
import org.json.JSONArray;
import org.json.JSONObject;
Expand All @@ -18,14 +20,14 @@
public class DynamicRepoSyncProcessV1 {
private final Pack parent;
private final DynamicRepoRemote remote;
private final SyncProgress progress;
private final PackSyncProgress progress;
private final JSONObject j;

private final Set<String> oldestFilesList = new HashSet<>();
private FileSystem zipFileSystem;
private Path packRootPath;

public DynamicRepoSyncProcessV1(Pack pack, DynamicRepoRemote dynamicRepoRemote, SyncProgress progress, JSONObject j) throws IOException {
public DynamicRepoSyncProcessV1(Pack pack, DynamicRepoRemote dynamicRepoRemote, PackSyncProgress progress, JSONObject j) throws IOException {
this.parent = pack;
this.remote = dynamicRepoRemote;
this.progress = progress;
Expand Down Expand Up @@ -54,9 +56,11 @@ public void run() throws IOException, NoSuchAlgorithmException {

for (String s : oldestFilesList) {
if (s.contains(DynamicPackModBase.CLIENT_FILE)) continue;
Path path = packRootPath.resolve(s);
progress.stateChanged(new StateFileDeleted(path));

progress.textLog("File deleted from resource-pack: " + s);
AFiles.noEmptyDirDelete(packRootPath.resolve(s));
AFiles.noEmptyDirDelete(path);
}
}

Expand Down Expand Up @@ -85,11 +89,13 @@ private void processContent(JSONObject object) throws IOException, NoSuchAlgorit
}

progress.textLog("process content id:" + id);
progress.downloading("<content>.json", 0);
String content = compressSupported ? Urls.parseGZipContent(urlCompressed, Mod.GZIP_LIMIT) : Urls.parseContent(url, Mod.MOD_FILES_LIMIT);
String receivedHash = Hashes.calcHashForBytes(content.getBytes(StandardCharsets.UTF_8));
if (!contentRemoteHash.equals(receivedHash)) {
throw new SecurityException("Hash of content at " + url + " not verified. remote: " + contentRemoteHash + "; received: " + receivedHash);
}
progress.downloading("<content>.json", 100);
processContentParsed(new JSONObject(content));
}

Expand Down Expand Up @@ -130,7 +136,7 @@ private void processContentParsed(JSONObject j) throws IOException {
Urls.downloadDynamicFile(realUrl, filePath, hash, new FileDownloadConsumer() {
@Override
public void onUpdate(FileDownloadConsumer it) {
progress.downloading(path, it.getPercentage());
progress.downloading(filePath.getFileName().toString(), it.getPercentage());
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public void accept(long value) {

public float getPercentage() {
if (max <= latest) {
return -1;
return 99.99f;
}

return (float) latest * 100f / (float) max;
Expand Down
Loading
Loading