| 1 | package net.minecraft.server.packs;
|
| 2 |
|
| 3 | import com.google.common.annotations.VisibleForTesting;
|
| 4 | import com.google.common.collect.Sets;
|
| 5 | import com.mojang.logging.LogUtils;
|
| 6 | import java.io.File;
|
| 7 | import java.io.IOException;
|
| 8 | import java.io.InputStream;
|
| 9 | import java.nio.file.Path;
|
| 10 | import java.util.ArrayList;
|
| 11 | import java.util.Enumeration;
|
| 12 | import java.util.List;
|
| 13 | import java.util.Locale;
|
| 14 | import java.util.Set;
|
| 15 | import java.util.stream.Stream;
|
| 16 | import java.util.zip.ZipEntry;
|
| 17 | import java.util.zip.ZipFile;
|
| 18 | import net.minecraft.resources.Identifier;
|
| 19 | import net.minecraft.server.packs.repository.Pack;
|
| 20 | import net.minecraft.server.packs.resources.IoSupplier;
|
| 21 | import org.apache.commons.io.IOUtils;
|
| 22 | import org.jspecify.annotations.Nullable;
|
| 23 | import org.slf4j.Logger;
|
| 24 |
|
| 25 | public class FilePackResources extends AbstractPackMetadataResources implements PackResources {
|
| 26 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 27 | private final FilePackResources.SharedZipFileAccess zipFileAccess;
|
| 28 | private final String prefix;
|
| 29 |
|
| 30 | private FilePackResources(final PackLocationInfo location, final FilePackResources.SharedZipFileAccess zipFileAccess, final String prefix) {
|
| 31 | super(location);
|
| 32 | this.zipFileAccess = zipFileAccess;
|
| 33 | this.prefix = prefix;
|
| 34 | }
|
| 35 |
|
| 36 | private static String getPathFromLocation(final PackType type, final Identifier location) {
|
| 37 | return String.format(Locale.ROOT, "%s/%s/%s", type.getDirectory(), location.getNamespace(), location.getPath());
|
| 38 | }
|
| 39 |
|
| 40 | @Override
|
| 41 | public @Nullable IoSupplier<InputStream> getRootResource(final String... path) {
|
| 42 | return this.getResource(String.join("/", path));
|
| 43 | }
|
| 44 |
|
| 45 | @Override
|
| 46 | public IoSupplier<InputStream> getResource(final PackType type, final Identifier location) {
|
| 47 | return this.getResource(getPathFromLocation(type, location));
|
| 48 | }
|
| 49 |
|
| 50 | private String addPrefix(final String path) {
|
| 51 | return this.prefix.isEmpty() ? path : this.prefix + "/" + path;
|
| 52 | }
|
| 53 |
|
| 54 | private @Nullable IoSupplier<InputStream> getResource(final String path) {
|
| 55 | ZipFile zipFile = this.zipFileAccess.getOrCreateZipFile();
|
| 56 | if (zipFile == null) {
|
| 57 | return null;
|
| 58 | }
|
| 59 |
|
| 60 | ZipEntry entry = zipFile.getEntry(this.addPrefix(path));
|
| 61 | return entry == null ? null : IoSupplier.create(zipFile, entry);
|
| 62 | }
|
| 63 |
|
| 64 | @Override
|
| 65 | public Set<String> getNamespaces(final PackType type) {
|
| 66 | ZipFile zipFile = this.zipFileAccess.getOrCreateZipFile();
|
| 67 | if (zipFile == null) {
|
| 68 | return Set.of();
|
| 69 | }
|
| 70 |
|
| 71 | Enumeration<? extends ZipEntry> entries = zipFile.entries();
|
| 72 | Set<String> namespaces = Sets.newHashSet();
|
| 73 | String typePrefix = this.addPrefix(type.getDirectory() + "/");
|
| 74 |
|
| 75 | while (entries.hasMoreElements()) {
|
| 76 | ZipEntry zipEntry = entries.nextElement();
|
| 77 | String name = zipEntry.getName();
|
| 78 | String namespace = extractNamespace(typePrefix, name);
|
| 79 | if (!namespace.isEmpty()) {
|
| 80 | if (Identifier.isValidNamespace(namespace)) {
|
| 81 | namespaces.add(namespace);
|
| 82 | } else {
|
| 83 | LOGGER.warn("Non {} character in namespace {} in pack {}, ignoring", "[a-z0-9_.-]", namespace, this.zipFileAccess.file);
|
| 84 | }
|
| 85 | }
|
| 86 | }
|
| 87 |
|
| 88 | return namespaces;
|
| 89 | }
|
| 90 |
|
| 91 | @VisibleForTesting
|
| 92 | public static String extractNamespace(final String prefix, final String name) {
|
| 93 | if (!name.startsWith(prefix)) {
|
| 94 | return "";
|
| 95 | }
|
| 96 |
|
| 97 | int prefixLength = prefix.length();
|
| 98 | int firstPart = name.indexOf(47, prefixLength);
|
| 99 | return firstPart == -1 ? name.substring(prefixLength) : name.substring(prefixLength, firstPart);
|
| 100 | }
|
| 101 |
|
| 102 | @Override
|
| 103 | public void close() {
|
| 104 | this.zipFileAccess.close();
|
| 105 | }
|
| 106 |
|
| 107 | @Override
|
| 108 | public void listResources(final PackType type, final String namespace, final String directory, final PackResources.ResourceOutput output) {
|
| 109 | ZipFile zipFile = this.zipFileAccess.getOrCreateZipFile();
|
| 110 | if (zipFile != null) {
|
| 111 | Enumeration<? extends ZipEntry> entries = zipFile.entries();
|
| 112 | String root = this.addPrefix(type.getDirectory() + "/" + namespace + "/");
|
| 113 | String prefix = root + directory + "/";
|
| 114 |
|
| 115 | while (entries.hasMoreElements()) {
|
| 116 | ZipEntry zipEntry = entries.nextElement();
|
| 117 | if (!zipEntry.isDirectory()) {
|
| 118 | String name = zipEntry.getName();
|
| 119 | if (name.startsWith(prefix)) {
|
| 120 | String path = name.substring(root.length());
|
| 121 | Identifier id = Identifier.tryBuild(namespace, path);
|
| 122 | if (id != null) {
|
| 123 | output.accept(id, IoSupplier.create(zipFile, zipEntry));
|
| 124 | } else {
|
| 125 | LOGGER.warn("Invalid path in datapack: {}:{}, ignoring", namespace, path);
|
| 126 | }
|
| 127 | }
|
| 128 | }
|
| 129 | }
|
| 130 | }
|
| 131 | }
|
| 132 |
|
| 133 | public static class FileResourcesSupplier implements Pack.ResourcesSupplier {
|
| 134 | private final File content;
|
| 135 |
|
| 136 | public FileResourcesSupplier(final Path content) {
|
| 137 | this(content.toFile());
|
| 138 | }
|
| 139 |
|
| 140 | public FileResourcesSupplier(final File content) {
|
| 141 | this.content = content;
|
| 142 | }
|
| 143 |
|
| 144 | @Override
|
| 145 | public PackMetadataResources openMetadata(final PackLocationInfo location) {
|
| 146 | FilePackResources.SharedZipFileAccess fileAccess = new FilePackResources.SharedZipFileAccess(this.content);
|
| 147 | return new FilePackResources(location, fileAccess, "");
|
| 148 | }
|
| 149 |
|
| 150 | @Override
|
| 151 | public Stream<PackResources> openResources(final PackLocationInfo location, final Pack.Metadata metadata) {
|
| 152 | FilePackResources.SharedZipFileAccess fileAccess = new FilePackResources.SharedZipFileAccess(this.content);
|
| 153 | PackResources primary = new FilePackResources(location, fileAccess, "");
|
| 154 | List<String> overlays = metadata.overlays();
|
| 155 | if (overlays.isEmpty()) {
|
| 156 | return Stream.of(primary);
|
| 157 | }
|
| 158 |
|
| 159 | List<PackResources> overlayResources = new ArrayList<>(overlays.size());
|
| 160 |
|
| 161 | for (String overlay : overlays) {
|
| 162 | overlayResources.add(new FilePackResources(location, fileAccess, overlay));
|
| 163 | }
|
| 164 |
|
| 165 | return Stream.of(new OverlayedPackResources(primary, overlayResources));
|
| 166 | }
|
| 167 | }
|
| 168 |
|
| 169 | private static class SharedZipFileAccess implements AutoCloseable {
|
| 170 | private final File file;
|
| 171 | private @Nullable ZipFile zipFile;
|
| 172 | private boolean failedToLoad;
|
| 173 |
|
| 174 | private SharedZipFileAccess(final File file) {
|
| 175 | this.file = file;
|
| 176 | }
|
| 177 |
|
| 178 | private @Nullable ZipFile getOrCreateZipFile() {
|
| 179 | if (this.failedToLoad) {
|
| 180 | return null;
|
| 181 | }
|
| 182 |
|
| 183 | if (this.zipFile == null) {
|
| 184 | try {
|
| 185 | this.zipFile = new ZipFile(this.file);
|
| 186 | } catch (IOException e) {
|
| 187 | FilePackResources.LOGGER.error("Failed to open pack {}", this.file, e);
|
| 188 | this.failedToLoad = true;
|
| 189 | return null;
|
| 190 | }
|
| 191 | }
|
| 192 |
|
| 193 | return this.zipFile;
|
| 194 | }
|
| 195 |
|
| 196 | @Override
|
| 197 | public void close() {
|
| 198 | if (this.zipFile != null) {
|
| 199 | IOUtils.closeQuietly(this.zipFile);
|
| 200 | this.zipFile = null;
|
| 201 | }
|
| 202 | }
|
| 203 |
|
| 204 | @Override
|
| 205 | protected void finalize() throws Throwable {
|
| 206 | this.close();
|
| 207 | super.finalize();
|
| 208 | }
|
| 209 | }
|
| 210 | }
|
| 211 |
|