Files
obs-studio/frontend/plugin-manager/PluginModuleLoader_MacOS.mm
FiniteSingularityandPatTheMav 11a8445aa4 frontend: Update app module loading behavior
With this change module loading in the application is managed by the
plugin manager and allows the application to explicitly load core
modules independantly from 3rd party (plugin) modules.

The implementation is split into separate files for different platforms
to allow for cleaner code handling the different possible locations and
path schemes used, including all possible legacy locations and plugin
bundle formats.

This change introduces new locations and directory formats for plugins:
* WINDOWS: Portable plugins need to be placed in a directory called
  "plugins" adjacent to the "bin" directory of an OBS Studio
  installation.
* WINDOWS: Plugins need to be packaged into their own directory
  "bundles", with the associated DLL in the root of the bundle and the
  associated "data" directory next to it.
* WINDOWS: The "obs-plugins" directory adjacent to the "bin" directory
  is deprecated and considered a legacy location for all plugins.
* MACOS: No changes in locations or formats.
* LINUX: Plugins distributed via system package managers now need to use
  the "obs-modules/plugins" directory in the system library directory
  for binaries, and the "obs/obs-modules/plugins" directory in the
  system data directory for resources.
* LINUX: The "obs-plugins" directories in the system library and system
  data directories are deprecated.
* LINUX: Plugins distributed outside of system packages need to use the
  same directory format as plugins for Windows (see above).
* LINUX: A "portable" plugin location like on Windows is not supported.
* LINUX: Proper XDG directory locations are scanned for plugins, using
  XDG_CONFIG_HOME for the legacy plugin format and XDG_DATA_HOME for the
  new plugin structure.
* LINUX: Changes Flatpak plugin loading to use the new plugin structure
  (while still supporting the legacy structure), and moves Flatpak
  specific plugin loading to its own source files.
* ALL PLATFORMS: A single environment variable "OBS_PLUGINS_PATH" can
  now be used to provide a highest-priority location from which to load
  plugins. This directory needs to contain plugin bundles per the new
  format. The one exception is Flatpak which does not support loading
  plugins from an env provided location.
* ALL PLATFORMS: A custom legacy plugin locations can still be provided
  via the "OBS_LEGACY_PLUGINS_PATH" and "OBS_LEGACY_PLUGINS_DATA_PATH"
  environment variables. These are only compatible with plugins using
  the legacy format and are considered deprecated. The one exception is
  Flatpak which does not support loading plugins from an env provided
  location.

Co-authored-by: PatTheMav <PatTheMav@users.noreply.github.com>
2026-09-23 18:53:19 -04:00

197 lines
7.5 KiB
Plaintext

/******************************************************************************
Copyright (C) 2026 by FiniteSingularity <finitesingularityttv@gmail.com>
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 2 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 <http://www.gnu.org/licenses/>.
******************************************************************************/
#include "PluginModuleLoader.hpp"
#include "PluginManager.hpp"
#include <obs.h>
#include <util/dstr.h>
#include <util/platform.h>
#include <string>
#include <string_view>
#include <vector>
// macOS Third-Party Plugin Locations
//
// * User Path: <Environment Variable>
// * Binary: <Environment Variable>/<Plugin>.plugin/Contents/MacOS
// * Data: <Environment Variable>/<Plugin>.plugin/Contents/Resources
//
// * Root Path: <User>/Library/Application Support
// * Binary: <Root Path>/obs-studio/plugins/<Plugin>.plugin/Contents/MacOS/<Plugin>
// * Data: <Root Path>/obs-studio/plugins/<Plugin>.plugin/Contents/Resources
//
// * Legacy User Path: <Environment Variable> + <Data Environment Variable>
// * Binary: <Environment Variable>/<Plugin>.plugin/Contents/MacOS/<Plugin>
// * Data: <Data Environment Variable>/<Plugin>.plugin/Contents/Resources
//
// * Legacy System Root Path: /Library/Application Support
// * Legacy binary: <Legacy System Root Path>/obs-studio/plugins/<Plugin>/bin/<Plugin>.so
// * Legacy data: <Legacy System Root Path>/obs-studio/plugins/<Plugin>/data
//
// * Legacy User Root Path: <User>/Library/Application Support
// * Legacy binary: <Legacy User Root Path>/obs-studio/plugins/<Plugin>/bin/<Plugin>.so
// * Legacy data: <Legacy User Root Path>/obs-studio/plugins/<Plugin>/data
//
using State = OBS::PluginManager::State;
using ModuleType = obs_runtime_module_type;
constexpr std::string_view kPluginPathSuffix {"obs-studio/plugins/%module%.plugin"};
constexpr std::string_view kUserPluginPathSuffix {"/%module%.plugin"};
constexpr std::string_view kLegacyPluginPathSuffix {"obs-studio/plugins/%module%"};
#ifdef __aarch64__
constexpr bool kIsAppleSilicon = true;
#else
constexpr bool kIsAppleSilicon = false;
#endif
namespace {
State loadPluginsFromPath(const std::string &pathString, ModuleType type, ModuleList &failedModules)
{
std::string binaryPath {pathString};
std::string dataPath {pathString};
switch (type) {
case OBS_MODULE_TYPE_LEGACY_PLUGIN: {
binaryPath.append("/bin");
dataPath.append("/data");
break;
}
default: {
binaryPath.append("/Contents/MacOS");
dataPath.append("/Contents/Resources");
break;
}
}
ModuleLoadInfo info = {
.path_info = {.binary = binaryPath.c_str(), .data = dataPath.c_str()},
.type = type,
.name = nullptr
};
int failedModuleCount = loadPluginsByInfo(info, failedModules);
State result = (failedModuleCount > 0) ? State::PartialFailure : State::Success;
return result;
}
State loadPluginsFromLegacyPaths(const std::string &binaryPathString, const std::string &dataPathString,
ModuleList &failedModules)
{
std::string binaryPath {binaryPathString};
std::string dataPath {dataPathString};
binaryPath.append("/%module%.plugin/Contents/MacOS");
dataPath.append("/%module%.plugin/Contents/Resources");
ModuleLoadInfo info = {
.path_info = {.binary = binaryPath.c_str(), .data = dataPath.c_str()},
.type = OBS_MODULE_TYPE_LEGACY_PLUGIN,
.name = nullptr
};
int failedModuleCount = loadPluginsByInfo(info, failedModules);
State result = (failedModuleCount > 0) ? State::PartialFailure : State::Success;
return result;
}
} // namespace
namespace OBS {
State PluginManager::loadPlugins(bool usePortableMode __unused)
{
State userPluginState = State::Failure;
std::string userPluginPath = getEnvironmentVariable(kPathVariable);
if (!userPluginPath.empty()) {
userPluginPath.append(kUserPluginPathSuffix);
userPluginState = loadPluginsFromPath(userPluginPath, OBS_MODULE_TYPE_PLUGIN, failedModules_);
} else {
userPluginState = State::Success;
}
State libraryPluginState = State::Failure;
std::string appLibraryPath {};
appLibraryPath.resize(PATH_MAX);
int bufferSize = os_get_config_path(appLibraryPath.data(), appLibraryPath.size(), kPluginPathSuffix.data());
if (bufferSize > 0) {
appLibraryPath.resize(bufferSize);
libraryPluginState = loadPluginsFromPath(appLibraryPath, OBS_MODULE_TYPE_PLUGIN, failedModules_);
}
return (userPluginState && libraryPluginState) ? State::Success : State::PartialFailure;
}
State PluginManager::loadLegacyPlugins(bool usePortableMode __unused)
{
State userPluginState = State::Failure;
std::string userPluginPath = getEnvironmentVariable(kLegacyBinaryPathVariable);
std::string userDataPath = getEnvironmentVariable(kLegacyDataPathVariable);
if (!userPluginPath.empty() && !userDataPath.empty()) {
userPluginState = loadPluginsFromLegacyPaths(userPluginPath, userDataPath, failedModules_);
} else {
userPluginState = State::Success;
}
State legacyPluginState = State::Failure;
if constexpr (!kIsAppleSilicon) {
State systemLibraryState = State::Failure;
std::string systemLibraryPath {};
systemLibraryPath.resize(PATH_MAX);
int bufferSize = os_get_program_data_path(systemLibraryPath.data(), systemLibraryPath.size(),
kLegacyPluginPathSuffix.data());
if (bufferSize > 0) {
systemLibraryPath.resize(bufferSize);
systemLibraryState =
loadPluginsFromPath(systemLibraryPath, OBS_MODULE_TYPE_LEGACY_PLUGIN, failedModules_);
}
State appLibrayState = State::Failure;
std::string appLibraryPath {};
appLibraryPath.resize(PATH_MAX);
bufferSize =
os_get_config_path(appLibraryPath.data(), appLibraryPath.size(), kLegacyPluginPathSuffix.data());
if (bufferSize > 0) {
appLibraryPath.resize(bufferSize);
appLibrayState = loadPluginsFromPath(appLibraryPath, OBS_MODULE_TYPE_LEGACY_PLUGIN, failedModules_);
}
legacyPluginState = (systemLibraryState && appLibrayState) ? State::Success : State::PartialFailure;
} else {
legacyPluginState = State::Success;
}
return (userPluginState && legacyPluginState) ? State::Success : State::PartialFailure;
}
} // namespace OBS