Skip to content

Commit

Permalink
Fix: Indev saving to home directory. Closes #15
Browse files Browse the repository at this point in the history
  • Loading branch information
Moresteck committed Oct 16, 2024
1 parent 9c450a7 commit 326a9de
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 8 deletions.
18 changes: 11 additions & 7 deletions src/main/java/uk/betacraft/legacyfix/LegacyFixLauncher.java
Original file line number Diff line number Diff line change
Expand Up @@ -118,37 +118,41 @@ public static boolean hasKey(String key) {
return arguments.contains("--" + key);
}

public static String getScreenshotsDir() {
return getValue("screenshotsDir", new File(getGameDir(), "screenshots").getPath());
}

public static String getGameDir() {
return LegacyFixLauncher.getValue("gameDir", "minecraft");
return getValue("gameDir", "minecraft");
}

public static String getAssetsDir() {
return LegacyFixLauncher.getValue("assetsDir", "assets");
return getValue("assetsDir", "assets");
}

public static String getAssetIndexPath() {
String assetIndex = LegacyFixLauncher.getValue("assetIndex", "empty");
String assetIndex = getValue("assetIndex", "empty");

return new File(getAssetsDir(), "indexes/" + assetIndex + ".json").getAbsolutePath();
}

// Used by DeAwtPatch & LWJGLFramePatch
public static int getWidth() {
return Integer.parseInt(LegacyFixLauncher.getValue("width", "854"));
return Integer.parseInt(getValue("width", "854"));
}

// Used by DeAwtPatch & LWJGLFramePatch
public static int getHeight() {
return Integer.parseInt(LegacyFixLauncher.getValue("height", "480"));
return Integer.parseInt(getValue("height", "480"));
}

// Used by DeAwtPatch
public static boolean getFullscreen() {
return Boolean.parseBoolean(LegacyFixLauncher.getValue("fullscreen", "false"));
return hasKey("fullscreen");
}

// Used by LWJGLFramePatch
public static String getFrameName() {
return LegacyFixLauncher.getValue("version", "Minecraft");
return getValue("version", "Minecraft");
}
}
17 changes: 17 additions & 0 deletions src/main/java/uk/betacraft/legacyfix/patch/PatchHelper.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package uk.betacraft.legacyfix.patch;

import java.io.File;
import java.lang.reflect.Modifier;

import javassist.ClassPool;
Expand All @@ -10,6 +11,7 @@
import javassist.bytecode.ConstPool;
import uk.betacraft.legacyfix.LFLogger;
import uk.betacraft.legacyfix.LegacyFixAgent;
import uk.betacraft.legacyfix.LegacyFixLauncher;

public class PatchHelper {
public static final CtClass stringClass = ClassPool.getDefault().getOrNull("java.lang.String");
Expand Down Expand Up @@ -159,6 +161,21 @@ public static CtClass findMouseHelperClass(ClassPool pool) throws NotFoundExcept
return mouseHelperClass;
}

// Used by GameDirPatch
public static File getIndevMapRenderFromExpectedPath(File file) {
String fileName = file.getName();
File expectedFile = new File(new File(System.getProperty("user.home", ".")), fileName).getAbsoluteFile();

if (!fileName.startsWith("mc_map_") ||
!fileName.endsWith(".png") ||
!expectedFile.getPath().equals(file.getAbsoluteFile().getPath())) {
return null;
}

return new File(LegacyFixLauncher.getScreenshotsDir(), fileName).getAbsoluteFile();
}

// Bytecode manipulation helper methods
public static boolean isString(ConstPool constPool, int ldcPos) {
return constPool.getTag(ldcPos) == ConstPool.CONST_String;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import javassist.CtClass;
import javassist.CtConstructor;
import javassist.CtMethod;
import uk.betacraft.legacyfix.LegacyFixAgent;
import uk.betacraft.legacyfix.patch.Patch;
import uk.betacraft.legacyfix.patch.PatchException;
import uk.betacraft.legacyfix.patch.PatchHelper;
Expand Down Expand Up @@ -47,6 +46,13 @@ public void apply(Instrumentation inst) throws PatchException, Exception {
" return true;" +
" }" +
" }" +
" if ($0.path.endsWith(\".png\")) {" +
" Class patchHelper = ClassLoader.getSystemClassLoader().loadClass(\"uk.betacraft.legacyfix.patch.PatchHelper\");" +
" java.io.File newFile = (java.io.File) patchHelper.getMethod(\"getIndevMapRenderFromExpectedPath\", new Class[] {java.io.File.class}).invoke(null, new Object[] {$0});" +
" if (newFile != null) {" +
" return newFile.exists();" +
" }" +
" }" +
" } catch (Throwable t) { t.printStackTrace(); }" +
"}"
);
Expand Down Expand Up @@ -125,5 +131,21 @@ public void apply(Instrumentation inst) throws PatchException, Exception {
);

inst.redefineClasses(new ClassDefinition(Class.forName(fileInputStreamClass.getName()), fileInputStreamClass.toBytecode()));

CtClass fileOutputStreamClass = pool.get("java.io.FileOutputStream");
CtConstructor fileOutputStreamConstructor = fileOutputStreamClass.getDeclaredConstructor(
new CtClass[]{fileClass});

fileOutputStreamConstructor.insertBefore(
"try {" +
" Class patchHelper = ClassLoader.getSystemClassLoader().loadClass(\"uk.betacraft.legacyfix.patch.PatchHelper\");" +
" java.io.File newFile = (java.io.File) patchHelper.getMethod(\"getIndevMapRenderFromExpectedPath\", new Class[] {java.io.File.class}).invoke(null, new Object[] {$1});" +
" if (newFile != null) {" +
" $1 = newFile;" +
" }" +
"} catch (Throwable t) { t.printStackTrace(); }"
);

inst.redefineClasses(new ClassDefinition(Class.forName(fileOutputStreamClass.getName()), fileOutputStreamClass.toBytecode()));
}
}

0 comments on commit 326a9de

Please sign in to comment.