commit ef20de4de237afa12a9282a2a5675797967bf079 Author: Lassebq Date: Thu Nov 10 12:52:25 2022 +0200 Initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a7e41e1 --- /dev/null +++ b/.gitignore @@ -0,0 +1,30 @@ +# Eclipse stuff +.classpath +.project +.settings/ + +# netbeans +nbproject + +# gradle +.gradle/ + +# vim +.*.sw[a-p] + +# various other potential build files +/build/ +/out + +# Mac filesystem dust +.DS_Store + +# intellij +*.iml +*.ipr +*.iws +.idea/ + +# generated +/repo/ +/bin/ diff --git a/build.gradle b/build.gradle new file mode 100644 index 0000000..71a2d22 --- /dev/null +++ b/build.gradle @@ -0,0 +1,30 @@ +apply plugin: 'java' +apply plugin: 'eclipse' + +repositories { + maven { + url "https://libraries.minecraft.net/" + } + mavenCentral() +} + +group = 'org.mcphackers' +archivesBaseName = 'launchwrapper' +version = '1.0' +sourceCompatibility = 1.6 + +dependencies { + implementation 'org.ow2.asm:asm:9.3' + implementation 'org.ow2.asm:asm-tree:9.3' + implementation 'org.lwjgl.lwjgl:lwjgl:2.9.3' +} + +task sourcesJar(type: Jar) { + classifier = 'sources' + from sourceSets.main.allSource +} + +artifacts { + archives jar + archives sourcesJar +} diff --git a/gradle/wrapper/gradle-wrapper.jar b/gradle/wrapper/gradle-wrapper.jar new file mode 100644 index 0000000..b5166da Binary files /dev/null and b/gradle/wrapper/gradle-wrapper.jar differ diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties new file mode 100644 index 0000000..495da1d --- /dev/null +++ b/gradle/wrapper/gradle-wrapper.properties @@ -0,0 +1,6 @@ +#Sat Jun 20 12:56:50 EDT 2015 +distributionBase=GRADLE_USER_HOME +distributionPath=wrapper/dists +zipStoreBase=GRADLE_USER_HOME +zipStorePath=wrapper/dists +distributionUrl=https\://services.gradle.org/distributions/gradle-7.4-bin.zip diff --git a/gradlew b/gradlew new file mode 100644 index 0000000..91a7e26 --- /dev/null +++ b/gradlew @@ -0,0 +1,164 @@ +#!/usr/bin/env bash + +############################################################################## +## +## Gradle start up script for UN*X +## +############################################################################## + +# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +DEFAULT_JVM_OPTS="" + +APP_NAME="Gradle" +APP_BASE_NAME=`basename "$0"` + +# Use the maximum available, or set MAX_FD != -1 to use that value. +MAX_FD="maximum" + +warn ( ) { + echo "$*" +} + +die ( ) { + echo + echo "$*" + echo + exit 1 +} + +# OS specific support (must be 'true' or 'false'). +cygwin=false +msys=false +darwin=false +case "`uname`" in + CYGWIN* ) + cygwin=true + ;; + Darwin* ) + darwin=true + ;; + MINGW* ) + msys=true + ;; +esac + +# For Cygwin, ensure paths are in UNIX format before anything is touched. +if $cygwin ; then + [ -n "$JAVA_HOME" ] && JAVA_HOME=`cygpath --unix "$JAVA_HOME"` +fi + +# Attempt to set APP_HOME +# Resolve links: $0 may be a link +PRG="$0" +# Need this for relative symlinks. +while [ -h "$PRG" ] ; do + ls=`ls -ld "$PRG"` + link=`expr "$ls" : '.*-> \(.*\)$'` + if expr "$link" : '/.*' > /dev/null; then + PRG="$link" + else + PRG=`dirname "$PRG"`"/$link" + fi +done +SAVED="`pwd`" +cd "`dirname \"$PRG\"`/" >&- +APP_HOME="`pwd -P`" +cd "$SAVED" >&- + +CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar + +# Determine the Java command to use to start the JVM. +if [ -n "$JAVA_HOME" ] ; then + if [ -x "$JAVA_HOME/jre/sh/java" ] ; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD="$JAVA_HOME/jre/sh/java" + else + JAVACMD="$JAVA_HOME/bin/java" + fi + if [ ! -x "$JAVACMD" ] ; then + die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +else + JAVACMD="java" + which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." +fi + +# Increase the maximum file descriptors if we can. +if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then + MAX_FD_LIMIT=`ulimit -H -n` + if [ $? -eq 0 ] ; then + if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then + MAX_FD="$MAX_FD_LIMIT" + fi + ulimit -n $MAX_FD + if [ $? -ne 0 ] ; then + warn "Could not set maximum file descriptor limit: $MAX_FD" + fi + else + warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" + fi +fi + +# For Darwin, add options to specify how the application appears in the dock +if $darwin; then + GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" +fi + +# For Cygwin, switch paths to Windows format before running java +if $cygwin ; then + APP_HOME=`cygpath --path --mixed "$APP_HOME"` + CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` + + # We build the pattern for arguments to be converted via cygpath + ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` + SEP="" + for dir in $ROOTDIRSRAW ; do + ROOTDIRS="$ROOTDIRS$SEP$dir" + SEP="|" + done + OURCYGPATTERN="(^($ROOTDIRS))" + # Add a user-defined pattern to the cygpath arguments + if [ "$GRADLE_CYGPATTERN" != "" ] ; then + OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" + fi + # Now convert the arguments - kludge to limit ourselves to /bin/sh + i=0 + for arg in "$@" ; do + CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` + CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option + + if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition + eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` + else + eval `echo args$i`="\"$arg\"" + fi + i=$((i+1)) + done + case $i in + (0) set -- ;; + (1) set -- "$args0" ;; + (2) set -- "$args0" "$args1" ;; + (3) set -- "$args0" "$args1" "$args2" ;; + (4) set -- "$args0" "$args1" "$args2" "$args3" ;; + (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; + (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; + (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; + (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; + (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; + esac +fi + +# Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules +function splitJvmOpts() { + JVM_OPTS=("$@") +} +eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS +JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME" + +exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@" diff --git a/gradlew.bat b/gradlew.bat new file mode 100644 index 0000000..8a0b282 --- /dev/null +++ b/gradlew.bat @@ -0,0 +1,90 @@ +@if "%DEBUG%" == "" @echo off +@rem ########################################################################## +@rem +@rem Gradle startup script for Windows +@rem +@rem ########################################################################## + +@rem Set local scope for the variables with windows NT shell +if "%OS%"=="Windows_NT" setlocal + +@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +set DEFAULT_JVM_OPTS= + +set DIRNAME=%~dp0 +if "%DIRNAME%" == "" set DIRNAME=. +set APP_BASE_NAME=%~n0 +set APP_HOME=%DIRNAME% + +@rem Find java.exe +if defined JAVA_HOME goto findJavaFromJavaHome + +set JAVA_EXE=java.exe +%JAVA_EXE% -version >NUL 2>&1 +if "%ERRORLEVEL%" == "0" goto init + +echo. +echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:findJavaFromJavaHome +set JAVA_HOME=%JAVA_HOME:"=% +set JAVA_EXE=%JAVA_HOME%/bin/java.exe + +if exist "%JAVA_EXE%" goto init + +echo. +echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:init +@rem Get command-line arguments, handling Windowz variants + +if not "%OS%" == "Windows_NT" goto win9xME_args +if "%@eval[2+2]" == "4" goto 4NT_args + +:win9xME_args +@rem Slurp the command line arguments. +set CMD_LINE_ARGS= +set _SKIP=2 + +:win9xME_args_slurp +if "x%~1" == "x" goto execute + +set CMD_LINE_ARGS=%* +goto execute + +:4NT_args +@rem Get arguments from the 4NT Shell from JP Software +set CMD_LINE_ARGS=%$ + +:execute +@rem Setup the command line + +set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar + +@rem Execute Gradle +"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% + +:end +@rem End local scope for the variables with windows NT shell +if "%ERRORLEVEL%"=="0" goto mainEnd + +:fail +rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of +rem the _cmd.exe /c_ return code! +if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 +exit /b 1 + +:mainEnd +if "%OS%"=="Windows_NT" endlocal + +:omega diff --git a/level.dat b/level.dat new file mode 100644 index 0000000..577f303 Binary files /dev/null and b/level.dat differ diff --git a/src/main/java/org/mcphackers/launchwrapper/AppletWrapper.java b/src/main/java/org/mcphackers/launchwrapper/AppletWrapper.java new file mode 100644 index 0000000..33ae7b5 --- /dev/null +++ b/src/main/java/org/mcphackers/launchwrapper/AppletWrapper.java @@ -0,0 +1,62 @@ +package org.mcphackers.launchwrapper; + +import java.applet.Applet; +import java.applet.AppletStub; +import java.net.MalformedURLException; +import java.net.URL; + +public class AppletWrapper extends Applet implements AppletStub { + private static final long serialVersionUID = 1L; + + private Object instance; + private LaunchTarget launchTarget; + + public AppletWrapper(LaunchTarget launchTarget) { + this.launchTarget = launchTarget; + } + + public void setInstance(Object instance) { + this.instance = instance; + } + + public void appletResize(int width, int height) { + if(instance != null) { + launchTarget.setWidth(instance, width); + launchTarget.setHeight(instance, height); + } + } + + @Override + public boolean isActive() { + return true; + } + + @Override + public URL getDocumentBase() { + try { + return new URL("http://www.minecraft.net/game/"); + } catch (MalformedURLException e) { + e.printStackTrace(); + } + return null; + } + + @Override + public URL getCodeBase() { + try { + return new URL("http://www.minecraft.net/game/"); + } catch (MalformedURLException e) { + e.printStackTrace(); + } + return null; + } + + @Override + public String getParameter(String paramName) { + if(paramName.equals("stand-alone")) { + return "true"; + } + System.err.println("Client asked for parameter: " + paramName); + return null; + } +} diff --git a/src/main/java/org/mcphackers/launchwrapper/Launch.java b/src/main/java/org/mcphackers/launchwrapper/Launch.java new file mode 100644 index 0000000..1f48be0 --- /dev/null +++ b/src/main/java/org/mcphackers/launchwrapper/Launch.java @@ -0,0 +1,218 @@ +package org.mcphackers.launchwrapper; + +import java.applet.Applet; +import java.awt.BorderLayout; +import java.awt.Canvas; +import java.awt.Color; +import java.awt.Dimension; +import java.awt.Frame; +import java.io.File; +import java.io.IOException; +import java.lang.reflect.Field; + +public class Launch { + + public static final ClassLoader CLASS_LOADER = ClassLoader.getSystemClassLoader(); + + public static final String[] MAIN_CLASSES = { + "net.minecraft.client.Minecraft", + "com.mojang.minecraft.Minecraft", + "com.mojang.minecraft.RubyDung", + "com.mojang.rubydung.RubyDung" + }; + public static final String[] MAIN_APPLETS = { + "net.minecraft.client.MinecraftApplet", + "com.mojang.minecraft.MinecraftApplet" + }; + + + public static void main(String[] args) { + File gameDir = null; + String username = null; + String sessionId = null; + for(int i = 0; i < args.length; i++) { + switch(i) { + case 0: + username = args[i]; + break; + case 1: + sessionId = args[i]; + break; + case 2: + gameDir = new File(args[i]); + break; + } + } + + new Launch() + .setUser(username, sessionId) + .setGameDirectory(gameDir) + .setLWJGLFrame(true) + .setFullscreen(true) + .launch(); + } + + private String username, sessionId; + private int width, height; + private boolean fullscreen; + private File gameDir; + private String serverIp; + private int serverPort; + private boolean lwjglFrame; + private boolean useMain = true; + + public Launch() { + setUser(null, null); + setResolution(854, 480); + } + + public Launch(String username, String sessionId) { + this(); + setUser(username, sessionId); + } + + public Launch setUser(String username, String sessionId) { + this.username = username != null ? username : "Player" + System.currentTimeMillis() % 1000L; + this.sessionId = sessionId != null ? sessionId : "-"; + return this; + } + + public Launch setServer(String ip, int port) { + this.serverIp = ip; + this.serverPort = port; + return this; + } + + public Launch setResolution(int w, int h) { + this.width = w; + this.height = h; + return this; + } + + public Launch setFullscreen(boolean fullscreen) { + this.fullscreen = fullscreen; + return this; + } + + public Launch setGameDirectory(File file) { + this.gameDir = file; + return this; + } + + public Launch setLWJGLFrame(boolean f) { + lwjglFrame = f; + return this; + } + + public void launch() { + LaunchTarget launchTarget = getLaunchTarget(); + + if(launchTarget == null) { + System.err.println("Could not find launch target"); + return; + } + if(launchTarget.main != null && useMain && Util.isStatic(launchTarget.mcDir)) { + try { + Util.setField(null, launchTarget.mcDir, gameDir); + launchTarget.main.invoke(null, (Object) new String[] {username, sessionId}); + } catch (Exception e) { + e.printStackTrace(); + } + return; + } + Frame frame = null; + Canvas canvas = null; + Applet applet = launchTarget.getAppletInstance(); + boolean useLWJGLFrame = lwjglFrame || !launchTarget.supportsCanvas(); + if(!useLWJGLFrame) { + canvas = new Canvas(); + applet.setLayout(new BorderLayout()); + applet.add(canvas, "Center"); + frame = new Frame("Minecraft"); + frame.setBackground(Color.BLACK); + frame.setLayout(new BorderLayout()); + frame.add(applet, "Center"); + canvas.setPreferredSize(new Dimension(width, height)); + frame.pack(); + frame.setLocationRelativeTo(null); + } + AppletWrapper stubApplet = new AppletWrapper(launchTarget); + if(applet != null) { + applet.setStub(stubApplet); + } + if(applet != null) { + applet.setStub(new AppletWrapper(launchTarget)); + } + Runnable mc = launchTarget.getInstance(frame, canvas, applet, width, height, fullscreen, false); + stubApplet.setInstance(mc); + if(mc == null) { + System.err.println("Could not create Minecraft instance!"); + return; + } + Thread mcThread = new Thread(mc, "Minecraft main thread"); + mcThread.setPriority(10); + + launchTarget.setAppletMode(mc, false); + launchTarget.setUser(mc, username, sessionId); + if(gameDir != null && launchTarget.mcDir != null) { + Util.setField(mc, launchTarget.mcDir, gameDir); + } + + if(serverIp != null) { + launchTarget.setServer(mc, serverIp, serverPort); + } + if(!useLWJGLFrame) { + frame.setVisible(true); + frame.addWindowListener(launchTarget.getWindowListener(mc, mcThread)); + } + mcThread.start(); + } + + public static LaunchTarget getLaunchTarget() { + try { + Class applet = getApplet(); + LaunchTarget target = new LaunchTarget(getMinecraft(applet), applet); + return target; + } catch (IOException e) { + e.printStackTrace(); + } + return null; + } + public static Class getApplet() { + Class applet = null; + for(String main : MAIN_APPLETS) { + try { + applet = CLASS_LOADER.loadClass(main); + } + catch (ClassNotFoundException e) {}; + } + return applet == null ? null : applet.asSubclass(Applet.class); + } + + public static Class getMinecraft(Class applet) { + Class launchTarget = null; + searchMain: + for(String main : MAIN_CLASSES) { + try { + Class cls = CLASS_LOADER.loadClass(main); + for(Class cls2 : cls.getInterfaces()) { + if(cls2 == Runnable.class) { + launchTarget = cls; + break searchMain; + } + } + } + catch (ClassNotFoundException e) {}; + } + if(launchTarget == null && applet != null) { + for(Field field : applet.getDeclaredFields()) { + Class type = field.getType(); + if(type != Canvas.class && type != Thread.class) { + launchTarget = type; + } + } + } + return launchTarget; + } + +} diff --git a/src/main/java/org/mcphackers/launchwrapper/LaunchTarget.java b/src/main/java/org/mcphackers/launchwrapper/LaunchTarget.java new file mode 100644 index 0000000..b988dab --- /dev/null +++ b/src/main/java/org/mcphackers/launchwrapper/LaunchTarget.java @@ -0,0 +1,300 @@ +package org.mcphackers.launchwrapper; + +import java.applet.Applet; +import java.awt.Canvas; +import java.awt.Component; +import java.awt.Frame; +import java.awt.event.WindowAdapter; +import java.awt.event.WindowEvent; +import java.awt.event.WindowListener; +import java.io.File; +import java.io.IOException; +import java.lang.reflect.Constructor; +import java.lang.reflect.Field; +import java.lang.reflect.Method; + +import org.objectweb.asm.ClassReader; +import org.objectweb.asm.Opcodes; +import org.objectweb.asm.Type; +import org.objectweb.asm.tree.AbstractInsnNode; +import org.objectweb.asm.tree.ClassNode; +import org.objectweb.asm.tree.FieldInsnNode; +import org.objectweb.asm.tree.FieldNode; +import org.objectweb.asm.tree.LdcInsnNode; +import org.objectweb.asm.tree.MethodInsnNode; +import org.objectweb.asm.tree.MethodNode; + +public class LaunchTarget { + public final Class minecraft; + public final Class minecraftApplet; + public Constructor session; + public Field sessionField; + public Field running; + public Field width; + public Field height; + public Field mcDir; + public Field user; + public Method main; + private Constructor mcConstruct; + + public LaunchTarget(Class mc, Class mcApplet) throws IOException { + minecraft = mc; + minecraftApplet = mcApplet; + readClasses(); + } + + private void readClasses() throws IOException { + ClassNode classNode = new ClassNode(); + ClassReader classReader = new ClassReader(minecraft.getName()); + classReader.accept(classNode, ClassReader.EXPAND_FRAMES); + try { + main = minecraft.getMethod("main", new Class[] { String[].class }); + } catch (Exception e) { + // No main method + } + //MethodNode init = null; + for(MethodNode method : classNode.methods) { + if("run".equals(method.name) && "()V".equals(method.desc)) { + AbstractInsnNode insn = method.instructions.getFirst(); + while(insn != null) { + if(insn.getOpcode() == Opcodes.PUTFIELD) { + FieldInsnNode putField = (FieldInsnNode)insn; + if("Z".equals(putField.desc)) { + try { + running = minecraft.getDeclaredField(putField.name); + if(!running.isAccessible()) { + running.setAccessible(true); + } + } catch (Exception e) { + e.printStackTrace(); + } + } else { + // Stop looking for it, it's hopeless + break; + } + } + insn = insn.getNext(); + } + } + if("".equals(method.name)) { + //init = method; TODO + } + } + FieldNode mcDirectory = null; + for(FieldNode field : classNode.fields) { + if(Type.getDescriptor(File.class).equals(field.desc)) { + mcDirectory = field; // Possible candidate (Needed for infdev) + if((field.access & Opcodes.ACC_STATIC) != 0) { + // Definitely the mc directory + break; + } + } + } + if(mcDirectory != null) { + try { + mcDir = minecraft.getDeclaredField(mcDirectory.name); + if(!mcDir.isAccessible()) { + mcDir.setAccessible(true); + } + } catch (Exception e) {} + } + Class minecraftImpl = minecraft; + if(minecraftApplet != null) { + classNode = new ClassNode(); + classReader = new ClassReader(minecraftApplet.getName()); + classReader.accept(classNode, ClassReader.EXPAND_FRAMES); + String mcAppletName = minecraftApplet.getName().replace('.', '/'); + String mcName = minecraft.getName().replace('.', '/'); + String mcDesc = "L" + mcName + ";"; + String mcField = null; + for(FieldNode field : classNode.fields) { + if(mcDesc.equals(field.desc)) { + mcField = field.name; + } + } + + for(MethodNode method : classNode.methods) { + if("init".equals(method.name) && "()V".equals(method.desc)) { + AbstractInsnNode insn = method.instructions.getFirst(); + while(insn != null) { + if(insn.getOpcode() == Opcodes.PUTFIELD) { + FieldInsnNode putField = (FieldInsnNode)insn; + if(mcDesc.equals(putField.desc) && mcAppletName.equals(putField.owner) && putField.name.equals(mcField)) { + if(putField.getPrevious().getOpcode() == Opcodes.INVOKESPECIAL) { + MethodInsnNode invoke = (MethodInsnNode)putField.getPrevious(); + if("".equals(invoke.name)) { + try { + minecraftImpl = Launch.CLASS_LOADER.loadClass(invoke.owner.replace('/', '.')); + } catch (ClassNotFoundException e) { + minecraftImpl = null; + e.printStackTrace(); + } + } + } + } + } + AbstractInsnNode insn2 = insn; + if(insn2.getOpcode() == Opcodes.ALOAD) { + insn2 = insn2.getNext(); + if(insn2 != null && insn2.getOpcode() == Opcodes.LDC && ((LdcInsnNode)insn2).cst.equals("username")) { + insn2 = insn2.getNext(); + if(insn2 != null && insn2.getOpcode() == Opcodes.INVOKEVIRTUAL) { + MethodInsnNode invoke = (MethodInsnNode)insn2; + if(invoke.owner.equals(mcAppletName) && invoke.name.equals("getParameter") && invoke.desc.equals("(Ljava/lang/String;)Ljava/lang/String;")) { + insn2 = insn2.getNext(); + if(insn2.getOpcode() == Opcodes.ALOAD) { + insn2 = insn2.getNext(); + if(insn2 != null && insn2.getOpcode() == Opcodes.LDC && ((LdcInsnNode)insn2).cst.equals("sessionid")) { + insn2 = insn2.getNext(); + if(insn2 != null && insn2.getOpcode() == Opcodes.INVOKEVIRTUAL) { + invoke = (MethodInsnNode)insn2; + if(invoke.owner.equals(mcAppletName) && invoke.name.equals("getParameter") && invoke.desc.equals("(Ljava/lang/String;)Ljava/lang/String;")) { + insn2 = insn2.getNext(); + if(insn2 != null && insn2.getOpcode() == Opcodes.INVOKESPECIAL) { + invoke = (MethodInsnNode)insn2; + if(invoke.name.equals("") && invoke.desc.equals("(Ljava/lang/String;Ljava/lang/String;)V")) { + try { + session = Launch.CLASS_LOADER.loadClass(invoke.owner.replace('/', '.')).getConstructor(String.class, String.class); + } catch (Exception e) { + e.printStackTrace(); + } + } + insn2 = insn2.getNext(); + if(insn2 != null && insn2.getOpcode() == Opcodes.PUTFIELD) { + FieldInsnNode field = (FieldInsnNode)insn2; + if(field.owner.equals(mcName)) { + try { + sessionField = minecraft.getDeclaredField(field.name); + } catch (Exception e) { + e.printStackTrace(); + } + } + } + } + } + } + } + } + } + } + } + } + insn = insn.getNext(); + } + } + } + } + + Constructor[] constructors = minecraftImpl.getConstructors(); + if(constructors.length >= 1) { + mcConstruct = constructors[0]; + } + if(constructors.length > 1) { + System.out.println("WARNING: Main class contains more than one constructor!"); + } + + } + + public Applet getAppletInstance() { + if(minecraftApplet == null) return null; + try { + Applet appletInstance = minecraftApplet.newInstance(); + return appletInstance; + } catch (Exception e) { + e.printStackTrace(); + return null; + } + } + + public boolean supportsCanvas() { + return mcConstruct.getParameterTypes().length != 0; + } + + public Runnable getInstance(Frame frame, Canvas canvas, Applet applet, int w, int h, boolean fullscreen, boolean appletMode) { + //TODO Set appletMode + //TODO Resize width and height + Class[] types = mcConstruct.getParameterTypes(); + try { + if(types.length == 0) { + return (Runnable)mcConstruct.newInstance(); + } + if(types.length == 4 + && types[0] == Canvas.class + && types[1] == int.class + && types[2] == int.class + && types[3] == boolean.class) { + return (Runnable)mcConstruct.newInstance(canvas, w, h, fullscreen); + } + if(types.length == 5 + && types[0] == Canvas.class + && types[1] == minecraftApplet + && types[2] == int.class + && types[3] == int.class + && types[4] == boolean.class) { + return (Runnable)mcConstruct.newInstance(canvas, applet, w, h, fullscreen); + } + if(types.length == 6 + && types[0] == Component.class + && types[1] == Canvas.class + && types[2] == minecraftApplet + && types[3] == int.class + && types[4] == int.class + && types[5] == boolean.class) { + return (Runnable)mcConstruct.newInstance(frame, canvas, applet, w, h, fullscreen); + } + if(types.length == 7 + && types[0] == minecraftApplet + && types[1] == Component.class + && types[2] == Canvas.class + && types[3] == minecraftApplet + && types[4] == int.class + && types[5] == int.class + && types[6] == boolean.class) { + return (Runnable)mcConstruct.newInstance(applet, frame, canvas, applet, w, h, fullscreen); + } + } + catch (Exception e) { + e.printStackTrace(); + } + return null; + } + + public WindowListener getWindowListener(final Runnable mc, final Thread thread) { + return new WindowAdapter() { + public void windowClosing(WindowEvent event) { + if(running != null) { + Util.setField(mc, running, false); + try { + thread.join(); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + System.exit(0); + } + }; + } + + public void setAppletMode(Object instance, boolean b) { + Util.setField(instance, null, b); //FIXME + } + + public void setUser(Object instance, String username, String sessionId) { + if(session != null && sessionField != null) { + try { + Util.setField(instance, sessionField, session.newInstance(username, sessionId)); + } catch (Exception e) { + e.printStackTrace(); + } + } + } + + public void setServer(Object instance, String serverIp, int serverPort) { + } + + public void setWidth(Object instance, int width) { + } + + public void setHeight(Object instance, int height) { + } +} diff --git a/src/main/java/org/mcphackers/launchwrapper/Util.java b/src/main/java/org/mcphackers/launchwrapper/Util.java new file mode 100644 index 0000000..c8f299e --- /dev/null +++ b/src/main/java/org/mcphackers/launchwrapper/Util.java @@ -0,0 +1,21 @@ +package org.mcphackers.launchwrapper; + +import java.lang.reflect.Field; +import java.lang.reflect.Modifier; + +public final class Util { + + public static void setField(Object instance, Field field, Object value) { + if(field != null) { + try { + field.set(instance, value); + } + catch (Exception e) { } + } + } + + public static boolean isStatic(Field field) { + return (field.getModifiers() & Modifier.STATIC) != 0; + } + +}