From f2f5dfac2689d33f58a556ba801ee423251a97a7 Mon Sep 17 00:00:00 2001 From: BlazeMCworld Date: Tue, 27 Jun 2023 13:29:50 +0200 Subject: [PATCH] /jsscripts update and others - jspm plain response logging - increase gradle memory - remove outdated global.d.ts --- gradle.properties | 2 +- .../java/de/blazemcworld/jsscripts/JSPM.java | 25 ++++++++-- .../blazemcworld/jsscripts/JsScriptsCmd.java | 49 ++++++++++++++++++- .../de/blazemcworld/jsscripts/TypingGen.java | 5 -- 4 files changed, 69 insertions(+), 12 deletions(-) diff --git a/gradle.properties b/gradle.properties index 9d1b6e2..735e60e 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,5 +1,5 @@ # Done to increase the memory available to gradle. -org.gradle.jvmargs=-Xmx2G +org.gradle.jvmargs=-Xmx4G # Fabric Properties # check these on https://modmuss50.me/fabric.html minecraft_version=1.20.1 diff --git a/src/main/java/de/blazemcworld/jsscripts/JSPM.java b/src/main/java/de/blazemcworld/jsscripts/JSPM.java index a231959..69b0dbd 100644 --- a/src/main/java/de/blazemcworld/jsscripts/JSPM.java +++ b/src/main/java/de/blazemcworld/jsscripts/JSPM.java @@ -35,7 +35,9 @@ public static void upload(String name, byte[] zip) throws Exception { .POST(HttpRequest.BodyPublishers.ofByteArray(zip)) .build(); - JsonObject res = JsonParser.parseString(client.send(req, HttpResponse.BodyHandlers.ofString()).body()).getAsJsonObject(); + String strRes = client.send(req, HttpResponse.BodyHandlers.ofString()).body(); + JsScripts.LOGGER.info(strRes); + JsonObject res = JsonParser.parseString(strRes).getAsJsonObject(); if (!res.get("success").getAsBoolean()) { throw new Exception(res.get("error").getAsString()); } @@ -52,7 +54,10 @@ private static String getToken() throws Exception { .uri(URI.create(HOST + "/auth/getnonce/" + JsScripts.MC.getSession().getProfile().getId().toString())) .build(); - JsonObject nonceRes = JsonParser.parseString(client.send(req, HttpResponse.BodyHandlers.ofString()).body()).getAsJsonObject(); + + String nonceStrRes = client.send(req, HttpResponse.BodyHandlers.ofString()).body(); + JsScripts.LOGGER.info(nonceStrRes); + JsonObject nonceRes = JsonParser.parseString(nonceStrRes).getAsJsonObject(); if (!nonceRes.get("success").getAsBoolean()) { throw new Exception(nonceRes.get("error").getAsString()); } @@ -90,12 +95,14 @@ private static String getToken() throws Exception { """.formatted(clientNonce))) .build(); - JsonObject tokenRes = JsonParser.parseString(client.send(req, HttpResponse.BodyHandlers.ofString()).body()).getAsJsonObject(); + String tokenStrRes = client.send(req, HttpResponse.BodyHandlers.ofString()).body(); + JsScripts.LOGGER.info(tokenStrRes); + JsonObject tokenRes = JsonParser.parseString(tokenStrRes).getAsJsonObject(); if (!tokenRes.get("success").getAsBoolean()) { throw new Exception(nonceRes.get("error").getAsString()); } token = tokenRes.get("token").getAsString(); - expires = System.currentTimeMillis() + 50 * 60 * 1000; + expires = tokenRes.get("expireIn").getAsLong(); return token; } @@ -140,4 +147,14 @@ public static boolean has(String name) throws Exception { return client.send(req, HttpResponse.BodyHandlers.discarding()).statusCode() != 404; } + + public static String getVersion(String name) throws Exception { + HttpClient client = HttpClient.newHttpClient(); + + HttpRequest req = HttpRequest.newBuilder() + .uri(URI.create("https://raw.githubusercontent.com/McJsScripts/JSPMRegistry/master/packages/" + name + "/jspm.json")) + .build(); + + return JsonParser.parseString(client.send(req, HttpResponse.BodyHandlers.ofString()).body()).getAsJsonObject().getAsJsonObject("version").get("pkg").getAsString(); + } } diff --git a/src/main/java/de/blazemcworld/jsscripts/JsScriptsCmd.java b/src/main/java/de/blazemcworld/jsscripts/JsScriptsCmd.java index 9d5ee90..7f072e4 100644 --- a/src/main/java/de/blazemcworld/jsscripts/JsScriptsCmd.java +++ b/src/main/java/de/blazemcworld/jsscripts/JsScriptsCmd.java @@ -2,6 +2,7 @@ import com.google.gson.*; import com.mojang.brigadier.arguments.StringArgumentType; +import com.sun.jna.platform.FileUtils; import net.fabricmc.fabric.api.client.command.v2.ClientCommandRegistrationCallback; import net.minecraft.MinecraftVersion; import net.minecraft.text.ClickEvent; @@ -35,6 +36,7 @@ public void register() { JsScripts.displayChat(Text.literal("/jsscripts disable - Remove a script from the auto-enable list.").formatted(Formatting.AQUA)); JsScripts.displayChat(Text.literal("/jsscripts upload - Upload a script to jspm.").formatted(Formatting.AQUA)); JsScripts.displayChat(Text.literal("/jsscripts download - Download a script from jspm.").formatted(Formatting.AQUA)); + JsScripts.displayChat(Text.literal("/jsscripts update - Check for script updates from jspm.").formatted(Formatting.AQUA)); return 1; }) .then(literal("reload") @@ -219,6 +221,7 @@ public void register() { MinecraftVersion.CURRENT.getName() )); JsScripts.displayChat(Text.literal("Please update the newly created jspm.json file in the script if necessary, then retry.").formatted(Formatting.AQUA)); + JsScripts.displayChat(Text.literal("It is also recommended to have a README.md and LICENSE file, as the code will be made public.").formatted(Formatting.AQUA)); return; } @@ -272,8 +275,14 @@ public void register() { } if (root.toFile().exists()) { - JsScripts.displayChat(Text.literal("Script already found locally! Delete it to re-download.").formatted(Formatting.RED)); - return; + FileUtils fu = FileUtils.getInstance(); + if (fu.hasTrash()) { + fu.moveToTrash(root.toFile()); + JsScripts.displayChat(Text.literal("Moved previous script version to the trash.").formatted(Formatting.AQUA)); + } else { + JsScripts.displayChat(Text.literal("Script already found locally! Delete it to re-download.").formatted(Formatting.RED)); + return; + } } JsScripts.displayChat(Text.literal("Downloading script from JSPM...").formatted(Formatting.AQUA)); @@ -290,6 +299,42 @@ public void register() { }) ) ) + .then(literal("update") + .executes((e) -> { + new Thread(() -> { + int updates = 0; + for (File f : ScriptManager.modDir.resolve("scripts").toFile().listFiles()) { + try { + Path p = f.toPath().resolve("jspm.json"); + if (!Files.exists(p)) { + JsScripts.displayChat(Text.literal(f.getName() + ": No jspm.json").formatted(Formatting.AQUA)); + continue; + } + String ver = JsonParser.parseString(Files.readString(p)).getAsJsonObject().getAsJsonObject("version").get("pkg").getAsString(); + + try { + String remoteVer = JSPM.getVersion(f.getName()); + if (ver.equals(remoteVer)) { + JsScripts.displayChat(Text.literal(f.getName() + ": Up to date. (" + ver + ")").formatted(Formatting.AQUA)); + } else { + JsScripts.displayChat(Text.literal(f.getName() + ": Update available. (" + ver + " => " + remoteVer + ")").formatted(Formatting.GREEN) + .styled(s -> s.withClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/jsscripts download " + f.getName())))); + updates++; + } + } catch (Exception unknown) { + JsScripts.displayChat(Text.literal(f.getName() + ": No remote found.").formatted(Formatting.RED)); + } + } catch (Exception err) { + JsScripts.displayChat(Text.literal(f.getName() + ": Unknown Error").formatted(Formatting.RED)); + } + } + if (updates > 0) { + JsScripts.displayChat(Text.literal(updates + " updates found. Click them to update.").formatted(Formatting.GREEN)); + } + }).start(); + return 1; + }) + ) )); } } diff --git a/src/main/java/de/blazemcworld/jsscripts/TypingGen.java b/src/main/java/de/blazemcworld/jsscripts/TypingGen.java index dd0301b..9e6f292 100644 --- a/src/main/java/de/blazemcworld/jsscripts/TypingGen.java +++ b/src/main/java/de/blazemcworld/jsscripts/TypingGen.java @@ -42,7 +42,6 @@ public static void genTypesIn(String targets) { Files.writeString(out.resolve("jsconfig.json"), """ { "compilerOptions": { - "typeRoots": ["types/global.d.ts"], "paths": { "$*": ["./types/*"], "#*": ["./scripts/*"] @@ -57,10 +56,6 @@ public static void genTypesIn(String targets) { out.toFile().mkdirs(); } - Files.writeString(out.resolve("global.d.ts"), """ - declare const script: import("../types/de/blazemcworld/jsscripts/Script").default; - """); - JsScripts.displayChat(Text.literal("Scanning available classes...").formatted(Formatting.AQUA)); ClassLoader cl = JsScripts.class.getClassLoader();