diff options
author | Maciej <sppmacd@pm.me> | 2022-12-04 14:52:41 +0100 |
---|---|---|
committer | Linus Groh <mail@linusgroh.de> | 2022-12-06 17:24:45 +0000 |
commit | 6e4f886999c5ab90c38cd2d6d8c90354b2d6713b (patch) | |
tree | f181bf215a628beff49ab4fa0c02457120ba81ac | |
parent | a3e82eaad3b808d96d5d83abab94ecb8124a49ef (diff) | |
download | serenity-6e4f886999c5ab90c38cd2d6d8c90354b2d6713b.zip |
3DFileViewer: Properly propagate errors from WavefrontOBJLoader
Fixes 3 FIXMEs.
4 files changed, 14 insertions, 19 deletions
diff --git a/Userland/Applications/3DFileViewer/MeshLoader.h b/Userland/Applications/3DFileViewer/MeshLoader.h index 2ad0bcba1d..0c33507962 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<Mesh> load(Core::File& file) = 0; + virtual ErrorOr<NonnullRefPtr<Mesh>> load(Core::File& file) = 0; }; diff --git a/Userland/Applications/3DFileViewer/WavefrontOBJLoader.cpp b/Userland/Applications/3DFileViewer/WavefrontOBJLoader.cpp index baed60913a..ebe71058f5 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<Mesh> WavefrontOBJLoader::load(Core::File& file) +ErrorOr<NonnullRefPtr<Mesh>> WavefrontOBJLoader::load(Core::File& file) { Vector<Vertex> vertices; Vector<Vertex> normals; @@ -34,8 +34,7 @@ RefPtr<Mesh> 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<GLfloat>(atof(DeprecatedString(tex_coord_line.at(1)).characters())), @@ -47,8 +46,7 @@ RefPtr<Mesh> 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<GLfloat>(atof(DeprecatedString(normal_line.at(1)).characters())), @@ -62,8 +60,7 @@ RefPtr<Mesh> 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<GLfloat>(atof(DeprecatedString(vertex_line.at(1)).characters())), @@ -78,13 +75,12 @@ RefPtr<Mesh> 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<GLuint>::must_create_but_fixme_should_propagate_errors(number_of_vertices); - auto tex_coord_indices = FixedArray<GLuint>::must_create_but_fixme_should_propagate_errors(number_of_vertices); - auto normal_indices = FixedArray<GLuint>::must_create_but_fixme_should_propagate_errors(number_of_vertices); + auto vertex_indices = TRY(FixedArray<GLuint>::try_create(number_of_vertices)); + auto tex_coord_indices = TRY(FixedArray<GLuint>::try_create(number_of_vertices)); + auto normal_indices = TRY(FixedArray<GLuint>::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<Mesh> 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 67a260b46c..77e197e43d 100644 --- a/Userland/Applications/3DFileViewer/WavefrontOBJLoader.h +++ b/Userland/Applications/3DFileViewer/WavefrontOBJLoader.h @@ -18,5 +18,5 @@ public: WavefrontOBJLoader() = default; ~WavefrontOBJLoader() override = default; - RefPtr<Mesh> load(Core::File& file) override; + ErrorOr<NonnullRefPtr<Mesh>> load(Core::File& file) override; }; diff --git a/Userland/Applications/3DFileViewer/main.cpp b/Userland/Applications/3DFileViewer/main.cpp index c5de4c6373..70451f07fd 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; |