Skip to content

Commit

Permalink
Fix injection logic not taking into accounts that trees might need to…
Browse files Browse the repository at this point in the history
… be offset by a prefix. (#156)
  • Loading branch information
marchermans authored Apr 28, 2024
1 parent f4baaba commit b15b5af
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 75 deletions.
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());
}
}

0 comments on commit b15b5af

Please sign in to comment.