2019-01-26 21:33:37 -05:00
|
|
|
|
using SharedLibraryCore;
|
|
|
|
|
using SharedLibraryCore.Database.Models;
|
|
|
|
|
using SharedLibraryCore.Exceptions;
|
2018-04-11 18:24:21 -04:00
|
|
|
|
using SharedLibraryCore.Interfaces;
|
|
|
|
|
using SharedLibraryCore.RCon;
|
2019-01-26 21:33:37 -05:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2018-04-11 18:24:21 -04:00
|
|
|
|
using System.Text;
|
2019-01-26 21:33:37 -05:00
|
|
|
|
using System.Text.RegularExpressions;
|
|
|
|
|
using System.Threading.Tasks;
|
2018-04-11 18:24:21 -04:00
|
|
|
|
|
2018-08-07 14:43:09 -04:00
|
|
|
|
namespace IW4MAdmin.Application.RconParsers
|
2018-04-11 18:24:21 -04:00
|
|
|
|
{
|
2018-04-13 02:32:30 -04:00
|
|
|
|
public class T6MRConParser : IRConParser
|
2018-04-11 18:24:21 -04:00
|
|
|
|
{
|
2019-01-26 21:33:37 -05:00
|
|
|
|
public IRConParserConfiguration Configuration { get; set; }
|
2019-02-02 19:54:30 -05:00
|
|
|
|
public string Version { get; set; } = "";
|
2018-04-13 02:32:30 -04:00
|
|
|
|
|
2019-01-26 21:33:37 -05:00
|
|
|
|
public T6MRConParser()
|
|
|
|
|
{
|
|
|
|
|
Configuration = new DynamicRConParserConfiguration()
|
|
|
|
|
{
|
|
|
|
|
CommandPrefixes = new CommandPrefix()
|
|
|
|
|
{
|
|
|
|
|
Tell = "tell {0} {1}",
|
|
|
|
|
Say = "say {0}",
|
|
|
|
|
Kick = "clientkick_for_reason {0} \"{1}\"",
|
|
|
|
|
Ban = "clientkick_for_reason {0} \"{1}\"",
|
|
|
|
|
TempBan = "clientkick_for_reason {0} \"{1}\""
|
|
|
|
|
},
|
|
|
|
|
GameName = Server.Game.T6M
|
|
|
|
|
};
|
|
|
|
|
}
|
2018-04-13 02:32:30 -04:00
|
|
|
|
|
2018-04-11 18:24:21 -04:00
|
|
|
|
public async Task<string[]> ExecuteCommandAsync(Connection connection, string command)
|
|
|
|
|
{
|
|
|
|
|
await connection.SendQueryAsync(StaticHelpers.QueryType.COMMAND, command, false);
|
|
|
|
|
return new string[] { "Command Executed" };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<Dvar<T>> GetDvarAsync<T>(Connection connection, string dvarName)
|
|
|
|
|
{
|
|
|
|
|
string[] LineSplit = await connection.SendQueryAsync(StaticHelpers.QueryType.COMMAND, $"get {dvarName}");
|
|
|
|
|
|
|
|
|
|
if (LineSplit.Length < 2)
|
|
|
|
|
{
|
|
|
|
|
var e = new DvarException($"DVAR \"{dvarName}\" does not exist");
|
|
|
|
|
e.Data["dvar_name"] = dvarName;
|
|
|
|
|
throw e;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
string[] ValueSplit = LineSplit[1].Split(new char[] { '"' });
|
|
|
|
|
|
|
|
|
|
if (ValueSplit.Length == 0)
|
|
|
|
|
{
|
|
|
|
|
var e = new DvarException($"DVAR \"{dvarName}\" does not exist");
|
|
|
|
|
e.Data["dvar_name"] = dvarName;
|
|
|
|
|
throw e;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
string DvarName = dvarName;
|
|
|
|
|
string DvarCurrentValue = Regex.Replace(ValueSplit[1], @"\^[0-9]", "");
|
|
|
|
|
|
2019-02-01 20:49:25 -05:00
|
|
|
|
return new Dvar<T>()
|
2018-04-11 18:24:21 -04:00
|
|
|
|
{
|
2019-02-01 20:49:25 -05:00
|
|
|
|
Value = (T)Convert.ChangeType(DvarCurrentValue, typeof(T)),
|
|
|
|
|
Name = DvarName
|
2018-04-11 18:24:21 -04:00
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-05 22:01:29 -05:00
|
|
|
|
public async Task<List<EFClient>> GetStatusAsync(Connection connection)
|
2018-04-11 18:24:21 -04:00
|
|
|
|
{
|
|
|
|
|
string[] response = await connection.SendQueryAsync(StaticHelpers.QueryType.COMMAND, "status");
|
|
|
|
|
return ClientsFromStatus(response);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<bool> SetDvarAsync(Connection connection, string dvarName, object dvarValue)
|
|
|
|
|
{
|
|
|
|
|
// T6M doesn't respond with anything when a value is set, so we can only hope for the best :c
|
|
|
|
|
await connection.SendQueryAsync(StaticHelpers.QueryType.DVAR, $"set {dvarName} {dvarValue}", false);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-05 22:01:29 -05:00
|
|
|
|
private List<EFClient> ClientsFromStatus(string[] status)
|
2018-04-11 18:24:21 -04:00
|
|
|
|
{
|
2018-11-05 22:01:29 -05:00
|
|
|
|
List<EFClient> StatusPlayers = new List<EFClient>();
|
2018-04-11 18:24:21 -04:00
|
|
|
|
|
|
|
|
|
foreach (string statusLine in status)
|
|
|
|
|
{
|
2018-04-13 02:32:30 -04:00
|
|
|
|
String responseLine = statusLine;
|
2018-04-11 18:24:21 -04:00
|
|
|
|
|
2018-04-14 00:51:38 -04:00
|
|
|
|
if (Regex.Matches(responseLine, @"^ *\d+", RegexOptions.IgnoreCase).Count > 0) // its a client line!
|
2018-04-11 18:24:21 -04:00
|
|
|
|
{
|
|
|
|
|
String[] playerInfo = responseLine.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
|
|
|
|
|
int clientId = -1;
|
|
|
|
|
int Ping = -1;
|
|
|
|
|
|
|
|
|
|
Int32.TryParse(playerInfo[3], out Ping);
|
2018-04-13 02:32:30 -04:00
|
|
|
|
var regex = Regex.Match(responseLine, @"\^7.*\ +0 ");
|
2018-04-21 18:18:20 -04:00
|
|
|
|
string name = Encoding.UTF8.GetString(Encoding.Convert(Utilities.EncodingType, Encoding.UTF8, Utilities.EncodingType.GetBytes(regex.Value.Substring(0, regex.Value.Length - 2).StripColors().Trim())));
|
2018-04-11 18:24:21 -04:00
|
|
|
|
long networkId = playerInfo[4].ConvertLong();
|
|
|
|
|
int.TryParse(playerInfo[0], out clientId);
|
2018-04-13 02:32:30 -04:00
|
|
|
|
regex = Regex.Match(responseLine, @"\d+\.\d+\.\d+.\d+\:\d{1,5}");
|
2018-04-11 18:24:21 -04:00
|
|
|
|
#if DEBUG
|
|
|
|
|
Ping = 1;
|
|
|
|
|
#endif
|
2018-12-16 22:16:56 -05:00
|
|
|
|
int? ipAddress = regex.Value.Split(':')[0].ConvertToIP();
|
2018-04-11 18:24:21 -04:00
|
|
|
|
regex = Regex.Match(responseLine, @"[0-9]{1,2}\s+[0-9]+\s+");
|
2018-11-05 22:01:29 -05:00
|
|
|
|
var p = new EFClient()
|
2018-04-11 18:24:21 -04:00
|
|
|
|
{
|
|
|
|
|
Name = name,
|
|
|
|
|
NetworkId = networkId,
|
|
|
|
|
ClientNumber = clientId,
|
|
|
|
|
IPAddress = ipAddress,
|
|
|
|
|
Ping = Ping,
|
2018-09-29 22:49:12 -04:00
|
|
|
|
Score = 0,
|
2018-11-05 22:01:29 -05:00
|
|
|
|
State = EFClient.ClientState.Connecting,
|
2018-12-16 22:16:56 -05:00
|
|
|
|
IsBot = ipAddress == null
|
2018-04-28 01:22:18 -04:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if (p.IsBot)
|
2019-01-26 21:33:37 -05:00
|
|
|
|
{
|
2018-04-28 01:22:18 -04:00
|
|
|
|
p.NetworkId = -p.ClientNumber;
|
2019-01-26 21:33:37 -05:00
|
|
|
|
}
|
2018-04-28 01:22:18 -04:00
|
|
|
|
|
|
|
|
|
StatusPlayers.Add(p);
|
2018-04-11 18:24:21 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return StatusPlayers;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|