diff --git a/src/plugin/bridges/clap-impls/plugin-factory-proxy.cpp b/src/plugin/bridges/clap-impls/plugin-factory-proxy.cpp index 86881259..2466fe10 100644 --- a/src/plugin/bridges/clap-impls/plugin-factory-proxy.cpp +++ b/src/plugin/bridges/clap-impls/plugin-factory-proxy.cpp @@ -56,6 +56,43 @@ clap_plugin_factory_proxy::plugin_factory_create_plugin( const struct clap_plugin_factory* factory, const clap_host_t* host, const char* plugin_id) { - // TODO: Implement - return nullptr; + assert(factory && host && plugin_id); + auto self = reinterpret_cast(factory); + + // We'll need to store another copy of this descriptor in the plugin + // instance + const auto descriptor = + std::find_if(self->descriptors_.begin(), self->descriptors_.end(), + [plugin_id](const auto& descriptor) { + return descriptor.id == plugin_id; + }); + if (descriptor == self->descriptors_.end()) { + self->bridge_.logger_.log_trace([&]() { + return "The host tried to create an instance for ID \"" + + std::string(plugin_id) + + "\", but we don't have a descriptor for this plugin."; + }); + + return nullptr; + } + + const clap::plugin_factory::CreateResponse response = + self->bridge_.send_main_thread_message(clap::plugin_factory::Create{ + .host = *host, .plugin_id = plugin_id}); + if (response.instance_id) { + // This plugin proxy is tied to the instance ID created on the Wine + // side. That way we can link function calls from the host to the + // correct plugin instance, and callbacks made from the plugin to the + // correct host instance. + self->bridge_.register_plugin_proxy(std::make_unique( + self->bridge_, *response.instance_id, *descriptor)); + + const auto& [plugin_proxy, _] = + self->bridge_.get_proxy(*response.instance_id); + + return plugin_proxy.plugin_vtable(); + } else { + // The plugin couldn't be created, for whatever reason that might be + return nullptr; + } }