Przeglądaj źródła

resource: return newly loaded resource

Daniele Bartolini 1 rok temu
rodzic
commit
cb6d3cc5ba

+ 6 - 6
src/resource/resource_manager.cpp

@@ -158,27 +158,27 @@ void ResourceManager::unload(StringId64 type, StringId64 name)
 	}
 }
 
-void ResourceManager::reload(StringId64 type, StringId64 name)
+void *ResourceManager::reload(StringId64 type, StringId64 name)
 {
 	const ResourcePair id = { type, name };
 	const ResourceData &rd = hash_map::get(_resources, id, ResourceData::NOT_FOUND);
-	const u32 old_refs = rd.references;
 
 	if (rd == ResourceData::NOT_FOUND)
-		return;
+		return NULL;
 
+	const u32 old_refs = rd.references;
 	unload(type, name);
 
 	while (!try_load(PACKAGE_RESOURCE_NONE, type, name, 0)) {
 		complete_requests();
 	}
 
-	while (!hash_map::has(_resources, id)) {
+	ResourceData new_rd;
+	while ((new_rd = hash_map::get(_resources, id, ResourceData::NOT_FOUND)) == ResourceData::NOT_FOUND)
 		complete_requests();
-	}
 
-	ResourceData &new_rd = hash_map::get(_resources, id, ResourceData::NOT_FOUND);
 	new_rd.references = old_refs;
+	return new_rd.data;
 }
 
 bool ResourceManager::can_get(StringId64 type, StringId64 name)

+ 3 - 2
src/resource/resource_manager.h

@@ -79,9 +79,10 @@ struct ResourceManager
 	/// Unloads the resource @a type @a name.
 	void unload(StringId64 type, StringId64 name);
 
-	/// Reloads the resource (@a type, @a name).
+	/// Reloads the resource (@a type, @a name) and returns its new data.
+	/// If the resource is not loaded, it returns NULL.
 	/// @note The user has to manually update all the references to the old resource.
-	void reload(StringId64 type, StringId64 name);
+	void *reload(StringId64 type, StringId64 name);
 
 	/// Returns whether the manager has the resource (@a type, @a name).
 	bool can_get(StringId64 type, StringId64 name);