2022-09-08 16:03:38 -04:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Concurrent;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using Data.Models;
|
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
using SharedLibraryCore.Interfaces;
|
|
|
|
|
using InteractionRegistrationCallback =
|
|
|
|
|
System.Func<int?, Data.Models.Reference.Game?, System.Threading.CancellationToken,
|
|
|
|
|
System.Threading.Tasks.Task<SharedLibraryCore.Interfaces.IInteractionData>>;
|
|
|
|
|
|
|
|
|
|
namespace IW4MAdmin.Application.Misc;
|
|
|
|
|
|
|
|
|
|
public class InteractionRegistration : IInteractionRegistration
|
|
|
|
|
{
|
|
|
|
|
private readonly ILogger<InteractionRegistration> _logger;
|
|
|
|
|
private readonly IServiceProvider _serviceProvider;
|
|
|
|
|
private readonly ConcurrentDictionary<string, InteractionRegistrationCallback> _interactions = new();
|
|
|
|
|
|
|
|
|
|
public InteractionRegistration(ILogger<InteractionRegistration> logger, IServiceProvider serviceProvider)
|
|
|
|
|
{
|
|
|
|
|
_logger = logger;
|
|
|
|
|
_serviceProvider = serviceProvider;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void RegisterScriptInteraction(string interactionName, string source, Delegate interactionRegistration)
|
|
|
|
|
{
|
2022-10-24 19:53:30 -04:00
|
|
|
|
if (string.IsNullOrWhiteSpace(source))
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentException("Script interaction source cannot be null");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_logger.LogDebug("Registering script interaction {InteractionName} from {Source}", interactionName, source);
|
|
|
|
|
|
2022-09-08 16:03:38 -04:00
|
|
|
|
var plugin = _serviceProvider.GetRequiredService<IEnumerable<IPlugin>>()
|
|
|
|
|
.FirstOrDefault(plugin => plugin.Name == source);
|
|
|
|
|
|
|
|
|
|
if (plugin is not ScriptPlugin scriptPlugin)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-24 19:53:30 -04:00
|
|
|
|
Task<IInteractionData> WrappedDelegate(int? clientId, Reference.Game? game, CancellationToken token) =>
|
2022-09-08 16:03:38 -04:00
|
|
|
|
Task.FromResult(
|
2022-10-24 19:53:30 -04:00
|
|
|
|
scriptPlugin.WrapDelegate<IInteractionData>(interactionRegistration, token, clientId, game, token));
|
2022-09-08 16:03:38 -04:00
|
|
|
|
|
|
|
|
|
if (!_interactions.ContainsKey(interactionName))
|
|
|
|
|
{
|
2022-10-24 19:53:30 -04:00
|
|
|
|
_interactions.TryAdd(interactionName, WrappedDelegate);
|
2022-09-08 16:03:38 -04:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2022-10-24 19:53:30 -04:00
|
|
|
|
_interactions[interactionName] = WrappedDelegate;
|
2022-09-08 16:03:38 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void RegisterInteraction(string interactionName, InteractionRegistrationCallback interactionRegistration)
|
|
|
|
|
{
|
|
|
|
|
if (!_interactions.ContainsKey(interactionName))
|
|
|
|
|
{
|
2022-10-24 19:53:30 -04:00
|
|
|
|
_logger.LogDebug("Registering interaction {InteractionName}", interactionName);
|
2022-09-08 16:03:38 -04:00
|
|
|
|
_interactions.TryAdd(interactionName, interactionRegistration);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2022-10-24 19:53:30 -04:00
|
|
|
|
_logger.LogDebug("Updating interaction {InteractionName}", interactionName);
|
2022-09-08 16:03:38 -04:00
|
|
|
|
_interactions[interactionName] = interactionRegistration;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void UnregisterInteraction(string interactionName)
|
|
|
|
|
{
|
2022-10-24 19:53:30 -04:00
|
|
|
|
if (!_interactions.ContainsKey(interactionName))
|
2022-09-08 16:03:38 -04:00
|
|
|
|
{
|
2022-10-24 19:53:30 -04:00
|
|
|
|
return;
|
2022-09-08 16:03:38 -04:00
|
|
|
|
}
|
2022-10-24 19:53:30 -04:00
|
|
|
|
|
|
|
|
|
_logger.LogDebug("Unregistering interaction {InteractionName}", interactionName);
|
|
|
|
|
_interactions.TryRemove(interactionName, out _);
|
2022-09-08 16:03:38 -04:00
|
|
|
|
}
|
|
|
|
|
|
2022-10-17 10:17:43 -04:00
|
|
|
|
public async Task<IEnumerable<IInteractionData>> GetInteractions(string interactionPrefix = null,
|
|
|
|
|
int? clientId = null,
|
2022-09-08 16:03:38 -04:00
|
|
|
|
Reference.Game? game = null, CancellationToken token = default)
|
|
|
|
|
{
|
2023-01-06 14:42:38 -05:00
|
|
|
|
return Enumerable.Empty<IInteractionData>();
|
|
|
|
|
// fixme: multi-threading is broken when dealing with script plugins
|
2022-10-24 19:53:30 -04:00
|
|
|
|
return await GetInteractionsInternal(interactionPrefix, clientId, game, token);
|
2022-09-08 16:03:38 -04:00
|
|
|
|
}
|
|
|
|
|
|
2022-10-12 22:06:18 -04:00
|
|
|
|
public async Task<string> ProcessInteraction(string interactionId, int originId, int? targetId = null,
|
|
|
|
|
Reference.Game? game = null, IDictionary<string, string> meta = null, CancellationToken token = default)
|
2022-09-08 16:03:38 -04:00
|
|
|
|
{
|
|
|
|
|
if (!_interactions.ContainsKey(interactionId))
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentException($"Interaction with ID {interactionId} has not been registered");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
2022-10-13 11:26:22 -04:00
|
|
|
|
var interaction = await _interactions[interactionId](targetId, game, token);
|
2022-09-08 16:03:38 -04:00
|
|
|
|
|
|
|
|
|
if (interaction.Action is not null)
|
|
|
|
|
{
|
2022-10-12 22:06:18 -04:00
|
|
|
|
return await interaction.Action(originId, targetId, game, meta, token);
|
2022-09-08 16:03:38 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (interaction.ScriptAction is not null)
|
|
|
|
|
{
|
|
|
|
|
foreach (var plugin in _serviceProvider.GetRequiredService<IEnumerable<IPlugin>>())
|
|
|
|
|
{
|
|
|
|
|
if (plugin is not ScriptPlugin scriptPlugin || scriptPlugin.Name != interaction.Source)
|
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-24 19:53:30 -04:00
|
|
|
|
return scriptPlugin.ExecuteAction<string>(interaction.ScriptAction, token, originId, targetId, game, meta,
|
2022-10-17 10:17:43 -04:00
|
|
|
|
token);
|
2022-09-08 16:03:38 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning(ex,
|
2022-10-17 10:17:43 -04:00
|
|
|
|
"Could not process interaction for {InteractionName} and OriginId {ClientId}",
|
2022-10-12 22:06:18 -04:00
|
|
|
|
interactionId, originId);
|
2022-09-08 16:03:38 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2022-10-17 10:17:43 -04:00
|
|
|
|
|
2022-10-25 15:03:35 -04:00
|
|
|
|
private async Task<IEnumerable<IInteractionData>> GetInteractionsInternal(string prefix = null,
|
|
|
|
|
int? clientId = null, Reference.Game? game = null, CancellationToken token = default)
|
2022-10-17 10:17:43 -04:00
|
|
|
|
{
|
2022-10-24 19:53:30 -04:00
|
|
|
|
var interactions = _interactions
|
2022-10-17 10:17:43 -04:00
|
|
|
|
.Where(interaction => string.IsNullOrWhiteSpace(prefix) || interaction.Key.StartsWith(prefix)).Select(
|
|
|
|
|
async kvp =>
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
return await kvp.Value(clientId, game, token);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning(ex,
|
|
|
|
|
"Could not get interaction for {InteractionName} and ClientId {ClientId}",
|
|
|
|
|
kvp.Key,
|
|
|
|
|
clientId);
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2022-10-25 15:03:35 -04:00
|
|
|
|
});
|
2022-10-24 19:53:30 -04:00
|
|
|
|
|
2022-10-25 15:03:35 -04:00
|
|
|
|
return (await Task.WhenAll(interactions))
|
|
|
|
|
.Where(interaction => interaction is not null)
|
|
|
|
|
.ToList();
|
2022-10-17 10:17:43 -04:00
|
|
|
|
}
|
2022-09-08 16:03:38 -04:00
|
|
|
|
}
|