plguin loading + logging

and some mixin work
This commit is contained in:
moehreag 2024-05-12 17:40:51 +02:00
parent 2f03504e49
commit 00c36a746c
10 changed files with 219 additions and 45 deletions

View file

@ -1,6 +1,6 @@
plugins {
id("java")
id("io.freefair.lombok").version("8.1.+")
id("io.freefair.lombok").version("8.+")
}
group = "org.example"
@ -14,6 +14,9 @@ repositories {
dependencies {
implementation("net.fabricmc:sponge-mixin:0.13.4+mixin.0.8.5")
implementation("org.apache.logging.log4j:log4j-slf4j2-impl:3.0.0-beta2")
implementation("org.apache.logging.log4j:log4j-api:3.0.0-beta2")
implementation("org.apache.logging.log4j:log4j-core:3.0.0-beta2")
}
tasks.test {

View file

@ -1,5 +1,5 @@
#Sat May 11 16:50:23 CEST 2024
distributionUrl=https\://services.gradle.org/distributions/gradle-8.5-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.7-bin.zip
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStorePath=wrapper/dists

View file

@ -5,16 +5,20 @@ import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import io.github.moehreag.nonsense.loader.api.Loader;
import io.github.moehreag.nonsense.loader.impl.plugin.NonsensePlugin;
import lombok.Getter;
import org.spongepowered.asm.launch.MixinBootstrap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.spongepowered.asm.service.IPropertyKey;
public class LoaderImpl implements Loader {
private static final String PLUGIN_SERVICE_FILE_LOCATION = "META-INF/nonsense.plugin.service";
private static final String PLUGIN_SERVICE_FILE_LOCATION = "META-INF/services/io.github.moehreag.nonsense.loader.impl.plugin.NonsensePlugin";
@Getter
private final String[] args;
@ -24,6 +28,11 @@ public class LoaderImpl implements Loader {
@Getter
private static LoaderImpl instance;
@Getter
private final Map<IPropertyKey, Object> globalProperties = new HashMap<>();
private Logger LOGGER = LoggerFactory.getLogger("Nonsense Loader");
@Getter
private final List<NonsensePlugin> plugins = new ArrayList<>();
@ -31,7 +40,6 @@ public class LoaderImpl implements Loader {
this.args = args;
this.env = env;
MixinBootstrap.init();
discoverPlugins();
}
@ -44,7 +52,7 @@ public class LoaderImpl implements Loader {
instance = new LoaderImpl(args, env);
}
private void discoverPlugins(){
private void discoverPlugins() {
List<String> classes = new ArrayList<>();
this.getClass().getClassLoader().resources(PLUGIN_SERVICE_FILE_LOCATION).forEach(url -> {
try (InputStream inputStream = url.openStream()) {
@ -62,17 +70,16 @@ public class LoaderImpl implements Loader {
// TODO error handling
throw new RuntimeException(e);
}
}).filter(c -> c.isAssignableFrom(NonsensePlugin.class)).forEach(c -> {
}).filter(NonsensePlugin.class::isAssignableFrom).forEach(c -> {
try {
MethodHandle ctor = MethodHandles.publicLookup().findConstructor(c, MethodType.methodType(void.class));
NonsensePlugin plugin = (NonsensePlugin) ctor.invoke();
plugins.add(plugin);
plugin.init();
} catch (Throwable e) {
throw new RuntimeException(e);
e.printStackTrace();
LOGGER.error("Failed to instantiate plugin: ", e);
}
});
}
}

View file

@ -0,0 +1,39 @@
package io.github.moehreag.nonsense.loader.impl.mixin;
import io.github.moehreag.nonsense.loader.impl.LoaderImpl;
import org.spongepowered.asm.service.IGlobalPropertyService;
import org.spongepowered.asm.service.IPropertyKey;
public class NonsenseGlobalPropertyService implements IGlobalPropertyService {
@Override
public IPropertyKey resolveKey(String name) {
return new IPropertyKey() {
@Override
public String toString() {
return name;
}
};
}
@SuppressWarnings("unchecked")
@Override
public <T> T getProperty(IPropertyKey key) {
return (T) LoaderImpl.getInstance().getGlobalProperties().get(key);
}
@Override
public void setProperty(IPropertyKey key, Object value) {
LoaderImpl.getInstance().getGlobalProperties().put(key, value);
}
@SuppressWarnings("unchecked")
@Override
public <T> T getProperty(IPropertyKey key, T defaultValue) {
return (T) LoaderImpl.getInstance().getGlobalProperties().getOrDefault(key, defaultValue);
}
@Override
public String getPropertyString(IPropertyKey key, String defaultValue) {
return LoaderImpl.getInstance().getGlobalProperties().getOrDefault(key, defaultValue).toString();
}
}

View file

@ -0,0 +1,45 @@
package io.github.moehreag.nonsense.loader.impl.mixin;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import org.spongepowered.asm.logging.ILogger;
import org.spongepowered.asm.logging.Level;
import org.spongepowered.asm.logging.LoggerAdapterAbstract;
public class NonsenseMixinLogger extends LoggerAdapterAbstract {
private static final Map<String, NonsenseMixinLogger> LOGGERS = new ConcurrentHashMap<>();
public static ILogger get(String name){
return LOGGERS.computeIfAbsent(name, NonsenseMixinLogger::new);
}
public NonsenseMixinLogger(String name){
super(name);
}
@Override
public String getType() {
return "Nonsense Loader/Mixin Logger";
}
@Override
public void catching(Level level, Throwable t) {
}
@Override
public void log(Level level, String message, Object... params) {
}
@Override
public void log(Level level, String message, Throwable t) {
}
@Override
public <T extends Throwable> T throwing(T t) {
return t;
}
}

View file

@ -1,18 +1,23 @@
package io.github.moehreag.nonsense.loader.impl.mixin;
import io.github.moehreag.nonsense.loader.impl.LoaderImpl;
import org.objectweb.asm.tree.ClassNode;
import org.spongepowered.asm.launch.platform.container.IContainerHandle;
import org.spongepowered.asm.logging.ILogger;
import org.spongepowered.asm.mixin.MixinEnvironment;
import org.spongepowered.asm.mixin.transformer.IMixinTransformer;
import org.spongepowered.asm.service.*;
import org.spongepowered.asm.util.ReEntranceLock;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Collection;
import java.util.List;
public class NonsenseMixinService implements IMixinService {
private final ReEntranceLock lock = new ReEntranceLock(2);
public class NonsenseMixinService implements IMixinService, IClassProvider, IClassBytecodeProvider, ITransformerProvider, IClassTracker {
static IMixinTransformer transformer;
private final ReEntranceLock lock = new ReEntranceLock(1);
@Override
public String getName() {
@ -61,22 +66,22 @@ public class NonsenseMixinService implements IMixinService {
@Override
public IClassProvider getClassProvider() {
return null;
return this;
}
@Override
public IClassBytecodeProvider getBytecodeProvider() {
return null;
return this;
}
@Override
public ITransformerProvider getTransformerProvider() {
return null;
return this;
}
@Override
public IClassTracker getClassTracker() {
return null;
return this;
}
@Override
@ -121,6 +126,67 @@ public class NonsenseMixinService implements IMixinService {
@Override
public ILogger getLogger(String name) {
return NonsenseMixinLogger.get(name);
}
@Override
public ClassNode getClassNode(String name) throws ClassNotFoundException, IOException {
return null;
}
@Override
public ClassNode getClassNode(String name, boolean runTransformers) throws ClassNotFoundException, IOException {
return null;
}
@Override
public URL[] getClassPath() {
return new URL[0];
}
@Override
public Class<?> findClass(String name) throws ClassNotFoundException {
return null;
}
@Override
public Class<?> findClass(String name, boolean initialize) throws ClassNotFoundException {
return null;
}
@Override
public Class<?> findAgentClass(String name, boolean initialize) throws ClassNotFoundException {
return null;
}
@Override
public void registerInvalidClass(String className) {
}
@Override
public boolean isClassLoaded(String className) {
return false;
}
@Override
public String getClassRestrictions(String className) {
return "";
}
@Override
public Collection<ITransformer> getTransformers() {
return List.of();
}
@Override
public Collection<ITransformer> getDelegatedTransformers() {
return List.of();
}
@Override
public void addTransformerExclusion(String name) {
}
}

View file

@ -1,6 +1,8 @@
package io.github.moehreag.nonsense.loader.impl.plugin;
import io.github.moehreag.nonsense.loader.impl.Env;
import io.github.moehreag.nonsense.loader.impl.mixin.NonsenseMixinService;
import org.spongepowered.asm.launch.MixinBootstrap;
import java.util.Collections;
import java.util.Map;
@ -18,4 +20,10 @@ public interface NonsensePlugin extends Runnable {
default void init(){
}
default void initMixin(){
System.setProperty("mixin.service", NonsenseMixinService.class.getName());
MixinBootstrap.init();
}
}

View file

@ -14,4 +14,9 @@ public class Minecraft implements NonsensePlugin {
Env.SERVER, "net.minecraft.server.main.Main"
);
}
@Override
public void init() {
initMixin();
}
}

View file

@ -0,0 +1 @@
io.github.moehreag.nonsense.loader.impl.mixin.NonsenseGlobalPropertyService