Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add texture debug feature #86

Merged
merged 2 commits into from
Oct 27, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/main/java/com/gtnewhorizon/gtnhlib/core/GTNHLibCore.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,12 @@ public String getMixinConfig() {

@Override
public List<String> getMixins(Set<String> loadedCoreMods) {
return Mixins.getEarlyMixins(loadedCoreMods);
final List<String> mixins = Mixins.getEarlyMixins(loadedCoreMods);
if (Boolean.parseBoolean(System.getProperty("gtnhlib.debugtextures", "false"))) {
Alexdoru marked this conversation as resolved.
Show resolved Hide resolved
mixins.add("debug.MixinDynamicTexture");
mixins.add("debug.MixinTextureAtlasSprite");
}
return mixins;
}

@Override
Expand Down
74 changes: 74 additions & 0 deletions src/main/java/com/gtnewhorizon/gtnhlib/debug/TexturesDebug.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package com.gtnewhorizon.gtnhlib.debug;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;

import net.minecraft.client.Minecraft;

public class TexturesDebug {

private static boolean initSpriteLogger = false;
private static PrintStream logAtlasSprite = null;
private static boolean initDynamicLogger = false;
private static PrintStream logDynamic = null;

public static void logTextureAtlasSprite(String iconName, int width, int height, int frames, int sizeBytes) {
logTextureAtlasSprite(iconName + "," + width + "," + height + "," + frames + "," + sizeBytes);
}

private static void logTextureAtlasSprite(String message) {
if (!initSpriteLogger) {
logAtlasSprite = initLogger("TexturesDebug.csv");
initSpriteLogger = true;
logTextureAtlasSprite("iconName,width,height,frames,sizeBytes");
}
if (logAtlasSprite != null) {
logAtlasSprite.println(message);
}
}

public static void logDynamicTexture(int width, int height) {
if (!initDynamicLogger) {
logDynamic = initLogger("DynamicTextures.txt");
initDynamicLogger = true;
}
if (logDynamic != null) {
StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
logDynamic.println("Created texture of width " + width + " height " + height);
for (int i = 0; i < stackTrace.length; i++) {
StackTraceElement element = stackTrace[i];
if (i == 4 || i == 5) {
logDynamic.println("at " + element);
break;
}
}
logDynamic.println(" ");
}
}

private static PrintStream initLogger(String file) {
final File logFile = new File(Minecraft.getMinecraft().mcDataDir, file);
if (logFile.exists()) {
// noinspection ResultOfMethodCallIgnored
logFile.delete();
}
if (!logFile.exists()) {
try {
// noinspection ResultOfMethodCallIgnored
logFile.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
try {
return new PrintStream(new FileOutputStream(logFile, true));
} catch (FileNotFoundException e) {
e.printStackTrace();
return null;
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.gtnewhorizon.gtnhlib.mixins.early.debug;

import net.minecraft.client.renderer.texture.DynamicTexture;

import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

import com.gtnewhorizon.gtnhlib.debug.TexturesDebug;

@Mixin(DynamicTexture.class)
public class MixinDynamicTexture {

@Inject(method = "<init>(II)V", at = @At("RETURN"))
private void gtnhlib$debug(int width, int height, CallbackInfo ci) {
if (width > 16 || height > 16) {
TexturesDebug.logDynamicTexture(width, height);
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.gtnewhorizon.gtnhlib.mixins.early.debug;

import java.awt.image.BufferedImage;
import java.util.List;

import net.minecraft.client.renderer.texture.TextureAtlasSprite;
import net.minecraft.client.resources.data.AnimationMetadataSection;

import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

import com.gtnewhorizon.gtnhlib.debug.TexturesDebug;

@Mixin(TextureAtlasSprite.class)
public class MixinTextureAtlasSprite {

@Shadow
protected List<int[][]> framesTextureData;

@Shadow
@Final
private String iconName;

@Shadow
protected int width;

@Shadow
protected int height;

@Inject(method = "loadSprite", at = @At("RETURN"))
private void gtnhlib$debug(BufferedImage[] buff, AnimationMetadataSection anim, boolean p_147964_3_,
CallbackInfo ci) {
int sizeBytes = 0;
for (int[][] ints : framesTextureData) {
for (int[] pixels : ints) {
if (pixels != null) {
sizeBytes += pixels.length * 4;
}
}
}
TexturesDebug.logTextureAtlasSprite(iconName, width, height, framesTextureData.size(), sizeBytes);
}

}