| 1 | package net.minecraft.server.packs.repository;
|
| 2 |
|
| 3 | import com.mojang.logging.LogUtils;
|
| 4 | import java.io.IOException;
|
| 5 | import java.nio.file.DirectoryStream;
|
| 6 | import java.nio.file.FileSystem;
|
| 7 | import java.nio.file.FileSystems;
|
| 8 | import java.nio.file.Files;
|
| 9 | import java.nio.file.Path;
|
| 10 | import java.util.ArrayList;
|
| 11 | import java.util.List;
|
| 12 | import java.util.Optional;
|
| 13 | import java.util.function.BiConsumer;
|
| 14 | import java.util.function.Consumer;
|
| 15 | import net.minecraft.network.chat.Component;
|
| 16 | import net.minecraft.server.packs.FilePackResources;
|
| 17 | import net.minecraft.server.packs.PackLocationInfo;
|
| 18 | import net.minecraft.server.packs.PackSelectionConfig;
|
| 19 | import net.minecraft.server.packs.PackType;
|
| 20 | import net.minecraft.server.packs.PathPackResources;
|
| 21 | import net.minecraft.server.packs.linkfs.LinkFileSystem;
|
| 22 | import net.minecraft.util.FileUtil;
|
| 23 | import net.minecraft.world.level.validation.ContentValidationException;
|
| 24 | import net.minecraft.world.level.validation.DirectoryValidator;
|
| 25 | import net.minecraft.world.level.validation.ForbiddenSymlinkInfo;
|
| 26 | import org.jspecify.annotations.Nullable;
|
| 27 | import org.slf4j.Logger;
|
| 28 |
|
| 29 | public class FolderRepositorySource implements RepositorySource {
|
| 30 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 31 | private static final PackSelectionConfig DISCOVERED_PACK_SELECTION_CONFIG = new PackSelectionConfig(false, Pack.Position.TOP, false);
|
| 32 | private final Path folder;
|
| 33 | private final PackType packType;
|
| 34 | private final PackSource packSource;
|
| 35 | private final DirectoryValidator validator;
|
| 36 |
|
| 37 | public FolderRepositorySource(final Path folder, final PackType packType, final PackSource packSource, final DirectoryValidator validator) {
|
| 38 | this.folder = folder;
|
| 39 | this.packType = packType;
|
| 40 | this.packSource = packSource;
|
| 41 | this.validator = validator;
|
| 42 | }
|
| 43 |
|
| 44 | private static String nameFromPath(final Path content) {
|
| 45 | return content.getFileName().toString();
|
| 46 | }
|
| 47 |
|
| 48 | @Override
|
| 49 | public void loadPacks(final Consumer<Pack> result) {
|
| 50 | try {
|
| 51 | FileUtil.createDirectoriesSafe(this.folder);
|
| 52 | discoverPacks(this.folder, this.validator, (content, resources) -> {
|
| 53 | PackLocationInfo locationInfo = this.createDiscoveredFilePackInfo(content);
|
| 54 | Pack pack = Pack.readMetaAndCreate(locationInfo, resources, this.packType, DISCOVERED_PACK_SELECTION_CONFIG);
|
| 55 | if (pack != null) {
|
| 56 | result.accept(pack);
|
| 57 | }
|
| 58 | });
|
| 59 | } catch (IOException e) {
|
| 60 | LOGGER.warn("Failed to list packs in {}", this.folder, e);
|
| 61 | }
|
| 62 | }
|
| 63 |
|
| 64 | private PackLocationInfo createDiscoveredFilePackInfo(final Path content) {
|
| 65 | String name = nameFromPath(content);
|
| 66 | return new PackLocationInfo("file/" + name, Component.literal(name), this.packSource, Optional.empty());
|
| 67 | }
|
| 68 |
|
| 69 | public static void discoverPacks(final Path folder, final DirectoryValidator validator, final BiConsumer<Path, Pack.ResourcesSupplier> result) throws IOException {
|
| 70 | FolderRepositorySource.FolderPackDetector detector = new FolderRepositorySource.FolderPackDetector(validator);
|
| 71 |
|
| 72 | try (DirectoryStream<Path> contents = Files.newDirectoryStream(folder)) {
|
| 73 | for (Path content : contents) {
|
| 74 | try {
|
| 75 | List<ForbiddenSymlinkInfo> validationIssues = new ArrayList<>();
|
| 76 | Pack.ResourcesSupplier resources = detector.detectPackResources(content, validationIssues);
|
| 77 | if (!validationIssues.isEmpty()) {
|
| 78 | LOGGER.warn("Ignoring potential pack entry: {}", ContentValidationException.getMessage(content, validationIssues));
|
| 79 | } else if (resources != null) {
|
| 80 | result.accept(content, resources);
|
| 81 | } else {
|
| 82 | LOGGER.info("Found non-pack entry '{}', ignoring", content);
|
| 83 | }
|
| 84 | } catch (IOException e) {
|
| 85 | LOGGER.warn("Failed to read properties of '{}', ignoring", content, e);
|
| 86 | }
|
| 87 | }
|
| 88 | }
|
| 89 | }
|
| 90 |
|
| 91 | private static class FolderPackDetector extends PackDetector<Pack.ResourcesSupplier> {
|
| 92 | protected FolderPackDetector(final DirectoryValidator validator) {
|
| 93 | super(validator);
|
| 94 | }
|
| 95 |
|
| 96 | protected Pack.@Nullable ResourcesSupplier createZipPack(final Path content) {
|
| 97 | FileSystem fileSystem = content.getFileSystem();
|
| 98 | if (fileSystem != FileSystems.getDefault() && !(fileSystem instanceof LinkFileSystem)) {
|
| 99 | FolderRepositorySource.LOGGER.info("Can't open pack archive at {}", content);
|
| 100 | return null;
|
| 101 | } else {
|
| 102 | return new FilePackResources.FileResourcesSupplier(content);
|
| 103 | }
|
| 104 | }
|
| 105 |
|
| 106 | protected Pack.ResourcesSupplier createDirectoryPack(final Path content) {
|
| 107 | return new PathPackResources.PathResourcesSupplier(content);
|
| 108 | }
|
| 109 | }
|
| 110 | }
|
| 111 |
|