From cf705024fe4e54f8ffcab74a08c21fe9fc89e9dd Mon Sep 17 00:00:00 2001 From: syeyoung Date: Fri, 18 Nov 2022 03:16:58 +0900 Subject: - cleanup pain Signed-off-by: syeyoung --- .../kr/syeyoung/dungeonsguide/launcher/Main.java | 12 +++++------ .../launcher/events/OwnerAwareASMEventHandler.java | 11 ++-------- .../DungeonsGuideUnloadingException.java | 24 ++++++++++++++++++++++ .../exceptions/ReferenceLeakedException.java | 22 -------------------- .../dungeonsguide/launcher/loader/IDGLoader.java | 6 ++---- .../dungeonsguide/launcher/loader/JarLoader.java | 18 ++++++++-------- .../dungeonsguide/launcher/loader/LocalLoader.java | 16 +++++++-------- .../launcher/loader/RemoteLoader.java | 15 ++++++++------ 8 files changed, 59 insertions(+), 65 deletions(-) create mode 100644 loader/src/main/java/kr/syeyoung/dungeonsguide/launcher/exceptions/DungeonsGuideUnloadingException.java delete mode 100644 loader/src/main/java/kr/syeyoung/dungeonsguide/launcher/exceptions/ReferenceLeakedException.java (limited to 'loader/src/main') diff --git a/loader/src/main/java/kr/syeyoung/dungeonsguide/launcher/Main.java b/loader/src/main/java/kr/syeyoung/dungeonsguide/launcher/Main.java index 1678c58f..2ada9dc8 100755 --- a/loader/src/main/java/kr/syeyoung/dungeonsguide/launcher/Main.java +++ b/loader/src/main/java/kr/syeyoung/dungeonsguide/launcher/Main.java @@ -23,7 +23,7 @@ import kr.syeyoung.dungeonsguide.launcher.branch.UpdateRetrieverUtil; import kr.syeyoung.dungeonsguide.launcher.exceptions.DungeonsGuideLoadingException; import kr.syeyoung.dungeonsguide.launcher.exceptions.NoSuitableLoaderFoundException; import kr.syeyoung.dungeonsguide.launcher.exceptions.NoVersionFoundException; -import kr.syeyoung.dungeonsguide.launcher.exceptions.ReferenceLeakedException; +import kr.syeyoung.dungeonsguide.launcher.exceptions.DungeonsGuideUnloadingException; import kr.syeyoung.dungeonsguide.launcher.gui.screen.GuiDisplayer; import kr.syeyoung.dungeonsguide.launcher.gui.screen.GuiLoadingError; import kr.syeyoung.dungeonsguide.launcher.gui.screen.GuiUnloadingError; @@ -134,7 +134,7 @@ public class Main } } - public void unload() throws ReferenceLeakedException { + public void unload() throws DungeonsGuideUnloadingException { if (currentLoader != null && !currentLoader.isUnloadable()) { throw new UnsupportedOperationException("Current version is not unloadable"); } @@ -171,7 +171,7 @@ public class Main GuiDisplayer.INSTANCE.displayGui(new GuiUnloadingError(e2)); } GuiDisplayer.INSTANCE.displayGui(new GuiLoadingError(e)); - } catch (ReferenceLeakedException e) { + } catch (DungeonsGuideUnloadingException e) { GuiDisplayer.INSTANCE.displayGui(new GuiUnloadingError(e)); } }) @@ -221,17 +221,17 @@ public class Main GuiDisplayer.INSTANCE.displayGui(new GuiUnloadingError(e2)); } GuiDisplayer.INSTANCE.displayGui(new GuiLoadingError(e)); - } catch (ReferenceLeakedException e) { + } catch (DungeonsGuideUnloadingException e) { GuiDisplayer.INSTANCE.displayGui(new GuiUnloadingError(e)); } } } - public void reload(IDGLoader newLoader) throws DungeonsGuideLoadingException, ReferenceLeakedException { + public void reload(IDGLoader newLoader) throws DungeonsGuideLoadingException, DungeonsGuideUnloadingException { try { unload(); load(newLoader); - } catch (DungeonsGuideLoadingException | ReferenceLeakedException | UnsupportedOperationException e) { + } catch (DungeonsGuideLoadingException | DungeonsGuideUnloadingException | UnsupportedOperationException e) { dgInterface = null; currentLoader = null; diff --git a/loader/src/main/java/kr/syeyoung/dungeonsguide/launcher/events/OwnerAwareASMEventHandler.java b/loader/src/main/java/kr/syeyoung/dungeonsguide/launcher/events/OwnerAwareASMEventHandler.java index 3475e672..83f78720 100644 --- a/loader/src/main/java/kr/syeyoung/dungeonsguide/launcher/events/OwnerAwareASMEventHandler.java +++ b/loader/src/main/java/kr/syeyoung/dungeonsguide/launcher/events/OwnerAwareASMEventHandler.java @@ -4,6 +4,7 @@ import static org.objectweb.asm.Opcodes.*; import java.lang.reflect.Method; import java.util.HashMap; +import java.util.WeakHashMap; import net.minecraftforge.fml.common.ModContainer; @@ -24,7 +25,6 @@ public class OwnerAwareASMEventHandler implements IEventListener private static int IDs = 0; private static final String HANDLER_DESC = Type.getInternalName(IEventListener.class); private static final String HANDLER_FUNC_DESC = Type.getMethodDescriptor(IEventListener.class.getDeclaredMethods()[0]); - private static final HashMap> cache = Maps.newHashMap(); private static final boolean GETCONTEXT = Boolean.parseBoolean(System.getProperty("fml.LogContext", "false")); private final IEventListener handler; @@ -63,11 +63,6 @@ public class OwnerAwareASMEventHandler implements IEventListener public Class createWrapper(Method callback) { - if (cache.containsKey(callback)) - { - return cache.get(callback); - } - ClassWriter cw = new ClassWriter(0); MethodVisitor mv; @@ -116,9 +111,7 @@ public class OwnerAwareASMEventHandler implements IEventListener mv.visitEnd(); } cw.visitEnd(); - Class ret = new ASMClassLoader(callback.getDeclaringClass().getClassLoader()).define(name, cw.toByteArray()); - cache.put(callback, ret); - return ret; + return new ASMClassLoader(callback.getDeclaringClass().getClassLoader()).define(name, cw.toByteArray()); } private String getUniqueName(Method callback) diff --git a/loader/src/main/java/kr/syeyoung/dungeonsguide/launcher/exceptions/DungeonsGuideUnloadingException.java b/loader/src/main/java/kr/syeyoung/dungeonsguide/launcher/exceptions/DungeonsGuideUnloadingException.java new file mode 100644 index 00000000..6b2850f6 --- /dev/null +++ b/loader/src/main/java/kr/syeyoung/dungeonsguide/launcher/exceptions/DungeonsGuideUnloadingException.java @@ -0,0 +1,24 @@ +/* + * Dungeons Guide - The most intelligent Hypixel Skyblock Dungeons Mod + * Copyright (C) 2021 cyoung06 + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published + * by the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program 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 Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package kr.syeyoung.dungeonsguide.launcher.exceptions; + +public class DungeonsGuideUnloadingException extends Exception { + public DungeonsGuideUnloadingException(String message) {super(message);} + public DungeonsGuideUnloadingException(Exception cause) {super(cause);} +} diff --git a/loader/src/main/java/kr/syeyoung/dungeonsguide/launcher/exceptions/ReferenceLeakedException.java b/loader/src/main/java/kr/syeyoung/dungeonsguide/launcher/exceptions/ReferenceLeakedException.java deleted file mode 100644 index 3a88b862..00000000 --- a/loader/src/main/java/kr/syeyoung/dungeonsguide/launcher/exceptions/ReferenceLeakedException.java +++ /dev/null @@ -1,22 +0,0 @@ -/* - * Dungeons Guide - The most intelligent Hypixel Skyblock Dungeons Mod - * Copyright (C) 2021 cyoung06 - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published - * by the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program 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 Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ - -package kr.syeyoung.dungeonsguide.launcher.exceptions; - -public class ReferenceLeakedException extends Exception { -} diff --git a/loader/src/main/java/kr/syeyoung/dungeonsguide/launcher/loader/IDGLoader.java b/loader/src/main/java/kr/syeyoung/dungeonsguide/launcher/loader/IDGLoader.java index 1edc0b7c..52982355 100644 --- a/loader/src/main/java/kr/syeyoung/dungeonsguide/launcher/loader/IDGLoader.java +++ b/loader/src/main/java/kr/syeyoung/dungeonsguide/launcher/loader/IDGLoader.java @@ -20,14 +20,12 @@ package kr.syeyoung.dungeonsguide.launcher.loader; import kr.syeyoung.dungeonsguide.launcher.DGInterface; import kr.syeyoung.dungeonsguide.launcher.exceptions.DungeonsGuideLoadingException; -import kr.syeyoung.dungeonsguide.launcher.exceptions.ReferenceLeakedException; - -import java.io.IOException; +import kr.syeyoung.dungeonsguide.launcher.exceptions.DungeonsGuideUnloadingException; public interface IDGLoader { DGInterface loadDungeonsGuide() throws DungeonsGuideLoadingException; DGInterface getInstance(); - void unloadDungeonsGuide() throws ReferenceLeakedException; + void unloadDungeonsGuide() throws DungeonsGuideUnloadingException; boolean isUnloadable(); diff --git a/loader/src/main/java/kr/syeyoung/dungeonsguide/launcher/loader/JarLoader.java b/loader/src/main/java/kr/syeyoung/dungeonsguide/launcher/loader/JarLoader.java index 39fcbf58..61dec833 100644 --- a/loader/src/main/java/kr/syeyoung/dungeonsguide/launcher/loader/JarLoader.java +++ b/loader/src/main/java/kr/syeyoung/dungeonsguide/launcher/loader/JarLoader.java @@ -19,22 +19,16 @@ package kr.syeyoung.dungeonsguide.launcher.loader; import kr.syeyoung.dungeonsguide.launcher.DGInterface; -import kr.syeyoung.dungeonsguide.launcher.Main; import kr.syeyoung.dungeonsguide.launcher.exceptions.DungeonsGuideLoadingException; -import kr.syeyoung.dungeonsguide.launcher.exceptions.ReferenceLeakedException; +import kr.syeyoung.dungeonsguide.launcher.exceptions.DungeonsGuideUnloadingException; import org.apache.commons.io.IOUtils; -import sun.misc.Resource; import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.lang.ref.PhantomReference; import java.lang.ref.Reference; import java.lang.ref.ReferenceQueue; -import java.lang.ref.WeakReference; -import java.net.URL; -import java.net.URLClassLoader; import java.util.HashMap; import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; @@ -94,13 +88,17 @@ public class JarLoader implements IDGLoader { } @Override - public void unloadDungeonsGuide() throws ReferenceLeakedException { + public void unloadDungeonsGuide() throws DungeonsGuideUnloadingException { classLoader = null; - dgInterface.unload(); + try { + dgInterface.unload(); + } catch (Exception e) { + throw new DungeonsGuideUnloadingException(e); + } dgInterface = null; System.gc();// pls do Reference t = refQueue.poll(); - if (t == null) throw new ReferenceLeakedException(); // Why do you have to be that strict? Well, to tell them to actually listen on DungeonsGuideReloadListener. If it starts causing issues then I will remove check cus it's not really loaded (classes are loaded by child classloader) + if (t == null) throw new DungeonsGuideUnloadingException("Reference Leaked"); // Why do you have to be that strict? Well, to tell them to actually listen on DungeonsGuideReloadListener. If it starts causing issues then I will remove check cus it's not really loaded (classes are loaded by child classloader) t.clear(); phantomReference = null; } diff --git a/loader/src/main/java/kr/syeyoung/dungeonsguide/launcher/loader/LocalLoader.java b/loader/src/main/java/kr/syeyoung/dungeonsguide/launcher/loader/LocalLoader.java index ec3e59fb..519578f1 100644 --- a/loader/src/main/java/kr/syeyoung/dungeonsguide/launcher/loader/LocalLoader.java +++ b/loader/src/main/java/kr/syeyoung/dungeonsguide/launcher/loader/LocalLoader.java @@ -20,19 +20,15 @@ package kr.syeyoung.dungeonsguide.launcher.loader; import kr.syeyoung.dungeonsguide.launcher.DGInterface; import kr.syeyoung.dungeonsguide.launcher.exceptions.DungeonsGuideLoadingException; -import kr.syeyoung.dungeonsguide.launcher.exceptions.ReferenceLeakedException; +import kr.syeyoung.dungeonsguide.launcher.exceptions.DungeonsGuideUnloadingException; import org.apache.commons.io.IOUtils; import java.io.BufferedInputStream; -import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.lang.ref.PhantomReference; import java.lang.ref.Reference; import java.lang.ref.ReferenceQueue; -import java.util.HashMap; -import java.util.zip.ZipEntry; -import java.util.zip.ZipInputStream; public class LocalLoader implements IDGLoader { private DGInterface dgInterface; @@ -84,13 +80,17 @@ public class LocalLoader implements IDGLoader { } @Override - public void unloadDungeonsGuide() throws ReferenceLeakedException { + public void unloadDungeonsGuide() throws DungeonsGuideUnloadingException { classLoader = null; - dgInterface.unload(); + try { + dgInterface.unload(); + } catch (Exception e) { + throw new DungeonsGuideUnloadingException(e); + } dgInterface = null; System.gc();// pls do Reference t = refQueue.poll(); - if (t == null) throw new ReferenceLeakedException(); // Why do you have to be that strict? Well, to tell them to actually listen on DungeonsGuideReloadListener. If it starts causing issues then I will remove check cus it's not really loaded (classes are loaded by child classloader) + if (t == null) throw new DungeonsGuideUnloadingException("Reference Leaked"); // Why do you have to be that strict? Well, to tell them to actually listen on DungeonsGuideReloadListener. If it starts causing issues then I will remove check cus it's not really loaded (classes are loaded by child classloader) t.clear(); phantomReference = null; } diff --git a/loader/src/main/java/kr/syeyoung/dungeonsguide/launcher/loader/RemoteLoader.java b/loader/src/main/java/kr/syeyoung/dungeonsguide/launcher/loader/RemoteLoader.java index ceafb6f7..e4becc53 100644 --- a/loader/src/main/java/kr/syeyoung/dungeonsguide/launcher/loader/RemoteLoader.java +++ b/loader/src/main/java/kr/syeyoung/dungeonsguide/launcher/loader/RemoteLoader.java @@ -20,12 +20,11 @@ package kr.syeyoung.dungeonsguide.launcher.loader; import kr.syeyoung.dungeonsguide.launcher.DGInterface; import kr.syeyoung.dungeonsguide.launcher.branch.Update; -import kr.syeyoung.dungeonsguide.launcher.branch.UpdateBranch; import kr.syeyoung.dungeonsguide.launcher.branch.UpdateRetrieverUtil; import kr.syeyoung.dungeonsguide.launcher.exceptions.DungeonsGuideLoadingException; import kr.syeyoung.dungeonsguide.launcher.exceptions.InvalidSignatureException; import kr.syeyoung.dungeonsguide.launcher.exceptions.NoVersionFoundException; -import kr.syeyoung.dungeonsguide.launcher.exceptions.ReferenceLeakedException; +import kr.syeyoung.dungeonsguide.launcher.exceptions.DungeonsGuideUnloadingException; import org.apache.commons.io.IOUtils; import java.io.ByteArrayInputStream; @@ -35,7 +34,6 @@ import java.lang.ref.PhantomReference; import java.lang.ref.Reference; import java.lang.ref.ReferenceQueue; import java.util.HashMap; -import java.util.List; import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; @@ -123,13 +121,18 @@ public class RemoteLoader implements IDGLoader { } @Override - public void unloadDungeonsGuide() throws ReferenceLeakedException { + public void unloadDungeonsGuide() throws DungeonsGuideUnloadingException { classLoader = null; - dgInterface.unload(); + + try { + dgInterface.unload(); + } catch (Exception e) { + throw new DungeonsGuideUnloadingException(e); + } dgInterface = null; System.gc();// pls do Reference t = refQueue.poll(); - if (t == null) throw new ReferenceLeakedException(); // Why do you have to be that strict? Well, to tell them to actually listen on DungeonsGuideReloadListener. If it starts causing issues then I will remove check cus it's not really loaded (classes are loaded by child classloader) + if (t == null) throw new DungeonsGuideUnloadingException("Reference Leaked"); // Why do you have to be that strict? Well, to tell them to actually listen on DungeonsGuideReloadListener. If it starts causing issues then I will remove check cus it's not really loaded (classes are loaded by child classloader) t.clear(); phantomReference = null; } -- cgit