Skip to content

Commit

Permalink
Update dv_plugin to conform to plugin changes
Browse files Browse the repository at this point in the history
  • Loading branch information
dvsku committed Jan 27, 2024
1 parent f6c41d9 commit e79005e
Show file tree
Hide file tree
Showing 2 changed files with 152 additions and 14 deletions.
25 changes: 16 additions & 9 deletions include/devue_core/plugins/dv_plugin.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#pragma once

#include "dv_file_type.hpp"
#include "dv_plugin_importer.hpp"
#include "misc/dv_file_type.hpp"
#include "devue_plugin_interface.hpp"
#include "devue_plugin_model.hpp"
#include "devue_plugin_texture.hpp"

#include <windows.h>
#include <string>
Expand All @@ -16,9 +18,8 @@ namespace devue::core {
std::string website = "";
std::string version = "";

plugins::dv_plugin_importer::plugin_type type = plugins::dv_plugin_importer::plugin_type::undefined;

std::vector<plugins::dv_file_type> supported_file_types;
std::vector<dv_file_type> supported_model_types;
std::vector<dv_file_type> supported_texture_types;

public:
dv_plugin() = default;
Expand All @@ -27,14 +28,20 @@ namespace devue::core {

virtual ~dv_plugin() = default;

dv_plugin& operator=(const dv_plugin&) = delete;
dv_plugin& operator=(dv_plugin&&) = default;
dv_plugin& operator=(const dv_plugin&) = delete;
dv_plugin& operator=(dv_plugin&&) = default;

public:
virtual void prepare();

devue::plugins::devue_plugin_model import_model(const std::string& filepath);
devue::plugins::devue_plugin_texture import_texture(const std::string& filepath);

void cleanup();

protected:
HMODULE m_handle = nullptr;
plugins::dv_plugin_importer* m_importer = nullptr;
HMODULE m_handle = nullptr;
devue::plugins::devue_plugin_interface* m_iface = nullptr;

friend class dv_sys_plugin;
};
Expand Down
141 changes: 136 additions & 5 deletions src/devue_core/src/plugins/dv_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
#include "exceptions/dv_exception.hpp"
#include "json/include/json.hpp"

using namespace devue;
using namespace devue::core;
using namespace devue::plugins;

void dv_plugin::prepare() {
dv_plugin_importer::serialized serialized = m_importer->get_plugin_info();
plugins::devue_plugin_interface::serialized serialized = m_iface->get_plugin_info();

if (!serialized)
throw dv_exception("");
Expand All @@ -25,19 +25,150 @@ void dv_plugin::prepare() {
if (json.contains("version") && json["version"].is_string())
version = json["version"];

if (json.contains("supported_types") && json["supported_types"].is_array()) {
for (auto& json_type : json["supported_types"]) {
if (json.contains("model_types") && json["model_types"].is_array()) {
for (auto& json_type : json["model_types"]) {
if (!json_type.contains("name") || !json_type["name"].is_string()) continue;
if (!json_type.contains("extensions") || !json_type["extensions"].is_string()) continue;

dv_file_type file_type;
file_type.name = json_type["name"];
file_type.extensions = json_type["extensions"];

supported_file_types.push_back(file_type);
supported_model_types.push_back(file_type);
}
}

if (json.contains("texture_types") && json["texture_types"].is_array()) {
for (auto& json_type : json["texture_types"]) {
if (!json_type.contains("name") || !json_type["name"].is_string()) continue;
if (!json_type.contains("extensions") || !json_type["extensions"].is_string()) continue;

dv_file_type file_type;
file_type.name = json_type["name"];
file_type.extensions = json_type["extensions"];

supported_texture_types.push_back(file_type);
}
}

if (name.empty())
throw dv_exception("");
}

plugins::devue_plugin_model dv_plugin::import_model(const std::string& filepath) {
plugins::devue_plugin_model model;
plugins::devue_plugin_interface::serialized serialized;

serialized = m_iface->import_model(filepath.c_str());

if (!serialized)
throw;

nlohmann::json json = nlohmann::json::from_cbor(serialized.data, serialized.data + serialized.size);

if (!json.contains("meshes") || json["meshes"].empty())
return plugins::devue_plugin_model();

for (auto& json_mesh : json["meshes"]) {
plugins::devue_plugin_mesh mesh;

if (json_mesh.contains("name") && json_mesh["name"].is_string())
mesh.name = json_mesh["name"];

if (json_mesh.contains("material") && json_mesh["material"].is_object()) {
auto& json_material = json_mesh["material"];

if (json_material.contains("name") && json_material["name"].is_string())
mesh.material.name = json_material["name"];

if (json_material.contains("diffuse_texture") && json_material["diffuse_texture"].is_string())
mesh.material.diffuse_texture = json_material["diffuse_texture"];
}

if (json_mesh.contains("vertices") && json_mesh["vertices"].is_array()) {
auto& json_vertices = json_mesh["vertices"];

for (auto& json_vertex : json_vertices) {
plugins::devue_plugin_vertex vertex{};

if (json_vertex.contains("position")) {
auto& json_position = json_vertex["position"];

if (json_position.contains("x") && json_position["x"].is_number())
vertex.position.x = json_position["x"];

if (json_position.contains("y") && json_position["y"].is_number())
vertex.position.y = json_position["y"];

if (json_position.contains("z") && json_position["z"].is_number())
vertex.position.z = json_position["z"];
}

if (json_vertex.contains("normal")) {
auto& json_normal = json_vertex["normal"];

if (json_normal.contains("x") && json_normal["x"].is_number())
vertex.normal.x = json_normal["x"];

if (json_normal.contains("y") && json_normal["y"].is_number())
vertex.normal.y = json_normal["y"];

if (json_normal.contains("z") && json_normal["z"].is_number())
vertex.normal.z = json_normal["z"];
}

if (json_vertex.contains("uv")) {
auto& json_uv = json_vertex["uv"];

if (json_uv.contains("x") && json_uv["x"].is_number())
vertex.uv.x = json_uv["x"];

if (json_uv.contains("y") && json_uv["y"].is_number())
vertex.uv.y = json_uv["y"];
}

mesh.vertices.push_back(vertex);
}

if (json_mesh.contains("indices") && json_mesh["indices"].is_array()) {
auto& json_indices = json_mesh["indices"];

for (auto& json_index : json_indices) {
if (json_index.is_number())
mesh.indices.push_back(json_index);
}
}
}

model.meshes.push_back(mesh);
}

return model;
}

plugins::devue_plugin_texture dv_plugin::import_texture(const std::string& filepath) {
plugins::devue_plugin_texture texture;
plugins::devue_plugin_interface::serialized serialized;

serialized = m_iface->import_texture(filepath.c_str());

if (!serialized)
throw;

nlohmann::json json = nlohmann::json::from_cbor(serialized.data, serialized.data + serialized.size);

if (json.contains("width") && json["width"].is_number())
texture.width = json["width"];

if (json.contains("height") && json["height"].is_number())
texture.height = json["height"];

if (json.contains("data") && json["data"].is_binary())
texture.data = std::move(json["data"].get_binary());

return texture;
}

void dv_plugin::cleanup() {
m_iface->cleanup();
}

0 comments on commit e79005e

Please sign in to comment.