Skip to content

Commit

Permalink
fix: Icons for deAWT
Browse files Browse the repository at this point in the history
  • Loading branch information
Moresteck committed Aug 19, 2024
1 parent 9620220 commit 32cfcc9
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 12 deletions.
41 changes: 29 additions & 12 deletions src/main/java/uk/betacraft/legacyfix/patch/impl/DeAwtPatch.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import uk.betacraft.legacyfix.LegacyFixAgent;
import uk.betacraft.legacyfix.patch.Patch;
import uk.betacraft.legacyfix.patch.PatchException;
import uk.betacraft.legacyfix.util.IconUtils;

public class DeAwtPatch extends Patch {
private String canvasClassName;
Expand All @@ -27,12 +28,19 @@ public DeAwtPatch() {

@Override
public void apply(final Instrumentation inst) throws Exception {
// Attempt to load icons
try {
IconUtils.loadIcons((String) LegacyFixAgent.getSettings().get("icon"));
} catch (Exception e) {
LFLogger.error(this, e);
}

// Find the applet class
String[] typicalPaths = new String[] {"net.minecraft.client.MinecraftApplet", "com.mojang.minecraft.MinecraftApplet"};

CtClass appletClass = null;
for (int i = 0; i < typicalPaths.length; i++) {
appletClass = pool.getOrNull(typicalPaths[i]);
for (String path : typicalPaths) {
appletClass = pool.getOrNull(path);

if (appletClass != null)
break;
Expand Down Expand Up @@ -370,17 +378,26 @@ public void edit(NewExpr m) throws CannotCompileException {
// On init
// TODO: Implement instance icons
setTitleMethod.insertBefore(
// Title
"$1 = \"" + LegacyFixAgent.getSettings().get("frameName") + "\";" +
"org.lwjgl.opengl.Display.setResizable(true);"// +
// "java.lang.reflect.Field f16 = java.lang.ClassLoader.getSystemClassLoader()" +
// " .loadClass(\"legacyfix.agent.LegacyFixAgent\").getDeclaredField(\"pixels16\");" +
// "f16.setAccessible(true);" +
// "java.nio.ByteBuffer pix16 = f16.get(null);" +
// "java.lang.reflect.Field f32 = java.lang.ClassLoader.getSystemClassLoader()" +
// " .loadClass(\"legacyfix.agent.LegacyFixAgent\").getDeclaredField(\"pixels32\");" +
// "f32.setAccessible(true);" +
// "java.nio.ByteBuffer pix32 = f32.get(null);" +
// "org.lwjgl.opengl.Display.setIcon(new java.nio.ByteBuffer[] {pix16, pix32});"

// Resizable
"org.lwjgl.opengl.Display.setResizable(true);" +

// 16x16 icon
"java.lang.reflect.Field f16 = java.lang.ClassLoader.getSystemClassLoader()" +
" .loadClass(\"uk.betacraft.legacyfix.util.IconUtils\").getDeclaredField(\"pixels16\");" +
"f16.setAccessible(true);" +
"java.nio.ByteBuffer pix16 = f16.get(null);" +

// 32x32 icon
"java.lang.reflect.Field f32 = java.lang.ClassLoader.getSystemClassLoader()" +
" .loadClass(\"uk.betacraft.legacyfix.util.IconUtils\").getDeclaredField(\"pixels32\");" +
"f32.setAccessible(true);" +
"java.nio.ByteBuffer pix32 = f32.get(null);" +

// Setting the icon
"org.lwjgl.opengl.Display.setIcon(new java.nio.ByteBuffer[] {pix16, pix32});"
);

// On tick - couldn't really hook anywhere else, this looks like a safe spot
Expand Down
60 changes: 60 additions & 0 deletions src/main/java/uk/betacraft/legacyfix/util/IconUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package uk.betacraft.legacyfix.util;

import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;

import javax.imageio.ImageIO;

import uk.betacraft.legacyfix.LFLogger;
import uk.betacraft.legacyfix.LegacyFixAgent;

public class IconUtils {

static ByteBuffer pixels16 = null;
static ByteBuffer pixels32 = null;

public static void loadIcons(String iconPath) throws IOException {
if (iconPath != null) {
File iconFile = new File(iconPath);

if (iconFile.exists() && iconFile.isFile()) {
pixels32 = getIconForLWJGL(new FileInputStream(iconFile), 32);
pixels16 = getIconForLWJGL(new FileInputStream(iconFile), 16);
} else {
LFLogger.error("No icon found at given path: " + iconPath);

pixels16 = getIconForLWJGL(LegacyFixAgent.class.getResourceAsStream("/favicon.png"), 16);
pixels32 = getIconForLWJGL(LegacyFixAgent.class.getResourceAsStream("/favicon.png"), 32);
}
} else {
pixels16 = getIconForLWJGL(LegacyFixAgent.class.getResourceAsStream("/favicon.png"), 16);
pixels32 = getIconForLWJGL(LegacyFixAgent.class.getResourceAsStream("/favicon.png"), 32);
}
}

private static ByteBuffer getIconForLWJGL(InputStream stream, int resolution) throws IOException {
final Image read = ImageIO.read(stream).getScaledInstance(resolution, resolution, Image.SCALE_SMOOTH);

BufferedImage bufImg = new BufferedImage(resolution, resolution, BufferedImage.TYPE_INT_ARGB);

Graphics g = bufImg.getGraphics();
g.drawImage(read, 0, 0, null);
g.dispose();

final int[] rgb = bufImg.getRGB(0, 0, resolution, resolution, null, 0, resolution);
final ByteBuffer allocate = ByteBuffer.allocate(4 * rgb.length);

for (final int n : rgb) {
allocate.putInt(n << 8 | (n >> 24 & 0xFF));
}

allocate.flip();
return allocate;
}
}

0 comments on commit 32cfcc9

Please sign in to comment.