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

Fix injection logic for NeoForm classes #156

Merged
merged 1 commit into from
Apr 28, 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
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,13 @@ plugins {

dependencies {
// For depending on a Minecraft JAR-file with both client- and server-classes
implementation "net.minecraft:neoform_joined:<neoform-version>'
implementation "net.minecraft:neoform_joined:<neoform-version>"

// For depending on the Minecraft client JAR-file
implementation "net.minecraft:neoform_client:<neoform-version>'
implementation "net.minecraft:neoform_client:<neoform-version>"

// For depending on the Minecraft dedicated server JAR-file
implementation "net.minecraft:neoform_server:<neoform-version>'
implementation "net.minecraft:neoform_server:<neoform-version>"
}
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ public DefaultRuntime() {
Map.Entry::getKey,
entry -> getNeoFormArchive()
.getAsFileTree()
.matching(archive -> archive.include(entry.getValue())
)))));
.matching(archive -> archive.include(entry.getValue()))
))));

getOutputDirectory().finalizeValueOnRead();
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ private static TaskProvider<? extends WithOutput> createBuiltIn(final NeoFormRun
}
})
);
fileTreeSource.getTreePrefix().set(task.getSymbolicDataSources().map(data -> data.get("inject")));
return fileTreeSource;
})
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
import net.neoforged.gradle.util.ZipBuildingFileTreeVisitor;
import org.gradle.api.file.ConfigurableFileCollection;
import org.gradle.api.file.FileTree;
import org.gradle.api.tasks.InputFiles;
import org.gradle.api.tasks.PathSensitive;
import org.gradle.api.tasks.PathSensitivity;
import org.gradle.api.file.FileVisitDetails;
import org.gradle.api.provider.Property;
import org.gradle.api.tasks.*;
import org.jetbrains.annotations.Nullable;

import java.io.IOException;
import java.nio.file.Files;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

/**
Expand All @@ -22,10 +23,15 @@ public abstract class InjectFromFileTreeSource extends AbstractInjectSource {
@PathSensitive(PathSensitivity.NONE)
public abstract ConfigurableFileCollection getFiles();

@Optional
@Input
public abstract Property<String> getTreePrefix();

@Override
public byte @Nullable [] tryReadFile(String path) throws IOException {
final String lookupPath = buildTreePrefix() + path;
final FileTree matching = getFiles().getAsFileTree().matching(createFilter())
.matching(pattern -> pattern.include(path));
.matching(pattern -> pattern.include(lookupPath));
if (matching.isEmpty()) {
return null;
}
Expand All @@ -39,6 +45,42 @@ public void copyTo(ZipOutputStream out) throws IOException {
}

final FileTree source = getFiles().getAsFileTree().matching(createFilter());
source.visit(new ZipBuildingFileTreeVisitor(out));
source.visit(new ZipBuildingFileTreeVisitor(out) {
@Override
protected ZipEntry createDirectory(FileVisitDetails fileVisitDetails) {
String path = fileVisitDetails.getRelativePath().getPathString();
if (path.startsWith(buildTreePrefix())) {
path = path.substring(buildTreePrefix().length());
}

if (!path.endsWith("/")) {
path += "/";
}

return new ZipEntry(path);
}

@Override
protected ZipEntry createFile(FileVisitDetails fileVisitDetails) {
String path = fileVisitDetails.getRelativePath().getPathString();
if (path.startsWith(buildTreePrefix())) {
path = path.substring(buildTreePrefix().length());
}

return new ZipEntry(path);
}
});
}

private String buildTreePrefix() {
if (getTreePrefix().isPresent()) {
final String prefix = getTreePrefix().get();
if (!prefix.endsWith("/")) {
return prefix + "/";
}
return prefix;
}

return "";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public ZipBuildingFileTreeVisitor(ZipOutputStream outputZipStream) {
@Override
public void visitDir(FileVisitDetails fileVisitDetails) {
try {
final ZipEntry directoryEntry = new ZipEntry(fileVisitDetails.getRelativePath().getPathString() + "/");
final ZipEntry directoryEntry = createDirectory(fileVisitDetails);
outputZipStream.putNextEntry(directoryEntry);
outputZipStream.closeEntry();
} catch (IOException zip) {
Expand All @@ -33,16 +33,24 @@ public void visitDir(FileVisitDetails fileVisitDetails) {
}
}
}

protected ZipEntry createDirectory(FileVisitDetails fileVisitDetails) {
return new ZipEntry(fileVisitDetails.getRelativePath().getPathString() + "/");
}

@Override
public void visitFile(FileVisitDetails fileVisitDetails) {
try {
final ZipEntry fileEntry = new ZipEntry(fileVisitDetails.getRelativePath().getPathString());
final ZipEntry fileEntry = createFile(fileVisitDetails);
outputZipStream.putNextEntry(fileEntry);
fileVisitDetails.copyTo(outputZipStream);
outputZipStream.closeEntry();
} catch (IOException e) {
throw new RuntimeException("Could not create zip file: " + fileVisitDetails.getRelativePath().getPathString(), e);
}
}

protected ZipEntry createFile(FileVisitDetails fileVisitDetails) {
return new ZipEntry(fileVisitDetails.getRelativePath().getPathString());
}
}
Loading