[base] Prematurely update java-commons, attempt to provide ASM-based workaround for existing uses

This commit is contained in:
Johannes Frohnmeyer 2022-06-21 19:49:24 +02:00
parent cebb022838
commit 3155392ed3
Signed by: Johannes
GPG Key ID: E76429612C2929F4
11 changed files with 82 additions and 66 deletions

View File

@ -13,4 +13,4 @@ modrinth_optional_dependencies=P7dR8mSH
curseforge_id=482600
curseforge_optional_dependencies=fabric-api
commons_version=2022.6.7+17-43-1
commons_version=2022.6.21+16-38-52

View File

@ -5,4 +5,10 @@ dependencies {
shadow "io.gitlab.jfronny:commons:$rootProject.commons_version"
shadow "io.gitlab.jfronny:commons-gson:$rootProject.commons_version"
shadow "io.gitlab.jfronny:commons-slf4j:$rootProject.commons_version"
}
shadowJar {
dependencies {
exclude(dependency('org.slf4j:slf4j-api'))
}
}

View File

@ -3,7 +3,6 @@ package io.gitlab.jfronny.libjf.data.manipulation.api;
import io.gitlab.jfronny.commons.LazySupplier;
import io.gitlab.jfronny.commons.throwable.*;
import io.gitlab.jfronny.libjf.LibJf;
import io.gitlab.jfronny.libjf.data.manipulation.impl.JLazySupplier;
import io.gitlab.jfronny.libjf.data.manipulation.impl.ResourcePackHook;
import net.fabricmc.fabric.api.event.Event;
import net.fabricmc.fabric.api.event.EventFactory;
@ -74,7 +73,7 @@ public class UserResourceEvents {
public static final Event<OpenRoot> OPEN_ROOT = EventFactory.createArrayBacked(OpenRoot.class,
(listeners) -> ((fileName, previous, pack) -> {
JLazySupplier<InputStream> lazy = new JLazySupplier<>(previous);
LazySupplier<InputStream> lazy = new LazySupplier<>(previous);
for (OpenRoot listener : listeners) {
lazy = lazy.andThen(supplier -> {
try {

View File

@ -1,59 +0,0 @@
package io.gitlab.jfronny.libjf.data.manipulation.impl;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Objects;
import java.util.function.Function;
import java.util.function.Supplier;
/**
* @deprecated Use java-commons LazySupplier instead once that is included in libjf-base
*/
@Deprecated
//TODO remove this once I update libjf-base
public class JLazySupplier<T> implements Supplier<T> {
private final Supplier<T> supplier;
private T cache = null;
private boolean initialized;
/**
* Create a lazy supplier from a pre-initialized value.
* The backing supplier will be empty and never called.
* @param value The value to initialize the cache with
*/
public JLazySupplier(@Nullable T value) {
this.supplier = () -> {
throw new RuntimeException("Supplier should have never been called");
};
initialized = true;
cache = value;
}
/**
* Create a lazy supplier backed with the specified supplier.
* It will at most be called once when this supplier is first used.
* @param supplier The backing supplier
*/
public JLazySupplier(@NotNull Supplier<T> supplier) {
this.supplier = Objects.requireNonNull(supplier);
initialized = false;
}
@Override @Contract(pure = true) @NotNull public T get() {
if (!initialized) cache = supplier.get();
return cache;
}
/**
* Generates a new lazy supplier from a function.
* This function is provided with this lazy supplier as a data source for transformation.
* Allows complex transformations to not be run if a later supplier has a shortcut
* @param after A backing function for the new lazy supplier
* @return A new lazy supplier
*/
@Contract(pure = true) @NotNull public JLazySupplier<T> andThen(@NotNull Function<JLazySupplier<T>, T> after) {
return new JLazySupplier<>(() -> after.apply(this));
}
}

View File

@ -0,0 +1,5 @@
archivesBaseName = "libjf-legacy-v0"
dependencies {
moduleDependencies(project, ["libjf-base", "libjf-unsafe-v0"])
}

View File

@ -0,0 +1,32 @@
package io.gitlab.jfronny.libjf.legacy;
import io.gitlab.jfronny.libjf.unsafe.asm.AsmConfig;
import io.gitlab.jfronny.libjf.unsafe.asm.patch.Patch;
import org.objectweb.asm.Opcodes;
import org.objectweb.asm.tree.AbstractInsnNode;
import org.objectweb.asm.tree.MethodInsnNode;
import org.objectweb.asm.tree.MethodNode;
import java.util.Set;
public class LegacyPatcher implements AsmConfig {
@Override
public Set<String> skipClasses() {
return Set.of();
}
@Override
public Set<Patch> getPatches() {
return Set.of(klazz -> {
if (!klazz.name.startsWith("io/gitlab/jfronny/")) return;
for (MethodNode method : klazz.methods) {
for (AbstractInsnNode insn : method.instructions) {
if (!(insn instanceof MethodInsnNode node)) continue;
if (!node.owner.equals("io/gitlab/jfronny/commons/log/Logger")) continue;
node.itf = false;
if (node.getOpcode() == Opcodes.INVOKEINTERFACE) node.setOpcode(Opcodes.INVOKEVIRTUAL);
}
}
});
}
}

View File

@ -0,0 +1,32 @@
{
"schemaVersion": 1,
"id": "libjf-legacy-v0",
"name": "LibJF Legacy Compatibility",
"version": "${version}",
"authors": [
"JFronny"
],
"contact": {
"website": "https://jfronny.gitlab.io",
"repo": "https://gitlab.com/jfmods/libjf"
},
"license": "MIT",
"environment": "*",
"entrypoints": {
"libjf:asm": [
"io.gitlab.jfronny.libjf.legacy.LegacyPatcher"
]
},
"depends": {
"fabricloader": ">=0.12.0",
"minecraft": "*",
"libjf-base": ">=${version}",
"libjf-unsafe-v0": ">=${version}"
},
"custom": {
"modmenu": {
"parent": "libjf",
"badges": ["library"]
}
}
}

View File

@ -1,10 +1,10 @@
package io.gitlab.jfronny.libjf.unsafe;
import io.gitlab.jfronny.commons.log.Logger;
import io.gitlab.jfronny.commons.log.LogStrategy;
import net.fabricmc.loader.impl.util.log.Log;
import net.fabricmc.loader.impl.util.log.LogCategory;
public class FLLogger implements Logger {
public class FLLogger implements LogStrategy {
private final LogCategory category;
public FLLogger(String context, String... names) {

View File

@ -17,7 +17,7 @@ public class JfLanguageAdapter implements LanguageAdapter {
public native <T> T create(net.fabricmc.loader.api.ModContainer mod, String value, Class<T> type);
static {
Logger.registerFactory(FLLogger::new); // Reset in SafeLog entrypoint
Logger.updateStrategy(FLLogger::new); // Reset in SafeLog entrypoint
Set<Flags.BooleanFlag> flags = Flags.getBoolFlags("unsafe.unlock");
if (flags.stream().map(Flags.BooleanFlag::value).reduce(false, (left, right) -> left || right)) {
SafeLog.warn("Unlocking classpath due to: " + flags.stream().map(Flags.BooleanFlag::source).collect(Collectors.joining(", ")));

View File

@ -28,7 +28,7 @@ public class SafeLog implements ModInitializer {
@Override
public void onInitialize() {
Logger.resetFactory();
Logger.resetStrategy();
BACKEND = Logger.forName("libjf");
}
}

View File

@ -16,6 +16,7 @@ include 'libjf-config-v0'
include 'libjf-data-v0'
include 'libjf-data-manipulation-v0'
include 'libjf-devutil-v0'
include 'libjf-legacy-v0'
include 'libjf-translate-v1'
include 'libjf-unsafe-v0'
include 'libjf-web-v0'