Skip to content

Commit

Permalink
idk
Browse files Browse the repository at this point in the history
  • Loading branch information
cubewhy committed Jan 7, 2024
1 parent ce8cc24 commit 1f19c66
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 37 deletions.
6 changes: 0 additions & 6 deletions src/main/java/org/cubewhy/celestial/entities/Assets.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,6 @@

import java.util.Date;

/**
* Auto-generated: 2024-01-07 19:7:0
*
* @author json.cn (i@json.cn)
* @website http://www.json.cn/java2pojo/
*/
@Data
public class Assets {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* @website http://www.json.cn/java2pojo/
*/
@Data
public class releaseEntity {
public class ReleaseEntity {

private String url;
private String assets_url;
Expand Down
54 changes: 24 additions & 30 deletions src/main/java/org/cubewhy/celestial/game/addon/WeaveMod.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,26 @@
import lombok.Getter;
import okhttp3.Response;
import org.cubewhy.celestial.entities.Assets;
import org.cubewhy.celestial.entities.releaseEntity;
import org.cubewhy.celestial.entities.ReleaseEntity;
import org.cubewhy.celestial.files.DownloadManager;
import org.cubewhy.celestial.files.Downloadable;
import org.cubewhy.celestial.game.BaseAddon;
import org.cubewhy.celestial.utils.RequestUtils;
import org.cubewhy.celestial.utils.TextUtils;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import static org.cubewhy.celestial.Celestial.config;

@Getter
public class WeaveMod extends BaseAddon {
public static final File modFolder = new File(System.getProperty("user.home"), ".weave/mods");
Expand Down Expand Up @@ -59,38 +66,25 @@ public String toString() {
return this.file.getName();
}

public boolean downloadWeaveLoader(@Nullable String loaderUrl){
if(loaderUrl == null){
String api_json;
try(Response response = RequestUtils.get("https://api.github.com/repos/Weave-MC/Weave-Loader/releases/latest").execute()){
assert response.body() != null;
api_json = response.body().string();
} catch (IOException e) {
return false;
}
releaseEntity releaseEntity = JsonToObj(api_json, org.cubewhy.celestial.entities.releaseEntity.class);
if (releaseEntity != null) {
Assets[] assets = releaseEntity.getAssets().toArray(new Assets[0]);
for(Assets i:assets){
if(i.getName().endsWith(".jar")){
//TODO: download i.browser_download_url() to ~/.cubewhy/addon/Weave-<%s>.jar, releaseEntity.getName()
}
public boolean downloadWeaveLoader() throws MalformedURLException {
String api_json;
try (Response response = RequestUtils.get("https://api.github.com/repos/Weave-MC/Weave-Loader/releases/latest").execute()) {
assert response.body() != null;
api_json = response.body().string();
} catch (IOException e) {
return false;
}
ReleaseEntity releaseEntity = TextUtils.jsonToObj(api_json, ReleaseEntity.class);
if (releaseEntity != null) {
Assets[] assetsArray = releaseEntity.getAssets().toArray(new Assets[0]);
for (Assets assets : assetsArray) {
if (assets.getName().endsWith(".jar")) {
//TODO: download assets.browser_download_url() to ~/.cubewhy/addon/Weave-<%s>.jar, releaseEntity.getName()

DownloadManager.download(new Downloadable(new URL(assets.getBrowser_download_url()), new File(config.getValue("addon").getAsJsonObject().getAsJsonObject("weave").get("installation").getAsString()), ""));
}
}
}else{
//TODO: loaderUrl to ~/.cubewhy/addon/Weave-<%s>.jar, releaseEntity.getName()
}
return true;
}

public static <T> T JsonToObj(String json, Class<T> clz){
Gson gson = new Gson();
if(Objects.isNull(json)) return null;
T obj = gson.fromJson(json, clz);
if(Objects.isNull(obj)){
return null;
}else{
return obj;
}
}
}
13 changes: 13 additions & 0 deletions src/main/java/org/cubewhy/celestial/utils/TextUtils.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package org.cubewhy.celestial.utils;

import com.google.gson.Gson;
import org.jetbrains.annotations.NotNull;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Objects;

public final class TextUtils {
private TextUtils() {
Expand All @@ -18,4 +20,15 @@ public static String dumpTrace(@NotNull Exception e) {
e.printStackTrace(stream);
return s.toString();
}

public static <T> T jsonToObj(String json, Class<T> clz) {
Gson gson = new Gson();
if (Objects.isNull(json)) return null;
T obj = gson.fromJson(json, clz);
if (Objects.isNull(obj)) {
return null;
} else {
return obj;
}
}
}

0 comments on commit 1f19c66

Please sign in to comment.