From e6154822f6d2060c3f6f4956d64f4734db98d2ba Mon Sep 17 00:00:00 2001 From: RaidMax Date: Sun, 27 Jan 2019 16:40:08 -0600 Subject: [PATCH] Implement more dynamic parser stuff --- .../DynamicEventParserConfiguration.cs | 20 ++- Application/EventParsers/IW4EventParser.cs | 119 +++++++++++++----- Application/IW4MServer.cs | 2 +- SharedLibraryCore/Helpers/ParserRegex.cs | 41 ++++++ .../Interfaces/IEventParserConfiguration.cs | 16 +-- 5 files changed, 153 insertions(+), 45 deletions(-) create mode 100644 SharedLibraryCore/Helpers/ParserRegex.cs diff --git a/Application/EventParsers/DynamicEventParserConfiguration.cs b/Application/EventParsers/DynamicEventParserConfiguration.cs index c3d869659..78e06a0fa 100644 --- a/Application/EventParsers/DynamicEventParserConfiguration.cs +++ b/Application/EventParsers/DynamicEventParserConfiguration.cs @@ -5,10 +5,20 @@ namespace IW4MAdmin.Application.EventParsers class DynamicEventParserConfiguration : IEventParserConfiguration { public string GameDirectory { get; set; } - public string SayRegex { get; set; } - public string JoinRegex { get; set; } - public string QuitRegex { get; set; } - public string KillRegex { get; set; } - public string DamageRegex { get; set; } + + public ParserRegex Say { get; set; } + public ParserRegex Join { get; set; } + public ParserRegex Quit { get; set; } + public ParserRegex Kill { get; set; } + public ParserRegex Damage { get; set; } + + public DynamicEventParserConfiguration() + { + Say = new ParserRegex(); + Join = new ParserRegex(); + Quit = new ParserRegex(); + Kill = new ParserRegex(); + Damage = new ParserRegex(); + } } } diff --git a/Application/EventParsers/IW4EventParser.cs b/Application/EventParsers/IW4EventParser.cs index defa54dd6..79704c3ea 100644 --- a/Application/EventParsers/IW4EventParser.cs +++ b/Application/EventParsers/IW4EventParser.cs @@ -16,11 +16,56 @@ namespace IW4MAdmin.Application.EventParsers _configuration = new DynamicEventParserConfiguration() { GameDirectory = "userraw", - SayRegex = "(say|sayteam);(.{1,32});([0-9]+)(.*);(.*)", - QuitRegex = @"^(Q;)(.{1,32});([0-9]+);(.*)$", - JoinRegex = @"^(J;)(.{1,32});([0-9]+);(.*)$", - DamageRegex = @"^(D);((?:bot[0-9]+)|(?:[A-Z]|[0-9])+);([0-9]+);(axis|allies);(.+);((?:[A-Z]|[0-9])+);([0-9]+);(axis|allies);(.+);((?:[0-9]+|[a-z]+|_)+);([0-9]+);((?:[A-Z]|_)+);((?:[a-z]|_)+)$" }; + + _configuration.Say.Pattern = @"^(say|sayteam);(.{1,32});([0-9]+)(.*);(.*)$"; + _configuration.Say.GroupMapping.Add(ParserRegex.GroupType.EventType, 1); + _configuration.Say.GroupMapping.Add(ParserRegex.GroupType.OriginNetworkId, 2); + _configuration.Say.GroupMapping.Add(ParserRegex.GroupType.OriginClientNumber, 3); + _configuration.Say.GroupMapping.Add(ParserRegex.GroupType.OriginName, 4); + _configuration.Say.GroupMapping.Add(ParserRegex.GroupType.Message, 5); + + _configuration.Quit.Pattern = @"^(Q);(.{16,32}|bot[0-9]+);([0-9]+);(.*)$"; + _configuration.Quit.GroupMapping.Add(ParserRegex.GroupType.EventType, 1); + _configuration.Quit.GroupMapping.Add(ParserRegex.GroupType.OriginNetworkId, 2); + _configuration.Quit.GroupMapping.Add(ParserRegex.GroupType.OriginClientNumber, 3); + _configuration.Quit.GroupMapping.Add(ParserRegex.GroupType.OriginName, 4); + + _configuration.Join.Pattern = @"^(J);(.{16,32}|bot[0-9]+);([0-9]+);(.*)$"; + _configuration.Join.GroupMapping.Add(ParserRegex.GroupType.EventType, 1); + _configuration.Join.GroupMapping.Add(ParserRegex.GroupType.OriginNetworkId, 2); + _configuration.Join.GroupMapping.Add(ParserRegex.GroupType.OriginClientNumber, 3); + _configuration.Join.GroupMapping.Add(ParserRegex.GroupType.OriginName, 4); + + _configuration.Damage.Pattern = @"^(D);([A-Fa-f0-9_]{16,32}|bot[0-9]+);(-?[0-9]+);(axis|allies|world);(.{1,24});([A-Fa-f0-9_]{16,32}|bot[0-9]+)?;-?([0-9]+);(axis|allies|world);(.{1,24})?;((?:[0-9]+|[a-z]+|_)+);([0-9]+);((?:[A-Z]|_)+);((?:[a-z]|_)+)$"; + _configuration.Damage.GroupMapping.Add(ParserRegex.GroupType.EventType, 1); + _configuration.Damage.GroupMapping.Add(ParserRegex.GroupType.TargetNetworkId, 2); + _configuration.Damage.GroupMapping.Add(ParserRegex.GroupType.TargetClientNumber, 3); + _configuration.Damage.GroupMapping.Add(ParserRegex.GroupType.TargetTeam, 4); + _configuration.Damage.GroupMapping.Add(ParserRegex.GroupType.TargetName, 5); + _configuration.Damage.GroupMapping.Add(ParserRegex.GroupType.OriginNetworkId, 6); + _configuration.Damage.GroupMapping.Add(ParserRegex.GroupType.OriginClientNumber, 7); + _configuration.Damage.GroupMapping.Add(ParserRegex.GroupType.OriginTeam, 8); + _configuration.Damage.GroupMapping.Add(ParserRegex.GroupType.OriginName, 9); + _configuration.Damage.GroupMapping.Add(ParserRegex.GroupType.Weapon, 10); + _configuration.Damage.GroupMapping.Add(ParserRegex.GroupType.Damage, 11); + _configuration.Damage.GroupMapping.Add(ParserRegex.GroupType.MeansOfDeath, 12); + _configuration.Damage.GroupMapping.Add(ParserRegex.GroupType.HitLocation, 13); + + _configuration.Kill.Pattern = @"^(K);([A-Fa-f0-9_]{16,32}|bot[0-9]+);(-?[0-9]+);(axis|allies|world);(.{1,24});([A-Fa-f0-9_]{16,32}|bot[0-9]+)?;-?([0-9]+);(axis|allies|world);(.{1,24})?;((?:[0-9]+|[a-z]+|_)+);([0-9]+);((?:[A-Z]|_)+);((?:[a-z]|_)+)$"; + _configuration.Kill.GroupMapping.Add(ParserRegex.GroupType.EventType, 1); + _configuration.Kill.GroupMapping.Add(ParserRegex.GroupType.TargetNetworkId, 2); + _configuration.Kill.GroupMapping.Add(ParserRegex.GroupType.TargetClientNumber, 3); + _configuration.Kill.GroupMapping.Add(ParserRegex.GroupType.TargetTeam, 4); + _configuration.Kill.GroupMapping.Add(ParserRegex.GroupType.TargetName, 5); + _configuration.Kill.GroupMapping.Add(ParserRegex.GroupType.OriginNetworkId, 6); + _configuration.Kill.GroupMapping.Add(ParserRegex.GroupType.OriginClientNumber, 7); + _configuration.Kill.GroupMapping.Add(ParserRegex.GroupType.OriginTeam, 8); + _configuration.Kill.GroupMapping.Add(ParserRegex.GroupType.OriginName, 9); + _configuration.Kill.GroupMapping.Add(ParserRegex.GroupType.Weapon, 10); + _configuration.Kill.GroupMapping.Add(ParserRegex.GroupType.Damage, 11); + _configuration.Kill.GroupMapping.Add(ParserRegex.GroupType.MeansOfDeath, 12); + _configuration.Kill.GroupMapping.Add(ParserRegex.GroupType.HitLocation, 13); } public IEventParserConfiguration Configuration { get => _configuration; set => _configuration = value; } @@ -33,7 +78,8 @@ namespace IW4MAdmin.Application.EventParsers if (eventType == "JoinTeam") { - var origin = server.GetClientsAsList().FirstOrDefault(c => c.NetworkId == lineSplit[1].ConvertLong()); + var origin = server.GetClientsAsList() + .FirstOrDefault(c => c.NetworkId == lineSplit[1].ConvertLong()); return new GameEvent() { @@ -46,15 +92,18 @@ namespace IW4MAdmin.Application.EventParsers if (eventType == "say" || eventType == "sayteam") { - var matchResult = Regex.Match(logLine, _configuration.SayRegex); + var matchResult = Regex.Match(logLine, _configuration.Say.Pattern); if (matchResult.Success) { - string message = matchResult.Groups[5].ToString() + string message = matchResult + .Groups[_configuration.Say.GroupMapping[ParserRegex.GroupType.Message]] + .ToString() .Replace("\x15", "") .Trim(); - var origin = server.GetClientsAsList().First(c => c.ClientNumber == Utilities.ClientIdFromString(lineSplit, 2)); + var origin = server.GetClientsAsList() + .First(c => c.NetworkId == matchResult.Groups[_configuration.Say.GroupMapping[ParserRegex.GroupType.OriginNetworkId]].ToString().ConvertLong()); if (message[0] == '!' || message[0] == '@') { @@ -83,17 +132,25 @@ namespace IW4MAdmin.Application.EventParsers { if (!server.CustomCallback) { - var origin = server.GetClientsAsList().First(c => c.ClientNumber == Utilities.ClientIdFromString(lineSplit, 6)); - var target = server.GetClientsAsList().First(c => c.ClientNumber == Utilities.ClientIdFromString(lineSplit, 2)); + var match = Regex.Match(logLine, _configuration.Kill.Pattern); - return new GameEvent() + if (match.Success) { - Type = GameEvent.EventType.Kill, - Data = logLine, - Origin = origin, - Target = target, - Owner = server - }; + var origin = server.GetClientsAsList() + .First(c => c.NetworkId == match.Groups[_configuration.Kill.GroupMapping[ParserRegex.GroupType.OriginNetworkId]].ToString().ConvertLong()); + var target = server.GetClientsAsList() + .First(c => c.NetworkId == match.Groups[_configuration.Kill.GroupMapping[ParserRegex.GroupType.TargetNetworkId]].ToString().ConvertLong()); + + + return new GameEvent() + { + Type = GameEvent.EventType.Kill, + Data = logLine, + Origin = origin, + Target = target, + Owner = server + }; + } } } @@ -129,12 +186,16 @@ namespace IW4MAdmin.Application.EventParsers // damage if (eventType == "D") { - if (!server.CustomCallback) + // if (!server.CustomCallback) { - if (Regex.Match(eventType, _configuration.DamageRegex).Success) + var regexMatch = Regex.Match(logLine, _configuration.Damage.Pattern); + + if (regexMatch.Success) { - var origin = server.GetClientsAsList().First(c => c.NetworkId == lineSplit[5].ConvertLong()); - var target = server.GetClientsAsList().First(c => c.NetworkId == lineSplit[1].ConvertLong()); + var origin = server.GetClientsAsList() + .First(c => c.NetworkId == regexMatch.Groups[_configuration.Damage.GroupMapping[ParserRegex.GroupType.OriginNetworkId]].ToString().ConvertLong()); + var target = server.GetClientsAsList() + .First(c => c.NetworkId == regexMatch.Groups[_configuration.Damage.GroupMapping[ParserRegex.GroupType.TargetNetworkId]].ToString().ConvertLong()); return new GameEvent() { @@ -151,7 +212,7 @@ namespace IW4MAdmin.Application.EventParsers // join if (eventType == "J") { - var regexMatch = Regex.Match(logLine, _configuration.JoinRegex); + var regexMatch = Regex.Match(logLine, _configuration.Join.Pattern); if (regexMatch.Success) { return new GameEvent() @@ -164,10 +225,10 @@ namespace IW4MAdmin.Application.EventParsers CurrentAlias = new EFAlias() { Active = false, - Name = regexMatch.Groups[4].ToString().StripColors(), + Name = regexMatch.Groups[_configuration.Join.GroupMapping[ParserRegex.GroupType.OriginName]].ToString().StripColors(), }, - NetworkId = regexMatch.Groups[2].ToString().ConvertLong(), - ClientNumber = Convert.ToInt32(regexMatch.Groups[3].ToString()), + NetworkId = regexMatch.Groups[_configuration.Join.GroupMapping[ParserRegex.GroupType.OriginNetworkId]].ToString().ConvertLong(), + ClientNumber = Convert.ToInt32(regexMatch.Groups[_configuration.Join.GroupMapping[ParserRegex.GroupType.OriginClientNumber]].ToString()), State = EFClient.ClientState.Connecting, CurrentServer = server } @@ -177,7 +238,7 @@ namespace IW4MAdmin.Application.EventParsers if (eventType == "Q") { - var regexMatch = Regex.Match(logLine, _configuration.QuitRegex); + var regexMatch = Regex.Match(logLine, _configuration.Quit.Pattern); if (regexMatch.Success) { return new GameEvent() @@ -190,10 +251,10 @@ namespace IW4MAdmin.Application.EventParsers CurrentAlias = new EFAlias() { Active = false, - Name = regexMatch.Groups[4].ToString().StripColors() + Name = regexMatch.Groups[_configuration.Quit.GroupMapping[ParserRegex.GroupType.OriginName]].ToString().StripColors() }, - NetworkId = regexMatch.Groups[2].ToString().ConvertLong(), - ClientNumber = Convert.ToInt32(regexMatch.Groups[3].ToString()), + NetworkId = regexMatch.Groups[_configuration.Quit.GroupMapping[ParserRegex.GroupType.OriginNetworkId]].ToString().ConvertLong(), + ClientNumber = Convert.ToInt32(regexMatch.Groups[_configuration.Quit.GroupMapping[ParserRegex.GroupType.OriginClientNumber]].ToString()), State = EFClient.ClientState.Disconnecting } }; diff --git a/Application/IW4MServer.cs b/Application/IW4MServer.cs index 65e21d868..205b96bea 100644 --- a/Application/IW4MServer.cs +++ b/Application/IW4MServer.cs @@ -671,7 +671,7 @@ namespace IW4MAdmin var version = await this.GetDvarAsync("version"); Version = version.Value; - //GameName = Utilities.GetGame(version.Value); + GameName = Utilities.GetGame(version.Value); if (GameName == Game.IW4) { diff --git a/SharedLibraryCore/Helpers/ParserRegex.cs b/SharedLibraryCore/Helpers/ParserRegex.cs new file mode 100644 index 000000000..b57c4f370 --- /dev/null +++ b/SharedLibraryCore/Helpers/ParserRegex.cs @@ -0,0 +1,41 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace SharedLibraryCore.Interfaces +{ + public sealed class ParserRegex + { + public enum GroupType + { + EventType, + OriginNetworkId, + TargetNetworkId, + OriginClientNumber, + TargetClientNumber, + OriginName, + TargetName, + OriginTeam, + TargetTeam, + Weapon, + Damage, + MeansOfDeath, + HitLocation, + Message, + RConClientNumber = 100, + RConScore = 101, + RConPing = 102, + RConNetworkId = 103, + RConName = 104, + RConIpAddress = 105, + AdditionalGroup = 200 + } + public string Pattern { get; set; } + public Dictionary GroupMapping { get; private set; } + + public ParserRegex() + { + GroupMapping = new Dictionary(); + } + } +} diff --git a/SharedLibraryCore/Interfaces/IEventParserConfiguration.cs b/SharedLibraryCore/Interfaces/IEventParserConfiguration.cs index 2292ed95f..a92e5b5c6 100644 --- a/SharedLibraryCore/Interfaces/IEventParserConfiguration.cs +++ b/SharedLibraryCore/Interfaces/IEventParserConfiguration.cs @@ -1,16 +1,12 @@ -using System; -using System.Collections.Generic; -using System.Text; - -namespace SharedLibraryCore.Interfaces +namespace SharedLibraryCore.Interfaces { public interface IEventParserConfiguration { string GameDirectory { get; set; } - string SayRegex { get; set; } - string JoinRegex { get; set; } - string QuitRegex { get; set; } - string KillRegex { get; set; } - string DamageRegex { get; set; } + ParserRegex Say { get; set; } + ParserRegex Join { get; set; } + ParserRegex Quit { get; set; } + ParserRegex Kill { get; set; } + ParserRegex Damage { get; set; } } }