From 1a923ff1fda799906cc56c7a8a767053558db95c Mon Sep 17 00:00:00 2001 From: Linnea Gräf Date: Wed, 8 Jan 2025 14:48:54 +0100 Subject: fix: Hide mod resources patch outside of the devenv --- .../nea/ledger/init/AutoDiscoveryMixinPlugin.java | 319 +++++++++++---------- .../ledger/mixin/RegisterModResourcesPatch.java | 66 ----- .../mixin/devenv/RegisterModResourcesPatch.java | 66 +++++ 3 files changed, 228 insertions(+), 223 deletions(-) delete mode 100644 src/main/java/moe/nea/ledger/mixin/RegisterModResourcesPatch.java create mode 100644 src/main/java/moe/nea/ledger/mixin/devenv/RegisterModResourcesPatch.java (limited to 'src/main/java/moe') diff --git a/src/main/java/moe/nea/ledger/init/AutoDiscoveryMixinPlugin.java b/src/main/java/moe/nea/ledger/init/AutoDiscoveryMixinPlugin.java index 2c3a9c7..56841b5 100644 --- a/src/main/java/moe/nea/ledger/init/AutoDiscoveryMixinPlugin.java +++ b/src/main/java/moe/nea/ledger/init/AutoDiscoveryMixinPlugin.java @@ -1,5 +1,6 @@ package moe.nea.ledger.init; +import net.minecraft.launchwrapper.Launch; import org.spongepowered.asm.lib.tree.ClassNode; import org.spongepowered.asm.mixin.extensibility.IMixinConfigPlugin; import org.spongepowered.asm.mixin.extensibility.IMixinInfo; @@ -28,161 +29,165 @@ import java.util.zip.ZipInputStream; * @author Linnea Gräf */ public class AutoDiscoveryMixinPlugin implements IMixinConfigPlugin { - private static final List mixinPlugins = new ArrayList<>(); - - public static List getMixinPlugins() { - return mixinPlugins; - } - - private String mixinPackage; - - @Override - public void onLoad(String mixinPackage) { - this.mixinPackage = mixinPackage; - mixinPlugins.add(this); - } - - /** - * Resolves the base class root for a given class URL. This resolves either the JAR root, or the class file root. - * In either case the return value of this + the class name will resolve back to the original class url, or to other - * class urls for other classes. - */ - public URL getBaseUrlForClassUrl(URL classUrl) { - String string = classUrl.toString(); - if (classUrl.getProtocol().equals("jar")) { - try { - return new URL(string.substring(4).split("!")[0]); - } catch (MalformedURLException e) { - throw new RuntimeException(e); - } - } - if (string.endsWith(".class")) { - try { - return new URL(string.replace("\\", "/") - .replace(getClass().getCanonicalName() - .replace(".", "/") + ".class", "")); - } catch (MalformedURLException e) { - throw new RuntimeException(e); - } - } - return classUrl; - } - - /** - * Get the package that contains all the mixins. This value is set by mixin itself using {@link #onLoad}. - */ - public String getMixinPackage() { - return mixinPackage; - } - - /** - * Get the path inside the class root to the mixin package - */ - public String getMixinBaseDir() { - return mixinPackage.replace(".", "/"); - } - - /** - * A list of all discovered mixins. - */ - private List mixins = null; - - /** - * Try to add mixin class ot the mixins based on the filepath inside of the class root. - * Removes the {@code .class} file suffix, as well as the base mixin package. - *

This method cannot be called after mixin initialization.

- * - * @param className the name or path of a class to be registered as a mixin. - */ - public void tryAddMixinClass(String className) { - String norm = (className.endsWith(".class") ? className.substring(0, className.length() - ".class".length()) : className) - .replace("\\", "/") - .replace("/", "."); - if (norm.startsWith(getMixinPackage() + ".") && !norm.endsWith(".")) { - mixins.add(norm.substring(getMixinPackage().length() + 1)); - } - } - - /** - * Search through the JAR or class directory to find mixins contained in {@link #getMixinPackage()} - */ - @Override - public List getMixins() { - if (mixins != null) return mixins; - System.out.println("Trying to discover mixins"); - mixins = new ArrayList<>(); - URL classUrl = getClass().getProtectionDomain().getCodeSource().getLocation(); - System.out.println("Found classes at " + classUrl); - Path file; - try { - file = Paths.get(getBaseUrlForClassUrl(classUrl).toURI()); - } catch (URISyntaxException e) { - throw new RuntimeException(e); - } - System.out.println("Base directory found at " + file); - if (Files.isDirectory(file)) { - walkDir(file); - } else { - walkJar(file); - } - System.out.println("Found mixins: " + mixins); - - return mixins; - } - - /** - * Search through directory for mixin classes based on {@link #getMixinBaseDir}. - * - * @param classRoot The root directory in which classes are stored for the default package. - */ - private void walkDir(Path classRoot) { - System.out.println("Trying to find mixins from directory"); - try (Stream classes = Files.walk(classRoot.resolve(getMixinBaseDir()))) { - classes.map(it -> classRoot.relativize(it).toString()) - .forEach(this::tryAddMixinClass); - } catch (IOException e) { - throw new RuntimeException(e); - } - } - - /** - * Read through a JAR file, trying to find all mixins inside. - */ - private void walkJar(Path file) { - System.out.println("Trying to find mixins from jar file"); - try (ZipInputStream zis = new ZipInputStream(Files.newInputStream(file))) { - ZipEntry next; - while ((next = zis.getNextEntry()) != null) { - tryAddMixinClass(next.getName()); - zis.closeEntry(); - } - } catch (IOException e) { - throw new RuntimeException(e); - } - } - - @Override - public void preApply(String targetClassName, ClassNode targetClass, String mixinClassName, IMixinInfo mixinInfo) { - - } - - @Override - public void postApply(String targetClassName, ClassNode targetClass, String mixinClassName, IMixinInfo mixinInfo) { - - } - - @Override - public String getRefMapperConfig() { - return null; - } - - @Override - public boolean shouldApplyMixin(String targetClassName, String mixinClassName) { - return true; - } - - @Override - public void acceptTargets(Set myTargets, Set otherTargets) { - - } + private static final List mixinPlugins = new ArrayList<>(); + + public static List getMixinPlugins() { + return mixinPlugins; + } + + private String mixinPackage; + + @Override + public void onLoad(String mixinPackage) { + this.mixinPackage = mixinPackage; + mixinPlugins.add(this); + } + + /** + * Resolves the base class root for a given class URL. This resolves either the JAR root, or the class file root. + * In either case the return value of this + the class name will resolve back to the original class url, or to other + * class urls for other classes. + */ + public URL getBaseUrlForClassUrl(URL classUrl) { + String string = classUrl.toString(); + if (classUrl.getProtocol().equals("jar")) { + try { + return new URL(string.substring(4).split("!")[0]); + } catch (MalformedURLException e) { + throw new RuntimeException(e); + } + } + if (string.endsWith(".class")) { + try { + return new URL(string.replace("\\", "/") + .replace(getClass().getCanonicalName() + .replace(".", "/") + ".class", "")); + } catch (MalformedURLException e) { + throw new RuntimeException(e); + } + } + return classUrl; + } + + /** + * Get the package that contains all the mixins. This value is set by mixin itself using {@link #onLoad}. + */ + public String getMixinPackage() { + return mixinPackage; + } + + /** + * Get the path inside the class root to the mixin package + */ + public String getMixinBaseDir() { + return mixinPackage.replace(".", "/"); + } + + /** + * A list of all discovered mixins. + */ + private List mixins = null; + + /** + * Try to add mixin class ot the mixins based on the filepath inside of the class root. + * Removes the {@code .class} file suffix, as well as the base mixin package. + *

This method cannot be called after mixin initialization.

+ * + * @param className the name or path of a class to be registered as a mixin. + */ + public void tryAddMixinClass(String className) { + String norm = (className.endsWith(".class") ? className.substring(0, className.length() - ".class".length()) : className) + .replace("\\", "/") + .replace("/", "."); + if (norm.startsWith(getMixinPackage() + ".") && !norm.endsWith(".")) { + mixins.add(norm.substring(getMixinPackage().length() + 1)); + } + } + + /** + * Search through the JAR or class directory to find mixins contained in {@link #getMixinPackage()} + */ + @Override + public List getMixins() { + if (mixins != null) return mixins; + System.out.println("Trying to discover mixins"); + mixins = new ArrayList<>(); + URL classUrl = getClass().getProtectionDomain().getCodeSource().getLocation(); + System.out.println("Found classes at " + classUrl); + Path file; + try { + file = Paths.get(getBaseUrlForClassUrl(classUrl).toURI()); + } catch (URISyntaxException e) { + throw new RuntimeException(e); + } + System.out.println("Base directory found at " + file); + if (Files.isDirectory(file)) { + walkDir(file); + } else { + walkJar(file); + } + System.out.println("Found mixins: " + mixins); + + if (!(Boolean) Launch.blackboard.get("fml.deobfuscatedEnvironment")) { + mixins.removeIf(it -> it.contains("devenv")); + } + + return mixins; + } + + /** + * Search through directory for mixin classes based on {@link #getMixinBaseDir}. + * + * @param classRoot The root directory in which classes are stored for the default package. + */ + private void walkDir(Path classRoot) { + System.out.println("Trying to find mixins from directory"); + try (Stream classes = Files.walk(classRoot.resolve(getMixinBaseDir()))) { + classes.map(it -> classRoot.relativize(it).toString()) + .forEach(this::tryAddMixinClass); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + + /** + * Read through a JAR file, trying to find all mixins inside. + */ + private void walkJar(Path file) { + System.out.println("Trying to find mixins from jar file"); + try (ZipInputStream zis = new ZipInputStream(Files.newInputStream(file))) { + ZipEntry next; + while ((next = zis.getNextEntry()) != null) { + tryAddMixinClass(next.getName()); + zis.closeEntry(); + } + } catch (IOException e) { + throw new RuntimeException(e); + } + } + + @Override + public void preApply(String targetClassName, ClassNode targetClass, String mixinClassName, IMixinInfo mixinInfo) { + + } + + @Override + public void postApply(String targetClassName, ClassNode targetClass, String mixinClassName, IMixinInfo mixinInfo) { + + } + + @Override + public String getRefMapperConfig() { + return null; + } + + @Override + public boolean shouldApplyMixin(String targetClassName, String mixinClassName) { + return true; + } + + @Override + public void acceptTargets(Set myTargets, Set otherTargets) { + + } } diff --git a/src/main/java/moe/nea/ledger/mixin/RegisterModResourcesPatch.java b/src/main/java/moe/nea/ledger/mixin/RegisterModResourcesPatch.java deleted file mode 100644 index e925d18..0000000 --- a/src/main/java/moe/nea/ledger/mixin/RegisterModResourcesPatch.java +++ /dev/null @@ -1,66 +0,0 @@ -package moe.nea.ledger.mixin; - -import com.google.common.eventbus.EventBus; -import net.minecraftforge.fml.client.FMLFileResourcePack; -import net.minecraftforge.fml.common.DummyModContainer; -import net.minecraftforge.fml.common.LoadController; -import net.minecraftforge.fml.common.ModContainer; -import net.minecraftforge.fml.common.ModMetadata; -import net.minecraftforge.fml.common.discovery.ASMDataTable; -import net.minecraftforge.fml.common.discovery.ContainerType; -import net.minecraftforge.fml.common.discovery.ModCandidate; -import net.minecraftforge.fml.common.discovery.ModDiscoverer; -import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.Shadow; -import org.spongepowered.asm.mixin.injection.At; -import org.spongepowered.asm.mixin.injection.Inject; -import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; - -import java.io.File; -import java.util.Collections; -import java.util.List; - -@Mixin(value = ModDiscoverer.class, remap = false) -public class RegisterModResourcesPatch { - @Shadow - private List candidates; - - @Inject(method = "identifyMods", at = @At("HEAD"), remap = false) - private void addCandidate(CallbackInfoReturnable> cir) { - String bonusResourceMod = System.getProperty("ledger.bonusresourcemod"); - if (bonusResourceMod == null) return; - File file = new File(bonusResourceMod); - if (!file.isDirectory()) return; - ModMetadata modMetadata = new ModMetadata(); - modMetadata.modId = "ledger-bonus"; - modMetadata.name = "Ledger Bonus Resources"; - modMetadata.autogenerated = true; - ModContainer container = new DummyModContainer(modMetadata) { - @Override - public Object getMod() { - return new Object(); - } - - @Override - public boolean registerBus(EventBus bus, LoadController controller) { - return true; - } - - @Override - public File getSource() { - return file; - } - - @Override - public Class getCustomResourcePackClass() { - return FMLFileResourcePack.class; - } - }; - candidates.add(new ModCandidate(file, file, ContainerType.DIR) { - @Override - public List explore(ASMDataTable table) { - return Collections.singletonList(container); - } - }); - } -} diff --git a/src/main/java/moe/nea/ledger/mixin/devenv/RegisterModResourcesPatch.java b/src/main/java/moe/nea/ledger/mixin/devenv/RegisterModResourcesPatch.java new file mode 100644 index 0000000..88e8364 --- /dev/null +++ b/src/main/java/moe/nea/ledger/mixin/devenv/RegisterModResourcesPatch.java @@ -0,0 +1,66 @@ +package moe.nea.ledger.mixin.devenv; + +import com.google.common.eventbus.EventBus; +import net.minecraftforge.fml.client.FMLFileResourcePack; +import net.minecraftforge.fml.common.DummyModContainer; +import net.minecraftforge.fml.common.LoadController; +import net.minecraftforge.fml.common.ModContainer; +import net.minecraftforge.fml.common.ModMetadata; +import net.minecraftforge.fml.common.discovery.ASMDataTable; +import net.minecraftforge.fml.common.discovery.ContainerType; +import net.minecraftforge.fml.common.discovery.ModCandidate; +import net.minecraftforge.fml.common.discovery.ModDiscoverer; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Shadow; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; + +import java.io.File; +import java.util.Collections; +import java.util.List; + +@Mixin(value = ModDiscoverer.class, remap = false) +public class RegisterModResourcesPatch { + @Shadow + private List candidates; + + @Inject(method = "identifyMods", at = @At("HEAD"), remap = false) + private void addCandidate(CallbackInfoReturnable> cir) { + String bonusResourceMod = System.getProperty("ledger.bonusresourcemod"); + if (bonusResourceMod == null) return; + File file = new File(bonusResourceMod); + if (!file.isDirectory()) return; + ModMetadata modMetadata = new ModMetadata(); + modMetadata.modId = "ledger-bonus"; + modMetadata.name = "Ledger Bonus Resources"; + modMetadata.autogenerated = true; + ModContainer container = new DummyModContainer(modMetadata) { + @Override + public Object getMod() { + return new Object(); + } + + @Override + public boolean registerBus(EventBus bus, LoadController controller) { + return true; + } + + @Override + public File getSource() { + return file; + } + + @Override + public Class getCustomResourcePackClass() { + return FMLFileResourcePack.class; + } + }; + candidates.add(new ModCandidate(file, file, ContainerType.DIR) { + @Override + public List explore(ASMDataTable table) { + return Collections.singletonList(container); + } + }); + } +} -- cgit