66
77import cpw .mods .modlauncher .Launcher ;
88import cpw .mods .modlauncher .api .INameMappingService ;
9- import net . minecraftforge . coremod . CoreModEngine ;
9+ import cpw . mods . modlauncher . api . TypesafeMap ;
1010import net .minecraftforge .coremod .CoreModTracker ;
11+
1112import org .jetbrains .annotations .Nullable ;
1213import org .objectweb .asm .Opcodes ;
1314import org .objectweb .asm .tree .*;
1415import org .objectweb .asm .util .Textifier ;
1516import org .objectweb .asm .util .TraceClassVisitor ;
1617import org .objectweb .asm .util .TraceMethodVisitor ;
1718
18- import javax .script .ScriptException ;
1919import java .io .IOException ;
2020import java .io .PrintWriter ;
2121import java .io .StringWriter ;
3131 * to prevent boilerplate code, excessive imports, unnecessary loops, and to provide a more user-friendly API for
3232 * coremod developers.
3333 */
34- @ SuppressWarnings ({"unused" , " exports" }) // annoying IDE warnings
34+ @ SuppressWarnings ({"exports" }) // annoying IDE warnings
3535public class ASMAPI {
3636 /* BUILDING INSTRUCTION LISTS */
3737
@@ -473,7 +473,7 @@ public int get() {
473473 * {@link #findFirstInstructionBefore(MethodNode, int, InsnType, int, boolean)}.
474474 */
475475 public static @ Nullable AbstractInsnNode findFirstInstructionBefore (MethodNode method , int opCode , @ Nullable InsnType type , int startIndex ) {
476- return findFirstInstructionBefore (method , opCode , type , startIndex , !CoreModEngine . DO_NOT_FIX_INSNBEFORE );
476+ return findFirstInstructionBefore (method , opCode , type , startIndex , !DO_NOT_FIX_INSNBEFORE );
477477 }
478478
479479 /**
@@ -1171,8 +1171,11 @@ public static boolean getSystemPropertyFlag(final String propertyName) {
11711171 *
11721172 * @throws ScriptException If the script engine encounters an error, usually due to a syntax error in the script
11731173 * @throws IOException If an I/O error occurs while reading the file, usually due to a corrupt or missing file
1174+ *
1175+ * @apiNote This method only functions for JavaScript coremods managed by the main CoreMod engine.
1176+ * If using ASMAPI in a normal transformer do not use this method. Unknown exceptions could be thrown.
11741177 */
1175- public static boolean loadFile (String file ) throws ScriptException , IOException {
1178+ public static boolean loadFile (String file ) throws IOException {
11761179 return CoreModTracker .loadFileByName (file );
11771180 }
11781181
@@ -1184,10 +1187,13 @@ public static boolean loadFile(String file) throws ScriptException, IOException
11841187 * {@code initializeCoreMod()} or any of the transformer functions returned by it.
11851188 *
11861189 * @throws ScriptException If the parsed JSON data is malformed
1187- * @throws IOException If an I/O error occurs while reading the file, usually due to a corrupt or missing file
1190+ * @throws IOException If an I/O error occurs while reading the file, usually due to a corrupt or missing file.
1191+ *
1192+ * @apiNote This method only functions for JavaScript coremods managed by the main CoreMod engine.
1193+ * If using ASMAPI in a normal transformer do not use this method. Unknown exceptions could be thrown.
11881194 */
11891195 @ Nullable
1190- public static Object loadData (String file ) throws ScriptException , IOException {
1196+ public static Object loadData (String file ) throws IOException {
11911197 return CoreModTracker .loadDataByName (file );
11921198 }
11931199
@@ -1202,6 +1208,9 @@ public static Object loadData(String file) throws ScriptException, IOException {
12021208 * @param message The message
12031209 * @param args Any formatting arguments
12041210 * @see CoreModTracker#log(String, String, Object[])
1211+ *
1212+ * @apiNote This method only functions for JavaScript coremods managed by the main CoreMod engine.
1213+ * If using ASMAPI in a normal transformer do not use this method. Unknown exceptions could be thrown.
12051214 */
12061215 public static void log (String level , String message , Object ... args ) {
12071216 CoreModTracker .log (level , message , args );
@@ -1293,4 +1302,29 @@ private static String toString(Textifier text) {
12931302 private static int clamp (int value , int min , int max ) {
12941303 return Math .max (min , Math .min (max , value ));
12951304 }
1305+
1306+ // INTERNAL
1307+ /**
1308+ * Whether to preserve the legacy behavior of
1309+ * {@link net.minecraftforge.coremod.api.ASMAPI#findFirstInstructionBefore(org.objectweb.asm.tree.MethodNode, int,
1310+ * int)} for backwards-compatibility.
1311+ * <p>
1312+ * In Forge's case, this is set by FML in Minecraft 1.21.1 and earlier, but not in 1.21.3 and later.
1313+ *
1314+ * @see net.minecraftforge.coremod.api.ASMAPI#findFirstInstructionBefore(org.objectweb.asm.tree.MethodNode, int,
1315+ * int)
1316+ */
1317+ private static final boolean DO_NOT_FIX_INSNBEFORE = shouldntFixInsnBefore ();
1318+
1319+ private static final boolean shouldntFixInsnBefore () {
1320+ try {
1321+ if (Launcher .INSTANCE == null )
1322+ return false ;
1323+
1324+ var blackboardVar = Launcher .INSTANCE .blackboard ().get (TypesafeMap .Key .getOrCreate (Launcher .INSTANCE .blackboard (), "coremods.use_old_findFirstInstructionBefore" , Boolean .class ));
1325+ return blackboardVar .isPresent () && blackboardVar .get ();
1326+ } catch (Throwable t ) { // If ModLauncher doesn't exist.
1327+ return false ;
1328+ }
1329+ }
12961330}
0 commit comments