| 1 | package net.minecraft.server.packs.repository;
|
| 2 |
|
| 3 | import java.io.IOException;
|
| 4 | import java.nio.file.Files;
|
| 5 | import java.nio.file.LinkOption;
|
| 6 | import java.nio.file.NoSuchFileException;
|
| 7 | import java.nio.file.Path;
|
| 8 | import java.nio.file.attribute.BasicFileAttributes;
|
| 9 | import java.util.List;
|
| 10 | import net.minecraft.world.level.validation.DirectoryValidator;
|
| 11 | import net.minecraft.world.level.validation.ForbiddenSymlinkInfo;
|
| 12 | import org.jspecify.annotations.Nullable;
|
| 13 |
|
| 14 | public abstract class PackDetector<T> {
|
| 15 | private final DirectoryValidator validator;
|
| 16 |
|
| 17 | protected PackDetector(final DirectoryValidator validator) {
|
| 18 | this.validator = validator;
|
| 19 | }
|
| 20 |
|
| 21 | public @Nullable T detectPackResources(final Path content, final List<ForbiddenSymlinkInfo> issues) throws IOException {
|
| 22 | Path targetContext = content;
|
| 23 |
|
| 24 | BasicFileAttributes attributes;
|
| 25 | try {
|
| 26 | attributes = Files.readAttributes(content, BasicFileAttributes.class, LinkOption.NOFOLLOW_LINKS);
|
| 27 | } catch (NoSuchFileException e) {
|
| 28 | return null;
|
| 29 | }
|
| 30 |
|
| 31 | if (attributes.isSymbolicLink()) {
|
| 32 | this.validator.validateSymlink(content, issues);
|
| 33 | if (!issues.isEmpty()) {
|
| 34 | return null;
|
| 35 | }
|
| 36 |
|
| 37 | targetContext = Files.readSymbolicLink(content);
|
| 38 | attributes = Files.readAttributes(targetContext, BasicFileAttributes.class, LinkOption.NOFOLLOW_LINKS);
|
| 39 | }
|
| 40 |
|
| 41 | if (attributes.isDirectory()) {
|
| 42 | this.validator.validateKnownDirectory(targetContext, issues);
|
| 43 | if (!issues.isEmpty()) {
|
| 44 | return null;
|
| 45 | } else {
|
| 46 | return !Files.isRegularFile(targetContext.resolve("pack.mcmeta")) ? null : this.createDirectoryPack(targetContext);
|
| 47 | }
|
| 48 | } else {
|
| 49 | return attributes.isRegularFile() && targetContext.getFileName().toString().endsWith(".zip") ? this.createZipPack(targetContext) : null;
|
| 50 | }
|
| 51 | }
|
| 52 |
|
| 53 | protected abstract @Nullable T createZipPack(final Path content) throws IOException;
|
| 54 |
|
| 55 | protected abstract @Nullable T createDirectoryPack(final Path content) throws IOException;
|
| 56 | }
|
| 57 |
|