- P0-2/P0-3 插件管理器实现(src/plugin/plugin_manager.cpp):严格语义化版本、vde-plugin.json 解析与 ABI/平台 entry/库存在性校验、CapabilityRegistry(冲突记录)、发现/加载/卸载/失败隔离/格式分派/ 线程安全;tests/plugin/test_plugin_manager.cpp 29 用例全绿; - P0-4 宿主几何服务表(src/plugin/host_services_geometry.cpp,编入 vde_io):alloc/log/set_error、 box/cylinder/sphere、VDE-TEXT/二进制序列化往返、query/volume/area/bbox/tessellate/destroy/free_buffer; - P0-5 示例插件 plugins/sample_vbox(.vbox 文本格式,纯 C ABI 不链接内核)+ 端到端测试 12 项全绿; - P0-6 CMake:vde::kernel 分层聚合、VDE_BUILD_PLUGIN/SAMPLE_PLUGINS/INSTALL_SDK/TOOLS 开关、 kernel SDK install/export(find_package(vde-kernel CONFIG),12 库/231 头/consumer 验证)、 -DVDE_BUILD_PLUGIN=OFF kernel-only 可构建; - tools/vde_plugin_tool(--list/--inspect/--abi); - docs/design/microkernel-plugin-architecture.md(P0..P3 计划)+ docs/guides/plugin-development.md + docs/guides/architecture.md 微内核章节。 验证:全量 ctest 7249/7249 PASS(0 failed、22 skipped、406.31s);插件测试 41 项(29+12)全绿; kernel SDK consumer find_package + 编译运行 PASS;-DVDE_BUILD_PLUGIN=OFF / -DVDE_BUILD_SAMPLE_PLUGINS=OFF 可构建。
334 lines
13 KiB
C++
334 lines
13 KiB
C++
// 示例格式插件 .vbox(P0-5):演示纯 C ABI 插件的最小完整实现。
|
||
//
|
||
// 文件格式(UTF-8 文本;允许空白行与 '#' 注释):
|
||
//
|
||
// vbox 1
|
||
// <w> <h> <d>
|
||
// [<w> <h> <d> ...] ; 可选:多实体,每行一个盒体尺寸
|
||
//
|
||
// 读:通过宿主服务 host->body_create_box 创建盒体(中心在原点);
|
||
// 写:通过 host->body_bbox 取包围盒尺寸输出文本。
|
||
// 插件不链接内核;所有内存均由宿主服务表分配/释放。
|
||
#include <cctype>
|
||
#include <cstdio>
|
||
#include <cstdlib>
|
||
#include <cstring>
|
||
#include <string>
|
||
#include <vector>
|
||
|
||
#include "vde/plugin/vde_plugin_api.h"
|
||
|
||
namespace {
|
||
|
||
constexpr long kMaxFileSize = 1L << 20; // 1 MiB 上限
|
||
constexpr size_t kMaxBodies = 1024;
|
||
|
||
int 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->struct_size < sizeof(VdePluginHost))
|
||
return fail(host, VDE_PLUGIN_ERR_ABI, "vbox: 宿主服务表结构体过小(ABI 不兼容)");
|
||
if (host->abi_major != VDE_PLUGIN_ABI_MAJOR || host->abi_minor < VDE_PLUGIN_ABI_MINOR)
|
||
return fail(host, VDE_PLUGIN_ERR_ABI, "vbox: 宿主 ABI 版本不兼容");
|
||
if (!host->alloc || !host->free || !host->set_error || !host->body_create_box ||
|
||
!host->body_bbox || !host->body_destroy)
|
||
return fail(host, VDE_PLUGIN_ERR_UNSUPPORTED,
|
||
"vbox: 宿主缺少必需的几何服务(create_box/bbox/destroy/alloc)");
|
||
return VDE_PLUGIN_OK;
|
||
}
|
||
|
||
/** 空白与 '#' 注释感知的轻量 tokenizer(不复制整份文件) */
|
||
class Tokenizer {
|
||
public:
|
||
Tokenizer(const char* begin, const char* end) : p_(begin), end_(end) {}
|
||
|
||
/** 跳过空白/注释;返回是否还有内容 */
|
||
bool skip_space() {
|
||
while (p_ < end_) {
|
||
const unsigned char c = static_cast<unsigned char>(*p_);
|
||
if (std::isspace(c)) {
|
||
++p_;
|
||
continue;
|
||
}
|
||
if (c == '#') {
|
||
while (p_ < end_ && *p_ != '\n') ++p_;
|
||
continue;
|
||
}
|
||
return true;
|
||
}
|
||
return false;
|
||
}
|
||
|
||
bool read_word(std::string* out) {
|
||
if (!out || !skip_space()) return false;
|
||
const char* start = p_;
|
||
while (p_ < end_) {
|
||
const unsigned char c = static_cast<unsigned char>(*p_);
|
||
if (std::isspace(c) || c == '#') break;
|
||
++p_;
|
||
}
|
||
out->assign(start, static_cast<size_t>(p_ - start));
|
||
return !out->empty();
|
||
}
|
||
|
||
bool read_number(double* out) {
|
||
if (!out || !skip_space()) return false;
|
||
char buffer[64];
|
||
size_t count = 0;
|
||
while (p_ < end_) {
|
||
const unsigned char c = static_cast<unsigned char>(*p_);
|
||
if (std::isspace(c) || c == '#') break;
|
||
if (count + 1 >= sizeof(buffer)) return false; // token 过长 → 视为非法
|
||
buffer[count++] = static_cast<char>(c);
|
||
++p_;
|
||
}
|
||
if (count == 0) return false;
|
||
buffer[count] = '\0';
|
||
char* parse_end = nullptr;
|
||
const double value = std::strtod(buffer, &parse_end);
|
||
if (parse_end != buffer + count) return false; // 整个 token 必须是数字
|
||
if (!(value == value) || value > 1e300 || value < -1e300) return false;
|
||
*out = value;
|
||
return true;
|
||
}
|
||
|
||
private:
|
||
const char* p_ = nullptr;
|
||
const char* end_ = nullptr;
|
||
};
|
||
|
||
int vbox_probe(const void* head, size_t head_size) {
|
||
if (!head || head_size == 0) return 0;
|
||
const char* p = static_cast<const char*>(head);
|
||
size_t i = 0;
|
||
while (i < head_size) {
|
||
const unsigned char c = static_cast<unsigned char>(p[i]);
|
||
if (std::isspace(c)) {
|
||
++i;
|
||
continue;
|
||
}
|
||
if (c == '#') {
|
||
while (i < head_size && p[i] != '\n') ++i;
|
||
continue;
|
||
}
|
||
break;
|
||
}
|
||
if (head_size - i < 4) return 0;
|
||
if (std::memcmp(p + i, "vbox", 4) != 0) return 0;
|
||
return 100; // magic 完全匹配
|
||
}
|
||
|
||
int read_impl(const VdePluginHost* host, const void* data, size_t size,
|
||
VdeBody** bodies, size_t* count);
|
||
|
||
int vbox_read_memory(const VdePluginHost* host, const void* data, size_t size,
|
||
VdeBody** bodies, size_t* count) {
|
||
if (bodies) *bodies = nullptr;
|
||
if (count) *count = 0;
|
||
if (!bodies || !count)
|
||
return fail(host, VDE_PLUGIN_ERR_INVALID_ARG, "vbox.read: bodies/count 为空");
|
||
const int host_rc = validate_host(host);
|
||
if (host_rc != VDE_PLUGIN_OK) return host_rc;
|
||
if (!data || size == 0)
|
||
return fail(host, VDE_PLUGIN_ERR_INVALID_ARG, "vbox.read: 数据为空");
|
||
if (size > static_cast<size_t>(kMaxFileSize))
|
||
return fail(host, VDE_PLUGIN_ERR_BUFFER, "vbox.read: 文件过大(>1 MiB)");
|
||
return read_impl(host, data, size, bodies, count);
|
||
}
|
||
|
||
int vbox_read_file(const VdePluginHost* host, const char* path,
|
||
VdeBody** bodies, size_t* count) {
|
||
if (bodies) *bodies = nullptr;
|
||
if (count) *count = 0;
|
||
if (!bodies || !count)
|
||
return fail(host, VDE_PLUGIN_ERR_INVALID_ARG, "vbox.read: bodies/count 为空");
|
||
const int host_rc = validate_host(host);
|
||
if (host_rc != VDE_PLUGIN_OK) return host_rc;
|
||
if (!path || !*path)
|
||
return fail(host, VDE_PLUGIN_ERR_INVALID_ARG, "vbox.read: 路径为空");
|
||
std::FILE* fp = std::fopen(path, "rb");
|
||
if (!fp) return fail(host, VDE_PLUGIN_ERR_IO, "vbox.read: 无法打开输入文件");
|
||
if (std::fseek(fp, 0, SEEK_END) != 0) {
|
||
std::fclose(fp);
|
||
return fail(host, VDE_PLUGIN_ERR_IO, "vbox.read: 无法定位文件尾");
|
||
}
|
||
const long length = std::ftell(fp);
|
||
std::rewind(fp);
|
||
if (length <= 0 || length > kMaxFileSize) {
|
||
std::fclose(fp);
|
||
return fail(host, VDE_PLUGIN_ERR_IO, "vbox.read: 文件为空或过大");
|
||
}
|
||
std::vector<char> buffer(static_cast<size_t>(length));
|
||
const size_t read_bytes = std::fread(buffer.data(), 1, buffer.size(), fp);
|
||
std::fclose(fp);
|
||
if (read_bytes != buffer.size())
|
||
return fail(host, VDE_PLUGIN_ERR_IO, "vbox.read: 文件读取不完整");
|
||
return read_impl(host, buffer.data(), buffer.size(), bodies, count);
|
||
}
|
||
|
||
int read_impl(const VdePluginHost* host, const void* data, size_t size,
|
||
VdeBody** bodies, size_t* count) {
|
||
Tokenizer tokenizer(static_cast<const char*>(data),
|
||
static_cast<const char*>(data) + size);
|
||
std::string magic;
|
||
if (!tokenizer.read_word(&magic) || magic != "vbox")
|
||
return fail(host, VDE_PLUGIN_ERR_PARSE,
|
||
"vbox.read: 文件头不是 'vbox'(格式不匹配)");
|
||
double version = 0.0;
|
||
if (!tokenizer.read_number(&version) || version != 1.0)
|
||
return fail(host, VDE_PLUGIN_ERR_UNSUPPORTED,
|
||
"vbox.read: 仅支持 'vbox 1' 版本");
|
||
|
||
std::vector<std::vector<double>> dimensions;
|
||
while (tokenizer.skip_space()) {
|
||
double w = 0.0, h = 0.0, d = 0.0;
|
||
if (!tokenizer.read_number(&w) || !tokenizer.read_number(&h) ||
|
||
!tokenizer.read_number(&d))
|
||
return fail(host, VDE_PLUGIN_ERR_PARSE,
|
||
"vbox.read: 盒体尺寸行不完整(需要 w h d 三个数)");
|
||
if (!(w > 0.0) || !(h > 0.0) || !(d > 0.0))
|
||
return fail(host, VDE_PLUGIN_ERR_PARSE, "vbox.read: 盒体尺寸必须为正");
|
||
dimensions.push_back({w, h, d});
|
||
if (dimensions.size() > kMaxBodies)
|
||
return fail(host, VDE_PLUGIN_ERR_BUFFER, "vbox.read: 实体数量超过上限");
|
||
}
|
||
if (dimensions.empty())
|
||
return fail(host, VDE_PLUGIN_ERR_PARSE, "vbox.read: 没有任何盒体尺寸");
|
||
|
||
auto* array = static_cast<VdeBody*>(
|
||
host->alloc(sizeof(VdeBody) * dimensions.size()));
|
||
if (!array)
|
||
return fail(host, VDE_PLUGIN_ERR_BUFFER, "vbox.read: 句柄数组分配失败");
|
||
size_t made = 0;
|
||
for (; made < dimensions.size(); ++made) {
|
||
VdeBody body = host->body_create_box(dimensions[made][0],
|
||
dimensions[made][1],
|
||
dimensions[made][2]);
|
||
if (!body) {
|
||
for (size_t i = 0; i < made; ++i) host->body_destroy(array[i]);
|
||
host->free(array);
|
||
return fail(host, VDE_PLUGIN_ERR_INTERNAL,
|
||
"vbox.read: 宿主创建盒体失败(见 host 错误信息)");
|
||
}
|
||
array[made] = body;
|
||
}
|
||
*bodies = array;
|
||
*count = dimensions.size();
|
||
return VDE_PLUGIN_OK;
|
||
}
|
||
|
||
int vbox_write_memory(const VdePluginHost* host, const VdeBody* bodies,
|
||
size_t count, VdeBuffer* out) {
|
||
if (out) {
|
||
out->data = nullptr;
|
||
out->size = 0;
|
||
}
|
||
if (!out) return fail(host, VDE_PLUGIN_ERR_INVALID_ARG, "vbox.write: out 为空");
|
||
const int host_rc = validate_host(host);
|
||
if (host_rc != VDE_PLUGIN_OK) return host_rc;
|
||
if (!bodies || count == 0)
|
||
return fail(host, VDE_PLUGIN_ERR_INVALID_ARG, "vbox.write: 没有实体可写");
|
||
std::string text = "vbox 1\n";
|
||
char line[128];
|
||
for (size_t i = 0; i < count; ++i) {
|
||
if (!bodies[i])
|
||
return fail(host, VDE_PLUGIN_ERR_INVALID_ARG, "vbox.write: 实体句柄为空");
|
||
double lo[3] = {0.0, 0.0, 0.0};
|
||
double hi[3] = {0.0, 0.0, 0.0};
|
||
if (host->body_bbox(bodies[i], lo, hi) != VDE_PLUGIN_OK)
|
||
return fail(host, VDE_PLUGIN_ERR_INTERNAL, "vbox.write: 取包围盒失败");
|
||
const double w = hi[0] - lo[0];
|
||
const double h = hi[1] - lo[1];
|
||
const double d = hi[2] - lo[2];
|
||
if (!(w > 0.0) || !(h > 0.0) || !(d > 0.0))
|
||
return fail(host, VDE_PLUGIN_ERR_INTERNAL,
|
||
"vbox.write: 实体包围盒退化(尺寸非正)");
|
||
std::snprintf(line, sizeof(line), "%.10g %.10g %.10g\n", w, h, d);
|
||
text += line;
|
||
}
|
||
char* buffer = static_cast<char*>(host->alloc(text.size() + 1));
|
||
if (!buffer)
|
||
return fail(host, VDE_PLUGIN_ERR_BUFFER, "vbox.write: 输出缓冲分配失败");
|
||
std::memcpy(buffer, text.data(), text.size());
|
||
buffer[text.size()] = '\0';
|
||
out->data = buffer;
|
||
out->size = text.size();
|
||
return VDE_PLUGIN_OK;
|
||
}
|
||
|
||
int vbox_write_file(const VdePluginHost* host, const char* path,
|
||
const VdeBody* bodies, size_t count) {
|
||
if (!path || !*path)
|
||
return fail(host, VDE_PLUGIN_ERR_INVALID_ARG, "vbox.write: 路径为空");
|
||
VdeBuffer buffer{nullptr, 0};
|
||
const int rc = vbox_write_memory(host, bodies, count, &buffer);
|
||
if (rc != VDE_PLUGIN_OK) return rc;
|
||
std::FILE* fp = std::fopen(path, "wb");
|
||
if (!fp) {
|
||
host->free(buffer.data);
|
||
return fail(host, VDE_PLUGIN_ERR_IO, "vbox.write: 无法打开输出文件");
|
||
}
|
||
const size_t written = std::fwrite(buffer.data, 1, buffer.size, fp);
|
||
std::fclose(fp);
|
||
host->free(buffer.data);
|
||
if (written != buffer.size)
|
||
return fail(host, VDE_PLUGIN_ERR_IO, "vbox.write: 写入不完整");
|
||
return VDE_PLUGIN_OK;
|
||
}
|
||
|
||
const char* vbox_describe() { return "VDE sample VBox format (vbox 1: w h d lines)"; }
|
||
|
||
const VdeFormatPlugin kFormat = [] {
|
||
VdeFormatPlugin format{};
|
||
format.struct_size = static_cast<uint32_t>(sizeof(VdeFormatPlugin));
|
||
format.format_id = "vbox";
|
||
format.extensions = ".vbox";
|
||
format.probe = &vbox_probe;
|
||
format.read_file = &vbox_read_file;
|
||
format.read_memory = &vbox_read_memory;
|
||
format.write_file = &vbox_write_file;
|
||
format.write_memory = &vbox_write_memory;
|
||
format.describe = &vbox_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.sample.vbox";
|
||
info->name = "VDE Sample VBox Format";
|
||
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:vbox.read;format:vbox.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;
|
||
return VDE_PLUGIN_OK;
|
||
}
|
||
|
||
VDE_PLUGIN_EXPORT void vde_plugin_shutdown() {}
|
||
|
||
} // extern "C"
|