Skip to content

Commit

Permalink
fixed bug with unloading assets
Browse files Browse the repository at this point in the history
  • Loading branch information
prime31 committed Oct 10, 2016
1 parent 123efd2 commit ef00d30
Showing 1 changed file with 23 additions and 10 deletions.
33 changes: 23 additions & 10 deletions Nez-PCL/Utils/NezContentManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -207,26 +207,39 @@ public void unloadAsset<T>( string assetName ) where T : class, IDisposable
{
try
{
var fieldInfo = typeof( ContentManager ).GetRuntimeField( "disposableAssets" );
var assets = fieldInfo.GetValue( this ) as List<IDisposable>;
FieldInfo fieldInfo = null;
var fields = typeof( ContentManager ).GetRuntimeFields();
foreach( var field in fields )
{
if( field.Name == "disposableAssets" )
{
fieldInfo = field;
break;
}
}

// first fetch the actual asset. we already know its loaded so we'll grab it directly
#if FNA
fieldInfo = typeof( ContentManager ).GetRuntimeField( "loadedAssets" );
var LoadedAssets = fieldInfo.GetValue( this ) as Dictionary<string, object>;
#endif

var assetToRemove = LoadedAssets[assetName];

var assets = fieldInfo.GetValue( this ) as List<IDisposable>;
for( var i = 0; i < assets.Count; i++ )
{
// see if the asset is disposeable. If so, find and dispose of it.
var typedAsset = assets[i] as T;
if( typedAsset != null )
if( typedAsset != null && typedAsset == assetToRemove )
{
typedAsset.Dispose();
assets.RemoveAt( i );

#if FNA
fieldInfo = typeof( ContentManager ).GetRuntimeField( "loadedAssets" );
var LoadedAssets = fieldInfo.GetValue( this ) as Dictionary<string, object>;
#endif

LoadedAssets.Remove( assetName );
break;
}
}

LoadedAssets.Remove( assetName );
}
catch( Exception e )
{
Expand Down

0 comments on commit ef00d30

Please sign in to comment.