| 1 | package net.minecraft.server.packs;
|
| 2 |
|
| 3 | import com.google.common.collect.ImmutableMap;
|
| 4 | import com.google.common.collect.ImmutableMap.Builder;
|
| 5 | import com.mojang.logging.LogUtils;
|
| 6 | import java.io.IOException;
|
| 7 | import java.net.URI;
|
| 8 | import java.net.URL;
|
| 9 | import java.nio.file.Path;
|
| 10 | import java.nio.file.Paths;
|
| 11 | import java.util.ArrayList;
|
| 12 | import java.util.Enumeration;
|
| 13 | import java.util.HashSet;
|
| 14 | import java.util.List;
|
| 15 | import java.util.Map;
|
| 16 | import java.util.Set;
|
| 17 | import java.util.function.Consumer;
|
| 18 | import java.util.stream.Collectors;
|
| 19 | import net.minecraft.server.packs.resources.ResourceMetadata;
|
| 20 | import net.minecraft.util.FileSystemUtil;
|
| 21 | import net.minecraft.util.Util;
|
| 22 | import org.jspecify.annotations.Nullable;
|
| 23 | import org.slf4j.Logger;
|
| 24 |
|
| 25 | public class VanillaPackResourcesBuilder {
|
| 26 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 27 | public static Consumer<VanillaPackResourcesBuilder> developmentConfig = builder -> {};
|
| 28 | private final FixedPathPackResources.Builder fullBuilder = new FixedPathPackResources.Builder();
|
| 29 | private final List<FixedPathPackResources.Builder> layeredBuilders = new ArrayList<>();
|
| 30 | private final Set<String> namespaces = new HashSet<>();
|
| 31 | private @Nullable ResourceMetadata metadata;
|
| 32 | private static final Map<PackType, Path> ROOT_DIR_BY_TYPE = Util.make(() -> {
|
| 33 | synchronized (VanillaPackResources.class) {
|
| 34 | Builder<PackType, Path> result = ImmutableMap.builder();
|
| 35 |
|
| 36 | for (PackType type : PackType.values()) {
|
| 37 | String probeName = "/" + type.getDirectory() + "/.mcassetsroot";
|
| 38 | URL probeUrl = VanillaPackResources.class.getResource(probeName);
|
| 39 | if (probeUrl == null) {
|
| 40 | LOGGER.error("File {} does not exist in classpath", probeName);
|
| 41 | } else {
|
| 42 | try {
|
| 43 | URI probeUri = probeUrl.toURI();
|
| 44 | String scheme = probeUri.getScheme();
|
| 45 | if (!"jar".equals(scheme) && !"file".equals(scheme)) {
|
| 46 | LOGGER.warn("Assets URL '{}' uses unexpected schema", probeUri);
|
| 47 | }
|
| 48 |
|
| 49 | Path probePath = FileSystemUtil.safeGetPath(probeUri);
|
| 50 | result.put(type, probePath.getParent());
|
| 51 | } catch (Exception e) {
|
| 52 | LOGGER.error("Couldn't resolve path to vanilla assets", e);
|
| 53 | }
|
| 54 | }
|
| 55 | }
|
| 56 |
|
| 57 | return result.build();
|
| 58 | }
|
| 59 | });
|
| 60 |
|
| 61 | public VanillaPackResourcesBuilder() {
|
| 62 | this.layeredBuilders.add(new FixedPathPackResources.Builder());
|
| 63 | }
|
| 64 |
|
| 65 | private void forLastLayer(final Consumer<FixedPathPackResources.Builder> task) {
|
| 66 | task.accept(this.fullBuilder);
|
| 67 | task.accept(this.layeredBuilders.getLast());
|
| 68 | }
|
| 69 |
|
| 70 | private void forAllLayers(final Consumer<FixedPathPackResources.Builder> task) {
|
| 71 | task.accept(this.fullBuilder);
|
| 72 |
|
| 73 | for (FixedPathPackResources.Builder layeredBuilder : this.layeredBuilders) {
|
| 74 | task.accept(layeredBuilder);
|
| 75 | }
|
| 76 | }
|
| 77 |
|
| 78 | private void pushRootPath(final Path path) {
|
| 79 | this.forLastLayer(builder -> builder.pushRootPath(path));
|
| 80 | }
|
| 81 |
|
| 82 | private void pushPathForType(final PackType packType, final Path path) {
|
| 83 | this.forLastLayer(builder -> builder.pushPathForType(packType, path));
|
| 84 | }
|
| 85 |
|
| 86 | public VanillaPackResourcesBuilder pushJarResources() {
|
| 87 | ROOT_DIR_BY_TYPE.forEach((packType, path) -> {
|
| 88 | this.pushRootPath(path.getParent());
|
| 89 | this.pushPathForType(packType, path);
|
| 90 | });
|
| 91 | return this;
|
| 92 | }
|
| 93 |
|
| 94 | public VanillaPackResourcesBuilder pushClasspathResources(final PackType packType, final Class<?> source) {
|
| 95 | Enumeration<URL> resources = null;
|
| 96 |
|
| 97 | try {
|
| 98 | resources = source.getClassLoader().getResources(packType.getDirectory() + "/");
|
| 99 | } catch (IOException var8) {
|
| 100 | }
|
| 101 |
|
| 102 | while (resources != null && resources.hasMoreElements()) {
|
| 103 | URL url = resources.nextElement();
|
| 104 |
|
| 105 | try {
|
| 106 | URI uri = url.toURI();
|
| 107 | if ("file".equals(uri.getScheme())) {
|
| 108 | Path assetsPath = Paths.get(uri);
|
| 109 | this.pushRootPath(assetsPath.getParent());
|
| 110 | this.pushPathForType(packType, assetsPath);
|
| 111 | }
|
| 112 | } catch (Exception e) {
|
| 113 | LOGGER.error("Failed to extract path from {}", url, e);
|
| 114 | }
|
| 115 | }
|
| 116 |
|
| 117 | return this;
|
| 118 | }
|
| 119 |
|
| 120 | public VanillaPackResourcesBuilder applyDevelopmentConfig() {
|
| 121 | developmentConfig.accept(this);
|
| 122 | return this;
|
| 123 | }
|
| 124 |
|
| 125 | public VanillaPackResourcesBuilder pushUniversalPath(final Path path) {
|
| 126 | this.pushRootPath(path);
|
| 127 |
|
| 128 | for (PackType packType : PackType.values()) {
|
| 129 | this.pushPathForType(packType, path.resolve(packType.getDirectory()));
|
| 130 | }
|
| 131 |
|
| 132 | return this;
|
| 133 | }
|
| 134 |
|
| 135 | public VanillaPackResourcesBuilder pushAssetPath(final PackType packType, final Path path) {
|
| 136 | this.pushRootPath(path);
|
| 137 | this.pushPathForType(packType, path);
|
| 138 | return this;
|
| 139 | }
|
| 140 |
|
| 141 | public VanillaPackResourcesBuilder setMetadata(final ResourceMetadata metadata) {
|
| 142 | this.forAllLayers(builder -> builder.setMetadata(metadata));
|
| 143 | this.metadata = metadata;
|
| 144 | return this;
|
| 145 | }
|
| 146 |
|
| 147 | public VanillaPackResourcesBuilder exposeNamespace(final String... namespaces) {
|
| 148 | List<String> namespaceList = List.of(namespaces);
|
| 149 | this.forAllLayers(builder -> builder.exposeNamespace(namespaceList));
|
| 150 | this.namespaces.addAll(namespaceList);
|
| 151 | return this;
|
| 152 | }
|
| 153 |
|
| 154 | public VanillaPackResourcesBuilder pushLayer() {
|
| 155 | FixedPathPackResources.Builder newBuilder = new FixedPathPackResources.Builder();
|
| 156 | this.layeredBuilders.add(newBuilder);
|
| 157 | newBuilder.exposeNamespace(this.namespaces);
|
| 158 | if (this.metadata != null) {
|
| 159 | newBuilder.setMetadata(this.metadata);
|
| 160 | }
|
| 161 |
|
| 162 | return this;
|
| 163 | }
|
| 164 |
|
| 165 | public VanillaPackResources build(final PackLocationInfo location) {
|
| 166 | return new VanillaPackResources(
|
| 167 | this.fullBuilder.build(location), this.layeredBuilders.stream().map(builder -> builder.build(location)).collect(Collectors.toUnmodifiableList())
|
| 168 | );
|
| 169 | }
|
| 170 | }
|
| 171 |
|