Skip to content

Commit

Permalink
Merge branch 'dev/1.4' into dev/1.5
Browse files Browse the repository at this point in the history
  • Loading branch information
Juuxel committed Jan 24, 2024
2 parents 275cf41 + fcd0701 commit 5135aaa
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 92 deletions.
2 changes: 2 additions & 0 deletions gradle/runtime.libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ access-transformers-new = "8.0.5"
unprotect = "1.2.0"
asm = "9.3"
union-relauncher = "1.0.0"
access-transformers-log4j = "2.17.1"

[libraries]
# Decompilers
Expand All @@ -43,3 +44,4 @@ access-transformers-new = { module = "net.minecraftforge:accesstransformers", ve
unprotect = { module = "io.github.juuxel:unprotect", version.ref = "unprotect" }
asm = { module = "org.ow2.asm:asm", version.ref = "asm" }
union-relauncher = { module = "io.github.juuxel:union-relauncher", version.ref = "union-relauncher" }
access-transformers-log4j-bom = { module = "org.apache.logging.log4j:log4j-bom", version.ref = "access-transformers-log4j" }
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ public static void executeAt(Project project, Path input, Path output, AccessTra
FileCollection classpath = new DependencyDownloader(project)
.add((serverBundleMetadataPresent ? LoomVersions.ACCESS_TRANSFORMERS_NEW : LoomVersions.ACCESS_TRANSFORMERS).mavenNotation())
.add(LoomVersions.ASM.mavenNotation())
.platform(LoomVersions.ACCESS_TRANSFORMERS_LOG4J_BOM.mavenNotation())
.download();
List<String> args = new ArrayList<>();
args.add("--inJar");
Expand Down
74 changes: 22 additions & 52 deletions src/main/java/net/fabricmc/loom/util/DependencyDownloader.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* This file is part of fabric-loom, licensed under the MIT License (MIT).
*
* Copyright (c) 2021-2024 FabricMC
* Copyright (c) 2021-2023 FabricMC
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -35,25 +35,17 @@
import org.gradle.api.Project;
import org.gradle.api.artifacts.Configuration;
import org.gradle.api.artifacts.Dependency;
import org.gradle.api.artifacts.DependencyResolveDetails;
import org.gradle.api.artifacts.ModuleDependency;
import org.gradle.api.artifacts.ModuleVersionSelector;
import org.gradle.api.artifacts.dsl.DependencyHandler;
import org.gradle.api.attributes.Attribute;
import org.gradle.api.file.FileCollection;
import org.jetbrains.annotations.VisibleForTesting;

/**
* Simplified but powerful dependency downloading.
*
* @author Juuz
*/
public final class DependencyDownloader {
private static final String LOG4J_GROUP = "org.apache.logging.log4j";
private static final String LOG4J_NAME = "log4j-core";
private static final String LOG4J_MINIMUM_VERSION = "2.17.1";
private static final int[] LOG4J_MINIMUM_VERSION_COMPONENTS = {2, 17, 1};

private final Project project;
private final List<DependencyEntry> dependencies = new ArrayList<>();
private final Map<Attribute<?>, Object> attributes = new HashMap<>();
Expand All @@ -73,6 +65,17 @@ public DependencyDownloader add(String dependencyNotation) {
return this;
}

/**
* Adds a platform dependency.
*
* @param dependencyNotation the dependency notation
* @return this downloader
*/
public DependencyDownloader platform(String dependencyNotation) {
dependencies.add(new DependencyEntry.Platform(dependencyNotation));
return this;
}

/**
* Adds all dependencies from a configuration to download.
*
Expand Down Expand Up @@ -141,57 +144,17 @@ public FileCollection download(boolean transitive, boolean resolve) {
attributes.attribute((Attribute<Object>) attribute, value);
});
});
config.getResolutionStrategy().eachDependency(DependencyDownloader::upgradeLog4j);
FileCollection files = config.fileCollection(dep -> true);

if (resolve) {
files = project.files(files.getFiles());
}

return files;
}

private static void upgradeLog4j(DependencyResolveDetails details) {
ModuleVersionSelector requested = details.getRequested();

if (LOG4J_GROUP.equals(requested.getGroup()) && LOG4J_NAME.equals(requested.getName())) {
final String requestedVersion = requested.getVersion();

if (requestedVersion != null && shouldUpgradeLog4jVersion(requestedVersion)) {
details.useVersion(LOG4J_MINIMUM_VERSION);
for (File file : files) {
System.out.println(file.getAbsolutePath());
}
}
}

@VisibleForTesting
public static boolean shouldUpgradeLog4jVersion(String requestedVersion) {
final String[] splitVersion = requestedVersion.split("\\.");

for (int i = 0; i < LOG4J_MINIMUM_VERSION_COMPONENTS.length; i++) {
if (i >= splitVersion.length) {
// Not enough version components in the requested version, upgrade just to be sure.
return true;
}

final int minimumComponent = LOG4J_MINIMUM_VERSION_COMPONENTS[i];
final String givenComponentStr = splitVersion[i];
final int givenComponent;

try {
givenComponent = Integer.parseInt(givenComponentStr);
} catch (NumberFormatException e) {
// We can't read the version component for comparing, upgrade just to be sure.
return true;
}

if (givenComponent < minimumComponent) {
// Too old, upgrade.
return true;
}
}

// Seems to be new enough, let's not upgrade.
return false;
return files;
}

/**
Expand Down Expand Up @@ -242,6 +205,13 @@ public Dependency getDependency(DependencyHandler dependencies, boolean transiti
}
}

record Platform(String notation) implements DependencyEntry {
@Override
public Dependency getDependency(DependencyHandler dependencies, boolean transitive) {
return dependencies.platform(notation);
}
}

record Direct(Dependency dependency) implements DependencyEntry {
@Override
public Dependency getDependency(DependencyHandler dependencies, boolean transitive) {
Expand Down

This file was deleted.

0 comments on commit 5135aaa

Please sign in to comment.