Benny/gamemodes/benny/gamemode/modules/player/sh_checker.lua

182 lines
4.7 KiB
Lua

local easywaycvar = CreateConVar( "benny_net_easyway", 0, FCVAR_ARCHIVE + FCVAR_REPLICATED, "Use a disgusting way of networking inventories for maximum duplication." )
local easyway = easywaycvar:GetBool()
local UINTBITS = 8
if easyway then
if SERVER then
gameevent.Listen( "player_activate" )
hook.Add( "player_activate", "Benny_Activate", function( data )
Player(data.userid).CheckerReady = true
end )
local checkerinterval = 1
util.AddNetworkString( "Benny_Checker" )
hook.Add( "PlayerTick", "Benny_Checker", function( ply )
if ply.CheckerReady then
if (ply.CheckerLast or 0) + checkerinterval <= CurTime() then
local inv = ply:INV_Get()
net.Start( "Benny_Checker" )
net.WriteUInt( table.Count(inv), UINTBITS )
for i, v in pairs( inv ) do
net.WriteString( i )
net.WriteTable( v )
end
print( ply, net.BytesWritten() )
net.Send( ply )
ply.CheckerLast = CurTime()
end
end
end)
else -- client begin
net.Receive( "Benny_Checker", function( len, ply )
local ply = LocalPlayer()
local inv = ply:INV_Get()
local amt = net.ReadUInt( UINTBITS )
for i=1, amt do
local id = net.ReadString()
inv[id] = net.ReadTable()
end
end)
end -- client end
else -- hardway
if SERVER then
gameevent.Listen( "player_activate" )
hook.Add( "player_activate", "Benny_Activate", function( data )
Player(data.userid).CheckerReady = true
end )
local checkerinterval = 1
util.AddNetworkString( "Benny_Checker" )
util.AddNetworkString( "Benny_Checker_CL_Request" )
hook.Add( "PlayerTick", "Benny_Checker", function( ply )
if ply.CheckerReady then
if (ply.CheckerLast or 0) + checkerinterval <= CurTime() then
local inv = ply:INV_Get()
net.Start( "Benny_Checker" )
net.WriteUInt( table.Count(inv), UINTBITS )
for i, v in pairs( inv ) do
net.WriteString( i )
net.WriteBool( v.Loaded )
if v.Loaded then net.WriteString( v.Loaded ) end
end
net.Send( ply )
ply.CheckerLast = CurTime()
end
end
end)
net.Receive("Benny_Checker_CL_Request", function( len, ply )
if (ply.CheckerRequestBan or 0) <= CurTime() then
local amt = net.ReadUInt( UINTBITS )
local inv = ply:INV_Get()
local regenlist = {}
-- Make sure they all exist first
for i=1, amt do
local id = net.ReadString()
if inv[id] then
table.insert( regenlist, id )
else
-- Punish
print( ply, "The item the client requested, '" .. tostring(id) .."' didn't exist. Malicious?") -- Matters in public servers!! -- Not supporting for 5 seconds." )
-- ply.CheckerRequestBan = CurTime() + 5
-- return
end
end
net.Start("Benny_Checker_CL_Request")
net.WriteUInt( #regenlist, UINTBITS )
for i, id in ipairs( regenlist ) do
if inv[id] then
print( "Doing " .. id )
net.WriteString( id )
net.WriteTable( inv[id] )
end
end
net.Send( ply )
end
end)
else
net.Receive( "Benny_Checker", function( len, ply )
local ply = LocalPlayer()
-- Get started
local amt = net.ReadUInt( UINTBITS )
local evallist = {}
for i=1, amt do
local id = net.ReadString()
local loaded_exists = net.ReadBool()
local loaded = nil
if loaded_exists then
loaded = net.ReadString()
end
evallist[id] = loaded or true
end
local inv = ply:INV_Get()
-- Check which items DO NOT exist
local missinglist = {}
for i, v in pairs( evallist ) do
if inv[i] then
-- Success
if isstring(v) then
inv[i].Loaded = v
end
else
missinglist[i] = true
end
end
-- Check any ghost items we have
local ghostlist = {}
for i, v in pairs( inv ) do
if evallist[i] then
-- Success
else
ghostlist[i] = true
end
end
-- Regenerate missing items
if table.Count(missinglist) > 0 then
local concat = ""
for i, v in pairs( missinglist ) do
concat = concat .. "'" .. i .. "' "
end
print( "[".. string.FormattedTime( CurTime(), "%02i:%02i") .. "] [Checker]: You are missing items " .. concat )
net.Start( "Benny_Checker_CL_Request" )
net.WriteUInt( table.Count( missinglist ), UINTBITS )
for i, v in pairs( missinglist ) do
net.WriteString( i )
end
net.SendToServer()
end
-- Remove ghost items
for i, v in pairs( ghostlist ) do
inv[i] = nil
print( "[".. string.FormattedTime( CurTime(), "%02i:%02i") .. "] [Checker]: Removed a ghost item with ID '" .. i .. "'" )
end
end )
net.Receive( "Benny_Checker_CL_Request", function( len, ply )
local ply = LocalPlayer()
local inv = ply:INV_Get()
local amt = net.ReadUInt( UINTBITS )
for i=1, amt do
local id = net.ReadString()
print( "[".. string.FormattedTime( CurTime(), "%02i:%02i") .. "] [Checker]: Restoring " .. id )
inv[id] = net.ReadTable()
end
end)
end
end -- hardway