Przeglądaj źródła

Throw "resource not found" instead of crashing on invalid query

Ivan Savenko 1 rok temu
rodzic
commit
2d8692c142
1 zmienionych plików z 16 dodań i 11 usunięć
  1. 16 11
      lib/modding/CModHandler.cpp

+ 16 - 11
lib/modding/CModHandler.cpp

@@ -339,20 +339,25 @@ void CModHandler::loadModFilesystems()
 
 TModID CModHandler::findResourceOrigin(const ResourcePath & name) const
 {
-	for(const auto & modID : boost::adaptors::reverse(activeMods))
+	try
 	{
-		if(CResourceHandler::get(modID)->existsResource(name))
-			return modID;
-	}
-
-	if(CResourceHandler::get("core")->existsResource(name))
-		return "core";
+		for(const auto & modID : boost::adaptors::reverse(activeMods))
+		{
+			if(CResourceHandler::get(modID)->existsResource(name))
+				return modID;
+		}
 
-	if(CResourceHandler::get("mapEditor")->existsResource(name))
-		return "core"; // Workaround for loading maps via map editor
+		if(CResourceHandler::get("core")->existsResource(name))
+			return "core";
 
-	assert(0);
-	return "";
+		if(CResourceHandler::get("mapEditor")->existsResource(name))
+			return "core"; // Workaround for loading maps via map editor
+	}
+	catch( const std::out_of_range & e)
+	{
+		// no-op
+	}
+	throw std::runtime_error("Resource with name " + name.getName() + " and type " + EResTypeHelper::getEResTypeAsString(name.getType()) + " wasn't found.");
 }
 
 std::string CModHandler::getModLanguage(const TModID& modId) const