| 1 | package net.minecraft.client.renderer.texture.atlas.sources;
|
| 2 |
|
| 3 | import com.google.common.base.Suppliers;
|
| 4 | import com.mojang.blaze3d.platform.NativeImage;
|
| 5 | import com.mojang.logging.LogUtils;
|
| 6 | import com.mojang.serialization.Codec;
|
| 7 | import com.mojang.serialization.MapCodec;
|
| 8 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 9 | import java.io.IOException;
|
| 10 | import java.util.HashMap;
|
| 11 | import java.util.List;
|
| 12 | import java.util.Map;
|
| 13 | import java.util.Optional;
|
| 14 | import java.util.Map.Entry;
|
| 15 | import java.util.function.Supplier;
|
| 16 | import net.fabricmc.api.EnvType;
|
| 17 | import net.fabricmc.api.Environment;
|
| 18 | import net.minecraft.client.renderer.texture.SpriteContents;
|
| 19 | import net.minecraft.client.renderer.texture.atlas.SpriteResourceLoader;
|
| 20 | import net.minecraft.client.renderer.texture.atlas.SpriteSource;
|
| 21 | import net.minecraft.client.resources.metadata.animation.FrameSize;
|
| 22 | import net.minecraft.client.resources.palette.Palette;
|
| 23 | import net.minecraft.client.resources.palette.PaletteMapping;
|
| 24 | import net.minecraft.resources.Identifier;
|
| 25 | import net.minecraft.server.packs.resources.Resource;
|
| 26 | import net.minecraft.server.packs.resources.ResourceManager;
|
| 27 | import org.jspecify.annotations.Nullable;
|
| 28 | import org.slf4j.Logger;
|
| 29 |
|
| 30 | @Environment(EnvType.CLIENT)
|
| 31 | public record PalettedPermutations(List<Identifier> textures, Identifier paletteKey, Map<String, Identifier> permutations, String separator)
|
| 32 | implements SpriteSource {
|
| 33 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 34 | public static final String DEFAULT_SEPARATOR = "_";
|
| 35 | public static final MapCodec<PalettedPermutations> MAP_CODEC = RecordCodecBuilder.mapCodec(
|
| 36 | i -> i.group(
|
| 37 | Codec.list(Identifier.CODEC).fieldOf("textures").forGetter(PalettedPermutations::textures),
|
| 38 | Identifier.CODEC.fieldOf("palette_key").forGetter(PalettedPermutations::paletteKey),
|
| 39 | Codec.unboundedMap(Codec.STRING, Identifier.CODEC).fieldOf("permutations").forGetter(PalettedPermutations::permutations),
|
| 40 | Codec.STRING.optionalFieldOf("separator", "_").forGetter(PalettedPermutations::separator)
|
| 41 | )
|
| 42 | .apply(i, PalettedPermutations::new)
|
| 43 | );
|
| 44 |
|
| 45 | public PalettedPermutations(final List<Identifier> textures, final Identifier paletteKey, final Map<String, Identifier> permutations) {
|
| 46 | this(textures, paletteKey, permutations, "_");
|
| 47 | }
|
| 48 |
|
| 49 | @Override
|
| 50 | public void run(final ResourceManager resourceManager, final SpriteSource.Output output) {
|
| 51 | Supplier<Palette> paletteKeySupplier = Suppliers.memoize(() -> loadPaletteEntryFromImage(resourceManager, this.paletteKey));
|
| 52 | Map<String, Supplier<PaletteMapping>> palettes = new HashMap<>();
|
| 53 | this.permutations
|
| 54 | .forEach(
|
| 55 | (suffix, palette) -> palettes.put(
|
| 56 | suffix, Suppliers.memoize(() -> PaletteMapping.create(paletteKeySupplier.get(), loadPaletteEntryFromImage(resourceManager, palette)))
|
| 57 | )
|
| 58 | );
|
| 59 |
|
| 60 | for (Identifier textureLocation : this.textures) {
|
| 61 | Identifier textureId = TEXTURE_ID_CONVERTER.idToFile(textureLocation);
|
| 62 | Optional<Resource> resource = resourceManager.getResource(textureId);
|
| 63 | if (resource.isEmpty()) {
|
| 64 | LOGGER.warn("Unable to find texture {}", textureId);
|
| 65 | } else {
|
| 66 | LazyLoadedImage baseImage = new LazyLoadedImage(textureId, resource.get(), palettes.size());
|
| 67 |
|
| 68 | for (Entry<String, Supplier<PaletteMapping>> entry : palettes.entrySet()) {
|
| 69 | Identifier permutationLocation = textureLocation.withSuffix(this.separator + entry.getKey());
|
| 70 | output.add(permutationLocation, new PalettedPermutations.PalettedSpriteSupplier(baseImage, entry.getValue(), permutationLocation));
|
| 71 | }
|
| 72 | }
|
| 73 | }
|
| 74 | }
|
| 75 |
|
| 76 | private static Palette loadPaletteEntryFromImage(final ResourceManager resourceManager, final Identifier paletteId) {
|
| 77 | Optional<Resource> resource = resourceManager.getResource(Palette.ID_CONVERTER.idToFile(paletteId));
|
| 78 | if (resource.isEmpty()) {
|
| 79 | LOGGER.error("Failed to load palette image {}", paletteId);
|
| 80 | throw new IllegalArgumentException();
|
| 81 | }
|
| 82 |
|
| 83 | try {
|
| 84 | return Palette.load(resource.get());
|
| 85 | } catch (Exception exception) {
|
| 86 | LOGGER.error("Couldn't load texture {}", paletteId, exception);
|
| 87 | throw new IllegalArgumentException();
|
| 88 | }
|
| 89 | }
|
| 90 |
|
| 91 | @Override
|
| 92 | public MapCodec<PalettedPermutations> codec() {
|
| 93 | return MAP_CODEC;
|
| 94 | }
|
| 95 |
|
| 96 | @Environment(EnvType.CLIENT)
|
| 97 | private record PalettedSpriteSupplier(LazyLoadedImage baseImage, Supplier<PaletteMapping> palette, Identifier permutationLocation)
|
| 98 | implements SpriteSource.DiscardableLoader {
|
| 99 | @Override
|
| 100 | public @Nullable SpriteContents get(final SpriteResourceLoader loader) {
|
| 101 | try {
|
| 102 | NativeImage image = this.baseImage.get().mappedCopy(this.palette.get());
|
| 103 | return new SpriteContents(this.permutationLocation, new FrameSize(image.getWidth(), image.getHeight()), image);
|
| 104 | } catch (IOException | IllegalArgumentException e) {
|
| 105 | PalettedPermutations.LOGGER.error("unable to apply palette to {}", this.permutationLocation, e);
|
| 106 | return null;
|
| 107 | } finally {
|
| 108 | this.baseImage.release();
|
| 109 | }
|
| 110 | }
|
| 111 |
|
| 112 | @Override
|
| 113 | public void discard() {
|
| 114 | this.baseImage.release();
|
| 115 | }
|
| 116 | }
|
| 117 | }
|
| 118 |
|