| 1 | package net.minecraft.server.packs;
|
| 2 |
|
| 3 | import com.mojang.logging.LogUtils;
|
| 4 | import java.io.InputStream;
|
| 5 | import java.nio.file.Files;
|
| 6 | import java.nio.file.Path;
|
| 7 | import java.util.ArrayList;
|
| 8 | import java.util.Collection;
|
| 9 | import java.util.Collections;
|
| 10 | import java.util.EnumMap;
|
| 11 | import java.util.HashMap;
|
| 12 | import java.util.HashSet;
|
| 13 | import java.util.LinkedHashSet;
|
| 14 | import java.util.List;
|
| 15 | import java.util.Map;
|
| 16 | import java.util.Optional;
|
| 17 | import java.util.Set;
|
| 18 | import java.util.function.Consumer;
|
| 19 | import net.minecraft.resources.Identifier;
|
| 20 | import net.minecraft.server.packs.metadata.MetadataSectionType;
|
| 21 | import net.minecraft.server.packs.resources.IoSupplier;
|
| 22 | import net.minecraft.server.packs.resources.ResourceMetadata;
|
| 23 | import net.minecraft.util.FileUtil;
|
| 24 | import net.minecraft.util.Util;
|
| 25 | import org.jspecify.annotations.Nullable;
|
| 26 | import org.slf4j.Logger;
|
| 27 |
|
| 28 | public class FixedPathPackResources implements PackResources {
|
| 29 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 30 | private final PackLocationInfo location;
|
| 31 | private final ResourceMetadata builtInMetadata;
|
| 32 | private @Nullable ResourceMetadata resourceMetadata;
|
| 33 | private final Set<String> namespaces;
|
| 34 | private final List<Path> rootPaths;
|
| 35 | private final Map<PackType, List<Path>> pathsForType;
|
| 36 |
|
| 37 | private FixedPathPackResources(
|
| 38 | final PackLocationInfo location,
|
| 39 | final ResourceMetadata metadata,
|
| 40 | final Set<String> namespaces,
|
| 41 | final List<Path> rootPaths,
|
| 42 | final Map<PackType, List<Path>> pathsForType
|
| 43 | ) {
|
| 44 | this.location = location;
|
| 45 | this.builtInMetadata = metadata;
|
| 46 | this.namespaces = namespaces;
|
| 47 | this.rootPaths = rootPaths;
|
| 48 | this.pathsForType = pathsForType;
|
| 49 | }
|
| 50 |
|
| 51 | @Override
|
| 52 | public @Nullable IoSupplier<InputStream> getRootResource(final String... path) {
|
| 53 | FileUtil.validatePath(path);
|
| 54 | List<String> pathList = List.of(path);
|
| 55 |
|
| 56 | for (Path rootPath : this.rootPaths) {
|
| 57 | Path pathInRoot = FileUtil.resolvePath(rootPath, pathList);
|
| 58 | if (Files.exists(pathInRoot) && PathPackResources.validatePath(pathInRoot)) {
|
| 59 | return IoSupplier.create(pathInRoot);
|
| 60 | }
|
| 61 | }
|
| 62 |
|
| 63 | return null;
|
| 64 | }
|
| 65 |
|
| 66 | public void listRawPaths(final PackType type, final Identifier resource, final Consumer<Path> output) {
|
| 67 | FileUtil.decomposePath(resource.getPath()).ifSuccess(decomposedPath -> {
|
| 68 | String namespace = resource.getNamespace();
|
| 69 |
|
| 70 | for (Path typePath : this.pathsForType.get(type)) {
|
| 71 | Path namespacedPath = typePath.resolve(namespace);
|
| 72 | output.accept(FileUtil.resolvePath(namespacedPath, (List<String>)decomposedPath));
|
| 73 | }
|
| 74 | }).ifError(error -> LOGGER.error("Invalid path {}: {}", resource, error.message()));
|
| 75 | }
|
| 76 |
|
| 77 | @Override
|
| 78 | public void listResources(final PackType type, final String namespace, final String directory, final PackResources.ResourceOutput output) {
|
| 79 | FileUtil.decomposePath(directory).ifSuccess(decomposedPath -> {
|
| 80 | List<Path> paths = this.pathsForType.get(type);
|
| 81 | int pathsSize = paths.size();
|
| 82 | if (pathsSize == 1) {
|
| 83 | getResources(output, namespace, paths.getFirst(), (List<String>)decomposedPath);
|
| 84 | } else if (pathsSize > 1) {
|
| 85 | Map<Identifier, IoSupplier<InputStream>> resources = new HashMap<>();
|
| 86 |
|
| 87 | for (int i = 0; i < pathsSize - 1; i++) {
|
| 88 | getResources(resources::putIfAbsent, namespace, paths.get(i), (List<String>)decomposedPath);
|
| 89 | }
|
| 90 |
|
| 91 | Path lastPath = paths.getLast();
|
| 92 | if (resources.isEmpty()) {
|
| 93 | getResources(output, namespace, lastPath, (List<String>)decomposedPath);
|
| 94 | } else {
|
| 95 | getResources(resources::putIfAbsent, namespace, lastPath, (List<String>)decomposedPath);
|
| 96 | resources.forEach(output);
|
| 97 | }
|
| 98 | }
|
| 99 | }).ifError(error -> LOGGER.error("Invalid path {}: {}", directory, error.message()));
|
| 100 | }
|
| 101 |
|
| 102 | private static void getResources(final PackResources.ResourceOutput result, final String namespace, final Path root, final List<String> directory) {
|
| 103 | Path namespaceDir = root.resolve(namespace);
|
| 104 | PathPackResources.listPath(namespace, namespaceDir, directory, result);
|
| 105 | }
|
| 106 |
|
| 107 | @Override
|
| 108 | public @Nullable IoSupplier<InputStream> getResource(final PackType type, final Identifier location) {
|
| 109 | return FileUtil.decomposePath(location.getPath()).mapOrElse(decomposedPath -> {
|
| 110 | String namespace = location.getNamespace();
|
| 111 |
|
| 112 | for (Path typePath : this.pathsForType.get(type)) {
|
| 113 | Path resource = FileUtil.resolvePath(typePath.resolve(namespace), (List<String>)decomposedPath);
|
| 114 | if (Files.exists(resource) && PathPackResources.validatePath(resource)) {
|
| 115 | return IoSupplier.create(resource);
|
| 116 | }
|
| 117 | }
|
| 118 |
|
| 119 | return null;
|
| 120 | }, error -> {
|
| 121 | LOGGER.error("Invalid path {}: {}", location, error.message());
|
| 122 | return null;
|
| 123 | });
|
| 124 | }
|
| 125 |
|
| 126 | @Override
|
| 127 | public Set<String> getNamespaces(final PackType type) {
|
| 128 | return this.namespaces;
|
| 129 | }
|
| 130 |
|
| 131 | @Override
|
| 132 | public <T> @Nullable T getMetadataSection(final MetadataSectionType<T> metadataSerializer) {
|
| 133 | try {
|
| 134 | if (this.resourceMetadata == null) {
|
| 135 | this.resourceMetadata = AbstractPackMetadataResources.loadMetadata(this);
|
| 136 | }
|
| 137 |
|
| 138 | Optional<T> section = this.resourceMetadata.getSection(metadataSerializer);
|
| 139 | if (section.isPresent()) {
|
| 140 | return section.get();
|
| 141 | }
|
| 142 | } catch (Exception e) {
|
| 143 | LOGGER.warn("Failed to parse vanilla pack metadata", e);
|
| 144 | }
|
| 145 |
|
| 146 | return this.builtInMetadata.getSection(metadataSerializer).orElse(null);
|
| 147 | }
|
| 148 |
|
| 149 | @Override
|
| 150 | public PackLocationInfo location() {
|
| 151 | return this.location;
|
| 152 | }
|
| 153 |
|
| 154 | @Override
|
| 155 | public void close() {
|
| 156 | }
|
| 157 |
|
| 158 | public static class Builder {
|
| 159 | private final Set<Path> rootPaths = new LinkedHashSet<>();
|
| 160 | private final Map<PackType, Set<Path>> pathsForType = new EnumMap<>(PackType.class);
|
| 161 | private ResourceMetadata metadata = ResourceMetadata.EMPTY;
|
| 162 | private final Set<String> namespaces = new HashSet<>();
|
| 163 |
|
| 164 | private static boolean validateDirPath(final Path path) {
|
| 165 | if (!Files.exists(path)) {
|
| 166 | return false;
|
| 167 | } else if (!Files.isDirectory(path)) {
|
| 168 | throw new IllegalArgumentException("Path " + path.toAbsolutePath() + " is not directory");
|
| 169 | } else {
|
| 170 | return true;
|
| 171 | }
|
| 172 | }
|
| 173 |
|
| 174 | public void pushRootPath(final Path path) {
|
| 175 | if (validateDirPath(path)) {
|
| 176 | this.rootPaths.add(path);
|
| 177 | }
|
| 178 | }
|
| 179 |
|
| 180 | public void pushPathForType(final PackType packType, final Path path) {
|
| 181 | if (validateDirPath(path)) {
|
| 182 | this.pathsForType.computeIfAbsent(packType, var0 -> new LinkedHashSet<>()).add(path);
|
| 183 | }
|
| 184 | }
|
| 185 |
|
| 186 | public void setMetadata(final ResourceMetadata metadata) {
|
| 187 | this.metadata = metadata;
|
| 188 | }
|
| 189 |
|
| 190 | public void exposeNamespace(final Collection<String> namespaces) {
|
| 191 | this.namespaces.addAll(namespaces);
|
| 192 | }
|
| 193 |
|
| 194 | public FixedPathPackResources build(final PackLocationInfo location) {
|
| 195 | return new FixedPathPackResources(
|
| 196 | location,
|
| 197 | this.metadata,
|
| 198 | Set.copyOf(this.namespaces),
|
| 199 | copyAndReverse(this.rootPaths),
|
| 200 | Util.makeEnumMap(PackType.class, packType -> copyAndReverse(this.pathsForType.getOrDefault(packType, Set.of())))
|
| 201 | );
|
| 202 | }
|
| 203 |
|
| 204 | private static List<Path> copyAndReverse(final Collection<Path> input) {
|
| 205 | List<Path> paths = new ArrayList<>(input);
|
| 206 | Collections.reverse(paths);
|
| 207 | return List.copyOf(paths);
|
| 208 | }
|
| 209 | }
|
| 210 | }
|
| 211 |
|