2023-04-04 19:24:13 -04:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
2020-05-11 17:10:43 -04:00
|
|
|
|
using System.Threading.Tasks;
|
2023-04-04 19:24:13 -04:00
|
|
|
|
using Data.Models;
|
2021-03-22 12:09:25 -04:00
|
|
|
|
using Data.Models.Client;
|
2020-11-11 18:31:26 -05:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
2023-04-04 19:24:13 -04:00
|
|
|
|
using SharedLibraryCore;
|
|
|
|
|
using SharedLibraryCore.Commands;
|
|
|
|
|
using SharedLibraryCore.Configuration;
|
|
|
|
|
using SharedLibraryCore.Interfaces;
|
2020-11-11 18:31:26 -05:00
|
|
|
|
using ILogger = Microsoft.Extensions.Logging.ILogger;
|
2020-05-11 17:10:43 -04:00
|
|
|
|
|
2023-04-04 19:24:13 -04:00
|
|
|
|
namespace IW4MAdmin.Application.Plugin.Script
|
2020-05-11 17:10:43 -04:00
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// generic script command implementation
|
|
|
|
|
/// </summary>
|
|
|
|
|
public class ScriptCommand : Command
|
|
|
|
|
{
|
2022-02-07 19:43:36 -05:00
|
|
|
|
private readonly Func<GameEvent, Task> _executeAction;
|
2020-11-11 18:31:26 -05:00
|
|
|
|
private readonly ILogger _logger;
|
2020-05-11 17:10:43 -04:00
|
|
|
|
|
2022-02-07 19:43:36 -05:00
|
|
|
|
public ScriptCommand(string name, string alias, string description, bool isTargetRequired,
|
|
|
|
|
EFClient.Permission permission,
|
2023-04-04 19:24:13 -04:00
|
|
|
|
IEnumerable<CommandArgument> args, Func<GameEvent, Task> executeAction, CommandConfiguration config,
|
|
|
|
|
ITranslationLookup layout, ILogger<ScriptCommand> logger, IEnumerable<Reference.Game> supportedGames)
|
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;
|
2023-04-04 19:24:13 -04:00
|
|
|
|
Arguments = args.ToArray();
|
|
|
|
|
SupportedGames = supportedGames?.Select(game => (Server.Game)game).ToArray();
|
2020-05-11 17:10:43 -04:00
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
|
{
|
2022-02-07 19:43:36 -05:00
|
|
|
|
await _executeAction(e);
|
2020-11-11 18:31:26 -05:00
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2023-04-04 19:24:13 -04:00
|
|
|
|
_logger.LogError(ex, "Failed to execute ScriptCommand action for command {Command} {@Event}", Name, e);
|
2020-11-11 18:31:26 -05:00
|
|
|
|
}
|
2020-05-11 17:10:43 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|