Files
Z3D/include/assets/file_parsers/mtl_parser.hpp
2025-04-01 21:51:56 +02:00

104 lines
2.0 KiB
C++

#pragma once
#include <filesystem>
#include <system_error>
#include "assets/data_stores.hpp"
#include "assets/path_id_lookups.hpp"
#include "assets/data/material_data.hpp"
#include "assets/data/material_library_data.hpp"
#include "assets/data_stores/material_store.hpp"
#include "assets/data_stores/material_library_store.hpp"
#include "util/result.hpp"
namespace assets
{
namespace mtl_parser_error
{
enum class codes {
ok = 0,
cannot_open_file,
// TODO these are incomplete
cannot_open_texture,
malformed_ambient_color,
malformed_diffuse_color,
malformed_specular_color,
malformed_specular_exponent,
malformed_dissolve,
unknown_line_begin
};
} // namespace mtl_loader_error
struct mtl_parser
{
static constexpr auto name = std::string_view("mtl");
using data_type = material_library_data;
using store_type = material_library_store;
using lookup_type = material_library_id_lookup;
[[nodiscard]] std::error_code prefetch(
path_id_lookups& lookups
);
[[nodiscard]] std::error_code load(
path_id_lookups& lookups,
data_stores& stores,
bool pedantic = false
);
protected:
class prefetcher_context
{
public:
prefetcher_context(
path_id_lookups& lookups
);
void operator()(lookup_type::const_pointer entry) noexcept;
protected:
[[nodiscard]] std::optional<texture_id> fetch_texture_id(
const std::filesystem::path& mtl_dir,
std::string_view filename,
std::string_view texture_type_name
);
private:
path_id_lookups* m_lookups;
std::vector<char> m_buffer{};
};
class parser_context
{
public:
parser_context(
path_id_lookups& lookups,
data_stores& stores
);
void operator()(lookup_type::const_pointer entry) noexcept;
protected:
void reset();
[[nodiscard]] std::optional<texture_id> fetch_texture_id(
const std::filesystem::path& mtl_dir,
std::string_view filename,
std::string_view texture_type_name
);
private:
path_id_lookups* m_lookups;
data_stores* m_stores;
data_type m_buffer{};
};
private:
std::vector<lookup_type::const_pointer> m_path_buffer;
};
}