2020-05-11 17:10:43 -04:00
|
|
|
|
using SharedLibraryCore;
|
|
|
|
|
using SharedLibraryCore.Commands;
|
|
|
|
|
using SharedLibraryCore.Configuration;
|
|
|
|
|
using SharedLibraryCore.Interfaces;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Threading.Tasks;
|
2021-03-22 12:09:25 -04:00
|
|
|
|
using Data.Models.Client;
|
2020-11-11 18:31:26 -05:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
2020-05-11 17:10:43 -04:00
|
|
|
|
using static SharedLibraryCore.Database.Models.EFClient;
|
2020-11-11 18:31:26 -05:00
|
|
|
|
using ILogger = Microsoft.Extensions.Logging.ILogger;
|
2020-05-11 17:10:43 -04:00
|
|
|
|
|
|
|
|
|
namespace IW4MAdmin.Application.Misc
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// generic script command implementation
|
|
|
|
|
/// </summary>
|
|
|
|
|
public class ScriptCommand : Command
|
|
|
|
|
{
|
|
|
|
|
private readonly Action<GameEvent> _executeAction;
|
2020-11-11 18:31:26 -05:00
|
|
|
|
private readonly ILogger _logger;
|
2020-05-11 17:10:43 -04:00
|
|
|
|
|
2021-03-22 12:09:25 -04:00
|
|
|
|
public ScriptCommand(string name, string alias, string description, bool isTargetRequired, EFClient.Permission permission,
|
2020-11-11 18:31:26 -05:00
|
|
|
|
CommandArgument[] args, Action<GameEvent> executeAction, CommandConfiguration config, ITranslationLookup layout, ILogger<ScriptCommand> logger)
|
2020-05-11 17:10:43 -04:00
|
|
|
|
: base(config, layout)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
_executeAction = executeAction;
|
2020-11-11 18:31:26 -05:00
|
|
|
|
_logger = logger;
|
2020-05-11 17:10:43 -04:00
|
|
|
|
Name = name;
|
|
|
|
|
Alias = alias;
|
|
|
|
|
Description = description;
|
2020-09-28 21:32:53 -04:00
|
|
|
|
RequiresTarget = isTargetRequired;
|
2020-05-11 17:10:43 -04:00
|
|
|
|
Permission = permission;
|
|
|
|
|
Arguments = args;
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-11 18:31:26 -05:00
|
|
|
|
public override async Task ExecuteAsync(GameEvent e)
|
2020-05-11 17:10:43 -04:00
|
|
|
|
{
|
|
|
|
|
if (_executeAction == null)
|
|
|
|
|
{
|
|
|
|
|
throw new InvalidOperationException($"No execute action defined for command \"{Name}\"");
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-11 18:31:26 -05:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
await Task.Run(() => _executeAction(e));
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogError(ex, "Failed to execute ScriptCommand action for command {command} {@event}", Name, e);
|
|
|
|
|
}
|
2020-05-11 17:10:43 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|