1. Overview Purpose: Allow a host application (e.g., editor, IDE, media player, game engine) to open external files via a plugin system. The plugin registers a custom file open handler to intercept or extend the application’s native file opening behavior.
// Called after host opens file void (*on_after_file_open)(const char* path, void* context, int host_result); plugin file open
bool is_safe_path(const char* requested, const char* allowed_root) char real_req[PATH_MAX], real_root[PATH_MAX]; if (!realpath(requested, real_req)) return false; if (!realpath(allowed_root, real_root)) return false; return strncmp(real_req, real_root, strlen(real_root)) == 0; bool is_safe_path(const char* requested
// Called instead of host opening (if plugin handles fully) int (*on_open_file)(const char* path, void* context, char** output_data, size_t* output_size); const char* allowed_root) char real_req[PATH_MAX]
Step 1: Plugin Registration PluginManager::register_file_handler( plugin_id, FileOpenFlags::CAN_HANDLE_EXTENSION | FileOpenFlags::ASYNC, ".xyz", ".abc", &my_file_open_callback ); Step 2: Host Invocation Logic def host_open_file(filepath): handlers = plugin_mgr.get_matching_handlers(filepath) handlers.sort(key=lambda h: h.priority, reverse=True) for handler in handlers: result = handler.before_open(filepath) if result.action == "HANDLE_FULLY": data = handler.open(filepath) return data elif result.action == "MODIFY_PATH": filepath = result.new_path
Define standard return codes:
# Fallback to native open return native_open(filepath) # plugin.py from hostapi import FileOpenHandler class CustomHandler(FileOpenHandler): def can_handle(self, path): return path.endswith('.encrypted')