2023-04-05 10:54:57 -04:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2022-10-12 22:06:18 -04:00
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading.Tasks;
|
2022-10-16 17:25:09 -04:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
2022-10-12 22:06:18 -04:00
|
|
|
|
using SharedLibraryCore;
|
|
|
|
|
using SharedLibraryCore.Configuration;
|
|
|
|
|
using SharedLibraryCore.Dtos;
|
|
|
|
|
using SharedLibraryCore.Interfaces;
|
|
|
|
|
using SharedLibraryCore.Services;
|
2022-10-16 17:25:09 -04:00
|
|
|
|
using ILogger = Microsoft.Extensions.Logging.ILogger;
|
2022-10-12 22:06:18 -04:00
|
|
|
|
|
|
|
|
|
namespace IW4MAdmin.Application.Misc;
|
|
|
|
|
|
|
|
|
|
public class RemoteCommandService : IRemoteCommandService
|
|
|
|
|
{
|
2022-10-16 17:25:09 -04:00
|
|
|
|
private readonly ILogger _logger;
|
2022-10-12 22:06:18 -04:00
|
|
|
|
private readonly ApplicationConfiguration _appConfig;
|
|
|
|
|
private readonly ClientService _clientService;
|
|
|
|
|
|
2022-10-16 17:25:09 -04:00
|
|
|
|
public RemoteCommandService(ILogger<RemoteCommandService> logger, ApplicationConfiguration appConfig, ClientService clientService)
|
2022-10-12 22:06:18 -04:00
|
|
|
|
{
|
2022-10-16 17:25:09 -04:00
|
|
|
|
_logger = logger;
|
2022-10-12 22:06:18 -04:00
|
|
|
|
_appConfig = appConfig;
|
|
|
|
|
_clientService = clientService;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<IEnumerable<CommandResponseInfo>> Execute(int originId, int? targetId, string command,
|
|
|
|
|
IEnumerable<string> arguments, Server server)
|
2022-10-25 15:52:12 -04:00
|
|
|
|
{
|
2023-04-05 10:54:57 -04:00
|
|
|
|
var (_, result) = await ExecuteWithResult(originId, targetId, command, arguments, server);
|
2022-10-25 15:52:12 -04:00
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<(bool, IEnumerable<CommandResponseInfo>)> ExecuteWithResult(int originId, int? targetId, string command,
|
|
|
|
|
IEnumerable<string> arguments, Server server)
|
2022-10-12 22:06:18 -04:00
|
|
|
|
{
|
2022-10-16 17:25:09 -04:00
|
|
|
|
if (originId < 1)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("Not executing command {Command} for {Originid} because origin id is invalid", command,
|
|
|
|
|
originId);
|
2022-10-25 15:52:12 -04:00
|
|
|
|
return (false, Enumerable.Empty<CommandResponseInfo>());
|
2022-10-16 17:25:09 -04:00
|
|
|
|
}
|
|
|
|
|
|
2022-10-12 22:06:18 -04:00
|
|
|
|
var client = await _clientService.Get(originId);
|
|
|
|
|
client.CurrentServer = server;
|
|
|
|
|
|
|
|
|
|
command += $" {(targetId.HasValue ? $"@{targetId} " : "")}{string.Join(" ", arguments ?? Enumerable.Empty<string>())}";
|
|
|
|
|
|
|
|
|
|
var remoteEvent = new GameEvent
|
|
|
|
|
{
|
|
|
|
|
Type = GameEvent.EventType.Command,
|
|
|
|
|
Data = command.StartsWith(_appConfig.CommandPrefix) ||
|
|
|
|
|
command.StartsWith(_appConfig.BroadcastCommandPrefix)
|
|
|
|
|
? command
|
|
|
|
|
: $"{_appConfig.CommandPrefix}{command}",
|
|
|
|
|
Origin = client,
|
|
|
|
|
Owner = server,
|
2023-04-05 10:54:57 -04:00
|
|
|
|
IsRemote = true,
|
|
|
|
|
CorrelationId = Guid.NewGuid()
|
2022-10-12 22:06:18 -04:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
server.Manager.AddEvent(remoteEvent);
|
|
|
|
|
CommandResponseInfo[] response;
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
// wait for the event to process
|
|
|
|
|
var completedEvent =
|
|
|
|
|
await remoteEvent.WaitAsync(Utilities.DefaultCommandTimeout, server.Manager.CancellationToken);
|
|
|
|
|
|
|
|
|
|
if (completedEvent.FailReason == GameEvent.EventFailReason.Timeout)
|
|
|
|
|
{
|
|
|
|
|
response = new[]
|
|
|
|
|
{
|
2023-04-05 10:54:57 -04:00
|
|
|
|
new CommandResponseInfo
|
2022-10-12 22:06:18 -04:00
|
|
|
|
{
|
|
|
|
|
ClientId = client.ClientId,
|
|
|
|
|
Response = Utilities.CurrentLocalization.LocalizationIndex["SERVER_ERROR_COMMAND_TIMEOUT"]
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
response = completedEvent.Output.Select(output => new CommandResponseInfo()
|
|
|
|
|
{
|
|
|
|
|
Response = output,
|
|
|
|
|
ClientId = client.ClientId
|
|
|
|
|
}).ToArray();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-05 10:54:57 -04:00
|
|
|
|
catch (OperationCanceledException)
|
2022-10-12 22:06:18 -04:00
|
|
|
|
{
|
|
|
|
|
response = new[]
|
|
|
|
|
{
|
|
|
|
|
new CommandResponseInfo
|
|
|
|
|
{
|
|
|
|
|
ClientId = client.ClientId,
|
|
|
|
|
Response = Utilities.CurrentLocalization.LocalizationIndex["COMMANDS_RESTART_SUCCESS"]
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-25 15:52:12 -04:00
|
|
|
|
return (!remoteEvent.Failed, response);
|
2022-10-12 22:06:18 -04:00
|
|
|
|
}
|
|
|
|
|
}
|