diff --git a/Configuration/SteamIdConverter.cs b/Configuration/SteamIdConverter.cs index f8236d9..111e8c1 100644 --- a/Configuration/SteamIdConverter.cs +++ b/Configuration/SteamIdConverter.cs @@ -1,4 +1,6 @@ using System; +using System.Text.RegularExpressions; +using NFive.SDK.Core.Configuration; using NFive.SDK.Core.Plugins; using YamlDotNet.Core; using YamlDotNet.Core.Events; @@ -13,6 +15,10 @@ namespace NFive.SDK.Plugins.Configuration /// public class SteamIdConverter : IYamlTypeConverter { + private static readonly Regex Steam2Regex = new Regex("^STEAM_0:[0-1]:([0-9]{1,10})$", RegexOptions.IgnoreCase | RegexOptions.Compiled); + private static readonly Regex Steam32Regex = new Regex("^\\[?U:1:([0-9]{1,10})\\]?$", RegexOptions.IgnoreCase | RegexOptions.Compiled); + private static readonly Regex Steam64Regex = new Regex("^7656119([0-9]{10})$", RegexOptions.Compiled); + /// /// /// Gets a value indicating whether the current converter supports converting the specified type. @@ -25,9 +31,29 @@ public class SteamIdConverter : IYamlTypeConverter /// public object ReadYaml(IParser parser, Type type) { - var value = ((Scalar)parser.Current).Value; + var value = ((Scalar)parser.Current).Value.Trim(); parser.MoveNext(); - return new SteamId(value).ToSteam64(); + + long id; + + if (Steam64Regex.IsMatch(value)) + { + id = long.Parse(value); + } + else if (Steam2Regex.IsMatch(value)) + { + id = SteamId.FromSteamId2(value); + } + else if (Steam32Regex.IsMatch(value)) + { + id = SteamId.FromSteamId32(value); + } + else + { + throw new YamlException("YML input not in valid SteamID 2, 32 or 64 format"); + } + + return new SteamId(id); } /// @@ -36,7 +62,7 @@ public object ReadYaml(IParser parser, Type type) /// public void WriteYaml(IEmitter emitter, object value, Type type) { - emitter.Emit(new Scalar(new SteamId(value.ToString()).ToSteam64().ToString())); + emitter.Emit(new Scalar(value.ToString())); } } } diff --git a/NFive.SDK.Plugins.csproj b/NFive.SDK.Plugins.csproj index ad37e99..5471619 100644 --- a/NFive.SDK.Plugins.csproj +++ b/NFive.SDK.Plugins.csproj @@ -43,11 +43,11 @@ - packages\Newtonsoft.Json.12.0.3\lib\net45\Newtonsoft.Json.dll + packages\Newtonsoft.Json.12.0.2\lib\net45\Newtonsoft.Json.dll False - - packages\NFive.SDK.Core.0.1.0.57\lib\net452\NFive.SDK.Core.net.dll + + packages\NFive.SDK.Core.0.1.0.60\lib\net452\NFive.SDK.Core.net.dll False @@ -76,7 +76,6 @@ - diff --git a/SteamId.cs b/SteamId.cs deleted file mode 100644 index 2754c66..0000000 --- a/SteamId.cs +++ /dev/null @@ -1,79 +0,0 @@ -using System; -using System.Globalization; -using System.Text.RegularExpressions; - -namespace NFive.SDK.Plugins -{ - public class SteamId - { - /// - /// SteamID2 Regex - /// - private const string Steam2Regex = "^STEAM_0:[0-1]:([0-9]{1,10})$"; - - /// - /// SteamID32 Regex - /// - private const string Steam32Regex = "^U:1:([0-9]{1,10})$"; - - /// - /// SteamID64 Regex - /// - private const string Steam64Regex = "^7656119([0-9]{10})$"; - - private readonly long id; - - public SteamId(string input) - { - if (Regex.IsMatch(input, Steam64Regex)) - { - this.id = long.Parse(input); - } - else if (Regex.IsMatch(input, Steam2Regex)) - { - this.id = Steam2ToSteam64(input); - } - else if (Regex.IsMatch(input, Steam32Regex)) - { - this.id = Steam32ToSteam64(input); - } - else - { - throw new Exception(); - } - } - - /// - /// Converts Steam32 IDs to Steam64 IDs format. - /// - /// Steam32 ID - /// Returns the SteamID64(76561197960265728) in long type - private static long Steam32ToSteam64(string input) - { - var steam32 = Convert.ToInt64(input.Substring(4)); - if (steam32 < 1L || !Regex.IsMatch("U:1:" + steam32.ToString(CultureInfo.InvariantCulture), "^U:1:([0-9]{1,10})$")) - { - return 0; - } - - return steam32 + 76561197960265728L; - } - - /// - /// Converts Steam2 IDs to Steam64 IDs format. - /// - /// - /// Returns the SteamID64(76561197960265728) in long type - private static long Steam2ToSteam64(string accountId) - { - if (!Regex.IsMatch(accountId, "^STEAM_0:[0-1]:([0-9]{1,10})$")) - { - return 0; - } - - return 76561197960265728L + Convert.ToInt64(accountId.Substring(10)) * 2L + Convert.ToInt64(accountId.Substring(8, 1)); - } - - public long ToSteam64() => this.id; - } -} diff --git a/packages.config b/packages.config index 3faa1a8..0aa5dc5 100644 --- a/packages.config +++ b/packages.config @@ -1,8 +1,8 @@  - - + + \ No newline at end of file