1+ using System ;
2+ using System . Collections . Generic ;
3+ using System . Linq ;
4+ using AsmResolver . DotNet ;
5+ using BitMono . Core . Contexts ;
6+
7+ namespace BitMono . Unity ;
8+
9+ public class UnityIntegrationModule
10+ {
11+ private readonly ProtectionContext _context ;
12+ private readonly UnityMethodDetector _methodDetector ;
13+
14+ public UnityIntegrationModule ( ProtectionContext context )
15+ {
16+ _context = context ?? throw new ArgumentNullException ( nameof ( context ) ) ;
17+ _methodDetector = new UnityMethodDetector ( ) ;
18+ }
19+
20+ public bool IsUnityAssembly ( ModuleDefinition module )
21+ {
22+ var hasUnityEngine = module . AssemblyReferences . Any ( a =>
23+ a . Name ? . ToString ( ) ? . IndexOf ( "UnityEngine" , StringComparison . OrdinalIgnoreCase ) >= 0 ) ;
24+
25+ var isAssemblyCSharp = module . Assembly ? . Name ? . ToString ( ) ? . IndexOf ( "Assembly-CSharp" , StringComparison . OrdinalIgnoreCase ) >= 0 ;
26+
27+ var hasMonoBehaviour = module . GetAllTypes ( ) . Any ( t =>
28+ t . BaseType ? . FullName ? . Contains ( "UnityEngine.MonoBehaviour" ) == true ) ;
29+
30+ return hasUnityEngine || isAssemblyCSharp || hasMonoBehaviour ;
31+ }
32+
33+ public void PrepareForIL2CPP ( ModuleDefinition module )
34+ {
35+ foreach ( var type in module . GetAllTypes ( ) )
36+ {
37+ if ( IsUnityType ( type ) )
38+ {
39+ PreserveUnityMetadata ( type ) ;
40+ }
41+ }
42+ }
43+
44+ private bool IsUnityType ( TypeDefinition type )
45+ {
46+ var unityBaseTypes = new [ ]
47+ {
48+ "UnityEngine.MonoBehaviour" ,
49+ "UnityEngine.ScriptableObject" ,
50+ "UnityEngine.Component" ,
51+ "UnityEditor.Editor" ,
52+ "UnityEditor.EditorWindow"
53+ } ;
54+
55+ var currentType = type ;
56+ while ( currentType != null )
57+ {
58+ if ( unityBaseTypes . Contains ( currentType . BaseType ? . FullName ) )
59+ return true ;
60+
61+ currentType = currentType . BaseType ? . Resolve ( ) ;
62+ }
63+
64+ return false ;
65+ }
66+
67+ private void PreserveUnityMetadata ( TypeDefinition type )
68+ {
69+ type . IsPublic = true ;
70+ }
71+
72+ public bool ShouldExcludeFromRenaming ( MethodDefinition method )
73+ {
74+ return _methodDetector . IsUnityCallback ( method ) ||
75+ _methodDetector . IsSerializationCallback ( method ) ||
76+ _methodDetector . IsCoroutine ( method ) ;
77+ }
78+ }
79+
80+ public class UnityMethodDetector
81+ {
82+ private readonly HashSet < string > _unityCallbacks = new ( )
83+ {
84+ "Awake" , "Start" , "Update" , "FixedUpdate" , "LateUpdate" ,
85+ "OnEnable" , "OnDisable" , "OnDestroy" , "OnApplicationPause" ,
86+ "OnApplicationFocus" , "OnApplicationQuit" ,
87+ "OnCollisionEnter" , "OnCollisionStay" , "OnCollisionExit" ,
88+ "OnCollisionEnter2D" , "OnCollisionStay2D" , "OnCollisionExit2D" ,
89+ "OnTriggerEnter" , "OnTriggerStay" , "OnTriggerExit" ,
90+ "OnTriggerEnter2D" , "OnTriggerStay2D" , "OnTriggerExit2D" ,
91+ "OnBecameVisible" , "OnBecameInvisible" ,
92+ "OnPreCull" , "OnPreRender" , "OnPostRender" ,
93+ "OnRenderObject" , "OnWillRenderObject" ,
94+ "OnGUI" , "OnMouseDown" , "OnMouseUp" , "OnMouseOver" ,
95+ "OnMouseExit" , "OnMouseEnter" , "OnMouseDrag" ,
96+ "OnAnimatorMove" , "OnAnimatorIK" ,
97+ "OnStartServer" , "OnStopServer" , "OnStartClient" , "OnStopClient" ,
98+ "OnServerConnect" , "OnServerDisconnect" , "OnClientConnect" , "OnClientDisconnect"
99+ } ;
100+
101+ private readonly HashSet < string > _serializationCallbacks = new ( )
102+ {
103+ "OnBeforeSerialize" , "OnAfterDeserialize" ,
104+ "OnValidate" , "Reset"
105+ } ;
106+
107+ public bool IsUnityCallback ( MethodDefinition method )
108+ {
109+ var methodName = method . Name ? . ToString ( ) ;
110+ return ! string . IsNullOrEmpty ( methodName ) && _unityCallbacks . Contains ( methodName ) ;
111+ }
112+
113+ public bool IsSerializationCallback ( MethodDefinition method )
114+ {
115+ var methodName = method . Name ? . ToString ( ) ;
116+ return ! string . IsNullOrEmpty ( methodName ) && _serializationCallbacks . Contains ( methodName ) ;
117+ }
118+
119+ public bool IsCoroutine ( MethodDefinition method )
120+ {
121+ return method . Signature ? . ReturnType ? . FullName ? . IndexOf ( "System.Collections.IEnumerator" ) >= 0 ;
122+ }
123+ }
0 commit comments