mirror of
https://github.com/robbert-vdh/yabridge.git
synced 2026-05-10 04:30:12 +02:00
Move plugin bridge helper functions to utils.h
This commit is contained in:
@@ -62,6 +62,7 @@ shared_library(
|
|||||||
'src/common/serialization.cpp',
|
'src/common/serialization.cpp',
|
||||||
'src/plugin/plugin.cpp',
|
'src/plugin/plugin.cpp',
|
||||||
'src/plugin/plugin-bridge.cpp',
|
'src/plugin/plugin-bridge.cpp',
|
||||||
|
'src/plugin/utils.cpp',
|
||||||
version_header,
|
version_header,
|
||||||
],
|
],
|
||||||
native : true,
|
native : true,
|
||||||
|
|||||||
@@ -17,14 +17,9 @@
|
|||||||
#include "plugin-bridge.h"
|
#include "plugin-bridge.h"
|
||||||
|
|
||||||
#include <boost/asio/read_until.hpp>
|
#include <boost/asio/read_until.hpp>
|
||||||
#include <boost/dll/runtime_symbol_info.hpp>
|
|
||||||
#include <boost/filesystem.hpp>
|
|
||||||
#include <boost/process/env.hpp>
|
#include <boost/process/env.hpp>
|
||||||
#include <boost/process/io.hpp>
|
#include <boost/process/io.hpp>
|
||||||
#include <boost/process/search_path.hpp>
|
|
||||||
#include <boost/process/system.hpp>
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <random>
|
|
||||||
|
|
||||||
#ifdef USE_WINEDBG
|
#ifdef USE_WINEDBG
|
||||||
#include <boost/process/start_dir.hpp>
|
#include <boost/process/start_dir.hpp>
|
||||||
@@ -42,116 +37,12 @@ namespace bp = boost::process;
|
|||||||
// boost::filesystem
|
// boost::filesystem
|
||||||
namespace fs = boost::filesystem;
|
namespace fs = boost::filesystem;
|
||||||
|
|
||||||
/**
|
|
||||||
* Used for generating random identifiers.
|
|
||||||
*/
|
|
||||||
constexpr char alphanumeric_characters[] =
|
|
||||||
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
|
|
||||||
|
|
||||||
intptr_t dispatch_proxy(AEffect*, int, int, intptr_t, void*, float);
|
intptr_t dispatch_proxy(AEffect*, int, int, intptr_t, void*, float);
|
||||||
void process_proxy(AEffect*, float**, float**, int);
|
void process_proxy(AEffect*, float**, float**, int);
|
||||||
void process_replacing_proxy(AEffect*, float**, float**, int);
|
void process_replacing_proxy(AEffect*, float**, float**, int);
|
||||||
void setParameter_proxy(AEffect*, int, float);
|
void setParameter_proxy(AEffect*, int, float);
|
||||||
float getParameter_proxy(AEffect*, int);
|
float getParameter_proxy(AEffect*, int);
|
||||||
|
|
||||||
/**
|
|
||||||
* Create a logger prefix based on the unique socket path for easy
|
|
||||||
* identification. The socket path contains both the plugin's name and a unique
|
|
||||||
* identifier.
|
|
||||||
*
|
|
||||||
* @param socket_path The path to the socket endpoint in use.
|
|
||||||
*
|
|
||||||
* @return A prefix string for log messages.
|
|
||||||
*/
|
|
||||||
std::string create_logger_prefix(const fs::path& socket_path);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Determine the architecture of a VST plugin (or rather, a .dll file) based on
|
|
||||||
* it's header values.
|
|
||||||
*
|
|
||||||
* See https://docs.microsoft.com/en-us/windows/win32/debug/pe-format for more
|
|
||||||
* information on the PE32 format.
|
|
||||||
*
|
|
||||||
* @param plugin_path The path to the .dll file we're going to check.
|
|
||||||
*
|
|
||||||
* @return The detected architecture.
|
|
||||||
* @throw std::runtime_error If the file is not a .dll file.
|
|
||||||
*/
|
|
||||||
PluginArchitecture find_vst_architecture(fs::path);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Finds the Wine VST hsot (either `yabridge-host.exe` or `yabridge-host.exe`
|
|
||||||
* depending on the plugin). For this we will search in two places:
|
|
||||||
*
|
|
||||||
* 1. Alongside libyabridge.so if the file got symlinked. This is useful
|
|
||||||
* when developing, as you can simply symlink the the libyabridge.so
|
|
||||||
* file in the build directory without having to install anything to
|
|
||||||
* /usr.
|
|
||||||
* 2. In the regular search path.
|
|
||||||
*
|
|
||||||
* @param plugin_arch The architecture of the plugin, either 64-bit or 32-bit.
|
|
||||||
* Used to determine which host application to use, if available.
|
|
||||||
*
|
|
||||||
* @return The a path to the VST host, if found.
|
|
||||||
* @throw std::runtime_error If the Wine VST host could not be found.
|
|
||||||
*/
|
|
||||||
fs::path find_vst_host(PluginArchitecture plugin_arch);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Find the VST plugin .dll file that corresponds to this copy of
|
|
||||||
* `libyabridge.so`. This should be the same as the name of this file but with a
|
|
||||||
* `.dll` file extension instead of `.so`. In case this file does not exist and
|
|
||||||
* the `.so` file is a symlink, we'll also repeat this check for the file it
|
|
||||||
* links to. This is to support the workflow described in issue #3 where you use
|
|
||||||
* symlinks to copies of `libyabridge.so`.
|
|
||||||
*
|
|
||||||
* @return The a path to the accompanying VST plugin .dll file.
|
|
||||||
* @throw std::runtime_error If no matching .dll file could be found.
|
|
||||||
*/
|
|
||||||
fs::path find_vst_plugin();
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Locate the Wine prefix this file is located in, if it is inside of a wine
|
|
||||||
* prefix.
|
|
||||||
*
|
|
||||||
* @return Either the path to the Wine prefix (containing the `drive_c?`
|
|
||||||
* directory), or `std::nullopt` if it is not inside of a wine prefix.
|
|
||||||
*/
|
|
||||||
std::optional<fs::path> find_wineprefix();
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Generate a unique name for the Unix domain socket endpoint based on the VST
|
|
||||||
* plugin's name. This will also generate the parent directory if it does not
|
|
||||||
* yet exist since we're using this in the constructor's initializer list.
|
|
||||||
*
|
|
||||||
* @return A path to a not yet existing Unix domain socket endpoint.
|
|
||||||
* @throw std::runtime_error If no matching .dll file could be found.
|
|
||||||
*/
|
|
||||||
fs::path generate_endpoint_name();
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Return a path to this `.so` file. This can be used to find out from where
|
|
||||||
* this link to or copy of `libyabridge.so` was loaded.
|
|
||||||
*/
|
|
||||||
fs::path get_this_file_location();
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Return the installed Wine version. This is obtained by from `wine --version`
|
|
||||||
* and then stripping the `wine-` prefix. This respects the `WINELOADER`
|
|
||||||
* environment variable used in the scripts generated by winegcc.
|
|
||||||
*
|
|
||||||
* This will *not* throw when Wine can not be found, but will instead return
|
|
||||||
* '<NOT FOUND>'. This way the user will still get some useful log files.
|
|
||||||
*/
|
|
||||||
std::string get_wine_version();
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Locate the Wine prefix and set the `WINEPREFIX` environment variable if
|
|
||||||
* found. This way it's also possible to run .dll files outside of a Wine prefix
|
|
||||||
* using the user's default prefix.
|
|
||||||
*/
|
|
||||||
bp::environment set_wineprefix();
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Fetch the bridge instance stored in an unused pointer from a VST plugin. This
|
* Fetch the bridge instance stored in an unused pointer from a VST plugin. This
|
||||||
* is sadly needed as a workaround to avoid using globals since we need free
|
* is sadly needed as a workaround to avoid using globals since we need free
|
||||||
@@ -718,227 +609,6 @@ void PluginBridge::async_log_pipe_lines(patched_async_pipe& pipe,
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string create_logger_prefix(const fs::path& socket_path) {
|
|
||||||
// Use the socket filename as the logger prefix, but strip the `yabridge-`
|
|
||||||
// part since that's redundant
|
|
||||||
std::string socket_name =
|
|
||||||
socket_path.filename().replace_extension().string();
|
|
||||||
const std::string socket_prefix("yabridge-");
|
|
||||||
assert(socket_name.find(socket_prefix) == 0);
|
|
||||||
socket_name = socket_name.substr(socket_prefix.size());
|
|
||||||
|
|
||||||
std::ostringstream prefix;
|
|
||||||
prefix << "[" << socket_name << "] ";
|
|
||||||
|
|
||||||
return prefix.str();
|
|
||||||
}
|
|
||||||
|
|
||||||
std::optional<fs::path> find_wineprefix() {
|
|
||||||
// Try to locate the Wine prefix the plugin's .dll file is located in by
|
|
||||||
// finding the first parent directory that contains a directory named
|
|
||||||
// `dosdevices`
|
|
||||||
fs::path wineprefix_path = find_vst_plugin();
|
|
||||||
while (wineprefix_path != "") {
|
|
||||||
if (fs::is_directory(wineprefix_path / "dosdevices")) {
|
|
||||||
return wineprefix_path;
|
|
||||||
}
|
|
||||||
|
|
||||||
wineprefix_path = wineprefix_path.parent_path();
|
|
||||||
}
|
|
||||||
|
|
||||||
return std::nullopt;
|
|
||||||
}
|
|
||||||
|
|
||||||
PluginArchitecture find_vst_architecture(fs::path plugin_path) {
|
|
||||||
std::ifstream file(plugin_path, std::ifstream::binary | std::ifstream::in);
|
|
||||||
|
|
||||||
// The linker will place the offset where the PE signature is placed at the
|
|
||||||
// end of the MS-DOS stub, at offset 0x3c
|
|
||||||
uint32_t pe_signature_offset;
|
|
||||||
file.seekg(0x3c);
|
|
||||||
file.read(reinterpret_cast<char*>(&pe_signature_offset),
|
|
||||||
sizeof(pe_signature_offset));
|
|
||||||
|
|
||||||
// The PE32 signature will be followed by a magic number that indicates the
|
|
||||||
// target architecture of the binary
|
|
||||||
uint32_t pe_signature;
|
|
||||||
uint16_t machine_type;
|
|
||||||
file.seekg(pe_signature_offset);
|
|
||||||
file.read(reinterpret_cast<char*>(&pe_signature), sizeof(pe_signature));
|
|
||||||
file.read(reinterpret_cast<char*>(&machine_type), sizeof(machine_type));
|
|
||||||
|
|
||||||
constexpr char expected_pe_signature[4] = {'P', 'E', '\0', '\0'};
|
|
||||||
if (pe_signature !=
|
|
||||||
*reinterpret_cast<const uint32_t*>(expected_pe_signature)) {
|
|
||||||
throw std::runtime_error("'" + plugin_path.string() +
|
|
||||||
"' is not a valid .dll file");
|
|
||||||
}
|
|
||||||
|
|
||||||
// These constants are specified in
|
|
||||||
// https://docs.microsoft.com/en-us/windows/win32/debug/pe-format#machine-types
|
|
||||||
switch (machine_type) {
|
|
||||||
case 0x014c: // IMAGE_FILE_MACHINE_I386
|
|
||||||
return PluginArchitecture::vst_32;
|
|
||||||
break;
|
|
||||||
case 0x8664: // IMAGE_FILE_MACHINE_AMD64
|
|
||||||
case 0x0000: // IMAGE_FILE_MACHINE_UNKNOWN
|
|
||||||
return PluginArchitecture::vst_64;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
// When compiled without optimizations, GCC 9.3 will warn that the function
|
|
||||||
// does not return if we put this in a `default:` case instead.
|
|
||||||
std::ostringstream error_msg;
|
|
||||||
error_msg << "'" << plugin_path
|
|
||||||
<< "' is neither a x86 nor a x86_64 PE32 file. Actual "
|
|
||||||
"architecture: 0x"
|
|
||||||
<< std::hex << machine_type;
|
|
||||||
throw std::runtime_error(error_msg.str());
|
|
||||||
}
|
|
||||||
|
|
||||||
fs::path find_vst_host(PluginArchitecture plugin_arch) {
|
|
||||||
auto host_name = yabridge_wine_host_name;
|
|
||||||
if (plugin_arch == PluginArchitecture::vst_32) {
|
|
||||||
host_name = yabridge_wine_host_name_32bit;
|
|
||||||
}
|
|
||||||
|
|
||||||
fs::path host_path =
|
|
||||||
fs::canonical(get_this_file_location()).remove_filename() / host_name;
|
|
||||||
if (fs::exists(host_path)) {
|
|
||||||
return host_path;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Bosot will return an empty path if the file could not be found in the
|
|
||||||
// search path
|
|
||||||
const fs::path vst_host_path = bp::search_path(host_name);
|
|
||||||
if (vst_host_path == "") {
|
|
||||||
throw std::runtime_error("Could not locate '" + std::string(host_name) +
|
|
||||||
"'");
|
|
||||||
}
|
|
||||||
|
|
||||||
return vst_host_path;
|
|
||||||
}
|
|
||||||
|
|
||||||
fs::path find_vst_plugin() {
|
|
||||||
const fs::path this_plugin_path =
|
|
||||||
"/" / fs::path("/" + get_this_file_location().string());
|
|
||||||
|
|
||||||
fs::path plugin_path(this_plugin_path);
|
|
||||||
plugin_path.replace_extension(".dll");
|
|
||||||
if (fs::exists(plugin_path)) {
|
|
||||||
// Also resolve symlinks here, to support symlinked .dll files
|
|
||||||
return fs::canonical(plugin_path);
|
|
||||||
}
|
|
||||||
|
|
||||||
// In case this files does not exist and our `.so` file is a symlink, we'll
|
|
||||||
// also repeat this check after resolving that symlink to support links to
|
|
||||||
// copies of `libyabridge.so` as described in issue #3
|
|
||||||
fs::path alternative_plugin_path = fs::canonical(this_plugin_path);
|
|
||||||
alternative_plugin_path.replace_extension(".dll");
|
|
||||||
if (fs::exists(alternative_plugin_path)) {
|
|
||||||
return fs::canonical(alternative_plugin_path);
|
|
||||||
}
|
|
||||||
|
|
||||||
// This function is used in the constructor's initializer list so we have to
|
|
||||||
// throw when the path could not be found
|
|
||||||
throw std::runtime_error("'" + plugin_path.string() +
|
|
||||||
"' does not exist, make sure to rename "
|
|
||||||
"'libyabridge.so' to match a "
|
|
||||||
"VST plugin .dll file.");
|
|
||||||
}
|
|
||||||
|
|
||||||
fs::path generate_endpoint_name() {
|
|
||||||
const auto plugin_name =
|
|
||||||
find_vst_plugin().filename().replace_extension("").string();
|
|
||||||
|
|
||||||
std::random_device random_device;
|
|
||||||
std::mt19937 rng(random_device());
|
|
||||||
fs::path candidate_endpoint;
|
|
||||||
do {
|
|
||||||
std::string random_id;
|
|
||||||
std::sample(
|
|
||||||
alphanumeric_characters,
|
|
||||||
alphanumeric_characters + strlen(alphanumeric_characters) - 1,
|
|
||||||
std::back_inserter(random_id), 8, rng);
|
|
||||||
|
|
||||||
// We'll get rid of the file descriptors immediately after accepting the
|
|
||||||
// sockets, so putting them inside of a subdirectory would only leave
|
|
||||||
// behind an empty directory
|
|
||||||
std::ostringstream socket_name;
|
|
||||||
socket_name << "yabridge-" << plugin_name << "-" << random_id
|
|
||||||
<< ".sock";
|
|
||||||
|
|
||||||
candidate_endpoint = fs::temp_directory_path() / socket_name.str();
|
|
||||||
} while (fs::exists(candidate_endpoint));
|
|
||||||
|
|
||||||
// TODO: Should probably try creating the endpoint right here and catch any
|
|
||||||
// exceptions since this could technically result in a race condition
|
|
||||||
// when two instances of yabridge decide to use the same endpoint name
|
|
||||||
// at the same time
|
|
||||||
|
|
||||||
return candidate_endpoint;
|
|
||||||
}
|
|
||||||
|
|
||||||
fs::path get_this_file_location() {
|
|
||||||
// HACK: Not sure why, but `boost::dll::this_line_location()` returns a path
|
|
||||||
// starting with a double slash on some systems. I've seen this happen
|
|
||||||
// on both Ubuntu 18.04 and 20.04, but not on Arch based distros.
|
|
||||||
// Under Linux a path starting with two slashes is treated the same as
|
|
||||||
// a path starting with only a single slash, but Wine will refuse to
|
|
||||||
// load any files when the path starts with two slashes. Prepending
|
|
||||||
// `/` to a pad coerces theses two slashes into a single slash.
|
|
||||||
return "/" / boost::dll::this_line_location();
|
|
||||||
}
|
|
||||||
|
|
||||||
std::string get_wine_version() {
|
|
||||||
// The '*.exe' scripts generated by winegcc allow you to override the binary
|
|
||||||
// used to run Wine, so will will respect this as well
|
|
||||||
std::string wine_command = "wine";
|
|
||||||
|
|
||||||
bp::native_environment env = boost::this_process::environment();
|
|
||||||
if (!env["WINELOADER"].empty()) {
|
|
||||||
wine_command = env.get("WINELOADER");
|
|
||||||
}
|
|
||||||
|
|
||||||
bp::ipstream output;
|
|
||||||
try {
|
|
||||||
const fs::path wine_path = bp::search_path(wine_command);
|
|
||||||
bp::system(wine_path, "--version", bp::std_out = output);
|
|
||||||
} catch (const std::system_error&) {
|
|
||||||
return "<NOT FOUND>";
|
|
||||||
}
|
|
||||||
|
|
||||||
// `wine --version` might contain additional output in certain custom Wine
|
|
||||||
// builds, so we only want to look at the first line
|
|
||||||
std::string version_string;
|
|
||||||
std::getline(output, version_string);
|
|
||||||
|
|
||||||
// Strip the `wine-` prefix from the output, could potentially be absent in
|
|
||||||
// custom Wine builds
|
|
||||||
const std::string version_prefix("wine-");
|
|
||||||
if (version_string.find(version_prefix) == 0) {
|
|
||||||
version_string = version_string.substr(version_prefix.size());
|
|
||||||
}
|
|
||||||
|
|
||||||
return version_string;
|
|
||||||
}
|
|
||||||
|
|
||||||
bp::environment set_wineprefix() {
|
|
||||||
bp::native_environment env = boost::this_process::environment();
|
|
||||||
|
|
||||||
// Allow the wine prefix to be overridden manually
|
|
||||||
if (!env["WINEPREFIX"].empty()) {
|
|
||||||
return env;
|
|
||||||
}
|
|
||||||
|
|
||||||
const auto wineprefix_path = find_wineprefix();
|
|
||||||
if (wineprefix_path.has_value()) {
|
|
||||||
env["WINEPREFIX"] = wineprefix_path->string();
|
|
||||||
}
|
|
||||||
|
|
||||||
return env;
|
|
||||||
}
|
|
||||||
|
|
||||||
// The below functions are proxy functions for the methods defined in
|
// The below functions are proxy functions for the methods defined in
|
||||||
// `Bridge.cpp`
|
// `Bridge.cpp`
|
||||||
|
|
||||||
|
|||||||
@@ -21,35 +21,12 @@
|
|||||||
#include <boost/asio/io_context.hpp>
|
#include <boost/asio/io_context.hpp>
|
||||||
#include <boost/asio/local/stream_protocol.hpp>
|
#include <boost/asio/local/stream_protocol.hpp>
|
||||||
#include <boost/asio/streambuf.hpp>
|
#include <boost/asio/streambuf.hpp>
|
||||||
#include <boost/process/async_pipe.hpp>
|
|
||||||
#include <boost/process/child.hpp>
|
#include <boost/process/child.hpp>
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
#include <thread>
|
#include <thread>
|
||||||
|
|
||||||
#include "../common/logging.h"
|
#include "../common/logging.h"
|
||||||
|
#include "utils.h"
|
||||||
/**
|
|
||||||
* Boost 1.72 was released with a known breaking bug caused by a missing
|
|
||||||
* typedef: https://github.com/boostorg/process/issues/116.
|
|
||||||
*
|
|
||||||
* Luckily this is easy to fix since it's not really possible to downgrade Boost
|
|
||||||
* as it would break other applications.
|
|
||||||
*
|
|
||||||
* Check if this is still needed for other distros after Arch starts packaging
|
|
||||||
* Boost 1.73.
|
|
||||||
*/
|
|
||||||
class patched_async_pipe : public boost::process::async_pipe {
|
|
||||||
public:
|
|
||||||
using boost::process::async_pipe::async_pipe;
|
|
||||||
|
|
||||||
typedef typename handle_type::executor_type executor_type;
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* A tag to differentiate between 32 and 64-bit plugins, used to determine which
|
|
||||||
* host application to use.
|
|
||||||
*/
|
|
||||||
enum class PluginArchitecture { vst_32, vst_64 };
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This handles the communication between the Linux native VST plugin and the
|
* This handles the communication between the Linux native VST plugin and the
|
||||||
|
|||||||
@@ -0,0 +1,259 @@
|
|||||||
|
// yabridge: a Wine VST bridge
|
||||||
|
// Copyright (C) 2020 Robbert van der Helm
|
||||||
|
//
|
||||||
|
// This program is free software: you can redistribute it and/or modify
|
||||||
|
// it under the terms of the GNU General Public License as published by
|
||||||
|
// the Free Software Foundation, either version 3 of the License, or
|
||||||
|
// (at your option) any later version.
|
||||||
|
//
|
||||||
|
// This program is distributed in the hope that it will be useful,
|
||||||
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
// GNU General Public License for more details.
|
||||||
|
//
|
||||||
|
// You should have received a copy of the GNU General Public License
|
||||||
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
#include "utils.h"
|
||||||
|
|
||||||
|
#include <boost/dll/runtime_symbol_info.hpp>
|
||||||
|
#include <boost/process/io.hpp>
|
||||||
|
#include <boost/process/pipe.hpp>
|
||||||
|
#include <boost/process/search_path.hpp>
|
||||||
|
#include <boost/process/system.hpp>
|
||||||
|
#include <fstream>
|
||||||
|
#include <random>
|
||||||
|
#include <sstream>
|
||||||
|
|
||||||
|
// Generated inside of build directory
|
||||||
|
#include <src/common/config/config.h>
|
||||||
|
|
||||||
|
namespace bp = boost::process;
|
||||||
|
namespace fs = boost::filesystem;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Used for generating random identifiers.
|
||||||
|
*/
|
||||||
|
constexpr char alphanumeric_characters[] =
|
||||||
|
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
|
||||||
|
|
||||||
|
std::string create_logger_prefix(const fs::path& socket_path) {
|
||||||
|
// Use the socket filename as the logger prefix, but strip the `yabridge-`
|
||||||
|
// part since that's redundant
|
||||||
|
std::string socket_name =
|
||||||
|
socket_path.filename().replace_extension().string();
|
||||||
|
const std::string socket_prefix("yabridge-");
|
||||||
|
assert(socket_name.find(socket_prefix) == 0);
|
||||||
|
socket_name = socket_name.substr(socket_prefix.size());
|
||||||
|
|
||||||
|
std::ostringstream prefix;
|
||||||
|
prefix << "[" << socket_name << "] ";
|
||||||
|
|
||||||
|
return prefix.str();
|
||||||
|
}
|
||||||
|
|
||||||
|
std::optional<fs::path> find_wineprefix() {
|
||||||
|
// Try to locate the Wine prefix the plugin's .dll file is located in by
|
||||||
|
// finding the first parent directory that contains a directory named
|
||||||
|
// `dosdevices`
|
||||||
|
fs::path wineprefix_path = find_vst_plugin();
|
||||||
|
while (wineprefix_path != "") {
|
||||||
|
if (fs::is_directory(wineprefix_path / "dosdevices")) {
|
||||||
|
return wineprefix_path;
|
||||||
|
}
|
||||||
|
|
||||||
|
wineprefix_path = wineprefix_path.parent_path();
|
||||||
|
}
|
||||||
|
|
||||||
|
return std::nullopt;
|
||||||
|
}
|
||||||
|
|
||||||
|
PluginArchitecture find_vst_architecture(fs::path plugin_path) {
|
||||||
|
std::ifstream file(plugin_path, std::ifstream::binary | std::ifstream::in);
|
||||||
|
|
||||||
|
// The linker will place the offset where the PE signature is placed at the
|
||||||
|
// end of the MS-DOS stub, at offset 0x3c
|
||||||
|
uint32_t pe_signature_offset;
|
||||||
|
file.seekg(0x3c);
|
||||||
|
file.read(reinterpret_cast<char*>(&pe_signature_offset),
|
||||||
|
sizeof(pe_signature_offset));
|
||||||
|
|
||||||
|
// The PE32 signature will be followed by a magic number that indicates the
|
||||||
|
// target architecture of the binary
|
||||||
|
uint32_t pe_signature;
|
||||||
|
uint16_t machine_type;
|
||||||
|
file.seekg(pe_signature_offset);
|
||||||
|
file.read(reinterpret_cast<char*>(&pe_signature), sizeof(pe_signature));
|
||||||
|
file.read(reinterpret_cast<char*>(&machine_type), sizeof(machine_type));
|
||||||
|
|
||||||
|
constexpr char expected_pe_signature[4] = {'P', 'E', '\0', '\0'};
|
||||||
|
if (pe_signature !=
|
||||||
|
*reinterpret_cast<const uint32_t*>(expected_pe_signature)) {
|
||||||
|
throw std::runtime_error("'" + plugin_path.string() +
|
||||||
|
"' is not a valid .dll file");
|
||||||
|
}
|
||||||
|
|
||||||
|
// These constants are specified in
|
||||||
|
// https://docs.microsoft.com/en-us/windows/win32/debug/pe-format#machine-types
|
||||||
|
switch (machine_type) {
|
||||||
|
case 0x014c: // IMAGE_FILE_MACHINE_I386
|
||||||
|
return PluginArchitecture::vst_32;
|
||||||
|
break;
|
||||||
|
case 0x8664: // IMAGE_FILE_MACHINE_AMD64
|
||||||
|
case 0x0000: // IMAGE_FILE_MACHINE_UNKNOWN
|
||||||
|
return PluginArchitecture::vst_64;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
// When compiled without optimizations, GCC 9.3 will warn that the function
|
||||||
|
// does not return if we put this in a `default:` case instead.
|
||||||
|
std::ostringstream error_msg;
|
||||||
|
error_msg << "'" << plugin_path
|
||||||
|
<< "' is neither a x86 nor a x86_64 PE32 file. Actual "
|
||||||
|
"architecture: 0x"
|
||||||
|
<< std::hex << machine_type;
|
||||||
|
throw std::runtime_error(error_msg.str());
|
||||||
|
}
|
||||||
|
|
||||||
|
fs::path find_vst_host(PluginArchitecture plugin_arch) {
|
||||||
|
auto host_name = yabridge_wine_host_name;
|
||||||
|
if (plugin_arch == PluginArchitecture::vst_32) {
|
||||||
|
host_name = yabridge_wine_host_name_32bit;
|
||||||
|
}
|
||||||
|
|
||||||
|
fs::path host_path =
|
||||||
|
fs::canonical(get_this_file_location()).remove_filename() / host_name;
|
||||||
|
if (fs::exists(host_path)) {
|
||||||
|
return host_path;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Bosot will return an empty path if the file could not be found in the
|
||||||
|
// search path
|
||||||
|
const fs::path vst_host_path = bp::search_path(host_name);
|
||||||
|
if (vst_host_path == "") {
|
||||||
|
throw std::runtime_error("Could not locate '" + std::string(host_name) +
|
||||||
|
"'");
|
||||||
|
}
|
||||||
|
|
||||||
|
return vst_host_path;
|
||||||
|
}
|
||||||
|
|
||||||
|
fs::path find_vst_plugin() {
|
||||||
|
const fs::path this_plugin_path =
|
||||||
|
"/" / fs::path("/" + get_this_file_location().string());
|
||||||
|
|
||||||
|
fs::path plugin_path(this_plugin_path);
|
||||||
|
plugin_path.replace_extension(".dll");
|
||||||
|
if (fs::exists(plugin_path)) {
|
||||||
|
// Also resolve symlinks here, to support symlinked .dll files
|
||||||
|
return fs::canonical(plugin_path);
|
||||||
|
}
|
||||||
|
|
||||||
|
// In case this files does not exist and our `.so` file is a symlink, we'll
|
||||||
|
// also repeat this check after resolving that symlink to support links to
|
||||||
|
// copies of `libyabridge.so` as described in issue #3
|
||||||
|
fs::path alternative_plugin_path = fs::canonical(this_plugin_path);
|
||||||
|
alternative_plugin_path.replace_extension(".dll");
|
||||||
|
if (fs::exists(alternative_plugin_path)) {
|
||||||
|
return fs::canonical(alternative_plugin_path);
|
||||||
|
}
|
||||||
|
|
||||||
|
// This function is used in the constructor's initializer list so we have to
|
||||||
|
// throw when the path could not be found
|
||||||
|
throw std::runtime_error("'" + plugin_path.string() +
|
||||||
|
"' does not exist, make sure to rename "
|
||||||
|
"'libyabridge.so' to match a "
|
||||||
|
"VST plugin .dll file.");
|
||||||
|
}
|
||||||
|
|
||||||
|
fs::path generate_endpoint_name() {
|
||||||
|
const auto plugin_name =
|
||||||
|
find_vst_plugin().filename().replace_extension("").string();
|
||||||
|
|
||||||
|
std::random_device random_device;
|
||||||
|
std::mt19937 rng(random_device());
|
||||||
|
fs::path candidate_endpoint;
|
||||||
|
do {
|
||||||
|
std::string random_id;
|
||||||
|
std::sample(
|
||||||
|
alphanumeric_characters,
|
||||||
|
alphanumeric_characters + strlen(alphanumeric_characters) - 1,
|
||||||
|
std::back_inserter(random_id), 8, rng);
|
||||||
|
|
||||||
|
// We'll get rid of the file descriptors immediately after accepting the
|
||||||
|
// sockets, so putting them inside of a subdirectory would only leave
|
||||||
|
// behind an empty directory
|
||||||
|
std::ostringstream socket_name;
|
||||||
|
socket_name << "yabridge-" << plugin_name << "-" << random_id
|
||||||
|
<< ".sock";
|
||||||
|
|
||||||
|
candidate_endpoint = fs::temp_directory_path() / socket_name.str();
|
||||||
|
} while (fs::exists(candidate_endpoint));
|
||||||
|
|
||||||
|
// TODO: Should probably try creating the endpoint right here and catch any
|
||||||
|
// exceptions since this could technically result in a race condition
|
||||||
|
// when two instances of yabridge decide to use the same endpoint name
|
||||||
|
// at the same time
|
||||||
|
|
||||||
|
return candidate_endpoint;
|
||||||
|
}
|
||||||
|
|
||||||
|
fs::path get_this_file_location() {
|
||||||
|
// HACK: Not sure why, but `boost::dll::this_line_location()` returns a path
|
||||||
|
// starting with a double slash on some systems. I've seen this happen
|
||||||
|
// on both Ubuntu 18.04 and 20.04, but not on Arch based distros.
|
||||||
|
// Under Linux a path starting with two slashes is treated the same as
|
||||||
|
// a path starting with only a single slash, but Wine will refuse to
|
||||||
|
// load any files when the path starts with two slashes. Prepending
|
||||||
|
// `/` to a pad coerces theses two slashes into a single slash.
|
||||||
|
return "/" / boost::dll::this_line_location();
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string get_wine_version() {
|
||||||
|
// The '*.exe' scripts generated by winegcc allow you to override the binary
|
||||||
|
// used to run Wine, so will will respect this as well
|
||||||
|
std::string wine_command = "wine";
|
||||||
|
|
||||||
|
bp::native_environment env = boost::this_process::environment();
|
||||||
|
if (!env["WINELOADER"].empty()) {
|
||||||
|
wine_command = env.get("WINELOADER");
|
||||||
|
}
|
||||||
|
|
||||||
|
bp::ipstream output;
|
||||||
|
try {
|
||||||
|
const fs::path wine_path = bp::search_path(wine_command);
|
||||||
|
bp::system(wine_path, "--version", bp::std_out = output);
|
||||||
|
} catch (const std::system_error&) {
|
||||||
|
return "<NOT FOUND>";
|
||||||
|
}
|
||||||
|
|
||||||
|
// `wine --version` might contain additional output in certain custom Wine
|
||||||
|
// builds, so we only want to look at the first line
|
||||||
|
std::string version_string;
|
||||||
|
std::getline(output, version_string);
|
||||||
|
|
||||||
|
// Strip the `wine-` prefix from the output, could potentially be absent in
|
||||||
|
// custom Wine builds
|
||||||
|
const std::string version_prefix("wine-");
|
||||||
|
if (version_string.find(version_prefix) == 0) {
|
||||||
|
version_string = version_string.substr(version_prefix.size());
|
||||||
|
}
|
||||||
|
|
||||||
|
return version_string;
|
||||||
|
}
|
||||||
|
|
||||||
|
bp::environment set_wineprefix() {
|
||||||
|
bp::native_environment env = boost::this_process::environment();
|
||||||
|
|
||||||
|
// Allow the wine prefix to be overridden manually
|
||||||
|
if (!env["WINEPREFIX"].empty()) {
|
||||||
|
return env;
|
||||||
|
}
|
||||||
|
|
||||||
|
const auto wineprefix_path = find_wineprefix();
|
||||||
|
if (wineprefix_path.has_value()) {
|
||||||
|
env["WINEPREFIX"] = wineprefix_path->string();
|
||||||
|
}
|
||||||
|
|
||||||
|
return env;
|
||||||
|
}
|
||||||
@@ -0,0 +1,142 @@
|
|||||||
|
// yabridge: a Wine VST bridge
|
||||||
|
// Copyright (C) 2020 Robbert van der Helm
|
||||||
|
//
|
||||||
|
// This program is free software: you can redistribute it and/or modify
|
||||||
|
// it under the terms of the GNU General Public License as published by
|
||||||
|
// the Free Software Foundation, either version 3 of the License, or
|
||||||
|
// (at your option) any later version.
|
||||||
|
//
|
||||||
|
// This program is distributed in the hope that it will be useful,
|
||||||
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
// GNU General Public License for more details.
|
||||||
|
//
|
||||||
|
// You should have received a copy of the GNU General Public License
|
||||||
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <boost/filesystem.hpp>
|
||||||
|
#include <boost/process/async_pipe.hpp>
|
||||||
|
#include <boost/process/environment.hpp>
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Boost 1.72 was released with a known breaking bug caused by a missing
|
||||||
|
* typedef: https://github.com/boostorg/process/issues/116.
|
||||||
|
*
|
||||||
|
* Luckily this is easy to fix since it's not really possible to downgrade Boost
|
||||||
|
* as it would break other applications.
|
||||||
|
*
|
||||||
|
* Check if this is still needed for other distros after Arch starts packaging
|
||||||
|
* Boost 1.73.
|
||||||
|
*/
|
||||||
|
class patched_async_pipe : public boost::process::async_pipe {
|
||||||
|
public:
|
||||||
|
using boost::process::async_pipe::async_pipe;
|
||||||
|
|
||||||
|
typedef typename handle_type::executor_type executor_type;
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A tag to differentiate between 32 and 64-bit plugins, used to determine which
|
||||||
|
* host application to use.
|
||||||
|
*/
|
||||||
|
enum class PluginArchitecture { vst_32, vst_64 };
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a logger prefix based on the unique socket path for easy
|
||||||
|
* identification. The socket path contains both the plugin's name and a unique
|
||||||
|
* identifier.
|
||||||
|
*
|
||||||
|
* @param socket_path The path to the socket endpoint in use.
|
||||||
|
*
|
||||||
|
* @return A prefix string for log messages.
|
||||||
|
*/
|
||||||
|
std::string create_logger_prefix(const boost::filesystem::path& socket_path);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Determine the architecture of a VST plugin (or rather, a .dll file) based on
|
||||||
|
* it's header values.
|
||||||
|
*
|
||||||
|
* See https://docs.microsoft.com/en-us/windows/win32/debug/pe-format for more
|
||||||
|
* information on the PE32 format.
|
||||||
|
*
|
||||||
|
* @param plugin_path The path to the .dll file we're going to check.
|
||||||
|
*
|
||||||
|
* @return The detected architecture.
|
||||||
|
* @throw std::runtime_error If the file is not a .dll file.
|
||||||
|
*/
|
||||||
|
PluginArchitecture find_vst_architecture(boost::filesystem::path);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Finds the Wine VST hsot (either `yabridge-host.exe` or `yabridge-host.exe`
|
||||||
|
* depending on the plugin). For this we will search in two places:
|
||||||
|
*
|
||||||
|
* 1. Alongside libyabridge.so if the file got symlinked. This is useful
|
||||||
|
* when developing, as you can simply symlink the the libyabridge.so
|
||||||
|
* file in the build directory without having to install anything to
|
||||||
|
* /usr.
|
||||||
|
* 2. In the regular search path.
|
||||||
|
*
|
||||||
|
* @param plugin_arch The architecture of the plugin, either 64-bit or 32-bit.
|
||||||
|
* Used to determine which host application to use, if available.
|
||||||
|
*
|
||||||
|
* @return The a path to the VST host, if found.
|
||||||
|
* @throw std::runtime_error If the Wine VST host could not be found.
|
||||||
|
*/
|
||||||
|
boost::filesystem::path find_vst_host(PluginArchitecture plugin_arch);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Find the VST plugin .dll file that corresponds to this copy of
|
||||||
|
* `libyabridge.so`. This should be the same as the name of this file but with a
|
||||||
|
* `.dll` file extension instead of `.so`. In case this file does not exist and
|
||||||
|
* the `.so` file is a symlink, we'll also repeat this check for the file it
|
||||||
|
* links to. This is to support the workflow described in issue #3 where you use
|
||||||
|
* symlinks to copies of `libyabridge.so`.
|
||||||
|
*
|
||||||
|
* @return The a path to the accompanying VST plugin .dll file.
|
||||||
|
* @throw std::runtime_error If no matching .dll file could be found.
|
||||||
|
*/
|
||||||
|
boost::filesystem::path find_vst_plugin();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Locate the Wine prefix this file is located in, if it is inside of a wine
|
||||||
|
* prefix.
|
||||||
|
*
|
||||||
|
* @return Either the path to the Wine prefix (containing the `drive_c?`
|
||||||
|
* directory), or `std::nullopt` if it is not inside of a wine prefix.
|
||||||
|
*/
|
||||||
|
std::optional<boost::filesystem::path> find_wineprefix();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Generate a unique name for the Unix domain socket endpoint based on the VST
|
||||||
|
* plugin's name. This will also generate the parent directory if it does not
|
||||||
|
* yet exist since we're using this in the constructor's initializer list.
|
||||||
|
*
|
||||||
|
* @return A path to a not yet existing Unix domain socket endpoint.
|
||||||
|
* @throw std::runtime_error If no matching .dll file could be found.
|
||||||
|
*/
|
||||||
|
boost::filesystem::path generate_endpoint_name();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return a path to this `.so` file. This can be used to find out from where
|
||||||
|
* this link to or copy of `libyabridge.so` was loaded.
|
||||||
|
*/
|
||||||
|
boost::filesystem::path get_this_file_location();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return the installed Wine version. This is obtained by from `wine --version`
|
||||||
|
* and then stripping the `wine-` prefix. This respects the `WINELOADER`
|
||||||
|
* environment variable used in the scripts generated by winegcc.
|
||||||
|
*
|
||||||
|
* This will *not* throw when Wine can not be found, but will instead return
|
||||||
|
* '<NOT FOUND>'. This way the user will still get some useful log files.
|
||||||
|
*/
|
||||||
|
std::string get_wine_version();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Locate the Wine prefix and set the `WINEPREFIX` environment variable if
|
||||||
|
* found. This way it's also possible to run .dll files outside of a Wine prefix
|
||||||
|
* using the user's default prefix.
|
||||||
|
*/
|
||||||
|
boost::process::environment set_wineprefix();
|
||||||
Reference in New Issue
Block a user