|
6 | 6 |
|
7 | 7 | import cpw.mods.modlauncher.Launcher;
|
8 | 8 | import cpw.mods.modlauncher.api.INameMappingService;
|
| 9 | +import net.minecraftforge.coremod.CoreModEngine; |
9 | 10 | import net.minecraftforge.coremod.CoreModTracker;
|
10 | 11 | import org.jetbrains.annotations.Nullable;
|
11 | 12 | import org.objectweb.asm.Opcodes;
|
@@ -305,22 +306,60 @@ public static AbstractInsnNode findFirstInstructionAfter(MethodNode method, int
|
305 | 306 | * @param opCode the opcode to search for
|
306 | 307 | * @param startIndex the index at which to start searching (inclusive)
|
307 | 308 | * @return the found instruction node or null if none matched before the given startIndex
|
| 309 | + * |
| 310 | + * @apiNote In Minecraft 1.21.1 and earlier, this method contains broken logic that ignores the {@code startIndex} |
| 311 | + * parameter and searches for the requested instruction at the end of the method. This behavior is preserved to |
| 312 | + * not disrupt older coremods. If you are on one of these older versions and need to use the fixed logic, please |
| 313 | + * use {@link #findFirstInstructionBefore(MethodNode, int, int, boolean)}. |
308 | 314 | */
|
309 | 315 | public static AbstractInsnNode findFirstInstructionBefore(MethodNode method, int opCode, int startIndex) {
|
310 | 316 | return findFirstInstructionBefore(method, opCode, null, startIndex);
|
311 | 317 | }
|
312 | 318 |
|
| 319 | + /** |
| 320 | + * Finds the first instruction with matching opcode before the given index in reverse search. |
| 321 | + * |
| 322 | + * @param method the method to search in |
| 323 | + * @param opCode the opcode to search for |
| 324 | + * @param startIndex the index at which to start searching (inclusive) |
| 325 | + * @param fixLogic whether to use the fixed logic for finding instructions before the given startIndex (true by |
| 326 | + * default on versions since 1.21.3, false otherwise) |
| 327 | + * @return the found instruction node or null if none matched before the given startIndex |
| 328 | + */ |
| 329 | + public static AbstractInsnNode findFirstInstructionBefore(MethodNode method, int opCode, int startIndex, boolean fixLogic) { |
| 330 | + return findFirstInstructionBefore(method, opCode, null, startIndex, fixLogic); |
| 331 | + } |
| 332 | + |
313 | 333 | /**
|
314 | 334 | * Finds the first instruction with matching opcode before the given index in reverse search
|
315 | 335 | *
|
316 |
| - * @param method the method to search in |
317 |
| - * @param opCode the opcode to search for |
| 336 | + * @param method the method to search in |
| 337 | + * @param opCode the opcode to search for |
318 | 338 | * @param startIndex the index at which to start searching (inclusive)
|
319 | 339 | * @return the found instruction node or null if none matched before the given startIndex
|
| 340 | + * |
| 341 | + * @apiNote In Minecraft 1.21.1 and earlier, this method contains broken logic that ignores the {@code startIndex} |
| 342 | + * parameter and searches for the requested instruction at the end of the method. This behavior is preserved to |
| 343 | + * not disrupt older coremods. If you are on one of these older versions and need to use the fixed logic, please |
| 344 | + * use {@link #findFirstInstructionBefore(MethodNode, int, InsnType, int, boolean)}. |
320 | 345 | */
|
321 | 346 | public static AbstractInsnNode findFirstInstructionBefore(MethodNode method, int opCode, @Nullable InsnType type, int startIndex) {
|
| 347 | + return findFirstInstructionBefore(method, opCode, type, startIndex, !CoreModEngine.DO_NOT_FIX_INSNBEFORE); |
| 348 | + } |
| 349 | + |
| 350 | + /** |
| 351 | + * Finds the first instruction with matching opcode before the given index in reverse search |
| 352 | + * |
| 353 | + * @param method the method to search in |
| 354 | + * @param opCode the opcode to search for |
| 355 | + * @param startIndex the index at which to start searching (inclusive) |
| 356 | + * @param fixLogic whether to use the fixed logic for finding instructions before the given startIndex (true by |
| 357 | + * default on versions since 1.21.3, false otherwise) |
| 358 | + * @return the found instruction node or null if none matched before the given startIndex |
| 359 | + */ |
| 360 | + public static AbstractInsnNode findFirstInstructionBefore(MethodNode method, int opCode, @Nullable InsnType type, int startIndex, boolean fixLogic) { |
322 | 361 | boolean checkType = type != null;
|
323 |
| - for (int i = Math.min(method.instructions.size() - 1, startIndex); i >= 0; i--) { |
| 362 | + for (int i = fixLogic ? Math.min(method.instructions.size() - 1, startIndex) : startIndex; i >= 0; i--) { |
324 | 363 | AbstractInsnNode ain = method.instructions.get(i);
|
325 | 364 | if (ain.getOpcode() == opCode) {
|
326 | 365 | if (!checkType || type.get() == ain.getType()) {
|
|
0 commit comments