/// <summary> /// Constructs a Steam Id from three of the four components. Instance is defaulted to a value based on the Account Type. /// </summary> /// <param name="universe">Which Steam system this Steam Id belongs to (such as Public)</param> /// <param name="accountType">The type of account for this Steam Id (such as Individual)</param> /// <param name="accountId">The 32-bit account identifier for the Steam account</param> public SteamId(SteamUniverse universe, SteamAccountType accountType, uint accountId) { Universe = universe; AccountType = accountType; AccountId = accountId; ResolvedFrom = SteamIdResolvedFrom.IndividualComponents; }
/// <summary> /// Constructs an approximated Steam Id based on a 32-bit Account Id. Since a full Steam Id includes more than just an Account Id, it is /// impossible to convert directly from Account Id to 64-bit Steam Id without making some assumptions. This constructor will assume the following. /// Universe: Public (1), Instance: Desktop (1), AccountType: Individual (1). /// </summary> /// <param name="accountId">32-bit Account Id portion of the Steam Id (the lowest 32-bits of the 64-bit Steam Id)</param> public SteamId(uint accountId) { AccountId = accountId; ulong steamId64 = (ulong)AccountId | 0x110000100000000; ConstructFromSteamId64(steamId64); ResolvedFrom = SteamIdResolvedFrom.AccountIdApproximation; }
/// <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 == "http" || uriResult.Scheme == "https"); 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; } } }
/// <summary> /// Constructs a Steam Id based on its 64-bit representation. The Universe, Account Type, Instance, and Account Id will be extracted from the 64-bit value. /// </summary> /// <param name="steamId64">The 64-bit Steam Id</param> public SteamId(ulong steamId64) { ConstructFromSteamId64(steamId64); ResolvedFrom = SteamIdResolvedFrom.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; } } }
/// <summary> /// Constructs a Steam Id by parsing the provided value. This 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> public async Task ResolveAsync(string 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 == "http" || uriResult.Scheme == "https"); try { // 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]; // if a user has a vanity name setup in their steam profile, the steam profile url will be in the format of: // http://steamcommunity.com/id/<vanity name> // otherwise, the format will be: // http://steamcommunity.com/profiles/<64-bit Steam ID> if (uriResult.Segments[1] == "id/") { steamId = await ResolveSteamIdFromValueAsync(steamUser, profileId); ConstructFromSteamId64(steamId); ResolvedFrom = SteamIdResolvedFrom.SteamCommunityUri; } else if (uriResult.Segments[1] == "profiles/") { bool isSteamId64 = ulong.TryParse(profileId, out steamId); if (isSteamId64) { ConstructFromSteamId64(steamId); ResolvedFrom = SteamIdResolvedFrom.SteamCommunityUri; } else { throw new InvalidSteamCommunityUriException(ErrorMessages.InvalidSteamCommunityUri); } } else { throw new InvalidSteamCommunityUriException(ErrorMessages.InvalidSteamCommunityUri); } } else { throw new InvalidSteamCommunityUriException(ErrorMessages.InvalidSteamCommunityUri); } } else { // 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; } } 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; } } }