| 1 | package com.mojang.realmsclient.util;
|
| 2 |
|
| 3 | import com.google.common.collect.Maps;
|
| 4 | import com.mojang.blaze3d.platform.NativeImage;
|
| 5 | import com.mojang.logging.LogUtils;
|
| 6 | import java.io.IOException;
|
| 7 | import java.nio.ByteBuffer;
|
| 8 | import java.util.Base64;
|
| 9 | import java.util.Map;
|
| 10 | import net.fabricmc.api.EnvType;
|
| 11 | import net.fabricmc.api.Environment;
|
| 12 | import net.minecraft.client.Minecraft;
|
| 13 | import net.minecraft.client.renderer.texture.DynamicTexture;
|
| 14 | import net.minecraft.client.renderer.texture.MissingTextureAtlasSprite;
|
| 15 | import net.minecraft.resources.Identifier;
|
| 16 | import org.jspecify.annotations.Nullable;
|
| 17 | import org.lwjgl.system.MemoryUtil;
|
| 18 | import org.slf4j.Logger;
|
| 19 |
|
| 20 | @Environment(EnvType.CLIENT)
|
| 21 | public class RealmsTextureManager {
|
| 22 | private static final Map<String, RealmsTextureManager.RealmsTexture> TEXTURES = Maps.newHashMap();
|
| 23 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 24 | private static final Identifier TEMPLATE_ICON_LOCATION = Identifier.withDefaultNamespace("textures/gui/presets/isles.png");
|
| 25 |
|
| 26 | public static Identifier worldTemplate(final String id, final @Nullable String image) {
|
| 27 | return image == null ? TEMPLATE_ICON_LOCATION : getTexture(id, image);
|
| 28 | }
|
| 29 |
|
| 30 | private static Identifier getTexture(final String id, final String encodedImage) {
|
| 31 | RealmsTextureManager.RealmsTexture texture = TEXTURES.get(id);
|
| 32 | if (texture != null && texture.image().equals(encodedImage)) {
|
| 33 | return texture.textureId;
|
| 34 | } else {
|
| 35 | NativeImage image = loadImage(encodedImage);
|
| 36 | if (image == null) {
|
| 37 | Identifier missingTexture = MissingTextureAtlasSprite.getLocation();
|
| 38 | TEXTURES.put(id, new RealmsTextureManager.RealmsTexture(encodedImage, missingTexture));
|
| 39 | return missingTexture;
|
| 40 | } else {
|
| 41 | Identifier textureId = Identifier.fromNamespaceAndPath("realms", "dynamic/" + id);
|
| 42 | Minecraft.getInstance().getTextureManager().register(textureId, new DynamicTexture(textureId::toString, image));
|
| 43 | TEXTURES.put(id, new RealmsTextureManager.RealmsTexture(encodedImage, textureId));
|
| 44 | return textureId;
|
| 45 | }
|
| 46 | }
|
| 47 | }
|
| 48 |
|
| 49 | private static @Nullable NativeImage loadImage(final String encodedImage) {
|
| 50 | byte[] bytes = Base64.getDecoder().decode(encodedImage);
|
| 51 | ByteBuffer buffer = MemoryUtil.memAlloc(bytes.length);
|
| 52 |
|
| 53 | try {
|
| 54 | return NativeImage.read(buffer.put(bytes).flip());
|
| 55 | } catch (IOException e) {
|
| 56 | LOGGER.warn("Failed to load world image: {}", encodedImage, e);
|
| 57 | } finally {
|
| 58 | MemoryUtil.memFree(buffer);
|
| 59 | }
|
| 60 |
|
| 61 | return null;
|
| 62 | }
|
| 63 |
|
| 64 | @Environment(EnvType.CLIENT)
|
| 65 | public record RealmsTexture(String image, Identifier textureId) {
|
| 66 | }
|
| 67 | }
|
| 68 |
|