| 1 | package net.minecraft.client.renderer.texture.atlas;
|
| 2 |
|
| 3 | import com.mojang.blaze3d.platform.NativeImage;
|
| 4 | import com.mojang.logging.LogUtils;
|
| 5 | import java.io.IOException;
|
| 6 | import java.io.InputStream;
|
| 7 | import java.util.List;
|
| 8 | import java.util.Optional;
|
| 9 | import java.util.Set;
|
| 10 | import net.fabricmc.api.EnvType;
|
| 11 | import net.fabricmc.api.Environment;
|
| 12 | import net.minecraft.client.renderer.texture.SpriteContents;
|
| 13 | import net.minecraft.client.resources.metadata.animation.AnimationMetadataSection;
|
| 14 | import net.minecraft.client.resources.metadata.animation.FrameSize;
|
| 15 | import net.minecraft.client.resources.metadata.texture.TextureMetadataSection;
|
| 16 | import net.minecraft.resources.Identifier;
|
| 17 | import net.minecraft.server.packs.metadata.MetadataSectionType;
|
| 18 | import net.minecraft.server.packs.metadata.MetadataSectionType.WithValue;
|
| 19 | import net.minecraft.server.packs.resources.Resource;
|
| 20 | import net.minecraft.server.packs.resources.ResourceMetadata;
|
| 21 | import net.minecraft.util.Mth;
|
| 22 | import org.jspecify.annotations.Nullable;
|
| 23 | import org.slf4j.Logger;
|
| 24 |
|
| 25 | @FunctionalInterface
|
| 26 | @Environment(EnvType.CLIENT)
|
| 27 | public interface SpriteResourceLoader {
|
| 28 | Logger LOGGER = LogUtils.getLogger();
|
| 29 |
|
| 30 | static SpriteResourceLoader create(final Set<MetadataSectionType<?>> additionalMetadataSections) {
|
| 31 | return (spriteLocation, resource) -> {
|
| 32 | Optional<AnimationMetadataSection> animationInfo;
|
| 33 | Optional<TextureMetadataSection> textureInfo;
|
| 34 | List<WithValue<?>> additionalMetadata;
|
| 35 | try {
|
| 36 | ResourceMetadata metadata = resource.metadata();
|
| 37 | animationInfo = metadata.getSection(AnimationMetadataSection.TYPE);
|
| 38 | textureInfo = metadata.getSection(TextureMetadataSection.TYPE);
|
| 39 | additionalMetadata = metadata.getTypedSections(additionalMetadataSections);
|
| 40 | } catch (Exception e) {
|
| 41 | LOGGER.error("Unable to parse metadata from {}", spriteLocation, e);
|
| 42 | return null;
|
| 43 | }
|
| 44 |
|
| 45 | NativeImage image;
|
| 46 | try (InputStream is = resource.open()) {
|
| 47 | image = NativeImage.read(is);
|
| 48 | } catch (IOException e) {
|
| 49 | LOGGER.error("Using missing texture, unable to load {}", spriteLocation, e);
|
| 50 | return null;
|
| 51 | }
|
| 52 |
|
| 53 | FrameSize frameSize;
|
| 54 | if (animationInfo.isPresent()) {
|
| 55 | frameSize = animationInfo.get().calculateFrameSize(image.getWidth(), image.getHeight());
|
| 56 | if (!Mth.isMultipleOf(image.getWidth(), frameSize.width()) || !Mth.isMultipleOf(image.getHeight(), frameSize.height())) {
|
| 57 | LOGGER.error(
|
| 58 | "Image {} size {},{} is not multiple of frame size {},{}", spriteLocation, image.getWidth(), image.getHeight(), frameSize.width(), frameSize.height()
|
| 59 | );
|
| 60 | image.close();
|
| 61 | return null;
|
| 62 | }
|
| 63 | } else {
|
| 64 | frameSize = new FrameSize(image.getWidth(), image.getHeight());
|
| 65 | }
|
| 66 |
|
| 67 | return new SpriteContents(spriteLocation, frameSize, image, animationInfo, additionalMetadata, textureInfo);
|
| 68 | };
|
| 69 | }
|
| 70 |
|
| 71 | @Nullable SpriteContents loadSprite(Identifier spriteLocation, Resource resource);
|
| 72 | }
|
| 73 |
|