Sfoglia il codice sorgente

Switch back to using Array, bug in Hash?

Daniele Bartolini 11 anni fa
parent
commit
9f5cdfb13d

+ 2 - 0
engine/resource/Resource.h

@@ -72,6 +72,8 @@ struct ResourceId
 	ResourceId(const char* type, const char* name);
 	ResourceId(const char* name);
 
+	bool operator==(const ResourceId& a) const { return id == a.id; }
+
 	uint64_t id;
 };
 

+ 20 - 29
engine/resource/ResourceManager.cpp

@@ -46,7 +46,7 @@ ResourceId::ResourceId(const char* type, const char* name)
 	res_name += '.';
 	res_name += type;
 
-	id = string::murmur2_64(res_name.c_str(), string::strlen(res_name.c_str()), 0);
+	id = string::murmur2_64(res_name.c_str(), res_name.length(), 0);
 }
 
 ResourceId::ResourceId(const char* name)
@@ -55,12 +55,11 @@ ResourceId::ResourceId(const char* name)
 }
 
 //-----------------------------------------------------------------------------
-ResourceManager::ResourceManager(Bundle& bundle, uint32_t seed) :
-	m_resource_heap("resource", default_allocator()),
-	m_loader(bundle, m_resource_heap),
-	m_seed(seed),
-	m_pendings(default_allocator()),
-	m_resources(default_allocator())
+ResourceManager::ResourceManager(Bundle& bundle)
+	: m_resource_heap("resource", default_allocator())
+	, m_loader(bundle, m_resource_heap)
+	, m_pendings(default_allocator())
+	, m_resources(default_allocator())
 {
 	m_loader.start();
 }
@@ -85,13 +84,16 @@ void ResourceManager::unload(ResourceId name, bool force)
 	ResourceEntry* entry = find(name);
 
 	entry->references--;
-	
+
 	if (entry->references == 0 || force)
 	{
 		resource_on_offline(entry->type, entry->resource);
 		resource_on_unload(entry->type, m_resource_heap, entry->resource);
 
-		hash::remove(m_resources, name.id);
+		// Swap with last
+		ResourceEntry temp = m_resources[array::size(m_resources) - 1];
+		(*entry) = temp;
+		array::pop_back(m_resources);
 	}
 }
 
@@ -106,7 +108,6 @@ bool ResourceManager::has(ResourceId name) const
 //-----------------------------------------------------------------------------
 const void* ResourceManager::get(const char* type, const char* name) const
 {
-
 	ResourceEntry* entry = find(ResourceId(type, name));
 
 	CE_ASSERT(entry != NULL, "Resource not loaded: type = '%s', name = '%s'", type, name);
@@ -148,23 +149,12 @@ void ResourceManager::flush()
 	}
 }
 
-//-----------------------------------------------------------------------------
-uint32_t ResourceManager::seed() const
-{
-	return m_seed;
-}
-
 //-----------------------------------------------------------------------------
 ResourceEntry* ResourceManager::find(ResourceId id) const
 {
-	ResourceEntry deff;
-	deff.type = 0xFFFFFFFFu;
-	deff.references = 0xFFFFFFFFu;
-	deff.resource = NULL;
-
-	const ResourceEntry& entry = hash::get(m_resources, id.id, deff);
+	const ResourceEntry* entry = std::find(array::begin(m_resources), array::end(m_resources), id);
 
-	return memcmp(&deff, &entry, sizeof(ResourceEntry)) == 0 ? NULL : const_cast<ResourceEntry*>(&entry);
+	return entry != array::end(m_resources) ? const_cast<ResourceEntry*>(entry) : NULL;
 }
 
 //-----------------------------------------------------------------------------
@@ -193,13 +183,14 @@ ResourceId ResourceManager::load(uint32_t type, ResourceId name)
 	// If resource not found, create a new one
 	if (entry == NULL)
 	{
-		ResourceEntry new_entry;
+		ResourceEntry entry;
 
-		new_entry.type = type;
-		new_entry.references = 1;
-		new_entry.resource = NULL;
+		entry.id = name;
+		entry.type = type;
+		entry.references = 1;
+		entry.resource = NULL;
 
-		hash::set(m_resources, name.id, new_entry);
+		array::push_back(m_resources, entry);
 
 		// Issue request to resource loader
 		PendingRequest pr;
@@ -214,7 +205,7 @@ ResourceId ResourceManager::load(uint32_t type, ResourceId name)
 	// Else, increment its reference count
 	entry->references++;
 
-	return name;
+	return entry->id;
 }
 
 //-----------------------------------------------------------------------------

+ 11 - 13
engine/resource/ResourceManager.h

@@ -31,16 +31,19 @@ OTHER DEALINGS IN THE SOFTWARE.
 #include "Resource.h"
 #include "ProxyAllocator.h"
 #include "ResourceLoader.h"
-#include "Hash.h"
 
 namespace crown
 {
 
 struct ResourceEntry
 {
-	uint32_t type;
-	uint32_t references;
-	void* resource;
+	bool operator==(const ResourceId& resource) const { return id == resource; }
+	bool operator==(const ResourceEntry& b) const { return id == b.id; }
+
+	ResourceId		id;
+	uint32_t		type;
+	uint32_t		references;
+	void*			resource;
 };
 
 struct PendingRequest
@@ -52,17 +55,13 @@ struct PendingRequest
 
 class Bundle;
 
-/// @defgroup Resource Resource.
-
 /// Keeps track and manages resources loaded by ResourceLoader.
-///
-/// @ingroup Resource
 class ResourceManager
 {
 public:
 
 	/// The resources will be loaded from @a bundle.
-	ResourceManager(Bundle& bundle, uint32_t seed);
+	ResourceManager(Bundle& bundle);
 	~ResourceManager();
 
 	/// Loads the resource by @a type and @a name and returns its ResourceId.
@@ -102,11 +101,10 @@ public:
 	/// Forces all of the loading requests to complete before preceeding.
 	void flush();
 
-	/// Returns the seed used to generate resource name hashes.
-	uint32_t seed() const;
-
 private:
 
+
+	// Returns the entry of the given id.
 	ResourceEntry* find(ResourceId id) const;
 
 	// Polls the resource loader for loaded resources.
@@ -123,7 +121,7 @@ private:
 	uint32_t m_seed;
 
 	Queue<PendingRequest> m_pendings;
-	Hash<ResourceEntry> m_resources;
+	Array<ResourceEntry> m_resources;
 
 private: