2022-01-26 11:32:16 -05:00
|
|
|
|
using System;
|
2018-11-25 21:00:36 -05:00
|
|
|
|
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;
|
2022-01-26 11:32:16 -05:00
|
|
|
|
using System.Net.Http;
|
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;
|
2022-01-26 11:32:16 -05:00
|
|
|
|
using Data.Models;
|
|
|
|
|
using Humanizer;
|
|
|
|
|
using Humanizer.Localisation;
|
2020-11-11 18:31:26 -05:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
2020-11-18 19:48:24 -05:00
|
|
|
|
using SharedLibraryCore.Configuration;
|
2022-01-26 11:32:16 -05:00
|
|
|
|
using SharedLibraryCore.Database.Models;
|
|
|
|
|
using SharedLibraryCore.Dtos.Meta;
|
|
|
|
|
using SharedLibraryCore.Formatting;
|
|
|
|
|
using SharedLibraryCore.Helpers;
|
|
|
|
|
using SharedLibraryCore.Interfaces;
|
|
|
|
|
using SharedLibraryCore.Localization;
|
|
|
|
|
using SharedLibraryCore.RCon;
|
2018-11-25 21:00:36 -05:00
|
|
|
|
using static SharedLibraryCore.Server;
|
2021-03-22 12:09:25 -04:00
|
|
|
|
using static Data.Models.Client.EFClient;
|
|
|
|
|
using static Data.Models.EFPenalty;
|
2022-01-26 11:32:16 -05:00
|
|
|
|
using ILogger = Microsoft.Extensions.Logging.ILogger;
|
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
|
|
|
|
{
|
2020-11-11 18:31:26 -05:00
|
|
|
|
// note: this is only to be used by classes not created by dependency injection
|
|
|
|
|
public static ILogger DefaultLogger { get; set; }
|
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
|
2022-01-26 11:32:16 -05:00
|
|
|
|
public static string OperatingDirectory =>
|
|
|
|
|
$"{Path.GetDirectoryName(Assembly.GetEntryAssembly().Location)}{Path.DirectorySeparatorChar}..{Path.DirectorySeparatorChar}";
|
2018-10-05 23:10:39 -04:00
|
|
|
|
#endif
|
2018-04-21 18:18:20 -04:00
|
|
|
|
public static Encoding EncodingType;
|
2022-01-26 11:32:16 -05:00
|
|
|
|
public static Layout CurrentLocalization = new Layout(new Dictionary<string, string>());
|
2022-03-23 12:38:09 -04:00
|
|
|
|
|
|
|
|
|
public static TimeSpan DefaultCommandTimeout { get; set; } = new(0, 0, Utilities.IsDevelopment ? 360 : 25);
|
2022-01-26 11:32:16 -05:00
|
|
|
|
public static char[] DirectorySeparatorChars = { '\\', '/' };
|
2020-04-26 22:12:49 -04:00
|
|
|
|
public static char CommandPrefix { get; set; } = '!';
|
2022-01-26 11:32:16 -05:00
|
|
|
|
|
2018-11-25 21:00:36 -05:00
|
|
|
|
public static EFClient IW4MAdminClient(Server server = null)
|
2018-10-08 22:15:59 -04:00
|
|
|
|
{
|
2022-01-26 11:32:16 -05:00
|
|
|
|
return new EFClient
|
2018-11-25 21:00:36 -05:00
|
|
|
|
{
|
|
|
|
|
ClientId = 1,
|
|
|
|
|
State = EFClient.ClientState.Connected,
|
2022-01-26 11:32:16 -05:00
|
|
|
|
Level = Permission.Console,
|
2018-11-25 21:00:36 -05:00
|
|
|
|
CurrentServer = server,
|
2022-01-26 11:32:16 -05:00
|
|
|
|
CurrentAlias = new EFAlias
|
2018-11-25 21:00:36 -05:00
|
|
|
|
{
|
|
|
|
|
Name = "IW4MAdmin"
|
2018-12-03 20:21:13 -05:00
|
|
|
|
},
|
|
|
|
|
AdministeredPenalties = new List<EFPenalty>()
|
2018-11-25 21:00:36 -05:00
|
|
|
|
};
|
|
|
|
|
}
|
2022-01-26 11:32:16 -05:00
|
|
|
|
|
2020-05-04 17:50:02 -04:00
|
|
|
|
/// <summary>
|
2022-01-26 11:32:16 -05:00
|
|
|
|
/// fallback id for world events
|
2020-05-04 17:50:02 -04:00
|
|
|
|
/// </summary>
|
|
|
|
|
public const long WORLD_ID = -1;
|
2022-01-26 11:32:16 -05:00
|
|
|
|
|
|
|
|
|
public static Dictionary<Permission, string> PermissionLevelOverrides { get; } =
|
|
|
|
|
new Dictionary<Permission, string>();
|
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)
|
|
|
|
|
{
|
2022-01-26 11:32:16 -05:00
|
|
|
|
using (var RequestClient = new HttpClient())
|
2018-08-26 20:20:47 -04:00
|
|
|
|
{
|
|
|
|
|
RequestClient.DefaultRequestHeaders.Add(header, headerValue);
|
2022-01-26 11:32:16 -05:00
|
|
|
|
var response = RequestClient.GetStringAsync(location).Result;
|
2018-08-26 20:20:47 -04:00
|
|
|
|
return response;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-08-20 01:06:44 -04:00
|
|
|
|
//Get string with specified number of spaces -- really only for visual output
|
2022-01-26 11:32:16 -05:00
|
|
|
|
public static string GetSpaces(int Num)
|
2015-08-20 01:06:44 -04:00
|
|
|
|
{
|
2022-01-26 11:32:16 -05:00
|
|
|
|
var SpaceString = string.Empty;
|
2015-08-20 01:06:44 -04:00
|
|
|
|
while (Num > 0)
|
|
|
|
|
{
|
|
|
|
|
SpaceString += ' ';
|
|
|
|
|
Num--;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return SpaceString;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//Remove words from a space delimited string
|
2022-01-26 11:32:16 -05: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
|
|
|
|
|
2022-01-26 11:32:16 -05:00
|
|
|
|
var newStr = string.Empty;
|
|
|
|
|
var tmp = str.Split(' ');
|
2015-08-20 01:06:44 -04:00
|
|
|
|
|
2022-01-26 11:32:16 -05:00
|
|
|
|
for (var i = 0; i < tmp.Length; i++)
|
2015-08-20 01:06:44 -04:00
|
|
|
|
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>
|
2022-01-26 11:32:16 -05:00
|
|
|
|
/// caps client name to the specified character length - 3
|
|
|
|
|
/// and adds ellipses to the end of the reamining client name
|
2020-04-04 13:40:23 -04:00
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="str">client name</param>
|
|
|
|
|
/// <param name="maxLength">max number of characters for the name</param>
|
|
|
|
|
/// <returns></returns>
|
2022-01-26 11:32:16 -05:00
|
|
|
|
public static string CapClientName(this string str, int maxLength)
|
|
|
|
|
{
|
|
|
|
|
return str.Length > maxLength ? $"{str.Substring(0, maxLength - 3)}..." : str;
|
|
|
|
|
}
|
2020-04-04 13:40:23 -04:00
|
|
|
|
|
2022-01-26 11:32:16 -05:00
|
|
|
|
public static Permission MatchPermission(string str)
|
2015-08-20 01:06:44 -04:00
|
|
|
|
{
|
2022-01-26 11:32:16 -05:00
|
|
|
|
var lookingFor = str.ToLower();
|
2017-06-12 17:47:31 -04:00
|
|
|
|
|
2022-01-26 11:32:16 -05:00
|
|
|
|
for (var Perm = Permission.User; Perm < Permission.Console; Perm++)
|
2018-08-03 22:11:58 -04:00
|
|
|
|
if (lookingFor.Contains(Perm.ToString().ToLower())
|
2022-01-26 11:32:16 -05:00
|
|
|
|
|| 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
|
|
|
|
|
2022-01-26 11:32:16 -05:00
|
|
|
|
return Permission.Banned;
|
2015-08-20 01:06:44 -04:00
|
|
|
|
}
|
|
|
|
|
|
2015-08-23 17:58:48 -04:00
|
|
|
|
/// <summary>
|
2022-01-26 11:32:16 -05:00
|
|
|
|
/// Remove all IW Engine color codes
|
2015-08-23 17:58:48 -04:00
|
|
|
|
/// </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})+", "");
|
2022-04-08 18:14:04 -04:00
|
|
|
|
str = Regex.Replace(str, @"\(Color::(.{1,16})\)", "");
|
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>
|
2022-01-26 11:32:16 -05:00
|
|
|
|
/// returns a "fixed" string that prevents message truncation in IW4 (and probably other Q3 clients)
|
2019-08-02 19:04:34 -04:00
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="str"></param>
|
|
|
|
|
/// <returns></returns>
|
2022-01-26 11:32:16 -05:00
|
|
|
|
public static string FixIW4ForwardSlash(this string str)
|
|
|
|
|
{
|
|
|
|
|
return str.Replace("//", "/ /");
|
|
|
|
|
}
|
2019-08-02 19:04:34 -04:00
|
|
|
|
|
2021-11-23 18:26:33 -05:00
|
|
|
|
public static string FormatMessageForEngine(this string str, ColorCodeMapping mapping)
|
|
|
|
|
{
|
|
|
|
|
if (mapping == null || string.IsNullOrEmpty(str))
|
|
|
|
|
{
|
|
|
|
|
return str;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var output = str;
|
2022-01-26 11:32:16 -05:00
|
|
|
|
var colorCodeMatches = Regex.Matches(output, @"\(Color::(.{1,16})\)",
|
|
|
|
|
RegexOptions.IgnoreCase | RegexOptions.Compiled);
|
2021-11-23 18:26:33 -05:00
|
|
|
|
foreach (var match in colorCodeMatches.Where(m => m.Success))
|
|
|
|
|
{
|
|
|
|
|
var key = match.Groups[1].ToString();
|
|
|
|
|
output = output.Replace(match.Value, mapping.TryGetValue(key, out var code) ? code : "");
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-28 18:22:15 -04:00
|
|
|
|
return output.FixIW4ForwardSlash();
|
2021-11-23 18:26:33 -05:00
|
|
|
|
}
|
|
|
|
|
|
2019-08-04 21:38:55 -04:00
|
|
|
|
private static readonly IList<string> _zmGameTypes = new[] { "zclassic", "zstandard", "zcleansed", "zgrief" };
|
2022-01-26 11:32:16 -05:00
|
|
|
|
|
2019-08-04 21:38:55 -04:00
|
|
|
|
/// <summary>
|
2022-01-26 11:32:16 -05:00
|
|
|
|
/// indicates if the given server is running a zombie game mode
|
2019-08-04 21:38:55 -04:00
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="server"></param>
|
|
|
|
|
/// <returns></returns>
|
2022-01-26 11:32:16 -05:00
|
|
|
|
public static bool IsZombieServer(this Server server)
|
|
|
|
|
{
|
|
|
|
|
return server.GameName == Game.T6 && _zmGameTypes.Contains(server.Gametype.ToLower());
|
|
|
|
|
}
|
2019-08-04 21:38:55 -04:00
|
|
|
|
|
2022-01-26 11:32:16 -05:00
|
|
|
|
public static bool IsCodGame(this Server server)
|
|
|
|
|
{
|
|
|
|
|
return server.RconParser?.RConEngine == "COD";
|
|
|
|
|
}
|
2021-09-18 19:10:47 -04:00
|
|
|
|
|
2015-08-23 17:58:48 -04:00
|
|
|
|
/// <summary>
|
2022-01-26 11:32:16 -05:00
|
|
|
|
/// Get the color key corresponding to a given user level
|
2015-08-23 17:58:48 -04:00
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="level">Specified player level</param>
|
2021-11-23 18:26:33 -05:00
|
|
|
|
/// <param name="localizedLevel"></param>
|
2015-08-23 17:58:48 -04:00
|
|
|
|
/// <returns></returns>
|
2021-11-23 18:26:33 -05:00
|
|
|
|
public static string ConvertLevelToColor(Permission level, string localizedLevel)
|
2015-08-20 01:06:44 -04:00
|
|
|
|
{
|
2021-11-23 18:26:33 -05:00
|
|
|
|
// todo: make configurable
|
|
|
|
|
var colorCode = level switch
|
2015-08-20 01:06:44 -04:00
|
|
|
|
{
|
2021-11-23 18:26:33 -05:00
|
|
|
|
Permission.Banned => "Red",
|
|
|
|
|
Permission.Flagged => "Map",
|
|
|
|
|
Permission.Owner => "Accent",
|
|
|
|
|
Permission.User => "Yellow",
|
|
|
|
|
Permission.Trusted => "Green",
|
|
|
|
|
_ => "Pink"
|
|
|
|
|
};
|
2018-08-03 22:11:58 -04:00
|
|
|
|
|
2021-11-23 18:26:33 -05:00
|
|
|
|
return $"(Color::{colorCode}){localizedLevel ?? level.ToString()}";
|
2015-08-20 01:06:44 -04:00
|
|
|
|
}
|
|
|
|
|
|
2021-01-24 12:47:19 -05:00
|
|
|
|
public static string ToLocalizedLevelName(this Permission permission)
|
2018-11-25 21:00:36 -05:00
|
|
|
|
{
|
2022-01-26 11:32:16 -05:00
|
|
|
|
var localized =
|
|
|
|
|
CurrentLocalization.LocalizationIndex[$"GLOBAL_PERMISSION_{permission.ToString().ToUpper()}"];
|
2021-01-24 12:47:19 -05:00
|
|
|
|
return PermissionLevelOverrides.ContainsKey(permission) && PermissionLevelOverrides[permission] != localized
|
|
|
|
|
? PermissionLevelOverrides[permission]
|
|
|
|
|
: localized;
|
2018-11-25 21:00:36 -05:00
|
|
|
|
}
|
2018-08-03 22:11:58 -04:00
|
|
|
|
|
2022-01-26 11:32:16 -05:00
|
|
|
|
public static async Task<string> ProcessMessageToken(this Server server, IList<MessageToken> tokens, string str)
|
2015-08-20 01:06:44 -04:00
|
|
|
|
{
|
2022-01-26 11:32:16 -05:00
|
|
|
|
var RegexMatches = Regex.Matches(str, @"\{\{[A-Z]+\}\}", RegexOptions.IgnoreCase);
|
2017-05-31 01:31:56 -04:00
|
|
|
|
foreach (Match M in RegexMatches)
|
2015-08-20 01:06:44 -04:00
|
|
|
|
{
|
2022-01-26 11:32:16 -05:00
|
|
|
|
var Match = M.Value;
|
|
|
|
|
var 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>
|
2022-01-26 11:32:16 -05:00
|
|
|
|
/// Get the full gametype name
|
2015-08-23 17:58:48 -04:00
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="input">Shorthand gametype reported from server</param>
|
|
|
|
|
/// <returns></returns>
|
2022-01-26 11:32:16 -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>
|
2022-01-26 11:32:16 -05:00
|
|
|
|
/// converts a string to numerical guid
|
2020-05-04 17:50:02 -04:00
|
|
|
|
/// </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
|
|
|
|
{
|
2021-06-03 11:51:03 -04:00
|
|
|
|
// added for source games that provide the steam ID
|
2021-07-01 10:10:56 -04:00
|
|
|
|
var match = Regex.Match(str, @"^STEAM_(\d):(\d):(\d+)$");
|
|
|
|
|
if (match.Success)
|
|
|
|
|
{
|
|
|
|
|
var x = int.Parse(match.Groups[1].ToString());
|
|
|
|
|
var y = int.Parse(match.Groups[2].ToString());
|
|
|
|
|
var z = long.Parse(match.Groups[3].ToString());
|
|
|
|
|
|
2021-07-01 11:14:58 -04:00
|
|
|
|
return z * 2 + 0x0110000100000000 + y;
|
2021-07-01 10:10:56 -04:00
|
|
|
|
}
|
2022-01-26 11:32:16 -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
|
|
|
|
{
|
2022-01-26 11:32:16 -05: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>
|
2022-01-26 11:32:16 -05:00
|
|
|
|
/// determines if the guid provided appears to be a bot guid
|
|
|
|
|
/// "1277538174" - (Pluto?)WaW (T4)
|
2020-05-04 17:50:02 -04:00
|
|
|
|
/// </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)
|
|
|
|
|
{
|
2021-04-16 14:48:52 -04:00
|
|
|
|
return guid.Contains("bot") || guid == "0" || guid == "1277538174";
|
2020-05-04 17:50:02 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2022-01-26 11:32:16 -05:00
|
|
|
|
/// generates a numerical hashcode from a string value
|
2020-05-04 17:50:02 -04:00
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="value">value string</param>
|
|
|
|
|
/// <returns></returns>
|
2022-01-26 11:32:16 -05:00
|
|
|
|
public static long GenerateGuidFromString(this string value)
|
|
|
|
|
{
|
|
|
|
|
return string.IsNullOrEmpty(value) ? -1 : GetStableHashCode(value.StripColors());
|
|
|
|
|
}
|
2020-09-21 16:30:42 -04:00
|
|
|
|
|
|
|
|
|
/// https://stackoverflow.com/questions/36845430/persistent-hashcode-for-strings
|
|
|
|
|
public static int GetStableHashCode(this string str)
|
|
|
|
|
{
|
|
|
|
|
unchecked
|
|
|
|
|
{
|
2022-01-26 11:32:16 -05:00
|
|
|
|
var hash1 = 5381;
|
|
|
|
|
var hash2 = hash1;
|
2020-09-21 16:30:42 -04:00
|
|
|
|
|
2022-01-26 11:32:16 -05:00
|
|
|
|
for (var i = 0; i < str.Length && str[i] != '\0'; i += 2)
|
2020-09-21 16:30:42 -04:00
|
|
|
|
{
|
|
|
|
|
hash1 = ((hash1 << 5) + hash1) ^ str[i];
|
|
|
|
|
if (i == str.Length - 1 || str[i + 1] == '\0')
|
2022-01-26 11:32:16 -05:00
|
|
|
|
{
|
2020-09-21 16:30:42 -04:00
|
|
|
|
break;
|
2022-01-26 11:32:16 -05:00
|
|
|
|
}
|
|
|
|
|
|
2020-09-21 16:30:42 -04:00
|
|
|
|
hash2 = ((hash2 << 5) + hash2) ^ str[i + 1];
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-26 11:32:16 -05:00
|
|
|
|
return hash1 + hash2 * 1566083941;
|
2020-09-21 16:30:42 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
2020-05-04 17:50:02 -04:00
|
|
|
|
|
2018-12-16 22:16:56 -05:00
|
|
|
|
public static int? ConvertToIP(this string str)
|
2018-02-10 23:33:42 -05:00
|
|
|
|
{
|
2022-01-26 11:32:16 -05:00
|
|
|
|
var success = IPAddress.TryParse(str, out var ip);
|
|
|
|
|
return success && ip.GetAddressBytes().Count(_byte => _byte == 0) != 4
|
|
|
|
|
? (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
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
|
2022-01-26 11:32:16 -05:00
|
|
|
|
var lengthDenote = expressionMatch.Groups[2].ToString()[0];
|
|
|
|
|
var length = int.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
|
|
|
|
}
|
|
|
|
|
|
2022-04-04 23:16:40 -04:00
|
|
|
|
public static bool HasPermission<TEntity, TPermission>(this IEnumerable<string> permissionsSet, TEntity entity,
|
|
|
|
|
TPermission permission) where TEntity : Enum where TPermission : Enum
|
|
|
|
|
{
|
2022-04-19 19:43:58 -04:00
|
|
|
|
if (permissionsSet == null)
|
2022-04-04 23:16:40 -04:00
|
|
|
|
{
|
2022-04-19 19:43:58 -04:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var requiredPermission = $"{entity.ToString()}.{permission.ToString()}";
|
|
|
|
|
var hasAllPermissions = permissionsSet.Any(p => p.Equals("*"));
|
|
|
|
|
var permissionCheckResult = permissionsSet.Select(p =>
|
|
|
|
|
{
|
|
|
|
|
if (p.Equals(requiredPermission, StringComparison.InvariantCultureIgnoreCase))
|
2022-04-04 23:16:40 -04:00
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-19 19:43:58 -04:00
|
|
|
|
if (p.Equals($"-{requiredPermission}", StringComparison.InvariantCultureIgnoreCase))
|
2022-04-04 23:16:40 -04:00
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-19 19:43:58 -04:00
|
|
|
|
return (bool?)null;
|
|
|
|
|
}).ToList();
|
2022-04-04 23:16:40 -04:00
|
|
|
|
|
2022-04-19 19:43:58 -04:00
|
|
|
|
var permissionNegated = permissionCheckResult.Any(result => result.HasValue && !result.Value);
|
|
|
|
|
|
|
|
|
|
if (permissionNegated)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2022-04-04 23:16:40 -04:00
|
|
|
|
|
2022-04-19 19:43:58 -04:00
|
|
|
|
return hasAllPermissions || permissionCheckResult.Any(result => result.HasValue && result.Value);
|
2022-04-04 23:16:40 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static bool HasPermission<TEntity, TPermission>(this ApplicationConfiguration appConfig,
|
|
|
|
|
Permission permissionLevel, TEntity entity,
|
|
|
|
|
TPermission permission) where TEntity : Enum where TPermission : Enum
|
|
|
|
|
{
|
|
|
|
|
return appConfig.PermissionSets.ContainsKey(permissionLevel.ToString()) &&
|
|
|
|
|
HasPermission(appConfig.PermissionSets[permissionLevel.ToString()], entity, permission);
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-28 17:57:01 -04:00
|
|
|
|
/// <summary>
|
2022-01-26 11:32:16 -05:00
|
|
|
|
/// returns a list of penalty types that should be shown across all profiles
|
2019-06-28 17:57:01 -04:00
|
|
|
|
/// </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,
|
2022-01-26 11:32:16 -05:00
|
|
|
|
PenaltyType.Unflag
|
2019-06-28 17:57:01 -04:00
|
|
|
|
};
|
2019-04-05 14:34:03 -04:00
|
|
|
|
}
|
|
|
|
|
|
2019-04-02 21:20:37 -04:00
|
|
|
|
/// <summary>
|
2022-01-26 11:32:16 -05:00
|
|
|
|
/// Helper extension that determines if a user is a privileged client
|
2019-04-02 21:20:37 -04:00
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="p"></param>
|
|
|
|
|
/// <returns></returns>
|
2018-11-25 21:00:36 -05:00
|
|
|
|
public static bool IsPrivileged(this EFClient p)
|
|
|
|
|
{
|
2022-01-26 11:32:16 -05:00
|
|
|
|
return p.Level > 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>
|
2022-01-26 11:32:16 -05:00
|
|
|
|
/// prompt user to answer a yes/no question
|
2018-12-30 19:13:13 -05:00
|
|
|
|
/// </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>
|
2021-11-14 22:38:00 -05:00
|
|
|
|
public static bool PromptBool(this 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]: ");
|
2022-01-26 11:32:16 -05:00
|
|
|
|
var response = Console.ReadLine().ToLower().FirstOrDefault();
|
2019-01-03 15:39:22 -05:00
|
|
|
|
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>
|
2022-01-26 11:32:16 -05:00
|
|
|
|
/// prompt user to make a selection
|
2019-02-04 20:38:24 -05:00
|
|
|
|
/// </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>
|
2022-01-26 11:32:16 -05:00
|
|
|
|
public static Tuple<int, T> PromptSelection<T>(this string question, T defaultValue, string description = null,
|
|
|
|
|
params T[] selections)
|
2019-02-04 20:38:24 -05:00
|
|
|
|
{
|
2022-01-26 11:32:16 -05:00
|
|
|
|
var hasDefault = false;
|
2019-02-04 20:38:24 -05:00
|
|
|
|
|
|
|
|
|
if (defaultValue != null)
|
|
|
|
|
{
|
|
|
|
|
hasDefault = true;
|
2022-01-26 11:32:16 -05:00
|
|
|
|
selections = new[] { defaultValue }.Union(selections).ToArray();
|
2019-02-04 20:38:24 -05:00
|
|
|
|
}
|
|
|
|
|
|
2022-01-26 11:32:16 -05:00
|
|
|
|
Console.WriteLine($"{question}{(string.IsNullOrEmpty(description) ? "" : $" [{description}:]")}");
|
2019-02-04 20:38:24 -05:00
|
|
|
|
Console.WriteLine(new string('=', 52));
|
2022-01-26 11:32:16 -05:00
|
|
|
|
for (var index = 0; index < selections.Length; index++)
|
2019-02-04 20:38:24 -05:00
|
|
|
|
Console.WriteLine($"{(hasDefault ? index : index + 1)}] {selections[index]}");
|
|
|
|
|
Console.WriteLine(new string('=', 52));
|
|
|
|
|
|
2022-01-26 11:32:16 -05:00
|
|
|
|
var selectionIndex = PromptInt(CurrentLocalization.LocalizationIndex["SETUP_PROMPT_MAKE_SELECTION"], null,
|
|
|
|
|
hasDefault ? 0 : 1, selections.Length, hasDefault ? 0 : (int?)null);
|
2019-02-04 20:38:24 -05:00
|
|
|
|
|
|
|
|
|
if (!hasDefault)
|
|
|
|
|
{
|
|
|
|
|
selectionIndex--;
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-26 11:32:16 -05:00
|
|
|
|
var 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>
|
2022-01-26 11:32:16 -05:00
|
|
|
|
/// prompt user to enter a number
|
2018-08-07 14:43:09 -04:00
|
|
|
|
/// </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>
|
2022-01-26 11:32:16 -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
|
|
|
|
{
|
2022-01-26 11:32:16 -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()
|
|
|
|
|
{
|
2022-01-26 11:32:16 -05:00
|
|
|
|
var input = Console.ReadLine();
|
2018-12-30 19:13:13 -05:00
|
|
|
|
return string.IsNullOrEmpty(input) && defaultValue != null ? defaultValue.ToString() : input;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
while (!int.TryParse(inputOrDefault(), out response) ||
|
2022-01-26 11:32:16 -05:00
|
|
|
|
response < minValue ||
|
|
|
|
|
response > maxValue)
|
2018-08-07 14:43:09 -04:00
|
|
|
|
{
|
2022-01-26 11:32:16 -05:00
|
|
|
|
var range = "";
|
2018-08-07 14:43:09 -04:00
|
|
|
|
if (minValue != 0 || maxValue != int.MaxValue)
|
|
|
|
|
{
|
|
|
|
|
range = $" [{minValue}-{maxValue}]";
|
|
|
|
|
}
|
2022-01-26 11:32:16 -05:00
|
|
|
|
|
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>
|
2022-01-26 11:32:16 -05:00
|
|
|
|
/// prompt use to enter a string response
|
2018-12-30 19:13:13 -05:00
|
|
|
|
/// </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>
|
2021-11-14 22:38:00 -05:00
|
|
|
|
public static string PromptString(this 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()
|
|
|
|
|
{
|
2022-01-26 11:32:16 -05:00
|
|
|
|
var input = Console.ReadLine();
|
|
|
|
|
return string.IsNullOrEmpty(input) && defaultValue != null ? defaultValue : input;
|
2018-12-30 19:13:13 -05:00
|
|
|
|
}
|
|
|
|
|
|
2018-03-30 00:13:40 -04:00
|
|
|
|
string response;
|
|
|
|
|
do
|
|
|
|
|
{
|
2022-01-26 11:32:16 -05:00
|
|
|
|
Console.Write(
|
|
|
|
|
$"{question}{(string.IsNullOrEmpty(description) ? "" : $" ({description})")}{(defaultValue == null ? "" : $" [{CurrentLocalization.LocalizationIndex["SETUP_PROMPT_DEFAULT"]} {defaultValue}]")}: ");
|
2018-12-30 19:13:13 -05:00
|
|
|
|
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)
|
|
|
|
|
{
|
2022-01-26 11:32:16 -05:00
|
|
|
|
var values = eventLine.Substring(1).Split('\\');
|
2018-04-24 18:01:27 -04:00
|
|
|
|
|
|
|
|
|
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>();
|
2022-01-26 11:32:16 -05:00
|
|
|
|
for (var i = values.Length % 2 == 0 ? 0 : 1; i < values.Length; i += 2)
|
2018-04-24 18:01:27 -04:00
|
|
|
|
dict.Add(values[i], values[i + 1]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
{
|
2022-01-26 11:32:16 -05:00
|
|
|
|
var cmdProcess = new Process
|
2018-05-05 16:36:26 -04:00
|
|
|
|
{
|
2022-01-26 11:32:16 -05:00
|
|
|
|
StartInfo = new ProcessStartInfo
|
2018-05-05 16:36:26 -04:00
|
|
|
|
{
|
|
|
|
|
FileName = "cmd.exe",
|
|
|
|
|
Arguments = $"/c wmic process where processid={pId} get CommandLine",
|
|
|
|
|
RedirectStandardOutput = true,
|
|
|
|
|
UseShellExecute = false,
|
2022-01-26 11:32:16 -05:00
|
|
|
|
CreateNoWindow = true
|
2018-05-05 16:36:26 -04:00
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
cmdProcess.Start();
|
|
|
|
|
cmdProcess.WaitForExit();
|
|
|
|
|
|
2022-01-26 11:32:16 -05:00
|
|
|
|
var 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>
|
2022-01-26 11:32:16 -05:00
|
|
|
|
/// indicates if the given log path is a remote (http) uri
|
2019-02-09 16:35:13 -05:00
|
|
|
|
/// </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)
|
|
|
|
|
{
|
2022-01-26 11:32:16 -05:00
|
|
|
|
return Convert.ToBase64String(src.Select(c => Convert.ToByte(c)).ToArray()).Replace('+', '-')
|
|
|
|
|
.Replace('/', '_');
|
2018-11-25 21:00:36 -05:00
|
|
|
|
}
|
2018-05-05 16:36:26 -04:00
|
|
|
|
|
2022-02-28 21:44:30 -05:00
|
|
|
|
public static async Task<Dvar<T>> GetDvarAsync<T>(this Server server, string dvarName,
|
|
|
|
|
T fallbackValue = default, CancellationToken token = default)
|
2018-11-25 21:00:36 -05:00
|
|
|
|
{
|
2022-02-28 21:44:30 -05:00
|
|
|
|
return await server.RconParser.GetDvarAsync(server.RemoteConnection, dvarName, fallbackValue, token);
|
2020-06-16 18:16:12 -04:00
|
|
|
|
}
|
2022-06-01 12:25:11 -04:00
|
|
|
|
|
|
|
|
|
public static void BeginGetDvar(this Server server, string dvarName, AsyncCallback callback, CancellationToken token = default)
|
|
|
|
|
{
|
|
|
|
|
server.RconParser.BeginGetDvar(server.RemoteConnection, dvarName, callback, token);
|
|
|
|
|
}
|
2022-03-02 09:29:15 -05:00
|
|
|
|
|
|
|
|
|
public static async Task<Dvar<T>> GetDvarAsync<T>(this Server server, string dvarName,
|
|
|
|
|
T fallbackValue = default)
|
|
|
|
|
{
|
|
|
|
|
return await GetDvarAsync(server, dvarName, fallbackValue, default);
|
|
|
|
|
}
|
2020-06-16 18:16:12 -04:00
|
|
|
|
|
2022-01-26 11:32:16 -05:00
|
|
|
|
public static async Task<Dvar<T>> GetMappedDvarValueOrDefaultAsync<T>(this Server server, string dvarName,
|
|
|
|
|
string infoResponseName = null, IDictionary<string, string> infoResponse = null,
|
2022-02-28 21:44:30 -05:00
|
|
|
|
T overrideDefault = default, CancellationToken token = default)
|
2020-06-16 18:16:12 -04:00
|
|
|
|
{
|
|
|
|
|
// todo: unit test this
|
2022-01-26 11:32:16 -05:00
|
|
|
|
var mappedKey = server.RconParser.GetOverrideDvarName(dvarName);
|
2020-08-20 11:38:11 -04:00
|
|
|
|
var defaultValue = server.RconParser.GetDefaultDvarValue<T>(mappedKey) ?? overrideDefault;
|
2020-06-16 18:16:12 -04:00
|
|
|
|
|
2022-01-26 11:32:16 -05:00
|
|
|
|
var foundKey = infoResponse?.Keys
|
|
|
|
|
.Where(_key => new[] { mappedKey, dvarName, infoResponseName ?? dvarName }.Contains(_key))
|
|
|
|
|
.FirstOrDefault();
|
2020-06-16 18:16:12 -04:00
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(foundKey))
|
|
|
|
|
{
|
|
|
|
|
return new Dvar<T>
|
|
|
|
|
{
|
|
|
|
|
Value = (T)Convert.ChangeType(infoResponse[foundKey], typeof(T)),
|
|
|
|
|
Name = foundKey
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-28 21:44:30 -05:00
|
|
|
|
return await server.GetDvarAsync(mappedKey, defaultValue, token: token);
|
2018-11-25 21:00:36 -05:00
|
|
|
|
}
|
2018-04-02 01:25:06 -04:00
|
|
|
|
|
2022-02-28 21:44:30 -05:00
|
|
|
|
public static async Task SetDvarAsync(this Server server, string dvarName, object dvarValue, CancellationToken token = default)
|
2018-11-25 21:00:36 -05:00
|
|
|
|
{
|
2022-02-28 21:44:30 -05:00
|
|
|
|
await server.RconParser.SetDvarAsync(server.RemoteConnection, dvarName, dvarValue, token);
|
2018-11-25 21:00:36 -05:00
|
|
|
|
}
|
2022-06-01 12:25:11 -04:00
|
|
|
|
|
|
|
|
|
public static void BeginSetDvar(this Server server, string dvarName, object dvarValue,
|
|
|
|
|
AsyncCallback callback, CancellationToken token = default)
|
|
|
|
|
{
|
|
|
|
|
server.RconParser.BeginSetDvar(server.RemoteConnection, dvarName, dvarValue, callback, token);
|
|
|
|
|
}
|
2022-03-02 09:29:15 -05:00
|
|
|
|
|
|
|
|
|
public static async Task SetDvarAsync(this Server server, string dvarName, object dvarValue)
|
|
|
|
|
{
|
|
|
|
|
await SetDvarAsync(server, dvarName, dvarValue, default);
|
|
|
|
|
}
|
2018-04-02 01:25:06 -04:00
|
|
|
|
|
2022-02-28 21:44:30 -05:00
|
|
|
|
public static async Task<string[]> ExecuteCommandAsync(this Server server, string commandName, CancellationToken token = default)
|
2018-11-25 21:00:36 -05:00
|
|
|
|
{
|
2022-02-28 21:44:30 -05:00
|
|
|
|
return await server.RconParser.ExecuteCommandAsync(server.RemoteConnection, commandName, token);
|
2018-11-25 21:00:36 -05:00
|
|
|
|
}
|
2022-03-02 09:29:15 -05:00
|
|
|
|
|
|
|
|
|
public static async Task<string[]> ExecuteCommandAsync(this Server server, string commandName)
|
|
|
|
|
{
|
|
|
|
|
return await ExecuteCommandAsync(server, commandName, default);
|
|
|
|
|
}
|
2018-08-02 21:52:35 -04:00
|
|
|
|
|
2022-06-01 12:25:11 -04:00
|
|
|
|
public static async Task<IStatusResponse> GetStatusAsync(this Server server, CancellationToken token)
|
2018-11-25 21:00:36 -05:00
|
|
|
|
{
|
2022-06-01 12:25:11 -04:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
return await server.RconParser.GetStatusAsync(server.RemoteConnection, token);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
catch (TaskCanceledException)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2018-11-25 21:00:36 -05:00
|
|
|
|
}
|
2018-04-06 20:15:17 -04:00
|
|
|
|
|
2019-10-23 11:40:24 -04:00
|
|
|
|
/// <summary>
|
2022-01-26 11:32:16 -05:00
|
|
|
|
/// Retrieves the key value pairs for server information usually checked after map rotation
|
2019-10-23 11:40:24 -04:00
|
|
|
|
/// </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
|
|
|
|
|
2022-01-26 11:32:16 -05:00
|
|
|
|
var response = await server.RemoteConnection.SendQueryAsync(StaticHelpers.QueryType.GET_INFO);
|
|
|
|
|
var 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()
|
|
|
|
|
{
|
2022-01-26 11:32:16 -05:00
|
|
|
|
var version = Assembly.GetCallingAssembly().GetName().Version.ToString();
|
2018-09-13 15:34:42 -04:00
|
|
|
|
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+}}");
|
2022-01-26 11:32:16 -05:00
|
|
|
|
var output = input;
|
|
|
|
|
var index = 0;
|
2019-04-06 22:48:49 -04:00
|
|
|
|
foreach (Match match in matches)
|
|
|
|
|
{
|
2022-01-26 11:32:16 -05:00
|
|
|
|
output = output.Replace(match.Value, $"{{{index.ToString()}}}");
|
2019-04-06 22:48:49 -04:00
|
|
|
|
index++;
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-09 16:02:49 -04:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
return string.Format(output, values);
|
|
|
|
|
}
|
2022-01-26 11:32:16 -05:00
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
return input;
|
|
|
|
|
}
|
2019-04-06 22:48:49 -04:00
|
|
|
|
}
|
|
|
|
|
|
2019-04-08 13:29:48 -04:00
|
|
|
|
/// <summary>
|
2022-01-26 11:32:16 -05:00
|
|
|
|
/// 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
|
2019-04-08 13:29:48 -04:00
|
|
|
|
/// </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;
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-26 11:32:16 -05:00
|
|
|
|
var bytes = toTest.GetAddressBytes();
|
2019-04-08 13:29:48 -04:00
|
|
|
|
switch (bytes[0])
|
|
|
|
|
{
|
2020-09-04 13:58:54 -04:00
|
|
|
|
case 0:
|
|
|
|
|
return bytes[1] == 0 && bytes[2] == 0 && bytes[3] == 0;
|
2019-04-08 13:29:48 -04:00
|
|
|
|
case 10:
|
|
|
|
|
return true;
|
|
|
|
|
case 172:
|
|
|
|
|
return bytes[1] < 32 && bytes[1] >= 16;
|
|
|
|
|
case 192:
|
|
|
|
|
return bytes[1] == 168;
|
|
|
|
|
default:
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2022-01-26 11:32:16 -05:00
|
|
|
|
/// retrieves the external IP address of the current running machine
|
2019-04-08 13:29:48 -04:00
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public static async Task<string> GetExternalIP()
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2022-01-26 11:32:16 -05:00
|
|
|
|
using var wc = new HttpClient();
|
|
|
|
|
return await wc.GetStringAsync("https://api.ipify.org");
|
2019-04-08 13:29:48 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-08 21:34:17 -04:00
|
|
|
|
/// <summary>
|
2022-01-26 11:32:16 -05:00
|
|
|
|
/// Determines if the given message is a quick message
|
2019-05-08 21:34:17 -04:00
|
|
|
|
/// </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)
|
|
|
|
|
{
|
2020-08-20 11:38:11 -04:00
|
|
|
|
return Regex.IsMatch(message, @"^\u0014(?:\w|_|!|\s)+$");
|
2019-04-23 18:27:20 -04:00
|
|
|
|
}
|
|
|
|
|
|
2020-02-12 16:11:43 -05:00
|
|
|
|
/// <summary>
|
2022-01-26 11:32:16 -05:00
|
|
|
|
/// trims new line and whitespace from string
|
2020-02-12 16:11:43 -05:00
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="str">source string</param>
|
|
|
|
|
/// <returns></returns>
|
2022-01-26 11:32:16 -05:00
|
|
|
|
public static string TrimNewLine(this string str)
|
|
|
|
|
{
|
|
|
|
|
return str.Trim().TrimEnd('\r', '\n');
|
|
|
|
|
}
|
2020-02-12 16:11:43 -05:00
|
|
|
|
|
2019-06-30 14:37:59 -04:00
|
|
|
|
public static Vector3 FixIW4Angles(this Vector3 vector)
|
|
|
|
|
{
|
2022-01-26 11:32:16 -05:00
|
|
|
|
var X = vector.X >= 0 ? vector.X : 360.0f + vector.X;
|
|
|
|
|
var Y = vector.Y >= 0 ? vector.Y : 360.0f + vector.Y;
|
|
|
|
|
var Z = vector.Z >= 0 ? vector.Z : 360.0f + vector.Z;
|
2019-06-30 14:37:59 -04:00
|
|
|
|
|
|
|
|
|
return new Vector3(Y, X, Z);
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-26 11:32:16 -05:00
|
|
|
|
public static float ToRadians(this float value)
|
|
|
|
|
{
|
|
|
|
|
return (float)Math.PI * value / 180.0f;
|
|
|
|
|
}
|
2019-06-30 14:37:59 -04:00
|
|
|
|
|
2022-01-26 11:32:16 -05:00
|
|
|
|
public static float ToDegrees(this float value)
|
|
|
|
|
{
|
|
|
|
|
return value * 180.0f / (float)Math.PI;
|
|
|
|
|
}
|
2019-06-30 14:37:59 -04:00
|
|
|
|
|
|
|
|
|
public static double[] AngleStuff(Vector3 a, Vector3 b)
|
|
|
|
|
{
|
2022-01-26 11:32:16 -05:00
|
|
|
|
var deltaX = 180.0 - Math.Abs(Math.Abs(a.X - b.X) - 180.0);
|
|
|
|
|
var deltaY = 180.0 - Math.Abs(Math.Abs(a.Y - b.Y) - 180.0);
|
2019-06-30 14:37:59 -04:00
|
|
|
|
|
|
|
|
|
return new[] { deltaX, deltaY };
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-21 18:34:00 -04:00
|
|
|
|
/// <summary>
|
2022-01-26 11:32:16 -05:00
|
|
|
|
/// attempts to create and persist a penalty
|
2020-04-21 18:34:00 -04:00
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="penalty"></param>
|
|
|
|
|
/// <param name="penaltyService"></param>
|
|
|
|
|
/// <param name="logger"></param>
|
2020-08-17 22:21:11 -04:00
|
|
|
|
/// <returns>true of the create succeeds, false otherwise</returns>
|
2022-01-26 11:32:16 -05:00
|
|
|
|
public static async Task<bool> TryCreatePenalty(this EFPenalty penalty,
|
|
|
|
|
IEntityService<EFPenalty> penaltyService, ILogger logger)
|
2020-04-21 18:34:00 -04:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
await penaltyService.Create(penalty);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
2020-11-11 18:31:26 -05:00
|
|
|
|
logger.LogError(e, $"Could not create penalty of type {penalty.Type.ToString()}");
|
2020-04-21 18:34:00 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-04 17:50:02 -04:00
|
|
|
|
/// <summary>
|
2022-01-26 11:32:16 -05:00
|
|
|
|
/// https://www.planetgeek.ch/2016/12/08/async-method-without-cancellation-support-do-it-my-way/
|
2020-05-04 17:50:02 -04:00
|
|
|
|
/// </summary>
|
|
|
|
|
public static async Task WithWaitCancellation(this Task task,
|
2022-01-26 11:32:16 -05:00
|
|
|
|
CancellationToken cancellationToken)
|
2020-05-04 17:50:02 -04:00
|
|
|
|
{
|
2022-01-26 11:32:16 -05:00
|
|
|
|
var completedTask = await Task.WhenAny(task, Task.Delay(Timeout.Infinite, cancellationToken));
|
2020-05-04 17:50:02 -04:00
|
|
|
|
if (completedTask == task)
|
|
|
|
|
{
|
|
|
|
|
await task;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
cancellationToken.ThrowIfCancellationRequested();
|
|
|
|
|
throw new InvalidOperationException("Infinite delay task completed.");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-13 22:38:40 -05:00
|
|
|
|
public static async Task<T> WithWaitCancellation<T>(this Task<T> task,
|
|
|
|
|
CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
var completedTask = await Task.WhenAny(task, Task.Delay(Timeout.Infinite, cancellationToken));
|
|
|
|
|
if (completedTask == task)
|
|
|
|
|
{
|
|
|
|
|
return await task;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cancellationToken.ThrowIfCancellationRequested();
|
|
|
|
|
throw new InvalidOperationException("Infinite delay task completed.");
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-16 09:53:50 -04:00
|
|
|
|
public static async Task<T> WithTimeout<T>(this Task<T> task, TimeSpan timeout)
|
2022-01-26 11:32:16 -05:00
|
|
|
|
{
|
|
|
|
|
await Task.WhenAny(task, Task.Delay(timeout));
|
|
|
|
|
return await task;
|
2021-06-16 09:53:50 -04:00
|
|
|
|
}
|
2022-01-26 11:32:16 -05:00
|
|
|
|
|
2021-06-16 09:53:50 -04:00
|
|
|
|
public static async Task WithTimeout(this Task task, TimeSpan timeout)
|
2022-01-26 11:32:16 -05:00
|
|
|
|
{
|
2021-06-16 09:53:50 -04:00
|
|
|
|
await Task.WhenAny(task, Task.Delay(timeout));
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-26 11:32:16 -05:00
|
|
|
|
|
|
|
|
|
public static bool ShouldHideLevel(this Permission perm)
|
|
|
|
|
{
|
|
|
|
|
return perm == Permission.Flagged;
|
|
|
|
|
}
|
2020-08-17 22:21:11 -04:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
2022-01-26 11:32:16 -05:00
|
|
|
|
/// parses translation string into tokens that are able to be formatted by the webfront
|
2020-08-17 22:21:11 -04:00
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="translationKey">key for translation lookup</param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public static WebfrontTranslationHelper[] SplitTranslationTokens(string translationKey)
|
|
|
|
|
{
|
2022-01-26 11:32:16 -05:00
|
|
|
|
var translationString = CurrentLocalization.LocalizationIndex[translationKey];
|
2020-08-17 22:21:11 -04:00
|
|
|
|
var builder = new StringBuilder();
|
|
|
|
|
var results = new List<WebfrontTranslationHelper>();
|
|
|
|
|
|
2022-01-26 11:32:16 -05:00
|
|
|
|
foreach (var word in translationString.Split(' '))
|
2020-08-17 22:21:11 -04:00
|
|
|
|
{
|
2022-01-26 11:32:16 -05:00
|
|
|
|
var finalWord = word;
|
2020-08-17 22:21:11 -04:00
|
|
|
|
|
2022-01-26 11:32:16 -05:00
|
|
|
|
if (word.StartsWith("{{") && !word.EndsWith("}}") ||
|
|
|
|
|
builder.Length > 0 && !word.EndsWith("}}"))
|
2020-08-17 22:21:11 -04:00
|
|
|
|
{
|
|
|
|
|
builder.Append($"{word} ");
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (builder.Length > 0)
|
|
|
|
|
{
|
|
|
|
|
builder.Append(word);
|
|
|
|
|
finalWord = builder.ToString();
|
|
|
|
|
builder.Clear();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var match = Regex.Match(finalWord, @"{{([^}|^-]+)(?:->)([^}]+)}}|{{([^}]+)}}");
|
2022-01-26 11:32:16 -05:00
|
|
|
|
var isInterpolation = match.Success;
|
2020-08-17 22:21:11 -04:00
|
|
|
|
|
|
|
|
|
results.Add(new WebfrontTranslationHelper
|
|
|
|
|
{
|
|
|
|
|
IsInterpolation = isInterpolation,
|
2022-01-26 11:32:16 -05:00
|
|
|
|
MatchValue = isInterpolation
|
|
|
|
|
? match.Groups[3].Length > 0 ? match.Groups[3].ToString() : match.Groups[1].ToString()
|
|
|
|
|
: finalWord,
|
2020-08-17 22:21:11 -04:00
|
|
|
|
TranslationValue = isInterpolation && match.Groups[2].Length > 0 ? match.Groups[2].ToString() : ""
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return results.ToArray();
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-30 17:39:32 -04:00
|
|
|
|
/// <summary>
|
2022-01-26 11:32:16 -05:00
|
|
|
|
/// indicates if running in development mode
|
2020-06-30 17:39:32 -04:00
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns></returns>
|
2022-01-26 11:32:16 -05:00
|
|
|
|
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>
|
2022-01-26 11:32:16 -05:00
|
|
|
|
/// replaces any directory separator chars with the platform specific character
|
2020-04-13 17:16:31 -04:00
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="path">original file path</param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public static string FixDirectoryCharacters(this string path)
|
|
|
|
|
{
|
2022-01-26 11:32:16 -05:00
|
|
|
|
foreach (var 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;
|
|
|
|
|
}
|
2020-08-17 22:21:11 -04:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
2022-01-26 11:32:16 -05:00
|
|
|
|
/// wrapper method for humanizee that uses current current culture
|
2020-08-17 22:21:11 -04:00
|
|
|
|
/// </summary>
|
2022-01-26 11:32:16 -05:00
|
|
|
|
public static string HumanizeForCurrentCulture(this TimeSpan timeSpan, int precision = 1,
|
|
|
|
|
TimeUnit maxUnit = TimeUnit.Week,
|
2021-03-22 12:09:25 -04:00
|
|
|
|
TimeUnit minUnit = TimeUnit.Second, string collectionSeparator = ", ", bool toWords = false)
|
2020-08-17 22:21:11 -04:00
|
|
|
|
{
|
2022-01-26 11:32:16 -05:00
|
|
|
|
return timeSpan.Humanize(precision, CurrentLocalization.Culture, maxUnit, minUnit, collectionSeparator,
|
|
|
|
|
toWords);
|
2020-08-17 22:21:11 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2022-01-26 11:32:16 -05:00
|
|
|
|
/// wrapper method for humanizee that uses current current culture
|
2020-08-17 22:21:11 -04:00
|
|
|
|
/// </summary>
|
2022-01-26 11:32:16 -05:00
|
|
|
|
public static string HumanizeForCurrentCulture(this DateTime input, bool utcDate = true,
|
|
|
|
|
DateTime? dateToCompareAgainst = null, CultureInfo culture = null)
|
2020-08-17 22:21:11 -04:00
|
|
|
|
{
|
|
|
|
|
return input.Humanize(utcDate, dateToCompareAgainst, CurrentLocalization.Culture);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static string ToTranslatedName(this MetaType metaType)
|
|
|
|
|
{
|
|
|
|
|
return CurrentLocalization.LocalizationIndex[$"META_TYPE_{metaType.ToString().ToUpper()}_NAME"];
|
|
|
|
|
}
|
2020-11-18 19:48:24 -05:00
|
|
|
|
|
2021-03-22 12:09:25 -04:00
|
|
|
|
public static EFClient ToPartialClient(this Data.Models.Client.EFClient client)
|
|
|
|
|
{
|
2022-01-26 11:32:16 -05:00
|
|
|
|
return new EFClient
|
2021-03-22 12:09:25 -04:00
|
|
|
|
{
|
|
|
|
|
ClientId = client.ClientId,
|
|
|
|
|
NetworkId = client.NetworkId,
|
|
|
|
|
Connections = client.Connections,
|
|
|
|
|
TotalConnectionTime = client.TotalConnectionTime,
|
|
|
|
|
FirstConnection = client.FirstConnection,
|
|
|
|
|
LastConnection = client.LastConnection,
|
|
|
|
|
Masked = client.Masked,
|
|
|
|
|
AliasLinkId = client.AliasLinkId,
|
|
|
|
|
AliasLink = client.AliasLink,
|
|
|
|
|
Level = client.Level,
|
|
|
|
|
CurrentAliasId = client.CurrentAliasId,
|
|
|
|
|
CurrentAlias = client.CurrentAlias,
|
|
|
|
|
Password = client.Password,
|
|
|
|
|
PasswordSalt = client.PasswordSalt,
|
|
|
|
|
Meta = client.Meta,
|
|
|
|
|
ReceivedPenalties = client.ReceivedPenalties,
|
|
|
|
|
AdministeredPenalties = client.AdministeredPenalties,
|
|
|
|
|
Active = client.Active
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static string ToNumericalString(this int? value)
|
|
|
|
|
{
|
|
|
|
|
return value?.ToNumericalString();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static string ToNumericalString(this int value)
|
|
|
|
|
{
|
|
|
|
|
return value.ToString("#,##0", CurrentLocalization.Culture);
|
|
|
|
|
}
|
2022-01-26 11:32:16 -05:00
|
|
|
|
|
2021-03-22 12:09:25 -04:00
|
|
|
|
public static string ToNumericalString(this double value, int precision = 0)
|
|
|
|
|
{
|
2022-01-26 11:32:16 -05:00
|
|
|
|
return value.ToString(
|
|
|
|
|
"#,##0" + $"{(precision > 0 ? "." : "")}" + new string(Enumerable.Repeat('0', precision).ToArray()),
|
|
|
|
|
CurrentLocalization.Culture);
|
2021-03-22 12:09:25 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static string ToNumericalString(this double? value, int precision = 0)
|
|
|
|
|
{
|
|
|
|
|
return value?.ToNumericalString(precision);
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-08 22:12:09 -04:00
|
|
|
|
public static string[] FragmentMessageForDisplay(this string message)
|
|
|
|
|
{
|
|
|
|
|
var messages = new List<string>();
|
|
|
|
|
var length = 48;
|
|
|
|
|
|
|
|
|
|
if (message.Length <= length)
|
|
|
|
|
{
|
2022-01-26 11:32:16 -05:00
|
|
|
|
return new[] { message };
|
2021-07-08 22:12:09 -04:00
|
|
|
|
}
|
2022-01-26 11:32:16 -05:00
|
|
|
|
|
2021-07-08 22:12:09 -04:00
|
|
|
|
int i;
|
|
|
|
|
for (i = 0; i < message.Length - length; i += length)
|
|
|
|
|
messages.Add(new string(message.Skip(i).Take(length).ToArray()));
|
|
|
|
|
|
|
|
|
|
var left = message.Length - length;
|
|
|
|
|
|
|
|
|
|
if (left > 0)
|
|
|
|
|
{
|
|
|
|
|
messages.Add(new string(message.Skip(i).Take(left).ToArray()));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return messages.ToArray();
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-18 19:48:24 -05:00
|
|
|
|
public static string FindRuleForReason(this string reason, ApplicationConfiguration appConfig, Server server)
|
|
|
|
|
{
|
2020-12-31 19:48:58 -05:00
|
|
|
|
// allow for penalty presets
|
|
|
|
|
if (appConfig.PresetPenaltyReasons?.ContainsKey(reason.ToLower()) ?? false)
|
|
|
|
|
{
|
|
|
|
|
return appConfig.PresetPenaltyReasons[reason.ToLower()];
|
|
|
|
|
}
|
2022-01-26 11:32:16 -05:00
|
|
|
|
|
2021-02-27 10:40:25 -05:00
|
|
|
|
var regex = Regex.Match(reason, @"rule(\d+)", RegexOptions.IgnoreCase);
|
2020-11-18 19:48:24 -05:00
|
|
|
|
if (!regex.Success)
|
|
|
|
|
{
|
|
|
|
|
return reason;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var serverConfig = appConfig.Servers?
|
|
|
|
|
.FirstOrDefault(configServer =>
|
|
|
|
|
configServer.IPAddress == server.IP && configServer.Port == server.Port);
|
|
|
|
|
|
2021-02-27 10:40:25 -05:00
|
|
|
|
var allRules = appConfig.GlobalRules?.ToList() ?? new List<string>();
|
|
|
|
|
if (serverConfig?.Rules != null)
|
|
|
|
|
{
|
|
|
|
|
allRules.AddRange(serverConfig.Rules);
|
|
|
|
|
}
|
2020-11-18 19:48:24 -05:00
|
|
|
|
|
2021-02-27 10:40:25 -05:00
|
|
|
|
var index = int.Parse(regex.Groups[1].ToString()) - 1;
|
|
|
|
|
|
|
|
|
|
if (!allRules.Any() || index > allRules.Count - 1 || index < 0)
|
2020-11-18 19:48:24 -05:00
|
|
|
|
{
|
2021-02-27 10:40:25 -05:00
|
|
|
|
return reason;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return allRules[index];
|
2020-11-18 19:48:24 -05:00
|
|
|
|
}
|
2015-08-20 01:06:44 -04:00
|
|
|
|
}
|
2022-01-27 22:18:35 -05:00
|
|
|
|
}
|