Compare commits

..

1 Commits

Author SHA1 Message Date
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 58 additions and 43 deletions
@@ -111,7 +111,10 @@ namespace AssetUsageDetectorNamespace
fileSizes = new long[dependencies.Length];
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
@@ -384,12 +387,17 @@ namespace AssetUsageDetectorNamespace
// Initialize the nodes of searched asset(s)
foreach( Object obj in objectsToSearchSet )
{
BeginSearchObject( obj );
string objHash = obj.Hash();
ReferenceNode referenceNode;
if( !searchedObjects.TryGetValue( objHash, out referenceNode ) || referenceNode == null )
searchedObjects[objHash] = PopReferenceNode( obj );
if( searchParameters.dontSearchInSourceAssets )
searchedObjects.Add( objHash, PopReferenceNode( obj ) );
else
{
BeginSearchObject( obj );
ReferenceNode referenceNode;
if( !searchedObjects.TryGetValue( objHash, out referenceNode ) || referenceNode == null )
searchedObjects[objHash] = PopReferenceNode( obj );
}
}
// Progressbar values
@@ -1149,12 +1157,14 @@ namespace AssetUsageDetectorNamespace
FieldInfo[] fields = currType.GetFields( fieldModifiers );
for( int i = 0; i < fields.Length; i++ )
{
FieldInfo field = fields[i];
// Skip obsolete fields
if( Attribute.IsDefined( fields[i], typeof( ObsoleteAttribute ) ) )
if( Attribute.IsDefined( field, typeof( ObsoleteAttribute ) ) )
continue;
// Skip primitive types
Type variableType = fields[i].FieldType;
Type variableType = field.FieldType;
if( variableType.IsPrimitiveUnityType() )
continue;
@@ -1162,16 +1172,20 @@ namespace AssetUsageDetectorNamespace
if( typeof( Type ).IsAssignableFrom( variableType ) || variableType.Namespace == reflectionNameSpace )
continue;
// Searching pointer variables for reference throws ArgumentException
if( variableType.IsPointer )
continue;
// Additional filtering for fields:
// 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 ) &&
( fieldName == "m_RectTransform" || fieldName == "m_CanvasRenderer" || fieldName == "m_Canvas" ) )
continue;
VariableGetVal getter = fields[i].CreateGetter( type );
VariableGetVal getter = field.CreateGetter( type );
if( getter != null )
validVariables.Add( new VariableGetterHolder( fields[i], getter, fields[i].IsSerializable() ) );
validVariables.Add( new VariableGetterHolder( field, getter, field.IsSerializable() ) );
}
currType = currType.BaseType;
@@ -1186,12 +1200,14 @@ namespace AssetUsageDetectorNamespace
PropertyInfo[] properties = currType.GetProperties( propertyModifiers );
for( int i = 0; i < properties.Length; i++ )
{
PropertyInfo property = properties[i];
// Skip obsolete properties
if( Attribute.IsDefined( properties[i], typeof( ObsoleteAttribute ) ) )
if( Attribute.IsDefined( property, typeof( ObsoleteAttribute ) ) )
continue;
// Skip primitive types
Type variableType = properties[i].PropertyType;
Type variableType = property.PropertyType;
if( variableType.IsPrimitiveUnityType() )
continue;
@@ -1199,15 +1215,28 @@ namespace AssetUsageDetectorNamespace
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;
// No need to check properties with 'override' keyword
if( properties[i].IsOverridden() )
if( propertyGetter.GetBaseDefinition().DeclaringType != propertyGetter.DeclaringType )
continue;
// Additional filtering for properties:
// 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
// 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" ||
propertyName == "transform" || propertyName == "attachedRigidbody" || propertyName == "rectTransform" ) )
continue;
@@ -1230,9 +1259,9 @@ namespace AssetUsageDetectorNamespace
continue;
else
{
VariableGetVal getter = properties[i].CreateGetter();
VariableGetVal getter = property.CreateGetter();
if( getter != null )
validVariables.Add( new VariableGetterHolder( properties[i], getter, properties[i].IsSerializable() ) );
validVariables.Add( new VariableGetterHolder( property, getter, property.IsSerializable() ) );
}
}
@@ -1271,13 +1300,16 @@ namespace AssetUsageDetectorNamespace
{
// If a dependency was renamed (which doesn't affect the verified hash, unfortunately),
// force refresh the asset's dependencies and search it again
FileInfo assetFile = new FileInfo( dependencies[i] );
if( !assetFile.Exists || assetFile.Length != fileSizes[i] )
if( !Directory.Exists( dependencies[i] ) ) // Calling FileInfo.Length on a directory throws FileNotFoundException
{
cacheEntry.Refresh( assetPath );
cacheEntry.searchResult = CacheEntry.Result.Unknown;
FileInfo assetFile = new FileInfo( dependencies[i] );
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] ) )
@@ -306,7 +306,7 @@ namespace AssetUsageDetectorNamespace.Extras
searchInAssetsSubsetDrawer.Draw( searchInAssetsSubset );
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.EndHorizontal();
@@ -138,7 +138,7 @@ namespace AssetUsageDetectorNamespace.Extras
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;
}
// 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
public static VariableGetVal CreateGetter( this FieldInfo fieldInfo, Type type )
{
@@ -339,22 +332,12 @@ namespace AssetUsageDetectorNamespace.Extras
// Get <get> function for a property
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
if( propertyInfo.DeclaringType.IsValueType )
return propertyInfo.CanRead ? ( ( obj ) => propertyInfo.GetValue( obj, null ) ) : (VariableGetVal) null;
MethodInfo mi = propertyInfo.GetGetMethod( true );
if( mi != null )
{
Type GenType = typeof( PropertyWrapper<,> ).MakeGenericType( propertyInfo.DeclaringType, propertyInfo.PropertyType );
return ( (IPropertyAccessor) Activator.CreateInstance( GenType, mi ) ).GetValue;
}
return null;
Type GenType = typeof( PropertyWrapper<,> ).MakeGenericType( propertyInfo.DeclaringType, propertyInfo.PropertyType );
return ( (IPropertyAccessor) Activator.CreateInstance( GenType, propertyInfo.GetGetMethod( true ) ) ).GetValue;
}
// Check if all open scenes are saved (not dirty)