/// <summary> /// Queries the Steam Web API for a 64-bit Steam ID based on a provided string value. /// </summary> /// <param name="steamUser"></param> /// <param name="value"></param> /// <returns></returns> private static async Task<ulong> ResolveSteamIdFromValueAsync(SteamUser steamUser, string value) { ulong steamId64 = 0; var result = await steamUser.ResolveVanityUrlAsync(value); // the value didn't resolve to a 64-bit Steam ID if (result.Success == 42) // returns 42 on "no match" { throw new VanityUrlNotResolvedException(ErrorMessages.VanityUrlNotResolved); } else { steamId64 = result.SteamId; } return steamId64; }
/// <summary> /// Constructs a Steam Id by parsing the provided value. This constructor will try to parse the value to a 64-bit Steam Id or a Steam Community Profile URL depending on the input. /// If a Profile URL is provided but is unable to be resolved to a 64-bit Steam ID, a VanityUrlNotResolvedException will be thrown. Network access may be required /// to receive a return value from this method in the event that the Steam Web API is required. /// </summary> /// <param name="value">Value to parse, can be a 64-bit Steam ID, a full Steam Community Profile URL, or the user's Steam Community Profile Name.</param> /// <param name="steamWebApiKey">Required in the event that the Steam Web API is needed to resolve a Profile URL to a 64-bit Steam ID.</param> public SteamId(string value, string steamWebApiKey = "") { if (String.IsNullOrEmpty(value)) { throw new ArgumentNullException("value"); } ulong steamId = 0; MatchCollection legacyCheckResult = null; MatchCollection modernCheckResult = null; // it's a legacy steam id in the format of STEAM_X:Y:Z if (IsLegacySteamId(value, out legacyCheckResult)) { string universe = legacyCheckResult[0].Groups[1].Value; string authServer = legacyCheckResult[0].Groups[2].Value; string accountId = legacyCheckResult[0].Groups[3].Value; ConstructFromLegacySteamId(universe, authServer, accountId); ResolvedFrom = SteamIdResolvedFrom.LegacySteamId; } // it's a modern steam id in the format of [A:B:C:D] else if (IsModernSteamId(value, out modernCheckResult)) { string accountTypeCharacter = modernCheckResult[0].Groups[1].Value; string universe = modernCheckResult[0].Groups[2].Value; string accountId = modernCheckResult[0].Groups[3].Value; string instance = String.Empty; if (modernCheckResult[0].Groups.Count == 5) { instance = modernCheckResult[0].Groups[4].Value; } ConstructFromModernSteamId(accountTypeCharacter, instance, universe, accountId); ResolvedFrom = SteamIdResolvedFrom.ModernSteamId; } // it's a 64-bit steam id else if (ulong.TryParse(value, out steamId)) { ConstructFromSteamId64(steamId); ResolvedFrom = SteamIdResolvedFrom.SteamId64; } // it's some other type of string, let's find out what it is else { // check if the caller entered a valid uri Uri uriResult; bool isUri = Uri.TryCreate(value, UriKind.Absolute, out uriResult) && (uriResult.Scheme == Uri.UriSchemeHttp || uriResult.Scheme == Uri.UriSchemeHttps); SteamUser steamUser = new SteamUser(steamWebApiKey); try { Task.Run(async () => { // the caller provided a uri if (isUri) { // the caller provided a uri in the format we expected if (uriResult.Segments.Length == 3) { string profileId = uriResult.Segments[2]; // try to parse the 3rd segment as a 64-bit Steam ID (http://steamcommunity.com/profiles/762541427451 for example) bool isSteamId64 = ulong.TryParse(profileId, out steamId); // the third segment isn't a 64-bit Steam ID, check if it's a profile name which resolves to a 64-bit Steam ID if (!isSteamId64) { if (String.IsNullOrEmpty(steamWebApiKey)) { throw new InvalidOperationException(ErrorMessages.SteamWebApiKeyNotProvided); } steamId = await ResolveSteamIdFromValueAsync(steamUser, profileId); } ConstructFromSteamId64(steamId); } else { throw new InvalidSteamCommunityUriException(ErrorMessages.InvalidSteamCommunityUri); } ResolvedFrom = SteamIdResolvedFrom.SteamCommunityUri; } else { if (String.IsNullOrEmpty(steamWebApiKey)) { throw new InvalidOperationException(ErrorMessages.SteamWebApiKeyNotProvided); } // not a 64-bit Steam ID and not a uri, try to just resolve it as if it was a Steam Community Profile Name steamId = await ResolveSteamIdFromValueAsync(steamUser, value); ConstructFromSteamId64(steamId); ResolvedFrom = SteamIdResolvedFrom.SteamCommunityProfileName; } }).Wait(); } catch (AggregateException ex) { foreach (var innerEx in ex.InnerExceptions) { // throw the first specific exception that we expect if (innerEx is VanityUrlNotResolvedException || innerEx is InvalidSteamCommunityUriException) { ExceptionDispatchInfo.Capture(innerEx).Throw(); } } // otherwise just throw the Aggregate and let the caller handle it throw; } } }