Using fmt::format to format exception messages, using .extension() method to retrieve an extension

This commit is contained in:
Vladyslav Baranovskyi
2025-02-12 14:38:38 +02:00
parent 1f9154b46c
commit fdbf36c040
2 changed files with 12 additions and 18 deletions

View File

@@ -7,6 +7,7 @@
#include "MeshLoader.hpp"
#include "Scene/Geometry.hpp"
#include "Base/Logger.hpp"
#include "Extensions/FmtFormatter.hpp"
#if __has_include("assimp/Importer.hpp")
#include <assimp/Importer.hpp>
#include <assimp/scene.h>
@@ -26,14 +27,6 @@
#include "value-pprint.hh"
#include "value-types.hh"
namespace
{
static bool ends_with(std::string_view str, std::string_view suffix)
{
return str.size() >= suffix.size() && str.compare(str.size() - suffix.size(), suffix.size(), suffix) == 0;
}
}
namespace OpenVulkano::Scene
{
void MeshLoader::ParseAssimpFile(Geometry *geometry, const std::filesystem::path& file)
@@ -46,8 +39,8 @@ namespace OpenVulkano::Scene
aiProcess_ConvertToLeftHanded | aiProcess_PreTransformVertices | aiProcess_OptimizeGraph;
const aiScene* scene = importer.ReadFile(file.string().c_str(), flags);
if (!scene) throw std::runtime_error("Failed to load file \"" + file.string() + "\" Error: " + importer.GetErrorString());
if (!scene->HasMeshes()) throw std::runtime_error("File \"" + file.string() + "\" does not have any meshes");
if (!scene) throw std::runtime_error(fmt::format("Failed to load file \"{}\" Error: {}", file, importer.GetErrorString()));
if (!scene->HasMeshes()) throw std::runtime_error(fmt::format("File \"{}\" does not have any meshes", file));
if (scene->mNumMeshes > 1) Logger::DATA->warn("File {0} contains more than one mesh. Only first one will be loaded", file.string());
aiMesh *mesh = scene->mMeshes[0];
@@ -99,7 +92,7 @@ namespace OpenVulkano::Scene
auto result = tinyusdz::LoadUSDFromFile(file.string().c_str(), &stage, &warning, &err);
if (!result)
{
throw std::runtime_error("Failed to load USD file: " + file.string());
throw std::runtime_error(fmt::format("Failed to load USD file: {}", file));
}
for (auto &prim : stage.root_prims())
@@ -148,15 +141,15 @@ namespace OpenVulkano::Scene
}
}
}
throw std::runtime_error("No mesh found inside a xform in USD file: " + file.string());
throw std::runtime_error(fmt::format("No mesh found inside a xform in USD file: {}", file));
}
Geometry* MeshLoader::LoadFromFile(const std::filesystem::path& file)
{
Geometry* geometry = new Geometry();
std::string fileStr = file.string();
if (ends_with(fileStr, ".usd") || ends_with(fileStr, ".usda") || ends_with(fileStr, ".usdc") || ends_with(fileStr, ".usdz"))
const std::filesystem::path ext = file.extension();
if (ext == ".usd" || ext == ".usda" || ext == ".usdc" || ext == ".usdz")
{
ParseUSDFile(geometry, file);
}