Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimized player.GetBySteamID/player.GetBySteamID64 #2094

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 28 additions & 17 deletions garrysmod/lua/includes/extensions/player.lua
Original file line number Diff line number Diff line change
Expand Up @@ -284,28 +284,24 @@ function player.GetByUniqueID( ID )
return false
end

local sIDCache = {}

function player.GetBySteamID( ID )
ID = string.upper( ID )
local players = player.GetAll()
for i = 1, #players do
if ( players[i]:SteamID() == ID ) then
return players[i]
end

if ( ID == "BOT" ) then
return false
end

return false
local cID = util.SteamIDFrom64( ID )

return sIDCache[cID] or false
end

function player.GetBySteamID64( ID )
ID = tostring( ID )
local players = player.GetAll()
for i = 1, #players do
if ( players[i]:SteamID64() == ID ) then
return players[i]
end
end

return false
return sIDCache[ID] or false
end

local inext = ipairs( {} )
Expand All @@ -319,11 +315,26 @@ function player.Iterator()

end

local function InvalidatePlayerCache( ent )
local function InvalidatePlayer( ent, fullUpdate )

local isPlayer = ent:IsPlayer()

if ( isPlayer ) then PlayerCache = nil end

if ( ent:IsPlayer() ) then PlayerCache = nil end
if ( fullUpdate or not isPlayer ) then
return
end

local steamID = ent:SteamID64()

-- If fullUpdate isn't nil, this player was invalidated by EntityRemoved.
if ( fullUpdate == false ) then
sIDCache[steamID] = nil
else
sIDCache[steamID] = ent
end

end

hook.Add( "OnEntityCreated", "player.Iterator", InvalidatePlayerCache )
hook.Add( "EntityRemoved", "player.Iterator", InvalidatePlayerCache )
hook.Add( "OnEntityCreated", "player.Invalidation", InvalidatePlayer )
hook.Add( "EntityRemoved", "player.Invalidation", InvalidatePlayer )