Skip to content
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
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,10 +81,19 @@ private void processPack(File location, JSONObject json) {
packs.add(pack);

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

/**
* API FOR MODPACKERS etc all-in-one packs
* @param host host to add.
* @param requester any object. It is recommended that .toString explicitly give out your name.
*/
public static void addAllowedHosts(String host, Object requester) throws Exception {
Mod.addAllowedHosts(host, requester);
}

public boolean isResourcePackActive(Pack pack) throws IOException {
for (String readLine : Files.readAllLines(new File(getGameDir(), "options.txt").toPath(), StandardCharsets.UTF_8)) {
if (readLine.startsWith("resourcePacks:")) {
Expand All @@ -94,11 +110,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();
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
import java.util.regex.Pattern;

public class IDValidator {
private static final Pattern pattern = Pattern.compile("^[a-z0-9A-Z]{0,100}$");
private static final Pattern PATTERN_ID = Pattern.compile("^[a-z0-9]{1,100}$");

public static boolean isValid(String input) {
Matcher matcher = pattern.matcher(input);
Matcher matcher = PATTERN_ID.matcher(input);
return matcher.matches();
}
}
33 changes: 28 additions & 5 deletions common/src/main/java/com/adamcalculator/dynamicpack/Mod.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class Mod {
public static final long GZIP_LIMIT = megabyte(50); // 50 MB of .gz file
public static final long MOD_FILES_LIMIT = megabyte(8);

public static final Set<String> ALLOWED_HOSTS = new HashSet<>();
private static final Set<String> ALLOWED_HOSTS = new HashSet<>();
static {
ALLOWED_HOSTS.add("modrinth.com");
ALLOWED_HOSTS.add("github.com");
Expand All @@ -26,6 +26,29 @@ public class Mod {
}
}

/**
* API FOR MODPACKERS etc all-in-one packs
* @param host host to add.
* @param requester any object. It is recommended that .toString explicitly give out your name.
*/
protected static void addAllowedHosts(String host, Object requester) throws Exception {
if (host == null || requester == null) {
Out.securityWarning("Try to add allowed hosts is failed: null host or requester");
throw new Exception("Try to add allowed hosts is failed: null host or requester");
}

Out.securityWarning("==== SECURITY WARNING ====");
Out.securityWarning("# The DynamicPack mod limits the hosts it can interact with.");
Out.securityWarning("# But a certain requester allowed the mod another host to interact with");
Out.securityWarning("# ");
Out.securityWarning("# Host: " + host);
Out.securityWarning("# Requester: " + requester);
Out.securityWarning("# ");
Out.securityWarning("===========================");

ALLOWED_HOSTS.add(host);
}

public static boolean isUrlHostTrusted(String url) throws IOException {
try {
URI uri = new URI(url);
Expand Down Expand Up @@ -56,22 +79,22 @@ private static long megabyte(long mb) {
}

// TRUE FOR ALL PUBLIC VERSION!!!!!!
// false is equal not safe!1!!!
// false is equal not safe!1!!! RELEASE=true
public static boolean isRelease() {
return true;
}

// localhost allowed
// localhost allowed RELEASE=false
private static boolean isLocalHostAllowed() {
return false;
}

// file_debug_only:// allowed
// file_debug_only:// allowed RELEASE=false
public static boolean isFileDebugSchemeAllowed() {
return false;
}

// http:// allowed
// http:// allowed RELEASE=false
public static boolean isHTTPTrafficAllowed() {
return false;
}
Expand Down
47 changes: 34 additions & 13 deletions common/src/main/java/com/adamcalculator/dynamicpack/PackUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,19 @@
import org.json.JSONObject;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.Writer;
import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.nio.file.*;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
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 +29,34 @@ 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 openPackFileSystem(File pack, Consumer<Path> consumer) throws IOException {
if (!pack.exists()) {
throw new FileNotFoundException(pack.getCanonicalPath());
}

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);
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 +69,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
Loading