2017-06-12 17:47:31 -04:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2023-04-04 19:24:13 -04:00
|
|
|
|
using System.IO;
|
2018-08-23 17:16:30 -04:00
|
|
|
|
using System.Linq;
|
2023-04-04 19:24:13 -04:00
|
|
|
|
using System.Reflection;
|
|
|
|
|
using System.Text.RegularExpressions;
|
2020-10-24 16:02:38 -04:00
|
|
|
|
using IW4MAdmin.Application.API.Master;
|
2020-11-11 18:31:26 -05:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
2023-04-04 19:24:13 -04:00
|
|
|
|
using SharedLibraryCore;
|
2020-10-24 16:02:38 -04:00
|
|
|
|
using SharedLibraryCore.Configuration;
|
2023-04-04 19:24:13 -04:00
|
|
|
|
using SharedLibraryCore.Interfaces;
|
2020-11-11 18:31:26 -05:00
|
|
|
|
using ILogger = Microsoft.Extensions.Logging.ILogger;
|
2017-06-12 17:47:31 -04:00
|
|
|
|
|
2023-04-04 19:24:13 -04:00
|
|
|
|
namespace IW4MAdmin.Application.Plugin
|
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-10-24 16:02:38 -04:00
|
|
|
|
private IEnumerable<PluginSubscriptionContent> _pluginSubscription;
|
2023-04-04 19:24:13 -04:00
|
|
|
|
private static readonly string PluginDir = "Plugins";
|
|
|
|
|
private const string PluginV2Match = "^ *((?:var|const|let) +init)|function init";
|
2020-01-31 21:15:07 -05:00
|
|
|
|
private readonly ILogger _logger;
|
2020-10-24 16:02:38 -04:00
|
|
|
|
private readonly IRemoteAssemblyHandler _remoteAssemblyHandler;
|
|
|
|
|
private readonly IMasterApi _masterApi;
|
|
|
|
|
private readonly ApplicationConfiguration _appConfig;
|
2020-01-31 21:15:07 -05:00
|
|
|
|
|
2023-04-04 19:24:13 -04:00
|
|
|
|
public PluginImporter(ILogger<PluginImporter> logger, ApplicationConfiguration appConfig, IMasterApi masterApi,
|
|
|
|
|
IRemoteAssemblyHandler remoteAssemblyHandler)
|
2020-01-31 21:15:07 -05:00
|
|
|
|
{
|
|
|
|
|
_logger = logger;
|
2020-10-24 16:02:38 -04:00
|
|
|
|
_masterApi = masterApi;
|
|
|
|
|
_remoteAssemblyHandler = remoteAssemblyHandler;
|
|
|
|
|
_appConfig = appConfig;
|
2020-01-31 21:15:07 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <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>
|
2023-04-04 19:24:13 -04:00
|
|
|
|
public IEnumerable<(Type, string)> DiscoverScriptPlugins()
|
2017-06-12 17:47:31 -04:00
|
|
|
|
{
|
2023-04-04 19:24:13 -04:00
|
|
|
|
var pluginDir = $"{Utilities.OperatingDirectory}{PluginDir}{Path.DirectorySeparatorChar}";
|
2018-08-28 17:32:59 -04:00
|
|
|
|
|
2022-02-07 19:43:36 -05:00
|
|
|
|
if (!Directory.Exists(pluginDir))
|
2018-08-28 17:32:59 -04:00
|
|
|
|
{
|
2023-04-04 19:24:13 -04:00
|
|
|
|
return Enumerable.Empty<(Type, string)>();
|
2022-02-07 19:43:36 -05:00
|
|
|
|
}
|
2018-08-28 17:32:59 -04:00
|
|
|
|
|
2022-02-07 19:43:36 -05:00
|
|
|
|
var scriptPluginFiles =
|
|
|
|
|
Directory.GetFiles(pluginDir, "*.js").AsEnumerable().Union(GetRemoteScripts()).ToList();
|
2017-06-12 17:47:31 -04:00
|
|
|
|
|
2023-04-04 19:24:13 -04:00
|
|
|
|
var bothVersionPlugins = scriptPluginFiles.Select(fileName =>
|
2022-02-07 19:43:36 -05:00
|
|
|
|
{
|
2023-04-04 19:24:13 -04:00
|
|
|
|
_logger.LogDebug("Discovered script plugin {FileName}", fileName);
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var fileContents = File.ReadAllLines(fileName);
|
|
|
|
|
var isValidV2 = fileContents.Any(line => Regex.IsMatch(line, PluginV2Match));
|
|
|
|
|
return isValidV2 ? (typeof(IPluginV2), fileName) : (typeof(IPlugin), fileName);
|
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
return (typeof(IPlugin), fileName);
|
|
|
|
|
}
|
2022-02-07 19:43:36 -05:00
|
|
|
|
}).ToList();
|
2023-04-04 19:24:13 -04:00
|
|
|
|
|
|
|
|
|
return bothVersionPlugins;
|
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>
|
2021-03-22 12:09:25 -04:00
|
|
|
|
public (IEnumerable<Type>, IEnumerable<Type>, IEnumerable<Type>) DiscoverAssemblyPluginImplementations()
|
2020-02-11 17:44:06 -05:00
|
|
|
|
{
|
2023-04-04 19:24:13 -04:00
|
|
|
|
var pluginDir = $"{Utilities.OperatingDirectory}{PluginDir}{Path.DirectorySeparatorChar}";
|
2020-02-11 17:44:06 -05:00
|
|
|
|
var pluginTypes = Enumerable.Empty<Type>();
|
|
|
|
|
var commandTypes = Enumerable.Empty<Type>();
|
2021-03-22 12:09:25 -04:00
|
|
|
|
var configurationTypes = 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");
|
2023-04-04 19:24:13 -04:00
|
|
|
|
_logger.LogDebug("Discovered {Count} potential plugin assemblies", dllFileNames.Length);
|
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-10-24 16:02:38 -04:00
|
|
|
|
// we only want to load the most recent assembly in case of duplicates
|
2023-04-04 19:24:13 -04:00
|
|
|
|
var assemblies = dllFileNames.Select(name => Assembly.LoadFrom(name))
|
2020-10-24 16:02:38 -04:00
|
|
|
|
.Union(GetRemoteAssemblies())
|
2023-04-04 19:24:13 -04:00
|
|
|
|
.GroupBy(assembly => assembly.FullName).Select(assembly => assembly.OrderByDescending(asm => asm.GetName().Version).First());
|
2017-06-12 17:47:31 -04:00
|
|
|
|
|
2020-02-11 17:44:06 -05:00
|
|
|
|
pluginTypes = assemblies
|
2023-04-04 19:24:13 -04:00
|
|
|
|
.SelectMany(asm =>
|
2022-01-26 11:32:16 -05:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2023-04-04 19:24:13 -04:00
|
|
|
|
return asm.GetTypes();
|
2022-01-26 11:32:16 -05:00
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
return Enumerable.Empty<Type>();
|
|
|
|
|
}
|
|
|
|
|
})
|
2023-04-04 19:24:13 -04:00
|
|
|
|
.Where(assemblyType => (assemblyType.GetInterface(nameof(IPlugin), false) ?? assemblyType.GetInterface(nameof(IPluginV2), false)) != null)
|
|
|
|
|
.Where(assemblyType => !assemblyType.Namespace?.StartsWith(nameof(SharedLibraryCore)) ?? false);
|
2017-06-12 17:47:31 -04:00
|
|
|
|
|
2020-11-11 18:31:26 -05:00
|
|
|
|
_logger.LogDebug("Discovered {count} plugin implementations", pluginTypes.Count());
|
2017-06-12 17:47:31 -04:00
|
|
|
|
|
2020-02-11 17:44:06 -05:00
|
|
|
|
commandTypes = assemblies
|
2023-04-04 19:24:13 -04:00
|
|
|
|
.SelectMany(asm =>{
|
2022-01-26 11:32:16 -05:00
|
|
|
|
try
|
|
|
|
|
{
|
2023-04-04 19:24:13 -04:00
|
|
|
|
return asm.GetTypes();
|
2022-01-26 11:32:16 -05:00
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
return Enumerable.Empty<Type>();
|
|
|
|
|
}
|
|
|
|
|
})
|
2023-04-04 19:24:13 -04:00
|
|
|
|
.Where(assemblyType => assemblyType.IsClass && assemblyType.BaseType == typeof(Command))
|
|
|
|
|
.Where(assemblyType => !assemblyType.Namespace?.StartsWith(nameof(SharedLibraryCore)) ?? false);
|
2020-02-11 17:44:06 -05:00
|
|
|
|
|
2023-04-04 19:24:13 -04:00
|
|
|
|
_logger.LogDebug("Discovered {Count} plugin commands", commandTypes.Count());
|
2021-03-22 12:09:25 -04:00
|
|
|
|
|
|
|
|
|
configurationTypes = assemblies
|
2022-01-26 11:32:16 -05:00
|
|
|
|
.SelectMany(asm => {
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
return asm.GetTypes();
|
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
return Enumerable.Empty<Type>();
|
|
|
|
|
}
|
|
|
|
|
})
|
2021-03-22 12:09:25 -04:00
|
|
|
|
.Where(asmType =>
|
2023-04-04 19:24:13 -04:00
|
|
|
|
asmType.IsClass && asmType.GetInterface(nameof(IBaseConfiguration), false) != null)
|
|
|
|
|
.Where(assemblyType => !assemblyType.Namespace?.StartsWith(nameof(SharedLibraryCore)) ?? false);
|
2021-03-22 12:09:25 -04:00
|
|
|
|
|
2023-04-04 19:24:13 -04:00
|
|
|
|
_logger.LogDebug("Discovered {Count} configuration implementations", configurationTypes.Count());
|
2017-06-12 17:47:31 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
2020-01-31 21:15:07 -05:00
|
|
|
|
|
2021-03-22 12:09:25 -04:00
|
|
|
|
return (pluginTypes, commandTypes, configurationTypes);
|
2017-06-12 17:47:31 -04:00
|
|
|
|
}
|
2020-10-24 16:02:38 -04:00
|
|
|
|
|
|
|
|
|
private IEnumerable<Assembly> GetRemoteAssemblies()
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
if (_pluginSubscription == null)
|
|
|
|
|
_pluginSubscription = _masterApi.GetPluginSubscription(Guid.Parse(_appConfig.Id), _appConfig.SubscriptionId).Result;
|
|
|
|
|
|
|
|
|
|
return _remoteAssemblyHandler.DecryptAssemblies(_pluginSubscription.Where(sub => sub.Type == PluginType.Binary).Select(sub => sub.Content).ToArray());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2020-11-11 18:31:26 -05:00
|
|
|
|
_logger.LogWarning(ex, "Could not load remote assemblies");
|
2020-10-24 16:02:38 -04:00
|
|
|
|
return Enumerable.Empty<Assembly>();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private IEnumerable<string> GetRemoteScripts()
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2023-04-04 19:24:13 -04:00
|
|
|
|
_pluginSubscription ??= _masterApi.GetPluginSubscription(Guid.Parse(_appConfig.Id), _appConfig.SubscriptionId).Result;
|
2020-10-24 16:02:38 -04:00
|
|
|
|
|
|
|
|
|
return _remoteAssemblyHandler.DecryptScripts(_pluginSubscription.Where(sub => sub.Type == PluginType.Script).Select(sub => sub.Content).ToArray());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2020-11-11 18:31:26 -05:00
|
|
|
|
_logger.LogWarning(ex,"Could not load remote scripts");
|
2020-10-24 16:02:38 -04:00
|
|
|
|
return Enumerable.Empty<string>();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public enum PluginType
|
|
|
|
|
{
|
|
|
|
|
Binary,
|
|
|
|
|
Script
|
2017-06-12 17:47:31 -04:00
|
|
|
|
}
|
|
|
|
|
}
|