PathPackResources .java

net.minecraft.server.packs.PathPackResources (class): members: PathResourcesSupplier, PATH_JOINER, listPath, validatePath, getRootResource, getNamespaces.

net.minecraft.server.packsclass196 linesserverextends AbstractPackMetadataResources6.8 KB

Implements

API Surface (9)

boolean validatePath(final Path path)
void listResources(final PackType type, final String namespace, final String directory, final PackResources.ResourceOutput output)
void listResources(final Path topPath, final String namespace, final String directory, final PackResources.ResourceOutput output)
void listPath(final String namespace, final Path topDir, final List<String> decomposedPrefixPath, final PackResources.ResourceOutput output)
Set<String> getNamespaces(final PackType type)
Set<String> getNamespaces(final Path rootDir)
void close()
PackMetadataResources openMetadata(final PackLocationInfo location)
Stream<PackResources> openResources(final PackLocationInfo location, final Pack.Metadata metadata)

Fields & Constants (3)

LOGGER
PATH_JOINER
root

String literals (1)

".ds_store"

Full Source

PathPackResources.java196 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.server.packs;
2
3
import com.google.common.base.Joiner;
4
import com.mojang.logging.LogUtils;
5
import java.io.IOException;
6
import java.io.InputStream;
7
import java.nio.file.DirectoryStream;
8
import java.nio.file.FileSystems;
9
import java.nio.file.Files;
10
import java.nio.file.NoSuchFileException;
11
import java.nio.file.NotDirectoryException;
12
import java.nio.file.Path;
13
import java.nio.file.attribute.BasicFileAttributes;
14
import java.util.ArrayList;
15
import java.util.HashSet;
16
import java.util.List;
17
import java.util.Locale;
18
import java.util.Set;
19
import java.util.stream.Stream;
20
import net.minecraft.SharedConstants;
21
import net.minecraft.resources.Identifier;
22
import net.minecraft.server.packs.repository.Pack;
23
import net.minecraft.server.packs.resources.IoSupplier;
24
import net.minecraft.util.FileUtil;
25
import net.minecraft.util.Util;
26
import org.apache.commons.lang3.StringUtils;
27
import org.jspecify.annotations.Nullable;
28
import org.slf4j.Logger;
29
30
public class PathPackResources extends AbstractPackMetadataResources implements PackResources {
31
	private static final Logger LOGGER = LogUtils.getLogger();
32
	private static final Joiner PATH_JOINER = Joiner.on("/");
33
	private final Path root;
34
35
	public PathPackResources(final PackLocationInfo location, final Path root) {
36
		super(location);
37
		this.root = root;
38
	}
39
40
	@Override
41
	public @Nullable IoSupplier<InputStream> getRootResource(final String... path) {
42
		FileUtil.validatePath(path);
43
		Path pathInRoot = FileUtil.resolvePath(this.root, List.of(path));
44
		return Files.exists(pathInRoot) ? IoSupplier.create(pathInRoot) : null;
45
	}
46
47
	public static boolean validatePath(final Path path) {
48
		if (!SharedConstants.DEBUG_VALIDATE_RESOURCE_PATH_CASE) {
49
			return true;
50
		}
51
52
		if (path.getFileSystem() != FileSystems.getDefault()) {
53
			return true;
54
		}
55
56
		try {
57
			return path.toRealPath().endsWith(path);
58
		} catch (IOException e) {
59
			LOGGER.warn("Failed to resolve real path for {}", path, e);
60
			return false;
61
		}
62
	}
63
64
	private Path topPackDir(final PackType type) {
65
		return this.root.resolve(type.getDirectory());
66
	}
67
68
	@Override
69
	public @Nullable IoSupplier<InputStream> getResource(final PackType type, final Identifier location) {
70
		Path topDir = this.topPackDir(type);
71
		return getResource(topDir, location);
72
	}
73
74
	public static @Nullable IoSupplier<InputStream> getResource(final Path topDir, final Identifier location) {
75
		Path namespaceDir = topDir.resolve(location.getNamespace());
76
		return FileUtil.decomposePath(location.getPath()).mapOrElse(decomposedPath -> {
77
			Path resolvedPath = FileUtil.resolvePath(namespaceDir, (List<String>)decomposedPath);
78
			return returnFileIfExists(resolvedPath);
79
		}, error -> {
80
			LOGGER.error("Invalid path {}: {}", location, error.message());
81
			return null;
82
		});
83
	}
84
85
	private static @Nullable IoSupplier<InputStream> returnFileIfExists(final Path resolvedPath) {
86
		return Files.exists(resolvedPath) && validatePath(resolvedPath) ? IoSupplier.create(resolvedPath) : null;
87
	}
88
89
	@Override
90
	public void listResources(final PackType type, final String namespace, final String directory, final PackResources.ResourceOutput output) {
91
		Path topDir = this.topPackDir(type);
92
		listResources(topDir, namespace, directory, output);
93
	}
94
95
	public static void listResources(final Path topPath, final String namespace, final String directory, final PackResources.ResourceOutput output) {
96
		FileUtil.decomposePath(directory).ifSuccess(decomposedPath -> {
97
			Path namespaceDir = topPath.resolve(namespace);
98
			listPath(namespace, namespaceDir, (List<String>)decomposedPath, output);
99
		}).ifError(error -> LOGGER.error("Invalid path {}: {}", directory, error.message()));
100
	}
101
102
	public static void listPath(final String namespace, final Path topDir, final List<String> decomposedPrefixPath, final PackResources.ResourceOutput output) {
103
		Path targetPath = FileUtil.resolvePath(topDir, decomposedPrefixPath);
104
105
		try (Stream<Path> files = Files.find(targetPath, Integer.MAX_VALUE, PathPackResources::isRegularFile)) {
106
			files.forEach(file -> {
107
				String resourcePath = PATH_JOINER.join(topDir.relativize(file));
108
				Identifier identifier = Identifier.tryBuild(namespace, resourcePath);
109
				if (identifier == null) {
110
					Util.logAndPauseIfInIde(String.format(Locale.ROOT, "Invalid path in pack: %s:%s, ignoring", namespace, resourcePath));
111
				} else {
112
					output.accept(identifier, IoSupplier.create(file));
113
				}
114
			});
115
		} catch (NoSuchFileException | NotDirectoryException var10) {
116
		} catch (IOException e) {
117
			LOGGER.error("Failed to list path {}", targetPath, e);
118
		}
119
	}
120
121
	private static boolean isRegularFile(final Path file, final BasicFileAttributes attributes) {
122
		return !SharedConstants.IS_RUNNING_IN_IDE
123
			? attributes.isRegularFile()
124
			: attributes.isRegularFile() && !StringUtils.equalsIgnoreCase(file.getFileName().toString(), ".ds_store");
125
	}
126
127
	@Override
128
	public Set<String> getNamespaces(final PackType type) {
129
		Path assetRoot = this.topPackDir(type);
130
		return getNamespaces(assetRoot);
131
	}
132
133
	public static Set<String> getNamespaces(final Path rootDir) {
134
		Set<String> namespaces = new HashSet<>();
135
136
		try (DirectoryStream<Path> directDirs = Files.newDirectoryStream(rootDir)) {
137
			for (Path directDir : directDirs) {
138
				if (!Files.isDirectory(directDir)) {
139
					LOGGER.warn("Non-directory entry {} found in namespace directory, rejecting", directDir);
140
				} else {
141
					String namespace = directDir.getFileName().toString();
142
					if (Identifier.isValidNamespace(namespace)) {
143
						namespaces.add(namespace);
144
					} else {
145
						LOGGER.warn("Non {} character in namespace {} in pack directory {}, ignoring", "[a-z0-9_.-]", namespace, rootDir);
146
					}
147
				}
148
			}
149
		} catch (NoSuchFileException | NotDirectoryException var8) {
150
		} catch (IOException e) {
151
			LOGGER.error("Failed to list path {}", rootDir, e);
152
		}
153
154
		return namespaces;
155
	}
156
157
	@Override
158
	public void close() {
159
	}
160
161
	public static class PathResourcesSupplier implements Pack.ResourcesSupplier {
162
		private final Path content;
163
164
		public PathResourcesSupplier(final Path content) {
165
			this.content = content;
166
		}
167
168
		private PathPackResources openPrimaryResources(final PackLocationInfo location) {
169
			return new PathPackResources(location, this.content);
170
		}
171
172
		@Override
173
		public PackMetadataResources openMetadata(final PackLocationInfo location) {
174
			return this.openPrimaryResources(location);
175
		}
176
177
		@Override
178
		public Stream<PackResources> openResources(final PackLocationInfo location, final Pack.Metadata metadata) {
179
			PackResources primary = this.openPrimaryResources(location);
180
			List<String> overlays = metadata.overlays();
181
			if (overlays.isEmpty()) {
182
				return Stream.of(primary);
183
			}
184
185
			List<PackResources> overlayResources = new ArrayList<>(overlays.size());
186
187
			for (String overlay : overlays) {
188
				Path overlayRoot = this.content.resolve(overlay);
189
				overlayResources.add(new PathPackResources(location, overlayRoot));
190
			}
191
192
			return Stream.of(new OverlayedPackResources(primary, overlayResources));
193
		}
194
	}
195
}
196

Decompiled Minecraft 26.3 source of PathPackResources (net/minecraft/server/packs/PathPackResources.java) with Mojang official mappings, stored verbatim on the site.