#pragma once // 官方格式插件公共工具(VDE 微内核 P1;纯 C ABI,不链接内核)。 // // 只允许 include vde/plugin/vde_plugin_api.h + C/C++ 标准库;所有内核几何能力 // 通过 VdePluginHost 服务表获得(P1 的 body_from_mesh 用于把三角网格变成实体)。 #include #include #include #include #include #include #include #include "vde/plugin/vde_plugin_api.h" namespace vde_plugin_common { inline int fail(const VdePluginHost* host, int code, const char* message) { if (host && host->set_error) host->set_error(message); return code; } /// 宿主是否提供 P1 body_from_mesh(按 struct_size 判字段存在,兼容旧宿主 ABI 1.0) inline bool host_has_body_from_mesh(const VdePluginHost* host) { if (!host) return false; const std::size_t required = offsetof(VdePluginHost, body_from_mesh) + sizeof(host->body_from_mesh); return host->struct_size >= required && host->body_from_mesh != nullptr; } /// 所有官方插件共用的宿主表检查(内存/诊断/ABI) inline int validate_common_host(const VdePluginHost* host) { if (!host) return VDE_PLUGIN_ERR_INVALID_ARG; if (host->abi_major != VDE_PLUGIN_ABI_MAJOR) return fail(host, VDE_PLUGIN_ERR_ABI, "official plugin: 宿主 ABI major 不兼容"); if (!host->alloc || !host->free || !host->set_error || !host->body_destroy) return fail(host, VDE_PLUGIN_ERR_UNSUPPORTED, "official plugin: 宿主缺少 alloc/free/set_error/body_destroy"); return VDE_PLUGIN_OK; } /// 读插件读取路径所需宿主能力(含 P1 body_from_mesh) inline int validate_read_host(const VdePluginHost* host) { const int rc = validate_common_host(host); if (rc != VDE_PLUGIN_OK) return rc; if (!host_has_body_from_mesh(host)) return fail(host, VDE_PLUGIN_ERR_UNSUPPORTED, "official plugin: 宿主未提供 body_from_mesh(需要插件 ABI minor >= 1)"); return VDE_PLUGIN_OK; } /// 写插件写出路径所需宿主能力(三角化 + bbox + 缓冲区释放) inline int validate_write_host(const VdePluginHost* host) { const int rc = validate_common_host(host); if (rc != VDE_PLUGIN_OK) return rc; if (!host->body_tessellate || !host->free_buffer) return fail(host, VDE_PLUGIN_ERR_UNSUPPORTED, "official plugin: 宿主缺少 body_tessellate/free_buffer"); return VDE_PLUGIN_OK; } /// 整文件读取(二进制;失败返回 false) inline bool read_file_bytes(const char* path, std::string* out) { if (!path || !out) return false; out->clear(); std::FILE* file = std::fopen(path, "rb"); if (!file) return false; if (std::fseek(file, 0, SEEK_END) != 0) { std::fclose(file); return false; } const long length = std::ftell(file); std::rewind(file); if (length < 0) { std::fclose(file); return false; } out->resize(static_cast(length)); const std::size_t read_bytes = out->empty() ? 0u : std::fread(&(*out)[0], 1, out->size(), file); std::fclose(file); if (read_bytes != out->size()) { out->clear(); return false; } return true; } /// 整文件写出 inline bool write_file_bytes(const char* path, const void* data, std::size_t size) { if (!path || (!data && size > 0)) return false; std::FILE* file = std::fopen(path, "wb"); if (!file) return false; const std::size_t written = size == 0 ? 0u : std::fwrite(data, 1, size, file); std::fclose(file); return written == size; } /// 三角网格累加器(positions 3×N、indices 3×M,0-based) struct MeshAccumulator { std::vector positions; std::vector indices; [[nodiscard]] std::size_t vertex_count() const { return positions.size() / 3; } [[nodiscard]] std::size_t triangle_count() const { return indices.size() / 3; } [[nodiscard]] bool empty() const { return indices.empty(); } uint32_t add_vertex(double x, double y, double z) { const uint32_t index = static_cast(vertex_count()); positions.push_back(x); positions.push_back(y); positions.push_back(z); return index; } bool add_triangle(uint32_t a, uint32_t b, uint32_t c) { if (a == b || b == c || a == c) return false; const uint32_t count = static_cast(vertex_count()); if (a >= count || b >= count || c >= count) return false; indices.push_back(a); indices.push_back(b); indices.push_back(c); return true; } }; /// 把已解析网格经宿主 body_from_mesh 变成实体 + host->alloc 句柄数组交付。 /// 成功时 *out_bodies 由宿主 alloc 分配(调用方/宿主负责 free),实体所有权移交宿主。 inline int publish_single_body(const VdePluginHost* host, const MeshAccumulator& mesh, VdeBody** out_bodies, std::size_t* out_count, const char* format_name) { if (out_bodies) *out_bodies = nullptr; if (out_count) *out_count = 0; if (!out_bodies || !out_count) return fail(host, VDE_PLUGIN_ERR_INVALID_ARG, "official plugin: bodies/count 输出指针为空"); if (mesh.empty()) return fail(host, VDE_PLUGIN_ERR_PARSE, "official plugin: 文件不含可用三角面"); if (!host_has_body_from_mesh(host)) return fail(host, VDE_PLUGIN_ERR_UNSUPPORTED, "official plugin: 宿主未提供 body_from_mesh(需要插件 ABI minor >= 1)"); VdeBody body = host->body_from_mesh(mesh.positions.data(), mesh.vertex_count(), mesh.indices.data(), mesh.triangle_count()); if (!body) return fail(host, VDE_PLUGIN_ERR_INTERNAL, "official plugin: 宿主由网格创建实体失败(见 host 错误信息)"); VdeBody* array = static_cast(host->alloc(sizeof(VdeBody))); if (!array) { host->body_destroy(body); return fail(host, VDE_PLUGIN_ERR_BUFFER, "official plugin: 句柄数组分配失败"); } array[0] = body; *out_bodies = array; *out_count = 1; (void)format_name; return VDE_PLUGIN_OK; } // ── 轻量文本工具 ────────────────────────────────────────── inline bool is_space_char(char c) { return c == ' ' || c == '\t' || c == '\r' || c == '\n' || c == '\f' || c == '\v'; } /// 跳过空白后返回首个 token 起始位置(到 end 或空白为止),找不到返回 data+size inline const char* skip_spaces(const char* begin, const char* end) { const char* p = begin; while (p < end && is_space_char(*p)) ++p; return p; } inline std::string lower_copy(const std::string& text) { std::string out = text; for (char& c : out) { if (c >= 'A' && c <= 'Z') c = static_cast(c - 'A' + 'a'); } return out; } /// 首 token 是否等于 word(大小写不敏感) inline bool first_token_is(const char* data, std::size_t size, const char* word) { if (!data || !word) return false; const char* begin = data; const char* end = data + size; const char* p = skip_spaces(begin, end); const char* token_end = p; while (token_end < end && !is_space_char(*token_end)) ++token_end; const std::string token = lower_copy(std::string(p, token_end)); return token == lower_copy(std::string(word)); } } // namespace vde_plugin_common