Skip to content

Commit

Permalink
fix: register head textures on render thread
Browse files Browse the repository at this point in the history
this fixes crashes with fancymenu and vulkanmod (yippe!)

fixes #16
fixes #12
  • Loading branch information
uku3lig committed Feb 15, 2024
1 parent aa11f50 commit 56aada8
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 25 deletions.
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
package net.uku3lig.ukulib.config.impl;

import lombok.extern.slf4j.Slf4j;
import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.text.Text;
import net.uku3lig.ukulib.config.option.CyclingOption;
import net.uku3lig.ukulib.config.option.InputOption;
import net.uku3lig.ukulib.config.option.WidgetCreator;
import net.uku3lig.ukulib.config.screen.AbstractConfigScreen;
import net.uku3lig.ukulib.utils.Ukutils;

import java.util.Locale;

Expand All @@ -35,13 +32,4 @@ protected WidgetCreator[] getWidgets(UkulibConfig config) {
s -> s.toLowerCase(Locale.ROOT).matches("[a-z0-9_-]+"))
};
}

@Override
public void removed() {
super.removed();
if (FabricLoader.getInstance().isModLoaded("vulkanmod") && this.manager.getConfig().getHeadName().equalsIgnoreCase("uku3lig")) {
log.warn("VulkanMod detected, disabling custom heads.");
Ukutils.sendToast(Text.of("Warning!"), Text.of("Custom heads do not work with VulkanMod."));
}
}
}
22 changes: 10 additions & 12 deletions src/main/java/net/uku3lig/ukulib/mixin/MixinOptionsScreen.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package net.uku3lig.ukulib.mixin;

import com.mojang.blaze3d.systems.RenderSystem;
import lombok.extern.slf4j.Slf4j;
import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.client.MinecraftClient;
Expand Down Expand Up @@ -56,15 +57,12 @@ public void addUkulibButton(CallbackInfo ci) {
String username = UkulibConfig.get().getHeadName();
Identifier texture = DEFAULT_ICON;

// custom heads cause a crash with vulkanmod, see https://github.com/uku3lig/ukulib/issues/12
// TODO: custom icon caching?
if (!FabricLoader.getInstance().isModLoaded("vulkanmod")) {
Identifier customHeadTex = Ukutils.getHeadTex(username);
if (Ukutils.textureExists(customHeadTex)) {
texture = customHeadTex;
} else {
registerHeadTex(username).thenRun(() -> this.ukulibButton.setTexture(customHeadTex));
}
Identifier customHeadTex = Ukutils.getHeadTex(username);
if (Ukutils.textureExists(customHeadTex)) {
texture = customHeadTex;
} else {
registerHeadTex(username).thenRun(() -> this.ukulibButton.setTexture(customHeadTex));
}

this.ukulibButton = this.addDrawableChild(new IconButton(this.width / 2 + 158, this.height / 6 + 144 - 6, 20, 20,
Expand All @@ -80,8 +78,8 @@ public void addUkulibButton(CallbackInfo ci) {
*/
@Unique
private static CompletableFuture<Void> registerHeadTex(String username) {
Identifier texture = Ukutils.getHeadTex(username);
if (Ukutils.textureExists(texture)) {
Identifier identifier = Ukutils.getHeadTex(username);
if (Ukutils.textureExists(identifier)) {
return CompletableFuture.completedFuture(null);
}

Expand All @@ -91,8 +89,8 @@ private static CompletableFuture<Void> registerHeadTex(String username) {
return HTTP_CLIENT.sendAsync(req, HttpResponse.BodyHandlers.ofByteArray()).thenAccept(r -> {
if (r.statusCode() == 200) {
try {
NativeImage image = NativeImage.read(r.body());
texManager.registerTexture(texture, new NativeImageBackedTexture(image));
NativeImage image = NativeImage.read(r.body()); // crashes if put in a try-with-resources
RenderSystem.recordRenderCall(() -> texManager.registerTexture(identifier, new NativeImageBackedTexture(image)));
} catch (IOException e) {
log.error("Failed to register head texture", e);
}
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/net/uku3lig/ukulib/utils/IconButton.java
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@ public void renderWidget(DrawContext context, int mouseX, int mouseY, float delt
int rx = this.getX() + (this.width - this.iconWidth) / 2;
int ry = this.getY() + (this.height - this.iconHeight) / 2;

context.drawTexture(this.texture, rx, ry, 0, this.u, this.v, this.iconWidth, this.iconHeight, this.textureWidth, this.textureHeight);
if (Ukutils.textureExists(this.texture)) {
context.drawTexture(this.texture, rx, ry, 0, this.u, this.v, this.iconWidth, this.iconHeight, this.textureWidth, this.textureHeight);
}
}
}

0 comments on commit 56aada8

Please sign in to comment.