فهرست منبع

Check before loading

Daniele Bartolini 10 سال پیش
والد
کامیت
515f8dd063
3فایلهای تغییر یافته به همراه33 افزوده شده و 1 حذف شده
  1. 14 0
      src/resource/resource_loader.cpp
  2. 3 0
      src/resource/resource_loader.h
  3. 16 1
      src/resource/resource_manager.cpp

+ 14 - 0
src/resource/resource_loader.cpp

@@ -29,6 +29,20 @@ ResourceLoader::~ResourceLoader()
 	_thread.stop();
 }
 
+bool ResourceLoader::can_load(StringId64 type, StringId64 name)
+{
+	char buf[1 + 2*StringId64::STRING_LENGTH];
+	type.to_string(buf);
+	buf[16] = '-';
+	name.to_string(buf + 17);
+
+	TempAllocator256 alloc;
+	DynamicString path(alloc);
+	path::join(CROWN_DATA_DIRECTORY, buf, path);
+
+	return _fs.exists(path.c_str());
+}
+
 void ResourceLoader::add_request(const ResourceRequest& rr)
 {
 	ScopedMutex sm(_mutex);

+ 3 - 0
src/resource/resource_loader.h

@@ -37,6 +37,9 @@ public:
 	ResourceLoader(Filesystem& fs);
 	~ResourceLoader();
 
+	/// Returns whether the resource (type, name) can be loaded.
+	bool can_load(StringId64 type, StringId64 name);
+
 	/// Adds a request for loading the resource described by @a rr.
 	void add_request(const ResourceRequest& rr);
 

+ 16 - 1
src/resource/resource_manager.cpp

@@ -88,6 +88,17 @@ void ResourceManager::load(StringId64 type, StringId64 name)
 
 	if (entry == ResourceEntry::NOT_FOUND)
 	{
+		char type_buf[StringId64::STRING_LENGTH];
+		char name_buf[StringId64::STRING_LENGTH];
+
+		CE_ASSERT(_loader->can_load(type, name)
+			, "Can't load resource #ID(%s-%s)"
+			, type.to_string(type_buf)
+			, name.to_string(name_buf)
+			);
+		CE_UNUSED(type_buf);
+		CE_UNUSED(name_buf);
+
 		ResourceRequest rr;
 		rr.type = type;
 		rr.name = name;
@@ -144,7 +155,11 @@ const void* ResourceManager::get(StringId64 type, StringId64 name)
 	char type_buf[StringId64::STRING_LENGTH];
 	char name_buf[StringId64::STRING_LENGTH];
 
-	CE_ASSERT(can_get(type, name), "Resource not loaded #ID(%s-%s)", type.to_string(type_buf), name.to_string(name_buf));
+	CE_ASSERT(can_get(type, name)
+		, "Resource not loaded #ID(%s-%s)"
+		, type.to_string(type_buf)
+		, name.to_string(name_buf)
+		);
 	CE_UNUSED(type_buf);
 	CE_UNUSED(name_buf);