-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(mac-mouse): prevent erroneous hiding of cursor on macOS
- Loading branch information
Showing
5 changed files
with
118 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
src/main/java/tv/darkosto/sevpatches/core/hooks/MacMouseHook.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package tv.darkosto.sevpatches.core.hooks; | ||
|
||
import net.minecraft.client.Minecraft; | ||
import net.minecraftforge.fml.relauncher.Side; | ||
import net.minecraftforge.fml.relauncher.SideOnly; | ||
|
||
@SideOnly(Side.CLIENT) | ||
public class MacMouseHook { | ||
public static void setGrabbed(boolean grab) { | ||
if (grab) { | ||
Minecraft.getMinecraft().mouseHelper.grabMouseCursor(); | ||
} else { | ||
Minecraft.getMinecraft().mouseHelper.ungrabMouseCursor(); | ||
} | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
src/main/java/tv/darkosto/sevpatches/core/patches/PatchMacMouse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package tv.darkosto.sevpatches.core.patches; | ||
|
||
import org.objectweb.asm.Opcodes; | ||
import org.objectweb.asm.tree.*; | ||
import tv.darkosto.sevpatches.core.utils.AsmUtils; | ||
|
||
import java.util.Locale; | ||
|
||
import static tv.darkosto.sevpatches.core.SevPatchesLoadingPlugin.*; | ||
|
||
public class PatchMacMouse extends Patch { | ||
public PatchMacMouse(byte[] inputClass) { | ||
super(inputClass); | ||
} | ||
|
||
@Override | ||
protected boolean patch() { | ||
MethodNode grabMouse = AsmUtils.findMethod(this.classNode, GRAB_MOUSE_CURSOR); | ||
MethodNode ungrabMouse = AsmUtils.findMethod(this.classNode, UNGRAB_MOUSE_CURSOR); | ||
if (grabMouse == null || ungrabMouse == null) return false; | ||
grabMouse.instructions.insert(generateInsns(true)); | ||
ungrabMouse.instructions.insert(generateInsns(false)); | ||
|
||
return true; | ||
} | ||
|
||
@Override | ||
public byte[] apply() { | ||
String osName = System.getProperty("os.name").toLowerCase(Locale.ROOT); | ||
if (!osName.contains("mac")) { | ||
LOGGER.info("Skipping mouse patch; os is not macOS"); | ||
return inputClassBytes; | ||
} | ||
return super.apply(); | ||
} | ||
|
||
private InsnList generateInsns(boolean checkGrab) { | ||
LabelNode label = new LabelNode(); | ||
InsnList insns = new InsnList(); | ||
insns.add(new MethodInsnNode(Opcodes.INVOKESTATIC, "org/lwjgl/input/Mouse", "isGrabbed", "()Z", false)); | ||
insns.add(new JumpInsnNode(checkGrab ? Opcodes.IFEQ : Opcodes.IFNE, label)); | ||
insns.add(new InsnNode(Opcodes.RETURN)); | ||
insns.add(label); | ||
|
||
return insns; | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
src/main/java/tv/darkosto/sevpatches/core/patches/PatchMacMouseFBP.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package tv.darkosto.sevpatches.core.patches; | ||
|
||
import org.objectweb.asm.ClassWriter; | ||
import org.objectweb.asm.tree.AbstractInsnNode; | ||
import org.objectweb.asm.tree.InsnList; | ||
import org.objectweb.asm.tree.MethodInsnNode; | ||
import org.objectweb.asm.tree.MethodNode; | ||
|
||
import java.util.concurrent.atomic.AtomicInteger; | ||
import java.util.stream.Stream; | ||
import java.util.stream.StreamSupport; | ||
|
||
public class PatchMacMouseFBP extends PatchMacMouse { | ||
public PatchMacMouseFBP(byte[] inputClass) { | ||
super(inputClass); | ||
} | ||
|
||
@Override | ||
protected boolean patch() { | ||
AtomicInteger count = new AtomicInteger(); | ||
for (MethodNode method : classNode.methods) { | ||
InsnList insnList = method.instructions; | ||
Iterable<AbstractInsnNode> insnsIter = insnList::iterator; | ||
Stream<AbstractInsnNode> insns = StreamSupport.stream(insnsIter.spliterator(), true); | ||
|
||
insns.filter(insn -> insn instanceof MethodInsnNode) | ||
.map(insn -> (MethodInsnNode) insn) | ||
.filter(insn -> insn.owner.equals("org/lwjgl/input/Mouse") && insn.name.equals("setGrabbed")) | ||
.forEach(insn -> { | ||
count.getAndIncrement(); | ||
insn.owner = "tv/darkosto/sevpatches/core/hooks/MacMouseHook"; | ||
}); | ||
} | ||
|
||
return count.get() > 0; | ||
} | ||
|
||
@Override | ||
protected byte[] writeClass() { | ||
ClassWriter classWriter = new ClassWriter(0); | ||
classNode.accept(classWriter); | ||
return classWriter.toByteArray(); | ||
} | ||
} |