From 3072d624a60ff28922e8507ba9799c2f5d57ddff Mon Sep 17 00:00:00 2001 From: goat Date: Wed, 15 Feb 2023 19:51:36 +0100 Subject: [PATCH] feat: add PluginManager::FindCallingPlugin() --- Dalamud/Plugin/Internal/PluginManager.cs | 29 ++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/Dalamud/Plugin/Internal/PluginManager.cs b/Dalamud/Plugin/Internal/PluginManager.cs index a59e83a72..e6fbade38 100644 --- a/Dalamud/Plugin/Internal/PluginManager.cs +++ b/Dalamud/Plugin/Internal/PluginManager.cs @@ -1299,6 +1299,35 @@ Thanks and have fun!"; return this.bannedPlugins.LastOrDefault(ban => ban.Name == manifest.InternalName).Reason; } + /// + /// Get the plugin that called this method by walking the stack, + /// or null, if it cannot be determined. + /// At the time, this is naive and shouldn't be used for security-critical checks. + /// + /// The calling plugin, or null. + public LocalPlugin? FindCallingPlugin() + { + var trace = new StackTrace(); + foreach (var frame in trace.GetFrames()) + { + var declaringType = frame.GetMethod()?.DeclaringType; + if (declaringType == null) + continue; + + lock (this.pluginListLock) + { + foreach (var plugin in this.InstalledPlugins) + { + if (plugin.AssemblyName != null && + plugin.AssemblyName.FullName == declaringType.Assembly.GetName().FullName) + return plugin; + } + } + } + + return null; + } + private void DetectAvailablePluginUpdates() { var updatablePlugins = new List();