2020-06-16 18:16:12 -04:00
|
|
|
|
|
|
|
|
|
using SharedLibraryCore.Database.Models;
|
2019-06-30 14:37:59 -04:00
|
|
|
|
using SharedLibraryCore.Helpers;
|
2020-04-21 18:34:00 -04:00
|
|
|
|
using SharedLibraryCore.Interfaces;
|
2018-11-25 21:00:36 -05:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
using System.Globalization;
|
2019-04-07 21:14:59 -04:00
|
|
|
|
using System.IO;
|
2018-11-25 21:00:36 -05:00
|
|
|
|
using System.Linq;
|
2019-04-08 13:29:48 -04:00
|
|
|
|
using System.Net;
|
2018-11-25 21:00:36 -05:00
|
|
|
|
using System.Reflection;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Text.RegularExpressions;
|
2020-05-04 17:50:02 -04:00
|
|
|
|
using System.Threading;
|
2018-11-25 21:00:36 -05:00
|
|
|
|
using System.Threading.Tasks;
|
2019-07-24 11:36:37 -04:00
|
|
|
|
using static SharedLibraryCore.Database.Models.EFClient;
|
2019-05-29 17:55:35 -04:00
|
|
|
|
using static SharedLibraryCore.Database.Models.EFPenalty;
|
2018-11-25 21:00:36 -05:00
|
|
|
|
using static SharedLibraryCore.Server;
|
2018-09-07 23:29:42 -04:00
|
|
|
|
|
2018-04-08 02:44:42 -04:00
|
|
|
|
namespace SharedLibraryCore
|
2015-08-20 01:06:44 -04:00
|
|
|
|
{
|
2017-05-26 18:49:27 -04:00
|
|
|
|
public static class Utilities
|
2015-08-20 01:06:44 -04:00
|
|
|
|
{
|
2018-10-05 23:10:39 -04:00
|
|
|
|
#if DEBUG == true
|
2019-10-07 11:26:07 -04:00
|
|
|
|
public static string OperatingDirectory => $"{Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)}{Path.DirectorySeparatorChar}";
|
2018-10-05 23:10:39 -04:00
|
|
|
|
#else
|
|
|
|
|
public static string OperatingDirectory => $"{Path.GetDirectoryName(Assembly.GetEntryAssembly().Location)}{Path.DirectorySeparatorChar}..{Path.DirectorySeparatorChar}";
|
|
|
|
|
#endif
|
2018-04-21 18:18:20 -04:00
|
|
|
|
public static Encoding EncodingType;
|
2018-08-28 17:32:59 -04:00
|
|
|
|
public static Localization.Layout CurrentLocalization = new Localization.Layout(new Dictionary<string, string>());
|
2020-04-26 22:12:49 -04:00
|
|
|
|
public static TimeSpan DefaultCommandTimeout { get; set; } = new TimeSpan(0, 0, 25);
|
2020-04-13 17:16:31 -04:00
|
|
|
|
public static char[] DirectorySeparatorChars = new[] { '\\', '/' };
|
2020-04-26 22:12:49 -04:00
|
|
|
|
public static char CommandPrefix { get; set; } = '!';
|
2018-11-25 21:00:36 -05:00
|
|
|
|
public static EFClient IW4MAdminClient(Server server = null)
|
2018-10-08 22:15:59 -04:00
|
|
|
|
{
|
2018-11-25 21:00:36 -05:00
|
|
|
|
return new EFClient()
|
|
|
|
|
{
|
|
|
|
|
ClientId = 1,
|
|
|
|
|
State = EFClient.ClientState.Connected,
|
|
|
|
|
Level = EFClient.Permission.Console,
|
|
|
|
|
CurrentServer = server,
|
|
|
|
|
CurrentAlias = new EFAlias()
|
|
|
|
|
{
|
|
|
|
|
Name = "IW4MAdmin"
|
2018-12-03 20:21:13 -05:00
|
|
|
|
},
|
|
|
|
|
AdministeredPenalties = new List<EFPenalty>()
|
2018-11-25 21:00:36 -05:00
|
|
|
|
};
|
|
|
|
|
}
|
2020-05-04 17:50:02 -04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// fallback id for world events
|
|
|
|
|
/// </summary>
|
|
|
|
|
public const long WORLD_ID = -1;
|
2018-02-21 20:29:23 -05:00
|
|
|
|
|
2018-08-26 20:20:47 -04:00
|
|
|
|
public static string HttpRequest(string location, string header, string headerValue)
|
|
|
|
|
{
|
|
|
|
|
using (var RequestClient = new System.Net.Http.HttpClient())
|
|
|
|
|
{
|
|
|
|
|
RequestClient.DefaultRequestHeaders.Add(header, headerValue);
|
|
|
|
|
string response = RequestClient.GetStringAsync(location).Result;
|
|
|
|
|
return response;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-08-20 01:06:44 -04:00
|
|
|
|
//Get string with specified number of spaces -- really only for visual output
|
2017-06-12 17:47:31 -04:00
|
|
|
|
public static String GetSpaces(int Num)
|
2015-08-20 01:06:44 -04:00
|
|
|
|
{
|
|
|
|
|
String SpaceString = String.Empty;
|
|
|
|
|
while (Num > 0)
|
|
|
|
|
{
|
|
|
|
|
SpaceString += ' ';
|
|
|
|
|
Num--;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return SpaceString;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//Remove words from a space delimited string
|
2017-05-26 18:49:27 -04:00
|
|
|
|
public static String RemoveWords(this string str, int num)
|
2015-08-20 01:06:44 -04:00
|
|
|
|
{
|
2017-05-26 18:49:27 -04:00
|
|
|
|
if (str == null || str.Length == 0)
|
2018-11-25 21:00:36 -05:00
|
|
|
|
{
|
2017-05-26 18:49:27 -04:00
|
|
|
|
return "";
|
2018-11-25 21:00:36 -05:00
|
|
|
|
}
|
2017-05-26 18:49:27 -04:00
|
|
|
|
|
2015-08-20 01:06:44 -04:00
|
|
|
|
String newStr = String.Empty;
|
|
|
|
|
String[] tmp = str.Split(' ');
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < tmp.Length; i++)
|
|
|
|
|
{
|
|
|
|
|
if (i >= num)
|
2018-11-25 21:00:36 -05:00
|
|
|
|
{
|
2015-08-20 01:06:44 -04:00
|
|
|
|
newStr += tmp[i] + ' ';
|
2018-11-25 21:00:36 -05:00
|
|
|
|
}
|
2015-08-20 01:06:44 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return newStr;
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-04 13:40:23 -04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// caps client name to the specified character length - 3
|
|
|
|
|
/// and adds ellipses to the end of the reamining client name
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="str">client name</param>
|
|
|
|
|
/// <param name="maxLength">max number of characters for the name</param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public static string CapClientName(this string str, int maxLength) =>
|
|
|
|
|
str.Length > maxLength ?
|
|
|
|
|
$"{str.Substring(0, maxLength - 3)}..." :
|
|
|
|
|
str;
|
|
|
|
|
|
2018-09-16 16:34:16 -04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// helper method to get the information about an exception and inner exceptions
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="ex"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public static string GetExceptionInfo(this Exception ex)
|
|
|
|
|
{
|
|
|
|
|
var sb = new StringBuilder();
|
|
|
|
|
int depth = 0;
|
|
|
|
|
while (ex != null)
|
|
|
|
|
{
|
|
|
|
|
sb.AppendLine($"Exception[{depth}] Name: {ex.GetType().FullName}");
|
|
|
|
|
sb.AppendLine($"Exception[{depth}] Message: {ex.Message}");
|
|
|
|
|
sb.AppendLine($"Exception[{depth}] Call Stack: {ex.StackTrace}");
|
|
|
|
|
sb.AppendLine($"Exception[{depth}] Source: {ex.Source}");
|
|
|
|
|
depth++;
|
|
|
|
|
ex = ex.InnerException;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return sb.ToString();
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-05 22:01:29 -05:00
|
|
|
|
public static EFClient.Permission MatchPermission(String str)
|
2015-08-20 01:06:44 -04:00
|
|
|
|
{
|
|
|
|
|
String lookingFor = str.ToLower();
|
2017-06-12 17:47:31 -04:00
|
|
|
|
|
2018-11-05 22:01:29 -05:00
|
|
|
|
for (EFClient.Permission Perm = EFClient.Permission.User; Perm < EFClient.Permission.Console; Perm++)
|
2018-11-25 21:00:36 -05:00
|
|
|
|
{
|
2018-08-03 22:11:58 -04:00
|
|
|
|
if (lookingFor.Contains(Perm.ToString().ToLower())
|
|
|
|
|
|| lookingFor.Contains(CurrentLocalization.LocalizationIndex[$"GLOBAL_PERMISSION_{Perm.ToString().ToUpper()}"].ToLower()))
|
2018-11-25 21:00:36 -05:00
|
|
|
|
{
|
2015-08-20 01:06:44 -04:00
|
|
|
|
return Perm;
|
2018-11-25 21:00:36 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
2015-08-20 01:06:44 -04:00
|
|
|
|
|
2018-11-05 22:01:29 -05:00
|
|
|
|
return EFClient.Permission.Banned;
|
2015-08-20 01:06:44 -04:00
|
|
|
|
}
|
|
|
|
|
|
2015-08-23 17:58:48 -04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Remove all IW Engine color codes
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="str">String containing color codes</param>
|
|
|
|
|
/// <returns></returns>
|
2019-08-01 10:37:33 -04:00
|
|
|
|
public static string StripColors(this string str)
|
2015-08-20 01:06:44 -04:00
|
|
|
|
{
|
|
|
|
|
if (str == null)
|
2018-11-25 21:00:36 -05:00
|
|
|
|
{
|
2015-08-20 01:06:44 -04:00
|
|
|
|
return "";
|
2018-11-25 21:00:36 -05:00
|
|
|
|
}
|
|
|
|
|
|
2018-03-06 02:22:19 -05:00
|
|
|
|
str = Regex.Replace(str, @"(\^+((?![a-z]|[A-Z]).){0,1})+", "");
|
2019-08-02 19:04:34 -04:00
|
|
|
|
return str;
|
2015-08-20 01:06:44 -04:00
|
|
|
|
}
|
|
|
|
|
|
2019-08-02 19:04:34 -04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// returns a "fixed" string that prevents message truncation in IW4 (and probably other Q3 clients)
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="str"></param>
|
|
|
|
|
/// <returns></returns>
|
2020-05-16 21:55:18 -04:00
|
|
|
|
public static string FixIW4ForwardSlash(this string str) => str.Replace("//", "/ /");
|
2019-08-02 19:04:34 -04:00
|
|
|
|
|
2019-08-04 21:38:55 -04:00
|
|
|
|
private static readonly IList<string> _zmGameTypes = new[] { "zclassic", "zstandard", "zcleansed", "zgrief" };
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// indicates if the given server is running a zombie game mode
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="server"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public static bool IsZombieServer(this Server server) => server.GameName == Game.T6 && _zmGameTypes.Contains(server.Gametype.ToLower());
|
|
|
|
|
|
2015-08-23 17:58:48 -04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Get the IW Engine color code corresponding to an admin level
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="level">Specified player level</param>
|
|
|
|
|
/// <returns></returns>
|
2018-11-05 22:01:29 -05:00
|
|
|
|
public static String ConvertLevelToColor(EFClient.Permission level, string localizedLevel)
|
2015-08-20 01:06:44 -04:00
|
|
|
|
{
|
2018-08-03 22:11:58 -04:00
|
|
|
|
char colorCode = '6';
|
|
|
|
|
// todo: maybe make this game independant?
|
2015-08-20 01:06:44 -04:00
|
|
|
|
switch (level)
|
|
|
|
|
{
|
2018-11-05 22:01:29 -05:00
|
|
|
|
case EFClient.Permission.Banned:
|
2018-08-03 22:11:58 -04:00
|
|
|
|
colorCode = '1';
|
|
|
|
|
break;
|
2018-11-05 22:01:29 -05:00
|
|
|
|
case EFClient.Permission.Flagged:
|
2018-08-03 22:11:58 -04:00
|
|
|
|
colorCode = '9';
|
|
|
|
|
break;
|
2018-11-05 22:01:29 -05:00
|
|
|
|
case EFClient.Permission.Owner:
|
2018-08-03 22:11:58 -04:00
|
|
|
|
colorCode = '5';
|
|
|
|
|
break;
|
2018-11-05 22:01:29 -05:00
|
|
|
|
case EFClient.Permission.User:
|
2018-08-03 22:11:58 -04:00
|
|
|
|
colorCode = '2';
|
|
|
|
|
break;
|
2018-11-05 22:01:29 -05:00
|
|
|
|
case EFClient.Permission.Trusted:
|
2018-08-03 22:11:58 -04:00
|
|
|
|
colorCode = '3';
|
|
|
|
|
break;
|
2015-08-20 01:06:44 -04:00
|
|
|
|
default:
|
2018-08-03 22:11:58 -04:00
|
|
|
|
break;
|
2015-08-20 01:06:44 -04:00
|
|
|
|
}
|
2018-08-03 22:11:58 -04:00
|
|
|
|
|
|
|
|
|
return $"^{colorCode}{localizedLevel ?? level.ToString()}";
|
2015-08-20 01:06:44 -04:00
|
|
|
|
}
|
|
|
|
|
|
2018-11-25 21:00:36 -05:00
|
|
|
|
public static string ToLocalizedLevelName(this EFClient.Permission perm)
|
|
|
|
|
{
|
|
|
|
|
return CurrentLocalization.LocalizationIndex[$"GLOBAL_PERMISSION_{perm.ToString().ToUpper()}"];
|
|
|
|
|
}
|
2018-08-03 22:11:58 -04:00
|
|
|
|
|
2019-02-18 20:30:38 -05:00
|
|
|
|
public async static Task<string> ProcessMessageToken(this Server server, IList<Helpers.MessageToken> tokens, String str)
|
2015-08-20 01:06:44 -04:00
|
|
|
|
{
|
2017-05-31 01:31:56 -04:00
|
|
|
|
MatchCollection RegexMatches = Regex.Matches(str, @"\{\{[A-Z]+\}\}", RegexOptions.IgnoreCase);
|
|
|
|
|
foreach (Match M in RegexMatches)
|
2015-08-20 01:06:44 -04:00
|
|
|
|
{
|
|
|
|
|
String Match = M.Value;
|
|
|
|
|
String Identifier = M.Value.Substring(2, M.Length - 4);
|
2015-08-22 02:04:30 -04:00
|
|
|
|
|
2017-05-31 01:31:56 -04:00
|
|
|
|
var found = tokens.FirstOrDefault(t => t.Name.ToLower() == Identifier.ToLower());
|
2015-08-22 02:04:30 -04:00
|
|
|
|
|
2017-05-31 01:31:56 -04:00
|
|
|
|
if (found != null)
|
2018-11-25 21:00:36 -05:00
|
|
|
|
{
|
2019-02-19 20:39:09 -05:00
|
|
|
|
str = str.Replace(Match, await found.ProcessAsync(server));
|
2018-11-25 21:00:36 -05:00
|
|
|
|
}
|
2015-08-20 01:06:44 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return str;
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-31 21:40:03 -04:00
|
|
|
|
public static bool IsBroadcastCommand(this string str, string broadcastCommandPrefix)
|
2017-05-31 01:31:56 -04:00
|
|
|
|
{
|
2020-07-31 21:40:03 -04:00
|
|
|
|
return str.StartsWith(broadcastCommandPrefix);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static IManagerCommand AsCommand(this GameEvent gameEvent)
|
|
|
|
|
{
|
|
|
|
|
return gameEvent.Extra as IManagerCommand;
|
2017-05-31 01:31:56 -04:00
|
|
|
|
}
|
|
|
|
|
|
2015-08-23 17:58:48 -04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Get the full gametype name
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="input">Shorthand gametype reported from server</param>
|
|
|
|
|
/// <returns></returns>
|
2019-01-03 15:39:22 -05:00
|
|
|
|
public static string GetLocalizedGametype(String input)
|
2015-08-20 01:06:44 -04:00
|
|
|
|
{
|
|
|
|
|
switch (input)
|
|
|
|
|
{
|
|
|
|
|
case "dm":
|
|
|
|
|
return "Deathmatch";
|
|
|
|
|
case "war":
|
|
|
|
|
return "Team Deathmatch";
|
|
|
|
|
case "koth":
|
|
|
|
|
return "Headquarters";
|
|
|
|
|
case "ctf":
|
|
|
|
|
return "Capture The Flag";
|
|
|
|
|
case "dd":
|
|
|
|
|
return "Demolition";
|
|
|
|
|
case "dom":
|
|
|
|
|
return "Domination";
|
|
|
|
|
case "sab":
|
|
|
|
|
return "Sabotage";
|
|
|
|
|
case "sd":
|
|
|
|
|
return "Search & Destroy";
|
|
|
|
|
case "vip":
|
|
|
|
|
return "Very Important Person";
|
|
|
|
|
case "gtnw":
|
|
|
|
|
return "Global Thermonuclear War";
|
|
|
|
|
case "oitc":
|
|
|
|
|
return "One In The Chamber";
|
|
|
|
|
case "arena":
|
|
|
|
|
return "Arena";
|
|
|
|
|
case "dzone":
|
|
|
|
|
return "Drop Zone";
|
|
|
|
|
case "gg":
|
|
|
|
|
return "Gun Game";
|
|
|
|
|
case "snipe":
|
|
|
|
|
return "Sniping";
|
|
|
|
|
case "ss":
|
|
|
|
|
return "Sharp Shooter";
|
|
|
|
|
case "m40a3":
|
|
|
|
|
return "M40A3";
|
|
|
|
|
case "fo":
|
|
|
|
|
return "Face Off";
|
|
|
|
|
case "dmc":
|
|
|
|
|
return "Deathmatch Classic";
|
|
|
|
|
case "killcon":
|
|
|
|
|
return "Kill Confirmed";
|
|
|
|
|
case "oneflag":
|
|
|
|
|
return "One Flag CTF";
|
|
|
|
|
default:
|
2017-05-26 18:49:27 -04:00
|
|
|
|
return input;
|
2015-08-20 01:06:44 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-04 17:50:02 -04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// converts a string to numerical guid
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="str">source string for guid</param>
|
|
|
|
|
/// <param name="numberStyle">how to parse the guid</param>
|
|
|
|
|
/// <param name="fallback">value to use if string is empty</param>
|
|
|
|
|
/// <returns></returns>
|
2020-01-15 19:43:52 -05:00
|
|
|
|
public static long ConvertGuidToLong(this string str, NumberStyles numberStyle, long? fallback = null)
|
2018-02-10 23:33:42 -05:00
|
|
|
|
{
|
2019-06-13 20:10:08 -04:00
|
|
|
|
str = str.Substring(0, Math.Min(str.Length, 19));
|
2020-05-04 17:50:02 -04:00
|
|
|
|
var parsableAsNumber = Regex.Match(str, @"([A-F]|[a-f]|[0-9])+").Value;
|
2019-02-10 21:05:50 -05:00
|
|
|
|
|
2019-05-13 11:36:11 -04:00
|
|
|
|
if (string.IsNullOrWhiteSpace(str) && fallback.HasValue)
|
|
|
|
|
{
|
|
|
|
|
return fallback.Value;
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-04 17:50:02 -04:00
|
|
|
|
long id;
|
|
|
|
|
if (!string.IsNullOrEmpty(parsableAsNumber))
|
2018-11-25 21:00:36 -05:00
|
|
|
|
{
|
2020-05-04 17:50:02 -04:00
|
|
|
|
if (numberStyle == NumberStyles.Integer)
|
|
|
|
|
{
|
|
|
|
|
long.TryParse(str, numberStyle, CultureInfo.InvariantCulture, out id);
|
|
|
|
|
|
|
|
|
|
if (id < 0)
|
|
|
|
|
{
|
|
|
|
|
id = (uint)id;
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-01-15 19:43:52 -05:00
|
|
|
|
|
2020-05-04 17:50:02 -04:00
|
|
|
|
else
|
2019-06-15 09:52:59 -04:00
|
|
|
|
{
|
2020-05-04 17:50:02 -04:00
|
|
|
|
long.TryParse(str.Length > 16 ? str.Substring(0, 16) : str, numberStyle, CultureInfo.InvariantCulture, out id);
|
2019-06-15 09:52:59 -04:00
|
|
|
|
}
|
2018-11-25 21:00:36 -05:00
|
|
|
|
}
|
|
|
|
|
|
2020-01-15 19:43:52 -05:00
|
|
|
|
else
|
2019-02-09 16:35:13 -05:00
|
|
|
|
{
|
2020-05-04 17:50:02 -04:00
|
|
|
|
// this is a special case for when a real guid is not provided, so we generated it from another source
|
|
|
|
|
id = str.GenerateGuidFromString();
|
2019-05-02 23:33:38 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (id == 0)
|
|
|
|
|
{
|
2019-06-15 09:52:59 -04:00
|
|
|
|
throw new FormatException($"Could not parse client GUID - {str}");
|
2018-11-25 21:00:36 -05:00
|
|
|
|
}
|
|
|
|
|
|
2019-05-02 23:33:38 -04:00
|
|
|
|
return id;
|
2018-02-10 23:33:42 -05:00
|
|
|
|
}
|
|
|
|
|
|
2020-05-04 17:50:02 -04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// determines if the guid provided appears to be a bot guid
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="guid">value of the guid</param>
|
|
|
|
|
/// <returns>true if is bot guid, otherwise false</returns>
|
|
|
|
|
public static bool IsBotGuid(this string guid)
|
|
|
|
|
{
|
|
|
|
|
return guid.Contains("bot") || guid == "0";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// generates a numerical hashcode from a string value
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="value">value string</param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public static long GenerateGuidFromString(this string value) => string.IsNullOrEmpty(value) ? -1 : HashCode.Combine(value.StripColors());
|
|
|
|
|
|
2018-12-16 22:16:56 -05:00
|
|
|
|
public static int? ConvertToIP(this string str)
|
2018-02-10 23:33:42 -05:00
|
|
|
|
{
|
2019-05-03 21:13:51 -04:00
|
|
|
|
bool success = IPAddress.TryParse(str, out IPAddress ip);
|
2019-01-03 15:39:22 -05:00
|
|
|
|
return success && ip.GetAddressBytes().Count(_byte => _byte == 0) != 4 ?
|
2018-12-30 19:13:13 -05:00
|
|
|
|
(int?)BitConverter.ToInt32(ip.GetAddressBytes(), 0) :
|
|
|
|
|
null;
|
2018-02-10 23:33:42 -05:00
|
|
|
|
}
|
|
|
|
|
|
2018-11-25 21:00:36 -05:00
|
|
|
|
public static string ConvertIPtoString(this int? ip)
|
2018-02-10 23:33:42 -05:00
|
|
|
|
{
|
2019-05-03 21:13:51 -04:00
|
|
|
|
return !ip.HasValue ? "" : new IPAddress(BitConverter.GetBytes(ip.Value)).ToString();
|
2018-02-10 23:33:42 -05:00
|
|
|
|
}
|
|
|
|
|
|
2019-01-03 15:39:22 -05:00
|
|
|
|
public static string GetTimePassed(DateTime start)
|
2017-10-03 19:17:35 -04:00
|
|
|
|
{
|
|
|
|
|
return GetTimePassed(start, true);
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-03 15:39:22 -05:00
|
|
|
|
public static string GetTimePassed(DateTime start, bool includeAgo)
|
2015-08-20 01:06:44 -04:00
|
|
|
|
{
|
2017-11-29 19:35:50 -05:00
|
|
|
|
TimeSpan Elapsed = DateTime.UtcNow - start;
|
2018-08-02 21:52:35 -04:00
|
|
|
|
string ago = includeAgo ? $" {CurrentLocalization.LocalizationIndex["WEBFRONT_PENALTY_TEMPLATE_AGO"]}" : "";
|
2015-08-20 01:06:44 -04:00
|
|
|
|
|
2018-08-02 21:52:35 -04:00
|
|
|
|
if (Elapsed.TotalSeconds < 30)
|
|
|
|
|
{
|
2019-05-08 21:34:17 -04:00
|
|
|
|
return CurrentLocalization.LocalizationIndex["GLOBAL_TIME_JUSTNOW"] + ago;
|
2018-08-02 21:52:35 -04:00
|
|
|
|
}
|
2015-08-20 01:06:44 -04:00
|
|
|
|
if (Elapsed.TotalMinutes < 120)
|
2017-05-26 18:49:27 -04:00
|
|
|
|
{
|
|
|
|
|
if (Elapsed.TotalMinutes < 1.5)
|
2018-11-25 21:00:36 -05:00
|
|
|
|
{
|
2018-08-02 21:52:35 -04:00
|
|
|
|
return $"1 {CurrentLocalization.LocalizationIndex["GLOBAL_TIME_MINUTES"]}{ago}";
|
2018-11-25 21:00:36 -05:00
|
|
|
|
}
|
|
|
|
|
|
2018-08-02 21:52:35 -04:00
|
|
|
|
return Math.Round(Elapsed.TotalMinutes, 0) + $" {CurrentLocalization.LocalizationIndex["GLOBAL_TIME_MINUTES"]}{ago}";
|
2017-05-26 18:49:27 -04:00
|
|
|
|
}
|
2015-08-20 01:06:44 -04:00
|
|
|
|
if (Elapsed.TotalHours <= 24)
|
2017-05-26 18:49:27 -04:00
|
|
|
|
{
|
|
|
|
|
if (Elapsed.TotalHours < 1.5)
|
2018-11-25 21:00:36 -05:00
|
|
|
|
{
|
2018-08-02 21:52:35 -04:00
|
|
|
|
return $"1 {CurrentLocalization.LocalizationIndex["GLOBAL_TIME_HOURS"]}{ago}";
|
2018-11-25 21:00:36 -05:00
|
|
|
|
}
|
|
|
|
|
|
2018-08-02 21:52:35 -04:00
|
|
|
|
return Math.Round(Elapsed.TotalHours, 0) + $" { CurrentLocalization.LocalizationIndex["GLOBAL_TIME_HOURS"]}{ago}";
|
2017-05-26 18:49:27 -04:00
|
|
|
|
}
|
2018-08-02 21:52:35 -04:00
|
|
|
|
if (Elapsed.TotalDays <= 90)
|
2017-05-26 18:49:27 -04:00
|
|
|
|
{
|
2017-11-25 20:29:58 -05:00
|
|
|
|
if (Elapsed.TotalDays < 1.5)
|
2018-11-25 21:00:36 -05:00
|
|
|
|
{
|
2018-08-02 21:52:35 -04:00
|
|
|
|
return $"1 {CurrentLocalization.LocalizationIndex["GLOBAL_TIME_DAYS"]}{ago}";
|
2018-11-25 21:00:36 -05:00
|
|
|
|
}
|
|
|
|
|
|
2018-08-02 21:52:35 -04:00
|
|
|
|
return Math.Round(Elapsed.TotalDays, 0) + $" {CurrentLocalization.LocalizationIndex["GLOBAL_TIME_DAYS"]}{ago}";
|
|
|
|
|
}
|
|
|
|
|
if (Elapsed.TotalDays <= 365)
|
|
|
|
|
{
|
|
|
|
|
return $"{Math.Round(Elapsed.TotalDays / 7)} {CurrentLocalization.LocalizationIndex["GLOBAL_TIME_WEEKS"]}{ago}";
|
2017-05-26 18:49:27 -04:00
|
|
|
|
}
|
2015-08-20 01:06:44 -04:00
|
|
|
|
else
|
2018-08-02 21:52:35 -04:00
|
|
|
|
{
|
|
|
|
|
return $"{Math.Round(Elapsed.TotalDays / 30, 0)} {CurrentLocalization.LocalizationIndex["GLOBAL_TIME_MONTHS"]}{ago}";
|
|
|
|
|
}
|
2015-08-20 01:06:44 -04:00
|
|
|
|
}
|
2017-08-08 22:44:52 -04:00
|
|
|
|
|
|
|
|
|
public static Game GetGame(string gameName)
|
|
|
|
|
{
|
2019-02-01 20:49:25 -05:00
|
|
|
|
if (string.IsNullOrEmpty(gameName))
|
|
|
|
|
{
|
|
|
|
|
return Game.UKN;
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-08 22:44:52 -04:00
|
|
|
|
if (gameName.Contains("IW4"))
|
2018-11-25 21:00:36 -05:00
|
|
|
|
{
|
2017-08-08 22:44:52 -04:00
|
|
|
|
return Game.IW4;
|
2018-11-25 21:00:36 -05:00
|
|
|
|
}
|
|
|
|
|
|
2017-08-08 22:44:52 -04:00
|
|
|
|
if (gameName.Contains("CoD4"))
|
2018-11-25 21:00:36 -05:00
|
|
|
|
{
|
2017-08-08 22:44:52 -04:00
|
|
|
|
return Game.IW3;
|
2018-11-25 21:00:36 -05:00
|
|
|
|
}
|
|
|
|
|
|
2018-05-11 00:52:20 -04:00
|
|
|
|
if (gameName.Contains("COD_WaW"))
|
2018-11-25 21:00:36 -05:00
|
|
|
|
{
|
2017-08-08 22:44:52 -04:00
|
|
|
|
return Game.T4;
|
2018-11-25 21:00:36 -05:00
|
|
|
|
}
|
|
|
|
|
|
2019-02-05 19:02:45 -05:00
|
|
|
|
if (gameName.Contains("T5"))
|
2018-11-25 21:00:36 -05:00
|
|
|
|
{
|
2017-08-08 22:44:52 -04:00
|
|
|
|
return Game.T5;
|
2018-11-25 21:00:36 -05:00
|
|
|
|
}
|
|
|
|
|
|
2017-08-08 22:44:52 -04:00
|
|
|
|
if (gameName.Contains("IW5"))
|
2018-11-25 21:00:36 -05:00
|
|
|
|
{
|
2017-08-08 22:44:52 -04:00
|
|
|
|
return Game.IW5;
|
2018-11-25 21:00:36 -05:00
|
|
|
|
}
|
|
|
|
|
|
2018-04-08 17:50:58 -04:00
|
|
|
|
if (gameName.Contains("COD_T6_S"))
|
2018-11-25 21:00:36 -05:00
|
|
|
|
{
|
2019-02-05 19:02:45 -05:00
|
|
|
|
return Game.T6;
|
2018-11-25 21:00:36 -05:00
|
|
|
|
}
|
2017-08-08 22:44:52 -04:00
|
|
|
|
|
|
|
|
|
return Game.UKN;
|
|
|
|
|
}
|
2017-08-23 18:29:48 -04:00
|
|
|
|
|
2018-04-19 18:52:48 -04:00
|
|
|
|
public static string EscapeMarkdown(this string markdownString)
|
|
|
|
|
{
|
|
|
|
|
return markdownString.Replace("<", "\\<").Replace(">", "\\>").Replace("|", "\\|");
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-23 18:29:48 -04:00
|
|
|
|
public static TimeSpan ParseTimespan(this string input)
|
|
|
|
|
{
|
2018-10-02 13:39:08 -04:00
|
|
|
|
var expressionMatch = Regex.Match(input, @"([0-9]+)(\w+)");
|
2017-08-23 18:29:48 -04:00
|
|
|
|
|
|
|
|
|
if (!expressionMatch.Success) // fallback to default tempban length of 1 hour
|
2018-11-25 21:00:36 -05:00
|
|
|
|
{
|
2017-08-23 18:29:48 -04:00
|
|
|
|
return new TimeSpan(1, 0, 0);
|
2018-11-25 21:00:36 -05:00
|
|
|
|
}
|
2017-08-23 18:29:48 -04:00
|
|
|
|
|
2018-10-02 13:39:08 -04:00
|
|
|
|
char lengthDenote = expressionMatch.Groups[2].ToString()[0];
|
|
|
|
|
int length = Int32.Parse(expressionMatch.Groups[1].ToString());
|
2017-08-23 18:29:48 -04:00
|
|
|
|
|
2018-08-02 21:52:35 -04:00
|
|
|
|
var loc = CurrentLocalization.LocalizationIndex;
|
|
|
|
|
|
|
|
|
|
if (lengthDenote == char.ToLower(loc["GLOBAL_TIME_MINUTES"][0]))
|
2017-08-23 18:29:48 -04:00
|
|
|
|
{
|
2018-08-02 21:52:35 -04:00
|
|
|
|
return new TimeSpan(0, length, 0);
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-02 13:39:08 -04:00
|
|
|
|
if (lengthDenote == char.ToLower(loc["GLOBAL_TIME_HOURS"][0]))
|
2018-08-02 21:52:35 -04:00
|
|
|
|
{
|
|
|
|
|
return new TimeSpan(length, 0, 0);
|
2017-08-23 18:29:48 -04:00
|
|
|
|
}
|
2018-08-02 21:52:35 -04:00
|
|
|
|
|
2018-10-02 13:39:08 -04:00
|
|
|
|
if (lengthDenote == char.ToLower(loc["GLOBAL_TIME_DAYS"][0]))
|
2018-08-02 21:52:35 -04:00
|
|
|
|
{
|
|
|
|
|
return new TimeSpan(length, 0, 0, 0);
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-02 13:39:08 -04:00
|
|
|
|
if (lengthDenote == char.ToLower(loc["GLOBAL_TIME_WEEKS"][0]))
|
2018-08-02 21:52:35 -04:00
|
|
|
|
{
|
|
|
|
|
return new TimeSpan(length * 7, 0, 0, 0);
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-02 13:39:08 -04:00
|
|
|
|
if (lengthDenote == char.ToLower(loc["GLOBAL_TIME_YEARS"][0]))
|
2018-08-02 21:52:35 -04:00
|
|
|
|
{
|
|
|
|
|
return new TimeSpan(length * 365, 0, 0, 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return new TimeSpan(1, 0, 0);
|
2017-08-23 18:29:48 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static string TimeSpanText(this TimeSpan span)
|
|
|
|
|
{
|
2018-08-02 21:52:35 -04:00
|
|
|
|
var loc = CurrentLocalization.LocalizationIndex;
|
|
|
|
|
|
2017-09-27 16:07:43 -04:00
|
|
|
|
if (span.TotalMinutes < 60)
|
2018-11-25 21:00:36 -05:00
|
|
|
|
{
|
2018-08-02 21:52:35 -04:00
|
|
|
|
return $"{span.Minutes} {loc["GLOBAL_TIME_MINUTES"]}";
|
2018-11-25 21:00:36 -05:00
|
|
|
|
}
|
2017-09-27 16:07:43 -04:00
|
|
|
|
else if (span.Hours >= 1 && span.TotalHours < 24)
|
2018-11-25 21:00:36 -05:00
|
|
|
|
{
|
2018-08-02 21:52:35 -04:00
|
|
|
|
return $"{span.Hours} {loc["GLOBAL_TIME_HOURS"]}";
|
2018-11-25 21:00:36 -05:00
|
|
|
|
}
|
2017-09-27 16:07:43 -04:00
|
|
|
|
else if (span.TotalDays >= 1 && span.TotalDays < 7)
|
2018-11-25 21:00:36 -05:00
|
|
|
|
{
|
2018-08-02 21:52:35 -04:00
|
|
|
|
return $"{span.Days} {loc["GLOBAL_TIME_DAYS"]}";
|
2018-11-25 21:00:36 -05:00
|
|
|
|
}
|
2018-08-02 21:52:35 -04:00
|
|
|
|
else if (span.TotalDays >= 7 && span.TotalDays < 90)
|
2018-11-25 21:00:36 -05:00
|
|
|
|
{
|
2018-08-02 21:52:35 -04:00
|
|
|
|
return $"{Math.Round(span.Days / 7.0, 0)} {loc["GLOBAL_TIME_WEEKS"]}";
|
2018-11-25 21:00:36 -05:00
|
|
|
|
}
|
2018-08-02 21:52:35 -04:00
|
|
|
|
else if (span.TotalDays >= 90 && span.TotalDays < 365)
|
2018-11-25 21:00:36 -05:00
|
|
|
|
{
|
2018-08-02 21:52:35 -04:00
|
|
|
|
return $"{Math.Round(span.Days / 30.0, 0)} {loc["GLOBAL_TIME_MONTHS"]}";
|
2018-11-25 21:00:36 -05:00
|
|
|
|
}
|
2017-09-27 16:07:43 -04:00
|
|
|
|
else if (span.TotalDays >= 365 && span.TotalDays < 36500)
|
2018-11-25 21:00:36 -05:00
|
|
|
|
{
|
2018-08-02 21:52:35 -04:00
|
|
|
|
return $"{Math.Round(span.Days / 365.0, 0)} {loc["GLOBAL_TIME_YEARS"]}";
|
2018-11-25 21:00:36 -05:00
|
|
|
|
}
|
2017-09-27 16:07:43 -04:00
|
|
|
|
else if (span.TotalDays >= 36500)
|
2018-11-25 21:00:36 -05:00
|
|
|
|
{
|
2018-08-02 21:52:35 -04:00
|
|
|
|
return loc["GLOBAL_TIME_FOREVER"];
|
2018-11-25 21:00:36 -05:00
|
|
|
|
}
|
2017-08-23 18:29:48 -04:00
|
|
|
|
|
2018-08-02 21:52:35 -04:00
|
|
|
|
return "unknown";
|
2017-08-23 18:29:48 -04:00
|
|
|
|
}
|
2017-11-16 18:09:19 -05:00
|
|
|
|
|
2019-06-28 17:57:01 -04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// returns a list of penalty types that should be shown across all profiles
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public static PenaltyType[] LinkedPenaltyTypes()
|
2019-04-05 14:34:03 -04:00
|
|
|
|
{
|
2019-07-17 13:29:51 -04:00
|
|
|
|
return new[]
|
2019-06-28 17:57:01 -04:00
|
|
|
|
{
|
|
|
|
|
PenaltyType.Ban,
|
|
|
|
|
PenaltyType.Unban,
|
|
|
|
|
PenaltyType.TempBan,
|
|
|
|
|
PenaltyType.Flag,
|
|
|
|
|
PenaltyType.Unflag,
|
|
|
|
|
};
|
2019-04-05 14:34:03 -04:00
|
|
|
|
}
|
|
|
|
|
|
2019-04-02 21:20:37 -04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Helper extension that determines if a user is a privileged client
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="p"></param>
|
|
|
|
|
/// <returns></returns>
|
2018-11-25 21:00:36 -05:00
|
|
|
|
public static bool IsPrivileged(this EFClient p)
|
|
|
|
|
{
|
2019-04-02 21:20:37 -04:00
|
|
|
|
return p.Level > EFClient.Permission.Flagged;
|
2017-11-25 20:29:58 -05:00
|
|
|
|
}
|
2018-03-06 02:22:19 -05:00
|
|
|
|
|
2018-12-30 19:13:13 -05:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// prompt user to answer a yes/no question
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="question">question to prompt the user with</param>
|
|
|
|
|
/// <param name="description">description of the question's value</param>
|
|
|
|
|
/// <param name="defaultValue">default value to set if no input is entered</param>
|
|
|
|
|
/// <returns></returns>
|
2019-01-03 15:39:22 -05:00
|
|
|
|
public static bool PromptBool(string question, string description = null, bool defaultValue = true)
|
2018-03-29 00:40:57 -04:00
|
|
|
|
{
|
2019-01-28 19:21:56 -05:00
|
|
|
|
Console.Write($"{question}?{(string.IsNullOrEmpty(description) ? " " : $" ({description}) ")}[y/n]: ");
|
2019-01-03 15:39:22 -05:00
|
|
|
|
char response = Console.ReadLine().ToLower().FirstOrDefault();
|
|
|
|
|
return response != 0 ? response == 'y' : defaultValue;
|
2018-03-29 00:40:57 -04:00
|
|
|
|
}
|
2018-03-30 00:13:40 -04:00
|
|
|
|
|
2019-02-04 20:38:24 -05:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// prompt user to make a selection
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <typeparam name="T">type of selection</typeparam>
|
|
|
|
|
/// <param name="question">question to prompt the user with</param>
|
|
|
|
|
/// <param name="defaultValue">default value to set if no input is entered</param>
|
|
|
|
|
/// <param name="description">description of the question's value</param>
|
|
|
|
|
/// <param name="selections">array of possible selections (should be able to convert to string)</param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public static Tuple<int, T> PromptSelection<T>(string question, T defaultValue, string description = null, params T[] selections)
|
|
|
|
|
{
|
|
|
|
|
bool hasDefault = false;
|
|
|
|
|
|
|
|
|
|
if (defaultValue != null)
|
|
|
|
|
{
|
|
|
|
|
hasDefault = true;
|
|
|
|
|
selections = (new T[] { defaultValue }).Union(selections).ToArray();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Console.WriteLine($"{question}{(string.IsNullOrEmpty(description) ? "" : $" [{ description}:]")}");
|
|
|
|
|
Console.WriteLine(new string('=', 52));
|
|
|
|
|
for (int index = 0; index < selections.Length; index++)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine($"{(hasDefault ? index : index + 1)}] {selections[index]}");
|
|
|
|
|
}
|
|
|
|
|
Console.WriteLine(new string('=', 52));
|
|
|
|
|
|
|
|
|
|
int selectionIndex = PromptInt(CurrentLocalization.LocalizationIndex["SETUP_PROMPT_MAKE_SELECTION"], null, hasDefault ? 0 : 1, selections.Length, hasDefault ? 0 : (int?)null);
|
|
|
|
|
|
|
|
|
|
if (!hasDefault)
|
|
|
|
|
{
|
|
|
|
|
selectionIndex--;
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-10 21:05:50 -05:00
|
|
|
|
T selection = selections[selectionIndex];
|
2019-02-04 20:38:24 -05:00
|
|
|
|
|
|
|
|
|
return Tuple.Create(selectionIndex, selection);
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-07 14:43:09 -04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// prompt user to enter a number
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="question">question to prompt with</param>
|
|
|
|
|
/// <param name="maxValue">maximum value to allow</param>
|
|
|
|
|
/// <param name="minValue">minimum value to allow</param>
|
2018-12-30 19:13:13 -05:00
|
|
|
|
/// <param name="defaultValue">default value to set the return value to</param>
|
|
|
|
|
/// <param name="description">a description of the question's value</param>
|
2018-08-07 14:43:09 -04:00
|
|
|
|
/// <returns>integer from user's input</returns>
|
2018-12-30 19:13:13 -05:00
|
|
|
|
public static int PromptInt(this string question, string description = null, int minValue = 0, int maxValue = int.MaxValue, int? defaultValue = null)
|
2018-08-07 14:43:09 -04:00
|
|
|
|
{
|
2019-02-04 20:38:24 -05:00
|
|
|
|
Console.Write($"{question}{(string.IsNullOrEmpty(description) ? "" : $" ({description})")}{(defaultValue == null ? "" : $" [{CurrentLocalization.LocalizationIndex["SETUP_PROMPT_DEFAULT"]} {defaultValue.Value.ToString()}]")}: ");
|
2018-08-07 14:43:09 -04:00
|
|
|
|
int response;
|
|
|
|
|
|
2018-12-30 19:13:13 -05:00
|
|
|
|
string inputOrDefault()
|
|
|
|
|
{
|
|
|
|
|
string input = Console.ReadLine();
|
|
|
|
|
return string.IsNullOrEmpty(input) && defaultValue != null ? defaultValue.ToString() : input;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
while (!int.TryParse(inputOrDefault(), out response) ||
|
2018-08-07 14:43:09 -04:00
|
|
|
|
response < minValue ||
|
|
|
|
|
response > maxValue)
|
|
|
|
|
{
|
|
|
|
|
string range = "";
|
|
|
|
|
if (minValue != 0 || maxValue != int.MaxValue)
|
|
|
|
|
{
|
|
|
|
|
range = $" [{minValue}-{maxValue}]";
|
|
|
|
|
}
|
2018-12-30 19:13:13 -05:00
|
|
|
|
Console.Write($"{CurrentLocalization.LocalizationIndex["SETUP_PROMPT_INT"]}{range}: ");
|
2018-08-07 14:43:09 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return response;
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-30 19:13:13 -05:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// prompt use to enter a string response
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="question">question to prompt with</param>
|
|
|
|
|
/// <param name="description">description of the question's value</param>
|
|
|
|
|
/// <param name="defaultValue">default value to set the return value to</param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public static string PromptString(string question, string description = null, string defaultValue = null)
|
2018-03-30 00:13:40 -04:00
|
|
|
|
{
|
2018-12-30 19:13:13 -05:00
|
|
|
|
string inputOrDefault()
|
|
|
|
|
{
|
|
|
|
|
string input = Console.ReadLine();
|
|
|
|
|
return string.IsNullOrEmpty(input) && defaultValue != null ? defaultValue.ToString() : input;
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-30 00:13:40 -04:00
|
|
|
|
string response;
|
|
|
|
|
do
|
|
|
|
|
{
|
2018-12-30 19:13:13 -05:00
|
|
|
|
Console.Write($"{question}{(string.IsNullOrEmpty(description) ? "" : $" ({description})")}{(defaultValue == null ? "" : $" [{CurrentLocalization.LocalizationIndex["SETUP_PROMPT_DEFAULT"]} {defaultValue}]")}: ");
|
|
|
|
|
response = inputOrDefault();
|
2020-02-17 11:05:31 -05:00
|
|
|
|
} while (string.IsNullOrWhiteSpace(response) && response != defaultValue);
|
2018-03-30 00:13:40 -04:00
|
|
|
|
|
|
|
|
|
return response;
|
|
|
|
|
}
|
2018-04-02 01:25:06 -04:00
|
|
|
|
|
2018-04-24 18:01:27 -04:00
|
|
|
|
public static Dictionary<string, string> DictionaryFromKeyValue(this string eventLine)
|
|
|
|
|
{
|
|
|
|
|
string[] values = eventLine.Substring(1).Split('\\');
|
|
|
|
|
|
|
|
|
|
Dictionary<string, string> dict = null;
|
|
|
|
|
|
2020-04-18 11:46:55 -04:00
|
|
|
|
if (values.Length > 1)
|
2018-04-24 18:01:27 -04:00
|
|
|
|
{
|
|
|
|
|
dict = new Dictionary<string, string>();
|
2020-04-18 11:46:55 -04:00
|
|
|
|
for (int i = values.Length % 2 == 0 ? 0 : 1; i < values.Length; i += 2)
|
2018-11-25 21:00:36 -05:00
|
|
|
|
{
|
2018-04-24 18:01:27 -04:00
|
|
|
|
dict.Add(values[i], values[i + 1]);
|
2018-11-25 21:00:36 -05:00
|
|
|
|
}
|
2018-04-24 18:01:27 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return dict;
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-05 16:36:26 -04:00
|
|
|
|
/* https://loune.net/2017/06/running-shell-bash-commands-in-net-core/ */
|
|
|
|
|
public static string GetCommandLine(int pId)
|
|
|
|
|
{
|
|
|
|
|
var cmdProcess = new Process()
|
|
|
|
|
{
|
|
|
|
|
StartInfo = new ProcessStartInfo()
|
|
|
|
|
{
|
|
|
|
|
FileName = "cmd.exe",
|
|
|
|
|
Arguments = $"/c wmic process where processid={pId} get CommandLine",
|
|
|
|
|
RedirectStandardOutput = true,
|
|
|
|
|
UseShellExecute = false,
|
|
|
|
|
CreateNoWindow = true,
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
cmdProcess.Start();
|
|
|
|
|
cmdProcess.WaitForExit();
|
|
|
|
|
|
|
|
|
|
string[] cmdLine = cmdProcess.StandardOutput.ReadToEnd().Split("\r\n", StringSplitOptions.RemoveEmptyEntries);
|
2019-07-27 16:23:45 -04:00
|
|
|
|
cmdProcess.Dispose();
|
2018-05-05 16:36:26 -04:00
|
|
|
|
|
|
|
|
|
return cmdLine.Length > 1 ? cmdLine[1] : cmdLine[0];
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-09 16:35:13 -05:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// indicates if the given log path is a remote (http) uri
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="log"></param>
|
|
|
|
|
/// <returns></returns>
|
2019-02-10 21:05:50 -05:00
|
|
|
|
public static bool IsRemoteLog(this string log)
|
|
|
|
|
{
|
|
|
|
|
return (log ?? "").StartsWith("http");
|
|
|
|
|
}
|
2019-02-09 16:35:13 -05:00
|
|
|
|
|
2018-11-25 21:00:36 -05:00
|
|
|
|
public static string ToBase64UrlSafeString(this string src)
|
|
|
|
|
{
|
|
|
|
|
return Convert.ToBase64String(src.Select(c => Convert.ToByte(c)).ToArray()).Replace('+', '-').Replace('/', '_');
|
|
|
|
|
}
|
2018-05-05 16:36:26 -04:00
|
|
|
|
|
2020-04-17 16:05:16 -04:00
|
|
|
|
public static Task<Dvar<T>> GetDvarAsync<T>(this Server server, string dvarName, T fallbackValue = default)
|
2018-11-25 21:00:36 -05:00
|
|
|
|
{
|
2020-06-16 18:16:12 -04:00
|
|
|
|
return server.RconParser.GetDvarAsync(server.RemoteConnection, dvarName, fallbackValue);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static async Task<Dvar<T>> GetMappedDvarValueOrDefaultAsync<T>(this Server server, string dvarName, string infoResponseName = null, IDictionary<string, string> infoResponse = null)
|
|
|
|
|
{
|
|
|
|
|
// todo: unit test this
|
|
|
|
|
string mappedKey = server.RconParser.GetOverrideDvarName(dvarName);
|
|
|
|
|
var defaultValue = server.RconParser.GetDefaultDvarValue<T>(mappedKey);
|
|
|
|
|
|
|
|
|
|
string foundKey = infoResponse?.Keys.Where(_key => new[] { mappedKey, dvarName, infoResponseName ?? dvarName }.Contains(_key)).FirstOrDefault();
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(foundKey))
|
|
|
|
|
{
|
|
|
|
|
return new Dvar<T>
|
|
|
|
|
{
|
|
|
|
|
Value = (T)Convert.ChangeType(infoResponse[foundKey], typeof(T)),
|
|
|
|
|
Name = foundKey
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return await server.GetDvarAsync(mappedKey, defaultValue);
|
2018-11-25 21:00:36 -05:00
|
|
|
|
}
|
2018-04-02 01:25:06 -04:00
|
|
|
|
|
2018-11-25 21:00:36 -05:00
|
|
|
|
public static Task SetDvarAsync(this Server server, string dvarName, object dvarValue)
|
|
|
|
|
{
|
|
|
|
|
return server.RconParser.SetDvarAsync(server.RemoteConnection, dvarName, dvarValue);
|
|
|
|
|
}
|
2018-04-02 01:25:06 -04:00
|
|
|
|
|
2018-11-25 21:00:36 -05:00
|
|
|
|
public static async Task<string[]> ExecuteCommandAsync(this Server server, string commandName)
|
|
|
|
|
{
|
|
|
|
|
return await server.RconParser.ExecuteCommandAsync(server.RemoteConnection, commandName);
|
|
|
|
|
}
|
2018-08-02 21:52:35 -04:00
|
|
|
|
|
2020-08-05 10:30:02 -04:00
|
|
|
|
public static Task<(List<EFClient>, string, string)> GetStatusAsync(this Server server)
|
2018-11-25 21:00:36 -05:00
|
|
|
|
{
|
|
|
|
|
return server.RconParser.GetStatusAsync(server.RemoteConnection);
|
|
|
|
|
}
|
2018-04-06 20:15:17 -04:00
|
|
|
|
|
2019-10-23 11:40:24 -04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Retrieves the key value pairs for server information usually checked after map rotation
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="server"></param>
|
|
|
|
|
/// <param name="delay">How long to wait after the map has rotated to query</param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public static async Task<IDictionary<string, string>> GetInfoAsync(this Server server, TimeSpan? delay = null)
|
2018-04-24 18:01:27 -04:00
|
|
|
|
{
|
2019-10-23 11:40:24 -04:00
|
|
|
|
if (delay != null)
|
2018-09-11 15:28:37 -04:00
|
|
|
|
{
|
2019-10-23 11:40:24 -04:00
|
|
|
|
await Task.Delay(delay.Value);
|
2018-09-11 15:28:37 -04:00
|
|
|
|
}
|
2019-10-23 11:40:24 -04:00
|
|
|
|
|
|
|
|
|
var response = await server.RemoteConnection.SendQueryAsync(RCon.StaticHelpers.QueryType.GET_INFO);
|
2020-04-18 11:46:55 -04:00
|
|
|
|
string combinedResponse = response.Length > 1 ?
|
|
|
|
|
string.Join('\\', response.Where(r => r.Length > 0 && r[0] == '\\')) :
|
|
|
|
|
response[0];
|
2020-04-17 16:05:16 -04:00
|
|
|
|
return combinedResponse.DictionaryFromKeyValue();
|
2018-05-10 01:34:29 -04:00
|
|
|
|
}
|
2018-09-07 23:29:42 -04:00
|
|
|
|
|
2018-09-13 15:34:42 -04:00
|
|
|
|
public static double GetVersionAsDouble()
|
|
|
|
|
{
|
|
|
|
|
string version = Assembly.GetCallingAssembly().GetName().Version.ToString();
|
|
|
|
|
version = version.Replace(".", "");
|
|
|
|
|
return double.Parse(version) / 1000.0;
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-25 21:00:36 -05:00
|
|
|
|
public static string GetVersionAsString()
|
|
|
|
|
{
|
|
|
|
|
return Assembly.GetCallingAssembly().GetName().Version.ToString();
|
|
|
|
|
}
|
2018-09-07 23:29:42 -04:00
|
|
|
|
|
2019-04-06 22:48:49 -04:00
|
|
|
|
public static string FormatExt(this string input, params object[] values)
|
|
|
|
|
{
|
|
|
|
|
var matches = Regex.Matches(Regex.Unescape(input), @"{{\w+}}");
|
|
|
|
|
string output = input;
|
|
|
|
|
int index = 0;
|
|
|
|
|
foreach (Match match in matches)
|
|
|
|
|
{
|
|
|
|
|
output = output.Replace(match.Value.ToString(), $"{{{index.ToString()}}}");
|
|
|
|
|
index++;
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-09 16:02:49 -04:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
return string.Format(output, values);
|
|
|
|
|
}
|
|
|
|
|
catch { return input; }
|
2019-04-06 22:48:49 -04:00
|
|
|
|
}
|
|
|
|
|
|
2019-04-08 13:29:48 -04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// https://stackoverflow.com/questions/8113546/how-to-determine-whether-an-ip-address-in-private/39120248
|
|
|
|
|
/// An extension method to determine if an IP address is internal, as specified in RFC1918
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="toTest">The IP address that will be tested</param>
|
|
|
|
|
/// <returns>Returns true if the IP is internal, false if it is external</returns>
|
|
|
|
|
public static bool IsInternal(this IPAddress toTest)
|
|
|
|
|
{
|
|
|
|
|
if (toTest.ToString().StartsWith("127.0.0"))
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
byte[] bytes = toTest.GetAddressBytes();
|
|
|
|
|
switch (bytes[0])
|
|
|
|
|
{
|
|
|
|
|
case 10:
|
|
|
|
|
return true;
|
|
|
|
|
case 172:
|
|
|
|
|
return bytes[1] < 32 && bytes[1] >= 16;
|
|
|
|
|
case 192:
|
|
|
|
|
return bytes[1] == 168;
|
|
|
|
|
default:
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// retrieves the external IP address of the current running machine
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public static async Task<string> GetExternalIP()
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
using (var wc = new WebClient())
|
|
|
|
|
{
|
|
|
|
|
return await wc.DownloadStringTaskAsync("https://api.ipify.org");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-08 21:34:17 -04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Determines if the given message is a quick message
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="message"></param>
|
|
|
|
|
/// <returns>true if the </returns>
|
2019-04-23 18:27:20 -04:00
|
|
|
|
public static bool IsQuickMessage(this string message)
|
|
|
|
|
{
|
|
|
|
|
return Regex.IsMatch(message, @"^\u0014(?:[A-Z]|_)+$");
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-12 16:11:43 -05:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// trims new line and whitespace from string
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="str">source string</param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public static string TrimNewLine(this string str) => str.Trim().TrimEnd('\r', '\n');
|
|
|
|
|
|
2019-06-30 14:37:59 -04:00
|
|
|
|
public static Vector3 FixIW4Angles(this Vector3 vector)
|
|
|
|
|
{
|
|
|
|
|
float X = vector.X >= 0 ? vector.X : 360.0f + vector.X;
|
|
|
|
|
float Y = vector.Y >= 0 ? vector.Y : 360.0f + vector.Y;
|
|
|
|
|
float Z = vector.Z >= 0 ? vector.Z : 360.0f + vector.Z;
|
|
|
|
|
|
|
|
|
|
return new Vector3(Y, X, Z);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static float ToRadians(this float value) => (float)Math.PI * value / 180.0f;
|
|
|
|
|
|
|
|
|
|
public static float ToDegrees(this float value) => value * 180.0f / (float)Math.PI;
|
|
|
|
|
|
|
|
|
|
public static double[] AngleStuff(Vector3 a, Vector3 b)
|
|
|
|
|
{
|
|
|
|
|
double deltaX = 180.0 - Math.Abs(Math.Abs(a.X - b.X) - 180.0);
|
|
|
|
|
double deltaY = 180.0 - Math.Abs(Math.Abs(a.Y - b.Y) - 180.0);
|
|
|
|
|
|
|
|
|
|
return new[] { deltaX, deltaY };
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-21 18:34:00 -04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// attempts to create and persist a penalty
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="penalty"></param>
|
|
|
|
|
/// <param name="penaltyService"></param>
|
|
|
|
|
/// <param name="logger"></param>
|
|
|
|
|
/// <returns>true of the creat succeeds, false otherwise</returns>
|
|
|
|
|
public static async Task<bool> TryCreatePenalty(this EFPenalty penalty, IEntityService<EFPenalty> penaltyService, ILogger logger)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
await penaltyService.Create(penalty);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
logger.WriteWarning($"Could not create penalty of type {penalty.Type.ToString()}");
|
|
|
|
|
logger.WriteDebug(e.GetExceptionInfo());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-04 17:50:02 -04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// https://www.planetgeek.ch/2016/12/08/async-method-without-cancellation-support-do-it-my-way/
|
|
|
|
|
/// </summary>
|
|
|
|
|
public static async Task WithWaitCancellation(this Task task,
|
|
|
|
|
CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
Task completedTask = await Task.WhenAny(task, Task.Delay(Timeout.Infinite, cancellationToken));
|
|
|
|
|
if (completedTask == task)
|
|
|
|
|
{
|
|
|
|
|
await task;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
cancellationToken.ThrowIfCancellationRequested();
|
|
|
|
|
throw new InvalidOperationException("Infinite delay task completed.");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-24 11:36:37 -04:00
|
|
|
|
public static bool ShouldHideLevel(this Permission perm) => perm == Permission.Flagged;
|
2020-06-30 17:39:32 -04:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// indicates if running in development mode
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public static bool IsDevelopment => Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") == "Development";
|
2020-06-16 18:16:12 -04:00
|
|
|
|
|
|
|
|
|
|
2020-04-13 17:16:31 -04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// replaces any directory separator chars with the platform specific character
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="path">original file path</param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public static string FixDirectoryCharacters(this string path)
|
|
|
|
|
{
|
|
|
|
|
foreach (char separator in DirectorySeparatorChars)
|
|
|
|
|
{
|
2020-04-17 16:05:16 -04:00
|
|
|
|
path = (path ?? "").Replace(separator, Path.DirectorySeparatorChar);
|
2020-04-13 17:16:31 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return path;
|
|
|
|
|
}
|
2015-08-20 01:06:44 -04:00
|
|
|
|
}
|
|
|
|
|
}
|