Skip to content

Commit

Permalink
Fix duplicated dependencies causing module resolution failure; Fixes #…
Browse files Browse the repository at this point in the history
  • Loading branch information
InitAuther97 committed Feb 17, 2025
1 parent 9499335 commit 291776a
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package io.izzel.arclight.boot.neoforge.mod;

import cpw.mods.jarhandling.impl.JarContentsImpl;
import io.izzel.arclight.api.Unsafe;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.lang.invoke.MethodHandles;
import java.lang.invoke.VarHandle;
import java.util.Set;
import java.util.stream.Collectors;

/**
* Filter out packages already provided by Arclight.
* For duplicate modules, the packages will be removed
* before modules are removed.
* For duplicate packages in non-duplicate library modules,
* only the packages will be removed.
* For duplicate packages in non-duplicate mod modules,
* the packages won't be removed
* Duplicated shaded mods are like modules, and are removed
* later by JarInJarFilter.
*/
public class ArclightJarContentsImplFilter {
// Use unsafe to bypass JPMS accessibility check
private static final MethodHandles.Lookup LOOKUP = Unsafe.lookup();
private static VarHandle PACKAGES;
private static Set<String> serviceLayerPackages;
private static final Logger LOGGER = LogManager.getLogger("Arclight");

static {
try {
PACKAGES = LOOKUP.findVarHandle(JarContentsImpl.class, "packages", Set.class);
} catch (ReflectiveOperationException e) {
LOGGER.error("Arclight failed to filter JarContents. This may cause dependency conflicts with some mods!", e);
}
serviceLayerPackages = ArclightJarContentsImplFilter.class
.getModule()
.getLayer()
.modules()
.stream()
.flatMap(it -> it.getPackages().stream())
.collect(Collectors.toSet());
}

/*
* The result of getPackages() is cached
* Through modifying the cache, we modify the result of getPackages()
* Note: ModJarMetadata use getPackagesExcluding(String...),
* which bypass the cache. This won't work for ModJarMetadata.
*/
public static void filter(JarContentsImpl impl) {
if (PACKAGES != null) {
impl.getPackages();
Set<String> raw = (Set<String>)PACKAGES.get(impl);
Set<String> result = raw.stream()
.filter(ArclightJarContentsImplFilter::test)
.collect(Collectors.toSet());
PACKAGES.set(impl, result);
}
}

public static boolean test(String pkg) {
return !serviceLayerPackages.contains(pkg);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@
import net.neoforged.neoforgespi.locating.IDependencyLocator;
import net.neoforged.neoforgespi.locating.IDiscoveryPipeline;
import net.neoforged.neoforgespi.locating.IModFile;
import net.neoforged.neoforgespi.locating.IOrderedProvider;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

public class ArclightJarInJarFilter implements IDependencyLocator {
public class ArclightJarInJarFilter implements IDependencyLocator, IOrderedProvider {

private static final Logger LOGGER = LoggerFactory.getLogger("ArclightJiJ");

Expand All @@ -35,4 +36,9 @@ public void scanMods(List<IModFile> loadedMods, IDiscoveryPipeline pipeline) {
public String toString() {
return "arclight_jij";
}

@Override
public int getPriority() {
return IOrderedProvider.LOWEST_SYSTEM_PRIORITY;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package io.izzel.arclight.boot.neoforge.mod;

import cpw.mods.jarhandling.JarContents;
import cpw.mods.jarhandling.impl.JarContentsImpl;
import net.neoforged.neoforgespi.locating.IModFile;
import net.neoforged.neoforgespi.locating.IModFileReader;
import net.neoforged.neoforgespi.locating.IOrderedProvider;
import net.neoforged.neoforgespi.locating.ModFileDiscoveryAttributes;
import org.jetbrains.annotations.Nullable;

public class ArclightModFileReader implements IModFileReader, IOrderedProvider {
@Override
public @Nullable IModFile read(JarContents jarContents, ModFileDiscoveryAttributes modFileDiscoveryAttributes) {
ArclightJarContentsImplFilter.filter((JarContentsImpl) jarContents);
return null;
}

@Override
public int getPriority() {
return HIGHEST_SYSTEM_PRIORITY;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
io.izzel.arclight.boot.neoforge.mod.ArclightModFileReader

0 comments on commit 291776a

Please sign in to comment.