diff options
author | Linnea Gräf <nea@nea.moe> | 2024-02-22 21:23:38 +0100 |
---|---|---|
committer | Linnea Gräf <nea@nea.moe> | 2024-02-22 21:23:38 +0100 |
commit | e8196b0b5e02ec21f75109ab7aba8d48220c8d05 (patch) | |
tree | d612293d68050e75385ed9c3cd46724025b8501c /src | |
download | Kowalski-e8196b0b5e02ec21f75109ab7aba8d48220c8d05.tar.gz Kowalski-e8196b0b5e02ec21f75109ab7aba8d48220c8d05.tar.bz2 Kowalski-e8196b0b5e02ec21f75109ab7aba8d48220c8d05.zip |
Template
Diffstat (limited to 'src')
-rw-r--r-- | src/main/java/moe/nea/kowalski/init/AutoDiscoveryMixinPlugin.java | 188 | ||||
-rw-r--r-- | src/main/kotlin/moe/nea/kowalski/ExampleMod.kt | 22 | ||||
-rw-r--r-- | src/main/kotlin/moe/nea/kowalski/mixin/MixinGuiMainMenu.java | 16 | ||||
-rw-r--r-- | src/main/resources/assets/test/test.txt | 3 | ||||
-rw-r--r-- | src/main/resources/mcmod.info | 18 | ||||
-rw-r--r-- | src/main/resources/mixins.kowalski.json | 8 |
6 files changed, 255 insertions, 0 deletions
diff --git a/src/main/java/moe/nea/kowalski/init/AutoDiscoveryMixinPlugin.java b/src/main/java/moe/nea/kowalski/init/AutoDiscoveryMixinPlugin.java new file mode 100644 index 0000000..715c41f --- /dev/null +++ b/src/main/java/moe/nea/kowalski/init/AutoDiscoveryMixinPlugin.java @@ -0,0 +1,188 @@ +package moe.nea.kowalski.init; + +import org.spongepowered.asm.lib.tree.ClassNode; +import org.spongepowered.asm.mixin.extensibility.IMixinConfigPlugin; +import org.spongepowered.asm.mixin.extensibility.IMixinInfo; + +import java.io.IOException; +import java.net.MalformedURLException; +import java.net.URISyntaxException; +import java.net.URL; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.ArrayList; +import java.util.List; +import java.util.Set; +import java.util.stream.Stream; +import java.util.zip.ZipEntry; +import java.util.zip.ZipInputStream; + +/** + * A mixin plugin to automatically discover all mixins in the current JAR. + * <p> + * This mixin plugin automatically scans your entire JAR (or class directory, in case of an in-IDE launch) for classes inside of your + * mixin package and registers those. It does this recursively for sub packages of the mixin package as well. This means you will need + * to only have mixin classes inside of your mixin package, which is good style anyway. + * + * @author Linnea Gräf + */ +public class AutoDiscoveryMixinPlugin implements IMixinConfigPlugin { + private static final List<AutoDiscoveryMixinPlugin> mixinPlugins = new ArrayList<>(); + + public static List<AutoDiscoveryMixinPlugin> 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<String> 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. + * <p><b>This method cannot be called after mixin initialization.</p> + * + * @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<String> 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<Path> 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<String> myTargets, Set<String> otherTargets) { + + } +} diff --git a/src/main/kotlin/moe/nea/kowalski/ExampleMod.kt b/src/main/kotlin/moe/nea/kowalski/ExampleMod.kt new file mode 100644 index 0000000..a622a6c --- /dev/null +++ b/src/main/kotlin/moe/nea/kowalski/ExampleMod.kt @@ -0,0 +1,22 @@ +package moe.nea.kowalski + +import net.minecraft.client.Minecraft +import net.minecraft.init.Blocks +import net.minecraftforge.fml.common.Mod +import net.minecraftforge.fml.common.event.FMLInitializationEvent + +@Mod(modid = "kowalski", useMetadata = true) +class ExampleMod { + @Mod.EventHandler + fun init(event: FMLInitializationEvent) { + try { + val resource: net.minecraft.client.resources.IResource = Minecraft.getMinecraft().getResourceManager() + .getResource(net.minecraft.util.ResourceLocation("test:test.txt")) + org.apache.commons.io.IOUtils.copy(resource.getInputStream(), java.lang.System.out) + } catch (e: java.io.IOException) { + throw java.lang.RuntimeException(e) + } + + println("Dirt: ${Blocks.dirt.unlocalizedName}") + } +} diff --git a/src/main/kotlin/moe/nea/kowalski/mixin/MixinGuiMainMenu.java b/src/main/kotlin/moe/nea/kowalski/mixin/MixinGuiMainMenu.java new file mode 100644 index 0000000..4ddc1b4 --- /dev/null +++ b/src/main/kotlin/moe/nea/kowalski/mixin/MixinGuiMainMenu.java @@ -0,0 +1,16 @@ +package moe.nea.kowalski.mixin; + +import net.minecraft.client.gui.GuiMainMenu; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; + +@Mixin(GuiMainMenu.class) +public class MixinGuiMainMenu { + + @Inject(method = "initGui", at = @At("HEAD")) + public void onInitGui(CallbackInfo ci) { + System.out.println("Hello from Main Menu!"); + } +} diff --git a/src/main/resources/assets/test/test.txt b/src/main/resources/assets/test/test.txt new file mode 100644 index 0000000..ea839e2 --- /dev/null +++ b/src/main/resources/assets/test/test.txt @@ -0,0 +1,3 @@ +test +from resource pack + diff --git a/src/main/resources/mcmod.info b/src/main/resources/mcmod.info new file mode 100644 index 0000000..ab6d5dd --- /dev/null +++ b/src/main/resources/mcmod.info @@ -0,0 +1,18 @@ +[ + { + "modid": "${modid}", + "name": "Xample Mod", + "description": "A mod that is used as an example.", + "version": "${version}", + "mcversion": "${mcversion}", + "url": "https://github.com/romangraef/Forge1.8.9Template/", + "updateUrl": "", + "authorList": [ + "You" + ], + "credits": "", + "logoFile": "", + "screenshots": [], + "dependencies": [] + } +]
\ No newline at end of file diff --git a/src/main/resources/mixins.kowalski.json b/src/main/resources/mixins.kowalski.json new file mode 100644 index 0000000..5ea0c57 --- /dev/null +++ b/src/main/resources/mixins.kowalski.json @@ -0,0 +1,8 @@ +{ + "package": "${basePackage}.mixin", + "plugin": "${basePackage}.init.AutoDiscoveryMixinPlugin", + "refmap": "mixins.${modid}.refmap.json", + "minVersion": "0.7", + "compatibilityLevel": "JAVA_8", + "__comment": "You do not need to manually register mixins in this template. Check the auto discovery mixin plugin for more info." +} |