Permalink
Browse files

Cleaned more files and fixes for cookie library

cookie library - Buffered Deletion of items, doing cookie.Set( key, nil
) will now delete the item instead of doing nothing
  • Loading branch information...
1 parent ee0ce4f commit af34e33f75f89590eb4ff597a29ccc9bef5fcc70 @robotboy655 robotboy655 committed Aug 14, 2016
@@ -154,7 +154,7 @@ if ( CLIENT ) then
local TOOLS_LIST = SWEP.Tool
-- Add the STOOLS to the tool menu
- local function AddSToolsToMenu()
+ hook.Add( "PopulateToolMenu", "AddSToolsToMenu", function()
for ToolName, TOOL in pairs( TOOLS_LIST ) do
@@ -172,9 +172,7 @@ if ( CLIENT ) then
end
- end
-
- hook.Add( "PopulateToolMenu", "AddSToolsToMenu", AddSToolsToMenu )
+ end )
--
-- Search
@@ -47,8 +47,8 @@ if ( SERVER ) then
LastDupeArm = CurTime() + 1
- local len = net.ReadUInt( 32 )
- local data = net.ReadData( len )
+ local len = net.ReadUInt( 32 )
+ local data = net.ReadData( len )
if ( !IsValid( client ) ) then return end
@@ -207,7 +207,6 @@ function meta:InstallDataTable()
SetFunc = SetFunc,
GetFunc = GetFunc,
typename = typename,
- keyname = keyname,
Notify = {}
}
@@ -243,9 +242,7 @@ function meta:InstallDataTable()
KeyName = keyname,
Set = setfunc,
Get = getfunc,
- Type = type,
- EditType = EditType,
- EditData = EditData
+ Type = type
}
if ( other_data ) then
@@ -279,6 +276,8 @@ function meta:InstallDataTable()
if ( !other_data ) then return end
+ -- This KeyName stuff is absolutely unnecessary, there's absolutely no reason for it to exist
+ -- But we cannot remove it now because dupes will break. It should've used the "name" variable
if ( other_data.KeyName ) then
ent:SetupKeyValue( other_data.KeyName, typename, ent[ "Set" .. name ], ent[ "Get" .. name ], other_data )
ent:SetupEditing( name, other_data.KeyName, other_data.Edit )
@@ -318,6 +317,8 @@ function meta:InstallDataTable()
if ( !other_data ) then return end
+ -- This KeyName stuff is absolutely unnecessary, there's absolutely no reason for it to exist
+ -- But we cannot remove it now because dupes will break. It should've used the "name" variable
if ( other_data.KeyName ) then
ent:SetupKeyValue( other_data.KeyName, "float", ent[ "Set" .. name ], ent[ "Get" .. name ], other_data )
ent:SetupEditing( name, other_data.KeyName, other_data.Edit )
@@ -1,55 +1,42 @@
- --=====================================================--
--- ___ ___ _ _ _ __ _ ___ ___ __ __ --
--- |_ _|| __| / \ | \_/ | / _| / \ | o \ o \\ V / --
--- | | | _| | o || \_/ | ( |_n| o || / / \ / --
--- |_| |___||_n_||_| |_| \__/|_n_||_|\\_|\\ |_| 2012 --
--- --
- --=====================================================--
-
--[[---------------------------------------------------------
Non-Module includes
-----------------------------------------------------------]]
-
-include ( "util.lua" )
-include ( "util/sql.lua" ) -- Include sql here so it's
- -- available at loadtime to modules.
+include( "util.lua" )
+include( "util/sql.lua" ) -- Include sql here so it's available at loadtime to modules.
--[[---------------------------------------------------------
Modules
-----------------------------------------------------------]]
-
-require ( "concommand" )
-require ( "list" )
-require ( "hook" )
-require ( "draw" )
-require ( "http" )
-require ( "cvars" )
-require ( "cookie" )
-require ( "baseclass" )
-
+require( "concommand" )
+require( "list" )
+require( "hook" )
+require( "draw" )
+require( "http" )
+require( "cvars" )
+require( "cookie" )
+require( "baseclass" )
--[[---------------------------------------------------------
Extensions
-
+
Load extensions that we specifically need for the menu,
- to reduce the chances of loading something that might
+ to reduce the chances of loading something that might
cause errors.
-----------------------------------------------------------]]
-
-include ( "extensions/string.lua" )
-include ( "extensions/table.lua" )
-include ( "extensions/math.lua" )
-include ( "extensions/client/panel.lua" )
-include ( "extensions/util.lua" )
-include ( "extensions/file.lua" )
-include ( "extensions/debug.lua" )
-include ( "extensions/client/render.lua" )
+include( "extensions/string.lua" )
+include( "extensions/table.lua" )
+include( "extensions/math.lua" )
+include( "extensions/client/panel.lua" )
+include( "extensions/util.lua" )
+include( "extensions/file.lua" )
+include( "extensions/debug.lua" )
+include( "extensions/client/render.lua" )
include( "util/vgui_showlayout.lua" )
include( "util/workshop_files.lua" )
include( "util/javascript_util.lua" )
include( "util/tooltips.lua" )
-require ( "notification" )
+require( "notification" )
@@ -9,45 +9,47 @@ module( "cookie", package.seeall )
local CachedEntries = {}
local BufferedWrites = {}
+local BufferedDeletes = {}
local function GetCache( key )
+ if ( BufferedDeletes[ key ] ) then return nil end
+
local entry = CachedEntries[ key ]
-
- if entry == nil || SysTime() > entry[ 1 ] then
+
+ if ( entry == nil || SysTime() > entry[ 1 ] ) then
local name = SQLStr( key )
local val = sql.QueryValue( "SELECT value FROM cookies WHERE key = " .. name )
-
+
if !val then
return false
end
-
+
CachedEntries[ key ] = { SysTime() + 30, val }
end
-
- return CachedEntries[ key ][ 2 ]
-end
-local function FlushCacheEntry( key )
- CachedEntries[ key ] = nil
- BufferedWrites[ key ] = nil
+ return CachedEntries[ key ][ 2 ]
end
local function FlushCache()
CachedEntries = {}
BufferedWrites = {}
+ BufferedDeletes = {}
end
local function CommitToSQLite()
sql.Begin()
-
- for k,v in pairs( BufferedWrites ) do
- local name = SQLStr( k )
- local value = SQLStr( v )
- sql.Query( "INSERT OR REPLACE INTO cookies ( key, value ) VALUES ( " .. name .. ", " .. value .. " )" )
+ for k, v in pairs( BufferedWrites ) do
+ sql.Query( "INSERT OR REPLACE INTO cookies ( key, value ) VALUES ( " .. SQLStr( k ) .. ", " .. SQLStr( v ) .. " )" )
+ end
+
+ for k, v in pairs( BufferedDeletes ) do
+ sql.Query( "DELETE FROM cookies WHERE key = " .. SQLStr( k ) )
end
-
+
BufferedWrites = {}
+ BufferedDeletes = {}
+
sql.Commit()
end
@@ -56,70 +58,59 @@ local function ScheduleCommit()
end
local function SetCache( key, value )
+ if ( value == nil ) then return Delete( key ) end
+
if !CachedEntries[ key ] then
CachedEntries[ key ] = { SysTime() + 30, value }
end
-
+
CachedEntries[ key ][ 2 ] = value
BufferedWrites[ key ] = value
ScheduleCommit()
end
---[[---------------------------------------------------------
- Get a String Value
------------------------------------------------------------]]
+-- Get a String Value
function GetString( name, default )
local val = GetCache( name )
if ( !val ) then return default end
-
+
return val
end
-
---[[---------------------------------------------------------
- Get a Number Value
------------------------------------------------------------]]
+-- Get a Number Value
function GetNumber( name, default )
local val = GetCache( name )
if ( !val ) then return default end
-
+
return tonumber( val )
end
---[[---------------------------------------------------------
- Delete a Value
------------------------------------------------------------]]
+-- Delete a Value
function Delete( name )
- FlushCacheEntry( name )
-
- name = SQLStr( name )
- sql.Query( "DELETE FROM cookies WHERE key = " .. name )
+ CachedEntries[ name ] = nil
+ BufferedWrites[ name ] = nil
+ BufferedDeletes[ name ] = true
+
+ ScheduleCommit()
end
---[[---------------------------------------------------------
- Set a Value
------------------------------------------------------------]]
+-- Set a Value
function Set( name, value )
SetCache( name, value )
end
if ( !CLIENT_DLL ) then return end
---[[---------------------------------------------------------
- ClearCookies
------------------------------------------------------------]]
-local function ClearCookies( ply, command, arguments )
-
+concommand.Add( "lua_cookieclear", function( ply, command, arguments )
+
sql.Query( "DELETE FROM cookies" )
FlushCache()
-end
-
-concommand.Add( "lua_cookieclear", ClearCookies )
+end )

0 comments on commit af34e33

Please sign in to comment.