Raised This Month: $32 Target: $400
 8% 

Get CommunityID (SteamID64) String


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
11530
Veteran Member
Join Date: Sep 2011
Location: Underworld
Old 04-21-2012 , 19:38   Get CommunityID (SteamID64) String
Reply With Quote #1

I'd been wanting to find a function which retrieves a client's CommunityID but didn't find what I wanted from the original thread here. I also didn't want to have to resort to extensions/includes. The main problem (aside from 32-bit limits) was that most functions wouldn't work with newer SteamIDs so I've created a quick function for everyone here that will work with all individual SteamIDs.

I'm completely open to any optimizations here, so please write down any suggestions below.

To use the function, create two strings, one that holds a client's SteamID (you can use GetClientAuthString for this too) and the other for the function's output. I've also made two directives to define the maximum lengths of SteamIDs and CommunityIDs.

Usage:
PHP Code:
new String:SteamID[MAX_STEAMID_LENGTH] = "STEAM_0:0:6070829"String:CommunityID[MAX_COMMUNITYID_LENGTH];
GetCommunityIDString(SteamIDCommunityIDsizeof(CommunityID)); 
My original:
PHP Code:
#define MAX_STEAMID_LENGTH 21
#define MAX_COMMUNITYID_LENGTH 18

stock bool:GetCommunityIDString(const String:SteamID[], String:CommunityID[], const CommunityIDSize)
{
    
decl String:SteamIDParts[3][11];
    new const 
String:Identifier[] = "76561197960265728";
    
    if ((
CommunityIDSize 1) || (ExplodeString(SteamID":"SteamIDPartssizeof(SteamIDParts), sizeof(SteamIDParts[])) != 3))
    {
        
CommunityID[0] = '\0';
        return 
false;
    }

    new 
CurrentCarryOver = (SteamIDParts[1][0] == '1');
    for (new 
= (CommunityIDSize 2), = (strlen(SteamIDParts[2]) - 1), = (strlen(Identifier) - 1); >= 0i--, j--, k--)
    {
        
Current = (>= ? (* (SteamIDParts[2][j] - '0')) : 0) + CarryOver + (>= ? ((Identifier[k] - '0') * 1) : 0);
        
CarryOver Current 10;
        
CommunityID[i] = (Current 10) + '0';
    }

    
CommunityID[CommunityIDSize 1] = '\0';
    return 
true;

Output given my SteamID as above: 76561197972407386



Code attributed to Zephyrus - untested, but may work:
PHP Code:
stock bool:GetCommunityID(String:AuthID[], String:FriendID[], size)
{
    if(
strlen(AuthID) < 11 || AuthID[0]!='S' || AuthID[6]=='I')
    {
        
FriendID[0] = 0;
        return 
false;
    }

    new 
iUpper 765611979;
    new 
iFriendID StringToInt(AuthID[10])*60265728 AuthID[8]-48;

    new 
iDiv iFriendID/100000000;
    new 
iIdx 9-(iDiv?iDiv/10+1:0);
    
iUpper += iDiv;
    
    
IntToString(iFriendIDFriendID[iIdx], size-iIdx);
    
iIdx FriendID[9];
    
IntToString(iUpperFriendIDsize);
    
FriendID[9] = iIdx;

    return 
true;

Changelog:
  • Altered expensive strcmp checks for non-standard SteamIDs
  • Changed Format to strcopy
  • Removed code that itself removed "STEAM_"
  • Added a null-terminator so one can use decl as well as new.
  • Improved the function to be much smaller, more efficient and allow for larger output strings (with leading zeros).
  • Added Zephyrus's code as a possible faster alternative.
__________________

Last edited by 11530; 02-24-2013 at 15:31. Reason: Typo - post #33
11530 is offline
Alex30555
Member
Join Date: Sep 2011
Location: Paris
Old 04-22-2012 , 13:08   Re: Get CommunityID (SteamID64) String
Reply With Quote #2

Thanks
Alex30555 is offline
11530
Veteran Member
Join Date: Sep 2011
Location: Underworld
Old 04-24-2012 , 11:35   Re: Get CommunityID (SteamID64) String
Reply With Quote #3

Quote:
Originally Posted by Alex30555 View Post
Thanks
You're welcome!

I'll reserve this space to describe the process of getting the SteamID64 if requested.
__________________

Last edited by 11530; 04-24-2012 at 11:36.
11530 is offline
asherkin
SourceMod Developer
Join Date: Aug 2009
Location: OnGameFrame()
Old 04-24-2012 , 11:50   Re: Get CommunityID (SteamID64) String
Reply With Quote #4

Doing all the strcmp checks is expensive, just check ExplodeString's return value.
__________________
asherkin is offline
11530
Veteran Member
Join Date: Sep 2011
Location: Underworld
Old 04-24-2012 , 12:38   Re: Get CommunityID (SteamID64) String
Reply With Quote #5

Quote:
Originally Posted by asherkin View Post
Doing all the strcmp checks is expensive, just check ExplodeString's return value.
Cheers, I've updated the OP to reflect the optimizations.
__________________
11530 is offline
minimoney1
SourceMod Donor
Join Date: Dec 2010
Old 04-24-2012 , 18:15   Re: Get CommunityID (SteamID64) String
Reply With Quote #6

Quote:
Originally Posted by 11530 View Post
Cheers, I've updated the OP to reflect the optimizations.
I would recommend using strcopy instead of Format when checking the ExplodeString return.
(Format makes it go thorugh all the trouble of checking for format class functions, such as %s, %d, etc), and since you have none, strcopy would be faster in this case.

Last edited by minimoney1; 04-24-2012 at 18:15.
minimoney1 is offline
11530
Veteran Member
Join Date: Sep 2011
Location: Underworld
Old 04-24-2012 , 21:16   Re: Get CommunityID (SteamID64) String
Reply With Quote #7

Quote:
Originally Posted by minimoney1 View Post
I would recommend using strcopy instead of Format when checking the ExplodeString return.
(Format makes it go thorugh all the trouble of checking for format class functions, such as %s, %d, etc), and since you have none, strcopy would be faster in this case.
Yea I originally used Format because I kept hearing on another thread about how Format is "safer" and didn't really mind using either. Now I have a recommendation, I'll go ahead and change it. Thanks!
__________________

Last edited by 11530; 04-24-2012 at 21:17.
11530 is offline
Meow Mix
Member
Join Date: Mar 2009
Old 08-06-2012 , 19:31   Re: Get CommunityID (SteamID64) String
Reply With Quote #8

I went to use this for a CS:GO plugin, except realized that the Community ID's returned weren't quite right. After doing a little research, I discovered that your stock is returning nothing but "76561197960265728" which is the identifier (and the stock is missing this part of the equation: accountID * 2).

I tried manually having the SteamID's inputted, as well as get them from GetClientAuthString.

Code:
    new String:sAuth[32] = "STEAM_0:0:14297752";
    new String:sComm[32];
    GetCommunityIDString(sAuth, sComm, sizeof(sComm));
    LogMessage("SteamID to CommunityID: %s -> %s", sAuth, sComm);
    sAuth = "STEAM_0:0:6070829";
    GetCommunityIDString(sAuth, sComm, sizeof(sComm));
    LogMessage("SteamID to CommunityID: %s -> %s", sAuth, sComm);
Code:
L 08/06/2012 - 18:25:00: [test.smx] SteamID to CommunityID: STEAM_0:0:14297752 -> 76561197960265728
L 08/06/2012 - 18:25:00: [test.smx] SteamID to CommunityID: STEAM_0:0:6070829  -> 76561197960265728
Anyone got any ideas?
Meow Mix is offline
11530
Veteran Member
Join Date: Sep 2011
Location: Underworld
Old 08-06-2012 , 19:49   Re: Get CommunityID (SteamID64) String
Reply With Quote #9

It's just the mathematics of the function since it has to work with strings. Change new String:sComm[32] to [18] and you'll be fine.

When using [18], I get: STEAM_0:0:14297752 -> 76561197988861232

All Steam64IDs are 17 characters long, so an array of 18 always works, but when I have time I'll add something so that 0s are prefixed if someone needs to use a longer array.
__________________

Last edited by 11530; 08-06-2012 at 19:53.
11530 is offline
Meow Mix
Member
Join Date: Mar 2009
Old 08-06-2012 , 19:57   Re: Get CommunityID (SteamID64) String
Reply With Quote #10

Ah, and of course I changed that in my plugin while I was waiting for a reply...didn't expect that to be the problem but makes sense now! Thanks!
Meow Mix is offline
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 19:49.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode