1+ using System ;
2+ using System . Linq ;
3+ using System . Reflection ;
4+ using Il2CppInterop . Runtime . InteropTypes . Arrays ;
5+ using UnityEngine ;
6+
7+ namespace AuthFix ;
8+
9+ public sealed class AndroidJavaObjectSafe : IDisposable
10+ {
11+ public AndroidJavaObject Inner { get ; }
12+
13+ private static readonly MethodInfo s_Call ;
14+ private static readonly MethodInfo s_CallReturn ;
15+ private static readonly MethodInfo s_CallStatic ;
16+ private static readonly MethodInfo s_CallStaticReturn ;
17+
18+ private static readonly Type [ ] s_paramTypes =
19+ [ typeof ( string ) , typeof ( Il2CppReferenceArray < Il2CppSystem . Object > ) ] ;
20+
21+ static AndroidJavaObjectSafe ( )
22+ {
23+ var all = typeof ( AndroidJavaObject ) . GetMethods (
24+ BindingFlags . Public | BindingFlags . Instance ) ;
25+
26+ s_Call = FindVoid ( all , "Call" ) ;
27+ s_CallReturn = FindNonVoid ( all , "Call" ) ;
28+ s_CallStatic = FindVoid ( all , "CallStatic" ) ;
29+ s_CallStaticReturn = FindNonVoid ( all , "CallStatic" ) ;
30+ }
31+
32+ private static MethodInfo FindVoid ( MethodInfo [ ] methods , string name ) =>
33+ methods . FirstOrDefault ( m =>
34+ m . Name == name &&
35+ ! m . IsGenericMethod &&
36+ m . ReturnType == typeof ( void ) &&
37+ m . GetParameters ( ) . Select ( p => p . ParameterType ) . SequenceEqual ( s_paramTypes ) ) ;
38+
39+ private static MethodInfo FindNonVoid ( MethodInfo [ ] methods , string name ) =>
40+ methods . FirstOrDefault ( m =>
41+ m . Name == name &&
42+ ! m . IsGenericMethod &&
43+ m . ReturnType != typeof ( void ) &&
44+ m . GetParameters ( ) . Select ( p => p . ParameterType ) . SequenceEqual ( s_paramTypes ) ) ;
45+
46+ // ── Constructors ───────────────────────────────────────────────────────────
47+
48+ public AndroidJavaObjectSafe ( string className ,
49+ Il2CppReferenceArray < Il2CppSystem . Object > args = null )
50+ {
51+ Inner = new AndroidJavaObject ( className , args ?? new Il2CppReferenceArray < Il2CppSystem . Object > ( 0L ) ) ;
52+ }
53+
54+ public AndroidJavaObjectSafe ( AndroidJavaObject existing )
55+ {
56+ Inner = existing ?? throw new ArgumentNullException ( nameof ( existing ) ) ;
57+ }
58+
59+ public void Call ( string method , Il2CppReferenceArray < Il2CppSystem . Object > args = null )
60+ {
61+ args ??= new Il2CppReferenceArray < Il2CppSystem . Object > ( 0L ) ;
62+ s_Call . Invoke ( Inner , [ method, args ] ) ;
63+ }
64+
65+ public Il2CppSystem . Object CallReturn ( string method ,
66+ Il2CppReferenceArray < Il2CppSystem . Object > args = null )
67+ {
68+ args ??= new Il2CppReferenceArray < Il2CppSystem . Object > ( 0L ) ;
69+ return s_CallReturn . Invoke ( Inner , [ method, args ] ) as Il2CppSystem . Object ;
70+ }
71+
72+ public void CallStatic ( string method ,
73+ Il2CppReferenceArray < Il2CppSystem . Object > args = null )
74+ {
75+ args ??= new Il2CppReferenceArray < Il2CppSystem . Object > ( 0L ) ;
76+ s_CallStatic . Invoke ( Inner , [ method, args ] ) ;
77+ }
78+
79+ public Il2CppSystem . Object CallStaticReturn ( string method ,
80+ Il2CppReferenceArray < Il2CppSystem . Object > args = null )
81+ {
82+ args ??= new Il2CppReferenceArray < Il2CppSystem . Object > ( 0L ) ;
83+ return s_CallStaticReturn . Invoke ( Inner , [ method, args ] ) as Il2CppSystem . Object ;
84+ }
85+
86+ // ── Pass-through
87+
88+ public IntPtr GetRawObject ( ) => Inner . GetRawObject ( ) ;
89+ public IntPtr GetRawClass ( ) => Inner . GetRawClass ( ) ;
90+ public void Dispose ( ) => Inner . Dispose ( ) ;
91+
92+ // ── Convenience
93+
94+ public static Il2CppReferenceArray < Il2CppSystem . Object > Args (
95+ params Il2CppSystem . Object [ ] args ) => new ( args ) ;
96+ }
0 commit comments