Compare commits

...

2 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
4 changed files with 60 additions and 43 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
@@ -384,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
@@ -1120,6 +1128,8 @@ namespace AssetUsageDetectorNamespace
{ } { }
catch( MissingReferenceException ) catch( MissingReferenceException )
{ } { }
catch( MissingComponentException )
{ }
} }
} }
@@ -1149,12 +1159,14 @@ 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
Type variableType = fields[i].FieldType; Type variableType = field.FieldType;
if( variableType.IsPrimitiveUnityType() ) if( variableType.IsPrimitiveUnityType() )
continue; continue;
@@ -1162,16 +1174,20 @@ namespace AssetUsageDetectorNamespace
if( typeof( Type ).IsAssignableFrom( variableType ) || variableType.Namespace == reflectionNameSpace ) if( typeof( Type ).IsAssignableFrom( variableType ) || variableType.Namespace == reflectionNameSpace )
continue; continue;
// Searching pointer variables for reference throws ArgumentException
if( variableType.IsPointer )
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;
@@ -1186,12 +1202,14 @@ 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
Type variableType = properties[i].PropertyType; Type variableType = property.PropertyType;
if( variableType.IsPrimitiveUnityType() ) if( variableType.IsPrimitiveUnityType() )
continue; continue;
@@ -1199,15 +1217,28 @@ namespace AssetUsageDetectorNamespace
if( typeof( Type ).IsAssignableFrom( variableType ) || variableType.Namespace == reflectionNameSpace ) if( typeof( Type ).IsAssignableFrom( variableType ) || variableType.Namespace == reflectionNameSpace )
continue; 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;
// 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;
@@ -1230,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() ) );
} }
} }
@@ -1271,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();
@@ -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 )
{ {
} }
+2 -19
View File
@@ -310,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 )
{ {
@@ -339,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)