AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Snippets and Tutorials (https://forums.alliedmods.net/forumdisplay.php?f=112)
-   -   Get CommunityID (SteamID64) String (https://forums.alliedmods.net/showthread.php?t=183443)

11530 04-21-2012 19:38

Get CommunityID (SteamID64) String
 
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.

Alex30555 04-22-2012 13:08

Re: Get CommunityID (SteamID64) String
 
Thanks :D

11530 04-24-2012 11:35

Re: Get CommunityID (SteamID64) String
 
Quote:

Originally Posted by Alex30555 (Post 1694522)
Thanks :D

You're welcome! :3

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

asherkin 04-24-2012 11:50

Re: Get CommunityID (SteamID64) String
 
Doing all the strcmp checks is expensive, just check ExplodeString's return value.

11530 04-24-2012 12:38

Re: Get CommunityID (SteamID64) String
 
Quote:

Originally Posted by asherkin (Post 1695681)
Doing all the strcmp checks is expensive, just check ExplodeString's return value.

Cheers, I've updated the OP to reflect the optimizations.

minimoney1 04-24-2012 18:15

Re: Get CommunityID (SteamID64) String
 
Quote:

Originally Posted by 11530 (Post 1695702)
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.

11530 04-24-2012 21:16

Re: Get CommunityID (SteamID64) String
 
Quote:

Originally Posted by minimoney1 (Post 1695873)
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! :)

Meow Mix 08-06-2012 19:31

Re: Get CommunityID (SteamID64) String
 
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?

11530 08-06-2012 19:49

Re: Get CommunityID (SteamID64) String
 
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.

Meow Mix 08-06-2012 19:57

Re: Get CommunityID (SteamID64) String
 
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!


All times are GMT -4. The time now is 09:30.

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