Compare commits

...

4 Commits

Author SHA1 Message Date
Süleyman Yasir KULA 69ce197932 Catching MissingComponentException 2019-10-18 23:53:25 +03:00
Süleyman Yasir KULA e3dc45b917 - Fixed FileNotFoundException on directories
- Fixed NullReferenceException on properties without a getter
- Fixed ArgumentException on pointer types
2019-10-15 22:40:06 +03:00
Süleyman Yasir KULA 3719ce7473 Fixes #13 2019-09-28 12:54:50 +03:00
Süleyman Yasir KULA b4b3e7d7b3 Fixed "Scenes in Build Settings" always being true 2019-09-20 23:09:44 +03:00
4 changed files with 77 additions and 49 deletions
@@ -111,7 +111,10 @@ namespace AssetUsageDetectorNamespace
fileSizes = new long[dependencies.Length]; fileSizes = new long[dependencies.Length];
for( int i = 0; i < dependencies.Length; i++ ) for( int i = 0; i < dependencies.Length; i++ )
fileSizes[i] = new FileInfo( dependencies[i] ).Length; {
FileInfo assetFile = new FileInfo( dependencies[i] );
fileSizes[i] = assetFile.Exists ? assetFile.Length : 0L;
}
} }
} }
#endregion #endregion
@@ -153,8 +156,10 @@ namespace AssetUsageDetectorNamespace
private int searchedObjectsCount; // Number of searched objects private int searchedObjectsCount; // Number of searched objects
private double searchStartTime; private double searchStartTime;
private List<ReferenceNode> nodesPool = new List<ReferenceNode>( 32 ); private readonly List<ReferenceNode> nodesPool = new List<ReferenceNode>( 32 );
private List<VariableGetterHolder> validVariables = new List<VariableGetterHolder>( 32 ); private readonly List<VariableGetterHolder> validVariables = new List<VariableGetterHolder>( 32 );
private readonly string reflectionNameSpace = typeof( Assembly ).Namespace;
private string CachePath { get { return Application.dataPath + "/../Library/AssetUsageDetector.cache"; } } // Path of the cache file private string CachePath { get { return Application.dataPath + "/../Library/AssetUsageDetector.cache"; } } // Path of the cache file
@@ -382,12 +387,17 @@ namespace AssetUsageDetectorNamespace
// Initialize the nodes of searched asset(s) // Initialize the nodes of searched asset(s)
foreach( Object obj in objectsToSearchSet ) foreach( Object obj in objectsToSearchSet )
{ {
BeginSearchObject( obj );
string objHash = obj.Hash(); string objHash = obj.Hash();
ReferenceNode referenceNode; if( searchParameters.dontSearchInSourceAssets )
if( !searchedObjects.TryGetValue( objHash, out referenceNode ) || referenceNode == null ) searchedObjects.Add( objHash, PopReferenceNode( obj ) );
searchedObjects[objHash] = PopReferenceNode( obj ); else
{
BeginSearchObject( obj );
ReferenceNode referenceNode;
if( !searchedObjects.TryGetValue( objHash, out referenceNode ) || referenceNode == null )
searchedObjects[objHash] = PopReferenceNode( obj );
}
} }
// Progressbar values // Progressbar values
@@ -1118,6 +1128,8 @@ namespace AssetUsageDetectorNamespace
{ } { }
catch( MissingReferenceException ) catch( MissingReferenceException )
{ } { }
catch( MissingComponentException )
{ }
} }
} }
@@ -1147,24 +1159,35 @@ namespace AssetUsageDetectorNamespace
FieldInfo[] fields = currType.GetFields( fieldModifiers ); FieldInfo[] fields = currType.GetFields( fieldModifiers );
for( int i = 0; i < fields.Length; i++ ) for( int i = 0; i < fields.Length; i++ )
{ {
FieldInfo field = fields[i];
// Skip obsolete fields // Skip obsolete fields
if( Attribute.IsDefined( fields[i], typeof( ObsoleteAttribute ) ) ) if( Attribute.IsDefined( field, typeof( ObsoleteAttribute ) ) )
continue; continue;
// Skip primitive types // Skip primitive types
if( fields[i].FieldType.IsPrimitiveUnityType() ) Type variableType = field.FieldType;
if( variableType.IsPrimitiveUnityType() )
continue;
// Searching assembly variables for reference throws InvalidCastException on .NET 4.0 runtime
if( typeof( Type ).IsAssignableFrom( variableType ) || variableType.Namespace == reflectionNameSpace )
continue;
// Searching pointer variables for reference throws ArgumentException
if( variableType.IsPointer )
continue; continue;
// Additional filtering for fields: // Additional filtering for fields:
// 1- Ignore "m_RectTransform", "m_CanvasRenderer" and "m_Canvas" fields of Graphic components // 1- Ignore "m_RectTransform", "m_CanvasRenderer" and "m_Canvas" fields of Graphic components
string fieldName = fields[i].Name; string fieldName = field.Name;
if( typeof( Graphic ).IsAssignableFrom( currType ) && if( typeof( Graphic ).IsAssignableFrom( currType ) &&
( fieldName == "m_RectTransform" || fieldName == "m_CanvasRenderer" || fieldName == "m_Canvas" ) ) ( fieldName == "m_RectTransform" || fieldName == "m_CanvasRenderer" || fieldName == "m_Canvas" ) )
continue; continue;
VariableGetVal getter = fields[i].CreateGetter( type ); VariableGetVal getter = field.CreateGetter( type );
if( getter != null ) if( getter != null )
validVariables.Add( new VariableGetterHolder( fields[i], getter, fields[i].IsSerializable() ) ); validVariables.Add( new VariableGetterHolder( field, getter, field.IsSerializable() ) );
} }
currType = currType.BaseType; currType = currType.BaseType;
@@ -1179,23 +1202,43 @@ namespace AssetUsageDetectorNamespace
PropertyInfo[] properties = currType.GetProperties( propertyModifiers ); PropertyInfo[] properties = currType.GetProperties( propertyModifiers );
for( int i = 0; i < properties.Length; i++ ) for( int i = 0; i < properties.Length; i++ )
{ {
PropertyInfo property = properties[i];
// Skip obsolete properties // Skip obsolete properties
if( Attribute.IsDefined( properties[i], typeof( ObsoleteAttribute ) ) ) if( Attribute.IsDefined( property, typeof( ObsoleteAttribute ) ) )
continue; continue;
// Skip primitive types // Skip primitive types
if( properties[i].PropertyType.IsPrimitiveUnityType() ) Type variableType = property.PropertyType;
if( variableType.IsPrimitiveUnityType() )
continue;
// Searching assembly variables for reference throws InvalidCastException on .NET 4.0 runtime
if( typeof( Type ).IsAssignableFrom( variableType ) || variableType.Namespace == reflectionNameSpace )
continue;
// Searching pointer variables for reference throws ArgumentException
if( variableType.IsPointer )
continue;
// Skip properties without a getter function
MethodInfo propertyGetter = property.GetGetMethod( true );
if( propertyGetter == null )
continue;
// Skip indexer properties
if( property.GetIndexParameters().Length > 0 )
continue; continue;
// No need to check properties with 'override' keyword // No need to check properties with 'override' keyword
if( properties[i].IsOverridden() ) if( propertyGetter.GetBaseDefinition().DeclaringType != propertyGetter.DeclaringType )
continue; continue;
// Additional filtering for properties: // Additional filtering for properties:
// 1- Ignore "gameObject", "transform", "rectTransform" and "attachedRigidbody" properties of Component's to get more useful results // 1- Ignore "gameObject", "transform", "rectTransform" and "attachedRigidbody" properties of Component's to get more useful results
// 2- Ignore "canvasRenderer" and "canvas" properties of Graphic components // 2- Ignore "canvasRenderer" and "canvas" properties of Graphic components
// 3 & 4- Prevent accessing properties of Unity that instantiate an existing resource (causing memory leak) // 3 & 4- Prevent accessing properties of Unity that instantiate an existing resource (causing memory leak)
string propertyName = properties[i].Name; string propertyName = property.Name;
if( typeof( Component ).IsAssignableFrom( currType ) && ( propertyName == "gameObject" || if( typeof( Component ).IsAssignableFrom( currType ) && ( propertyName == "gameObject" ||
propertyName == "transform" || propertyName == "attachedRigidbody" || propertyName == "rectTransform" ) ) propertyName == "transform" || propertyName == "attachedRigidbody" || propertyName == "rectTransform" ) )
continue; continue;
@@ -1218,9 +1261,9 @@ namespace AssetUsageDetectorNamespace
continue; continue;
else else
{ {
VariableGetVal getter = properties[i].CreateGetter(); VariableGetVal getter = property.CreateGetter();
if( getter != null ) if( getter != null )
validVariables.Add( new VariableGetterHolder( properties[i], getter, properties[i].IsSerializable() ) ); validVariables.Add( new VariableGetterHolder( property, getter, property.IsSerializable() ) );
} }
} }
@@ -1259,13 +1302,16 @@ namespace AssetUsageDetectorNamespace
{ {
// If a dependency was renamed (which doesn't affect the verified hash, unfortunately), // If a dependency was renamed (which doesn't affect the verified hash, unfortunately),
// force refresh the asset's dependencies and search it again // force refresh the asset's dependencies and search it again
FileInfo assetFile = new FileInfo( dependencies[i] ); if( !Directory.Exists( dependencies[i] ) ) // Calling FileInfo.Length on a directory throws FileNotFoundException
if( !assetFile.Exists || assetFile.Length != fileSizes[i] )
{ {
cacheEntry.Refresh( assetPath ); FileInfo assetFile = new FileInfo( dependencies[i] );
cacheEntry.searchResult = CacheEntry.Result.Unknown; if( !assetFile.Exists || assetFile.Length != fileSizes[i] )
{
cacheEntry.Refresh( assetPath );
cacheEntry.searchResult = CacheEntry.Result.Unknown;
return AssetHasAnyReference( assetPath ); return AssetHasAnyReference( assetPath );
}
} }
if( assetsToSearchPathsSet.Contains( dependencies[i] ) ) if( assetsToSearchPathsSet.Contains( dependencies[i] ) )
@@ -306,7 +306,7 @@ namespace AssetUsageDetectorNamespace.Extras
searchInAssetsSubsetDrawer.Draw( searchInAssetsSubset ); searchInAssetsSubsetDrawer.Draw( searchInAssetsSubset );
excludedAssetsDrawer.Draw( excludedAssets ); excludedAssetsDrawer.Draw( excludedAssets );
dontSearchInSourceAssets = EditorGUILayout.ToggleLeft( "Don't search 'Asset(s)' themselves for references", dontSearchInSourceAssets ); dontSearchInSourceAssets = EditorGUILayout.ToggleLeft( "Don't search \"Find references of\" themselves for references", dontSearchInSourceAssets );
GUILayout.EndVertical(); GUILayout.EndVertical();
GUILayout.EndHorizontal(); GUILayout.EndHorizontal();
@@ -321,7 +321,7 @@ namespace AssetUsageDetectorNamespace.Extras
if( !EditorApplication.isPlaying ) if( !EditorApplication.isPlaying )
{ {
searchInScenesInBuild = EditorGUILayout.ToggleLeft( "Scenes in Build Settings", true ); searchInScenesInBuild = EditorGUILayout.ToggleLeft( "Scenes in Build Settings", searchInScenesInBuild );
if( searchInScenesInBuild ) if( searchInScenesInBuild )
{ {
@@ -138,7 +138,7 @@ namespace AssetUsageDetectorNamespace.Extras
public class ObjectToSearchListDrawer : ListDrawer<ObjectToSearch> public class ObjectToSearchListDrawer : ListDrawer<ObjectToSearch>
{ {
public ObjectToSearchListDrawer() : base( "Asset(s):", true ) public ObjectToSearchListDrawer() : base( "Find references of:", true )
{ {
} }
+4 -22
View File
@@ -16,19 +16,18 @@ namespace AssetUsageDetectorNamespace.Extras
typeof( string ), typeof( Vector4 ), typeof( Vector3 ), typeof( Vector2 ), typeof( Rect ), typeof( string ), typeof( Vector4 ), typeof( Vector3 ), typeof( Vector2 ), typeof( Rect ),
typeof( Quaternion ), typeof( Color ), typeof( Color32 ), typeof( LayerMask ), typeof( Quaternion ), typeof( Color ), typeof( Color32 ), typeof( LayerMask ),
typeof( Matrix4x4 ), typeof( AnimationCurve ), typeof( Gradient ), typeof( RectOffset ), typeof( Matrix4x4 ), typeof( AnimationCurve ), typeof( Gradient ), typeof( RectOffset ),
typeof( Assembly ), // Searching assembly variables for reference throws InvalidCastException on .NET 4.0 runtime
typeof( bool[] ), typeof( byte[] ), typeof( sbyte[] ), typeof( char[] ), typeof( decimal[] ), typeof( bool[] ), typeof( byte[] ), typeof( sbyte[] ), typeof( char[] ), typeof( decimal[] ),
typeof( double[] ), typeof( float[] ), typeof( int[] ), typeof( uint[] ), typeof( long[] ), typeof( double[] ), typeof( float[] ), typeof( int[] ), typeof( uint[] ), typeof( long[] ),
typeof( ulong[] ), typeof( short[] ), typeof( ushort[] ), typeof( string[] ), typeof( ulong[] ), typeof( short[] ), typeof( ushort[] ), typeof( string[] ),
typeof( Vector4[] ), typeof( Vector3[] ), typeof( Vector2[] ), typeof( Rect[] ), typeof( Vector4[] ), typeof( Vector3[] ), typeof( Vector2[] ), typeof( Rect[] ),
typeof( Quaternion[] ), typeof( Color[] ), typeof( Color32[] ), typeof( LayerMask[] ), typeof( Quaternion[] ), typeof( Color[] ), typeof( Color32[] ), typeof( LayerMask[] ),
typeof( Matrix4x4[] ), typeof( AnimationCurve[] ), typeof( Gradient[] ), typeof( RectOffset[] ), typeof( Assembly[] ), typeof( Matrix4x4[] ), typeof( AnimationCurve[] ), typeof( Gradient[] ), typeof( RectOffset[] ),
typeof( List<bool> ), typeof( List<byte> ), typeof( List<sbyte> ), typeof( List<char> ), typeof( List<decimal> ), typeof( List<bool> ), typeof( List<byte> ), typeof( List<sbyte> ), typeof( List<char> ), typeof( List<decimal> ),
typeof( List<double> ), typeof( List<float> ), typeof( List<int> ), typeof( List<uint> ), typeof( List<long> ), typeof( List<double> ), typeof( List<float> ), typeof( List<int> ), typeof( List<uint> ), typeof( List<long> ),
typeof( List<ulong> ), typeof( List<short> ), typeof( List<ushort> ), typeof( List<string> ), typeof( List<ulong> ), typeof( List<short> ), typeof( List<ushort> ), typeof( List<string> ),
typeof( List<Vector4> ), typeof( List<Vector3> ), typeof( List<Vector2> ), typeof( List<Rect> ), typeof( List<Vector4> ), typeof( List<Vector3> ), typeof( List<Vector2> ), typeof( List<Rect> ),
typeof( List<Quaternion> ), typeof( List<Color> ), typeof( List<Color32> ), typeof( List<LayerMask> ), typeof( List<Quaternion> ), typeof( List<Color> ), typeof( List<Color32> ), typeof( List<LayerMask> ),
typeof( List<Matrix4x4> ), typeof( List<AnimationCurve> ), typeof( List<Gradient> ), typeof( List<RectOffset> ), typeof( List<Assembly> ) typeof( List<Matrix4x4> ), typeof( List<AnimationCurve> ), typeof( List<Gradient> ), typeof( List<RectOffset> )
}; };
private static readonly HashSet<string> folderContentsSet = new HashSet<string>(); private static readonly HashSet<string> folderContentsSet = new HashSet<string>();
@@ -311,13 +310,6 @@ namespace AssetUsageDetectorNamespace.Extras
return type.IsPrimitive || primitiveUnityTypes.Contains( type ) || type.IsEnum; return type.IsPrimitive || primitiveUnityTypes.Contains( type ) || type.IsEnum;
} }
// Check if property has override keyword
public static bool IsOverridden( this PropertyInfo propertyInfo )
{
MethodInfo mi = propertyInfo.GetGetMethod( true );
return mi.GetBaseDefinition().DeclaringType != mi.DeclaringType;
}
// Get <get> function for a field // Get <get> function for a field
public static VariableGetVal CreateGetter( this FieldInfo fieldInfo, Type type ) public static VariableGetVal CreateGetter( this FieldInfo fieldInfo, Type type )
{ {
@@ -340,22 +332,12 @@ namespace AssetUsageDetectorNamespace.Extras
// Get <get> function for a property // Get <get> function for a property
public static VariableGetVal CreateGetter( this PropertyInfo propertyInfo ) public static VariableGetVal CreateGetter( this PropertyInfo propertyInfo )
{ {
// Ignore indexer properties
if( propertyInfo.GetIndexParameters().Length > 0 )
return null;
// Can't use PropertyWrapper (which uses CreateDelegate) for property getters of structs // Can't use PropertyWrapper (which uses CreateDelegate) for property getters of structs
if( propertyInfo.DeclaringType.IsValueType ) if( propertyInfo.DeclaringType.IsValueType )
return propertyInfo.CanRead ? ( ( obj ) => propertyInfo.GetValue( obj, null ) ) : (VariableGetVal) null; return propertyInfo.CanRead ? ( ( obj ) => propertyInfo.GetValue( obj, null ) ) : (VariableGetVal) null;
MethodInfo mi = propertyInfo.GetGetMethod( true ); Type GenType = typeof( PropertyWrapper<,> ).MakeGenericType( propertyInfo.DeclaringType, propertyInfo.PropertyType );
if( mi != null ) return ( (IPropertyAccessor) Activator.CreateInstance( GenType, propertyInfo.GetGetMethod( true ) ) ).GetValue;
{
Type GenType = typeof( PropertyWrapper<,> ).MakeGenericType( propertyInfo.DeclaringType, propertyInfo.PropertyType );
return ( (IPropertyAccessor) Activator.CreateInstance( GenType, mi ) ).GetValue;
}
return null;
} }
// Check if all open scenes are saved (not dirty) // Check if all open scenes are saved (not dirty)