Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some improvements for LibraryManager #4152

Merged
merged 4 commits into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions build-logic/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ plugins {
indra {
javaVersions {
strictVersions(false) // it's just buildscript, no need for anything fancy
target(17)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,35 +54,20 @@
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.TreeMap;
import java.util.stream.Collectors;

public abstract class OutputDependenciesToJson extends DefaultTask {

// From http://stackoverflow.com/questions/9655181/convert-from-byte-array-to-hex-string-in-java
private static final char[] hexArray = "0123456789abcdef".toCharArray();
private static final char[] hexChars = "0123456789abcdef".toCharArray();

private static final Gson GSON = new GsonBuilder().setPrettyPrinting().create();

/**
* A single dependency.
*/
static final class DependencyDescriptor implements Comparable<DependencyDescriptor> {

final String group;
final String module;
final String version;
final String md5;

DependencyDescriptor(final String group, final String module, final String version, final String md5) {
this.group = group;
this.module = module;
this.version = version;
this.md5 = md5;
}

record DependencyDescriptor(String group, String module, String version, String sha512) implements Comparable<DependencyDescriptor> {
@Override
public int compareTo(final DependencyDescriptor that) {
final int group = this.group.compareTo(that.group);
Expand All @@ -97,35 +82,6 @@ public int compareTo(final DependencyDescriptor that) {

return this.version.compareTo(that.version);
}

@Override
public boolean equals(final Object other) {
if (this == other) {
return true;
}
if (other == null || this.getClass() != other.getClass()) {
return false;
}
final DependencyDescriptor that = (DependencyDescriptor) other;
return Objects.equals(this.group, that.group)
&& Objects.equals(this.module, that.module)
&& Objects.equals(this.version, that.version);
}

@Override
public int hashCode() {
return Objects.hash(this.group, this.module, this.version);
}

@Override
public String toString() {
return "DependencyDescriptor{" +
"group='" + this.group + '\'' +
", module='" + this.module + '\'' +
", version='" + this.version + '\'' +
", md5='" + this.md5 + '\'' +
'}';
}
}

/**
Expand All @@ -134,14 +90,7 @@ public String toString() {
* <p>At runtime, transitive dependencies won't be traversed, so this needs to
* include direct + transitive depends.</p>
*/
static final class DependencyManifest {

final Map<String, List<DependencyDescriptor>> dependencies;

DependencyManifest(final Map<String, List<DependencyDescriptor>> dependencies) {
this.dependencies = dependencies;
}
}
record DependencyManifest(Map<String, List<DependencyDescriptor>> dependencies) {}

/**
* Configuration to gather dependency artifacts from.
Expand Down Expand Up @@ -225,41 +174,42 @@ private List<DependencyDescriptor> configToDescriptor(final Set<ResolvedArtifact
.map(dependency -> {
final ModuleComponentIdentifier id = (ModuleComponentIdentifier) dependency.getId().getComponentIdentifier();

// Get file input stream for reading the file content
final String md5hash;
final MessageDigest digest;
try {
digest = MessageDigest.getInstance("SHA-512");
} catch (final NoSuchAlgorithmException e) {
throw new GradleException("Failed to find digest algorithm", e);
}

try (final InputStream in = Files.newInputStream(dependency.getFile().toPath())) {
final MessageDigest hasher = MessageDigest.getInstance("MD5");
final byte[] buf = new byte[4096];
int read;
while ((read = in.read(buf)) != -1) {
hasher.update(buf, 0, read);
digest.update(buf, 0, read);
}

md5hash = OutputDependenciesToJson.toHexString(hasher.digest());
} catch (final IOException | NoSuchAlgorithmException ex) {
throw new GradleException("Failed to create hash for " + dependency, ex);
} catch (final IOException e) {
throw new GradleException("Failed to digest file for " + dependency, e);
}

// create descriptor
return new DependencyDescriptor(
id.getGroup(),
id.getModule(),
id.getVersion(),
md5hash
OutputDependenciesToJson.toHexString(digest.digest())
);
})
.sorted(Comparator.naturalOrder()) // sort dependencies for stable output
.collect(Collectors.toList());
}

public static String toHexString(final byte[] bytes) {
final char[] hexChars = new char[bytes.length * 2];
for (int j = 0; j < bytes.length; j++) {
final int v = bytes[j] & 0xFF;
hexChars[j * 2] = OutputDependenciesToJson.hexArray[v >>> 4];
hexChars[j * 2 + 1] = OutputDependenciesToJson.hexArray[v & 0x0F];
final char[] chars = new char[bytes.length << 1];
int i = 0;
for (final byte b : bytes) {
chars[i++] = OutputDependenciesToJson.hexChars[(b >> 4) & 15];
chars[i++] = OutputDependenciesToJson.hexChars[b & 15];
}
return new String(hexChars);
return new String(chars);
}

public static class ConfigurationHolder {
Expand All @@ -274,7 +224,7 @@ public Provider<Set<String>> getIds() {
return this.getArtifacts().map(set -> set.stream()
.map(art -> art.getId().getComponentIdentifier())
.filter(id -> id instanceof ModuleComponentIdentifier)
.map(art -> art.getDisplayName())
.map(ComponentIdentifier::getDisplayName)
.collect(Collectors.toSet()));
}

Expand Down
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ idea {
(project as ExtensionAware).extensions["settings"].run {
(this as ExtensionAware).extensions.getByType(org.jetbrains.gradle.ext.TaskTriggersConfig::class).run {
afterSync(":modlauncher-transformers:build")
afterSync(":library-manager:build")
}
}
}
Expand Down
5 changes: 4 additions & 1 deletion forge/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ plugins {
}

val commonProject = parent!!
val transformersProject = parent!!.project(":modlauncher-transformers")
val transformersProject = commonProject.project(":modlauncher-transformers")
val libraryManagerProject = commonProject.project(":library-manager")
val testPluginsProject: Project? = rootProject.subprojects.find { "testplugins" == it.name }

val apiVersion: String by project
Expand Down Expand Up @@ -211,6 +212,7 @@ dependencies {
service(project(transformersProject.path)) {
exclude(group = "cpw.mods", module = "modlauncher")
}
service(project(libraryManagerProject.path))
service(platform(apiLibs.configurate.bom))
service(apiLibs.configurate.core) {
exclude(group = "org.checkerframework", module = "checker-qual")
Expand All @@ -232,6 +234,7 @@ dependencies {

val serviceShadedLibraries = serviceShadedLibrariesConfig.name
serviceShadedLibraries(project(transformersProject.path)) { isTransitive = false }
serviceShadedLibraries(project(libraryManagerProject.path)) { isTransitive = false }

val gameShadedLibraries = gameShadedLibrariesConfig.name
gameShadedLibraries("org.spongepowered:spongeapi:$apiVersion") { isTransitive = false }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@
import net.minecraftforge.forgespi.locating.IModLocator;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.spongepowered.forge.applaunch.loading.moddiscovery.library.LibraryManager;
import org.spongepowered.forge.applaunch.loading.moddiscovery.library.Log4JLogger;
import org.spongepowered.forge.applaunch.transformation.SpongeForgeTransformationService;
import org.spongepowered.libs.LibraryManager;

import java.nio.file.Path;
import java.util.ArrayList;
Expand All @@ -60,8 +61,8 @@ public List<IModFile> scanMods(Iterable<IModFile> loadedMods) {
}
this.libraryManager.finishedProcessing();

for (final LibraryManager.Library library : this.libraryManager.getAll().values()) {
final Path path = library.getFile();
for (final LibraryManager.Library library : this.libraryManager.getAll("main")) {
final Path path = library.file();
SpongeForgeDependencyLocator.LOGGER.debug("Proposing jar {} as a game library", path);

final IModLocator.ModFileOrException fileOrException = createMod(path);
Expand Down Expand Up @@ -89,6 +90,7 @@ public String name() {
public void initArguments(final Map<String, ?> arguments) {
final Environment env = Launcher.INSTANCE.environment();
this.libraryManager = new LibraryManager(
new Log4JLogger(LogManager.getLogger(LibraryManager.class)),
env.getProperty(SpongeForgeTransformationService.Keys.CHECK_LIBRARY_HASHES.get()).orElse(true),
env.getProperty(SpongeForgeTransformationService.Keys.LIBRARIES_DIRECTORY.get())
.orElseThrow(() -> new IllegalStateException("no libraries available")),
Expand Down

This file was deleted.

Loading
Loading