Compare commits

...

3 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
4 changed files with 76 additions and 48 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
@@ -153,8 +156,10 @@ namespace AssetUsageDetectorNamespace
private int searchedObjectsCount; // Number of searched objects
private double searchStartTime;
private List<ReferenceNode> nodesPool = new List<ReferenceNode>( 32 );
private List<VariableGetterHolder> validVariables = new List<VariableGetterHolder>( 32 );
private readonly List<ReferenceNode> nodesPool = new List<ReferenceNode>( 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
@@ -382,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
@@ -1118,6 +1128,8 @@ namespace AssetUsageDetectorNamespace
{ }
catch( MissingReferenceException )
{ }
catch( MissingComponentException )
{ }
}
}
@@ -1147,24 +1159,35 @@ 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
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;
// 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;
@@ -1179,23 +1202,43 @@ 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
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;
// 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;
@@ -1218,9 +1261,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() ) );
}
}
@@ -1259,13 +1302,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 )
{
}
+4 -22
View File
@@ -16,19 +16,18 @@ namespace AssetUsageDetectorNamespace.Extras
typeof( string ), typeof( Vector4 ), typeof( Vector3 ), typeof( Vector2 ), typeof( Rect ),
typeof( Quaternion ), typeof( Color ), typeof( Color32 ), typeof( LayerMask ),
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( double[] ), typeof( float[] ), typeof( int[] ), typeof( uint[] ), typeof( long[] ),
typeof( ulong[] ), typeof( short[] ), typeof( ushort[] ), typeof( string[] ),
typeof( Vector4[] ), typeof( Vector3[] ), typeof( Vector2[] ), typeof( Rect[] ),
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<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<Vector4> ), typeof( List<Vector3> ), typeof( List<Vector2> ), typeof( List<Rect> ),
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>();
@@ -311,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 )
{
@@ -340,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)