Skip to content

Commit 2107597

Browse files
committed
Add applet remover
1 parent 61dee69 commit 2107597

File tree

8 files changed

+224
-36
lines changed

8 files changed

+224
-36
lines changed

build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ def depends = [
1717
"org.ow2.asm:asm:${asm_version}",
1818
// "org.ow2.asm:asm-util:${asm_version}",
1919
"org.ow2.asm:asm-tree:${asm_version}",
20+
"org.ow2.asm:asm-commons:${asm_version}",
2021
"org.json:json:20240303",
2122
]
2223

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package org.mcphackers.launchwrapper.applet;
2+
import java.awt.Dimension;
3+
import java.awt.Panel;
4+
import java.net.URL;
5+
import java.util.Locale;
6+
7+
public class Applet extends Panel {
8+
private AppletStub stub;
9+
10+
public final void setStub(AppletStub stub) {
11+
this.stub = stub;
12+
}
13+
14+
public boolean isActive() {
15+
if (stub != null) {
16+
return stub.isActive();
17+
} else {
18+
return false;
19+
}
20+
}
21+
22+
public URL getDocumentBase() {
23+
return stub.getDocumentBase();
24+
}
25+
26+
public URL getCodeBase() {
27+
return stub.getCodeBase();
28+
}
29+
30+
public String getParameter(String name) {
31+
return stub.getParameter(name);
32+
}
33+
34+
@SuppressWarnings("deprecation")
35+
@Override
36+
public void resize(int width, int height) {
37+
Dimension d = size();
38+
39+
if ((d.width != width) || (d.height != height)) {
40+
super.resize(width, height);
41+
}
42+
}
43+
44+
@SuppressWarnings("deprecation")
45+
@Override
46+
public void resize(Dimension d) {
47+
resize(d.width, d.height);
48+
}
49+
50+
@Override
51+
public boolean isValidateRoot() {
52+
return true;
53+
}
54+
55+
@Override
56+
public Locale getLocale() {
57+
Locale locale = super.getLocale();
58+
59+
if (locale == null) {
60+
return Locale.getDefault();
61+
}
62+
63+
return locale;
64+
}
65+
66+
public void init() {
67+
}
68+
69+
public void start() {
70+
}
71+
72+
public void stop() {
73+
}
74+
75+
public void destroy() {
76+
}
77+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package org.mcphackers.launchwrapper.applet;
2+
3+
import java.net.URL;
4+
5+
public interface AppletStub {
6+
boolean isActive();
7+
8+
URL getDocumentBase();
9+
10+
URL getCodeBase();
11+
12+
String getParameter(String name);
13+
14+
// void appletResize(int width, int height);
15+
}

src/main/java/org/mcphackers/launchwrapper/target/AppletLaunchTarget.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package org.mcphackers.launchwrapper.target;
22

3-
import java.applet.Applet;
43
import java.awt.Image;
54

5+
import org.mcphackers.launchwrapper.applet.Applet;
66
import org.mcphackers.launchwrapper.loader.LaunchClassLoader;
77
import org.mcphackers.launchwrapper.tweak.AppletWrapper;
88

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
package org.mcphackers.launchwrapper.tweak;
2+
3+
import static org.mcphackers.launchwrapper.util.asm.InsnHelper.*;
4+
import static org.objectweb.asm.tree.AbstractInsnNode.*;
5+
6+
import java.util.ArrayList;
7+
import java.util.Collections;
8+
import java.util.HashMap;
9+
import java.util.List;
10+
import java.util.Map;
11+
12+
import org.mcphackers.launchwrapper.util.ClassNodeSource;
13+
import org.objectweb.asm.commons.Remapper;
14+
import org.objectweb.asm.commons.SimpleRemapper;
15+
import org.objectweb.asm.tree.AbstractInsnNode;
16+
import org.objectweb.asm.tree.ClassNode;
17+
import org.objectweb.asm.tree.FieldInsnNode;
18+
import org.objectweb.asm.tree.FieldNode;
19+
import org.objectweb.asm.tree.MethodInsnNode;
20+
import org.objectweb.asm.tree.MethodNode;
21+
22+
public class AppletTweaker implements Tweaker {
23+
24+
Map<String, String> renames = getRenames();
25+
Remapper remapper = new SimpleRemapper(renames);
26+
27+
private static Map<String, String> getRenames() {
28+
Map<String, String> renames = new HashMap<String, String>();
29+
renames.put("java/applet/Applet", "org/mcphackers/launchwrapper/applet/Applet");
30+
renames.put("java/applet/AppletStub", "org/mcphackers/launchwrapper/applet/AppletStub");
31+
return Collections.unmodifiableMap(renames);
32+
}
33+
34+
public boolean tweakClass(ClassNodeSource source, String name) {
35+
ClassNode node = source.getClass(name);
36+
if (node == null) {
37+
return false;
38+
}
39+
boolean changed = false;
40+
// Decrease class version
41+
if(renames.get(node.superName) != null) {
42+
node.superName = renames.get(node.superName);
43+
changed = true;
44+
}
45+
List<String> newInterfaces = new ArrayList<String>();
46+
for(String itf : node.interfaces) {
47+
if(renames.get(itf) != null) {
48+
newInterfaces.add(renames.get(itf));
49+
changed = true;
50+
} else {
51+
newInterfaces.add(itf);
52+
}
53+
}
54+
node.interfaces = newInterfaces;
55+
56+
for (MethodNode m : node.methods) {
57+
for (AbstractInsnNode insn : iterator(m.instructions)) {
58+
if(insn.getType() == METHOD_INSN) {
59+
MethodInsnNode mInsn = (MethodInsnNode)insn;
60+
if(renames.get(mInsn.owner) != null) {
61+
mInsn.owner = renames.get(mInsn.owner);
62+
changed = true;
63+
}
64+
String desc = remapper.mapMethodDesc(mInsn.desc);
65+
mInsn.desc = desc;
66+
changed |= !desc.equals(mInsn.desc);
67+
}
68+
else if(insn.getType() == FIELD_INSN) {
69+
FieldInsnNode fInsn = (FieldInsnNode)insn;
70+
if(renames.get(fInsn.owner) != null) {
71+
fInsn.owner = renames.get(fInsn.owner);
72+
changed = true;
73+
}
74+
String desc = remapper.mapDesc(fInsn.desc);
75+
fInsn.desc = desc;
76+
changed |= !desc.equals(fInsn.desc);
77+
}
78+
}
79+
String desc = remapper.mapDesc(m.desc);
80+
m.desc = desc;
81+
changed |= !desc.equals(m.desc);
82+
}
83+
for (FieldNode f : node.fields) {
84+
String desc = remapper.mapDesc(f.desc);
85+
f.desc = desc;
86+
changed |= !desc.equals(f.desc);
87+
}
88+
if (changed) {
89+
source.overrideClass(node);
90+
}
91+
return changed;
92+
}
93+
}

src/main/java/org/mcphackers/launchwrapper/tweak/AppletWrapper.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package org.mcphackers.launchwrapper.tweak;
22

3-
import java.applet.Applet;
4-
import java.applet.AppletStub;
53
import java.awt.BorderLayout;
64
import java.awt.Color;
75
import java.awt.Dimension;
@@ -14,6 +12,9 @@
1412
import java.util.Collections;
1513
import java.util.Map;
1614

15+
import org.mcphackers.launchwrapper.applet.Applet;
16+
import org.mcphackers.launchwrapper.applet.AppletStub;
17+
1718
public class AppletWrapper extends Applet implements AppletStub {
1819
private static final long serialVersionUID = 1L;
1920

src/main/java/org/mcphackers/launchwrapper/tweak/DelCharTweaker.java

Lines changed: 33 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public class DelCharTweaker implements Tweaker {
6969
public DelCharTweaker(LaunchConfig config, LegacyTweakContext context) {
7070
if (config == null || context == null) {
7171
throw new IllegalArgumentException(
72-
"Launch config and legacy tweak context must be non-null to construct DelCharTweaker.");
72+
"Launch config and legacy tweak context must be non-null to construct DelCharTweaker.");
7373
}
7474

7575
this.config = config;
@@ -119,22 +119,22 @@ private String getGuiScreenName(ClassNode node) {
119119
AbstractInsnNode[] insns = fill(method.instructions.get(index), 5);
120120

121121
if (!compareInsn(insns[0], ALOAD, 1) || !compareInsn(insns[1], ALOAD, 0) ||
122-
!compareInsn(insns[2], ILOAD) || !compareInsn(insns[3], ILOAD) ||
123-
!compareInsn(insns[4], INVOKEVIRTUAL)) {
122+
!compareInsn(insns[2], ILOAD) || !compareInsn(insns[3], ILOAD) ||
123+
!compareInsn(insns[4], INVOKEVIRTUAL)) {
124124
continue;
125125
}
126126

127-
MethodInsnNode invokeInsn = (MethodInsnNode) insns[4];
127+
MethodInsnNode invokeInsn = (MethodInsnNode)insns[4];
128128
if (!invokeInsn.owner.equals(argTypes[0].getInternalName())) {
129129
continue;
130130
}
131131

132132
Type[] invokeArgTypes = Type.getArgumentTypes(invokeInsn.desc);
133133
if (invokeArgTypes.length != 3 ||
134-
invokeArgTypes[0].getSort() != Type.OBJECT ||
135-
!invokeArgTypes[0].getInternalName().equals(node.name) ||
136-
!invokeArgTypes[1].equals(Type.INT_TYPE) ||
137-
!invokeArgTypes[2].equals(Type.INT_TYPE)) {
134+
invokeArgTypes[0].getSort() != Type.OBJECT ||
135+
!invokeArgTypes[0].getInternalName().equals(node.name) ||
136+
!invokeArgTypes[1].equals(Type.INT_TYPE) ||
137+
!invokeArgTypes[2].equals(Type.INT_TYPE)) {
138138
continue;
139139
}
140140

@@ -178,19 +178,19 @@ private boolean isGuiChat(ClassNode node) {
178178
Type fieldType = Type.getType(field.desc);
179179

180180
if ((field.access & ACC_PROTECTED) != 0 &&
181-
(field.access & ACC_STATIC) == 0 &&
182-
fieldType.getSort() == Type.OBJECT &&
183-
fieldType.getClassName().equals("java.lang.String")) {
181+
(field.access & ACC_STATIC) == 0 &&
182+
fieldType.getSort() == Type.OBJECT &&
183+
fieldType.getClassName().equals("java.lang.String")) {
184184
hasProtectedString = true;
185185
} else if ((field.access & ACC_PRIVATE) != 0 &&
186-
(field.access & ACC_STATIC) == 0 &&
187-
fieldType.equals(Type.INT_TYPE)) {
186+
(field.access & ACC_STATIC) == 0 &&
187+
fieldType.equals(Type.INT_TYPE)) {
188188
hasPrivateInt = true;
189189
} else if ((field.access & ACC_PRIVATE) != 0 &&
190-
(field.access & ACC_STATIC) != 0 &&
191-
(field.access & ACC_FINAL) != 0 &&
192-
fieldType.getSort() == Type.OBJECT &&
193-
fieldType.getClassName().equals("java.lang.String")) {
190+
(field.access & ACC_STATIC) != 0 &&
191+
(field.access & ACC_FINAL) != 0 &&
192+
fieldType.getSort() == Type.OBJECT &&
193+
fieldType.getClassName().equals("java.lang.String")) {
194194
hasPrivateStaticFinalString = true;
195195
}
196196
}
@@ -206,7 +206,7 @@ private boolean isGuiChat(ClassNode node) {
206206

207207
Type[] argTypes = Type.getArgumentTypes(method.desc);
208208
if (argTypes.length != 2 || !argTypes[0].equals(Type.CHAR_TYPE) ||
209-
!argTypes[1].equals(Type.INT_TYPE)) {
209+
!argTypes[1].equals(Type.INT_TYPE)) {
210210
continue;
211211
}
212212

@@ -238,15 +238,14 @@ private boolean isGuiTextField(ClassNode node) {
238238
Type[] argTypes = Type.getArgumentTypes(method.desc);
239239

240240
if (argTypes.length == 7 &&
241-
argTypes[0].getSort() == Type.OBJECT &&
242-
argTypes[0].getInternalName().equals(guiScreenName) &&
243-
argTypes[1].getSort() == Type.OBJECT && // FontRenderer
244-
argTypes[2].equals(Type.INT_TYPE) &&
245-
argTypes[3].equals(Type.INT_TYPE) &&
246-
argTypes[4].equals(Type.INT_TYPE) &&
247-
argTypes[5].equals(Type.INT_TYPE) &&
248-
argTypes[6].getSort() == Type.OBJECT
249-
&& argTypes[6].getClassName().equals("java.lang.String")) {
241+
argTypes[0].getSort() == Type.OBJECT &&
242+
argTypes[0].getInternalName().equals(guiScreenName) &&
243+
argTypes[1].getSort() == Type.OBJECT && // FontRenderer
244+
argTypes[2].equals(Type.INT_TYPE) &&
245+
argTypes[3].equals(Type.INT_TYPE) &&
246+
argTypes[4].equals(Type.INT_TYPE) &&
247+
argTypes[5].equals(Type.INT_TYPE) &&
248+
argTypes[6].getSort() == Type.OBJECT && argTypes[6].getClassName().equals("java.lang.String")) {
250249
return true;
251250
}
252251
}
@@ -274,13 +273,13 @@ private IntInsnNode getTargetInsn(AbstractInsnNode insn) {
274273
AbstractInsnNode[] insns = fill(insn, 4);
275274

276275
if (!compareInsn(insns[0], IFGE) ||
277-
!compareInsn(insns[1], ILOAD, 1) ||
278-
!compareInsn(insns[2], BIPUSH, 32) ||
279-
!compareInsn(insns[3], IF_ICMPLE)) {
276+
!compareInsn(insns[1], ILOAD, 1) ||
277+
!compareInsn(insns[2], BIPUSH, 32) ||
278+
!compareInsn(insns[3], IF_ICMPLE)) {
280279
return null;
281280
}
282281

283-
return (IntInsnNode) insns[2];
282+
return (IntInsnNode)insns[2];
284283
}
285284

286285
public boolean tweakClass(ClassNodeSource source, String name) {
@@ -327,7 +326,9 @@ public boolean tweakClass(ClassNodeSource source, String name) {
327326

328327
for (MethodNode method : sourceNode.methods) {
329328
for (AbstractInsnNode currentInsn = getFirst(
330-
method.instructions); currentInsn != null; currentInsn = nextInsn(currentInsn)) {
329+
method.instructions);
330+
currentInsn != null;
331+
currentInsn = nextInsn(currentInsn)) {
331332
IntInsnNode targetInsn = getTargetInsn(currentInsn);
332333
if (targetInsn == null) {
333334
continue;

src/main/java/org/mcphackers/launchwrapper/tweak/LegacyTweak.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ public List<Injection> getInjections() {
8888

8989
@Override
9090
public List<Tweaker> getTweakers() {
91-
return Arrays.<Tweaker>asList(new Java5Tweaker(), new DelCharTweaker(config, context));
91+
return Arrays.<Tweaker>asList(new Java5Tweaker(), new AppletTweaker(), new DelCharTweaker(config, context));
9292
}
9393

9494
@Override

0 commit comments

Comments
 (0)