Files
ViewDesignEngine/plugins/templates/format_plugin/format_plugin.cpp
T
hm 4ef08ec011
Build & Test / build-and-test (push) Canceled after 0s
VDE Build & Test / build-and-test (push) Canceled after 0s
Build & Test / python-bindings (push) Canceled after 0s
feat: VDE_INSTALL/嵌入式消费完善 + 插件 SDK 模板包与 tool --check(1.0.71)
### P3-11 嵌入式消费/安装完善(VDE-1251 响应)
- option(VDE_INSTALL ON):OFF 统一关闭 VDE install/export 规则(kernel SDK/插件 SDK/CLI),宿主自身规则保留;
  未用 CMAKE_SKIP_INSTALL_RULES,采用 install 前置变量屏蔽;VDE_INSTALL_SDK 关系文档化;
- CI 新增 embedding-consumer job(ubuntu,模拟宿主普通 _vd_eigen3 + add_subdirectory(VDE),
  VDE_INSTALL=ON 断言 vde-kernel 包、OFF 断言无 VDE 包/内核库但宿主 exe 安装;防 VDE-1250/1251 回归);
- module-manual:vde_cae 显式链接说明(1.0.46 起)+ 嵌入消费方集成章节;CHANGELOG 1.0.70 补注;
- 验证:默认 build 0 error + 相关 ctest 274/274;嵌入 ON/OFF configure/build/run/install 全通过(EMBED_CONSUMER_PASS);

### P3-12 插件 SDK 模板包 + vde_plugin_tool --check
- plugins/templates/:format/CAE/CAM/GCS 四类最小模板(源码+manifest ABI1.3+独立 CMake+README),不加入默认构建,
  只依赖纯 C ABI;4 模板独立 configure/build 0 error;
- vde_plugin_tool --check:清单/entry DLL/query 与清单一致性/ABI 兼容/artifacts 路径+SHA-256/kind↔能力 warning,退出码 0/2/3;
  4/4 PASS、负例 RC=3、旧 ABI 1.1 RC=0、既有插件 PASS;默认构建零引用;
- 文档 plugin-development §14、roadmap P3-12 进展;

### 工程
- 版本 1.0.71(CMake/version.h/test_version/README/docs index/module-manual);
- 全量构建 0 error(345 targets);全量 ctest 7499 注册、7404/7404 PASS(0 failed,25 skipped,177.98s)。
2026-10-29 00:08:46 +08:00

208 lines
8.7 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// VDE 插件 SDK 模板:format(文件格式读写骨架;纯 C ABI,不链接 VDE 内核)
//
// 演示:
// - 最小 VdeFormatPlugin 注册(format_id/extensions/probe/read/write/describe);
// - 通过 VdePluginHost 宿主服务创建/查询/序列化实体(body_create_box/body_bbox/
// body_serialize/free_buffer),不直接链接内核;
// - 跨 ABI 内存所有权:输出数组/缓冲区用 host->alloc,宿主用 host->free_buffer/free;
// - 失败路径:负错误码 + host->set_error(不跨 C ABI 抛异常)。
//
// 本模板的 .example 格式(演示用,可替换为真实格式):
// VDE-EXAMPLE 1
// box <sx> <sy> <sz>
// 读取时解析 box 尺寸并调用 host->body_create_box 创建实体;写入时输出同样两行
// (尺寸取实体 bbox),即一个最小的“可读可写”格式插件。
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include "vde/plugin/vde_plugin_api.h"
namespace {
constexpr const char* kFormatId = "example";
constexpr const char* kMagic = "VDE-EXAMPLE";
int fmt_fail(const VdePluginHost* host, int code, const char* message) {
if (host && host->set_error) host->set_error(message);
return code;
}
/// 校验宿主服务表:本模板需要创建/查询/序列化实体与缓冲区管理。
int validate_host(const VdePluginHost* host) {
if (!host) return VDE_PLUGIN_ERR_INVALID_ARG;
if (host->abi_major != VDE_PLUGIN_ABI_MAJOR)
return fmt_fail(host, VDE_PLUGIN_ERR_ABI, "format template: host ABI major mismatch");
if (!host->alloc || !host->free || !host->set_error || !host->free_buffer)
return fmt_fail(host, VDE_PLUGIN_ERR_UNSUPPORTED,
"format template: host lacks alloc/free/set_error/free_buffer");
if (!host->body_create_box || !host->body_bbox || !host->body_serialize)
return fmt_fail(host, VDE_PLUGIN_ERR_UNSUPPORTED,
"format template: host lacks body_create_box/body_bbox/body_serialize");
return VDE_PLUGIN_OK;
}
int probe(const void* head, size_t head_size) {
if (!head || head_size < std::strlen(kMagic)) return 0;
return std::memcmp(head, kMagic, std::strlen(kMagic)) == 0 ? 100 : 0;
}
int read_memory(const VdePluginHost* host, const void* data, size_t size,
VdeBody** bodies, size_t* count) {
if (bodies) *bodies = nullptr;
if (count) *count = 0;
const int host_rc = validate_host(host);
if (host_rc != VDE_PLUGIN_OK) return host_rc;
if (!data || size == 0) return fmt_fail(host, VDE_PLUGIN_ERR_INVALID_ARG, "format template: empty input");
if (!bodies || !count) return fmt_fail(host, VDE_PLUGIN_ERR_INVALID_ARG, "format template: null output");
const std::string text(static_cast<const char*>(data), size);
if (text.compare(0, std::strlen(kMagic), kMagic) != 0)
return fmt_fail(host, VDE_PLUGIN_ERR_PARSE, "format template: missing VDE-EXAMPLE header");
double sx = 0.0, sy = 0.0, sz = 0.0;
if (std::sscanf(text.c_str(), "VDE-EXAMPLE 1 box %lf %lf %lf", &sx, &sy, &sz) != 3 ||
!(sx > 0.0) || !(sy > 0.0) || !(sz > 0.0)) {
return fmt_fail(host, VDE_PLUGIN_ERR_PARSE,
"format template: expected 'VDE-EXAMPLE 1 box <sx> <sy> <sz>'");
}
VdeBody body = host->body_create_box(sx, sy, sz);
if (!body) return fmt_fail(host, VDE_PLUGIN_ERR_INTERNAL, "format template: body_create_box failed");
VdeBody* array = static_cast<VdeBody*>(host->alloc(sizeof(VdeBody)));
if (!array) {
host->body_destroy(body);
return fmt_fail(host, VDE_PLUGIN_ERR_BUFFER, "format template: body array alloc failed");
}
array[0] = body;
*bodies = array;
*count = 1;
return VDE_PLUGIN_OK;
}
int write_memory(const VdePluginHost* host, const VdeBody* bodies, size_t count,
VdeBuffer* out) {
if (out) {
out->data = nullptr;
out->size = 0;
}
const int host_rc = validate_host(host);
if (host_rc != VDE_PLUGIN_OK) return host_rc;
if (!out) return fmt_fail(host, VDE_PLUGIN_ERR_INVALID_ARG, "format template: null output buffer");
if (!bodies || count == 0)
return fmt_fail(host, VDE_PLUGIN_ERR_INVALID_ARG, "format template: no bodies to write");
double bmin[3] = {0.0, 0.0, 0.0};
double bmax[3] = {0.0, 0.0, 0.0};
int rc = host->body_bbox(bodies[0], bmin, bmax);
if (rc != VDE_PLUGIN_OK) return fmt_fail(host, VDE_PLUGIN_ERR_INTERNAL, "format template: body_bbox failed");
char header[256];
const int written = std::snprintf(header, sizeof(header),
"VDE-EXAMPLE 1\nbox %.10g %.10g %.10g\n",
bmax[0] - bmin[0], bmax[1] - bmin[1],
bmax[2] - bmin[2]);
if (written <= 0) return fmt_fail(host, VDE_PLUGIN_ERR_INTERNAL, "format template: header format failed");
const std::size_t total = static_cast<std::size_t>(written);
void* buffer = host->alloc(total + 1);
if (!buffer) return fmt_fail(host, VDE_PLUGIN_ERR_BUFFER, "format template: output alloc failed");
std::memcpy(buffer, header, total);
static_cast<char*>(buffer)[total] = '\0';
out->data = buffer;
out->size = total;
return VDE_PLUGIN_OK;
}
int read_file(const VdePluginHost* host, const char* path, VdeBody** bodies,
size_t* count) {
if (!path) return fmt_fail(host, VDE_PLUGIN_ERR_INVALID_ARG, "format template: null path");
std::FILE* file = std::fopen(path, "rb");
if (!file) return fmt_fail(host, VDE_PLUGIN_ERR_IO, "format template: cannot open file");
std::fseek(file, 0, SEEK_END);
const long length = std::ftell(file);
std::fseek(file, 0, SEEK_SET);
if (length <= 0) {
std::fclose(file);
return fmt_fail(host, VDE_PLUGIN_ERR_PARSE, "format template: empty file");
}
std::string text(static_cast<std::size_t>(length), '\0');
const std::size_t read = std::fread(text.data(), 1, text.size(), file);
std::fclose(file);
if (read != text.size()) return fmt_fail(host, VDE_PLUGIN_ERR_IO, "format template: short read");
return read_memory(host, text.data(), text.size(), bodies, count);
}
int write_file(const VdePluginHost* host, const char* path, const VdeBody* bodies,
size_t count) {
if (!path) return fmt_fail(host, VDE_PLUGIN_ERR_INVALID_ARG, "format template: null path");
VdeBuffer buffer{};
const int rc = write_memory(host, bodies, count, &buffer);
if (rc != VDE_PLUGIN_OK) return rc;
std::FILE* file = std::fopen(path, "wb");
if (!file) {
if (buffer.data && host->free_buffer) host->free_buffer(buffer.data);
return fmt_fail(host, VDE_PLUGIN_ERR_IO, "format template: cannot create file");
}
const std::size_t written = std::fwrite(buffer.data, 1, buffer.size, file);
std::fclose(file);
if (host->free_buffer) host->free_buffer(buffer.data);
if (written != buffer.size) return fmt_fail(host, VDE_PLUGIN_ERR_IO, "format template: short write");
return VDE_PLUGIN_OK;
}
const char* describe() {
return "VDE format plugin template (.example: VDE-EXAMPLE 1 / box sx sy sz)";
}
const VdeFormatPlugin kFormat = [] {
VdeFormatPlugin plugin{};
plugin.struct_size = static_cast<uint32_t>(sizeof(VdeFormatPlugin));
plugin.format_id = kFormatId;
plugin.extensions = ".example";
plugin.probe = &probe;
plugin.read_file = &read_file;
plugin.read_memory = &read_memory;
plugin.write_file = &write_file;
plugin.write_memory = &write_memory;
plugin.describe = &describe;
return plugin;
}();
} // 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.example.vde.format";
info->name = "VDE Format Plugin Template";
info->version = "0.1.0";
info->kind = VDE_PLUGIN_KIND_FORMAT;
info->abi_major = VDE_PLUGIN_ABI_MAJOR;
info->abi_minor = VDE_PLUGIN_ABI_MINOR;
info->capabilities = "format:example.read;format:example.write";
info->license = "Apache-2.0";
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 host_rc = validate_host(host);
if (host_rc != VDE_PLUGIN_OK) return host_rc;
reg->kind = VDE_PLUGIN_KIND_FORMAT;
reg->format = &kFormat;
reg->generic = nullptr;
reg->cae_solver = nullptr;
reg->cam_strategy = nullptr;
reg->gcs_solver = nullptr;
return VDE_PLUGIN_OK;
}
VDE_PLUGIN_EXPORT void vde_plugin_shutdown() {}
} // extern "C"