|
| 1 | +/* |
| 2 | + * MIT License |
| 3 | + * |
| 4 | + * Copyright (c) 2026 Fox2Code |
| 5 | + * |
| 6 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | + * of this software and associated documentation files (the "Software"), to deal |
| 8 | + * in the Software without restriction, including without limitation the rights |
| 9 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | + * copies of the Software, and to permit persons to whom the Software is |
| 11 | + * furnished to do so, subject to the following conditions: |
| 12 | + * |
| 13 | + * The above copyright notice and this permission notice shall be included in all |
| 14 | + * copies or substantial portions of the Software. |
| 15 | + * |
| 16 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | + * SOFTWARE. |
| 23 | + */ |
| 24 | +package com.fox2code.hypertale.patcher.patches; |
| 25 | + |
| 26 | +import com.fox2code.hypertale.patcher.TransformerUtils; |
| 27 | +import org.objectweb.asm.tree.*; |
| 28 | + |
| 29 | +final class PatchMat4f extends HypertalePatch { |
| 30 | + PatchMat4f() { |
| 31 | + super(Mat4f); |
| 32 | + } |
| 33 | + |
| 34 | + @Override |
| 35 | + public ClassNode transform(ClassNode classNode) { |
| 36 | + MethodNode identity = TransformerUtils.findMethod(classNode, "identity", "()L" + Mat4f + ";"); |
| 37 | + if (identity == null || (identity.access & ACC_STATIC) == 0) return classNode; |
| 38 | + classNode.access |= TransformerUtils.ACC_COMPUTE_FRAMES; |
| 39 | + FieldNode fieldNode = new FieldNode(ACC_PRIVATE | ACC_STATIC | ACC_FINAL, |
| 40 | + "HYPERTALE_IDENTITY", "L" + Mat4f + ";", null, null); |
| 41 | + classNode.fields.addFirst(fieldNode); |
| 42 | + MethodNode init = TransformerUtils.findMethod(classNode, "<clinit>"); |
| 43 | + if (init == null) { |
| 44 | + init = new MethodNode(ACC_STATIC, "<clinit>", "()V", null, null); |
| 45 | + init.instructions.add(new InsnNode(RETURN)); |
| 46 | + classNode.methods.addFirst(init); |
| 47 | + } |
| 48 | + // Replace the identity method with the get field! |
| 49 | + MethodNode identityProxy = TransformerUtils.copyMethodNode(identity); |
| 50 | + identityProxy.instructions.clear(); |
| 51 | + identityProxy.instructions.add(new FieldInsnNode(GETSTATIC, classNode.name, |
| 52 | + "HYPERTALE_IDENTITY", "L" + Mat4f + ";")); |
| 53 | + identityProxy.instructions.add(new InsnNode(ARETURN)); |
| 54 | + classNode.methods.add(identityProxy); |
| 55 | + identity.name = "identityOriginal"; |
| 56 | + identity.access = ACC_PRIVATE | ACC_STATIC; |
| 57 | + |
| 58 | + InsnList injection = new InsnList(); |
| 59 | + injection.add(new MethodInsnNode(INVOKESTATIC, Mat4f, identity.name, "()L" + Mat4f + ";", false)); |
| 60 | + injection.add(new FieldInsnNode(PUTSTATIC, classNode.name, "HYPERTALE_IDENTITY", "L" + Mat4f + ";")); |
| 61 | + injection.add(new InsnNode(RETURN)); |
| 62 | + |
| 63 | + TransformerUtils.insertToBeginningOfCode(init, injection); |
| 64 | + |
| 65 | + return classNode; |
| 66 | + } |
| 67 | +} |
0 commit comments