@@ -66,54 +66,49 @@ public static FieldInsnNode getKeyboardInsn(int constant) {
6666 }
6767
6868 public static InsnList getGLInsn (MethodInsnNode invoke , int constant ) {
69- String pkg = invoke .owner .substring (17 ); //invoke.owner.replace("org/lwjgl/opengl/", "");
69+ String pkg = invoke .owner .substring (17 ); // invoke.owner.replace("org/lwjgl/opengl/", "");
7070 for (Pair <Map <String , List <String >>, Map <String , Map <Integer , String >>> group : CONSTANTS ) {
71- Map <String , List <String >> methodKeys = group .getLeft ();
72- List <String > methodList = methodKeys .get (pkg );
73- if (methodList != null && methodList .contains (invoke .name )) {
74- Map <String , Map <Integer , String >> methodValues = group .getRight ();
75- for (Entry <String , Map <Integer , String >> entry : methodValues .entrySet ()) {
76- String key = entry .getKey ();
77- Map <Integer , String > value = entry .getValue ();
78- String constantValue = value .get (constant );
79- if (constantValue == null ) continue ;
80-
81- InsnList instructions = new InsnList ();
82- int i = -1 ;
83- do {
84- char operator = i == -1 ? 0 : constantValue .charAt (i );
85- int index1 = i + 1 ;
86- i = indexOf (OPERATORS , index1 , constantValue );
87- int index2 = i == -1 ? constantValue .length () : i ;
88- String name = constantValue .substring (index1 , index2 ).trim ();
89- instructions .add (new FieldInsnNode (Opcodes .GETSTATIC , "org/lwjgl/opengl/" + key , name , "I" ));
90- switch (operator ) {
91- case '|' :
92- instructions .add (new InsnNode (Opcodes .IOR ));
93- break ;
94- case '&' :
95- instructions .add (new InsnNode (Opcodes .IAND ));
96- break ;
97- case '^' :
98- instructions .add (new InsnNode (Opcodes .IXOR ));
99- break ;
100- }
101- } while (i != -1 );
102- return instructions ;
103- }
71+ List <String > methodList = group .getLeft ().get (pkg );
72+ if (methodList == null || !methodList .contains (invoke .name ))
73+ continue ;
74+
75+ for (Entry <String , Map <Integer , String >> entry : group .getRight ().entrySet ()) {
76+ final String constantValue = entry .getValue ().get (constant );
77+ if (constantValue == null )
78+ continue ;
79+
80+ final InsnList instructions = new InsnList ();
81+ int i = -1 ;
82+ do {
83+ char operator = i == -1 ? 0 : constantValue .charAt (i );
84+ int index1 = i + 1 ;
85+ i = indexOf (OPERATORS , index1 , constantValue );
86+ int index2 = i == -1 ? constantValue .length () : i ;
87+ String name = constantValue .substring (index1 , index2 ).trim ();
88+ instructions .add (new FieldInsnNode (Opcodes .GETSTATIC , "org/lwjgl/opengl/" + entry .getKey (), name , "I" ));
89+ switch (operator ) {
90+ case '|' :
91+ instructions .add (new InsnNode (Opcodes .IOR ));
92+ break ;
93+ case '&' :
94+ instructions .add (new InsnNode (Opcodes .IAND ));
95+ break ;
96+ case '^' :
97+ instructions .add (new InsnNode (Opcodes .IXOR ));
98+ break ;
99+ }
100+ } while (i != -1 );
101+ return instructions ;
104102 }
105103 }
106104 return null ;
107105 }
108106
109107 public static int indexOf (char [] ch , int fromIndex , String string ) {
110108 final int max = string .length ();
111- if (fromIndex < 0 ) {
112- fromIndex = 0 ;
113- } else if (fromIndex >= max ) {
114- return -1 ;
115- }
116-
109+ fromIndex = Math .max (0 , fromIndex ); // fromIndex will be clamped to [0, +∞)
110+ if (fromIndex >= max ) return -1 ;
111+
117112 final char [] value = string .toCharArray ();
118113 for (int i = fromIndex ; i < max ; i ++) {
119114 for (char c : ch ) {
@@ -178,80 +173,78 @@ private static List<String> getPackages(List<Pair<Map<String, List<String>>, Map
178173 @ Override
179174 protected void visitMethod (MethodNode node ) {
180175 if (!INIT ) return ;
181- InsnList instructions = node .instructions ;
182- List <MethodInsnNode > glCalls = new ArrayList <>();
183- List <Pair <AbstractInsnNode , FieldInsnNode >> keyboardConstants = new ArrayList <>();
184- for (AbstractInsnNode insn : instructions ) {
185- if (insn instanceof MethodInsnNode ) {
186- MethodInsnNode invoke = (MethodInsnNode ) insn ;
187- if (PACKAGES != null && PACKAGES .contains (invoke .owner )) {
188- glCalls .add (invoke );
189- }
190- if (invoke .owner .equals ("org/lwjgl/input/Keyboard" )) {
191- if (invoke .name .equals ("isKeyDown" ) || invoke .name .equals ("getKeyName" )) {
192- AbstractInsnNode iconst = invoke .getPrevious ();
193- if (iconst == null ) {
194- continue ;
195- }
196- Integer value = intValue (iconst );
197- if (value != null ) {
198- FieldInsnNode getField = getKeyboardInsn (value );
199- if (getField != null ) {
200- keyboardConstants .add (Pair .of (iconst , getField ));
201- }
202- }
203- } else if (invoke .name .equals ("getEventKey" )) {
204- AbstractInsnNode iconst = invoke .getNext ();
205- if (iconst == null ) {
206- continue ;
207- }
208- AbstractInsnNode insn2 = iconst .getNext ();
209- // INVOKE, ICONST, (ANY INSTRUCTION), IADD, ICMP
210- // or
211- // INVOKE, ICONST, ICMP
212- boolean hasCompare = false ;
213- int count = 0 ;
214- while (insn2 != null && count < 3 && !hasCompare ) {
215- if (count == 1 && insn2 .getOpcode () != Opcodes .IADD )
216- break ;
217- if (isICmp (insn2 .getOpcode ()))
218- hasCompare = true ;
219- count ++;
220- insn2 = insn2 .getNext ();
221- }
222- if (hasCompare ) {
223- Integer value = intValue (iconst );
224- if (value != null ) {
225- FieldInsnNode getField = getKeyboardInsn (value );
226- if (getField != null ) {
227- keyboardConstants .add (Pair .of (iconst , getField ));
228- }
229- }
230- }
231- }
176+
177+ final List <MethodInsnNode > glCalls = new ArrayList <>();
178+ final List <Pair <AbstractInsnNode , FieldInsnNode >> keyboardConstants = new ArrayList <>();
179+ for (AbstractInsnNode insn : node .instructions ) {
180+ if (!(insn instanceof MethodInsnNode )) continue ;
181+
182+ MethodInsnNode invoke = (MethodInsnNode ) insn ;
183+ if (PACKAGES != null && PACKAGES .contains (invoke .owner ))
184+ glCalls .add (invoke );
185+
186+ if (!invoke .owner .equals ("org/lwjgl/input/Keyboard" )) continue ;
187+
188+ if (invoke .name .equals ("isKeyDown" ) || invoke .name .equals ("getKeyName" )) {
189+ AbstractInsnNode iconst = invoke .getPrevious ();
190+ if (iconst == null ) {
191+ continue ;
232192 }
193+ Integer value = intValue (iconst );
194+ if (value == null ) continue ;
195+ FieldInsnNode getField = getKeyboardInsn (value );
196+ if (getField != null )
197+ keyboardConstants .add (Pair .of (iconst , getField ));
198+
199+ continue ;
200+ }
201+
202+ if (!invoke .name .equals ("getEventKey" )) continue ;
203+ AbstractInsnNode iconst = invoke .getNext ();
204+ if (iconst == null ) continue ;
205+ AbstractInsnNode insn2 = iconst .getNext ();
206+ // INVOKE, ICONST, (ANY INSTRUCTION), IADD, ICMP
207+ // or
208+ // INVOKE, ICONST, ICMP
209+ boolean hasCompare = false ;
210+ int count = 0 ;
211+ while (insn2 != null && count < 3 && !hasCompare ) {
212+ if (count == 1 && insn2 .getOpcode () != Opcodes .IADD )
213+ break ;
214+ if (isICmp (insn2 .getOpcode ()))
215+ hasCompare = true ;
216+ count ++;
217+ insn2 = insn2 .getNext ();
218+ }
219+
220+ if (!hasCompare ) continue ;
221+ Integer value = intValue (iconst );
222+ if (value == null ) continue ;
223+
224+ FieldInsnNode getField = getKeyboardInsn (value );
225+ if (getField != null ) {
226+ keyboardConstants .add (Pair .of (iconst , getField ));
233227 }
234228 }
235229
236230 for (Pair <AbstractInsnNode , FieldInsnNode > pair : keyboardConstants ) {
237- instructions .set (pair .getLeft (), pair .getRight ());
231+ node . instructions .set (pair .getLeft (), pair .getRight ());
238232 }
239233
240234 for (MethodInsnNode invoke : glCalls ) {
241235 IdentifyCall identifiedCall = new IdentifyCall (invoke );
242236 for (AbstractInsnNode [] insns : identifiedCall .getArguments ()) {
243237 for (AbstractInsnNode insn : insns ) {
244- if (insn == null ) {
245- continue ;
246- }
238+ if (insn == null ) continue ;
239+
247240 Integer intValue = intValue (insn );
248- if (intValue != null ) {
249- InsnList newinsns = getGLInsn ( invoke , intValue );
250- if ( newinsns != null ) {
251- instructions . insert ( insn , newinsns ) ;
252- instructions . remove ( insn );
253- }
254- }
241+ if (intValue == null ) continue ;
242+
243+ InsnList newinsns = getGLInsn ( invoke , intValue );
244+ if ( newinsns == null ) continue ;
245+
246+ node . instructions . insert ( insn , newinsns );
247+ node . instructions . remove ( insn );
255248 }
256249 }
257250 }
0 commit comments