Skip to content

Commit

Permalink
night
Browse files Browse the repository at this point in the history
  • Loading branch information
adam committed Mar 18, 2024
1 parent 3fe3e34 commit ee3d775
Show file tree
Hide file tree
Showing 13 changed files with 232 additions and 119 deletions.
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.pack.Remote;
import com.adamcalculator.dynamicpack.util.AFiles;
import com.adamcalculator.dynamicpack.util.Out;
import org.json.JSONObject;
Expand All @@ -9,18 +10,18 @@
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

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 boolean isPacksScanning = false;
private List<Pack> packs = new ArrayList<>();
private File gameDir;
private File resourcePacks;
private boolean minecraftInitialized = false;
Expand All @@ -34,35 +35,41 @@ public void init(File gameDir) {
this.gameDir = gameDir;
this.resourcePacks = new File(gameDir, "resourcepacks");
this.resourcePacks.mkdirs();
Remote.initRemoteTypes();

startSyncThread();
}

public abstract void startSyncThread();

public void rescanPacks() {
if (isPacksScanning) {
Out.warn("rescanPacks already in scanning!");
return;
}
isPacksScanning = true;
packs.clear();

for (File packFile : AFiles.lists(resourcePacks)) {
Out.println("file: " + packFile.getName());
try {
if (packFile.isDirectory()) {
File dynamic = new File(packFile, CLIENT_FILE);
if (AFiles.exists(dynamic)) {
processPack(packFile, new JSONObject(AFiles.read(dynamic)));
}

} else if (packFile.getName().endsWith(".zip")) {
ZipFile zipFile = new ZipFile(packFile);
ZipEntry entry = zipFile.getEntry(CLIENT_FILE);
if (entry != null) {
processPack(packFile, PackUtil.readJson(zipFile.getInputStream(entry)));
PackUtil.openPackFileSystem(packFile, path -> {
Path dynamicPackPath = path.resolve(CLIENT_FILE);
if (Files.exists(dynamicPackPath)) {
Out.println(" + Pack " + packFile.getName() + " supported by mod!");
try {
processPack(packFile, PackUtil.readJson(dynamicPackPath));
} catch (IOException e) {
throw new RuntimeException(e);
}
} else {
Out.println(" - Pack " + packFile.getName() + " not supported by mod.");
}
}
});
} catch (Exception e) {
Out.error("Error while processing pack: " + packFile, e);
}
}
isPacksScanning = false;
}


Expand All @@ -74,7 +81,7 @@ private void processPack(File location, JSONObject json) {
packs.add(pack);

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

Expand All @@ -94,11 +101,17 @@ public File getGameDir() {
return gameDir;
}

public Pack[] getPacks() {
return packs.toArray(new Pack[0]);
}

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

public boolean isMinecraftInitialized() {
return minecraftInitialized;
}

public abstract String getCurrentGameVersion();
}
51 changes: 37 additions & 14 deletions common/src/main/java/com/adamcalculator/dynamicpack/PackUtil.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
package com.adamcalculator.dynamicpack;

import com.adamcalculator.dynamicpack.util.AFiles;
import org.json.JSONObject;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.Writer;
import java.io.*;
import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.nio.file.*;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.function.Consumer;
import java.util.stream.Stream;

public class PackUtil {
Expand All @@ -25,18 +24,41 @@ public static String readString(InputStream inputStream) throws IOException {
return new String(bytes, StandardCharsets.UTF_8);
}

public static JSONObject readJson(Path path) throws IOException {
return new JSONObject(readString(path));
}

public static String readString(Path path) throws IOException {
return Files.readString(path, StandardCharsets.UTF_8);
}


public static void addFileToZip(File zipFile, String name, String text) throws IOException {
Map<String, String> env = new HashMap<>();
env.put("create", "true");
java.nio.file.Path path = zipFile.toPath();
URI uri = URI.create("jar:" + path.toUri());
try (FileSystem fs = FileSystems.newFileSystem(uri, env))
{
Path nf = fs.getPath(name);
try (Writer writer = java.nio.file.Files.newBufferedWriter(nf, StandardCharsets.UTF_8, StandardOpenOption.CREATE)) {
writer.write(text);
openPackFileSystem(zipFile, path -> {
AFiles.nioWriteText(path.resolve(name), text);
});
}

public static void openPackFileSystem(File pack, Consumer<Path> consumer) throws IOException {
if (!pack.exists()) {
throw new FileNotFoundException(pack.getCanonicalPath());
}

if (pack.isDirectory()) {
consumer.accept(pack.toPath());


} else if (pack.isFile() && pack.getName().toLowerCase().endsWith(".zip")) {
Map<String, String> env = new HashMap<>();
env.put("create", "true");

URI uri = URI.create("jar:" + pack.toPath().toUri());
try (FileSystem fs = FileSystems.newFileSystem(uri, env)) {
consumer.accept(fs.getPath(""));
}

} else {
throw new RuntimeException("Failed to open pack file system");
}
}

Expand All @@ -49,7 +71,8 @@ public static void walkScan(Set<String> buffer, Path path) throws IOException {
});
}

public static boolean isPathFileExists(Path path) throws IOException {
// if path exist and isFile
public static boolean isPathFileExists(Path path) {
if (Files.exists(path)) {
return !Files.isDirectory(path);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,17 @@
import com.adamcalculator.dynamicpack.util.Urls;
import org.json.JSONObject;

import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.security.NoSuchAlgorithmException;

public class DynamicRepoRemote extends Remote {
private final Pack parent;
public static final String REPO_JSON = "dynamicmcpack.repo.json";
public static final String REPO_BUILD = "dynamicmcpack.repo.build";
public static final String REPO_SIGNATURE = "dynamicmcpack.repo.json.sig";


private Pack parent;

protected String url;
protected String buildUrl;
Expand All @@ -23,12 +28,15 @@ public class DynamicRepoRemote extends Remote {
public String publicKey;
protected boolean skipSign;

public DynamicRepoRemote(Pack pack, JSONObject remote) {
public DynamicRepoRemote() {
}

public void init(Pack pack, JSONObject remote) {
this.parent = pack;
this.url = remote.getString("url");
this.buildUrl = url + "/dynamicmcpack.repo.build";
this.packUrl = url + "/dynamicmcpack.repo.json";
this.packSigUrl = url + "/dynamicmcpack.repo.json.sig";
this.buildUrl = url + "/" + REPO_BUILD;
this.packUrl = url + "/" + REPO_JSON;
this.packSigUrl = url + "/" + REPO_SIGNATURE;
this.publicKey = remote.optString("public_key", "").replace("\n", "").trim();
this.skipSign = remote.optBoolean("sign_no_required", false);

Expand All @@ -44,28 +52,42 @@ public boolean checkUpdateAvailable() throws IOException {
return parent.getCurrentBuild() != Long.parseLong(content);
}


@Override
public boolean sync(PackSyncProgress progress) throws IOException, NoSuchAlgorithmException {
PackUtil.openPackFileSystem(parent.getLocation(), path -> {
try {
sync0(progress, path);

} catch (IOException | NoSuchAlgorithmException e) {
throw new RuntimeException(e);
}
});
return true;
}

public void sync0(PackSyncProgress progress, Path path) throws IOException, NoSuchAlgorithmException {
String packUrlContent;
progress.downloading("dynamicmcpack.repo.json", 0);
progress.downloading(REPO_JSON, 0);
if (skipSign) {
packUrlContent = Urls.parseContent(packUrl, Mod.MOD_FILES_LIMIT);
Out.warn("Dynamic pack " + parent.getLocation().getName() + " is skipping signing.");
Out.warn("Dynamic pack " + parent.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);
progress.downloading(REPO_JSON, 100);

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


DynamicRepoSyncProcessV1 dynamicRepoSyncProcessV1 = new DynamicRepoSyncProcessV1(parent, this, progress, j);
DynamicRepoSyncProcessV1 dynamicRepoSyncProcessV1 = new DynamicRepoSyncProcessV1(parent, this, progress, repoJson, path);
try {
dynamicRepoSyncProcessV1.run();
dynamicRepoSyncProcessV1.close();
Expand All @@ -74,16 +96,9 @@ public boolean sync(PackSyncProgress progress) throws IOException, NoSuchAlgorit
dynamicRepoSyncProcessV1.close();
throw e;
}
parent.cachedJson.getJSONObject("current").put("build", repoJson.getLong("build"));

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;
AFiles.nioWriteText(path.resolve(DynamicPackModBase.CLIENT_FILE), parent.cachedJson.toString(2));
}

public String getUrl() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,27 @@
package com.adamcalculator.dynamicpack.pack;

import com.adamcalculator.dynamicpack.*;
import com.adamcalculator.dynamicpack.DynamicPackModBase;
import com.adamcalculator.dynamicpack.IDValidator;
import com.adamcalculator.dynamicpack.Mod;
import com.adamcalculator.dynamicpack.PackUtil;
import com.adamcalculator.dynamicpack.sync.PackSyncProgress;
import com.adamcalculator.dynamicpack.sync.state.StateFileDeleted;
import com.adamcalculator.dynamicpack.util.*;
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.IOException;
import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.security.NoSuchAlgorithmException;
import java.util.*;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class DynamicRepoSyncProcessV1 {
private final Pack parent;
Expand All @@ -24,24 +30,14 @@ public class DynamicRepoSyncProcessV1 {
private final JSONObject j;

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

public DynamicRepoSyncProcessV1(Pack pack, DynamicRepoRemote dynamicRepoRemote, PackSyncProgress progress, JSONObject j) throws IOException {
public DynamicRepoSyncProcessV1(Pack pack, DynamicRepoRemote dynamicRepoRemote, PackSyncProgress progress, JSONObject j, Path path) throws IOException {
this.parent = pack;
this.remote = dynamicRepoRemote;
this.progress = progress;
this.j = j;
if (parent.isZip()) {
Map<String, String> env = new HashMap<>();
env.put("create", "true");
URI uri = URI.create("jar:" + parent.getLocation().toPath().toUri());
zipFileSystem = FileSystems.newFileSystem(uri, env);
packRootPath = zipFileSystem.getPath("");

} else {
packRootPath = parent.getLocation().toPath();
}
this.packRootPath = path;
}

public void run() throws IOException, NoSuchAlgorithmException {
Expand All @@ -65,9 +61,6 @@ public void run() throws IOException, NoSuchAlgorithmException {
}

public void close() throws IOException {
if (zipFileSystem != null && zipFileSystem.isOpen()) {
zipFileSystem.close();
}
}

private void processContent(JSONObject object) throws IOException, NoSuchAlgorithmException {
Expand Down
Loading

0 comments on commit ee3d775

Please sign in to comment.