| 1 | package net.minecraft.client.renderer.texture;
|
| 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.io.UncheckedIOException;
|
| 8 | import java.net.HttpURLConnection;
|
| 9 | import java.net.Proxy;
|
| 10 | import java.net.URI;
|
| 11 | import java.nio.file.Files;
|
| 12 | import java.nio.file.Path;
|
| 13 | import java.util.concurrent.CompletableFuture;
|
| 14 | import java.util.concurrent.Executor;
|
| 15 | import net.fabricmc.api.EnvType;
|
| 16 | import net.fabricmc.api.Environment;
|
| 17 | import net.minecraft.core.ClientAsset.DownloadedTexture;
|
| 18 | import net.minecraft.core.ClientAsset.Texture;
|
| 19 | import net.minecraft.resources.Identifier;
|
| 20 | import net.minecraft.util.ARGB;
|
| 21 | import net.minecraft.util.FileUtil;
|
| 22 | import net.minecraft.util.Util;
|
| 23 | import org.slf4j.Logger;
|
| 24 |
|
| 25 | @Environment(EnvType.CLIENT)
|
| 26 | public class SkinTextureDownloader {
|
| 27 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 28 | private static final int SKIN_WIDTH = 64;
|
| 29 | private static final int SKIN_HEIGHT = 64;
|
| 30 | private static final int LEGACY_SKIN_HEIGHT = 32;
|
| 31 | private final Proxy proxy;
|
| 32 | private final TextureManager textureManager;
|
| 33 | private final Executor mainThreadExecutor;
|
| 34 |
|
| 35 | public SkinTextureDownloader(final Proxy proxy, final TextureManager textureManager, final Executor mainThreadExecutor) {
|
| 36 | this.proxy = proxy;
|
| 37 | this.textureManager = textureManager;
|
| 38 | this.mainThreadExecutor = mainThreadExecutor;
|
| 39 | }
|
| 40 |
|
| 41 | public CompletableFuture<Texture> downloadAndRegisterSkin(final Identifier textureId, final Path localCopy, final String url, final boolean processLegacySkin) {
|
| 42 | DownloadedTexture texture = new DownloadedTexture(textureId, url);
|
| 43 | return CompletableFuture.<NativeImage>supplyAsync(() -> {
|
| 44 | NativeImage loadedSkin;
|
| 45 | try {
|
| 46 | loadedSkin = this.downloadSkin(localCopy, texture.url());
|
| 47 | } catch (IOException e) {
|
| 48 | throw new UncheckedIOException(e);
|
| 49 | }
|
| 50 |
|
| 51 | return processLegacySkin ? processLegacySkin(loadedSkin, texture.url()) : loadedSkin;
|
| 52 | }, Util.nonCriticalIoPool().forName("downloadTexture")).thenCompose(fixedSkin -> this.registerTextureInManager(texture, fixedSkin));
|
| 53 | }
|
| 54 |
|
| 55 | private NativeImage downloadSkin(final Path localCopy, final String url) throws IOException {
|
| 56 | if (Files.isRegularFile(localCopy)) {
|
| 57 | LOGGER.debug("Loading HTTP texture from local cache ({})", localCopy);
|
| 58 |
|
| 59 | try (InputStream inputStream = Files.newInputStream(localCopy)) {
|
| 60 | return NativeImage.read(inputStream);
|
| 61 | }
|
| 62 | } else {
|
| 63 | HttpURLConnection connection = null;
|
| 64 | LOGGER.debug("Downloading HTTP texture from {} to {}", url, localCopy);
|
| 65 | URI uri = URI.create(url);
|
| 66 |
|
| 67 | try {
|
| 68 | connection = (HttpURLConnection)uri.toURL().openConnection(this.proxy);
|
| 69 | connection.setDoInput(true);
|
| 70 | connection.setDoOutput(false);
|
| 71 | connection.connect();
|
| 72 | int responseCode = connection.getResponseCode();
|
| 73 | if (responseCode / 100 != 2) {
|
| 74 | throw new IOException("Failed to open " + uri + ", HTTP error code: " + responseCode);
|
| 75 | }
|
| 76 |
|
| 77 | byte[] imageContents = connection.getInputStream().readAllBytes();
|
| 78 |
|
| 79 | try {
|
| 80 | FileUtil.createDirectoriesSafe(localCopy.getParent());
|
| 81 | Files.write(localCopy, imageContents);
|
| 82 | } catch (IOException e) {
|
| 83 | LOGGER.warn("Failed to cache texture {} in {}", url, localCopy);
|
| 84 | }
|
| 85 |
|
| 86 | return NativeImage.read(imageContents);
|
| 87 | } finally {
|
| 88 | if (connection != null) {
|
| 89 | connection.disconnect();
|
| 90 | }
|
| 91 | }
|
| 92 | }
|
| 93 | }
|
| 94 |
|
| 95 | private CompletableFuture<Texture> registerTextureInManager(final Texture textureId, final NativeImage contents) {
|
| 96 | return CompletableFuture.supplyAsync(() -> {
|
| 97 | DynamicTexture texture = new DynamicTexture(textureId.texturePath()::toString, contents);
|
| 98 | this.textureManager.register(textureId.texturePath(), texture);
|
| 99 | return textureId;
|
| 100 | }, this.mainThreadExecutor);
|
| 101 | }
|
| 102 |
|
| 103 | private static NativeImage processLegacySkin(NativeImage image, final String url) {
|
| 104 | int height = image.getHeight();
|
| 105 | int width = image.getWidth();
|
| 106 | if (width == 64 && (height == 32 || height == 64)) {
|
| 107 | boolean isLegacy = height == 32;
|
| 108 | if (isLegacy) {
|
| 109 | NativeImage newImage = new NativeImage(64, 64, true);
|
| 110 | newImage.copyFrom(image);
|
| 111 | image.close();
|
| 112 | image = newImage;
|
| 113 | image.fillRect(0, 32, 64, 32, 0);
|
| 114 | image.copyRect(4, 16, 16, 32, 4, 4, true, false);
|
| 115 | image.copyRect(8, 16, 16, 32, 4, 4, true, false);
|
| 116 | image.copyRect(0, 20, 24, 32, 4, 12, true, false);
|
| 117 | image.copyRect(4, 20, 16, 32, 4, 12, true, false);
|
| 118 | image.copyRect(8, 20, 8, 32, 4, 12, true, false);
|
| 119 | image.copyRect(12, 20, 16, 32, 4, 12, true, false);
|
| 120 | image.copyRect(44, 16, -8, 32, 4, 4, true, false);
|
| 121 | image.copyRect(48, 16, -8, 32, 4, 4, true, false);
|
| 122 | image.copyRect(40, 20, 0, 32, 4, 12, true, false);
|
| 123 | image.copyRect(44, 20, -8, 32, 4, 12, true, false);
|
| 124 | image.copyRect(48, 20, -16, 32, 4, 12, true, false);
|
| 125 | image.copyRect(52, 20, -8, 32, 4, 12, true, false);
|
| 126 | }
|
| 127 |
|
| 128 | setNoAlpha(image, 0, 0, 32, 16);
|
| 129 | if (isLegacy) {
|
| 130 | doNotchTransparencyHack(image, 32, 0, 64, 32);
|
| 131 | }
|
| 132 |
|
| 133 | setNoAlpha(image, 0, 16, 64, 32);
|
| 134 | setNoAlpha(image, 16, 48, 48, 64);
|
| 135 | return image;
|
| 136 | } else {
|
| 137 | image.close();
|
| 138 | throw new IllegalStateException("Discarding incorrectly sized (" + width + "x" + height + ") skin texture from " + url);
|
| 139 | }
|
| 140 | }
|
| 141 |
|
| 142 | private static void doNotchTransparencyHack(final NativeImage image, final int x0, final int y0, final int x1, final int y1) {
|
| 143 | for (int x = x0; x < x1; x++) {
|
| 144 | for (int y = y0; y < y1; y++) {
|
| 145 | int pix = image.getPixel(x, y);
|
| 146 | if (ARGB.alpha(pix) < 128) {
|
| 147 | return;
|
| 148 | }
|
| 149 | }
|
| 150 | }
|
| 151 |
|
| 152 | for (int x = x0; x < x1; x++) {
|
| 153 | for (int y = y0; y < y1; y++) {
|
| 154 | image.setPixel(x, y, image.getPixel(x, y) & 16777215);
|
| 155 | }
|
| 156 | }
|
| 157 | }
|
| 158 |
|
| 159 | private static void setNoAlpha(final NativeImage image, final int x0, final int y0, final int x1, final int y1) {
|
| 160 | for (int x = x0; x < x1; x++) {
|
| 161 | for (int y = y0; y < y1; y++) {
|
| 162 | image.setPixel(x, y, ARGB.opaque(image.getPixel(x, y)));
|
| 163 | }
|
| 164 | }
|
| 165 | }
|
| 166 | }
|
| 167 |
|