mirror of
https://github.com/robbert-vdh/yabridge.git
synced 2026-05-07 20:10:13 +02:00
Move the configuration object to src/common/
This commit is contained in:
@@ -1,71 +0,0 @@
|
||||
// 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 "configuration.h"
|
||||
|
||||
#include <fnmatch.h>
|
||||
#include <toml++/toml.h>
|
||||
#include <fstream>
|
||||
|
||||
#include "utils.h"
|
||||
|
||||
namespace fs = boost::filesystem;
|
||||
|
||||
Configuration::Configuration() {}
|
||||
|
||||
Configuration::Configuration(const fs::path& config_path,
|
||||
const fs::path& yabridge_path)
|
||||
: Configuration() {
|
||||
// Will throw a `toml::parsing_error` if the file cannot be parsed. Better
|
||||
// to throw here rather than failing silently since syntax errors would
|
||||
// otherwise be impossible to spot.
|
||||
toml::table table = toml::parse_file(config_path.string());
|
||||
|
||||
const fs::path relative_path =
|
||||
yabridge_path.lexically_relative(config_path.parent_path());
|
||||
for (const auto& [pattern, value] : table) {
|
||||
// First try to match the glob pattern, allow matching an entire
|
||||
// directory for ease of use. If none of the patterns in the file match
|
||||
// the plugin path then everything will be left at the defaults.
|
||||
if (fnmatch(pattern.c_str(), relative_path.c_str(),
|
||||
FNM_PATHNAME | FNM_LEADING_DIR) != 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
matched_file = config_path;
|
||||
matched_pattern = pattern;
|
||||
|
||||
// If the table is missing some fields then they will simply be left at
|
||||
// their defaults
|
||||
if (toml::table* config = value.as_table()) {
|
||||
group = (*config)["group"].value<std::string>();
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
Configuration Configuration::load_for(const fs::path& yabridge_path) {
|
||||
// First find the closest `yabridge.tmol` file for the plugin, falling back
|
||||
// to default configuration settings if it doesn't exist
|
||||
const std::optional<fs::path> config_file =
|
||||
find_dominating_file("yabridge.toml", yabridge_path);
|
||||
if (!config_file) {
|
||||
return Configuration();
|
||||
}
|
||||
|
||||
return Configuration(*config_file, yabridge_path);
|
||||
}
|
||||
@@ -1,104 +0,0 @@
|
||||
// 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 <optional>
|
||||
|
||||
/**
|
||||
* An object that's used to provide plugin-specific configuration. Right now
|
||||
* this is only used to declare plugin groups. A plugin group is a set of
|
||||
* plugins that will be hosted in the same process rather than individually so
|
||||
* they can share resources. Configuration file loading works as follows:
|
||||
*
|
||||
* 1. `Configuration::load_for(path)` gets called with a path to the copy of or
|
||||
* symlink to `libyabridge.so` that the plugin host has tried to load.
|
||||
* 2. We start looking for a file named `yabridge.toml` in the same directory as
|
||||
* that `.so` file, iteratively continuing to search one directory higher
|
||||
* until we either find the file or we reach the filesystem root.
|
||||
* 3. If the file is found, then parse it as a TOML file and look for the first
|
||||
* table whose key is a glob pattern that (partially) matches the relative
|
||||
* path between the found `yabridge.toml` and the `.so` file. As a rule of
|
||||
* thumb, if the `find <pattern> -type f` command executed in Bash would list
|
||||
* the `.so` file, then the following table in `yabridge.tmol` would also
|
||||
* match the same `.so` file:
|
||||
*
|
||||
* ```toml
|
||||
* ["<patern>"]
|
||||
* group = "..."
|
||||
* ```
|
||||
* 4. If one of these glob patterns could be matched with the relative path of
|
||||
* the `.so` file then we'll use the settings specified in that section.
|
||||
* Otherwise the default settings will be used.
|
||||
*/
|
||||
class Configuration {
|
||||
public:
|
||||
/**
|
||||
* Create an empty configuration object with default settings.
|
||||
*/
|
||||
Configuration();
|
||||
|
||||
/**
|
||||
* Load the configuration for an instance of yabridge from a configuration
|
||||
* file by matching the plugin's relative path to the glob patterns in that
|
||||
* configuration file. Will leave the object empty if the plugin cannot be
|
||||
* matched to any of the patterns. Not meant to be used directly.
|
||||
*
|
||||
* @throw toml::parsing_error If the file could not be parsed.
|
||||
*
|
||||
* @see Configuration::load_for
|
||||
*/
|
||||
Configuration(const boost::filesystem::path& config_path,
|
||||
const boost::filesystem::path& yabridge_path);
|
||||
|
||||
/**
|
||||
* Load the configuration that belongs to a copy of or symlink to
|
||||
* `libyabridge.so`. If no configuration file could be found then this will
|
||||
* return an empty configuration object with default settings.
|
||||
*
|
||||
* This function will take any optional compile-time features that have not
|
||||
* been enabled into account.
|
||||
*
|
||||
* @param yabridge_path The path to the .so file that's being loaded.by the
|
||||
* VST host. This will be used both for the starting location of the
|
||||
* search and to determine which section in the config file to use.
|
||||
*
|
||||
* @return Either a configuration object populated with values from matched
|
||||
* glob pattern within the found configuration file, or an empty
|
||||
* configuration object if no configuration file could be found or if the
|
||||
* plugin could not be matched to any of the glob patterns in the
|
||||
* configuration file.
|
||||
*/
|
||||
static Configuration load_for(const boost::filesystem::path& yabridge_path);
|
||||
|
||||
/**
|
||||
* The name of the plugin group that should be used for the plugin this
|
||||
* configuration object was created for. If not set, then the plugin should
|
||||
* be hosted individually instead.
|
||||
*/
|
||||
std::optional<std::string> group;
|
||||
|
||||
/**
|
||||
* The path to the configuration file that was parsed.
|
||||
*/
|
||||
std::optional<boost::filesystem::path> matched_file;
|
||||
|
||||
/**
|
||||
* The matched glob pattern in the above configuration file.
|
||||
*/
|
||||
std::optional<std::string> matched_pattern;
|
||||
};
|
||||
@@ -22,6 +22,7 @@
|
||||
|
||||
#include "../common/communication.h"
|
||||
#include "../common/events.h"
|
||||
#include "utils.h"
|
||||
|
||||
namespace bp = boost::process;
|
||||
// I'd rather use std::filesystem instead, but Boost.Process depends on
|
||||
@@ -44,7 +45,7 @@ PluginBridge& get_bridge_instance(const AEffect& plugin) {
|
||||
}
|
||||
|
||||
PluginBridge::PluginBridge(audioMasterCallback host_callback)
|
||||
: config(Configuration::load_for(get_this_file_location())),
|
||||
: config(load_config_for(get_this_file_location())),
|
||||
vst_plugin_path(find_vst_plugin()),
|
||||
// All the fields should be zero initialized because
|
||||
// `Vst2PluginInstance::vstAudioMasterCallback` from Bitwig's plugin
|
||||
|
||||
@@ -23,8 +23,8 @@
|
||||
#include <mutex>
|
||||
#include <thread>
|
||||
|
||||
#include "../common/configuration.h"
|
||||
#include "../common/logging.h"
|
||||
#include "configuration.h"
|
||||
#include "host-process.h"
|
||||
|
||||
/**
|
||||
@@ -76,7 +76,7 @@ class PluginBridge {
|
||||
* The configuration for this instance of yabridge. Set based on the values
|
||||
* from a `yabridge.toml`, if it exists.
|
||||
*
|
||||
* @see Configuration::load_for
|
||||
* @see ./utils.h:load_config_for
|
||||
*/
|
||||
Configuration config;
|
||||
|
||||
|
||||
+13
-1
@@ -28,7 +28,7 @@
|
||||
// Generated inside of build directory
|
||||
#include <src/common/config/config.h>
|
||||
|
||||
#include "configuration.h"
|
||||
#include "../common/configuration.h"
|
||||
|
||||
namespace bp = boost::process;
|
||||
namespace fs = boost::filesystem;
|
||||
@@ -269,6 +269,18 @@ std::string get_wine_version() {
|
||||
return version_string;
|
||||
}
|
||||
|
||||
Configuration load_config_for(const fs::path& yabridge_path) {
|
||||
// First find the closest `yabridge.tmol` file for the plugin, falling back
|
||||
// to default configuration settings if it doesn't exist
|
||||
const std::optional<fs::path> config_file =
|
||||
find_dominating_file("yabridge.toml", yabridge_path);
|
||||
if (!config_file) {
|
||||
return Configuration();
|
||||
}
|
||||
|
||||
return Configuration(*config_file, yabridge_path);
|
||||
}
|
||||
|
||||
bp::environment set_wineprefix() {
|
||||
bp::environment env = boost::this_process::environment();
|
||||
|
||||
|
||||
@@ -20,6 +20,8 @@
|
||||
#include <boost/process/async_pipe.hpp>
|
||||
#include <boost/process/environment.hpp>
|
||||
|
||||
#include "../common/configuration.h"
|
||||
|
||||
/**
|
||||
* Boost 1.72 was released with a known breaking bug caused by a missing
|
||||
* typedef: https://github.com/boostorg/process/issues/116.
|
||||
@@ -164,6 +166,29 @@ boost::filesystem::path get_this_file_location();
|
||||
*/
|
||||
std::string get_wine_version();
|
||||
|
||||
/**
|
||||
* Load the configuration that belongs to a copy of or symlink to
|
||||
* `libyabridge.so`. If no configuration file could be found then this will
|
||||
* return an empty configuration object with default settings. See the docstrong
|
||||
* on the `Configuration` class for more details on how to choose the config
|
||||
* file to load.
|
||||
*
|
||||
* This function will take any optional compile-time features that have not been
|
||||
* enabled into account.
|
||||
*
|
||||
* @param yabridge_path The path to the .so file that's being loaded.by the VST
|
||||
* host. This will be used both for the starting location of the search and to
|
||||
* determine which section in the config file to use.
|
||||
*
|
||||
* @return Either a configuration object populated with values from matched glob
|
||||
* pattern within the found configuration file, or an empty configuration
|
||||
* object if no configuration file could be found or if the plugin could not
|
||||
* be matched to any of the glob patterns in the configuration file.
|
||||
*
|
||||
* @see Configuration
|
||||
*/
|
||||
Configuration load_config_for(const boost::filesystem::path& yabridge_path);
|
||||
|
||||
/**
|
||||
* 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
|
||||
|
||||
Reference in New Issue
Block a user