Skip to content

Commit

Permalink
Added force Unload functionality to ResourceManagers unload method an…
Browse files Browse the repository at this point in the history
…d also all IResourceProviders. Mainly to force an unload of an embeded resource when I need to.
  • Loading branch information
hybridmindset committed Feb 18, 2011
1 parent 974fb80 commit 051ba87
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 11 deletions.
7 changes: 4 additions & 3 deletions src/com/pblabs/engine/resource/ResourceManager.as
Expand Up @@ -163,8 +163,9 @@ package com.pblabs.engine.resource
*
* @param filename The url of the resource to unload.
* @param resourceType The type of the resource to unload.
* @param forceUnload will force an unload on a resource in a provider such as the EmbbedeResourceProvider
*/
public function unload(filename:String, resourceType:Class):void
public function unload(filename:String, resourceType:Class, forceUnload : Boolean = false):void
{
var resourceIdentifier:String = filename.toLowerCase() + resourceType;
if (!_resources[resourceIdentifier])
Expand All @@ -174,7 +175,7 @@ package com.pblabs.engine.resource
}
_resources[resourceIdentifier].decrementReferenceCount();

if (_resources[resourceIdentifier].referenceCount < 1)
if (_resources[resourceIdentifier].referenceCount < 1 || forceUnload)
{
_resources[resourceIdentifier] = null;
delete _resources[resourceIdentifier];
Expand All @@ -184,7 +185,7 @@ package com.pblabs.engine.resource
{
if ((resourceProviders[rp] as IResourceProvider).isResourceKnown(filename, resourceType))
{
(resourceProviders[rp] as IResourceProvider).unloadResource(filename, resourceType);
(resourceProviders[rp] as IResourceProvider).unloadResource(filename, resourceType, forceUnload);
return;
}
};
Expand Down
Expand Up @@ -96,7 +96,7 @@ package com.pblabs.engine.resource.provider
return resource;
}

public override function unloadResource(uri:String, type:Class):void
public override function unloadResource(uri:String, type:Class, force : Boolean = false):void
{
// no bulk loaded resources will be automaticly unloaded
// because we preloaded the resources and want to keep them available
Expand Down
Expand Up @@ -86,9 +86,17 @@ package com.pblabs.engine.resource.provider
}
}

public override function unloadResource(uri:String, type:Class):void
public override function unloadResource(uri:String, type:Class, force : Boolean = false):void
{
// no unloading of embedded resource
// Only unloading of embedded resource if forced to unload
if(force){
var resourceIdentifier:String = uri.toLowerCase() + type;
if (resources[resourceIdentifier]!=null)
{
resources[resourceIdentifier].dispose();
delete resources[resourceIdentifier];
}
}
}


Expand Down
Expand Up @@ -67,15 +67,14 @@ package com.pblabs.engine.resource.provider
return true;
}

public override function unloadResource(uri:String, type:Class):void
public override function unloadResource(uri:String, type:Class, force : Boolean = false):void
{
// because this resource was dynamicly loaded 'On The Fly'
// we have to unload it and clean memory
var resourceIdentifier:String = uri.toLowerCase() + type;
if (resources[resourceIdentifier]!=null)
{
if (resources[resourceIdentifier] is ImageResource)
(resources[resourceIdentifier] as ImageResource).dispose();
resources[resourceIdentifier].dispose();
resources[resourceIdentifier] = null;
}
}
Expand Down
Expand Up @@ -33,6 +33,6 @@ package com.pblabs.engine.resource.provider
* This method is called when the ResourceManager has no references to a specific resource
* and wants to unload the resource.
*/
function unloadResource(uri:String, type:Class):void;
function unloadResource(uri:String, type:Class, force : Boolean = false):void;
}
}
Expand Up @@ -57,7 +57,7 @@ package com.pblabs.engine.resource.provider
/**
* This method will unload a resource from the resources Dictionary
*/
public function unloadResource(uri:String, type:Class):void
public function unloadResource(uri:String, type:Class, force : Boolean = false):void
{
var resourceIdentifier:String = uri.toLowerCase() + type;
if (resources[resourceIdentifier]!=null)
Expand Down

0 comments on commit 051ba87

Please sign in to comment.