@@ -104,54 +104,49 @@ public static FieldInsnNode getKeyboardInsn(int constant) {
104104 }
105105
106106 public static InsnList getGLInsn (MethodInsnNode invoke , int constant ) {
107- String pkg = invoke .owner .substring (17 ); //invoke.owner.replace("org/lwjgl/opengl/", "");
107+ String pkg = invoke .owner .substring (17 ); // invoke.owner.replace("org/lwjgl/opengl/", "");
108108 for (Pair <Map <String , List <String >>, Map <String , Map <Integer , String >>> group : CONSTANTS ) {
109- Map <String , List <String >> methodKeys = group .getLeft ();
110- List <String > methodList = methodKeys .get (pkg );
111- if (methodList != null && methodList .contains (invoke .name )) {
112- Map <String , Map <Integer , String >> methodValues = group .getRight ();
113- for (Entry <String , Map <Integer , String >> entry : methodValues .entrySet ()) {
114- String key = entry .getKey ();
115- Map <Integer , String > value = entry .getValue ();
116- String constantValue = value .get (constant );
117- if (constantValue == null ) continue ;
109+ List <String > methodList = group .getLeft ().get (pkg );
110+ if (methodList == null || !methodList .contains (invoke .name ))
111+ continue ;
118112
119- InsnList instructions = new InsnList ();
120- int i = -1 ;
121- do {
122- char operator = i == -1 ? 0 : constantValue .charAt (i );
123- int index1 = i + 1 ;
124- i = indexOf (OPERATORS , index1 , constantValue );
125- int index2 = i == -1 ? constantValue .length () : i ;
126- String name = constantValue .substring (index1 , index2 ).trim ();
127- instructions .add (new FieldInsnNode (Opcodes .GETSTATIC , "org/lwjgl/opengl/" + key , name , "I" ));
128- switch (operator ) {
129- case '|' :
130- instructions .add (new InsnNode (Opcodes .IOR ));
131- break ;
132- case '&' :
133- instructions .add (new InsnNode (Opcodes .IAND ));
134- break ;
135- case '^' :
136- instructions .add (new InsnNode (Opcodes .IXOR ));
137- break ;
138- }
139- } while (i != -1 );
140- return instructions ;
141- }
113+ for (Entry <String , Map <Integer , String >> entry : group .getRight ().entrySet ()) {
114+ final String constantValue = entry .getValue ().get (constant );
115+ if (constantValue == null )
116+ continue ;
117+
118+ final InsnList instructions = new InsnList ();
119+ int i = -1 ;
120+ do {
121+ char operator = i == -1 ? 0 : constantValue .charAt (i );
122+ int index1 = i + 1 ;
123+ i = indexOf (OPERATORS , index1 , constantValue );
124+ int index2 = i == -1 ? constantValue .length () : i ;
125+ String name = constantValue .substring (index1 , index2 ).trim ();
126+ instructions .add (new FieldInsnNode (Opcodes .GETSTATIC , "org/lwjgl/opengl/" + entry .getKey (), name , "I" ));
127+ switch (operator ) {
128+ case '|' :
129+ instructions .add (new InsnNode (Opcodes .IOR ));
130+ break ;
131+ case '&' :
132+ instructions .add (new InsnNode (Opcodes .IAND ));
133+ break ;
134+ case '^' :
135+ instructions .add (new InsnNode (Opcodes .IXOR ));
136+ break ;
137+ }
138+ } while (i != -1 );
139+ return instructions ;
142140 }
143141 }
144142 return null ;
145143 }
146144
147145 public static int indexOf (char [] ch , int fromIndex , String string ) {
148146 final int max = string .length ();
149- if (fromIndex < 0 ) {
150- fromIndex = 0 ;
151- } else if (fromIndex >= max ) {
152- return -1 ;
153- }
154-
147+ fromIndex = Math .max (0 , fromIndex ); // fromIndex will be clamped to [0, +∞)
148+ if (fromIndex >= max ) return -1 ;
149+
155150 final char [] value = string .toCharArray ();
156151 for (int i = fromIndex ; i < max ; i ++) {
157152 for (char c : ch ) {
@@ -216,80 +211,78 @@ private static List<String> getPackages(List<Pair<Map<String, List<String>>, Map
216211 @ Override
217212 protected void visitMethod (MethodNode node ) {
218213 if (!INIT ) return ;
219- InsnList instructions = node .instructions ;
220- List <MethodInsnNode > glCalls = new ArrayList <>();
221- List <Pair <AbstractInsnNode , FieldInsnNode >> keyboardConstants = new ArrayList <>();
222- for (AbstractInsnNode insn : instructions ) {
223- if (insn instanceof MethodInsnNode ) {
224- MethodInsnNode invoke = (MethodInsnNode ) insn ;
225- if (PACKAGES != null && PACKAGES .contains (invoke .owner )) {
226- glCalls .add (invoke );
227- }
228- if (invoke .owner .equals ("org/lwjgl/input/Keyboard" )) {
229- if (invoke .name .equals ("isKeyDown" ) || invoke .name .equals ("getKeyName" )) {
230- AbstractInsnNode iconst = invoke .getPrevious ();
231- if (iconst == null ) {
232- continue ;
233- }
234- Integer value = intValue (iconst );
235- if (value != null ) {
236- FieldInsnNode getField = getKeyboardInsn (value );
237- if (getField != null ) {
238- keyboardConstants .add (Pair .of (iconst , getField ));
239- }
240- }
241- } else if (invoke .name .equals ("getEventKey" )) {
242- AbstractInsnNode iconst = invoke .getNext ();
243- if (iconst == null ) {
244- continue ;
245- }
246- AbstractInsnNode insn2 = iconst .getNext ();
247- // INVOKE, ICONST, (ANY INSTRUCTION), IADD, ICMP
248- // or
249- // INVOKE, ICONST, ICMP
250- boolean hasCompare = false ;
251- int count = 0 ;
252- while (insn2 != null && count < 3 && !hasCompare ) {
253- if (count == 1 && insn2 .getOpcode () != Opcodes .IADD )
254- break ;
255- if (isICmp (insn2 .getOpcode ()))
256- hasCompare = true ;
257- count ++;
258- insn2 = insn2 .getNext ();
259- }
260- if (hasCompare ) {
261- Integer value = intValue (iconst );
262- if (value != null ) {
263- FieldInsnNode getField = getKeyboardInsn (value );
264- if (getField != null ) {
265- keyboardConstants .add (Pair .of (iconst , getField ));
266- }
267- }
268- }
269- }
214+
215+ final List <MethodInsnNode > glCalls = new ArrayList <>();
216+ final List <Pair <AbstractInsnNode , FieldInsnNode >> keyboardConstants = new ArrayList <>();
217+ for (AbstractInsnNode insn : node .instructions ) {
218+ if (!(insn instanceof MethodInsnNode )) continue ;
219+
220+ MethodInsnNode invoke = (MethodInsnNode ) insn ;
221+ if (PACKAGES != null && PACKAGES .contains (invoke .owner ))
222+ glCalls .add (invoke );
223+
224+ if (!invoke .owner .equals ("org/lwjgl/input/Keyboard" )) continue ;
225+
226+ if (invoke .name .equals ("isKeyDown" ) || invoke .name .equals ("getKeyName" )) {
227+ AbstractInsnNode iconst = invoke .getPrevious ();
228+ if (iconst == null ) {
229+ continue ;
270230 }
231+ Integer value = intValue (iconst );
232+ if (value == null ) continue ;
233+ FieldInsnNode getField = getKeyboardInsn (value );
234+ if (getField != null )
235+ keyboardConstants .add (Pair .of (iconst , getField ));
236+
237+ continue ;
238+ }
239+
240+ if (!invoke .name .equals ("getEventKey" )) continue ;
241+ AbstractInsnNode iconst = invoke .getNext ();
242+ if (iconst == null ) continue ;
243+ AbstractInsnNode insn2 = iconst .getNext ();
244+ // INVOKE, ICONST, (ANY INSTRUCTION), IADD, ICMP
245+ // or
246+ // INVOKE, ICONST, ICMP
247+ boolean hasCompare = false ;
248+ int count = 0 ;
249+ while (insn2 != null && count < 3 && !hasCompare ) {
250+ if (count == 1 && insn2 .getOpcode () != Opcodes .IADD )
251+ break ;
252+ if (isICmp (insn2 .getOpcode ()))
253+ hasCompare = true ;
254+ count ++;
255+ insn2 = insn2 .getNext ();
256+ }
257+
258+ if (!hasCompare ) continue ;
259+ Integer value = intValue (iconst );
260+ if (value == null ) continue ;
261+
262+ FieldInsnNode getField = getKeyboardInsn (value );
263+ if (getField != null ) {
264+ keyboardConstants .add (Pair .of (iconst , getField ));
271265 }
272266 }
273267
274268 for (Pair <AbstractInsnNode , FieldInsnNode > pair : keyboardConstants ) {
275- instructions .set (pair .getLeft (), pair .getRight ());
269+ node . instructions .set (pair .getLeft (), pair .getRight ());
276270 }
277271
278272 for (MethodInsnNode invoke : glCalls ) {
279273 IdentifyCall identifiedCall = new IdentifyCall (invoke );
280274 for (AbstractInsnNode [] insns : identifiedCall .getArguments ()) {
281275 for (AbstractInsnNode insn : insns ) {
282- if (insn == null ) {
283- continue ;
284- }
276+ if (insn == null ) continue ;
277+
285278 Integer intValue = intValue (insn );
286- if (intValue != null ) {
287- InsnList newinsns = getGLInsn ( invoke , intValue );
288- if ( newinsns != null ) {
289- instructions . insert ( insn , newinsns ) ;
290- instructions . remove ( insn );
291- }
292- }
279+ if (intValue == null ) continue ;
280+
281+ InsnList newinsns = getGLInsn ( invoke , intValue );
282+ if ( newinsns == null ) continue ;
283+
284+ node . instructions . insert ( insn , newinsns );
285+ node . instructions . remove ( insn );
293286 }
294287 }
295288 }
0 commit comments