2017-06-12 17:47:31 -04:00
|
|
|
|
using System;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Reflection;
|
2018-04-08 02:44:42 -04:00
|
|
|
|
using SharedLibraryCore.Interfaces;
|
2018-08-23 17:16:30 -04:00
|
|
|
|
using System.Linq;
|
2020-01-31 21:15:07 -05:00
|
|
|
|
using SharedLibraryCore;
|
2020-02-11 17:44:06 -05:00
|
|
|
|
using IW4MAdmin.Application.Misc;
|
2017-06-12 17:47:31 -04:00
|
|
|
|
|
2020-01-31 21:15:07 -05:00
|
|
|
|
namespace IW4MAdmin.Application.Helpers
|
2017-06-12 17:47:31 -04:00
|
|
|
|
{
|
2020-02-11 17:44:06 -05:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// implementation of IPluginImporter
|
|
|
|
|
/// discovers plugins and script plugins
|
|
|
|
|
/// </summary>
|
2020-01-31 21:15:07 -05:00
|
|
|
|
public class PluginImporter : IPluginImporter
|
2017-06-12 17:47:31 -04:00
|
|
|
|
{
|
2020-02-11 17:44:06 -05:00
|
|
|
|
private static readonly string PLUGIN_DIR = "Plugins";
|
2020-01-31 21:15:07 -05:00
|
|
|
|
private readonly ILogger _logger;
|
|
|
|
|
|
2020-02-11 17:44:06 -05:00
|
|
|
|
public PluginImporter(ILogger logger)
|
2020-01-31 21:15:07 -05:00
|
|
|
|
{
|
|
|
|
|
_logger = logger;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2020-02-11 17:44:06 -05:00
|
|
|
|
/// discovers all the script plugins in the plugins dir
|
2020-01-31 21:15:07 -05:00
|
|
|
|
/// </summary>
|
2020-02-11 17:44:06 -05:00
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public IEnumerable<IPlugin> DiscoverScriptPlugins()
|
2017-06-12 17:47:31 -04:00
|
|
|
|
{
|
2020-02-11 17:44:06 -05:00
|
|
|
|
string pluginDir = $"{Utilities.OperatingDirectory}{PLUGIN_DIR}{Path.DirectorySeparatorChar}";
|
2018-08-28 17:32:59 -04:00
|
|
|
|
|
|
|
|
|
if (Directory.Exists(pluginDir))
|
|
|
|
|
{
|
2020-02-11 17:44:06 -05:00
|
|
|
|
string[] scriptPluginFiles = Directory.GetFiles(pluginDir, "*.js");
|
2018-08-28 17:32:59 -04:00
|
|
|
|
|
2020-02-11 17:44:06 -05:00
|
|
|
|
_logger.WriteInfo($"Discovered {scriptPluginFiles.Length} potential script plugins");
|
2017-06-12 17:47:31 -04:00
|
|
|
|
|
2020-02-11 17:44:06 -05:00
|
|
|
|
if (scriptPluginFiles.Length > 0)
|
|
|
|
|
{
|
|
|
|
|
foreach (string fileName in scriptPluginFiles)
|
|
|
|
|
{
|
|
|
|
|
_logger.WriteInfo($"Discovered script plugin {fileName}");
|
|
|
|
|
var plugin = new ScriptPlugin(fileName);
|
|
|
|
|
yield return plugin;
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-06-12 17:47:31 -04:00
|
|
|
|
}
|
2020-02-11 17:44:06 -05:00
|
|
|
|
}
|
2017-06-12 17:47:31 -04:00
|
|
|
|
|
2020-02-11 17:44:06 -05:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// discovers all the C# assembly plugins and commands
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public (IEnumerable<Type>, IEnumerable<Type>) DiscoverAssemblyPluginImplementations()
|
|
|
|
|
{
|
|
|
|
|
string pluginDir = $"{Utilities.OperatingDirectory}{PLUGIN_DIR}{Path.DirectorySeparatorChar}";
|
|
|
|
|
var pluginTypes = Enumerable.Empty<Type>();
|
|
|
|
|
var commandTypes = Enumerable.Empty<Type>();
|
2018-08-26 20:20:47 -04:00
|
|
|
|
|
2020-02-11 17:44:06 -05:00
|
|
|
|
if (Directory.Exists(pluginDir))
|
2017-06-12 17:47:31 -04:00
|
|
|
|
{
|
2020-02-11 17:44:06 -05:00
|
|
|
|
var dllFileNames = Directory.GetFiles(pluginDir, "*.dll");
|
|
|
|
|
_logger.WriteInfo($"Discovered {dllFileNames.Length} potential plugin assemblies");
|
2017-06-12 17:47:31 -04:00
|
|
|
|
|
2020-02-11 17:44:06 -05:00
|
|
|
|
if (dllFileNames.Length > 0)
|
2017-06-12 17:47:31 -04:00
|
|
|
|
{
|
2020-02-11 17:44:06 -05:00
|
|
|
|
var assemblies = dllFileNames.Select(_name => Assembly.LoadFrom(_name));
|
2017-06-12 17:47:31 -04:00
|
|
|
|
|
2020-02-11 17:44:06 -05:00
|
|
|
|
pluginTypes = assemblies
|
|
|
|
|
.SelectMany(_asm => _asm.GetTypes())
|
|
|
|
|
.Where(_assemblyType => _assemblyType.GetInterface(nameof(IPlugin), false) != null);
|
2017-06-12 17:47:31 -04:00
|
|
|
|
|
2020-02-11 17:44:06 -05:00
|
|
|
|
_logger.WriteInfo($"Discovered {pluginTypes.Count()} plugin implementations");
|
2017-06-12 17:47:31 -04:00
|
|
|
|
|
2020-02-11 17:44:06 -05:00
|
|
|
|
commandTypes = assemblies
|
|
|
|
|
.SelectMany(_asm => _asm.GetTypes())
|
|
|
|
|
.Where(_assemblyType => _assemblyType.IsClass && _assemblyType.BaseType == typeof(Command));
|
|
|
|
|
|
|
|
|
|
_logger.WriteInfo($"Discovered {commandTypes.Count()} plugin commands");
|
2017-06-12 17:47:31 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
2020-01-31 21:15:07 -05:00
|
|
|
|
|
2020-02-11 17:44:06 -05:00
|
|
|
|
return (pluginTypes, commandTypes);
|
2017-06-12 17:47:31 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|