-
-
Notifications
You must be signed in to change notification settings - Fork 191
Feature: Add the ability to order plugins in directories #1031
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
roflmuffin
merged 7 commits into
roflmuffin:main
from
Ravid-A:feature/directory-travesal
Oct 30, 2025
+258
−219
Merged
Changes from 2 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
ade5e21
feat: enable the option to order plugins in sub directories
Ravid-A 6cef466
Merge branch 'main' into feature/directory-travesal
Ravid-A ccd63ae
del: remove the limitation of load plugins from the disabled folder
Ravid-A d0f6d99
Merge branch 'main' into feature/directory-travesal
roflmuffin 5c1f589
Merge branch 'main' into feature/directory-travesal
Ravid-A 5b8c1f7
Merge branch 'main' into feature/directory-travesal
Ravid-A 0a9f9f7
Merge branch 'main' into feature/directory-travesal
roflmuffin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
285 changes: 159 additions & 126 deletions
285
managed/CounterStrikeSharp.API/Core/Plugin/Host/PluginManager.cs
Ravid-A marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,127 +1,160 @@ | ||
| using System.Collections.Generic; | ||
| using System.Linq; | ||
| using System.Reflection; | ||
| using System.Runtime.Loader; | ||
| using CounterStrikeSharp.API.Core.Capabilities; | ||
| using CounterStrikeSharp.API.Core.Commands; | ||
| using CounterStrikeSharp.API.Core.Hosting; | ||
| using McMaster.NETCore.Plugins; | ||
| using Microsoft.Extensions.DependencyInjection; | ||
| using Microsoft.Extensions.Logging; | ||
|
|
||
| namespace CounterStrikeSharp.API.Core.Plugin.Host; | ||
|
|
||
| public class PluginManager : IPluginManager | ||
| { | ||
| private readonly HashSet<PluginContext> _loadedPluginContexts = new(); | ||
| private readonly IScriptHostConfiguration _scriptHostConfiguration; | ||
| private readonly ICommandManager _commandManager; | ||
| private readonly IServiceProvider _serviceProvider; | ||
| private readonly ILogger<PluginManager> _logger; | ||
| private readonly Dictionary<string, Assembly> _sharedAssemblies = new(); | ||
| private bool _loadedSharedLibs = false; | ||
|
|
||
| public PluginManager(IScriptHostConfiguration scriptHostConfiguration, ICommandManager commandManager, | ||
| ILogger<PluginManager> logger, IServiceProvider serviceProvider, IServiceScopeFactory serviceScopeFactory) | ||
| { | ||
| _scriptHostConfiguration = scriptHostConfiguration; | ||
| _commandManager = commandManager; | ||
| _logger = logger; | ||
| _serviceProvider = serviceProvider; | ||
| } | ||
|
|
||
| private void LoadLibrary(string path) | ||
| { | ||
| var loader = PluginLoader.CreateFromAssemblyFile(path, new[] { typeof(IPlugin), typeof(PluginCapability<>), typeof(PlayerCapability<>) }, | ||
| config => { config.PreferSharedTypes = true; }); | ||
| var assembly = loader.LoadDefaultAssembly(); | ||
|
|
||
| _sharedAssemblies[assembly.GetName().Name] = assembly; | ||
| } | ||
|
|
||
| private void LoadSharedLibraries() | ||
| { | ||
| var sharedDirectory = Directory.GetDirectories(_scriptHostConfiguration.SharedPath); | ||
| var sharedAssemblyPaths = sharedDirectory | ||
| .Select(dir => Path.Combine(dir, Path.GetFileName(dir) + ".dll")) | ||
| .Where(File.Exists) | ||
| .ToArray(); | ||
|
|
||
| foreach (var sharedAssemblyPath in sharedAssemblyPaths) | ||
| { | ||
| try | ||
| { | ||
| LoadLibrary(sharedAssemblyPath); | ||
| } | ||
| catch (Exception e) | ||
| { | ||
| _logger.LogError(e, "Failed to load shared assembly from {Path}", sharedAssemblyPath); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| public void Load() | ||
| { | ||
| var pluginDirectories = Directory.GetDirectories(_scriptHostConfiguration.PluginPath); | ||
| var pluginAssemblyPaths = pluginDirectories | ||
| .Select(dir => Path.Combine(dir, Path.GetFileName(dir) + ".dll")) | ||
| .Where(File.Exists) | ||
| .ToArray(); | ||
|
|
||
| AssemblyLoadContext.Default.Resolving += (context, name) => | ||
| { | ||
| if (!_loadedSharedLibs) | ||
| { | ||
| LoadSharedLibraries(); | ||
| _loadedSharedLibs = true; | ||
| } | ||
|
|
||
| if (!_sharedAssemblies.TryGetValue(name.Name, out var assembly)) | ||
| { | ||
| return null; | ||
| } | ||
|
|
||
| return assembly; | ||
| }; | ||
|
|
||
| if (CoreConfig.PluginAutoLoadEnabled) | ||
| { | ||
| foreach (var path in pluginAssemblyPaths) | ||
| { | ||
| try | ||
| { | ||
| LoadPlugin(path); | ||
| } | ||
| catch (Exception e) | ||
| { | ||
| _logger.LogError(e, "Failed to load plugin from {Path}", path); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| foreach (var plugin in _loadedPluginContexts) | ||
| { | ||
| try | ||
| { | ||
| plugin.Plugin?.OnAllPluginsLoaded(false); | ||
| } | ||
| catch (Exception e) | ||
| { | ||
| _logger.LogError(e, "OnAllPluginsLoaded failed"); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| public IEnumerable<PluginContext> GetLoadedPlugins() | ||
| { | ||
| return _loadedPluginContexts; | ||
| } | ||
|
|
||
| public void LoadPlugin(string path) | ||
| { | ||
| var plugin = new PluginContext(_serviceProvider, _commandManager, _scriptHostConfiguration, path, | ||
| _loadedPluginContexts.Select(x => x.PluginId).DefaultIfEmpty(0).Max() + 1); | ||
| _loadedPluginContexts.Add(plugin); | ||
| plugin.Load(); | ||
| } | ||
| using System.Collections.Generic; | ||
| using System.Linq; | ||
| using System.Reflection; | ||
| using System.Runtime.Loader; | ||
| using CounterStrikeSharp.API.Core.Capabilities; | ||
| using CounterStrikeSharp.API.Core.Commands; | ||
| using CounterStrikeSharp.API.Core.Hosting; | ||
| using McMaster.NETCore.Plugins; | ||
| using Microsoft.Extensions.DependencyInjection; | ||
| using Microsoft.Extensions.Logging; | ||
|
|
||
| namespace CounterStrikeSharp.API.Core.Plugin.Host; | ||
|
|
||
| public class PluginManager : IPluginManager | ||
| { | ||
| private readonly HashSet<PluginContext> _loadedPluginContexts = new(); | ||
| private readonly IScriptHostConfiguration _scriptHostConfiguration; | ||
| private readonly ICommandManager _commandManager; | ||
| private readonly IServiceProvider _serviceProvider; | ||
| private readonly ILogger<PluginManager> _logger; | ||
| private readonly Dictionary<string, Assembly> _sharedAssemblies = new(); | ||
| private bool _loadedSharedLibs = false; | ||
|
|
||
| public PluginManager(IScriptHostConfiguration scriptHostConfiguration, ICommandManager commandManager, | ||
| ILogger<PluginManager> logger, IServiceProvider serviceProvider, IServiceScopeFactory serviceScopeFactory) | ||
| { | ||
| _scriptHostConfiguration = scriptHostConfiguration; | ||
| _commandManager = commandManager; | ||
| _logger = logger; | ||
| _serviceProvider = serviceProvider; | ||
| } | ||
|
|
||
| private void LoadLibrary(string path) | ||
| { | ||
| var loader = PluginLoader.CreateFromAssemblyFile(path, new[] { typeof(IPlugin), typeof(PluginCapability<>), typeof(PlayerCapability<>) }, | ||
| config => { config.PreferSharedTypes = true; }); | ||
| var assembly = loader.LoadDefaultAssembly(); | ||
|
|
||
| _sharedAssemblies[assembly.GetName().Name] = assembly; | ||
| } | ||
|
|
||
| private void LoadSharedLibraries() | ||
| { | ||
| var sharedDirectory = Directory.GetDirectories(_scriptHostConfiguration.SharedPath); | ||
| var sharedAssemblyPaths = sharedDirectory | ||
| .Select(dir => Path.Combine(dir, Path.GetFileName(dir) + ".dll")) | ||
| .Where(File.Exists) | ||
| .ToArray(); | ||
|
|
||
| foreach (var sharedAssemblyPath in sharedAssemblyPaths) | ||
| { | ||
| try | ||
| { | ||
| LoadLibrary(sharedAssemblyPath); | ||
| } | ||
| catch (Exception e) | ||
| { | ||
| _logger.LogError(e, "Failed to load shared assembly from {Path}", sharedAssemblyPath); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| public void Load() | ||
| { | ||
| var pluginAssemblyPaths = GetPluginsAssemblyPaths(); | ||
|
|
||
| AssemblyLoadContext.Default.Resolving += (context, name) => | ||
| { | ||
| if (!_loadedSharedLibs) | ||
| { | ||
| LoadSharedLibraries(); | ||
| _loadedSharedLibs = true; | ||
| } | ||
|
|
||
| if (!_sharedAssemblies.TryGetValue(name.Name, out var assembly)) | ||
| { | ||
| return null; | ||
| } | ||
|
|
||
| return assembly; | ||
| }; | ||
|
|
||
| if (CoreConfig.PluginAutoLoadEnabled) | ||
| { | ||
| foreach (var path in pluginAssemblyPaths) | ||
| { | ||
| try | ||
| { | ||
| LoadPlugin(path); | ||
| } | ||
| catch (Exception e) | ||
| { | ||
| _logger.LogError(e, "Failed to load plugin from {Path}", path); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| foreach (var plugin in _loadedPluginContexts) | ||
| { | ||
| try | ||
| { | ||
| plugin.Plugin?.OnAllPluginsLoaded(false); | ||
| } | ||
| catch (Exception e) | ||
| { | ||
| _logger.LogError(e, "OnAllPluginsLoaded failed"); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| public IEnumerable<PluginContext> GetLoadedPlugins() | ||
| { | ||
| return _loadedPluginContexts; | ||
| } | ||
|
|
||
| public void LoadPlugin(string path) | ||
| { | ||
| var plugin = new PluginContext(_serviceProvider, _commandManager, _scriptHostConfiguration, path, | ||
| _loadedPluginContexts.Select(x => x.PluginId).DefaultIfEmpty(0).Max() + 1); | ||
| _loadedPluginContexts.Add(plugin); | ||
| plugin.Load(); | ||
| } | ||
|
|
||
| private string[] GetPluginsAssemblyPaths() | ||
| { | ||
| // Skip "disabled" at root level | ||
| var rootSubDirs = Directory.GetDirectories(_scriptHostConfiguration.PluginPath) | ||
| .Where(d => !Path.GetFileName(d).Equals("disabled", StringComparison.OrdinalIgnoreCase)); | ||
|
|
||
| var pluginDirectories = new List<string>(); | ||
|
|
||
| foreach (var subDir in rootSubDirs) | ||
| { | ||
| var stack = new Stack<string>(); | ||
| stack.Push(subDir); | ||
|
|
||
| while (stack.Count > 0) | ||
| { | ||
| var currentDir = stack.Pop(); | ||
| var dirName = Path.GetFileName(currentDir); | ||
| var expectedDll = Path.Combine(currentDir, dirName + ".dll"); | ||
|
|
||
| if (File.Exists(expectedDll)) | ||
| { | ||
| pluginDirectories.Add(currentDir); | ||
| // Stop scanning deeper in this directory | ||
| continue; | ||
| } | ||
|
|
||
| // Add subdirectories to stack for further scanning | ||
| foreach (var child in Directory.GetDirectories(currentDir)) | ||
| stack.Push(child); | ||
| } | ||
| } | ||
|
|
||
| return pluginDirectories | ||
| .Select(d => Path.Combine(d, Path.GetFileName(d) + ".dll")) | ||
| .ToArray(); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.