mirror of
https://github.com/RaidMax/IW4M-Admin.git
synced 2025-06-10 15:20:48 -05:00
fixed loader offset
some stat stuff still not working made seperate parsers
This commit is contained in:
@ -1,13 +1,23 @@
|
||||
using System.Collections.Generic;
|
||||
using SharedLibraryCore.Interfaces;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace SharedLibraryCore.Configuration
|
||||
{
|
||||
public class ServerConfiguration
|
||||
public class ServerConfiguration : IBaseConfiguration
|
||||
{
|
||||
public string IPAddress { get; set; }
|
||||
public short Port { get; set; }
|
||||
public string Password { get; set; }
|
||||
public List<string> Rules { get; set; }
|
||||
public List<string> AutoMessages { get; set; }
|
||||
public bool UseT6MParser { get; set; }
|
||||
|
||||
public IBaseConfiguration Generate()
|
||||
{
|
||||
UseT6MParser = Utilities.PromptBool("Use T6M parser");
|
||||
return this;
|
||||
}
|
||||
|
||||
public string Name() => "ServerConfiguration";
|
||||
}
|
||||
}
|
||||
|
@ -1,11 +1,11 @@
|
||||
namespace SharedLibraryCore
|
||||
{
|
||||
public class DVAR<T>
|
||||
public class Dvar<T>
|
||||
{
|
||||
public string Name { get; private set; }
|
||||
public T Value;
|
||||
|
||||
public DVAR(string name)
|
||||
public Dvar(string name)
|
||||
{
|
||||
Name = name;
|
||||
}
|
||||
|
16
SharedLibraryCore/Interfaces/IRConParser.cs
Normal file
16
SharedLibraryCore/Interfaces/IRConParser.cs
Normal file
@ -0,0 +1,16 @@
|
||||
using SharedLibraryCore.Objects;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SharedLibraryCore.Interfaces
|
||||
{
|
||||
public interface IRConParser
|
||||
{
|
||||
Task<Dvar<T>> GetDvarAsync<T>(RCon.Connection connection, string dvarName);
|
||||
Task<bool> SetDvarAsync(RCon.Connection connection, string dvarName, object dvarValue);
|
||||
Task<string[]> ExecuteCommandAsync(RCon.Connection connection, string command);
|
||||
Task<List<Player>> GetStatusAsync(RCon.Connection connection);
|
||||
}
|
||||
}
|
@ -159,7 +159,7 @@ namespace SharedLibraryCore.RCon
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<string[]> SendQueryAsync(StaticHelpers.QueryType type, string parameters = "")
|
||||
public async Task<string[]> SendQueryAsync(StaticHelpers.QueryType type, string parameters = "", bool waitForResponse = true)
|
||||
{
|
||||
// will this really prevent flooding?
|
||||
if ((DateTime.Now - LastQuery).TotalMilliseconds < 35)
|
||||
@ -222,6 +222,9 @@ namespace SharedLibraryCore.RCon
|
||||
throw new NetworkException($"Unexpected error while sending data to server - {e.Message}");
|
||||
}
|
||||
|
||||
if (!waitForResponse)
|
||||
return await Task.FromResult(new string[] { "" });
|
||||
|
||||
var connectionState = new ConnectionState(ServerConnection);
|
||||
|
||||
retryReceive:
|
||||
@ -233,11 +236,6 @@ namespace SharedLibraryCore.RCon
|
||||
|
||||
if (!success)
|
||||
{
|
||||
// t6m doesn't respond to set requests
|
||||
if (type == StaticHelpers.QueryType.DVAR && parameters.Contains("set "))
|
||||
{
|
||||
return await Task.FromResult(new string[] { "" });
|
||||
}
|
||||
|
||||
FailedReceives++;
|
||||
#if DEBUG
|
||||
|
@ -8,6 +8,7 @@ using SharedLibraryCore.Helpers;
|
||||
using SharedLibraryCore.Objects;
|
||||
using SharedLibraryCore.Dtos;
|
||||
using SharedLibraryCore.Configuration;
|
||||
using SharedLibraryCore.Interfaces;
|
||||
|
||||
namespace SharedLibraryCore
|
||||
{
|
||||
@ -25,7 +26,7 @@ namespace SharedLibraryCore
|
||||
T6M,
|
||||
}
|
||||
|
||||
public Server(Interfaces.IManager mgr, ServerConfiguration config)
|
||||
public Server(IManager mgr, ServerConfiguration config)
|
||||
{
|
||||
Password = config.Password;
|
||||
IP = config.IPAddress;
|
||||
@ -313,6 +314,7 @@ namespace SharedLibraryCore
|
||||
public bool CustomCallback { get; protected set; }
|
||||
public string WorkingDirectory { get; protected set; }
|
||||
public RCon.Connection RemoteConnection { get; protected set; }
|
||||
public IRConParser RconParser { get; protected set; }
|
||||
|
||||
// Internal
|
||||
protected string IP;
|
||||
|
@ -52,54 +52,6 @@ namespace SharedLibraryCore
|
||||
return newStr;
|
||||
}
|
||||
|
||||
public static List<Player> PlayersFromStatus(this Server sv, string[] Status)
|
||||
{
|
||||
List<Player> StatusPlayers = new List<Player>();
|
||||
|
||||
foreach (String S in Status)
|
||||
{
|
||||
String responseLine = S.Trim();
|
||||
|
||||
if (Regex.Matches(responseLine, @"\d+$", RegexOptions.IgnoreCase).Count > 0 && responseLine.Length > 72) // its a client line!
|
||||
{
|
||||
String[] playerInfo = responseLine.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
|
||||
int cID = -1;
|
||||
int Ping = -1;
|
||||
|
||||
try
|
||||
{
|
||||
Ping = (sv.GameName != Game.T6M) ?
|
||||
Int32.Parse(playerInfo[2]) :
|
||||
Int32.Parse(playerInfo[3]);
|
||||
}
|
||||
|
||||
catch (FormatException) { }
|
||||
String cName = (sv.GameName != Game.T6M) ?
|
||||
Encoding.UTF8.GetString(Encoding.Convert(Encoding.UTF7, Encoding.UTF8, Encoding.UTF7.GetBytes(StripColors(responseLine.Substring(46, 18)).Trim()))) :
|
||||
Encoding.UTF8.GetString(Encoding.Convert(Encoding.UTF7, Encoding.UTF8, Encoding.UTF7.GetBytes(StripColors(responseLine.Substring(50, 15)).Trim())));
|
||||
long npID = sv.GameName != Game.T6M ?
|
||||
Regex.Match(responseLine, @"([a-z]|[0-9]){16}", RegexOptions.IgnoreCase).Value.ConvertLong() :
|
||||
playerInfo[4].ConvertLong();
|
||||
|
||||
int.TryParse(playerInfo[0], out cID);
|
||||
var regex = Regex.Match(responseLine, @"\d+\.\d+\.\d+.\d+\:\d{1,5}");
|
||||
#if DEBUG
|
||||
Ping = 1;
|
||||
#endif
|
||||
|
||||
int cIP = regex.Value.Split(':')[0].ConvertToIP();
|
||||
regex = Regex.Match(responseLine, @"[0-9]{1,2}\s+[0-9]+\s+");
|
||||
int score = (sv.GameName != Game.T6M) ?
|
||||
Int32.Parse(regex.Value.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)[1]) :
|
||||
Int32.Parse(playerInfo[1]);
|
||||
Player P = new Player() { Name = cName, NetworkId = npID, ClientNumber = cID, IPAddress = cIP, Ping = Ping, Score = score };
|
||||
StatusPlayers.Add(P);
|
||||
}
|
||||
}
|
||||
|
||||
return StatusPlayers;
|
||||
}
|
||||
|
||||
public static Player.Permission MatchPermission(String str)
|
||||
{
|
||||
String lookingFor = str.ToLower();
|
||||
@ -393,115 +345,23 @@ namespace SharedLibraryCore
|
||||
|
||||
public static string PromptString(string question)
|
||||
{
|
||||
Console.Write($"{question}: ");
|
||||
|
||||
string response;
|
||||
do
|
||||
{
|
||||
Console.Write($"{question}: ");
|
||||
response = Console.ReadLine();
|
||||
} while (string.IsNullOrWhiteSpace(response));
|
||||
|
||||
return response;
|
||||
}
|
||||
|
||||
public static async Task<DVAR<T>> GetDvarAsync<T>(this Server server, string dvarName)
|
||||
{
|
||||
string[] LineSplit = null;
|
||||
bool t6m = false;
|
||||
if (server.GameName == Game.UKN)
|
||||
{
|
||||
LineSplit = await server.RemoteConnection.SendQueryAsync(QueryType.COMMAND, $"get {dvarName}");
|
||||
if (LineSplit.Where(l => l.Contains("Unknown command")).Count() > 0)
|
||||
{
|
||||
LineSplit = await server.RemoteConnection.SendQueryAsync(QueryType.DVAR, dvarName);
|
||||
}
|
||||
public static async Task<Dvar<T>> GetDvarAsync<T>(this Server server, string dvarName) => await server.RconParser.GetDvarAsync<T>(server.RemoteConnection, dvarName);
|
||||
|
||||
else
|
||||
t6m = true;
|
||||
}
|
||||
public static async Task SetDvarAsync(this Server server, string dvarName, object dvarValue) => await server.RconParser.SetDvarAsync(server.RemoteConnection, dvarName, dvarValue);
|
||||
|
||||
else if (server.GameName == Game.T6M)
|
||||
{
|
||||
LineSplit = await server.RemoteConnection.SendQueryAsync(QueryType.COMMAND, $"get {dvarName}");
|
||||
}
|
||||
public static async Task<string[]> ExecuteCommandAsync(this Server server, string commandName) => await server.RconParser.ExecuteCommandAsync(server.RemoteConnection, commandName);
|
||||
|
||||
else
|
||||
{
|
||||
LineSplit = await server.RemoteConnection.SendQueryAsync(QueryType.DVAR, dvarName); ;
|
||||
}
|
||||
public static async Task<List<Player>> GetStatusAsync(this Server server) => await server.RconParser.GetStatusAsync(server.RemoteConnection);
|
||||
|
||||
if (server.GameName != Game.T6M && !t6m)
|
||||
{
|
||||
if (LineSplit.Length < 3)
|
||||
{
|
||||
var e = new Exceptions.DvarException($"DVAR \"{dvarName}\" does not exist");
|
||||
e.Data["dvar_name"] = dvarName;
|
||||
throw e;
|
||||
}
|
||||
|
||||
string[] ValueSplit = LineSplit[1].Split(new char[] { '"' }, StringSplitOptions.RemoveEmptyEntries);
|
||||
|
||||
if (ValueSplit.Length != 5)
|
||||
{
|
||||
var e = new Exceptions.DvarException($"DVAR \"{dvarName}\" does not exist");
|
||||
e.Data["dvar_name"] = dvarName;
|
||||
throw e;
|
||||
}
|
||||
|
||||
string DvarName = Regex.Replace(ValueSplit[0], @"\^[0-9]", "");
|
||||
string DvarCurrentValue = Regex.Replace(ValueSplit[2], @"\^[0-9]", "");
|
||||
string DvarDefaultValue = Regex.Replace(ValueSplit[4], @"\^[0-9]", "");
|
||||
|
||||
return new DVAR<T>(DvarName) { Value = (T)Convert.ChangeType(DvarCurrentValue, typeof(T)) };
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
if (LineSplit.Length < 2)
|
||||
{
|
||||
var e = new Exceptions.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 Exceptions.DvarException($"DVAR \"{dvarName}\" does not exist");
|
||||
e.Data["dvar_name"] = dvarName;
|
||||
throw e;
|
||||
}
|
||||
|
||||
string DvarName = dvarName;
|
||||
string DvarCurrentValue = Regex.Replace(ValueSplit[1], @"\^[0-9]", "");
|
||||
|
||||
return new DVAR<T>(DvarName) { Value = (T)Convert.ChangeType(DvarCurrentValue, typeof(T)) };
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
public static async Task SetDvarAsync(this Server server, string dvarName, object dvarValue)
|
||||
{
|
||||
await server.RemoteConnection.SendQueryAsync(QueryType.DVAR, $"set {dvarName} {dvarValue}");
|
||||
}
|
||||
|
||||
public static async Task<string[]> ExecuteCommandAsync(this Server server, string commandName)
|
||||
{
|
||||
return (await server.RemoteConnection.SendQueryAsync(QueryType.COMMAND, commandName)).Skip(1).ToArray();
|
||||
}
|
||||
|
||||
public static async Task<List<Player>> GetStatusAsync(this Server server)
|
||||
{
|
||||
#if DEBUG && DEBUG_PLAYERS
|
||||
string[] response = await Task.Run(() => System.IO.File.ReadAllLines("players.txt"));
|
||||
#else
|
||||
string[] response = await server.RemoteConnection.SendQueryAsync(QueryType.DVAR, "status");
|
||||
#endif
|
||||
return server.PlayersFromStatus(response);
|
||||
}
|
||||
|
||||
public static bool IsRunningOnMono() => Type.GetType("Mono.Runtime") != null;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user