aboutsummaryrefslogtreecommitdiff
path: root/loader/src/main/java/kr/syeyoung/dungeonsguide/launcher/branch
diff options
context:
space:
mode:
authorsyeyoung <cyoung06@naver.com>2022-11-16 17:45:55 +0900
committersyeyoung <cyoung06@naver.com>2022-11-16 17:59:56 +0900
commit60032db80aefe4a9f58b354b5a26938ed76f5c46 (patch)
tree17a7b6e873db2ea3c40aef508134842ce69c5991 /loader/src/main/java/kr/syeyoung/dungeonsguide/launcher/branch
parent3708965c0c22c216336f8aa28158ba22bfc03b60 (diff)
parent241893934ef119566693165589fce0921c35e4af (diff)
downloadSkyblock-Dungeons-Guide-60032db80aefe4a9f58b354b5a26938ed76f5c46.tar.gz
Skyblock-Dungeons-Guide-60032db80aefe4a9f58b354b5a26938ed76f5c46.tar.bz2
Skyblock-Dungeons-Guide-60032db80aefe4a9f58b354b5a26938ed76f5c46.zip
- Update Downloading and Signature Verification
Signed-off-by: syeyoung <cyoung06@naver.com>
Diffstat (limited to 'loader/src/main/java/kr/syeyoung/dungeonsguide/launcher/branch')
-rw-r--r--loader/src/main/java/kr/syeyoung/dungeonsguide/launcher/branch/UpdateRetrieverUtil.java182
1 files changed, 182 insertions, 0 deletions
diff --git a/loader/src/main/java/kr/syeyoung/dungeonsguide/launcher/branch/UpdateRetrieverUtil.java b/loader/src/main/java/kr/syeyoung/dungeonsguide/launcher/branch/UpdateRetrieverUtil.java
new file mode 100644
index 00000000..fe7e54dd
--- /dev/null
+++ b/loader/src/main/java/kr/syeyoung/dungeonsguide/launcher/branch/UpdateRetrieverUtil.java
@@ -0,0 +1,182 @@
+package kr.syeyoung.dungeonsguide.launcher.branch;
+
+import kr.syeyoung.dungeonsguide.launcher.Main;
+import kr.syeyoung.dungeonsguide.launcher.auth.DGResponse;
+import kr.syeyoung.dungeonsguide.launcher.exceptions.AssetNotFoundException;
+import kr.syeyoung.dungeonsguide.launcher.exceptions.NoVersionFoundException;
+import kr.syeyoung.dungeonsguide.launcher.exceptions.ResponseParsingException;
+import lombok.Builder;
+import lombok.Data;
+import org.apache.commons.io.IOUtils;
+import org.json.JSONArray;
+import org.json.JSONObject;
+
+import javax.net.ssl.HttpsURLConnection;
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.MalformedURLException;
+import java.net.ProtocolException;
+import java.net.URL;
+import java.time.Instant;
+import java.util.List;
+import java.util.Optional;
+import java.util.UUID;
+import java.util.stream.Collectors;
+
+public class UpdateRetrieverUtil {
+ private static String getResponse(HttpsURLConnection connection) throws IOException {
+ connection.getResponseCode();
+ InputStream toRead = connection.getErrorStream();
+ if (toRead == null)
+ toRead = connection.getInputStream();
+ String payload = IOUtils.readLines(toRead).stream().collect(Collectors.joining("\n"));
+ return payload;
+ }
+
+ public static List<UpdateBranch> getUpdateBranches() throws IOException {
+ HttpsURLConnection connection = (HttpsURLConnection) new URL(Main.DOMAIN + "/updates/").openConnection();
+ connection.setRequestProperty("User-Agent", "DungeonsGuide/1.0");
+ connection.setRequestProperty("Content-Type", "application/json");
+ connection.setRequestMethod("GET");
+ connection.setDoInput(true);
+ connection.setDoOutput(true);
+
+ JSONArray jsonArray = new JSONArray(getResponse(connection));
+ return jsonArray.toList()
+ .stream()
+ .map(a -> (JSONObject)a)
+ .map(a -> {
+ UpdateBranch updateBranch = new UpdateBranch();
+ updateBranch.setId(a.getLong("id"));
+ updateBranch.setName(a.getString("name"));
+ updateBranch.setMetadata(a.getJSONObject("metadata"));
+ return updateBranch;
+ }).collect(Collectors.toList());
+ }
+
+ public static List<Update> getLatestUpdates(long branchId, int page) throws IOException {
+ HttpsURLConnection connection = (HttpsURLConnection) new URL(Main.DOMAIN + "/updates/"+branchId+"/?page="+page).openConnection();
+ connection.setRequestProperty("User-Agent", "DungeonsGuide/1.0");
+ connection.setRequestMethod("GET");
+ connection.setDoInput(true);
+ connection.setDoOutput(true);
+
+ JSONArray jsonArray = new JSONArray(getResponse(connection));
+ return jsonArray.toList()
+ .stream()
+ .map(a -> (JSONObject)a)
+ .map(a -> {
+ Update update = new Update();
+ update.setId(a.getLong("id"));
+ update.setBranchId(a.getLong("branchId"));
+ update.setName(a.getString("name"));
+ update.setUpdateLog(a.getString("updateLog"));
+ update.setMetadata(a.getJSONObject("metadata"));
+ update.setAssets(a.getJSONObject("assets").getJSONArray("assets").toList().stream().map(b -> (JSONObject)b)
+ .map(b -> {
+ Update.Asset asset = new Update.Asset();
+ asset.setName(b.getString("name"));
+ asset.setAssetId(UUID.fromString(b.getString("assetId")));
+ asset.setSize(b.getLong("size"));
+ asset.setObjectId(b.getString("objectId"));
+ return asset;
+ }).collect(Collectors.toList()));
+ update.setReleaseDate(Instant.parse(a.getString("releaseDate")));
+ return update;
+ }).collect(Collectors.toList());
+ }
+
+ public static Update getUpdate(long branchId, long updateId) throws IOException {
+ HttpsURLConnection connection = (HttpsURLConnection) new URL(Main.DOMAIN + "/updates/"+branchId+"/"+updateId).openConnection();
+ connection.setRequestProperty("User-Agent", "DungeonsGuide/1.0");
+ connection.setRequestMethod("GET");
+ connection.setDoInput(true);
+ connection.setDoOutput(true);
+
+ JSONObject a = new JSONObject(getResponse(connection));
+
+ Update update = new Update();
+ update.setId(a.getLong("id"));
+ update.setBranchId(a.getLong("branchId"));
+ update.setName(a.getString("name"));
+ update.setUpdateLog(a.getString("updateLog"));
+ update.setMetadata(a.getJSONObject("metadata"));
+ update.setAssets(a.getJSONObject("assets").getJSONArray("assets").toList().stream().map(b -> (JSONObject)b)
+ .map(b -> {
+ Update.Asset asset = new Update.Asset();
+ asset.setName(b.getString("name"));
+ asset.setAssetId(UUID.fromString(b.getString("assetId")));
+ asset.setSize(b.getLong("size"));
+ asset.setObjectId(b.getString("objectId"));
+ return asset;
+ }).collect(Collectors.toList()));
+ update.setReleaseDate(Instant.parse(a.getString("releaseDate")));
+ return update;
+ }
+
+ public static InputStream downloadFile(Update update, String assetName) throws IOException {
+ Update.Asset asset = update.getAssets().stream().filter(a -> a.getName().equals(assetName))
+ .findFirst().orElseThrow(() -> new AssetNotFoundException(update.getBranchId()+"", update.getId()+"("+update.getName()+")", assetName));
+
+
+ HttpsURLConnection connection = (HttpsURLConnection) new URL(Main.DOMAIN + "/updates/"+update.getBranchId()+"/"+update.getId()+"/"+asset.getAssetId()).openConnection();
+ connection.setRequestProperty("User-Agent", "DungeonsGuide/1.0");
+ connection.setRequestMethod("GET");
+ connection.setDoInput(true);
+ connection.setDoOutput(true);
+
+ JSONObject result = new JSONObject(getResponse(connection));
+ String url = result.getString("url");
+ String method = result.getString("method");
+
+ connection = (HttpsURLConnection) new URL(url).openConnection();
+ connection.setRequestProperty("User-Agent", "DungeonsGuide/1.0");
+ connection.setRequestMethod(method);
+ return connection.getInputStream();
+ }
+
+ @Data @Builder
+ public static class VersionInfo {
+ String friendlyBranchName = "";
+ long branchId;
+ String friendlyVersionName = "";
+ long updateId;
+ }
+ public static VersionInfo getIds(String branch, String version) throws IOException {
+ long branchId = -1, updateId = -1;
+ UpdateBranch branch1 = null;
+ for (UpdateBranch updateBranch : UpdateRetrieverUtil.getUpdateBranches()) {
+ if (updateBranch.getName().equals(branch) || (branch.equals("$default") &&
+ Optional.ofNullable(updateBranch.getMetadata())
+ .map(a -> a.getJSONObject("additionalMeta"))
+ .map(a -> a.getBoolean("defaultMod")).orElse(false))) {
+ branchId = updateBranch.getId();
+ branch1 = updateBranch;
+ break;
+ }
+ }
+ if (branchId == -1) return null;
+
+ Update target = null;
+ int page = 0;
+ while (updateId == -1) {
+ List<Update> updateList = UpdateRetrieverUtil.getLatestUpdates(branchId, page++);
+ if (updateList == null || updateList.isEmpty()) return null;
+ for (Update update : updateList) {
+ if (update.getName().equals(version) || version.equals("latest")) { // if latest, get the first one.
+ updateId = update.getId();
+ target = update;
+ break;
+ }
+ }
+ }
+
+
+ return VersionInfo.builder()
+ .branchId(branchId)
+ .updateId(updateId)
+ .friendlyBranchName(branch1.getName())
+ .friendlyVersionName(target.getName())
+ .build();
+ }
+}