Browse Source

Add force option to ResourceManager::unload()

Daniele Bartolini 12 năm trước cách đây
mục cha
commit
17a9fb30d6

+ 2 - 2
engine/resource/ResourceManager.cpp

@@ -61,7 +61,7 @@ ResourceId ResourceManager::load(const char* type, const char* name)
 }
 }
 
 
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
-void ResourceManager::unload(ResourceId name)
+void ResourceManager::unload(ResourceId name, bool force)
 {
 {
 	CE_ASSERT(has(name), "Resource not loaded: " "%.16"PRIx64"", name.id);
 	CE_ASSERT(has(name), "Resource not loaded: " "%.16"PRIx64"", name.id);
 
 
@@ -69,7 +69,7 @@ void ResourceManager::unload(ResourceId name)
 
 
 	entry->references--;
 	entry->references--;
 	
 	
-	if (entry->references == 0)
+	if (entry->references == 0 || force)
 	{
 	{
 		resource_on_offline(entry->type, entry->resource);
 		resource_on_offline(entry->type, entry->resource);
 		resource_on_unload(entry->type, m_resource_heap, entry->resource);
 		resource_on_unload(entry->type, m_resource_heap, entry->resource);

+ 7 - 2
engine/resource/ResourceManager.h

@@ -71,7 +71,11 @@ public:
 
 
 	/// Unloads the resource @a name, freeing up all the memory associated by it
 	/// Unloads the resource @a name, freeing up all the memory associated by it
 	/// and eventually any global object associated with it.
 	/// and eventually any global object associated with it.
-	void					unload(ResourceId name);
+	/// If @a force is true, the resource is unloaded even if its reference count
+	/// is greater than 1.
+	/// @warning
+	/// Use @a force option only if you know - exactly - what you are doing.
+	void					unload(ResourceId name, bool force = false);
 
 
 	/// Returns the resource instance associated to the given @a type and @a name.
 	/// Returns the resource instance associated to the given @a type and @a name.
 	const void*				lookup(const char* type, const char* name) const;
 	const void*				lookup(const char* type, const char* name) const;
@@ -100,9 +104,10 @@ public:
 	/// Returns the seed used to generate resource name hashes.
 	/// Returns the seed used to generate resource name hashes.
 	uint32_t				seed() const;
 	uint32_t				seed() const;
 
 
+	ResourceId				resource_id(const char* type, const char* name) const;
+
 private:
 private:
 
 
-	ResourceId				resource_id(const char* type, const char* name) const;
 
 
 	// Returns the entry of the given id.
 	// Returns the entry of the given id.
 	ResourceEntry*			find(ResourceId id) const;
 	ResourceEntry*			find(ResourceId id) const;