@@ -22,6 +22,8 @@ public class SearchPage : WindowPage
2222 private string m_typeInput = "" ;
2323 private int m_limit = 100 ;
2424 private int m_pageOffset = 0 ;
25+ private List < object > m_searchResults = new List < object > ( ) ;
26+ private Vector2 resultsScroll = Vector2 . zero ;
2527
2628 public SceneFilter SceneMode = SceneFilter . Any ;
2729 public TypeFilter TypeMode = TypeFilter . Object ;
@@ -42,9 +44,6 @@ public enum TypeFilter
4244 Custom
4345 }
4446
45- private List < object > m_searchResults = new List < object > ( ) ;
46- private Vector2 resultsScroll = Vector2 . zero ;
47-
4847 public override void Init ( )
4948 {
5049 Instance = this ;
@@ -114,7 +113,7 @@ public override void DrawWindow()
114113 int offset = m_pageOffset * CppExplorer . ArrayLimit ;
115114 int preiterated = 0 ;
116115
117- if ( offset >= count ) offset = 0 ;
116+ if ( offset >= count ) m_pageOffset = 0 ;
118117
119118 for ( int i = 0 ; i < m_searchResults . Count ; i ++ )
120119 {
@@ -246,51 +245,13 @@ private void SceneFilterToggle(SceneFilter mode, string label, float width)
246245 }
247246
248247
249- // -------------- ACTUAL METHODS (not Gui draw) ----------------- //
250-
251- // credit: ManlyMarco (RuntimeUnityEditor)
252- public static IEnumerable < object > GetInstanceClassScanner ( )
253- {
254- var query = AppDomain . CurrentDomain . GetAssemblies ( )
255- . Where ( x => ! x . FullName . StartsWith ( "Mono" ) )
256- . SelectMany ( GetTypesSafe )
257- . Where ( t => t . IsClass && ! t . IsAbstract && ! t . ContainsGenericParameters ) ;
258-
259- foreach ( var type in query )
260- {
261- object obj = null ;
262- try
263- {
264- obj = type . GetProperty ( "Instance" , BindingFlags . Public | BindingFlags . Static | BindingFlags . FlattenHierarchy ) ? . GetValue ( null , null ) ;
265- }
266- catch
267- {
268- try
269- {
270- obj = type . GetField ( "Instance" , BindingFlags . Public | BindingFlags . Static | BindingFlags . FlattenHierarchy ) ? . GetValue ( null ) ;
271- }
272- catch
273- {
274- }
275- }
276- if ( obj != null && ! obj . ToString ( ) . StartsWith ( "Mono" ) )
277- {
278- yield return obj ;
279- }
280- }
281- }
282-
283- public static IEnumerable < Type > GetTypesSafe ( Assembly asm )
284- {
285- try { return asm . GetTypes ( ) ; }
286- catch ( ReflectionTypeLoadException e ) { return e . Types . Where ( x => x != null ) ; }
287- catch { return Enumerable . Empty < Type > ( ) ; }
288- }
248+ // -------------- ACTUAL METHODS (not Gui draw) ----------------- //
289249
290250 // ======= search functions =======
291251
292252 private void Search ( )
293253 {
254+ m_pageOffset = 0 ;
294255 m_searchResults = FindAllObjectsOfType ( m_searchInput , m_typeInput ) ;
295256 }
296257
@@ -312,73 +273,37 @@ private List<object> FindAllObjectsOfType(string _search, string _type)
312273 }
313274 else if ( TypeMode == TypeFilter . Object )
314275 {
315- type = Il2CppType . Of < Object > ( ) ;
276+ type = CppExplorer . ObjectType ;
316277 }
317278 else if ( TypeMode == TypeFilter . GameObject )
318279 {
319- type = Il2CppType . Of < GameObject > ( ) ;
280+ type = CppExplorer . GameObjectType ;
320281 }
321282 else if ( TypeMode == TypeFilter . Component )
322283 {
323- type = Il2CppType . Of < Component > ( ) ;
284+ type = CppExplorer . ComponentType ;
324285 }
325286
326- if ( ! Il2CppType . Of < Object > ( ) . IsAssignableFrom ( type ) )
287+ if ( ! CppExplorer . ObjectType . IsAssignableFrom ( type ) )
327288 {
328289 MelonLogger . LogError ( "Your Class Type must inherit from UnityEngine.Object! Leave blank to default to UnityEngine.Object" ) ;
329290 return new List < object > ( ) ;
330291 }
331292
332293 var matches = new List < object > ( ) ;
333294
334- foreach ( var obj in Resources . FindObjectsOfTypeAll ( type ) )
295+ var allObjectsOfType = Resources . FindObjectsOfTypeAll ( type ) ;
296+
297+ foreach ( var obj in allObjectsOfType )
335298 {
336299 if ( _search != "" && ! obj . name . ToLower ( ) . Contains ( _search . ToLower ( ) ) )
337300 {
338301 continue ;
339302 }
340303
341- if ( SceneMode != SceneFilter . Any )
304+ if ( SceneMode != SceneFilter . Any && ! FilterScene ( obj , this . SceneMode ) )
342305 {
343- if ( SceneMode == SceneFilter . None )
344- {
345- if ( ! NoSceneFilter ( obj , obj . GetType ( ) ) )
346- {
347- continue ;
348- }
349- }
350- else
351- {
352- GameObject go ;
353-
354- var objtype = obj . GetType ( ) ;
355- if ( objtype == typeof ( GameObject ) )
356- {
357- go = obj as GameObject ;
358- }
359- else if ( typeof ( Component ) . IsAssignableFrom ( objtype ) )
360- {
361- go = ( obj as Component ) . gameObject ;
362- }
363- else { continue ; }
364-
365- if ( ! go ) { continue ; }
366-
367- if ( SceneMode == SceneFilter . This )
368- {
369- if ( go . scene . name != CppExplorer . ActiveSceneName || go . scene . name == "DontDestroyOnLoad" )
370- {
371- continue ;
372- }
373- }
374- else if ( SceneMode == SceneFilter . DontDestroy )
375- {
376- if ( go . scene . name != "DontDestroyOnLoad" )
377- {
378- continue ;
379- }
380- }
381- }
306+ continue ;
382307 }
383308
384309 if ( ! matches . Contains ( obj ) )
@@ -390,68 +315,78 @@ private List<object> FindAllObjectsOfType(string _search, string _type)
390315 return matches ;
391316 }
392317
393- public static bool ThisSceneFilter ( object obj , Type type )
318+ public static bool FilterScene ( object obj , SceneFilter filter )
394319 {
395- if ( type == typeof ( GameObject ) || typeof ( Component ) . IsAssignableFrom ( type ) )
320+ GameObject go ;
321+ if ( obj is Il2CppSystem . Object ilObject )
396322 {
397- var go = obj as GameObject ?? ( obj as Component ) . gameObject ;
398-
399- if ( go != null && go . scene . name == CppExplorer . ActiveSceneName && go . scene . name != "DontDestroyOnLoad" )
400- {
401- return true ;
402- }
323+ go = ilObject . TryCast < GameObject > ( ) ?? ilObject . TryCast < Component > ( ) . gameObject ;
403324 }
404-
405- return false ;
406- }
407-
408- public static bool DontDestroyFilter ( object obj , Type type )
409- {
410- if ( type == typeof ( GameObject ) || typeof ( Component ) . IsAssignableFrom ( type ) )
325+ else
411326 {
412- var go = obj as GameObject ?? ( obj as Component ) . gameObject ;
327+ go = ( obj as GameObject ) ?? ( obj as Component ) . gameObject ;
328+ }
413329
414- if ( go != null && go . scene . name == "DontDestroyOnLoad" )
415- {
416- return true ;
417- }
330+ if ( ! go )
331+ {
332+ // object is not on a GameObject, cannot perform scene filter operation.
333+ return false ;
418334 }
419335
336+ if ( filter == SceneFilter . None )
337+ {
338+ return string . IsNullOrEmpty ( go . scene . name ) ;
339+ }
340+ else if ( filter == SceneFilter . This )
341+ {
342+ return go . scene . name == CppExplorer . ActiveSceneName ;
343+ }
344+ else if ( filter == SceneFilter . DontDestroy )
345+ {
346+ return go . scene . name == "DontDestroyOnLoad" ;
347+ }
420348 return false ;
421349 }
422350
423- public static bool NoSceneFilter ( object obj , Type type )
351+ // ====== other ========
352+
353+ // credit: ManlyMarco (RuntimeUnityEditor)
354+ public static IEnumerable < object > GetInstanceClassScanner ( )
424355 {
425- if ( type == typeof ( GameObject ) )
426- {
427- var go = obj as GameObject ;
356+ var query = AppDomain . CurrentDomain . GetAssemblies ( )
357+ . Where ( x => ! x . FullName . StartsWith ( "Mono" ) )
358+ . SelectMany ( GetTypesSafe )
359+ . Where ( t => t . IsClass && ! t . IsAbstract && ! t . ContainsGenericParameters ) ;
428360
429- if ( go . scene . name != CppExplorer . ActiveSceneName && go . scene . name != "DontDestroyOnLoad" )
430- {
431- return true ;
432- }
433- else
361+ foreach ( var type in query )
362+ {
363+ object obj = null ;
364+ try
434365 {
435- return false ;
366+ obj = type . GetProperty ( "Instance" , BindingFlags . Public | BindingFlags . Static | BindingFlags . FlattenHierarchy ) ? . GetValue ( null , null ) ;
436367 }
437- }
438- else if ( typeof ( Component ) . IsAssignableFrom ( type ) )
439- {
440- var go = ( obj as Component ) . gameObject ;
441-
442- if ( go == null || ( go . scene . name != CppExplorer . ActiveSceneName && go . scene . name != "DontDestroyOnLoad" ) )
368+ catch
443369 {
444- return true ;
370+ try
371+ {
372+ obj = type . GetField ( "Instance" , BindingFlags . Public | BindingFlags . Static | BindingFlags . FlattenHierarchy ) ? . GetValue ( null ) ;
373+ }
374+ catch
375+ {
376+ }
445377 }
446- else
378+ if ( obj != null && ! obj . ToString ( ) . StartsWith ( "Mono" ) )
447379 {
448- return false ;
380+ yield return obj ;
449381 }
450382 }
451- else
452- {
453- return true ;
454- }
383+ }
384+
385+ public static IEnumerable < Type > GetTypesSafe ( Assembly asm )
386+ {
387+ try { return asm . GetTypes ( ) ; }
388+ catch ( ReflectionTypeLoadException e ) { return e . Types . Where ( x => x != null ) ; }
389+ catch { return Enumerable . Empty < Type > ( ) ; }
455390 }
456391 }
457392}
0 commit comments