| 1 | package net.minecraft.client.renderer.texture.atlas;
|
| 2 |
|
| 3 | import com.google.common.collect.ImmutableList;
|
| 4 | import com.google.common.collect.ImmutableList.Builder;
|
| 5 | import com.google.gson.JsonElement;
|
| 6 | import com.mojang.logging.LogUtils;
|
| 7 | import com.mojang.serialization.Dynamic;
|
| 8 | import com.mojang.serialization.JsonOps;
|
| 9 | import java.io.BufferedReader;
|
| 10 | import java.util.ArrayList;
|
| 11 | import java.util.HashMap;
|
| 12 | import java.util.Iterator;
|
| 13 | import java.util.List;
|
| 14 | import java.util.Map;
|
| 15 | import java.util.Map.Entry;
|
| 16 | import java.util.function.Predicate;
|
| 17 | import net.fabricmc.api.EnvType;
|
| 18 | import net.fabricmc.api.Environment;
|
| 19 | import net.minecraft.client.renderer.texture.MissingTextureAtlasSprite;
|
| 20 | import net.minecraft.resources.FileToIdConverter;
|
| 21 | import net.minecraft.resources.Identifier;
|
| 22 | import net.minecraft.server.packs.resources.Resource;
|
| 23 | import net.minecraft.server.packs.resources.ResourceManager;
|
| 24 | import net.minecraft.util.StrictJsonParser;
|
| 25 | import org.slf4j.Logger;
|
| 26 |
|
| 27 | @Environment(EnvType.CLIENT)
|
| 28 | public class SpriteSourceList {
|
| 29 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 30 | private static final FileToIdConverter ATLAS_INFO_CONVERTER = new FileToIdConverter("atlases", ".json");
|
| 31 | private final List<SpriteSource> sources;
|
| 32 |
|
| 33 | private SpriteSourceList(final List<SpriteSource> sources) {
|
| 34 | this.sources = sources;
|
| 35 | }
|
| 36 |
|
| 37 | public List<SpriteSource.Loader> list(final ResourceManager resourceManager) {
|
| 38 | final Map<Identifier, SpriteSource.DiscardableLoader> sprites = new HashMap<>();
|
| 39 | SpriteSource.Output output = new SpriteSource.Output() {
|
| 40 | @Override
|
| 41 | public void add(final Identifier id, final SpriteSource.DiscardableLoader sprite) {
|
| 42 | SpriteSource.DiscardableLoader previous = sprites.put(id, sprite);
|
| 43 | if (previous != null) {
|
| 44 | previous.discard();
|
| 45 | }
|
| 46 | }
|
| 47 |
|
| 48 | @Override
|
| 49 | public void removeAll(final Predicate<Identifier> predicate) {
|
| 50 | Iterator<Entry<Identifier, SpriteSource.DiscardableLoader>> it = sprites.entrySet().iterator();
|
| 51 |
|
| 52 | while (it.hasNext()) {
|
| 53 | Entry<Identifier, SpriteSource.DiscardableLoader> entry = it.next();
|
| 54 | if (predicate.test(entry.getKey())) {
|
| 55 | entry.getValue().discard();
|
| 56 | it.remove();
|
| 57 | }
|
| 58 | }
|
| 59 | }
|
| 60 | };
|
| 61 | this.sources.forEach(s -> s.run(resourceManager, output));
|
| 62 | Builder<SpriteSource.Loader> result = ImmutableList.builder();
|
| 63 | result.add(loader -> MissingTextureAtlasSprite.create());
|
| 64 | result.addAll(sprites.values());
|
| 65 | return result.build();
|
| 66 | }
|
| 67 |
|
| 68 | public static SpriteSourceList load(final ResourceManager resourceManager, final Identifier atlasId) {
|
| 69 | Identifier resourceId = ATLAS_INFO_CONVERTER.idToFile(atlasId);
|
| 70 | List<SpriteSource> loaders = new ArrayList<>();
|
| 71 |
|
| 72 | for (Resource entry : resourceManager.getResourceStack(resourceId)) {
|
| 73 | try (BufferedReader reader = entry.openAsReader()) {
|
| 74 | Dynamic<JsonElement> contents = new Dynamic<>(JsonOps.INSTANCE, StrictJsonParser.parse(reader));
|
| 75 | loaders.addAll(SpriteSources.FILE_CODEC.parse(contents).getOrThrow());
|
| 76 | } catch (Exception e) {
|
| 77 | LOGGER.error("Failed to parse atlas definition {} in pack {}", resourceId, entry.sourcePackId(), e);
|
| 78 | }
|
| 79 | }
|
| 80 |
|
| 81 | return new SpriteSourceList(loaders);
|
| 82 | }
|
| 83 | }
|
| 84 |
|