Thomasims

Lua SteamID Converter

Jul 23rd, 2019
493
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.97 KB | None | 0 0
  1. do -- player.util
  2.     util = {}
  3.  
  4.     local universes64 = {
  5.         [ "45036" ] = "0",
  6.         [ "76561" ] = "1",
  7.         [ "14861" ] = "2",
  8.         [ "22067" ] = "3",
  9.         [ "29273" ] = "4",
  10.         [ "36479" ] = "5"
  11.     }
  12.     local bases64 = {
  13.         [ "0" ] = "450360",
  14.         [ "1" ] = "76561",
  15.         [ "2" ] = "14861",
  16.         [ "3" ] = "22067",
  17.         [ "4" ] = "29273",
  18.         [ "5" ] = "36479"
  19.     }
  20.     local offsets64 = {
  21.         [ "0" ] = 3922337792,
  22.         [ "1" ] = 197960265728,
  23.         [ "2" ] = 8791998193664,
  24.         [ "3" ] = 6386036121600,
  25.         [ "4" ] = 3980074049536,
  26.         [ "5" ] = 1574111977472
  27.     }
  28.  
  29.     function util.IsSteamID64( sParam )
  30.         if type( sParam ) ~= "string" then return end
  31.         return universes64[ string.sub( sParam, 1, 5 ) ]
  32.     end
  33.  
  34.     function util.IsSteamID32( sParam )
  35.         if type( sParam ) ~= "string" then return end
  36.         return string.match( sParam, "STEAM_([012345]):([01]):(%d+)" )
  37.     end
  38.  
  39.     function util.ChangeSteamID( sSteamID, sNewUniverse, bTo64 )
  40.         sNewUniverse = tostring( sNewUniverse or 1 )
  41.         if not offsets64[ sNewUniverse ] then return end
  42.         local sUniverse64 = util.IsSteamID64( sSteamID )
  43.         if sUniverse64 then
  44.             if sUniverse64 == sNewUniverse and bTo64 ~= false then return sSteamID end
  45.             local iAccount = tonumber( string.sub( sSteamID, 6 ) ) - offsets64[ sUniverse64 ]
  46.             if bTo64 ~= false then
  47.                 return string.format( "%s%s", bases64[ sNewUniverse ], tostring( iAccount + offsets64[ sNewUniverse ] ) )
  48.             else
  49.                 local sLow, sHigh = tostring( iAccount % 2 ), tostring( math.floor( iAccount / 2 ) )
  50.                 return string.format( "STEAM_%s:%s:%s", sNewUniverse, sLow, sHigh )
  51.             end
  52.         else
  53.             local sUniverse32, sLow, sHigh = util.IsSteamID32( sSteamID )
  54.             if sUniverse32 == sNewUniverse and not bTo64 then return sSteamID end
  55.             if not bTo64 then
  56.                 return string.format( "STEAM_%s:%s:%s", sNewUniverse, sLow, sHigh )
  57.             else
  58.                 local iAccount = tonumber( sLow ) + tonumber( sHigh ) * 2
  59.                 return string.format( "%s%s", bases64[ sNewUniverse ], tostring( iAccount + offsets64[ sNewUniverse ] ) )
  60.             end
  61.         end
  62.     end
  63. end
Advertisement
Add Comment
Please, Sign In to add comment