diff options
5 files changed, 148 insertions, 53 deletions
diff --git a/Update Notes/2.1.md b/Update Notes/2.1.md index 71350dc7..5dcb023a 100644 --- a/Update Notes/2.1.md +++ b/Update Notes/2.1.md @@ -100,6 +100,7 @@ - Added Fishing Timer over bobber - nea89 - Added [Auction Profit Viewer Overlay](https://cdn.discordapp.com/attachments/848901833119629332/993191851400101918/176946124-28ddf336-1ec7-460b-b22a-5fe2733b46a3.png) - efefury - Added Trophy Reward Overlay - hannibal2 +- Add Auto-Updater (linux and scu only) - nea89 ### **Bug Fixes:** diff --git a/src/main/java/io/github/moulberry/notenoughupdates/miscfeatures/updater/AutoUpdater.java b/src/main/java/io/github/moulberry/notenoughupdates/miscfeatures/updater/AutoUpdater.java index 7814c6e9..ff36b092 100644 --- a/src/main/java/io/github/moulberry/notenoughupdates/miscfeatures/updater/AutoUpdater.java +++ b/src/main/java/io/github/moulberry/notenoughupdates/miscfeatures/updater/AutoUpdater.java @@ -76,7 +76,7 @@ public class AutoUpdater { return new LinuxBasedUpdater(this, url); } if (Loader.isModLoaded("skyblockclientupdater")) { - // TODO: add SCU support + return SCUCompatUpdater.tryCreate(this, url); } return null; } @@ -99,7 +99,10 @@ public class AutoUpdater { "Your system does not support auto updates. Please download this update manually. Click here to read more about auto update compatibility (or the link above for manual downloads)") .setChatStyle( new ChatStyle() - .setChatHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ChatComponentText("Click here to read about auto update modalities"))) + .setChatHoverEvent(new HoverEvent( + HoverEvent.Action.SHOW_TEXT, + new ChatComponentText("Click here to read about auto update modalities") + )) .setChatClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/neuupdate updatemodes")) )); return; diff --git a/src/main/java/io/github/moulberry/notenoughupdates/miscfeatures/updater/LinuxBasedUpdater.java b/src/main/java/io/github/moulberry/notenoughupdates/miscfeatures/updater/LinuxBasedUpdater.java index a7eddf7b..3118b135 100644 --- a/src/main/java/io/github/moulberry/notenoughupdates/miscfeatures/updater/LinuxBasedUpdater.java +++ b/src/main/java/io/github/moulberry/notenoughupdates/miscfeatures/updater/LinuxBasedUpdater.java @@ -19,16 +19,11 @@ package io.github.moulberry.notenoughupdates.miscfeatures.updater; -import com.google.common.io.Files; -import net.minecraft.client.Minecraft; - import java.io.File; -import java.io.IOException; import java.net.URL; -import java.util.ArrayList; +import java.util.List; -/* Based on what? */ -class LinuxBasedUpdater extends UpdateLoader { +class LinuxBasedUpdater /* Based on what? */ extends UpdateLoader { LinuxBasedUpdater(AutoUpdater updater, URL url) { super(updater, url); @@ -41,44 +36,12 @@ class LinuxBasedUpdater extends UpdateLoader { } @Override - public void launchUpdate(File file) { - if (state != State.DOWNLOAD_FINISHED) { - updater.logProgress("§cUpdate is invalid state " + state + " to start update."); - state = State.FAILED; - return; - } - File mcDataDir = new File(Minecraft.getMinecraft().mcDataDir, "mods"); - if (!mcDataDir.exists() || !mcDataDir.isDirectory() || !mcDataDir.canRead()) { - updater.logProgress("§cCould not find mods folder. Searched: " + mcDataDir); - state = State.FAILED; - return; - } - ArrayList<File> toDelete = new ArrayList<>(); - for (File sus : mcDataDir.listFiles()) { - if (sus.getName().endsWith(".jar")) { - if (updater.isNeuJar(sus)) { - updater.logProgress("Found old NEU file: " + sus + ". Deleting later."); - toDelete.add(sus); - } - } - } - File dest = new File(mcDataDir, file.getName()); - try { - Files.copy(file, dest); - } catch (IOException e) { - e.printStackTrace(); - updater.logProgress( - "§cFailed to copy release JAR. Not making any changes to your mod folder. Consult your logs for more info."); - state = State.FAILED; - } + public void deleteFiles(List<File> toDelete) { for (File toDel : toDelete) { if (!toDel.delete()) { updater.logProgress("§cCould not delete old version of NEU: " + toDel + ". Please manually delete file."); state = State.FAILED; } } - if (state != State.FAILED) - state = State.INSTALLED; - updater.logProgress("Update successful. Thank you for your time."); } } diff --git a/src/main/java/io/github/moulberry/notenoughupdates/miscfeatures/updater/SCUCompatUpdater.java b/src/main/java/io/github/moulberry/notenoughupdates/miscfeatures/updater/SCUCompatUpdater.java new file mode 100644 index 00000000..3e68fcd0 --- /dev/null +++ b/src/main/java/io/github/moulberry/notenoughupdates/miscfeatures/updater/SCUCompatUpdater.java @@ -0,0 +1,96 @@ +/* + * Copyright (C) 2022 NotEnoughUpdates contributors + * + * This file is part of NotEnoughUpdates. + * + * NotEnoughUpdates is free software: you can redistribute it + * and/or modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation, either + * version 3 of the License, or (at your option) any later version. + * + * NotEnoughUpdates is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with NotEnoughUpdates. If not, see <https://www.gnu.org/licenses/>. + */ + +package io.github.moulberry.notenoughupdates.miscfeatures.updater; + +import java.io.File; +import java.lang.invoke.MethodHandle; +import java.lang.invoke.MethodHandles; +import java.lang.reflect.Field; +import java.lang.reflect.Method; +import java.net.URL; +import java.util.List; + +/* + * Legal considerations: Skyblock Client Updater is licensed under the GNU AGPL v3.0 or later (with modifications). + * https://github.com/My-Name-Is-Jeff/SkyblockClient-Updater/blob/main/LICENSE + * + * However, even tho the AGPL License does not allow conveying covered work in combination with LGPL licensed code + * (such as our own), we do not perceive ourselves as conveying neither an unmodified version of Skyblock Client Updater + * nor a work based on Skyblock Client Updater (modified work) since our work is usable and functional in its entirety + * without presence of Skyblock Client Updater and is not to be distributed along a copy of Skyblock Client Updater + * unless that combined work is licensed with respect of both the LGPL and the AGPL, therefore is not adapting any part + * of Skyblock Client Updater unless already part of a whole distribution. + * + * In case the Copyright owner (Lily aka My-Name-Is-Jeff on Github) disagrees, we are willing to take down this module + * (or only convey this component of our work under a pure GPL license) with or without them providing legal grounds + * for this request. However, due to them not being able to be reached for comment, we will include this + * component for the time being. + * */ +public class SCUCompatUpdater extends UpdateLoader { + + private SCUCompatUpdater(AutoUpdater updater, URL url) { + super(updater, url); + } + + @Override + public void greet() { + updater.logProgress("Skyblock Client Updater compatibility layer loaded."); + } + + @Override + public void deleteFiles(List<File> toDelete) { + try { + for (File f : toDelete) + ReflectionHolder.deleteFileOnShutdownHandle.invoke(f, ""); + } catch (Throwable e) { + e.printStackTrace(); + updater.logProgress("Invoking SCU failed. Check the log for more info."); + state = State.FAILED; + } + } + + static class ReflectionHolder { + static boolean isSCUFullyPresent = false; + static Class<?> updateChecker; + static Object updateCheckerInstance; + static Method deleteFileOnShutdown; + static MethodHandle deleteFileOnShutdownHandle; + + static { + try { + updateChecker = Class.forName("mynameisjeff.skyblockclientupdater.utils.UpdateChecker"); + Field instanceField = updateChecker.getDeclaredField("INSTANCE"); + instanceField.setAccessible(true); + updateCheckerInstance = instanceField.get(null); + deleteFileOnShutdown = updateChecker.getDeclaredMethod("deleteFileOnShutdown", File.class, String.class); + deleteFileOnShutdownHandle = MethodHandles.publicLookup().unreflect(deleteFileOnShutdown); + isSCUFullyPresent = true; + } catch (NoSuchFieldException | ClassNotFoundException | IllegalAccessException | NoSuchMethodException e) { + e.printStackTrace(); + } + } + } + + public static UpdateLoader tryCreate(AutoUpdater updater, URL url) { + if (!ReflectionHolder.isSCUFullyPresent) + updater.logProgress("§cFound Skyclient Updater Mod, however our hooks did not function properly."); + return new SCUCompatUpdater(updater, url); + } +} diff --git a/src/main/java/io/github/moulberry/notenoughupdates/miscfeatures/updater/UpdateLoader.java b/src/main/java/io/github/moulberry/notenoughupdates/miscfeatures/updater/UpdateLoader.java index 0f2a943b..8d301af7 100644 --- a/src/main/java/io/github/moulberry/notenoughupdates/miscfeatures/updater/UpdateLoader.java +++ b/src/main/java/io/github/moulberry/notenoughupdates/miscfeatures/updater/UpdateLoader.java @@ -19,20 +19,15 @@ package io.github.moulberry.notenoughupdates.miscfeatures.updater; -import com.google.gson.JsonArray; -import com.google.gson.JsonElement; -import com.google.gson.JsonObject; -import com.google.gson.JsonPrimitive; -import com.google.gson.JsonSyntaxException; +import com.google.common.io.Files; import io.github.moulberry.notenoughupdates.util.NetUtils; +import net.minecraft.client.Minecraft; import java.io.File; import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; import java.net.URL; -import java.util.zip.ZipEntry; -import java.util.zip.ZipFile; +import java.util.ArrayList; +import java.util.List; abstract class UpdateLoader { @@ -76,7 +71,6 @@ abstract class UpdateLoader { } state = State.DOWNLOAD_FINISHED; - updater.logProgress("Download completed. Trying to install"); launchUpdate(f); return null; @@ -90,5 +84,43 @@ abstract class UpdateLoader { public abstract void greet(); - public abstract void launchUpdate(File file); + public void launchUpdate(File file) { + + if (state != State.DOWNLOAD_FINISHED) { + updater.logProgress("§cUpdate is invalid state " + state + " to start update."); + state = State.FAILED; + return; + } + File mcDataDir = new File(Minecraft.getMinecraft().mcDataDir, "mods"); + if (!mcDataDir.exists() || !mcDataDir.isDirectory() || !mcDataDir.canRead()) { + updater.logProgress("§cCould not find mods folder. Searched: " + mcDataDir); + state = State.FAILED; + return; + } + ArrayList<File> toDelete = new ArrayList<>(); + for (File sus : mcDataDir.listFiles()) { + if (sus.getName().endsWith(".jar")) { + if (updater.isNeuJar(sus)) { + updater.logProgress("Found old NEU file: " + sus + ". Deleting later."); + toDelete.add(sus); + } + } + } + File dest = new File(mcDataDir, file.getName()); + try { + Files.copy(file, dest); + } catch (IOException e) { + e.printStackTrace(); + updater.logProgress( + "§cFailed to copy release JAR. Not making any changes to your mod folder. Consult your logs for more info."); + state = State.FAILED; + } + deleteFiles(toDelete); + if (state != State.FAILED) + state = State.INSTALLED; + updater.logProgress("Update successful. Thank you for your time."); + } + + public abstract void deleteFiles(List<File> toDelete); + } |