Skip to content

Commit

Permalink
file download event
Browse files Browse the repository at this point in the history
  • Loading branch information
cubewhy committed Jan 7, 2024
1 parent c28b312 commit df3f09b
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 1 deletion.
14 changes: 14 additions & 0 deletions src/main/java/org/cubewhy/celestial/Celestial.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.formdev.flatlaf.FlatDarkLaf;
import com.formdev.flatlaf.FlatLightLaf;
import com.formdev.flatlaf.IntelliJTheme;
import com.formdev.flatlaf.json.Json;
import com.google.gson.*;
import joptsimple.OptionParser;
import joptsimple.OptionSet;
Expand Down Expand Up @@ -221,11 +222,24 @@ private static void initConfig() {
resize.addProperty("width", 854);
resize.addProperty("height", 480);

JsonObject addon = new JsonObject();
// weave
JsonObject weave = new JsonObject();
weave.addProperty("enable", false);
weave.addProperty("check-update", true);
addon.add("weave", weave);
// lccn
JsonObject lunarcn = new JsonObject();
lunarcn.addProperty("enable", false);
lunarcn.addProperty("check-update", true);
addon.add("lunarcn", lunarcn);

config.initValue("jre", "") // leave empty if you want to use the default one
.initValue("language", "zh") // en, zh
.initValue("installation-dir", new File(configDir, "game").getPath())
.initValue("game-dir", getMinecraftFolder().getPath()) // the minecraft folder
.initValue("game", (JsonElement) null)
.initValue("addon", addon)
.initValue("ram", 4096)
.initValue("max-threads", Runtime.getRuntime().availableProcessors()) // recommend: same as your CPU core
.initValue("api", "https://api.lunarclient.top") // only support the LunarCN api, Moonsworth's looks like shit :(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Celestial Launcher <me@lunarclient.top>
* License under GPLv3
* Do NOT remove this note if you want to copy this file.
*/

package org.cubewhy.celestial.event.impl;

import org.cubewhy.celestial.event.Event;

import java.io.File;

public class FileDownloadEvent extends Event {

public final File file;
public final Type type;

public enum Type {
START,
SUCCESS, FALURE;
}

public FileDownloadEvent(File file, Type type) {
this.file = file;
this.type = type;
}
}
10 changes: 9 additions & 1 deletion src/main/java/org/cubewhy/celestial/files/DownloadManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import lombok.extern.slf4j.Slf4j;
import okhttp3.Response;
import org.apache.commons.io.FileUtils;
import org.cubewhy.celestial.event.impl.FileDownloadEvent;
import org.cubewhy.celestial.utils.RequestUtils;
import org.jetbrains.annotations.NotNull;

Expand Down Expand Up @@ -77,18 +78,25 @@ public static boolean download(URL url, @NotNull File file, String sha1) throws
return true;
}
}
new FileDownloadEvent(file, FileDownloadEvent.Type.START).call();
log.info("Downloading " + url + " to " + file);
try (Response response = RequestUtils.get(url).execute()) {
if (!response.isSuccessful() || response.body() == null) {
new FileDownloadEvent(file, FileDownloadEvent.Type.FALURE).call();
return false;
}
byte[] bytes = response.body().bytes();
FileUtils.writeByteArrayToFile(file, bytes);
}
statusBar.setText("Download " + file.getName() + " success.");
if (sha1 != null) {
return SecureUtil.sha1(file).equals(sha1);
boolean result = SecureUtil.sha1(file).equals(sha1);
if (!result) {
new FileDownloadEvent(file, FileDownloadEvent.Type.FALURE).call();
}
return result;
}
new FileDownloadEvent(file, FileDownloadEvent.Type.SUCCESS).call();
return true;
}

Expand Down
3 changes: 3 additions & 0 deletions src/main/java/org/cubewhy/celestial/game/addon/WeaveMod.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,11 @@
import java.util.List;
import java.util.Objects;

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

@Getter
public class WeaveMod extends BaseAddon {
public static final String build = "Weave-MC/Weave-Loader"; // https://github.com/<build>/releases/latest
public static final File modFolder = new File(System.getProperty("user.home"), ".weave/mods");
private final File file;

Expand Down

0 comments on commit df3f09b

Please sign in to comment.