add mixinextras, fix property keys, add mod paths

- refactor some other things as well
This commit is contained in:
moehreag 2024-06-05 20:06:20 +02:00
parent 101dec4e14
commit 8f58e1601c
12 changed files with 99 additions and 36 deletions

View file

@ -31,10 +31,13 @@ dependencies {
compileOnly("org.apache.logging.log4j:log4j-core:3.0.0-beta2") compileOnly("org.apache.logging.log4j:log4j-core:3.0.0-beta2")
api(libs.mixin) api(libs.mixin)
api(libs.mixinextras)
implementation(libs.nightconfig) implementation(libs.nightconfig)
api(libs.annotations) api(libs.annotations)
} }
java { java {
sourceCompatibility = JavaVersion.VERSION_21 sourceCompatibility = JavaVersion.VERSION_21
targetCompatibility = JavaVersion.VERSION_21 targetCompatibility = JavaVersion.VERSION_21

View file

@ -2,8 +2,9 @@
thyroxine = "1.0.0-SNAPSHOT" thyroxine = "1.0.0-SNAPSHOT"
nightconfig = "3.7.2" nightconfig = "3.7.2"
mixin = "0.13.4+mixin.0.8.5" mixin = "0.14.0+mixin.0.8.6"
annotations = "24.1.0" annotations = "24.1.0"
mixinextras = "0.3.6"
[libraries] [libraries]
@ -11,6 +12,7 @@ thyroxine = { module = "dev.frogmc:thyroxine", version.ref = "thyroxine" }
nightconfig = { module = "com.electronwill.night-config:toml", version.ref = "nightconfig" } nightconfig = { module = "com.electronwill.night-config:toml", version.ref = "nightconfig" }
mixin = { module = "net.fabricmc:sponge-mixin", version.ref = "mixin" } mixin = { module = "net.fabricmc:sponge-mixin", version.ref = "mixin" }
annotations = { module = "org.jetbrains:annotations", version.ref = "annotations" } annotations = { module = "org.jetbrains:annotations", version.ref = "annotations" }
mixinextras = { module = "io.github.llamalad7:mixinextras-common", version.ref = "mixinextras" }
[bundles] [bundles]

View file

@ -4,6 +4,7 @@ import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles; import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType; import java.lang.invoke.MethodType;
import java.util.Map; import java.util.Map;
import java.util.Collection;
import java.util.function.Consumer; import java.util.function.Consumer;
import org.slf4j.Logger; import org.slf4j.Logger;
@ -88,28 +89,37 @@ public final class ModExtensions {
* @param action The action to run on the newly retrieved instance of the provided class * @param action The action to run on the newly retrieved instance of the provided class
* @param <T> The type of the class * @param <T> The type of the class
*/ */
@SuppressWarnings("unchecked") @SuppressWarnings({"rawtypes", "unchecked"})
public <T> void runIfPresent(String key, Class<T> type, Consumer<T> action) { public <T> void runIfPresent(String key, Class<T> type, Consumer<T> action) {
String value = get(key);
Object value = get(key);
if (value == null) { if (value == null) {
return; return;
} }
try { Consumer<String> c = s -> {
MethodHandle handle; try {
if (value.contains("::")) { MethodHandle handle;
String[] parts = value.split("::"); if (s.contains("::")) {
handle = MethodHandles.lookup().findVirtual(Class.forName(parts[0]), parts[1], MethodType.methodType(type)); String[] parts = s.split("::");
} else { handle = MethodHandles.lookup().findVirtual(Class.forName(parts[0]), parts[1], MethodType.methodType(type));
handle = MethodHandles.lookup().findConstructor(Class.forName(value), MethodType.methodType(void.class)); } else {
handle = MethodHandles.lookup().findConstructor(Class.forName(s), MethodType.methodType(void.class));
}
T object = (T) handle.invoke();
if (object != null) {
action.accept(object);
}
} catch (Throwable e) {
LOGGER.warn("Failed to instantiate Extension: ", e);
} }
T object = (T) handle.invoke(); };
if (object != null) {
action.accept(object); if (value instanceof String s){
} c.accept(s);
} catch (Throwable e) { } else if (value instanceof Collection l){
LOGGER.warn("Failed to instantiate Extension: ", e); ((Collection<String>) l).forEach(c);
} }
} }

View file

@ -1,5 +1,6 @@
package dev.frogmc.frogloader.api.mod; package dev.frogmc.frogloader.api.mod;
import java.nio.file.Path;
import java.util.Collection; import java.util.Collection;
import java.util.Map; import java.util.Map;
@ -76,5 +77,10 @@ public interface ModProperties {
*/ */
ModExtensions extensions(); ModExtensions extensions();
/**
* Get this mod's paths
* @return Where this mod is loaded from
*/
Collection<Path> paths();
} }

View file

@ -168,4 +168,4 @@ public class FrogLoaderImpl implements FrogLoader {
public Collection<ModProperties> getMods() { public Collection<ModProperties> getMods() {
return mods.values(); return mods.values();
} }
} }

View file

@ -6,6 +6,7 @@ import java.lang.invoke.MethodType;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import com.llamalad7.mixinextras.MixinExtrasBootstrap;
import dev.frogmc.frogloader.api.env.Env; import dev.frogmc.frogloader.api.env.Env;
import dev.frogmc.frogloader.impl.mixin.FrogMixinService; import dev.frogmc.frogloader.impl.mixin.FrogMixinService;
import lombok.Getter; import lombok.Getter;
@ -18,8 +19,8 @@ public class FrogLauncher {
private static FrogLauncher instance; private static FrogLauncher instance;
@Getter @Getter
private final MixinClassLoader targetClassLoader; private final MixinClassLoader targetClassLoader;
@Getter
private final Map<IPropertyKey, Object> globalProperties = new HashMap<>(); private static final Map<IPropertyKey, Object> globalProperties = new HashMap<>();
@Getter @Getter
private final Env env; private final Env env;
@ -41,6 +42,7 @@ public class FrogLauncher {
System.setProperty("mixin.service", FrogMixinService.class.getName()); System.setProperty("mixin.service", FrogMixinService.class.getName());
MixinBootstrap.init(); MixinBootstrap.init();
MixinExtrasBootstrap.init();
try { try {
Class<?> clazz = targetClassLoader.findClass("dev.frogmc.frogloader.impl.FrogLoaderImpl"); Class<?> clazz = targetClassLoader.findClass("dev.frogmc.frogloader.impl.FrogLoaderImpl");
@ -55,4 +57,16 @@ public class FrogLauncher {
public static void run(String[] args, Env env) { public static void run(String[] args, Env env) {
new FrogLauncher(args, env); new FrogLauncher(args, env);
} }
public void putProperty(IPropertyKey key, Object value){
globalProperties.put(key, value);
}
public Object getProperty(IPropertyKey key){
return globalProperties.get(key);
}
public Object getProperty(IPropertyKey key, Object defaultValue){
return globalProperties.getOrDefault(key, defaultValue);
}
} }

View file

@ -12,28 +12,41 @@ public class FrogGlobalPropertyService implements IGlobalPropertyService {
public String toString() { public String toString() {
return name; return name;
} }
@Override
public boolean equals(Object other){
if (other instanceof IPropertyKey k){
return name.equals(k.toString());
}
return false;
}
@Override
public int hashCode(){
return name.hashCode();
}
}; };
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
@Override @Override
public <T> T getProperty(IPropertyKey key) { public <T> T getProperty(IPropertyKey key) {
return (T) FrogLauncher.getInstance().getGlobalProperties().get(key); return (T) FrogLauncher.getInstance().getProperty(key);
} }
@Override @Override
public void setProperty(IPropertyKey key, Object value) { public void setProperty(IPropertyKey key, Object value) {
FrogLauncher.getInstance().getGlobalProperties().put(key, value); FrogLauncher.getInstance().putProperty(key, value);
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
@Override @Override
public <T> T getProperty(IPropertyKey key, T defaultValue) { public <T> T getProperty(IPropertyKey key, T defaultValue) {
return (T) FrogLauncher.getInstance().getGlobalProperties().getOrDefault(key, defaultValue); return (T) FrogLauncher.getInstance().getProperty(key, defaultValue);
} }
@Override @Override
public String getPropertyString(IPropertyKey key, String defaultValue) { public String getPropertyString(IPropertyKey key, String defaultValue) {
return FrogLauncher.getInstance().getGlobalProperties().getOrDefault(key, defaultValue).toString(); return FrogLauncher.getInstance().getProperty(key, defaultValue).toString();
} }
} }

View file

@ -56,7 +56,6 @@ public class FrogMixinService implements IMixinService, IClassProvider, IClassBy
@Override @Override
public void init() { public void init() {
} }
@Override @Override

View file

@ -25,7 +25,7 @@ public class JavaModProperties {
"", "",
Map.of(System.getProperty("java.vm.vendor"), Collections.singleton("Vendor")), Map.of(System.getProperty("java.vm.vendor"), Collections.singleton("Vendor")),
new ModDependencies(Collections.emptySet(), Collections.emptySet(), Collections.emptySet(), Collections.emptySet()), new ModDependencies(Collections.emptySet(), Collections.emptySet(), Collections.emptySet(), Collections.emptySet()),
ModExtensions.of(Collections.emptyMap())); ModExtensions.of(Collections.emptyMap()), Collections.emptySet());
} }
return INSTANCE; return INSTANCE;
} }

View file

@ -1,5 +1,6 @@
package dev.frogmc.frogloader.impl.mod; package dev.frogmc.frogloader.impl.mod;
import java.nio.file.Path;
import java.util.Collection; import java.util.Collection;
import java.util.Map; import java.util.Map;
@ -10,5 +11,5 @@ import dev.frogmc.frogloader.api.mod.SemVer;
public record ModPropertiesImpl(String id, String name, String icon, SemVer version, String license, public record ModPropertiesImpl(String id, String name, String icon, SemVer version, String license,
Map<String, Collection<String>> credits, ModDependencies dependencies, Map<String, Collection<String>> credits, ModDependencies dependencies,
ModExtensions extensions) implements ModProperties { ModExtensions extensions, Collection<Path> paths) implements ModProperties {
} }

View file

@ -30,7 +30,7 @@ public class ModPropertiesReader {
try (FileSystem fs = FileSystems.newFileSystem(mod)) { try (FileSystem fs = FileSystems.newFileSystem(mod)) {
CommentedConfig props = PARSER.parse(fs.getPath(PROPERTIES_FILE_NAME), FileNotFoundAction.THROW_ERROR); CommentedConfig props = PARSER.parse(fs.getPath(PROPERTIES_FILE_NAME), FileNotFoundAction.THROW_ERROR);
return Optional.of(readProperties(props)); return Optional.of(readProperties(props, Collections.singleton(mod)));
} catch (IOException e) { } catch (IOException e) {
LOGGER.warn("Failed to read mod properties: ", e); LOGGER.warn("Failed to read mod properties: ", e);
} }
@ -38,17 +38,31 @@ public class ModPropertiesReader {
return Optional.empty(); return Optional.empty();
} }
public static ModProperties readFile(URL in) { public static Optional<ModProperties> readFile(URL in) {
CommentedConfig props = PARSER.parse(in); CommentedConfig props = PARSER.parse(in);
return readProperties(props); try {
String url = in.toString();
Path source = Path.of(url.substring(url.lastIndexOf(":")+1).split("!")[0]).toAbsolutePath();
if (!source.getFileName().toString().endsWith(".jar")){
source = source.getParent();
} else {
// TODO will this result in a memory leak?
FileSystem fs = FileSystems.newFileSystem(source);
source = fs.getPath("/");
}
return Optional.of(readProperties(props, Collections.singleton(source)));
} catch (Exception e) {
LOGGER.warn("Failed to read mod properties from "+in+": ", e);
}
return Optional.empty();
} }
private static ModProperties readProperties(UnmodifiableConfig config) { private static ModProperties readProperties(UnmodifiableConfig config, Collection<Path> sources) {
String manifestVer = config.get("frog.format_version"); String manifestVer = config.get("frog.format_version");
try { try {
return FormatVersion.get(manifestVer).parse(config); return FormatVersion.get(manifestVer).parse(config, sources);
} catch (IOException e) { } catch (IOException e) {
throw new UncheckedIOException(e); throw new UncheckedIOException(e);
} }
@ -56,7 +70,7 @@ public class ModPropertiesReader {
@AllArgsConstructor @AllArgsConstructor
private enum FormatVersion { private enum FormatVersion {
V1_0_0("1.0.0", config -> { V1_0_0("1.0.0", (config, sources) -> {
String id = config.get("frog.mod.id"); String id = config.get("frog.mod.id");
String name = config.get("frog.mod.name"); String name = config.get("frog.mod.name");
String icon = config.get("frog.mod.icon"); String icon = config.get("frog.mod.icon");
@ -103,7 +117,8 @@ public class ModPropertiesReader {
extensionsConfig.entrySet().forEach(entry -> extensions.put(entry.getKey(), entry.getValue())); extensionsConfig.entrySet().forEach(entry -> extensions.put(entry.getKey(), entry.getValue()));
} }
return new ModPropertiesImpl(id, name, icon, SemVerImpl.parse(version), license, Collections.unmodifiableMap(credits), new ModDependencies(depends, breaks, suggests, provides), ModExtensions.of(extensions)); return new ModPropertiesImpl(id, name, icon, SemVerImpl.parse(version), license, Collections.unmodifiableMap(credits),
new ModDependencies(depends, breaks, suggests, provides), ModExtensions.of(extensions), sources);
}); });
private static final Map<String, Parser> versions = Arrays.stream(values()).collect(Collectors.toMap(v -> v.version, v -> v.parser)); private static final Map<String, Parser> versions = Arrays.stream(values()).collect(Collectors.toMap(v -> v.version, v -> v.parser));
@ -116,7 +131,7 @@ public class ModPropertiesReader {
@FunctionalInterface @FunctionalInterface
public interface Parser { public interface Parser {
ModProperties parse(UnmodifiableConfig config) throws IOException; ModProperties parse(UnmodifiableConfig config, Collection<Path> sources) throws IOException;
} }
} }
} }

View file

@ -69,14 +69,14 @@ public class Minecraft implements FrogPlugin {
MinecraftSemVerImpl.get(version), "MC-EULA", MinecraftSemVerImpl.get(version), "MC-EULA",
Map.of("Mojang AB", Collections.singleton("Author")), Map.of("Mojang AB", Collections.singleton("Author")),
new ModDependencies(Collections.emptySet(), Collections.emptySet(), Collections.emptySet(), Collections.emptySet()), new ModDependencies(Collections.emptySet(), Collections.emptySet(), Collections.emptySet(), Collections.emptySet()),
ModExtensions.of(Collections.emptyMap()))); ModExtensions.of(Collections.emptyMap()), Collections.emptySet()));
Collection<Path> mods = Discovery.find(loader.getModsDir(), path -> Collection<Path> mods = Discovery.find(loader.getModsDir(), path ->
version.equals(path.getFileName().toString()), path -> version.equals(path.getFileName().toString()), path ->
path.getFileName().toString().endsWith(FrogLoaderImpl.MOD_FILE_EXTENSION)); path.getFileName().toString().endsWith(FrogLoaderImpl.MOD_FILE_EXTENSION));
Collection<URL> classpathMods = this.getClass().getClassLoader().resources(ModPropertiesReader.PROPERTIES_FILE_NAME).distinct().toList(); Collection<URL> classpathMods = this.getClass().getClassLoader().resources(ModPropertiesReader.PROPERTIES_FILE_NAME).distinct().toList();
classpathMods.stream().map(ModPropertiesReader::readFile).forEachOrdered(modProperties::add); classpathMods.stream().map(ModPropertiesReader::readFile).map(o -> o.orElse(null)).filter(Objects::nonNull).forEach(modProperties::add);
Map<Path, ModProperties> modPaths = new HashMap<>(); Map<Path, ModProperties> modPaths = new HashMap<>();
for (Path mod : new HashSet<>(mods)) { for (Path mod : new HashSet<>(mods)) {
findJiJMods(mod, mods, modPaths); findJiJMods(mod, mods, modPaths);