mirror of
https://github.com/RaidMax/IW4M-Admin.git
synced 2025-06-10 23:31:13 -05:00
update live radar plugin to IPluginV2
This commit is contained in:
@ -1,132 +1,145 @@
|
||||
using LiveRadar.Configuration;
|
||||
using SharedLibraryCore;
|
||||
using SharedLibraryCore;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Data.Models;
|
||||
using IW4MAdmin.Plugins.LiveRadar.Configuration;
|
||||
using IW4MAdmin.Plugins.LiveRadar.Events;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using SharedLibraryCore.Configuration;
|
||||
using SharedLibraryCore.Events.Game;
|
||||
using SharedLibraryCore.Events.Server;
|
||||
using SharedLibraryCore.Interfaces;
|
||||
using SharedLibraryCore.Interfaces.Events;
|
||||
using ILogger = Microsoft.Extensions.Logging.ILogger;
|
||||
|
||||
namespace LiveRadar
|
||||
namespace IW4MAdmin.Plugins.LiveRadar;
|
||||
|
||||
public class Plugin : IPluginV2
|
||||
{
|
||||
public class Plugin : IPlugin
|
||||
public string Name => "Live Radar";
|
||||
|
||||
public string Version => Utilities.GetVersionAsString();
|
||||
|
||||
public string Author => "RaidMax";
|
||||
|
||||
private bool _addedPage;
|
||||
private readonly Dictionary<string, long> _botGuidLookups;
|
||||
private readonly object _lockObject = new();
|
||||
private readonly ILogger _logger;
|
||||
private readonly ApplicationConfiguration _appConfig;
|
||||
|
||||
public static void RegisterDependencies(IServiceCollection serviceCollection)
|
||||
{
|
||||
public string Name => "Live Radar";
|
||||
serviceCollection.AddConfiguration<LiveRadarConfiguration>();
|
||||
}
|
||||
|
||||
public float Version => (float)Utilities.GetVersionAsDouble();
|
||||
public Plugin(ILogger<Plugin> logger, ApplicationConfiguration appConfig)
|
||||
{
|
||||
_botGuidLookups = new Dictionary<string, long>();
|
||||
_logger = logger;
|
||||
_appConfig = appConfig;
|
||||
|
||||
IGameServerEventSubscriptions.MonitoringStarted += OnMonitoringStarted;
|
||||
IGameEventSubscriptions.ClientEnteredMatch += OnClientEnteredMatch;
|
||||
IGameEventSubscriptions.ScriptEventTriggered += OnScriptEvent;
|
||||
}
|
||||
|
||||
public string Author => "RaidMax";
|
||||
|
||||
private readonly IConfigurationHandler<LiveRadarConfiguration> _configurationHandler;
|
||||
private readonly Dictionary<string, long> _botGuidLookups;
|
||||
private bool addedPage;
|
||||
private readonly object lockObject = new object();
|
||||
private readonly ILogger _logger;
|
||||
private readonly ApplicationConfiguration _appConfig;
|
||||
|
||||
public Plugin(ILogger<Plugin> logger, IConfigurationHandlerFactory configurationHandlerFactory, ApplicationConfiguration appConfig)
|
||||
private Task OnScriptEvent(GameScriptEvent scriptEvent, CancellationToken token)
|
||||
{
|
||||
if (scriptEvent is not LiveRadarEvent radarEvent)
|
||||
{
|
||||
_configurationHandler = configurationHandlerFactory.GetConfigurationHandler<LiveRadarConfiguration>("LiveRadarConfiguration");
|
||||
_botGuidLookups = new Dictionary<string, long>();
|
||||
_logger = logger;
|
||||
_appConfig = appConfig;
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
var originalBotGuid = radarEvent.ScriptData.Split(";")[1];
|
||||
|
||||
if (originalBotGuid.IsBotGuid() && _appConfig.IgnoreBots)
|
||||
{
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
var botKey = $"BotGuid_{originalBotGuid}";
|
||||
long generatedBotGuid;
|
||||
|
||||
lock (_lockObject)
|
||||
{
|
||||
var hasBotKey = _botGuidLookups.ContainsKey(botKey);
|
||||
|
||||
if (!hasBotKey && originalBotGuid.IsBotGuid())
|
||||
{
|
||||
// edge case where the bot guid has not been registered yet
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
generatedBotGuid = hasBotKey
|
||||
? _botGuidLookups[botKey]
|
||||
: (originalBotGuid ?? "0").ConvertGuidToLong(NumberStyles.HexNumber);
|
||||
}
|
||||
|
||||
var radarUpdate = RadarEvent.Parse(scriptEvent.ScriptData, generatedBotGuid);
|
||||
var client =
|
||||
radarEvent.Owner.ConnectedClients.FirstOrDefault(client => client.NetworkId == radarUpdate.Guid);
|
||||
|
||||
if (client != null)
|
||||
{
|
||||
radarUpdate.Name = client.Name.StripColors();
|
||||
client.SetAdditionalProperty("LiveRadar", radarUpdate);
|
||||
}
|
||||
}
|
||||
|
||||
public Task OnEventAsync(GameEvent E, Server S)
|
||||
catch (Exception e)
|
||||
{
|
||||
_logger.LogError(e, "Could not parse live radar output: {Data}", e.Data);
|
||||
}
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
private Task OnClientEnteredMatch(ClientEnterMatchEvent clientEvent, CancellationToken token)
|
||||
{
|
||||
if (!clientEvent.Client.IsBot)
|
||||
{
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
var botKey = $"BotGuid_{clientEvent.ClientNetworkId}";
|
||||
lock (_lockObject)
|
||||
{
|
||||
if (!_botGuidLookups.ContainsKey(botKey))
|
||||
{
|
||||
_botGuidLookups.Add(botKey, clientEvent.Client.NetworkId);
|
||||
}
|
||||
}
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
private Task OnMonitoringStarted(MonitorStartEvent monitorEvent, CancellationToken token)
|
||||
{
|
||||
lock (_lockObject)
|
||||
{
|
||||
// if it's an IW4 game, with custom callbacks, we want to
|
||||
// enable the live radar page
|
||||
lock (lockObject)
|
||||
var shouldRegisterPage = monitorEvent.Server.GameCode != Reference.Game.IW4 ||
|
||||
!monitorEvent.Server.IsLegacyGameIntegrationEnabled ||
|
||||
_addedPage;
|
||||
if (shouldRegisterPage)
|
||||
{
|
||||
if (E.Type == GameEvent.EventType.Start &&
|
||||
S.GameName == Server.Game.IW4 &&
|
||||
S.CustomCallback &&
|
||||
!addedPage)
|
||||
{
|
||||
E.Owner.Manager.GetPageList().Pages.Add(Utilities.CurrentLocalization.LocalizationIndex["WEBFRONT_RADAR_TITLE"], "/Radar");
|
||||
addedPage = true;
|
||||
}
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
if (E.Type == GameEvent.EventType.PreConnect && E.Origin.IsBot)
|
||||
{
|
||||
string botKey = $"BotGuid_{E.Extra}";
|
||||
lock (lockObject)
|
||||
{
|
||||
if (!_botGuidLookups.ContainsKey(botKey))
|
||||
{
|
||||
_botGuidLookups.Add(botKey, E.Origin.NetworkId);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (E.Type == GameEvent.EventType.Other && E.Subtype == "LiveRadar")
|
||||
{
|
||||
try
|
||||
{
|
||||
if (((string) E.Extra).IsBotGuid() && _appConfig.IgnoreBots)
|
||||
{
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
string botKey = $"BotGuid_{E.Extra}";
|
||||
long generatedBotGuid;
|
||||
|
||||
lock (lockObject)
|
||||
{
|
||||
var hasBotKey = _botGuidLookups.ContainsKey(botKey);
|
||||
|
||||
if (!hasBotKey && ((string)E.Extra).IsBotGuid())
|
||||
{
|
||||
// edge case where the bot guid has not been registered yet
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
generatedBotGuid = hasBotKey
|
||||
? _botGuidLookups[botKey]
|
||||
: (E.Extra.ToString() ?? "0").ConvertGuidToLong(NumberStyles.HexNumber);
|
||||
}
|
||||
|
||||
var radarUpdate = RadarEvent.Parse(E.Data, generatedBotGuid);
|
||||
var client = S.Manager.GetActiveClients().FirstOrDefault(_client => _client.NetworkId == radarUpdate.Guid);
|
||||
|
||||
if (client != null)
|
||||
{
|
||||
radarUpdate.Name = client.Name.StripColors();
|
||||
client.SetAdditionalProperty("LiveRadar", radarUpdate);
|
||||
}
|
||||
}
|
||||
|
||||
catch (Exception e)
|
||||
{
|
||||
_logger.LogError(e, "Could not parse live radar output: {data}", e.Data);
|
||||
}
|
||||
}
|
||||
|
||||
return Task.CompletedTask;
|
||||
(monitorEvent.Source as IManager)?.GetPageList().Pages
|
||||
.Add(Utilities.CurrentLocalization.LocalizationIndex["WEBFRONT_RADAR_TITLE"], "/Radar");
|
||||
_addedPage = true;
|
||||
}
|
||||
|
||||
public async Task OnLoadAsync(IManager manager)
|
||||
{
|
||||
await _configurationHandler.BuildAsync();
|
||||
if (_configurationHandler.Configuration() == null)
|
||||
{
|
||||
_configurationHandler.Set((LiveRadarConfiguration)new LiveRadarConfiguration().Generate());
|
||||
await _configurationHandler.Save();
|
||||
}
|
||||
}
|
||||
|
||||
public Task OnTickAsync(Server S)
|
||||
{
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
public Task OnUnloadAsync()
|
||||
{
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user