mirror of
https://github.com/RaidMax/IW4M-Admin.git
synced 2025-06-10 15:20:48 -05:00
Finish implementation of configuable command permissions
This commit is contained in:
@ -1317,19 +1317,21 @@ namespace SharedLibraryCore.Commands
|
||||
/// </summary>
|
||||
public class ListPluginsCommand : Command
|
||||
{
|
||||
public ListPluginsCommand(CommandConfiguration config, ITranslationLookup translationLookup) : base(config, translationLookup)
|
||||
private readonly IPluginImporter _pluginImporter;
|
||||
public ListPluginsCommand(CommandConfiguration config, ITranslationLookup translationLookup, IPluginImporter pluginImporter) : base(config, translationLookup)
|
||||
{
|
||||
Name = "plugins";
|
||||
Description = _translationLookup["COMMANDS_PLUGINS_DESC"];
|
||||
Alias = "p";
|
||||
Permission = Permission.Administrator;
|
||||
RequiresTarget = false;
|
||||
_pluginImporter = pluginImporter;
|
||||
}
|
||||
|
||||
public override Task ExecuteAsync(GameEvent E)
|
||||
{
|
||||
E.Origin.Tell(_translationLookup["COMMANDS_PLUGINS_LOADED"]);
|
||||
foreach (var P in Plugins.PluginImporter.ActivePlugins)
|
||||
foreach (var P in _pluginImporter.ActivePlugins)
|
||||
{
|
||||
E.Origin.Tell(string.Format("^3{0} ^7[v^3{1}^7] by ^5{2}^7", P.Name, P.Version, P.Author));
|
||||
}
|
||||
|
@ -5,6 +5,7 @@ namespace SharedLibraryCore.Exceptions
|
||||
public class ConfigurationException : Exception
|
||||
{
|
||||
public string[] Errors { get; set; }
|
||||
public string ConfigurationFileName { get; set; }
|
||||
|
||||
public ConfigurationException(string message) : base(message) { }
|
||||
}
|
||||
|
@ -9,20 +9,21 @@ namespace SharedLibraryCore.Configuration
|
||||
{
|
||||
public class BaseConfigurationHandler<T> : IConfigurationHandler<T> where T : IBaseConfiguration
|
||||
{
|
||||
readonly string _configurationPath;
|
||||
T _configuration;
|
||||
|
||||
public BaseConfigurationHandler(string fn)
|
||||
{
|
||||
_configurationPath = Path.Join(Utilities.OperatingDirectory, "Configuration", $"{fn}.json");
|
||||
FileName = Path.Join(Utilities.OperatingDirectory, "Configuration", $"{fn}.json");
|
||||
Build();
|
||||
}
|
||||
|
||||
public string FileName { get; }
|
||||
|
||||
public void Build()
|
||||
{
|
||||
try
|
||||
{
|
||||
var configContent = File.ReadAllText(_configurationPath);
|
||||
var configContent = File.ReadAllText(FileName);
|
||||
_configuration = JsonConvert.DeserializeObject<T>(configContent);
|
||||
}
|
||||
|
||||
@ -35,15 +36,22 @@ namespace SharedLibraryCore.Configuration
|
||||
{
|
||||
throw new ConfigurationException("MANAGER_CONFIGURATION_ERROR")
|
||||
{
|
||||
Errors = new[] { e.Message }
|
||||
Errors = new[] { e.Message },
|
||||
ConfigurationFileName = FileName
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
public Task Save()
|
||||
{
|
||||
var appConfigJSON = JsonConvert.SerializeObject(_configuration, Formatting.Indented);
|
||||
return File.WriteAllTextAsync(_configurationPath, appConfigJSON);
|
||||
var settings = new JsonSerializerSettings()
|
||||
{
|
||||
Formatting = Formatting.Indented
|
||||
};
|
||||
settings.Converters.Add(new Newtonsoft.Json.Converters.StringEnumConverter());
|
||||
|
||||
var appConfigJSON = JsonConvert.SerializeObject(_configuration, settings);
|
||||
return File.WriteAllTextAsync(FileName, appConfigJSON);
|
||||
}
|
||||
|
||||
public T Configuration()
|
||||
|
@ -12,5 +12,6 @@ namespace SharedLibraryCore.Interfaces
|
||||
void Build();
|
||||
T Configuration();
|
||||
void Set(T config);
|
||||
string FileName { get; }
|
||||
}
|
||||
}
|
||||
|
@ -17,7 +17,7 @@ namespace SharedLibraryCore.Interfaces
|
||||
void Restart();
|
||||
ILogger GetLogger(long serverId);
|
||||
IList<Server> GetServers();
|
||||
IList<Command> GetCommands();
|
||||
IList<IManagerCommand> GetCommands();
|
||||
IList<Helpers.MessageToken> GetMessageTokens();
|
||||
IList<EFClient> GetActiveClients();
|
||||
IConfigurationHandler<ApplicationConfiguration> GetApplicationSettings();
|
||||
|
@ -34,5 +34,15 @@ namespace SharedLibraryCore.Interfaces
|
||||
/// Minimum permission required to execute the command
|
||||
/// </summary>
|
||||
Permission Permission { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Syntax for using the command
|
||||
/// </summary>
|
||||
string Syntax { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Indicates if target is required
|
||||
/// </summary>
|
||||
bool RequiresTarget { get; }
|
||||
}
|
||||
}
|
||||
|
37
SharedLibraryCore/Interfaces/IPluginImporter.cs
Normal file
37
SharedLibraryCore/Interfaces/IPluginImporter.cs
Normal file
@ -0,0 +1,37 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Reflection;
|
||||
|
||||
namespace SharedLibraryCore.Interfaces
|
||||
{
|
||||
/// <summary>
|
||||
/// Defines the capabilities of the plugin importer
|
||||
/// </summary>
|
||||
public interface IPluginImporter
|
||||
{
|
||||
/// <summary>
|
||||
/// Command types that are defined in plugin assemblies
|
||||
/// </summary>
|
||||
IList<Type> CommandTypes { get; }
|
||||
|
||||
/// <summary>
|
||||
/// The loaded plugins from plugin assemblies
|
||||
/// </summary>
|
||||
IList<IPlugin> ActivePlugins { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Assemblies that contain plugins
|
||||
/// </summary>
|
||||
IList<Assembly> PluginAssemblies { get; }
|
||||
|
||||
/// <summary>
|
||||
/// All assemblies in the plugin folder
|
||||
/// </summary>
|
||||
IList<Assembly> Assemblies { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Loads in plugin assemblies and script plugins
|
||||
/// </summary>
|
||||
void Load();
|
||||
}
|
||||
}
|
@ -1,8 +1,10 @@
|
||||
using SharedLibraryCore.Localization;
|
||||
using Newtonsoft.Json.Converters;
|
||||
using SharedLibraryCore.Localization;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
using System.Linq;
|
||||
using System.Text.Json.Serialization;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
@ -1,103 +0,0 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Collections.Generic;
|
||||
using System.Reflection;
|
||||
using SharedLibraryCore.Interfaces;
|
||||
using System.Linq;
|
||||
|
||||
namespace SharedLibraryCore.Plugins
|
||||
{
|
||||
public class PluginImporter
|
||||
{
|
||||
public static List<Command> ActiveCommands = new List<Command>();
|
||||
public static List<IPlugin> ActivePlugins = new List<IPlugin>();
|
||||
public static List<Assembly> PluginAssemblies = new List<Assembly>();
|
||||
public static List<Assembly> Assemblies = new List<Assembly>();
|
||||
|
||||
public static bool Load(IManager Manager)
|
||||
{
|
||||
string pluginDir = $"{Utilities.OperatingDirectory}Plugins{Path.DirectorySeparatorChar}";
|
||||
string[] dllFileNames = null;
|
||||
string[] scriptFileNames = null;
|
||||
|
||||
if (Directory.Exists(pluginDir))
|
||||
{
|
||||
dllFileNames = Directory.GetFiles($"{Utilities.OperatingDirectory}Plugins{Path.DirectorySeparatorChar}", "*.dll");
|
||||
scriptFileNames = Directory.GetFiles($"{Utilities.OperatingDirectory}Plugins{Path.DirectorySeparatorChar}", "*.js");
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
dllFileNames = new string[0];
|
||||
scriptFileNames = new string[0];
|
||||
}
|
||||
|
||||
if (dllFileNames.Length == 0 &&
|
||||
scriptFileNames.Length == 0)
|
||||
{
|
||||
Manager.GetLogger(0).WriteDebug(Utilities.CurrentLocalization.LocalizationIndex["PLUGIN_IMPORTER_NOTFOUND"]);
|
||||
return true;
|
||||
}
|
||||
|
||||
// load up the script plugins
|
||||
foreach (string fileName in scriptFileNames)
|
||||
{
|
||||
var plugin = new ScriptPlugin(fileName);
|
||||
plugin.Initialize(Manager).Wait();
|
||||
Manager.GetLogger(0).WriteDebug($"Loaded script plugin \"{ plugin.Name }\" [{plugin.Version}]");
|
||||
ActivePlugins.Add(plugin);
|
||||
}
|
||||
|
||||
ICollection<Assembly> assemblies = new List<Assembly>(dllFileNames.Length);
|
||||
foreach (string dllFile in dllFileNames)
|
||||
{
|
||||
assemblies.Add(Assembly.LoadFrom(dllFile));
|
||||
}
|
||||
|
||||
int LoadedCommands = 0;
|
||||
foreach (Assembly Plugin in assemblies)
|
||||
{
|
||||
if (Plugin != null)
|
||||
{
|
||||
Assemblies.Add(Plugin);
|
||||
Type[] types = Plugin.GetTypes();
|
||||
foreach (Type assemblyType in types)
|
||||
{
|
||||
if (assemblyType.IsClass && assemblyType.BaseType.Name == "Command")
|
||||
{
|
||||
Object commandObject = Activator.CreateInstance(assemblyType);
|
||||
Command newCommand = (Command)commandObject;
|
||||
ActiveCommands.Add(newCommand);
|
||||
Manager.GetLogger(0).WriteDebug($"{Utilities.CurrentLocalization.LocalizationIndex["PLUGIN_IMPORTER_REGISTERCMD"]} \"{newCommand.Name}\"");
|
||||
LoadedCommands++;
|
||||
continue;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
if (assemblyType.GetInterface("IPlugin", false) == null)
|
||||
continue;
|
||||
|
||||
Object notifyObject = Activator.CreateInstance(assemblyType);
|
||||
IPlugin newNotify = (IPlugin)notifyObject;
|
||||
if (ActivePlugins.Find(x => x.Name == newNotify.Name) == null)
|
||||
{
|
||||
ActivePlugins.Add(newNotify);
|
||||
PluginAssemblies.Add(Plugin);
|
||||
Manager.GetLogger(0).WriteDebug($"Loaded plugin \"{ newNotify.Name }\" [{newNotify.Version}]");
|
||||
}
|
||||
}
|
||||
|
||||
catch (Exception e)
|
||||
{
|
||||
Manager.GetLogger(0).WriteWarning(Utilities.CurrentLocalization.LocalizationIndex["PLUGIN_IMPORTER_ERROR"].FormatExt(Plugin.Location));
|
||||
Manager.GetLogger(0).WriteDebug(e.GetExceptionInfo());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Manager.GetLogger(0).WriteInfo($"Loaded {ActivePlugins.Count} plugins and registered {LoadedCommands} commands.");
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
@ -10,7 +10,7 @@ using System.Threading.Tasks;
|
||||
|
||||
namespace SharedLibraryCore
|
||||
{
|
||||
class ScriptPlugin : IPlugin
|
||||
public class ScriptPlugin : IPlugin
|
||||
{
|
||||
public string Name { get; set; }
|
||||
|
||||
|
@ -6,7 +6,7 @@
|
||||
<ApplicationIcon />
|
||||
<StartupObject />
|
||||
<PackageId>RaidMax.IW4MAdmin.SharedLibraryCore</PackageId>
|
||||
<Version>2.2.5</Version>
|
||||
<Version>2.2.6</Version>
|
||||
<Authors>RaidMax</Authors>
|
||||
<Company>Forever None</Company>
|
||||
<Configurations>Debug;Release;Prerelease</Configurations>
|
||||
@ -20,8 +20,8 @@
|
||||
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
|
||||
<PackageLicenseExpression>MIT</PackageLicenseExpression>
|
||||
<Description>Shared Library for IW4MAdmin</Description>
|
||||
<AssemblyVersion>2.2.5.0</AssemblyVersion>
|
||||
<FileVersion>2.2.5.0</FileVersion>
|
||||
<AssemblyVersion>2.2.6.0</AssemblyVersion>
|
||||
<FileVersion>2.2.6.0</FileVersion>
|
||||
</PropertyGroup>
|
||||
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Prerelease|AnyCPU'">
|
||||
|
Reference in New Issue
Block a user