Skip to content

Commit e3dc45b

Browse files
committed
- Fixed FileNotFoundException on directories
- Fixed NullReferenceException on properties without a getter - Fixed ArgumentException on pointer types
1 parent 3719ce7 commit e3dc45b

File tree

4 files changed

+58
-43
lines changed

4 files changed

+58
-43
lines changed

Plugins/AssetUsageDetector/Editor/AssetUsageDetector.cs

+54-22
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,10 @@ public void Refresh( string path )
111111
fileSizes = new long[dependencies.Length];
112112

113113
for( int i = 0; i < dependencies.Length; i++ )
114-
fileSizes[i] = new FileInfo( dependencies[i] ).Length;
114+
{
115+
FileInfo assetFile = new FileInfo( dependencies[i] );
116+
fileSizes[i] = assetFile.Exists ? assetFile.Length : 0L;
117+
}
115118
}
116119
}
117120
#endregion
@@ -384,12 +387,17 @@ public SearchResult Run( Parameters searchParameters )
384387
// Initialize the nodes of searched asset(s)
385388
foreach( Object obj in objectsToSearchSet )
386389
{
387-
BeginSearchObject( obj );
388-
389390
string objHash = obj.Hash();
390-
ReferenceNode referenceNode;
391-
if( !searchedObjects.TryGetValue( objHash, out referenceNode ) || referenceNode == null )
392-
searchedObjects[objHash] = PopReferenceNode( obj );
391+
if( searchParameters.dontSearchInSourceAssets )
392+
searchedObjects.Add( objHash, PopReferenceNode( obj ) );
393+
else
394+
{
395+
BeginSearchObject( obj );
396+
397+
ReferenceNode referenceNode;
398+
if( !searchedObjects.TryGetValue( objHash, out referenceNode ) || referenceNode == null )
399+
searchedObjects[objHash] = PopReferenceNode( obj );
400+
}
393401
}
394402

395403
// Progressbar values
@@ -1149,29 +1157,35 @@ private VariableGetterHolder[] GetFilteredVariablesForType( Type type )
11491157
FieldInfo[] fields = currType.GetFields( fieldModifiers );
11501158
for( int i = 0; i < fields.Length; i++ )
11511159
{
1160+
FieldInfo field = fields[i];
1161+
11521162
// Skip obsolete fields
1153-
if( Attribute.IsDefined( fields[i], typeof( ObsoleteAttribute ) ) )
1163+
if( Attribute.IsDefined( field, typeof( ObsoleteAttribute ) ) )
11541164
continue;
11551165

11561166
// Skip primitive types
1157-
Type variableType = fields[i].FieldType;
1167+
Type variableType = field.FieldType;
11581168
if( variableType.IsPrimitiveUnityType() )
11591169
continue;
11601170

11611171
// Searching assembly variables for reference throws InvalidCastException on .NET 4.0 runtime
11621172
if( typeof( Type ).IsAssignableFrom( variableType ) || variableType.Namespace == reflectionNameSpace )
11631173
continue;
11641174

1175+
// Searching pointer variables for reference throws ArgumentException
1176+
if( variableType.IsPointer )
1177+
continue;
1178+
11651179
// Additional filtering for fields:
11661180
// 1- Ignore "m_RectTransform", "m_CanvasRenderer" and "m_Canvas" fields of Graphic components
1167-
string fieldName = fields[i].Name;
1181+
string fieldName = field.Name;
11681182
if( typeof( Graphic ).IsAssignableFrom( currType ) &&
11691183
( fieldName == "m_RectTransform" || fieldName == "m_CanvasRenderer" || fieldName == "m_Canvas" ) )
11701184
continue;
11711185

1172-
VariableGetVal getter = fields[i].CreateGetter( type );
1186+
VariableGetVal getter = field.CreateGetter( type );
11731187
if( getter != null )
1174-
validVariables.Add( new VariableGetterHolder( fields[i], getter, fields[i].IsSerializable() ) );
1188+
validVariables.Add( new VariableGetterHolder( field, getter, field.IsSerializable() ) );
11751189
}
11761190

11771191
currType = currType.BaseType;
@@ -1186,28 +1200,43 @@ private VariableGetterHolder[] GetFilteredVariablesForType( Type type )
11861200
PropertyInfo[] properties = currType.GetProperties( propertyModifiers );
11871201
for( int i = 0; i < properties.Length; i++ )
11881202
{
1203+
PropertyInfo property = properties[i];
1204+
11891205
// Skip obsolete properties
1190-
if( Attribute.IsDefined( properties[i], typeof( ObsoleteAttribute ) ) )
1206+
if( Attribute.IsDefined( property, typeof( ObsoleteAttribute ) ) )
11911207
continue;
11921208

11931209
// Skip primitive types
1194-
Type variableType = properties[i].PropertyType;
1210+
Type variableType = property.PropertyType;
11951211
if( variableType.IsPrimitiveUnityType() )
11961212
continue;
11971213

11981214
// Searching assembly variables for reference throws InvalidCastException on .NET 4.0 runtime
11991215
if( typeof( Type ).IsAssignableFrom( variableType ) || variableType.Namespace == reflectionNameSpace )
12001216
continue;
12011217

1218+
// Searching pointer variables for reference throws ArgumentException
1219+
if( variableType.IsPointer )
1220+
continue;
1221+
1222+
// Skip properties without a getter function
1223+
MethodInfo propertyGetter = property.GetGetMethod( true );
1224+
if( propertyGetter == null )
1225+
continue;
1226+
1227+
// Skip indexer properties
1228+
if( property.GetIndexParameters().Length > 0 )
1229+
continue;
1230+
12021231
// No need to check properties with 'override' keyword
1203-
if( properties[i].IsOverridden() )
1232+
if( propertyGetter.GetBaseDefinition().DeclaringType != propertyGetter.DeclaringType )
12041233
continue;
12051234

12061235
// Additional filtering for properties:
12071236
// 1- Ignore "gameObject", "transform", "rectTransform" and "attachedRigidbody" properties of Component's to get more useful results
12081237
// 2- Ignore "canvasRenderer" and "canvas" properties of Graphic components
12091238
// 3 & 4- Prevent accessing properties of Unity that instantiate an existing resource (causing memory leak)
1210-
string propertyName = properties[i].Name;
1239+
string propertyName = property.Name;
12111240
if( typeof( Component ).IsAssignableFrom( currType ) && ( propertyName == "gameObject" ||
12121241
propertyName == "transform" || propertyName == "attachedRigidbody" || propertyName == "rectTransform" ) )
12131242
continue;
@@ -1230,9 +1259,9 @@ private VariableGetterHolder[] GetFilteredVariablesForType( Type type )
12301259
continue;
12311260
else
12321261
{
1233-
VariableGetVal getter = properties[i].CreateGetter();
1262+
VariableGetVal getter = property.CreateGetter();
12341263
if( getter != null )
1235-
validVariables.Add( new VariableGetterHolder( properties[i], getter, properties[i].IsSerializable() ) );
1264+
validVariables.Add( new VariableGetterHolder( property, getter, property.IsSerializable() ) );
12361265
}
12371266
}
12381267

@@ -1271,13 +1300,16 @@ private bool AssetHasAnyReference( string assetPath )
12711300
{
12721301
// If a dependency was renamed (which doesn't affect the verified hash, unfortunately),
12731302
// force refresh the asset's dependencies and search it again
1274-
FileInfo assetFile = new FileInfo( dependencies[i] );
1275-
if( !assetFile.Exists || assetFile.Length != fileSizes[i] )
1303+
if( !Directory.Exists( dependencies[i] ) ) // Calling FileInfo.Length on a directory throws FileNotFoundException
12761304
{
1277-
cacheEntry.Refresh( assetPath );
1278-
cacheEntry.searchResult = CacheEntry.Result.Unknown;
1305+
FileInfo assetFile = new FileInfo( dependencies[i] );
1306+
if( !assetFile.Exists || assetFile.Length != fileSizes[i] )
1307+
{
1308+
cacheEntry.Refresh( assetPath );
1309+
cacheEntry.searchResult = CacheEntry.Result.Unknown;
12791310

1280-
return AssetHasAnyReference( assetPath );
1311+
return AssetHasAnyReference( assetPath );
1312+
}
12811313
}
12821314

12831315
if( assetsToSearchPathsSet.Contains( dependencies[i] ) )

Plugins/AssetUsageDetector/Editor/AssetUsageDetectorWindow.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ private void OnGUI()
306306
searchInAssetsSubsetDrawer.Draw( searchInAssetsSubset );
307307
excludedAssetsDrawer.Draw( excludedAssets );
308308

309-
dontSearchInSourceAssets = EditorGUILayout.ToggleLeft( "Don't search 'Asset(s)' themselves for references", dontSearchInSourceAssets );
309+
dontSearchInSourceAssets = EditorGUILayout.ToggleLeft( "Don't search \"Find references of\" themselves for references", dontSearchInSourceAssets );
310310

311311
GUILayout.EndVertical();
312312
GUILayout.EndHorizontal();

Plugins/AssetUsageDetector/Editor/ListDrawer.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ protected override void PostElementDrawer( Object element )
138138

139139
public class ObjectToSearchListDrawer : ListDrawer<ObjectToSearch>
140140
{
141-
public ObjectToSearchListDrawer() : base( "Asset(s):", true )
141+
public ObjectToSearchListDrawer() : base( "Find references of:", true )
142142
{
143143
}
144144

Plugins/AssetUsageDetector/Editor/Utilities.cs

+2-19
Original file line numberDiff line numberDiff line change
@@ -310,13 +310,6 @@ public static bool IsPrimitiveUnityType( this Type type )
310310
return type.IsPrimitive || primitiveUnityTypes.Contains( type ) || type.IsEnum;
311311
}
312312

313-
// Check if property has override keyword
314-
public static bool IsOverridden( this PropertyInfo propertyInfo )
315-
{
316-
MethodInfo mi = propertyInfo.GetGetMethod( true );
317-
return mi.GetBaseDefinition().DeclaringType != mi.DeclaringType;
318-
}
319-
320313
// Get <get> function for a field
321314
public static VariableGetVal CreateGetter( this FieldInfo fieldInfo, Type type )
322315
{
@@ -339,22 +332,12 @@ public static VariableGetVal CreateGetter( this FieldInfo fieldInfo, Type type )
339332
// Get <get> function for a property
340333
public static VariableGetVal CreateGetter( this PropertyInfo propertyInfo )
341334
{
342-
// Ignore indexer properties
343-
if( propertyInfo.GetIndexParameters().Length > 0 )
344-
return null;
345-
346335
// Can't use PropertyWrapper (which uses CreateDelegate) for property getters of structs
347336
if( propertyInfo.DeclaringType.IsValueType )
348337
return propertyInfo.CanRead ? ( ( obj ) => propertyInfo.GetValue( obj, null ) ) : (VariableGetVal) null;
349338

350-
MethodInfo mi = propertyInfo.GetGetMethod( true );
351-
if( mi != null )
352-
{
353-
Type GenType = typeof( PropertyWrapper<,> ).MakeGenericType( propertyInfo.DeclaringType, propertyInfo.PropertyType );
354-
return ( (IPropertyAccessor) Activator.CreateInstance( GenType, mi ) ).GetValue;
355-
}
356-
357-
return null;
339+
Type GenType = typeof( PropertyWrapper<,> ).MakeGenericType( propertyInfo.DeclaringType, propertyInfo.PropertyType );
340+
return ( (IPropertyAccessor) Activator.CreateInstance( GenType, propertyInfo.GetGetMethod( true ) ) ).GetValue;
358341
}
359342

360343
// Check if all open scenes are saved (not dirty)

0 commit comments

Comments
 (0)