3DFileViewer: Port MeshLoader and its child to the new Core::File

As it was using the `lines()` method of `Core::DeprecatedFile`, this
patch also introduce the usage of `BufferedFile` to take advantage of
its API: `can_read_line()` and `read_line()`.
This commit is contained in:
Lucas CHOLLET 2023-01-14 18:56:37 -05:00 committed by Sam Atkins
parent 83da3c5c3e
commit b4cea1c72e
Notes: sideshowbarker 2024-07-17 03:18:29 +09:00
4 changed files with 14 additions and 6 deletions

View File

@ -18,5 +18,5 @@ public:
MeshLoader() = default;
virtual ~MeshLoader() = default;
virtual ErrorOr<NonnullRefPtr<Mesh>> load(Core::DeprecatedFile& file) = 0;
virtual ErrorOr<NonnullRefPtr<Mesh>> load(String const& filename, NonnullOwnPtr<Core::File> file) = 0;
};

View File

@ -8,7 +8,9 @@
#include "WavefrontOBJLoader.h"
#include <AK/FixedArray.h>
#include <AK/String.h>
#include <LibCore/DeprecatedFile.h>
#include <LibCore/File.h>
#include <stdlib.h>
static inline GLuint get_index_value(StringView& representation)
@ -16,17 +18,22 @@ static inline GLuint get_index_value(StringView& representation)
return representation.to_uint().value_or(1) - 1;
}
ErrorOr<NonnullRefPtr<Mesh>> WavefrontOBJLoader::load(Core::DeprecatedFile& file)
ErrorOr<NonnullRefPtr<Mesh>> WavefrontOBJLoader::load(String const& filename, NonnullOwnPtr<Core::File> file)
{
auto buffered_file = TRY(Core::BufferedFile::create(move(file)));
Vector<Vertex> vertices;
Vector<Vertex> normals;
Vector<TexCoord> tex_coords;
Vector<Triangle> triangles;
dbgln("Wavefront: Loading {}...", file.name());
dbgln("Wavefront: Loading {}...", filename);
// Start reading file line by line
for (auto object_line : file.lines()) {
auto buffer = TRY(ByteBuffer::create_uninitialized(PAGE_SIZE));
while (TRY(buffered_file->can_read_line())) {
auto object_line = TRY(buffered_file->read_line(buffer));
// Ignore file comments
if (object_line.starts_with('#'))
continue;

View File

@ -18,5 +18,5 @@ public:
WavefrontOBJLoader() = default;
~WavefrontOBJLoader() override = default;
ErrorOr<NonnullRefPtr<Mesh>> load(Core::DeprecatedFile& file) override;
ErrorOr<NonnullRefPtr<Mesh>> load(String const& filename, NonnullOwnPtr<Core::File> file) override;
};

View File

@ -318,7 +318,8 @@ bool GLContextWidget::load_file(Core::DeprecatedFile& file)
return false;
}
auto new_mesh = m_mesh_loader->load(file);
auto new_mesh = m_mesh_loader->load(String::from_deprecated_string(file.filename()).release_value_but_fixme_should_propagate_errors(),
Core::File::adopt_fd(file.leak_fd(), Core::File::OpenMode::Read).release_value_but_fixme_should_propagate_errors());
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;