nodecore-cd2025/mods/nc_api/compat_authcache.lua
Aaron Suen a40077f6c0 Hotfix for default_privs not working
- If we fail to get the player name, instead of returning a dummy
  object with no privs, defer to the wrapped function without the
  cache, to preserve the old behavior.
- Clear cache more aggressively when the player joins or leaves.
  This may help ensure that stale cache data doesn't affect things
  for too long, and also allow the cache to be GC'd more on servers
  with more player turnover.
2022-10-07 14:35:43 -04:00

54 lines
1.4 KiB
Lua

-- LUALOCALS < ---------------------------------------------------------
local minetest, type
= minetest, type
-- LUALOCALS > ---------------------------------------------------------
local function player_name(player)
if not player then return end
if type(player) == "string" then return player end
player = player.get_player_name and player:get_player_name()
if type(player) == "string" then return player end
end
local priv_cache = {}
local function invalidateafter(method)
local oldfunc = minetest[method]
minetest[method] = function(player, ...)
local function helper(...)
local name = player_name(player)
if name then priv_cache[name] = nil end
return ...
end
return helper(oldfunc(player, ...))
end
end
invalidateafter("set_privileges")
invalidateafter("remove_player_auth")
local function invalidateon(event)
minetest["register_on_" .. event](function(player)
local name = player_name(player)
if name then priv_cache[name] = nil end
end)
end
invalidateon("joinplayer")
invalidateon("leaveplayer")
local oldreload = minetest.auth_reload
function minetest.auth_reload(...)
priv_cache = {}
return oldreload(...)
end
local oldget = minetest.get_player_privs
function minetest.get_player_privs(player)
local pname = player_name(player)
if not pname then return oldget(player) end
local cached = priv_cache[pname]
if cached then return cached end
cached = oldget(pname)
priv_cache[pname] = cached
return cached
end