28 Log::Warn(
"{} left a {} subscription behind; the host dropped it.", name, event);
30 for (
const std::string& service : leaks.
Services)
32 Log::Warn(
"{} left the service '{}' published; the host withdrew it.", name, service);
34 for (
const uint64_t addonId : leaks.
Addons)
36 Log::Warn(
"{} left workshop addon {} required; the host released it.", name, addonId);
40static std::filesystem::path
LibraryPath(std::string_view name)
60 LoadGroup(installed, [](std::string_view) {
return true; });
61 Log::Info(
"{} of {} installed plugin(s) loaded.", _loaded.size(),
68 while (!_loaded.empty())
70 UnloadOne(_loaded.back().Manifest.Name, UnloadTime::Shutdown);
76 _pending.push_back({.Kind = kind, .Name = std::string(name)});
88 const std::vector<PendingAction> actions = std::move(_pending);
91 for (
const PendingAction& action : actions)
99 RunUnload(action.Name);
102 RunReload(action.Name);
108void PluginLoader::LoadGroup(
const InstalledScan& installed,
const std::function<
bool(std::string_view)>& wanted)
111 for (
const std::vector<RefusedPlugin>* refusals : {&installed.
Refused, &list.
Refused})
113 for (
const RefusedPlugin& refused : *refusals)
115 if (wanted(refused.Name))
117 Refuse(refused.Name, refused.Reason.Detail);
122 for (
const std::string& name : list.
ToLoad)
124 if (!wanted(name) || FindLoaded(name) !=
nullptr)
130 if (
Status loaded = LoadOne(*found); !loaded)
132 Refuse(name, loaded.error().Detail);
137void PluginLoader::Refuse(std::string_view name, std::string reason)
139 Log::Error(
"Refusing '{}': {}", name, reason);
140 _refused.insert_or_assign(std::string(name), std::move(reason));
145 const std::string& name = manifest.Name;
150 return std::unexpected(code.error());
156 return std::unexpected(entry.error());
159 using EntryPoint =
const PluginDescriptor* (*)();
160 const PluginDescriptor* descriptor =
reinterpret_cast<EntryPoint
>(*entry)();
166 PluginContext* view = _registry.AddPlugin(name, manifest.LogTag, manifest.Version);
169 return std::unexpected(
Error::Failed(
"the host already holds a view under that name"));
171 view->SetMinLogLevel(manifest.LogLevel);
174 char failure[512] = {};
175 if (!descriptor->Load(view, failure,
sizeof failure))
177 failure[
sizeof failure - 1] =
'\0';
179 WarnLeaks(name, _registry.RemovePlugin(name));
180 _assets.Unmount(name);
181 return std::unexpected(
Error::Failed(failure[0] !=
'\0' ? failure :
"its Load returned false"));
184 view->ReplayMissedEvents();
186 _loaded.push_back({.Manifest = manifest, .Descriptor = descriptor, .Library = std::move(*code)});
187 _refused.erase(name);
189 Log::Info(
"Loaded {} v{}.", name, manifest.Version);
193void PluginLoader::UnloadOne(std::string_view name, UnloadTime when)
196 const std::string plugin(name);
199 std::ranges::find_if(_loaded, [&](
const LoadedPlugin& loaded) {
return loaded.Manifest.Name == plugin; });
200 if (found == _loaded.end())
205 if (when == UnloadTime::MidMap)
207 _registry.FindPlugin(plugin)->DisconnectClients();
209 found->Descriptor->Unload();
210 WarnLeaks(plugin, _registry.RemovePlugin(plugin));
211 _assets.Unmount(plugin);
214 _loaded.erase(found);
218LoadedPlugin* PluginLoader::FindLoaded(std::string_view name)
221 std::ranges::find_if(_loaded, [name](
const LoadedPlugin& plugin) {
return plugin.Manifest.Name == name; });
222 return found != _loaded.end() ? &*found :
nullptr;
228 if (plugin ==
nullptr)
235std::vector<PluginManifest> PluginLoader::LoadedManifests()
const
237 std::vector<PluginManifest> manifests;
238 manifests.reserve(_loaded.size());
241 manifests.push_back(plugin.Manifest);
246void PluginLoader::RunLoad(std::string_view name)
248 if (FindLoaded(name) !=
nullptr)
250 Log::Warn(
"'{}' is already loaded.", name);
254 const InstalledScan installed =
Installed();
256 broken != installed.Refused.end())
258 Refuse(name, broken->Reason.Detail);
263 if (found == installed.Plugins.end())
265 Log::Warn(
"'{}' is not installed.", name);
269 for (
const std::string& dependency : found->Dependencies)
271 if (FindLoaded(dependency) ==
nullptr)
273 Refuse(name, std::format(
"it requires '{}', which is not loaded.", dependency));
278 if (
Status loaded = LoadOne(*found); !loaded)
280 Refuse(name, loaded.error().Detail);
284void PluginLoader::RunUnload(std::string_view name)
292 if (!dependents.empty())
298 UnloadOne(name, UnloadTime::MidMap);
301void PluginLoader::RunReload(std::string_view name)
310 group.emplace_back(name);
312 std::vector<std::string> going;
313 for (
const LoadedPlugin& plugin : _loaded | std::views::reverse)
315 if (std::ranges::find(group, plugin.Manifest.Name) != group.end())
317 going.push_back(plugin.Manifest.Name);
321 for (
const std::string& plugin : going)
323 UnloadOne(plugin, UnloadTime::MidMap);
328 [&group](std::string_view plugin) {
return std::ranges::find(group, plugin) != group.end(); });
Enumerator names without a hand-written switch.
LoadedPlugin * RequireLoaded(std::string_view name)
PluginLoader(PluginRegistry ®istry, ServerAssets &assets)
void Defer(ActionKind kind, std::string_view name)
The loaded plugins and the engine events raised on them, one per process.
static Result< SharedLibrary > Open(const std::filesystem::path &path)
static std::string Join(const std::vector< std::string > &parts, std::string_view delimiter)
static std::string ToLower(std::string_view str)
InstalledScan Discover(const std::filesystem::path &plugins)
void Error(std::format_string< Args... > fmt, Args &&... args)
void Info(std::format_string< Args... > fmt, Args &&... args)
void Warn(std::format_string< Args... > fmt, Args &&... args)
LoadList Resolve(std::span< const PluginManifest > installed)
Decide which of installed the host loads.
std::vector< std::string > RequiredDependents(std::string_view plugin, std::span< const PluginManifest > loaded)
std::filesystem::path ResolvePath(std::string_view relativePath)
a relative path against the base directory. If the path is already absolute, returns it as-is.
constexpr std::string_view Name(E value) noexcept
static constexpr std::string_view LibrarySuffix
std::expected< void, Error > Status
std::expected< T, Error > Result
static std::filesystem::path LibraryPath(std::string_view name)
Status ValidateDescriptor(const PluginDescriptor *descriptor, std::string_view hostVersion)
std::string PluginFile(std::string_view plugin, std::string_view relative)
"addons/voltmod/plugins/<plugin>/<relative>".
constexpr const char * PluginEntryName
static void WarnLeaks(std::string_view name, const LeakReport &leaks)
static InstalledScan Installed()
static Error Failed(std::string detail)
std::vector< PluginManifest > Plugins
std::vector< RefusedPlugin > Refused
std::vector< std::string_view > Subscriptions
the event each was taken on
std::vector< std::string > Services
std::vector< uint64_t > Addons
std::vector< std::string > ToLoad
std::vector< RefusedPlugin > Refused