Skip to content

Commit

Permalink
optimizing
Browse files Browse the repository at this point in the history
  • Loading branch information
adam committed Mar 20, 2024
1 parent 24a54c6 commit 54c91fe
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.adamcalculator.dynamicpack.util.FileDownloadConsumer;
import com.adamcalculator.dynamicpack.util.Out;
import com.adamcalculator.dynamicpack.util.Urls;
import org.json.JSONArray;
import org.json.JSONObject;

import java.io.IOException;
Expand All @@ -24,7 +25,7 @@ public class DynamicRepoRemote extends Remote {


private Pack parent;

private JSONObject cachedCurrentJson;
protected String url;
protected String buildUrl;
protected String packUrl;
Expand All @@ -37,8 +38,9 @@ public class DynamicRepoRemote extends Remote {
public DynamicRepoRemote() {
}

public void init(Pack pack, JSONObject remote) {
public void init(Pack pack, JSONObject remote, JSONObject current) {
this.parent = pack;
this.cachedCurrentJson = current;
this.url = remote.getString("url");
this.buildUrl = url + "/" + REPO_BUILD;
this.packUrl = url + "/" + REPO_JSON;
Expand All @@ -62,7 +64,45 @@ public void init(Pack pack, JSONObject remote) {
@Override
public boolean checkUpdateAvailable() throws IOException {
String content = Urls.parseContent(buildUrl, 64).trim();
return parent.getCurrentBuild() != Long.parseLong(content);
return getCurrentBuild() != Long.parseLong(content);
}

public long getCurrentBuild() {
return cachedCurrentJson.optLong("build", -1);
}


// currently not using. but in feature this may be used in settings screen to Enable/disable contents
public void updateCurrentKnownContents(JSONArray repoContents) {
if (cachedCurrentJson.has("known_contents")) {
cachedCurrentJson.remove("known_contents");
}
JSONObject newKnown = new JSONObject();
cachedCurrentJson.put("known_contents", newKnown);
for (Object _repoContent : repoContents) {
JSONObject repoContent = (JSONObject) _repoContent;
String id = repoContent.getString("id");
boolean required = repoContent.optBoolean("required", false);
JSONObject jsonObject = new JSONObject()
.put("hash", repoContent.getString("hash"));
if (required) {
jsonObject.put("required", true);
}
newKnown.put(id, jsonObject);
}
}

public String getCurrentPackContentHash(String id) {
if (cachedCurrentJson.has("known_contents")) {
try {
return cachedCurrentJson.getJSONObject("known_contents").getJSONObject(id).getString("hash");

} catch (Exception e) {
// if hash not found
return null;
}
}
return null;
}


Expand Down Expand Up @@ -120,6 +160,7 @@ public void onUpdate(FileDownloadConsumer it) {
throw e;
}
parent.getPackJson().getJSONObject("current").put("build", repoJson.getLong("build"));
parent.updateJsonLatestUpdate();

AFiles.nioWriteText(path.resolve(DynamicPackModBase.CLIENT_FILE), parent.getPackJson().toString(2));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,15 @@
import com.adamcalculator.dynamicpack.PackUtil;
import com.adamcalculator.dynamicpack.sync.PackSyncProgress;
import com.adamcalculator.dynamicpack.sync.state.StateFileDeleted;
import com.adamcalculator.dynamicpack.util.AFiles;
import com.adamcalculator.dynamicpack.util.FileDownloadConsumer;
import com.adamcalculator.dynamicpack.util.Hashes;
import com.adamcalculator.dynamicpack.util.Urls;
import com.adamcalculator.dynamicpack.util.*;
import org.json.JSONArray;
import org.json.JSONObject;

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.HashSet;
import java.util.List;
import java.util.Set;
import java.util.*;

public class DynamicRepoSyncProcessV1 {
private final DynamicRepoRemote remote;
Expand Down Expand Up @@ -54,6 +48,12 @@ public void run() throws IOException {
progress.textLog("File deleted from resource-pack: " + s);
AFiles.nioSmartDelete(path);
}

try {
remote.updateCurrentKnownContents(repoJson.getJSONArray("contents"));
} catch (Exception e) {
Out.error("Error while update known_packs. Not fatal", e);
}
}

public void close() throws IOException {
Expand All @@ -66,6 +66,14 @@ private void processContent(JSONObject jsonContent) throws IOException {
}

String contentRemoteHash = jsonContent.getString("hash");
String localCache = remote.getCurrentPackContentHash(id);
if (Objects.equals(localCache, contentRemoteHash)) {
progress.textLog("Skipping content " + id + " because local hash is equal with remote...");
return;
} else {
progress.textLog("Content " + id + " local hash different with remote or null. Updating...");
}

String url = jsonContent.getString("url");
String urlCompressed = jsonContent.optString("url_compressed", null);
boolean compressSupported = urlCompressed != null;
Expand Down Expand Up @@ -180,12 +188,16 @@ private List<JSONObject> calcActiveContents() {
while (i < contents.length()) {
JSONObject content = contents.getJSONObject(i);
String id = content.getString("id");

if (!InputValidator.isContentIdValid(id)) {
throw new RuntimeException("Id of content is not valid.");
}

if (remote.isContentActive(id) || content.optBoolean("required", false)) {
activeContents.add(content);
}
i++;
}

return activeContents;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

public class ModrinthRemote extends Remote {
private Pack parent;
private JSONObject cachedCurrentJson;
private boolean usesCurrentGameVersion;

private String projectId;
private String gameVersion;
Expand All @@ -21,21 +23,40 @@ public class ModrinthRemote extends Remote {
public ModrinthRemote() {
}

public void init(Pack parent, JSONObject json) {
public void init(Pack parent, JSONObject json, JSONObject current) {
this.parent = parent;
this.cachedCurrentJson = current;
this.projectId = json.getString("modrinth_project_id");
var ver = json.optString("game_version", "current");
this.gameVersion = ver.equalsIgnoreCase("current") ? getCurrentGameVersion() : ver;
this.usesCurrentGameVersion = ver.equalsIgnoreCase("current");
this.gameVersion = usesCurrentGameVersion ? getCurrentGameVersion() : ver;
}

private String getCurrentGameVersion() {
return DynamicPackModBase.INSTANCE.getCurrentGameVersion();
}

public String getCurrentUnique() {
return cachedCurrentJson.optString("version", "");
}


public String getCurrentVersionNumber() {
return cachedCurrentJson.optString("version_number", "");
}

public String getVersionsUrl() {
return "https://api.modrinth.com/v2/project/" + projectId + "/version";
}

public String getProjectId() {
return projectId;
}

public boolean isUsesCurrentGameVersion() {
return usesCurrentGameVersion;
}

public JSONObject parseLatestVersionJson() throws IOException {
String content = Urls.parseContent(getVersionsUrl(), Mod.MOD_MODTINTH_API_LIMIT);
JSONArray j = new JSONArray(content);
Expand Down Expand Up @@ -63,10 +84,10 @@ public boolean checkUpdateAvailable() throws IOException {
Out.warn("Latest version of " + parent.getLocation().getName() + " not available for this game_version");
return false;
}
if (latest.optString("version_number", "").equals(parent.getCurrentVersionNumber())) {
if (latest.optString("version_number", "").equals(getCurrentVersionNumber())) {
return false;
}
return !parent.getCurrentUnique().equals(latest.getString("id"));
return !getCurrentUnique().equals(latest.getString("id"));
}

@Override
Expand Down Expand Up @@ -98,6 +119,7 @@ public void onUpdate(FileDownloadConsumer it) {

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


PackUtil.openPackFileSystem(tempFile, path -> AFiles.nioWriteText(path.resolve(DynamicPackModBase.CLIENT_FILE), parent.getPackJson().toString(2)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public Pack(File location, JSONObject json) {
JSONObject remote = json.getJSONObject("remote");
String remoteType = remote.getString("type");
this.remote = Remote.REMOTES.get(remoteType).get();
this.remote.init(this, remote);
this.remote.init(this, remote, cachedJson.getJSONObject("current"));

} catch (Exception e) {
throw new RuntimeException("Failed to parse remote", e);
Expand All @@ -63,17 +63,18 @@ public JSONObject getPackJson() {
return cachedJson;
}

public long getCurrentBuild() {
return cachedJson.getJSONObject("current").optLong("build", -1);
}

public String getCurrentUnique() {
return cachedJson.getJSONObject("current").optString("version", "");
public void updateJsonLatestUpdate() {
cachedJson.getJSONObject("current").put("latest_updated", System.currentTimeMillis() / 1000);
}

public long getLatestUpdated() {
try {
return cachedJson.getJSONObject("current").getLong("latest_updated");

public String getCurrentVersionNumber() {
return cachedJson.getJSONObject("current").optString("version_number", "");
} catch (Exception e) {
return -1;
}
}

public boolean checkIsUpdateAvailable() throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public static void initRemoteTypes() {
REMOTES.put("dynamic_repo", DynamicRepoRemote::new);
}

public abstract void init(Pack pack, JSONObject remote);
public abstract void init(Pack pack, JSONObject remote, JSONObject current);

public abstract boolean checkUpdateAvailable() throws IOException;

Expand Down

0 comments on commit 54c91fe

Please sign in to comment.