| 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.FileNotFoundException;
|
| 6 | import java.io.IOException;
|
| 7 | import java.io.UncheckedIOException;
|
| 8 | import java.nio.file.Files;
|
| 9 | import java.nio.file.Path;
|
| 10 | import java.util.ArrayList;
|
| 11 | import java.util.HashMap;
|
| 12 | import java.util.HashSet;
|
| 13 | import java.util.List;
|
| 14 | import java.util.Map;
|
| 15 | import java.util.Set;
|
| 16 | import java.util.concurrent.CompletableFuture;
|
| 17 | import java.util.concurrent.Executor;
|
| 18 | import net.fabricmc.api.EnvType;
|
| 19 | import net.fabricmc.api.Environment;
|
| 20 | import net.minecraft.CrashReport;
|
| 21 | import net.minecraft.CrashReportCategory;
|
| 22 | import net.minecraft.ReportedException;
|
| 23 | import net.minecraft.resources.Identifier;
|
| 24 | import net.minecraft.server.packs.resources.PreparableReloadListener;
|
| 25 | import net.minecraft.server.packs.resources.ResourceManager;
|
| 26 | import net.minecraft.server.packs.resources.PreparableReloadListener.PreparationBarrier;
|
| 27 | import net.minecraft.server.packs.resources.PreparableReloadListener.SharedState;
|
| 28 | import org.slf4j.Logger;
|
| 29 |
|
| 30 | @Environment(EnvType.CLIENT)
|
| 31 | public class TextureManager implements PreparableReloadListener, AutoCloseable {
|
| 32 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 33 | private final Map<Identifier, AbstractTexture> byPath = new HashMap<>();
|
| 34 | private final Set<TickableTexture> tickableTextures = new HashSet<>();
|
| 35 | private final ResourceManager resourceManager;
|
| 36 |
|
| 37 | public TextureManager(final ResourceManager resourceManager) {
|
| 38 | this.resourceManager = resourceManager;
|
| 39 | NativeImage checkerboard = MissingTextureAtlasSprite.generateMissingImage();
|
| 40 | this.register(MissingTextureAtlasSprite.getLocation(), new DynamicTexture(() -> "(intentionally-)Missing Texture", checkerboard));
|
| 41 | }
|
| 42 |
|
| 43 | public void registerAndLoad(final Identifier textureId, final ReloadableTexture texture) {
|
| 44 | try {
|
| 45 | texture.apply(this.loadContentsSafe(textureId, texture));
|
| 46 | } catch (Throwable t) {
|
| 47 | CrashReport report = CrashReport.forThrowable(t, "Uploading texture");
|
| 48 | CrashReportCategory category = report.addCategory("Uploaded texture");
|
| 49 | category.setDetail("Resource location", texture.resourceId());
|
| 50 | category.setDetail("Texture id", textureId);
|
| 51 | throw new ReportedException(report);
|
| 52 | }
|
| 53 |
|
| 54 | this.register(textureId, texture);
|
| 55 | }
|
| 56 |
|
| 57 | private TextureContents loadContentsSafe(final Identifier textureId, final ReloadableTexture texture) {
|
| 58 | try {
|
| 59 | return loadContents(this.resourceManager, texture);
|
| 60 | } catch (Exception e) {
|
| 61 | LOGGER.error("Failed to load texture {} into slot {}", texture.resourceId(), textureId, e);
|
| 62 | return TextureContents.createMissing();
|
| 63 | }
|
| 64 | }
|
| 65 |
|
| 66 | public void registerForNextReload(final Identifier location) {
|
| 67 | this.register(location, new SimpleTexture(location));
|
| 68 | }
|
| 69 |
|
| 70 | public void register(final Identifier location, final AbstractTexture texture) {
|
| 71 | AbstractTexture prev = this.byPath.put(location, texture);
|
| 72 | if (prev != texture) {
|
| 73 | if (prev != null) {
|
| 74 | this.safeClose(location, prev);
|
| 75 | }
|
| 76 |
|
| 77 | if (texture instanceof TickableTexture tickableTexture) {
|
| 78 | this.tickableTextures.add(tickableTexture);
|
| 79 | }
|
| 80 | }
|
| 81 | }
|
| 82 |
|
| 83 | private void safeClose(final Identifier id, final AbstractTexture texture) {
|
| 84 | this.tickableTextures.remove(texture);
|
| 85 |
|
| 86 | try {
|
| 87 | texture.close();
|
| 88 | } catch (Exception e) {
|
| 89 | LOGGER.warn("Failed to close texture {}", id, e);
|
| 90 | }
|
| 91 | }
|
| 92 |
|
| 93 | public AbstractTexture getTexture(final Identifier location) {
|
| 94 | AbstractTexture textureObject = this.byPath.get(location);
|
| 95 | if (textureObject != null) {
|
| 96 | return textureObject;
|
| 97 | }
|
| 98 |
|
| 99 | SimpleTexture texture = new SimpleTexture(location);
|
| 100 | this.registerAndLoad(location, texture);
|
| 101 | return texture;
|
| 102 | }
|
| 103 |
|
| 104 | public void tick() {
|
| 105 | for (TickableTexture tickableTexture : this.tickableTextures) {
|
| 106 | tickableTexture.tick();
|
| 107 | }
|
| 108 | }
|
| 109 |
|
| 110 | public void release(final Identifier location) {
|
| 111 | AbstractTexture texture = this.byPath.remove(location);
|
| 112 | if (texture != null) {
|
| 113 | this.safeClose(location, texture);
|
| 114 | }
|
| 115 | }
|
| 116 |
|
| 117 | @Override
|
| 118 | public void close() {
|
| 119 | this.byPath.forEach(this::safeClose);
|
| 120 | this.byPath.clear();
|
| 121 | this.tickableTextures.clear();
|
| 122 | }
|
| 123 |
|
| 124 | @Override
|
| 125 | public CompletableFuture<Void> reload(
|
| 126 | final SharedState currentReload, final Executor taskExecutor, final PreparationBarrier preparationBarrier, final Executor reloadExecutor
|
| 127 | ) {
|
| 128 | ResourceManager manager = currentReload.resourceManager();
|
| 129 | List<TextureManager.PendingReload> reloads = new ArrayList<>();
|
| 130 | this.byPath.forEach((var3, texture) -> {
|
| 131 | if (texture instanceof ReloadableTexture reloadableTexture) {
|
| 132 | reloads.add(scheduleLoad(manager, reloadableTexture, taskExecutor));
|
| 133 | }
|
| 134 | });
|
| 135 | return CompletableFuture.allOf(reloads.stream().map(TextureManager.PendingReload::newContents).toArray(CompletableFuture[]::new))
|
| 136 | .thenCompose(preparationBarrier::wait)
|
| 137 | .thenAcceptAsync(var1 -> {
|
| 138 | for (TextureManager.PendingReload reload : reloads) {
|
| 139 | reload.texture.apply(reload.newContents.join());
|
| 140 | }
|
| 141 | }, reloadExecutor);
|
| 142 | }
|
| 143 |
|
| 144 | public void dumpAllSheets(final Path targetDir) {
|
| 145 | try {
|
| 146 | Files.createDirectories(targetDir);
|
| 147 | } catch (IOException e) {
|
| 148 | LOGGER.error("Failed to create directory {}", targetDir, e);
|
| 149 | return;
|
| 150 | }
|
| 151 |
|
| 152 | this.byPath.forEach((location, texture) -> {
|
| 153 | if (texture instanceof Dumpable dumpable) {
|
| 154 | try {
|
| 155 | dumpable.dumpContents(location, targetDir);
|
| 156 | } catch (Exception e) {
|
| 157 | LOGGER.error("Failed to dump texture {}", location, e);
|
| 158 | }
|
| 159 | }
|
| 160 | });
|
| 161 | }
|
| 162 |
|
| 163 | private static TextureContents loadContents(final ResourceManager manager, final ReloadableTexture texture) throws IOException {
|
| 164 | try {
|
| 165 | return texture.loadContents(manager);
|
| 166 | } catch (FileNotFoundException e) {
|
| 167 | return TextureContents.createMissing();
|
| 168 | }
|
| 169 | }
|
| 170 |
|
| 171 | private static TextureManager.PendingReload scheduleLoad(final ResourceManager manager, final ReloadableTexture texture, final Executor executor) {
|
| 172 | return new TextureManager.PendingReload(texture, CompletableFuture.supplyAsync(() -> {
|
| 173 | try {
|
| 174 | return loadContents(manager, texture);
|
| 175 | } catch (IOException e) {
|
| 176 | throw new UncheckedIOException(e);
|
| 177 | }
|
| 178 | }, executor));
|
| 179 | }
|
| 180 |
|
| 181 | @Environment(EnvType.CLIENT)
|
| 182 | private record PendingReload(ReloadableTexture texture, CompletableFuture<TextureContents> newContents) {
|
| 183 | }
|
| 184 | }
|
| 185 |
|