Skip to content

Instantly share code, notes, and snippets.

@bmwalters
Last active December 14, 2019 02:26
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bmwalters/a5dfd114b067ea5e84c7 to your computer and use it in GitHub Desktop.
Save bmwalters/a5dfd114b067ea5e84c7 to your computer and use it in GitHub Desktop.
Lua SteamID class
-- version 1.2.0
-- ported from https://github.com/DoctorMcKay/node-steamid/blob/master/index.js
-- thanks to https://developer.valvesoftware.com/wiki/SteamID
local steamid = {}
steamid.Universe = {
INVALID = 0,
PUBLIC = 1,
BETA = 2,
INTERNAL = 3,
DEV = 4,
}
steamid.Type = {
INVALID = 0,
INDIVIDUAL = 1,
MULTISEAT = 2,
GAMESERVER = 3,
ANON_GAMESERVER = 4,
PENDING = 5,
CONTENT_SERVER = 6,
CLAN = 7,
CHAT = 8,
P2P_SUPER_SEEDER = 9,
ANON_USER = 10,
}
steamid.Instance = {
ALL = 0,
DESKTOP = 1,
CONSOLE = 2,
WEB = 4,
}
steamid.TypeChars = {
[steamid.Type.INVALID] = "I",
[steamid.Type.INDIVIDUAL] = "U",
[steamid.Type.MULTISEAT] = "M",
[steamid.Type.GAMESERVER] = "G",
[steamid.Type.ANON_GAMESERVER] = "A",
[steamid.Type.PENDING] = "P",
[steamid.Type.CONTENT_SERVER] = "C",
[steamid.Type.CLAN] = "g",
[steamid.Type.CHAT] = "T",
[steamid.Type.ANON_USER] = "a",
}
steamid.AccountIDMask = 0xFFFFFFFF
steamid.AccountInstanceMask = 0x000FFFFF
steamid.ChatInstanceFlags = {
Clan = (steamid.AccountInstanceMask + 1) >> 1,
Lobby = (steamid.AccountInstanceMask + 1) >> 2,
MMSLobby = (steamid.AccountInstanceMask + 1) >> 3,
}
local STEAMID = {}
STEAMID.__index = STEAMID
function STEAMID:initialize(sid)
self.universe = steamid.Universe.INVALID
self.type = steamid.Type.INVALID
self.instance = steamid.Instance.ALL
self.accountid = 0
if not sid then return end
sid = tostring(sid)
-- steam2
do
local universe, acclow, acchigh = string.match(sid, "^STEAM_([0-5]):([0-1]):([0-9]+)$")
if universe and acclow and acchigh then
self.universe = tonumber(universe)
if self.universe == steamid.Universe.INVALID then self.universe = steamid.Universe.PUBLIC end
self.type = steamid.Type.INDIVIDUAL
self.instance = steamid.Instance.DESKTOP
self.accountid = (tonumber(acchigh) << 1) + tonumber(acclow)
return
end
end
-- sid64 (community id)
do
local n = tonumber(sid)
if n and #sid >= 16 then
self.universe = n >> 56
if self.universe == steamid.Universe.INVALID then self.universe = steamid.Universe.PUBLIC end
self.type = (n >> 52) & (2^4 - 1)
self.instance = (n >> 32) & (2^20 - 1)
self.accountid = n & (2^32 - 1)
return
end
end
-- steam3
do
local typechar, universe, accountid, instance = string.match(sid, "^%[(%a):([0-5]):(%d+)(.*)%]$")
if typechar and universe and accountid then
self.universe = tonumber(universe)
if self.universe == steamid.Universe.INVALID then self.universe = steamid.Universe.PUBLIC end
self.accountid = tonumber(accountid)
if instance and instance ~= "" then
self.instance = tonumber(string.sub(instance, 2)) -- remove ":" in instance
elseif typechar == "U" then
self.instance = steamid.Instance.DESKTOP
end
if typechar == "c" then
self.instance = (self.instance | steamid.ChatInstanceFlags.Clan)
self.type = steamid.Type.CHAT
elseif typechar == "L" then
self.instance = (self.instance | steamid.ChatInstanceFlags.Lobby)
self.type = steamid.Type.CHAT
else
for k, v in pairs(steamid.TypeChars) do
if v == typechar then
self.type = k
break
end
end
end
return
end
end
-- plain accountid; you'll have to fill in other fields yourself
do
local n = tonumber(sid)
if n and #sid < 16 then
self.accountid = n
return
end
end
end
function STEAMID:to64()
return (self.universe << 56) | (self.type << 52) | (self.instance << 32) | self.accountid
end
function STEAMID:tosteam2()
if self.type ~= steamid.Type.INDIVIDUAL then return end
return "STEAM_" .. (self.universe == steamid.Universe.PUBLIC and 0 or self.universe) .. ':' .. (self.accountid & 1) .. ':' .. (self.accountid >> 1)
end
function STEAMID:tosteam3()
local typechar = steamid.TypeChars[self.type] or "i"
if (self.instance & steamid.ChatInstanceFlags.Clan) ~= 0 then
typechar = "c"
elseif (self.instance & steamid.ChatInstanceFlags.Lobby) ~= 0 then
typechar = "L"
end
local useinstance = (self.type == steamid.Type.ANON_GAMESERVER or self.type == steamid.Type.MULTISEAT or (self.type == steamid.Type.INDIVIDUAL and self.instance ~= steamid.Instance.DESKTOP))
return "[" .. typechar .. ":" .. self.universe .. ":" .. self.accountid .. (useinstance and (":" .. self.instance) or "") .. "]"
end
function STEAMID:__tostring()
return "steamid " .. self:tosteam3()
end
local function SteamID(...)
local s = setmetatable({}, STEAMID)
s:initialize(...)
return s
end
return SteamID
--[[
local s = SteamID("STEAM_0:0:46161927")
print(s.accountid) -- 92323854
print(s:to64()) -- 76561198052589582
print(s:tosteam2()) -- STEAM_0:0:46161927
print(s:tosteam3()) -- [U:1:92323854]
local s2 = SteamID("85568392920136453")
print(s2.accountid) -- 97029
print(s2:to64()) -- 85568392920136453
print(s2:tosteam2()) -- (nil)
print(s2:tosteam3()) -- [G:1:97029]
--]]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment