Files

692 lines
27 KiB
C++

// VDE official 3MF format plugin (P4 dogfood).
// Pure C ABI: reads ZIP/OPC 3MF mesh, builds host bodies; writes stored-ZIP 3MF.
// Does not link the VDE kernel. Deflate decode uses Mark Adler's puff (zlib license).
#include "../common/format_plugin_common.h"
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <map>
#include <string>
#include <vector>
extern "C" {
#include "puff.h"
}
namespace {
using vde_plugin_common::fail;
using vde_plugin_common::read_file_bytes;
using vde_plugin_common::validate_read_host;
using vde_plugin_common::validate_write_host;
using vde_plugin_common::write_file_bytes;
constexpr std::size_t kMaxEntries = 4096;
constexpr std::size_t kMaxModelBytes = 512u * 1024u * 1024u;
std::uint16_t rd16(const unsigned char* p) {
return static_cast<std::uint16_t>(p[0] | (p[1] << 8));
}
std::uint32_t rd32(const unsigned char* p) {
return static_cast<std::uint32_t>(p[0]) | (static_cast<std::uint32_t>(p[1]) << 8) |
(static_cast<std::uint32_t>(p[2]) << 16) |
(static_cast<std::uint32_t>(p[3]) << 24);
}
void put16(std::string* out, std::uint16_t v) {
out->push_back(static_cast<char>(v & 0xff));
out->push_back(static_cast<char>((v >> 8) & 0xff));
}
void put32(std::string* out, std::uint32_t v) {
out->push_back(static_cast<char>(v & 0xff));
out->push_back(static_cast<char>((v >> 8) & 0xff));
out->push_back(static_cast<char>((v >> 16) & 0xff));
out->push_back(static_cast<char>((v >> 24) & 0xff));
}
bool inflate_entry(const unsigned char* src, std::size_t src_size, std::size_t expected,
std::string* out) {
out->assign(expected, '\0');
unsigned long destlen = static_cast<unsigned long>(expected);
unsigned long sourcelen = static_cast<unsigned long>(src_size);
const int rc = puff(reinterpret_cast<unsigned char*>(&(*out)[0]), &destlen, src, &sourcelen);
if (rc != 0 || destlen != expected) {
out->clear();
return false;
}
return true;
}
/// Read one stored/deflated ZIP entry by name from an OPC package.
bool read_zip_entry(const std::string& zip, const std::string& wanted, std::string* out) {
out->clear();
if (zip.size() < 22) return false;
std::size_t eocd = std::string::npos;
const std::size_t begin = zip.size() > 65557 ? zip.size() - 65557 : 0;
for (std::size_t i = zip.size() - 22 + 1; i-- > begin;) {
const unsigned char* p = reinterpret_cast<const unsigned char*>(zip.data() + i);
if (rd32(p) == 0x06054b50u) {
eocd = i;
break;
}
}
if (eocd == std::string::npos) return false;
const auto* e = reinterpret_cast<const unsigned char*>(zip.data() + eocd);
const std::uint16_t total = rd16(e + 10);
const std::uint32_t cd_off = rd32(e + 16);
if (total > kMaxEntries || cd_off >= zip.size()) return false;
std::size_t pos = cd_off;
for (std::uint16_t n = 0; n < total; ++n) {
if (pos + 46 > zip.size()) return false;
const auto* c = reinterpret_cast<const unsigned char*>(zip.data() + pos);
if (rd32(c) != 0x02014b50u) return false;
const std::uint16_t method = rd16(c + 10);
const std::uint32_t csize = rd32(c + 20);
const std::uint32_t usize = rd32(c + 24);
const std::uint16_t name_len = rd16(c + 28);
const std::uint16_t extra_len = rd16(c + 30);
const std::uint16_t comment_len = rd16(c + 32);
const std::uint32_t local_off = rd32(c + 42);
if (pos + 46 + name_len > zip.size()) return false;
const std::string name(zip.data() + pos + 46, name_len);
if (name == wanted) {
if (usize > kMaxModelBytes || csize == 0 || local_off + 30 > zip.size())
return false;
const auto* l = reinterpret_cast<const unsigned char*>(zip.data() + local_off);
if (rd32(l) != 0x04034b50u) return false;
const std::uint16_t lname = rd16(l + 26);
const std::uint16_t lextra = rd16(l + 28);
const std::size_t data_off = local_off + 30u + lname + lextra;
if (data_off + csize > zip.size()) return false;
const auto* data = reinterpret_cast<const unsigned char*>(zip.data() + data_off);
if (method == 0) {
out->assign(reinterpret_cast<const char*>(data), csize);
return true;
}
if (method == 8) return inflate_entry(data, csize, usize, out);
return false;
}
pos += 46u + name_len + extra_len + comment_len;
}
return false;
}
bool attr_double(const std::string& text, std::size_t start, const char* key, double* value) {
const std::string needle = std::string(key) + "=";
std::size_t p = text.find(needle, start);
if (p == std::string::npos) return false;
p += needle.size();
while (p < text.size() && std::isspace(static_cast<unsigned char>(text[p]))) ++p;
if (p >= text.size()) return false;
const char quote = text[p];
if (quote == '"' || quote == '\'') ++p;
char* end = nullptr;
const double v = std::strtod(text.c_str() + p, &end);
if (end == text.c_str() + p) return false;
*value = v;
return true;
}
bool attr_int(const std::string& text, std::size_t start, const char* key, int* value) {
const std::string needle = std::string(key) + "=";
std::size_t p = text.find(needle, start);
if (p == std::string::npos) return false;
p += needle.size();
while (p < text.size() && std::isspace(static_cast<unsigned char>(text[p]))) ++p;
if (p >= text.size()) return false;
const char quote = text[p];
if (quote == '"' || quote == '\'') ++p;
char* end = nullptr;
const long v = std::strtol(text.c_str() + p, &end, 10);
if (end == text.c_str() + p) return false;
*value = static_cast<int>(v);
return true;
}
struct Mesh {
std::vector<double> positions;
std::vector<std::uint32_t> indices;
bool empty() const { return indices.empty(); }
};
bool parse_mesh(const std::string& body, Mesh* mesh) {
std::size_t p = 0;
while ((p = body.find("<vertex", p)) != std::string::npos) {
const std::size_t end = body.find('>', p);
if (end == std::string::npos) break;
double x = 0.0, y = 0.0, z = 0.0;
if (!attr_double(body, p, "x", &x) || !attr_double(body, p, "y", &y) ||
!attr_double(body, p, "z", &z))
return false;
mesh->positions.push_back(x);
mesh->positions.push_back(y);
mesh->positions.push_back(z);
p = end + 1;
}
p = 0;
while ((p = body.find("<triangle", p)) != std::string::npos) {
const std::size_t end = body.find('>', p);
if (end == std::string::npos) break;
int a = -1, b = -1, c = -1;
if (!attr_int(body, p, "v1", &a) || !attr_int(body, p, "v2", &b) ||
!attr_int(body, p, "v3", &c))
return false;
const std::uint32_t nv = static_cast<std::uint32_t>(mesh->positions.size() / 3);
if (a < 0 || b < 0 || c < 0 || static_cast<std::uint32_t>(a) >= nv ||
static_cast<std::uint32_t>(b) >= nv || static_cast<std::uint32_t>(c) >= nv)
return false;
if (a == b || b == c || a == c) {
p = end + 1;
continue;
}
mesh->indices.push_back(static_cast<std::uint32_t>(a));
mesh->indices.push_back(static_cast<std::uint32_t>(b));
mesh->indices.push_back(static_cast<std::uint32_t>(c));
p = end + 1;
}
return true;
}
struct Transform {
// 3MF 4x3 row-vector matrix: [x y z 1] * M.
double m[12] = {1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0};
void apply(double x, double y, double z, double* ox, double* oy, double* oz) const {
*ox = x * m[0] + y * m[3] + z * m[6] + m[9];
*oy = x * m[1] + y * m[4] + z * m[7] + m[10];
*oz = x * m[2] + y * m[5] + z * m[8] + m[11];
}
};
struct Component {
int object_id = 0;
Transform transform;
};
struct Object {
int id = 0;
bool has_mesh = false;
Mesh mesh;
std::vector<Component> components;
};
struct BuildItem {
int object_id = 0;
Transform transform;
};
struct Model {
double unit_scale = 1.0;
std::map<int, Object> objects;
std::vector<BuildItem> build;
};
bool parse_transform(const std::string& text, std::size_t start, Transform* out) {
*out = Transform{};
std::size_t p = text.find("transform=", start);
if (p == std::string::npos) return true;
p += 10;
while (p < text.size() && std::isspace(static_cast<unsigned char>(text[p]))) ++p;
if (p >= text.size()) return false;
const char quote = text[p];
if (quote != '"' && quote != '\'') return false;
++p;
for (int i = 0; i < 12; ++i) {
while (p < text.size() && std::isspace(static_cast<unsigned char>(text[p]))) ++p;
char* end = nullptr;
const double v = std::strtod(text.c_str() + p, &end);
if (end == text.c_str() + p) return false;
out->m[i] = v;
p = static_cast<std::size_t>(end - text.c_str());
}
return true;
}
double parse_unit_scale(const std::string& text, std::size_t model_tag) {
std::size_t p = text.find("unit=", model_tag);
if (p == std::string::npos) return 1.0;
p += 5;
while (p < text.size() && std::isspace(static_cast<unsigned char>(text[p]))) ++p;
if (p >= text.size()) return 1.0;
const char quote = text[p];
if (quote != '"' && quote != '\'') return 1.0;
const std::size_t begin = ++p;
const std::size_t end = text.find(quote, begin);
if (end == std::string::npos) return 1.0;
const std::string unit = text.substr(begin, end - begin);
if (unit == "micron") return 0.001;
if (unit == "millimeter") return 1.0;
if (unit == "centimeter") return 10.0;
if (unit == "inch") return 25.4;
if (unit == "foot") return 304.8;
if (unit == "meter") return 1000.0;
return 1.0;
}
Transform compose_parent_child(const Transform& parent, const Transform& child) {
// Row-vector convention: point * child * parent.
Transform out;
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 3; ++j) {
out.m[i * 3 + j] = child.m[i * 3 + 0] * parent.m[0 * 3 + j] +
child.m[i * 3 + 1] * parent.m[1 * 3 + j] +
child.m[i * 3 + 2] * parent.m[2 * 3 + j];
}
}
for (int j = 0; j < 3; ++j) {
out.m[9 + j] = child.m[9] * parent.m[0 * 3 + j] +
child.m[10] * parent.m[1 * 3 + j] +
child.m[11] * parent.m[2 * 3 + j] + parent.m[9 + j];
}
return out;
}
bool parse_3mf_model(const std::string& xml, std::vector<Mesh>* meshes) {
meshes->clear();
const std::size_t model_tag = xml.find("<model");
if (model_tag == std::string::npos) return false;
Model model;
model.unit_scale = parse_unit_scale(xml, model_tag);
std::size_t p = 0;
while ((p = xml.find("<object", p)) != std::string::npos) {
const std::size_t open_end = xml.find('>', p);
if (open_end == std::string::npos) break;
const std::size_t close = xml.find("</object>", open_end);
if (close == std::string::npos) break;
int id = 0;
if (!attr_int(xml, p, "id", &id) || id <= 0) return false;
Object object;
object.id = id;
const std::string body = xml.substr(open_end + 1, close - open_end - 1);
if (body.find("<mesh") != std::string::npos) {
if (!parse_mesh(body, &object.mesh)) return false;
object.has_mesh = !object.mesh.empty();
}
std::size_t cp = 0;
while ((cp = body.find("<component ", cp)) != std::string::npos) {
const std::size_t ce = body.find('>', cp);
if (ce == std::string::npos) break;
Component component;
if (!attr_int(body, cp, "objectid", &component.object_id) ||
component.object_id <= 0)
return false;
if (!parse_transform(body, cp, &component.transform)) return false;
object.components.push_back(component);
cp = ce + 1;
}
model.objects[id] = std::move(object);
p = close + 9;
}
if (model.objects.empty()) return false;
const std::size_t build_open = xml.find("<build");
if (build_open != std::string::npos) {
const std::size_t build_close = xml.find("</build>", build_open);
if (build_close == std::string::npos) return false;
const std::string build = xml.substr(build_open, build_close - build_open);
std::size_t ip = 0;
while ((ip = build.find("<item ", ip)) != std::string::npos) {
const std::size_t ie = build.find('>', ip);
if (ie == std::string::npos) break;
BuildItem item;
if (!attr_int(build, ip, "objectid", &item.object_id) || item.object_id <= 0)
return false;
if (!parse_transform(build, ip, &item.transform)) return false;
model.build.push_back(item);
ip = ie + 1;
}
}
if (model.build.empty()) {
for (const auto& [id, object] : model.objects) {
if (object.has_mesh) model.build.push_back(BuildItem{id, Transform{}});
}
}
if (model.build.empty()) return false;
std::vector<int> stack;
std::function<bool(int, const Transform&)> append = [&](int id, const Transform& parent) {
if (std::find(stack.begin(), stack.end(), id) != stack.end()) return false;
if (stack.size() > 32) return false;
const auto found = model.objects.find(id);
if (found == model.objects.end()) return false;
const Object& object = found->second;
stack.push_back(id);
if (object.has_mesh && !object.mesh.empty()) {
Mesh mesh;
mesh.positions.reserve(object.mesh.positions.size());
for (std::size_t i = 0; i < object.mesh.positions.size(); i += 3) {
double x = 0, y = 0, z = 0;
parent.apply(object.mesh.positions[i], object.mesh.positions[i + 1],
object.mesh.positions[i + 2], &x, &y, &z);
mesh.positions.push_back(x * model.unit_scale);
mesh.positions.push_back(y * model.unit_scale);
mesh.positions.push_back(z * model.unit_scale);
}
mesh.indices = object.mesh.indices;
if (!mesh.indices.empty()) meshes->push_back(std::move(mesh));
}
for (const Component& component : object.components) {
const Transform combined = compose_parent_child(parent, component.transform);
if (!append(component.object_id, combined)) return false;
}
stack.pop_back();
return true;
};
for (const BuildItem& item : model.build) {
if (!append(item.object_id, item.transform)) return false;
}
return !meshes->empty();
}
int publish(const VdePluginHost* host, const std::vector<Mesh>& meshes, VdeBody** bodies,
std::size_t* count) {
if (bodies) *bodies = nullptr;
if (count) *count = 0;
if (!bodies || !count) return fail(host, VDE_PLUGIN_ERR_INVALID_ARG, "3mf: null output");
if (meshes.empty()) return fail(host, VDE_PLUGIN_ERR_PARSE, "3mf: no mesh triangles");
std::vector<VdeBody> made;
made.reserve(meshes.size());
for (const auto& mesh : meshes) {
if (mesh.empty()) continue;
VdeBody body = host->body_from_mesh(
mesh.positions.data(), mesh.positions.size() / 3, mesh.indices.data(),
mesh.indices.size() / 3);
if (!body) {
for (VdeBody old : made) host->body_destroy(old);
return fail(host, VDE_PLUGIN_ERR_INTERNAL, "3mf: body_from_mesh failed");
}
made.push_back(body);
}
if (made.empty()) return fail(host, VDE_PLUGIN_ERR_PARSE, "3mf: no usable mesh");
VdeBody* array = static_cast<VdeBody*>(host->alloc(sizeof(VdeBody) * made.size()));
if (!array) {
for (VdeBody old : made) host->body_destroy(old);
return fail(host, VDE_PLUGIN_ERR_BUFFER, "3mf: body array alloc failed");
}
for (std::size_t i = 0; i < made.size(); ++i) array[i] = made[i];
*bodies = array;
*count = made.size();
return VDE_PLUGIN_OK;
}
int read_memory_impl(const VdePluginHost* host, const void* data, std::size_t size,
VdeBody** bodies, std::size_t* count) {
if (bodies) *bodies = nullptr;
if (count) *count = 0;
const int rc = validate_read_host(host);
if (rc != VDE_PLUGIN_OK) return rc;
if (!data || size == 0) return fail(host, VDE_PLUGIN_ERR_INVALID_ARG, "3mf: empty input");
const std::string zip(static_cast<const char*>(data), size);
std::string model;
if (!read_zip_entry(zip, "3D/3dmodel.model", &model))
return fail(host, VDE_PLUGIN_ERR_PARSE, "3mf: 3D/3dmodel.model missing or unsupported ZIP");
std::vector<Mesh> meshes;
if (!parse_3mf_model(model, &meshes))
return fail(host, VDE_PLUGIN_ERR_PARSE, "3mf: malformed mesh XML");
return publish(host, meshes, bodies, count);
}
int read_file(const VdePluginHost* host, const char* path, VdeBody** bodies, std::size_t* count) {
if (!path) return fail(host, VDE_PLUGIN_ERR_INVALID_ARG, "3mf: null path");
std::string bytes;
if (!read_file_bytes(path, &bytes))
return fail(host, VDE_PLUGIN_ERR_IO, "3mf: cannot read file");
return read_memory_impl(host, bytes.data(), bytes.size(), bodies, count);
}
int read_memory(const VdePluginHost* host, const void* data, std::size_t size, VdeBody** bodies,
std::size_t* count) {
return read_memory_impl(host, data, size, bodies, count);
}
// ── write ────────────────────────────────────────────────────────────────
std::uint32_t crc32_update(std::uint32_t crc, const unsigned char* data, std::size_t n) {
static std::uint32_t table[256];
static bool init = false;
if (!init) {
for (std::uint32_t i = 0; i < 256; ++i) {
std::uint32_t c = i;
for (int k = 0; k < 8; ++k) c = (c & 1) ? (0xedb88320u ^ (c >> 1)) : (c >> 1);
table[i] = c;
}
init = true;
}
for (std::size_t i = 0; i < n; ++i) crc = table[(crc ^ data[i]) & 0xff] ^ (crc >> 8);
return crc;
}
std::string zip_store(const std::vector<std::pair<std::string, std::string>>& entries) {
std::string out;
std::vector<std::uint32_t> local_offsets;
for (const auto& [name, data] : entries) {
local_offsets.push_back(static_cast<std::uint32_t>(out.size()));
put32(&out, 0x04034b50u);
put16(&out, 20);
put16(&out, 0);
put16(&out, 0);
put16(&out, 0);
put16(&out, 0);
const std::uint32_t crc = crc32_update(0xffffffffu,
reinterpret_cast<const unsigned char*>(data.data()), data.size()) ^ 0xffffffffu;
put32(&out, crc);
put32(&out, static_cast<std::uint32_t>(data.size()));
put32(&out, static_cast<std::uint32_t>(data.size()));
put16(&out, static_cast<std::uint16_t>(name.size()));
put16(&out, 0);
out += name;
out += data;
}
const std::uint32_t cd_off = static_cast<std::uint32_t>(out.size());
for (std::size_t i = 0; i < entries.size(); ++i) {
const auto& [name, data] = entries[i];
put32(&out, 0x02014b50u);
put16(&out, 20);
put16(&out, 20);
put16(&out, 0);
put16(&out, 0);
put16(&out, 0);
put16(&out, 0);
const std::uint32_t crc = crc32_update(0xffffffffu,
reinterpret_cast<const unsigned char*>(data.data()), data.size()) ^ 0xffffffffu;
put32(&out, crc);
put32(&out, static_cast<std::uint32_t>(data.size()));
put32(&out, static_cast<std::uint32_t>(data.size()));
put16(&out, static_cast<std::uint16_t>(name.size()));
put16(&out, 0);
put16(&out, 0);
put16(&out, 0);
put16(&out, 0);
put32(&out, 0);
put32(&out, local_offsets[i]);
out += name;
}
const std::uint32_t cd_size = static_cast<std::uint32_t>(out.size()) - cd_off;
put32(&out, 0x06054b50u);
put16(&out, 0);
put16(&out, 0);
put16(&out, static_cast<std::uint16_t>(entries.size()));
put16(&out, static_cast<std::uint16_t>(entries.size()));
put32(&out, cd_size);
put32(&out, cd_off);
put16(&out, 0);
return out;
}
bool build_model_xml(const VdePluginHost* host, const VdeBody* bodies, std::size_t count,
std::string* xml, std::string* error) {
std::string out =
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
"<model unit=\"millimeter\" xml:lang=\"en-US\" "
"xmlns=\"http://schemas.microsoft.com/3dmanufacturing/core/2015/02\">"
"<resources>";
for (std::size_t b = 0; b < count; ++b) {
VdeMeshData mesh{};
mesh.struct_size = static_cast<std::uint32_t>(sizeof(VdeMeshData));
const int rc = host->body_tessellate(bodies[b], 0.1, &mesh);
if (rc != VDE_PLUGIN_OK || mesh.num_vertices == 0 || mesh.num_triangles == 0) {
if (mesh.positions) host->free_buffer(mesh.positions);
if (mesh.indices) host->free_buffer(mesh.indices);
if (mesh.normals) host->free_buffer(mesh.normals);
*error = "3mf: body_tessellate produced no mesh";
return false;
}
if (mesh.num_vertices > 100000000ull || mesh.num_triangles > 100000000ull) {
host->free_buffer(mesh.positions);
host->free_buffer(mesh.indices);
if (mesh.normals) host->free_buffer(mesh.normals);
*error = "3mf: mesh too large";
return false;
}
char head[160];
std::snprintf(head, sizeof(head),
"<object id=\"%zu\" type=\"model\"><mesh><vertices>", b + 1);
out += head;
char point[160];
for (std::size_t i = 0; i < mesh.num_vertices; ++i) {
std::snprintf(point, sizeof(point), "<vertex x=\"%.9g\" y=\"%.9g\" z=\"%.9g\"/>",
mesh.positions[3 * i], mesh.positions[3 * i + 1],
mesh.positions[3 * i + 2]);
out += point;
}
out += "</vertices><triangles>";
char tri[128];
for (std::size_t i = 0; i < mesh.num_triangles; ++i) {
std::snprintf(tri, sizeof(tri), "<triangle v1=\"%u\" v2=\"%u\" v3=\"%u\"/>",
mesh.indices[3 * i], mesh.indices[3 * i + 1], mesh.indices[3 * i + 2]);
out += tri;
}
out += "</triangles></mesh></object>";
host->free_buffer(mesh.positions);
host->free_buffer(mesh.indices);
if (mesh.normals) host->free_buffer(mesh.normals);
}
out += "</resources><build>";
for (std::size_t b = 0; b < count; ++b) {
char item[64];
std::snprintf(item, sizeof(item), "<item objectid=\"%zu\"/>", b + 1);
out += item;
}
out += "</build></model>";
*xml = std::move(out);
return true;
}
int write_impl(const VdePluginHost* host, const VdeBody* bodies, std::size_t count,
std::string* bytes) {
const int rc = validate_write_host(host);
if (rc != VDE_PLUGIN_OK) return rc;
if (!bodies || count == 0) return fail(host, VDE_PLUGIN_ERR_INVALID_ARG, "3mf: no bodies");
std::string model;
std::string error;
if (!build_model_xml(host, bodies, count, &model, &error))
return fail(host, VDE_PLUGIN_ERR_INTERNAL, error.c_str());
const std::string content_types =
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
"<Types xmlns=\"http://schemas.openxmlformats.org/package/2006/content-types\">"
"<Default Extension=\"rels\" ContentType=\"application/vnd.openxmlformats-package.relationships+xml\"/>"
"<Default Extension=\"model\" ContentType=\"application/vnd.ms-package.3dmanufacturing-3dmodel+xml\"/>"
"</Types>";
const std::string rels =
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
"<Relationships xmlns=\"http://schemas.openxmlformats.org/package/2006/relationships\">"
"<Relationship Target=\"/3D/3dmodel.model\" Id=\"rel0\" "
"Type=\"http://schemas.microsoft.com/3dmanufacturing/2013/01/3dmodel\"/>"
"</Relationships>";
*bytes = zip_store({{"[Content_Types].xml", content_types},
{"_rels/.rels", rels},
{"3D/3dmodel.model", model}});
return VDE_PLUGIN_OK;
}
int write_file(const VdePluginHost* host, const char* path, const VdeBody* bodies,
std::size_t count) {
if (!path) return fail(host, VDE_PLUGIN_ERR_INVALID_ARG, "3mf: null path");
std::string bytes;
const int rc = write_impl(host, bodies, count, &bytes);
if (rc != VDE_PLUGIN_OK) return rc;
if (!write_file_bytes(path, bytes.data(), bytes.size()))
return fail(host, VDE_PLUGIN_ERR_IO, "3mf: write file failed");
return VDE_PLUGIN_OK;
}
int write_memory(const VdePluginHost* host, const VdeBody* bodies, std::size_t count,
VdeBuffer* out) {
if (out) {
out->data = nullptr;
out->size = 0;
}
if (!out) return fail(host, VDE_PLUGIN_ERR_INVALID_ARG, "3mf: null output buffer");
std::string bytes;
const int rc = write_impl(host, bodies, count, &bytes);
if (rc != VDE_PLUGIN_OK) return rc;
void* buffer = host->alloc(bytes.size());
if (!buffer) return fail(host, VDE_PLUGIN_ERR_BUFFER, "3mf: output alloc failed");
std::memcpy(buffer, bytes.data(), bytes.size());
out->data = buffer;
out->size = bytes.size();
return VDE_PLUGIN_OK;
}
int probe(const void* head, std::size_t size) {
if (!head || size < 4) return 0;
const auto* p = static_cast<const unsigned char*>(head);
return (p[0] == 'P' && p[1] == 'K' && p[2] == 3 && p[3] == 4) ? 60 : 0;
}
const char* describe() {
return "VDE official 3MF format (OPC ZIP + core mesh; read/write; no kernel link)";
}
const VdeFormatPlugin kFormat = [] {
VdeFormatPlugin format{};
format.struct_size = static_cast<std::uint32_t>(sizeof(VdeFormatPlugin));
format.format_id = "3mf";
format.extensions = ".3mf";
format.probe = &probe;
format.read_file = &read_file;
format.read_memory = &read_memory;
format.write_file = &write_file;
format.write_memory = &write_memory;
format.describe = &describe;
return format;
}();
} // namespace
extern "C" {
VDE_PLUGIN_EXPORT int vde_plugin_query(uint32_t host_abi_major, VdePluginInfo* info) {
if (!info || info->struct_size < sizeof(VdePluginInfo)) return VDE_PLUGIN_ERR_ABI;
if (host_abi_major != VDE_PLUGIN_ABI_MAJOR) return VDE_PLUGIN_ERR_ABI;
info->id = "com.vde.official.3mf";
info->name = "VDE Official 3MF Format";
info->version = "1.0.0";
info->kind = VDE_PLUGIN_KIND_FORMAT;
info->abi_major = VDE_PLUGIN_ABI_MAJOR;
info->abi_minor = VDE_PLUGIN_ABI_MINOR;
info->capabilities = "format:3mf.read;format:3mf.write";
info->license = "Apache-2.0 AND Zlib";
return VDE_PLUGIN_OK;
}
VDE_PLUGIN_EXPORT int vde_plugin_init(const VdePluginHost* host, VdePluginRegistration* reg) {
if (!reg || reg->struct_size < sizeof(VdePluginRegistration)) return VDE_PLUGIN_ERR_ABI;
const int rc = vde_plugin_common::validate_common_host(host);
if (rc != VDE_PLUGIN_OK) return rc;
reg->kind = VDE_PLUGIN_KIND_FORMAT;
reg->format = &kFormat;
reg->generic = nullptr;
return VDE_PLUGIN_OK;
}
VDE_PLUGIN_EXPORT void vde_plugin_shutdown() {}
} // extern "C"