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

Checker for 1.20.1 #22

Merged
merged 6 commits into from
Mar 21, 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.13"
version = "1.0.14"

repositories {
mavenCentral()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public abstract class DynamicPackModBase {
Expand All @@ -21,9 +22,10 @@ public abstract class DynamicPackModBase {

public static DynamicPackModBase INSTANCE;
protected static int manuallySyncThreadCounter = 0;
public boolean rescanPacksBlocked = false;

private boolean isPacksScanning = false;
private List<Pack> packs = new ArrayList<>();
private HashMap<String, Pack> packs = new HashMap<>();
private File gameDir;
private File resourcePacks;
private boolean minecraftInitialized = false;
Expand Down Expand Up @@ -54,9 +56,12 @@ public void rescanPacks() {
Out.warn("rescanPacks already in scanning!");
return;
}
if (rescanPacksBlocked) {
Out.warn("rescanPacks blocked");
return;
}
isPacksScanning = true;
packs.clear();

List<String> forDelete = new ArrayList<>(packs.keySet());
for (File packFile : AFiles.lists(resourcePacks)) {
try {
PackUtil.openPackFileSystem(packFile, path -> {
Expand All @@ -65,7 +70,8 @@ public void rescanPacks() {
Out.println("+ Pack " + packFile.getName() + " supported by mod!");
try {
processPack(packFile, PackUtil.readJson(dynamicPackPath));
} catch (IOException e) {
forDelete.remove(packFile.getName());
} catch (Exception e) {
throw new RuntimeException(e);
}
} else {
Expand All @@ -80,16 +86,24 @@ public void rescanPacks() {
}
}
}
for (String s : forDelete) {
Out.println("Pack " + s + " no longer exists!");
packs.remove(s);
}
isPacksScanning = false;
}



private void processPack(File location, JSONObject json) {
long formatVersion = json.getLong("formatVersion");
Pack oldestPack = packs.getOrDefault(location.getName(), null);
if (formatVersion == 1) {
Pack pack = new Pack(location, json);
packs.add(pack);
if (oldestPack != null) {
pack.saveReScanData(oldestPack);
}
packs.put(location.getName(), pack);

} else {
throw new RuntimeException("Unsupported formatVersion: " + formatVersion);
Expand Down Expand Up @@ -135,7 +149,7 @@ public File getGameDir() {
}

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

public void minecraftInitialized() {
Expand Down
9 changes: 9 additions & 0 deletions common/src/main/java/com/adamcalculator/dynamicpack/Mod.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,19 @@
import java.util.Set;

public class Mod {
public static final String VERSION_NAME_MOD = "1.0.14";
public static final String VERSION_NAME_BRANCH = "mc1.20.1";
public static final String VERSION_NAME = VERSION_NAME_MOD + "-" + VERSION_NAME_BRANCH;
public static final long VERSION_BUILD = 14;


// NOTE: for increase contact to mod developer.
public static final long DYNAMIC_PACK_HTTPS_FILE_SIZE_LIMIT = megabyte(8); // kb -> mb -> 5MB (for files in resourcepack)
public static final long MODRINTH_HTTPS_FILE_SIZE_LIMIT = megabyte(1024); // 1 GB (for .zip files from modrinth)
public static final long MOD_MODTINTH_API_LIMIT = megabyte(8); // 8 MB of api
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 String MODRINTH_URL = "https://modrinth.com/mod/dynamicpack";

private static final Set<String> ALLOWED_HOSTS = new HashSet<>();
static {
Expand Down Expand Up @@ -43,6 +50,8 @@ protected static void addAllowedHosts(String host, Object requester) throws Exce
Out.securityWarning("# ");
Out.securityWarning("# Host: " + host);
Out.securityWarning("# Requester: " + requester);
Out.securityWarning("# StackTrace:");
Out.securityStackTrace();
Out.securityWarning("# ");
Out.securityWarning("===========================");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import java.nio.file.Path;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.LongConsumer;

public class DynamicRepoRemote extends Remote {
Expand Down Expand Up @@ -55,7 +56,6 @@ public void init(Pack pack, JSONObject remote, JSONObject current) {
}
}


if (skipSign != this.publicKey.isBlank()) {
throw new RuntimeException("Incompatible parameters set. Select one of: sign_no_required or public_key");
}
Expand Down Expand Up @@ -107,19 +107,21 @@ public String getCurrentPackContentHash(String id) {


@Override
public boolean sync(PackSyncProgress progress, boolean manually) throws IOException, NoSuchAlgorithmException {
public boolean sync(PackSyncProgress progress, boolean manually) throws IOException {
AtomicBoolean returnValue = new AtomicBoolean(false);
PackUtil.openPackFileSystem(parent.getLocation(), path -> {
try {
sync0(progress, path);
boolean t = sync0(progress, path);
returnValue.set(t);

} catch (IOException | NoSuchAlgorithmException e) {
} catch (Exception e) {
throw new RuntimeException(e);
}
});
return true;
return returnValue.get();
}

public void sync0(PackSyncProgress progress, Path path) throws IOException, NoSuchAlgorithmException {
public boolean sync0(PackSyncProgress progress, Path path) throws IOException, NoSuchAlgorithmException {
String packUrlContent;

LongConsumer parseProgress = new FileDownloadConsumer() {
Expand All @@ -144,6 +146,11 @@ public void onUpdate(FileDownloadConsumer it) {
throw new RuntimeException("Incompatible formatVersion: " + formatVersion);
}

long minBuildForWork;
if ((minBuildForWork = repoJson.optLong("minimal_mod_build", Mod.VERSION_BUILD)) > Mod.VERSION_BUILD) {
throw new RuntimeException("Incompatible DynamicPack Mod version for this pack: required minimal_mod_build=" + minBuildForWork + ", but currently mod build is " + Mod.VERSION_BUILD);
}

String remoteName = repoJson.getString("name");
if (!InputValidator.isPackNameValid(remoteName)) {
throw new RuntimeException("Remote name of pack not valid.");
Expand All @@ -163,6 +170,8 @@ public void onUpdate(FileDownloadConsumer it) {
parent.updateJsonLatestUpdate();

AFiles.nioWriteText(path.resolve(DynamicPackModBase.CLIENT_FILE), parent.getPackJson().toString(2));

return dynamicRepoSyncProcessV1.isReloadRequired();
}

public String getUrl() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public class DynamicRepoSyncProcessV1 {

private final Set<String> oldestFilesList = new HashSet<>();
private final Path packRootPath;
private boolean isReloadRequired = false;

public DynamicRepoSyncProcessV1(Pack pack, DynamicRepoRemote dynamicRepoRemote, PackSyncProgress progress, JSONObject repoJson, Path path) {
this.remote = dynamicRepoRemote;
Expand All @@ -47,6 +48,7 @@ public void run() throws IOException {

progress.textLog("File deleted from resource-pack: " + s);
AFiles.nioSmartDelete(path);
markReloadRequired();
}

try {
Expand Down Expand Up @@ -140,6 +142,7 @@ private void processContentParsed(JSONObject jsonContent) throws IOException {
continue;
}

markReloadRequired();
this.progress.textLog("Overwriting: " + filePath);
Urls.downloadDynamicFile(fileRemoteUrl, filePath, hash, new FileDownloadConsumer() {
@Override
Expand Down Expand Up @@ -199,4 +202,15 @@ private List<JSONObject> calcActiveContents() {
}
return activeContents;
}

public boolean isReloadRequired() {
return isReloadRequired;
}

private void markReloadRequired() {
if (!isReloadRequired) {
Out.debug("Now reload is required in " + this);
}
this.isReloadRequired = true;
}
}
37 changes: 37 additions & 0 deletions common/src/main/java/com/adamcalculator/dynamicpack/pack/Pack.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.adamcalculator.dynamicpack.DynamicPackModBase;
import com.adamcalculator.dynamicpack.PackUtil;
import com.adamcalculator.dynamicpack.status.StatusChecker;
import com.adamcalculator.dynamicpack.sync.PackSyncProgress;
import com.adamcalculator.dynamicpack.util.AFiles;
import com.adamcalculator.dynamicpack.util.Out;
Expand All @@ -28,6 +29,7 @@ public class Pack {
private boolean cachedUpdateAvailable;
private boolean isSyncing = false;
private final String remoteTypeStr;
private Exception latestException;


public Pack(File location, JSONObject json) {
Expand All @@ -50,6 +52,12 @@ public boolean isSyncing() {
return isSyncing;
}

// See StatusChecker for this.
// Developer can block network for specify version in dynamicpack.status.v1.json by security questions
public boolean isNetworkBlocked() {
return StatusChecker.isBlockUpdating(remoteTypeStr);
}

public boolean isZip() {
if (location.isDirectory()) {
return false;
Expand Down Expand Up @@ -84,6 +92,7 @@ public long getLatestUpdated() {
}

public boolean checkIsUpdateAvailable() throws IOException {
checkNetwork();
return cachedUpdateAvailable = remote.checkUpdateAvailable();
}

Expand All @@ -95,9 +104,11 @@ public void sync(PackSyncProgress progress, boolean manually) throws Exception {
try {
sync0(progress, manually);
checkSafePackMinecraftMeta();
setLatestException(null);
} catch (Exception e) {
isSyncing = false;
checkSafePackMinecraftMeta();
setLatestException(e);
throw e;
}
}
Expand All @@ -108,6 +119,9 @@ private void sync0(PackSyncProgress progress, boolean manually) throws Exception
progress.done(false);
return;
}

checkNetwork();

if (!checkIsUpdateAvailable() && !manually) {
progress.textLog("update not available");
progress.done(false);
Expand All @@ -124,6 +138,12 @@ private void sync0(PackSyncProgress progress, boolean manually) throws Exception
progress.done(reloadRequired);
}

private void checkNetwork() {
if (isNetworkBlocked()) {
throw new SecurityException("Network is blocked for remote_type=" + remoteTypeStr + " current version of mod not safe. Update mod!");
}
}

private void checkSafePackMinecraftMeta() throws IOException {
PackUtil.openPackFileSystem(location, path -> {
Path mcmeta = path.resolve(DynamicPackModBase.MINECRAFT_META);
Expand Down Expand Up @@ -154,4 +174,21 @@ private boolean checkMinecraftMetaIsValid(String s) {
public String getRemoteType() {
return remoteTypeStr;
}

public void setLatestException(Exception e) {
Out.debug(this + ": latestExcep="+e);
this.latestException = e;
}

public Exception getLatestException() {
return latestException;
}

public void saveReScanData(Pack oldestPack) {
if (oldestPack == null) return;

if (this.latestException == null) {
this.latestException = oldestPack.latestException;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package com.adamcalculator.dynamicpack.status;

import com.adamcalculator.dynamicpack.Mod;
import com.adamcalculator.dynamicpack.util.Out;
import com.adamcalculator.dynamicpack.util.Urls;
import org.json.JSONObject;

public class StatusChecker {
private static final String URL = "https://adamcalculator.github.io/DynamicPack/dynamicpack.status.v1.json";


private static boolean isUpdateAvailable = false;
private static boolean isFormatActual = true;
private static boolean isSafe = true;
private static boolean isChecked = false;

public static void check() throws Exception {
Out.println("Checking status...");
String s = Urls.parseContent(URL, 1024 * 1024 * 128);
JSONObject j = new JSONObject(s);
JSONObject lat = j.getJSONObject("latest_version");
isUpdateAvailable = lat.getLong("build") > Mod.VERSION_BUILD;
isSafe = lat.getLong("safe") <= Mod.VERSION_BUILD;
isFormatActual = lat.getLong("format") <= Mod.VERSION_BUILD;

isChecked = true;
Out.println(String.format("Status checked! isSafe=%s, isFormatActual=%s, isUpdateAvailable=%s", isSafe, isFormatActual, isUpdateAvailable));
}

public static boolean isBlockUpdating(String remoteType) {
if (remoteType.equals("modrinth")) {
return false;
}
return !isSafe();
}


public static boolean isModUpdateAvailable() {
return isUpdateAvailable;
}

public static boolean isSafe() {
return isSafe;
}

public static boolean isFormatActual() {
return isFormatActual;
}

public static boolean isChecked() {
return isChecked;
}
}
Loading
Loading