Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent some camera related crashes #38

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions src/Client/JClassPatcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,15 @@
import org.objectweb.asm.tree.FieldInsnNode;
import org.objectweb.asm.tree.FieldNode;
import org.objectweb.asm.tree.IincInsnNode;
import org.objectweb.asm.tree.InsnList;
import org.objectweb.asm.tree.InsnNode;
import org.objectweb.asm.tree.IntInsnNode;
import org.objectweb.asm.tree.JumpInsnNode;
import org.objectweb.asm.tree.LabelNode;
import org.objectweb.asm.tree.LdcInsnNode;
import org.objectweb.asm.tree.MethodInsnNode;
import org.objectweb.asm.tree.MethodNode;
import org.objectweb.asm.tree.TryCatchBlockNode;
import org.objectweb.asm.tree.VarInsnNode;
import org.objectweb.asm.util.Printer;
import org.objectweb.asm.util.Textifier;
Expand Down Expand Up @@ -732,6 +734,14 @@ private void patchClient(ClassNode node) {
methodNode.instructions.insertBefore(
findNode,
new MethodInsnNode(Opcodes.INVOKESTATIC, "Game/Client", "init", "()V", false));

// Camera view distance crash fix
if (insnNode.getOpcode() == Opcodes.SIPUSH) {
IntInsnNode call = (IntInsnNode) insnNode;
if (call.operand == 15000) {
call.operand = 32767;
}
}
}
}
if (methodNode.name.equals("dj") && methodNode.desc.equals("()V")) {
Expand Down Expand Up @@ -2182,6 +2192,9 @@ private void patchScene(ClassNode node) {

// Patch crash due to exceeding max polygons
if (methodNode.name.equals("qi") && methodNode.desc.equals("()V")) {

safeify(methodNode);

Iterator<AbstractInsnNode> insnNodeList = methodNode.instructions.iterator();
int times = 0;
while (insnNodeList.hasNext()) {
Expand Down Expand Up @@ -2640,4 +2653,26 @@ public static JClassPatcher getInstance() {
}
return instance;
}

/**
* Wraps a given {@link MethodNode} in a try-catch block which simply returns,
* to prevent application crashes.
*
* @author Stormy
*/
private static void safeify(MethodNode methodNode) {
LabelNode method_start = new LabelNode();
LabelNode method_end = new LabelNode();
methodNode.instructions.insert(method_start);
methodNode.instructions.add(method_end);

LabelNode handler = new LabelNode();
InsnList list = new InsnList();
list.add(handler);
list.add(new InsnNode(Opcodes.RETURN));
methodNode.instructions.add(list);

TryCatchBlockNode n = new TryCatchBlockNode(method_start, method_end, handler, "java/lang/RuntimeException");
methodNode.tryCatchBlocks.add(n);
}
}