| 1 | package net.minecraft.server.packs.resources;
|
| 2 |
|
| 3 | import com.mojang.logging.LogUtils;
|
| 4 | import java.util.HashMap;
|
| 5 | import java.util.List;
|
| 6 | import java.util.Map;
|
| 7 | import java.util.Optional;
|
| 8 | import java.util.Set;
|
| 9 | import java.util.TreeMap;
|
| 10 | import java.util.stream.Stream;
|
| 11 | import net.minecraft.resources.Identifier;
|
| 12 | import net.minecraft.server.packs.PackMetadataResources;
|
| 13 | import net.minecraft.server.packs.PackResources;
|
| 14 | import net.minecraft.server.packs.PackType;
|
| 15 | import org.jspecify.annotations.Nullable;
|
| 16 | import org.slf4j.Logger;
|
| 17 |
|
| 18 | public class MultiPackResourceManager implements CloseableResourceManager {
|
| 19 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 20 | private final Map<String, FallbackResourceManager> namespacedManagers;
|
| 21 | private final List<PackResources> packs;
|
| 22 |
|
| 23 | public MultiPackResourceManager(final PackType type, final List<PackResources> packs) {
|
| 24 | this.packs = List.copyOf(packs);
|
| 25 | Map<String, FallbackResourceManager> namespacedManagers = new HashMap<>();
|
| 26 | List<String> namespaces = packs.stream().flatMap(p -> p.getNamespaces(type).stream()).distinct().toList();
|
| 27 |
|
| 28 | for (PackResources pack : packs) {
|
| 29 | ResourceFilterSection filterSection = this.getPackFilterSection(pack);
|
| 30 | Set<String> providedNamespaces = pack.getNamespaces(type);
|
| 31 | PackResources.Filter pathFilter = filterSection != null ? location -> filterSection.isPathFiltered(location.getPath()) : null;
|
| 32 |
|
| 33 | for (String namespace : namespaces) {
|
| 34 | boolean packContainsNamespace = providedNamespaces.contains(namespace);
|
| 35 | boolean filterMatchesNamespace = filterSection != null && filterSection.isNamespaceFiltered(namespace);
|
| 36 | if (packContainsNamespace || filterMatchesNamespace) {
|
| 37 | FallbackResourceManager fallbackResourceManager = namespacedManagers.get(namespace);
|
| 38 | if (fallbackResourceManager == null) {
|
| 39 | fallbackResourceManager = new FallbackResourceManager(type, namespace);
|
| 40 | namespacedManagers.put(namespace, fallbackResourceManager);
|
| 41 | }
|
| 42 |
|
| 43 | if (packContainsNamespace && filterMatchesNamespace) {
|
| 44 | fallbackResourceManager.push(pack, pathFilter);
|
| 45 | } else if (packContainsNamespace) {
|
| 46 | fallbackResourceManager.push(pack);
|
| 47 | } else {
|
| 48 | fallbackResourceManager.pushFilterOnly(pack.packId(), pathFilter);
|
| 49 | }
|
| 50 | }
|
| 51 | }
|
| 52 | }
|
| 53 |
|
| 54 | this.namespacedManagers = namespacedManagers;
|
| 55 | }
|
| 56 |
|
| 57 | private @Nullable ResourceFilterSection getPackFilterSection(final PackResources pack) {
|
| 58 | try {
|
| 59 | return pack.getMetadataSection(ResourceFilterSection.TYPE);
|
| 60 | } catch (Exception e) {
|
| 61 | LOGGER.error("Failed to get filter section from pack {}", pack.packId());
|
| 62 | return null;
|
| 63 | }
|
| 64 | }
|
| 65 |
|
| 66 | @Override
|
| 67 | public Set<String> getNamespaces() {
|
| 68 | return this.namespacedManagers.keySet();
|
| 69 | }
|
| 70 |
|
| 71 | @Override
|
| 72 | public Optional<Resource> getResource(final Identifier location) {
|
| 73 | ResourceManager pack = this.namespacedManagers.get(location.getNamespace());
|
| 74 | return pack != null ? pack.getResource(location) : Optional.empty();
|
| 75 | }
|
| 76 |
|
| 77 | @Override
|
| 78 | public List<Resource> getResourceStack(final Identifier location) {
|
| 79 | ResourceManager pack = this.namespacedManagers.get(location.getNamespace());
|
| 80 | return pack != null ? pack.getResourceStack(location) : List.of();
|
| 81 | }
|
| 82 |
|
| 83 | @Override
|
| 84 | public Map<Identifier, Resource> listResources(final String directory, final ResourceManager.Selector selector) {
|
| 85 | checkTrailingDirectoryPath(directory);
|
| 86 | Map<Identifier, Resource> result = new TreeMap<>();
|
| 87 |
|
| 88 | for (FallbackResourceManager manager : this.namespacedManagers.values()) {
|
| 89 | result.putAll(manager.listResources(directory, selector));
|
| 90 | }
|
| 91 |
|
| 92 | return result;
|
| 93 | }
|
| 94 |
|
| 95 | @Override
|
| 96 | public Map<Identifier, List<Resource>> listResourceStacks(final String directory, final ResourceManager.Selector selector) {
|
| 97 | checkTrailingDirectoryPath(directory);
|
| 98 | Map<Identifier, List<Resource>> result = new TreeMap<>();
|
| 99 |
|
| 100 | for (FallbackResourceManager manager : this.namespacedManagers.values()) {
|
| 101 | result.putAll(manager.listResourceStacks(directory, selector));
|
| 102 | }
|
| 103 |
|
| 104 | return result;
|
| 105 | }
|
| 106 |
|
| 107 | private static void checkTrailingDirectoryPath(final String directory) {
|
| 108 | if (directory.endsWith("/")) {
|
| 109 | throw new IllegalArgumentException("Trailing slash in path " + directory);
|
| 110 | }
|
| 111 | }
|
| 112 |
|
| 113 | @Override
|
| 114 | public Stream<PackResources> listPacks() {
|
| 115 | return this.packs.stream();
|
| 116 | }
|
| 117 |
|
| 118 | @Override
|
| 119 | public void close() {
|
| 120 | this.packs.forEach(PackMetadataResources::close);
|
| 121 | }
|
| 122 | }
|
| 123 |
|