Skip to content

Commit

Permalink
Fix level directory parameter. Patch cancel button for error screen i…
Browse files Browse the repository at this point in the history
…n Indev
  • Loading branch information
Lassebq committed Nov 6, 2024
1 parent bd974ef commit 2d04640
Show file tree
Hide file tree
Showing 11 changed files with 114 additions and 115 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@

public class LWGameTransformer extends GameTransformer implements ClassNodeSource {
private final LWGameProvider gameProvider;
private FabricLauncher launcher;
private Map<String, ClassNode> modified;
private Function<String, ClassNode> classSource;
private boolean entrypointsLocated = false;
Expand All @@ -44,6 +45,7 @@ public void locateEntrypoints(FabricLauncher launcher, List<Path> gameJars) {
}

modified = new HashMap<>();
this.launcher = launcher;

try (SimpleClassPath cp = new SimpleClassPath(gameJars)) {
classSource = name -> {
Expand All @@ -56,8 +58,7 @@ public void locateEntrypoints(FabricLauncher launcher, List<Path> gameJars) {
try {
CpEntry entry = cp.getEntry(LoaderUtil.getClassFileName(name));
if (entry == null) {
// SafeClassWriter needs access to whole classpath
return NodeHelper.readClass(launcher.getResourceAsStream(LoaderUtil.getClassFileName(name)), 0);
return null;
}

try (InputStream is = entry.getInputStream()) {
Expand All @@ -83,7 +84,15 @@ public void locateEntrypoints(FabricLauncher launcher, List<Path> gameJars) {
}

public ClassNode getClass(String name) {
return classSource.apply(name.replace("/", "."));
ClassNode node = classSource.apply(name.replace("/", "."));
if (node == null) {
try {
node = NodeHelper.readClass(launcher.getResourceAsStream(LoaderUtil.getClassFileName(name)), 0);
} catch (IOException e) {
e.printStackTrace();
}
}
return node;
}

public void overrideClass(ClassNode node) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/net/minecraft/launchwrapper/Launch.java
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ private void runTweakers(LaunchClassLoader loader) {
LOGGER.log("Loading tweak class name %s", tweakName);

loader.addClassLoaderExclusion(tweakName.substring(0, tweakName.lastIndexOf('.')));
final ITweaker tweaker = (ITweaker)Class.forName(tweakName, true, getClass().getClassLoader()).newInstance();
final ITweaker tweaker = (ITweaker)Class.forName(tweakName, true, loader).newInstance();
tweakers.add(tweaker);

it.remove();
Expand Down
69 changes: 27 additions & 42 deletions src/main/java/net/minecraft/launchwrapper/LaunchClassLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,41 @@ public class LaunchClassLoader extends org.mcphackers.launchwrapper.loader.Launc
private final List<URL> sources = new ArrayList<URL>();

private final Map<String, Class<?>> cachedClasses = Collections.<String, Class<?>>emptyMap();
private final Map<String, byte[]> resourceCache = Collections.<String, byte[]>emptyMap();
private final Set<String> invalidClasses = new HashSet<String>();
private final Set<String> classLoaderExceptions;
private final Set<String> transformerExceptions = new HashSet<String>();
private final List<IClassTransformer> transformers = new ArrayList<IClassTransformer>();
public IClassNameTransformer renameTransformer;
private IClassNameTransformer renameTransformer;

// Avoids exceptions with mods like FoamFix and CensoredASM
@Deprecated
private static final Manifest EMPTY = new Manifest();
@Deprecated
private Map<Package, Manifest> packageManifests = null;

public LaunchClassLoader(ClassLoader parent) {
super(parent);
// Some mods refer to these fields via reflection
this(getInitialURLs(parent), parent);
}
public LaunchClassLoader(URL[] sources) {
this(sources, LaunchClassLoader.class.getClassLoader());
}
public LaunchClassLoader(URL[] sources, ClassLoader parent) {
super(sources, parent);
classLoaderExceptions = exclusions;
addClassLoaderExclusion("org.lwjgl.");
addClassLoaderExclusion("org.apache.logging.");
addClassLoaderExclusion("net.minecraft.launchwrapper.");

addTransformerExclusion("javax.");
addTransformerExclusion("argo.");
addTransformerExclusion("org.objectweb.asm.");
addTransformerExclusion("com.google.common.");
addTransformerExclusion("org.bouncycastle.");
addTransformerExclusion("net.minecraft.launchwrapper.injector.");
}

public void registerTransformer(String transformerClassName) {
try {
IClassTransformer transformer = (IClassTransformer)Launch.classLoader.loadClass(transformerClassName).newInstance();
IClassTransformer transformer = (IClassTransformer)loadClass(transformerClassName).newInstance();
transformers.add(transformer);
if (transformer instanceof IClassNameTransformer && renameTransformer == null) {
renameTransformer = (IClassNameTransformer)transformer;
Expand All @@ -51,12 +65,12 @@ public List<IClassTransformer> getTransformers() {
return Collections.unmodifiableList(transformers);
}

public void addClassLoaderExclusion(String pkg) {
addExclusion(pkg);
public void addClassLoaderExclusion(String prefix) {
addExclusion(prefix);
}

public void addTransformerExclusion(String pkg) {
transformerExceptions.add(pkg + (pkg.endsWith(".") ? "" : "."));
public void addTransformerExclusion(String prefix) {
transformerExceptions.add(prefix);
}

private String untransformName(final String name) {
Expand All @@ -73,27 +87,6 @@ private String transformName(final String name) {
return name;
}

/**
* Foundation API extension
* used by Cleanroom
*
* @param name class name
* @return
*/
public boolean isClassLoaded(String name) {
return getLoadedClass(name) != null;
}

/**
* TODO Foundation API extension
* used by Cleanroom
*
* @param transformerClassName
*/
public void registerSuperTransformer(String transformerClassName) {
registerTransformer(transformerClassName);
}

@Override
public void addURL(URL url) {
super.addURL(url);
Expand All @@ -108,15 +101,7 @@ public void clearNegativeEntries(Set<String> entriesToClear) {
invalidClasses.clear();
}

/**
* Called by Cleanroom loader
*
* @param name
* @param transformedName
* @param data
* @return
*/
public byte[] runTrasformers(String name, String transformedName, byte[] data) {
private byte[] runTrasformers(String name, String transformedName, byte[] data) {
byte[] transformed = data;
for (IClassTransformer transformer : transformers) {
transformed = transformer.transform(name, transformedName, transformed);
Expand All @@ -125,7 +110,7 @@ public byte[] runTrasformers(String name, String transformedName, byte[] data) {
}

@Override
public byte[] getTransformedClass(String name) throws IOException {
protected byte[] getTransformedClass(String name) throws IOException {
String untransformedName = untransformName(name);
String transformedName = transformName(name);
byte[] classData = getClassBytes(untransformedName);
Expand All @@ -138,7 +123,7 @@ public byte[] getTransformedClass(String name) throws IOException {
}

@Override
public String getTransformedName(String name) {
protected String getTransformedName(String name) {
return transformName(name);
}

Expand Down
9 changes: 5 additions & 4 deletions src/main/java/org/mcphackers/launchwrapper/LaunchConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public class LaunchConfig {
private final LaunchParameterSwitch awtFrame = new LaunchParameterSwitchReverse("awtFrame", lwjglFrame);
public final LaunchParameterEnum<SkinType> skinProxy = new LaunchParameterEnum<SkinType>("skinProxy", SkinType.DEFAULT, true);
public final LaunchParameterSkinOptions skinOptions = new LaunchParameterSkinOptions("skinOptions");
public final LaunchParameterFile levelsDir = new LaunchParameterFile("levelsDir", new File(DEFAULT_GAME_DIR, "levels"), true);
public final LaunchParameterFile levelsDir = new LaunchParameterFile("levelsDir", null, true);
public final LaunchParameterSwitch isom = new LaunchParameterSwitch("isom", false, true);
public final LaunchParameterFileList icon = new LaunchParameterFileList("icon", null, true);
public final LaunchParameterString title = new LaunchParameterString("title", null, true);
Expand Down Expand Up @@ -124,6 +124,7 @@ public LaunchConfig clone() {
}

public LaunchConfig() {
levelsDir.set(new File(DEFAULT_GAME_DIR, "levels"));
}

public LaunchConfig(String[] args) {
Expand All @@ -150,6 +151,9 @@ public LaunchConfig(String[] args) {
}
}
}
if (levelsDir.get() == null) {
levelsDir.set(new File(gameDir.get(), "levels"));
}
if (uuid.get() == null && username.get() != null) {
// Resolve profile UUID if we only provide username
uuid.set(SkinRequests.getUUIDfromName(username.get()));
Expand Down Expand Up @@ -193,9 +197,6 @@ public String[] getArgs() {
}
}
for (String param : extraParameters) {
if (param == null) {
continue;
}
list.add(param);
}
String[] arr = new String[list.size()];
Expand Down
43 changes: 0 additions & 43 deletions src/main/java/org/mcphackers/launchwrapper/inject/Inject.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -68,19 +68,22 @@ public void addURL(URL url) {
}

public LaunchClassLoader(ClassLoader parent) {
super(getInitialURLs(parent), null);
this(getInitialURLs(parent), parent);
}

protected LaunchClassLoader(URL[] sources, ClassLoader parent) {
super(sources, null);
this.parent = parent;
this.urlClassLoader = new HighPriorityClassLoader(new URL[0]);
addExclusion("java");
addExclusion("javax");
addExclusion("sun");
addExclusion("net.minecraft.launchwrapper");
addExclusion("org.mcphackers.launchwrapper");
addExclusion("org.objectweb.asm");
addExclusion("org.json");
addExclusion("java.");
addExclusion("javax.");
addExclusion("sun.");
addExclusion("org.mcphackers.launchwrapper.");
addExclusion("org.objectweb.asm.");
addExclusion("org.json.");
}

private static URL[] getInitialURLs(ClassLoader parent) {
protected static URL[] getInitialURLs(ClassLoader parent) {
List<URL> urls = new ArrayList<URL>();
if (parent instanceof URLClassLoader) {
return ((URLClassLoader)parent).getURLs();
Expand Down Expand Up @@ -155,11 +158,11 @@ public Enumeration<URL> findResources(String name) throws IOException {
return parent.getResources(name);
}

public void addExclusion(String pkg) {
if (pkg == null) {
public void addExclusion(String prefix) {
if (prefix == null) {
return;
}
exclusions.add(pkg + (pkg.endsWith(".") ? "" : "."));
exclusions.add(prefix);
}

public void overrideClassSource(String name, String f) {
Expand Down Expand Up @@ -194,8 +197,8 @@ public Class<?> findClass(String name) throws ClassNotFoundException {
return redefineClass(name);
}

for (String pkg : exclusions) {
if (name.startsWith(pkg)) {
for (String prefix : exclusions) {
if (name.startsWith(prefix)) {
return parent.loadClass(name);
}
}
Expand Down Expand Up @@ -312,11 +315,11 @@ protected Class<?> redefineClass(String name) throws ClassNotFoundException {
}
}

public String getTransformedName(String name) {
protected String getTransformedName(String name) {
return name;
}

public byte[] getTransformedClass(String name) throws IOException {
protected byte[] getTransformedClass(String name) throws IOException {
return getClassBytes(name);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

public final class SaveRequests {
public static final String EMPTY_LEVEL = "-";
public static final String EMPTY_LEVEL_ALT = "---";
public static final int MAX_LEVELS = 5;

private SaveRequests() {
Expand Down Expand Up @@ -85,7 +86,7 @@ public static void saveLevel(File levelsDir, int index, String levelName, byte[]
}

// Since minecraft doesn't have a delete button on levels, just save a new one with this name and it'll get deleted
if (levelName.equals("---") /* Can't be "-" because save button is active at 3>= characters */) {
if (levelName.equals(EMPTY_LEVEL_ALT) /* Can't be "-" because save button is active at 3>= characters */) {
if (level.exists()) {
level.delete();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
import static org.mcphackers.launchwrapper.util.asm.InsnHelper.*;
import static org.objectweb.asm.Opcodes.*;

import org.mcphackers.launchwrapper.Launch;
import org.mcphackers.launchwrapper.LaunchConfig;
import org.mcphackers.launchwrapper.tweak.AppletWrapper;
import org.mcphackers.launchwrapper.tweak.injection.InjectionWithContext;
import org.mcphackers.launchwrapper.util.ClassNodeSource;
import org.mcphackers.launchwrapper.util.asm.IdentifyCall;
Expand Down Expand Up @@ -137,7 +139,7 @@ protected MethodNode getMain(ClassNodeSource source, LaunchConfig config) {
insns.add(new MethodInsnNode(INVOKESPECIAL, minecraftApplet.name, "<init>", "()V"));
insns.add(new VarInsnNode(ASTORE, appletIndex));
insns.add(new VarInsnNode(ALOAD, appletIndex));
insns.add(new MethodInsnNode(INVOKESTATIC, "org/mcphackers/launchwrapper/inject/Inject", "getApplet", "()Lorg/mcphackers/launchwrapper/tweak/AppletWrapper;"));
insns.add(new MethodInsnNode(INVOKESTATIC, "org/mcphackers/launchwrapper/tweak/injection/legacy/AddMain", "getApplet", "()Lorg/mcphackers/launchwrapper/tweak/AppletWrapper;"));
insns.add(new MethodInsnNode(INVOKEVIRTUAL, "java/applet/Applet", "setStub", "(Ljava/applet/AppletStub;)V"));
insns.add(new VarInsnNode(ALOAD, appletIndex));
insns.add(new MethodInsnNode(INVOKEVIRTUAL, minecraftApplet.name, "init", "()V"));
Expand Down Expand Up @@ -225,6 +227,10 @@ protected MethodNode getMain(ClassNodeSource source, LaunchConfig config) {
return node;
}

public static AppletWrapper getApplet() {
return new AppletWrapper(Launch.getInstance().config.getArgsAsMap());
}

protected int applyAccess(int access, int targetAccess) {
// ACC_PRIVATE, ACC_PROTECTED and ACC_PUBLIC are the first 3 bits
return (access & (~0x7)) | targetAccess;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,9 @@ public boolean apply(ClassNodeSource source, LaunchConfig config) {
}
}
if (patched) {
if (patchErrorScreen(source, errScreen, openScreen, null) && instanceOf != null) {
openScreen.instructions.set(instanceOf, new InsnNode(ICONST_0));
}
return true;
}
}
Expand Down
Loading

0 comments on commit 2d04640

Please sign in to comment.