Skip to content

Commit

Permalink
1.0.9 commit1
Browse files Browse the repository at this point in the history
  • Loading branch information
adam committed Mar 18, 2024
1 parent be30c8f commit 6456e5e
Show file tree
Hide file tree
Showing 21 changed files with 419 additions and 292 deletions.
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;
}
}
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
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
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.Hashes;
import com.adamcalculator.dynamicpack.util.Out;
import com.adamcalculator.dynamicpack.util.Urls;
import org.json.JSONArray;
import org.json.JSONObject;

import java.io.File;
import java.io.IOException;
import java.util.zip.ZipFile;

public class ModrinthRemote extends Remote {
private final Pack parent;
Expand Down Expand Up @@ -59,6 +66,56 @@ public boolean checkUpdateAvailable() throws IOException {
return !parent.getCurrentUnique().equals(latest.getString("id"));
}

@Override
public boolean sync(PackSyncProgress progress) throws IOException {
progress.textLog("getting latest version on modrinth...");
ModrinthRemote.LatestModrinthVersion latest = getLatest();

progress.textLog("downloading...");
File file = null;
int attempts = 3;
while (attempts > 0) {
file = Urls.downloadFileToTemp(latest.url, "dynamicpack_download", ".zip", Mod.MODRINTH_HTTPS_FILE_SIZE_LIMIT, new FileDownloadConsumer(){
@Override
public void onUpdate(FileDownloadConsumer it) {
float percentage = it.getPercentage();
progress.downloading("Modrinth pack (zip)", percentage);
}
});

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(DynamicPackModBase.CLIENT_FILE) != null;

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


if (!isDynamicPack) {
PackUtil.addFileToZip(file, DynamicPackModBase.CLIENT_FILE, parent.cachedJson.toString(2));
}
if (parent.isZip()) {
AFiles.moveFile(file, parent.getLocation());

} else {
AFiles.deleteDirectory(parent.getLocation());
AFiles.unzip(file, parent.getLocation());
}
progress.textLog("dynamicmcpack.json is updated.");

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

public LatestModrinthVersion getLatest() throws IOException {
JSONObject latest = parseLatestVersionJson();
String latestId = latest.getString("id");
Expand Down
Loading

0 comments on commit 6456e5e

Please sign in to comment.