From 8b9e45a503704754a2f640fecf83e49cec13ca64 Mon Sep 17 00:00:00 2001 From: poqdavid Date: Fri, 24 Nov 2023 18:01:42 -0500 Subject: [PATCH 1/3] Update versionCheck.js Updated versionCheck.js to work with JsMacros 1.8.5+ --- JavaScript/versionCheck.js | 132 +++++++++++++++++++++++++------------ 1 file changed, 90 insertions(+), 42 deletions(-) diff --git a/JavaScript/versionCheck.js b/JavaScript/versionCheck.js index fe55473..8f78853 100644 --- a/JavaScript/versionCheck.js +++ b/JavaScript/versionCheck.js @@ -1,52 +1,100 @@ -// works with: "1.1.8-beta titled jsmacros.open" or later +// works with: "1.8.5 or later // put this on the JOIN_SERVER event +const screen = Hud.createScreen("JsMacros Version Check", false); +let textcolor = 0xa; +let textcolorno = 0xb; +let textcolorclick = 0x6; + +function createOutdatedScreen(text1, text2, tcolor, currentVersion, newVersion, branch, url) { + const textout1 = Chat.createTextBuilder().append(text1) + .withColor(tcolor) + .withFormatting(false, true, false, false, false); + + const textout2 = Chat.createTextBuilder().append(text2) + .withColor(textcolorclick) + .withFormatting(false, true, false, false, false) + .withCustomClickEvent(JavaWrapper.methodToJava(() => { + screen.setOnInit(JavaWrapper.methodToJava((scr) => { + const w = scr.getWidth(); + const h = scr.getHeight(); + + const t1 = scr.addText(`Current Version:`, 100, 100, 0xFFFFFF, true); + const t2 = scr.addText(`New Version:`, 100, 100, 0xFFFFFF, true); + const t3 = scr.addText(`Branch:`, 100, 100, 0xFFFFFF, true); + + t1.setPos(Math.floor(w / 2 - (t1.getWidth() + 5)), Math.floor(h / 3 - 20)); + t2.setPos(Math.floor(w / 2 - (t1.getWidth() + 5)), Math.floor(h / 3)); + t3.setPos(Math.floor(w / 2 - (t1.getWidth() + 5)), Math.floor(h / 3 + 20)); + + scr.addText(currentVersion, Math.floor(w / 2 + 20), Math.floor(h / 3 - 20), 0xFFFFFF, true); + scr.addText(newVersion, Math.floor(w / 2 + 20), Math.floor(h / 3), 0xFFFFFF, true); + scr.addText(branch, Math.floor(w / 2 + 20), Math.floor(h / 3 + 20), 0xFFFFFF, true); + + scr.addButton(Math.floor(w / 2 + 10), Math.floor(h / 2 + 20), 100, 20, "Close", JavaWrapper.methodToJava(() => { + Hud.openScreen(null); + })); + scr.addButton(Math.floor(w / 2 - 110), Math.floor(h / 2 + 20), 100, 20, "Download", JavaWrapper.methodToJava(() => { + JsMacros.openUrl(url); + })); + + })); + + screen.catchInit = JavaWrapper.methodToJava((e) => { Chat.log(e) }); + + Hud.openScreen(screen); + })) + .withShowTextHover(Chat.createTextBuilder().append(`Click here.`).build()); + + Chat.log(textout1.build()); + Chat.log(textout2.build()); -function createOutdatedScreen(currentVersion, newVersion, url) { - screen = hud.createScreen("JsMacros Version Outdated", false); - screen.onInit = consumer.toConsumer((scr) => { - const w = scr.getWidth(); - const h = scr.getHeight(); - const t1 = scr.addText(`Current Version:`, 100, 100, 0xFFFFFF, true) - const t2 = scr.addText(`New Version:`, 100, 100, 0xFFFFFF, true) - t1.setPos(Math.floor(w / 2 - (t1.getWidth() + 5)), Math.floor(h / 3 - 5)) - t2.setPos(Math.floor(w / 2 - (t1.getWidth() + 5)), Math.floor(h / 3 + 5)) - scr.addText(currentVersion, Math.floor(w / 2 + 20), Math.floor(h / 3 - 5), 0xFFFFFF, true) - scr.addText(newVersion, Math.floor(w / 2 + 20), Math.floor(h / 3 + 5), 0xFFFFFF, true) - scr.addButton(Math.floor(w / 2 + 10), Math.floor(h / 2 + 20), 100, 20, "close", consumer.toBiConsumer(() => { - hud.openScreen(null); - })); - scr.addButton(Math.floor(w / 2 - 110), Math.floor(h / 2 + 20), 100, 20, "download", consumer.toBiConsumer(() => { - jsmacros.open(url); - })); - }) - - screen.catchInit = consumer.toConsumer((e) => {chat.log(e)}); - - hud.openScreen(screen); } -while (hud.getOpenScreen() != null) time.sleep(100); //sleeps java thread running this script for 100ms + +while (Hud.getOpenScreen() != null) Time.sleep(100); //sleeps java thread running this script for 100ms const FabricLoader = Java.type("net.fabricmc.loader.api.FabricLoader"); const loader = FabricLoader.getInstance(); -const versionString = loader.getModContainer("jsmacros").get().getMetadata().getVersion().toString(); -let [branch, version, beta, sha] = versionString.split("-"); +let versionString = loader.getModContainer("jsmacros").get().getMetadata().getVersion().toString(); -//this will need to be changed when I update to a new mc version. -if (branch == "1.16.1") branch = "master"; -else branch = `backport-${branch}` -if (beta) { - workflow_runs = JSON.parse(request.get(`https://api.github.com/repos/wagyourtail/JsMacros/actions/workflows/beta.yml/runs`).text()).workflow_runs - let run = workflow_runs.shift() - while (run && run.head_branch != branch) run = workflow_runs.shift(); - if (run.head_sha.slice(0, 7) != sha) { - newVer = JSON.parse(request.get(run.artifacts_url).text()).artifacts[0]?.name.split("-").slice(2).join("-"); - if (newVer) - createOutdatedScreen(`${version}-${beta}${sha ? `-${sha}` : ""}`, newVer, run.html_url); - } +let data = versionString.split("-"); + +let version = "1.0.0" +let branch = "main"; +let beta = versionString.includes("beta"); +let sha = ""; + +if (versionString.includes("-") && data.length >= 3) { + version = data[0]; + sha = data[2] } else { - gitVer = JSON.parse(request.get(`https://api.github.com/repos/wagyourtail/JsMacros/releases/latest`).text()); - if (gitVer.tag_name != version) { - createOutdatedScreen(version, gitVer.tag_name, "https://github.com/wagyourtail/JsMacros/releases/latest"); - } + version = versionString; } + +let branches = JSON.parse(Request.get(`https://api.github.com/repos/wagyourtail/JsMacros/branches`).text()); + +branch = branches.find(branch => branch.name.includes(`/${Client.mcVersion()}`))?.name ?? "main"; + +if (beta) { + let workflow_runs = JSON.parse(Request.get(`https://api.github.com/repos/wagyourtail/JsMacros/actions/workflows/betabuild.yml/runs`).text()).workflow_runs + let run = workflow_runs.shift() + + while (run && run.head_branch != branch) run = workflow_runs.shift(); + if (run.head_sha.slice(0, 7) != sha) { + newVer = JSON.parse(request.get(run.artifacts_url).text()).artifacts[0]?.name.split("-").slice(2).join("-"); + if (newVer) { + createOutdatedScreen(`Update version ${newVer} avilable!`, `Click here to download update.`, textcolor, `${version}-${beta}${sha ? `-${sha}` : ""}`, newVer, branch, run.html_url); + } else { + createOutdatedScreen(`No update!`, `Click here to open the GUI.`, textcolorno, `${version}-${beta}${sha ? `-${sha}` : ""}`, newVer, branch, "https://github.com/wagyourtail/JsMacros/releases/latest"); + } + } + +} else { + let gitVer = JSON.parse(Request.get(`https://api.github.com/repos/wagyourtail/JsMacros/releases/latest`).text()); + if (gitVer.tag_name != version) { + createOutdatedScreen(`Update version ${gitVer.tag_name} avilable!`, `Click here to download update.`, textcolorno, version, gitVer.tag_name, branch, "https://github.com/wagyourtail/JsMacros/releases/latest"); + } + else { + createOutdatedScreen(`No update!`, `Click here to open the GUI.`, textcolorno, version, gitVer.tag_name, branch, "https://github.com/wagyourtail/JsMacros/releases/latest"); + } +} \ No newline at end of file From 40ae21be38943201b4eed9aa5724d848e8243b58 Mon Sep 17 00:00:00 2001 From: poqdavid Date: Sat, 25 Nov 2023 08:51:45 -0500 Subject: [PATCH 2/3] Replaced deprecated function in versionCheck.js --- JavaScript/versionCheck.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/JavaScript/versionCheck.js b/JavaScript/versionCheck.js index 8f78853..da537b1 100644 --- a/JavaScript/versionCheck.js +++ b/JavaScript/versionCheck.js @@ -34,7 +34,7 @@ function createOutdatedScreen(text1, text2, tcolor, currentVersion, newVersion, Hud.openScreen(null); })); scr.addButton(Math.floor(w / 2 - 110), Math.floor(h / 2 + 20), 100, 20, "Download", JavaWrapper.methodToJava(() => { - JsMacros.openUrl(url); + Utils.openUrl(url); })); })); From 4332603613f6c3c81aceeec1a539a2917fe0e3b6 Mon Sep 17 00:00:00 2001 From: poqdavid Date: Sat, 25 Nov 2023 13:31:02 -0500 Subject: [PATCH 3/3] Changes to versionCheck.js Sound volume and pitch to 1 for click Extra check if correct asset exist in the release Error checking for the Request.get --- JavaScript/versionCheck.js | 53 ++++++++++++++++++++++++++++++++++---- 1 file changed, 48 insertions(+), 5 deletions(-) diff --git a/JavaScript/versionCheck.js b/JavaScript/versionCheck.js index da537b1..aba9cf0 100644 --- a/JavaScript/versionCheck.js +++ b/JavaScript/versionCheck.js @@ -15,6 +15,8 @@ function createOutdatedScreen(text1, text2, tcolor, currentVersion, newVersion, .withFormatting(false, true, false, false, false) .withCustomClickEvent(JavaWrapper.methodToJava(() => { screen.setOnInit(JavaWrapper.methodToJava((scr) => { + World.playSound("ui.button.click", 1, 1); + const w = scr.getWidth(); const h = scr.getHeight(); @@ -50,7 +52,6 @@ function createOutdatedScreen(text1, text2, tcolor, currentVersion, newVersion, } - while (Hud.getOpenScreen() != null) Time.sleep(100); //sleeps java thread running this script for 100ms const FabricLoader = Java.type("net.fabricmc.loader.api.FabricLoader"); @@ -71,12 +72,15 @@ if (versionString.includes("-") && data.length >= 3) { version = versionString; } -let branches = JSON.parse(Request.get(`https://api.github.com/repos/wagyourtail/JsMacros/branches`).text()); +let stringsToCheck = [version, Client.mcVersion(), Client.getModLoader()]; + + +let branches = JSON.parse(getResText(`https://api.github.com/repos/wagyourtail/JsMacros/branches`)); branch = branches.find(branch => branch.name.includes(`/${Client.mcVersion()}`))?.name ?? "main"; if (beta) { - let workflow_runs = JSON.parse(Request.get(`https://api.github.com/repos/wagyourtail/JsMacros/actions/workflows/betabuild.yml/runs`).text()).workflow_runs + let workflow_runs = JSON.parse(getResText(`https://api.github.com/repos/wagyourtail/JsMacros/actions/workflows/betabuild.yml/runs`)).workflow_runs let run = workflow_runs.shift() while (run && run.head_branch != branch) run = workflow_runs.shift(); @@ -90,11 +94,50 @@ if (beta) { } } else { - let gitVer = JSON.parse(Request.get(`https://api.github.com/repos/wagyourtail/JsMacros/releases/latest`).text()); - if (gitVer.tag_name != version) { + let gitVer = JSON.parse(getResText(`https://api.github.com/repos/wagyourtail/JsMacros/releases/latest`)); + let asset_avilable = gitVer.assets.find(asset => stringsToCheck.some(str => asset.name.includes(str))); + + if (gitVer.tag_name != version && asset_avilable) { createOutdatedScreen(`Update version ${gitVer.tag_name} avilable!`, `Click here to download update.`, textcolorno, version, gitVer.tag_name, branch, "https://github.com/wagyourtail/JsMacros/releases/latest"); } else { createOutdatedScreen(`No update!`, `Click here to open the GUI.`, textcolorno, version, gitVer.tag_name, branch, "https://github.com/wagyourtail/JsMacros/releases/latest"); } +} + +function getResText(url) { + try { + const response = Request.get(url); + return response.text(); + } catch (err) { + if (err.toString().includes("FileNotFoundException")) { + printResponseCode(url, "404"); + } else if (err.toString().includes(" 403 ")) { + printResponseCode(url, "403 API rate limit exceeded"); + } else { + throw err; + } + } +} + +function getUrlText(url) { + let parts = url.split('/', 4); + let trimmedUrl = parts.length < 4 ? url : parts.slice(0, 3).join('/') + "/..."; + let text = Chat.createTextBuilder().append(trimmedUrl) + .withColor(0xb) + .withFormatting(false, true, false, false, false) + .withShowTextHover(Chat.createTextBuilder().append(url).withColor(0xb).build()); + + return text.build(); +} + +function printResponseCode(url, message) { + Chat.getLogger().info(`URL: ${url}`); + Chat.log(Chat.createTextBuilder().append(`URL: `).withColor(0x6).append(getUrlText(url)).build()); + throw new ResponseCode(message); +} + +function ResponseCode(responseCode) { + this.name = "ResponseCode"; + this.message = `${responseCode}`; } \ No newline at end of file