Skip to content

Commit

Permalink
add debug arg to dump all texture sizes in a csv file
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexdoru committed Oct 26, 2024
1 parent 21e7a5b commit 8c63c64
Show file tree
Hide file tree
Showing 4 changed files with 150 additions and 1 deletion.
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"))) {
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);
}

}

0 comments on commit 8c63c64

Please sign in to comment.