From 6e4f886999c5ab90c38cd2d6d8c90354b2d6713b Mon Sep 17 00:00:00 2001 From: Maciej Date: Sun, 4 Dec 2022 14:52:41 +0100 Subject: [PATCH] 3DFileViewer: Properly propagate errors from WavefrontOBJLoader Fixes 3 FIXMEs. --- .../Applications/3DFileViewer/MeshLoader.h | 2 +- .../3DFileViewer/WavefrontOBJLoader.cpp | 23 ++++++++----------- .../3DFileViewer/WavefrontOBJLoader.h | 2 +- Userland/Applications/3DFileViewer/main.cpp | 6 ++--- 4 files changed, 14 insertions(+), 19 deletions(-) diff --git a/Userland/Applications/3DFileViewer/MeshLoader.h b/Userland/Applications/3DFileViewer/MeshLoader.h index 2ad0bcba1db..0c33507962c 100644 --- a/Userland/Applications/3DFileViewer/MeshLoader.h +++ b/Userland/Applications/3DFileViewer/MeshLoader.h @@ -18,5 +18,5 @@ public: MeshLoader() = default; virtual ~MeshLoader() = default; - virtual RefPtr load(Core::File& file) = 0; + virtual ErrorOr> load(Core::File& file) = 0; }; diff --git a/Userland/Applications/3DFileViewer/WavefrontOBJLoader.cpp b/Userland/Applications/3DFileViewer/WavefrontOBJLoader.cpp index baed60913a4..ebe71058f50 100644 --- a/Userland/Applications/3DFileViewer/WavefrontOBJLoader.cpp +++ b/Userland/Applications/3DFileViewer/WavefrontOBJLoader.cpp @@ -16,7 +16,7 @@ static inline GLuint get_index_value(StringView& representation) return representation.to_uint().value_or(1) - 1; } -RefPtr WavefrontOBJLoader::load(Core::File& file) +ErrorOr> WavefrontOBJLoader::load(Core::File& file) { Vector vertices; Vector normals; @@ -34,8 +34,7 @@ RefPtr WavefrontOBJLoader::load(Core::File& file) if (object_line.starts_with("vt"sv)) { auto tex_coord_line = object_line.split_view(' '); if (tex_coord_line.size() != 3) { - dbgln("Wavefront: Malformed TexCoord line. Aborting."); - return nullptr; + return Error::from_string_literal("Wavefront: Malformed TexCoord line."); } tex_coords.append({ static_cast(atof(DeprecatedString(tex_coord_line.at(1)).characters())), @@ -47,8 +46,7 @@ RefPtr WavefrontOBJLoader::load(Core::File& file) if (object_line.starts_with("vn"sv)) { auto normal_line = object_line.split_view(' '); if (normal_line.size() != 4) { - dbgln("Wavefront: Malformed vertex normal line. Aborting."); - return nullptr; + return Error::from_string_literal("Wavefront: Malformed vertex normal line."); } normals.append({ static_cast(atof(DeprecatedString(normal_line.at(1)).characters())), @@ -62,8 +60,7 @@ RefPtr WavefrontOBJLoader::load(Core::File& file) if (object_line.starts_with('v')) { auto vertex_line = object_line.split_view(' '); if (vertex_line.size() != 4) { - dbgln("Wavefront: Malformed vertex line. Aborting."); - return nullptr; + return Error::from_string_literal("Wavefront: Malformed vertex line."); } vertices.append({ static_cast(atof(DeprecatedString(vertex_line.at(1)).characters())), @@ -78,13 +75,12 @@ RefPtr WavefrontOBJLoader::load(Core::File& file) auto face_line = object_line.substring_view(2).split_view(' '); auto number_of_vertices = face_line.size(); if (number_of_vertices < 3) { - dbgln("Wavefront: Malformed face line. Aborting."); - return nullptr; + return Error::from_string_literal("Wavefront: Malformed face line."); } - auto vertex_indices = FixedArray::must_create_but_fixme_should_propagate_errors(number_of_vertices); - auto tex_coord_indices = FixedArray::must_create_but_fixme_should_propagate_errors(number_of_vertices); - auto normal_indices = FixedArray::must_create_but_fixme_should_propagate_errors(number_of_vertices); + auto vertex_indices = TRY(FixedArray::try_create(number_of_vertices)); + auto tex_coord_indices = TRY(FixedArray::try_create(number_of_vertices)); + auto normal_indices = TRY(FixedArray::try_create(number_of_vertices)); for (size_t i = 0; i < number_of_vertices; ++i) { auto vertex_parts = face_line.at(i).split_view('/', SplitBehavior::KeepEmpty); @@ -111,8 +107,7 @@ RefPtr WavefrontOBJLoader::load(Core::File& file) } if (vertices.is_empty()) { - dbgln("Wavefront: Failed to read any data from 3D file: {}", file.name()); - return nullptr; + return Error::from_string_literal("Wavefront: Failed to read any data from 3D file"); } dbgln("Wavefront: Done."); diff --git a/Userland/Applications/3DFileViewer/WavefrontOBJLoader.h b/Userland/Applications/3DFileViewer/WavefrontOBJLoader.h index 67a260b46c5..77e197e43d1 100644 --- a/Userland/Applications/3DFileViewer/WavefrontOBJLoader.h +++ b/Userland/Applications/3DFileViewer/WavefrontOBJLoader.h @@ -18,5 +18,5 @@ public: WavefrontOBJLoader() = default; ~WavefrontOBJLoader() override = default; - RefPtr load(Core::File& file) override; + ErrorOr> load(Core::File& file) override; }; diff --git a/Userland/Applications/3DFileViewer/main.cpp b/Userland/Applications/3DFileViewer/main.cpp index c5de4c63738..70451f07fdf 100644 --- a/Userland/Applications/3DFileViewer/main.cpp +++ b/Userland/Applications/3DFileViewer/main.cpp @@ -320,8 +320,8 @@ bool GLContextWidget::load_file(Core::File& file) } auto new_mesh = m_mesh_loader->load(file); - if (new_mesh.is_null()) { - GUI::MessageBox::show(window(), DeprecatedString::formatted("Reading \"{}\" failed.", filename), "Error"sv, GUI::MessageBox::Type::Error); + if (new_mesh.is_error()) { + GUI::MessageBox::show(window(), DeprecatedString::formatted("Reading \"{}\" failed: {}", filename, new_mesh.release_error()), "Error"sv, GUI::MessageBox::Type::Error); return false; } @@ -358,7 +358,7 @@ bool GLContextWidget::load_file(Core::File& file) dbgln("3DFileViewer: Couldn't load texture for {}", filename); } - m_mesh = new_mesh; + m_mesh = new_mesh.release_value(); dbgln("3DFileViewer: mesh has {} triangles.", m_mesh->triangle_count()); return true;