-
Notifications
You must be signed in to change notification settings - Fork 11
/
cookie.lua
206 lines (165 loc) · 6.12 KB
/
cookie.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
-- incredible-gmod.ru
-- https://github.com/Be1zebub/Small-GLua-Things/blob/master/cookies.lua
-- version without globals and metafunctions, for those who do not want the library to add values to _G
-- Same as PData, but without stupid as fuck 'Unique' ID collisions & with caching
-- also its optimizated & have non-meta functions that allow you to change data of offline players.
-- Also, since September 9 2021, lib has a networking functionality. you dont need netvars for simple things anymore.
-- global version: https://github.com/Be1zebub/Small-GLua-Things/blob/master/sh_player_cookies.lua
-- usage example: https://github.com/Be1zebub/Small-GLua-Things/blob/master/sh_player_cookies_debug.lua
local PLAYER, ENTITY = FindMetaTable("Player"), FindMetaTable("Entity")
local SteamID64 = PLAYER.SteamID64
local CookieCache = {}
local function GetCookies(ply)
if ply then
return CookieCache[SteamID64(ply)] or {}
else
return CookieCache
end
end
if SERVER then
util.AddNetworkString("incredible-gmod.ru/cookies")
local SQLStr, sql, hook, pairs, sql_QueryValue, sql_Query, string_find, EntIndex = SQLStr, sql, hook, pairs, sql.QueryValue, sql.Query, string.find, ENTITY.EntIndex
local net_Start, net_WriteString, net_WriteType, net_WriteBool, net_WriteUInt, net_Send, net_Broadcast = net.Start, net.WriteString, net.WriteType, net.WriteBool, net.WriteUInt, net.Send, net.Broadcast
local empty_func = function(a) return a end
local function NWCookie(ply, key, val, global) -- internal
net_Start("incredible-gmod.ru/cookies")
net_WriteString(key)
net_WriteType(val)
if global then
net_WriteBool(true)
net_WriteUInt(EntIndex(ply), 7)
net_Broadcast()
else
net_Send(ply)
end
end
local function SetCookie(sid, key, value)
CookieCache[sid][key] = value
return sql_Query("REPLACE INTO `incredible-gmod.ru/cookies` (`SteamID`, `Value`, `Key`) VALUES (".. SQLStr(sid) ..", ".. SQLStr(value) ..", ".. SQLStr(key) ..");") ~= false
end
local temp
local function GetCookie(sid, key, default)
temp = CookieCache[sid][key]
if temp then
return temp
end
temp = sql_QueryValue("SELECT `Value` FROM `incredible-gmod.ru/cookies` WHERE `SteamID` = ".. SQLStr(sid) .." AND `Key` = ".. SQLStr(key) .." LIMIT 1;")
return temp or default
end
local function DeleteCookie(sid, key)
CookieCache[sid][key] = nil
return sql_Query("DELETE FROM `incredible-gmod.ru/cookies` WHERE `SteamID` = ".. SQLStr(sid) .. " AND `Key` = ".. SQLStr(key) ..";") ~= false
end
local function ClearCookie(key)
for k, v in pairs(CookieCache) do
CookieCache[k][key] = nil
end
return sql_Query("DELETE FROM `incredible-gmod.ru/cookies` WHERE `Key` = ".. SQLStr(key) ..";") ~= false
end
local function FindCookie(sid, cback, needle, startPos, noPatterns)
if CookieCache[sid] == nil then return end
for key, val in pairs(CookieCache[sid]) do
if string_find(key, needle, startPos, noPatterns) then
cback(key, val)
end
end
end
local SetCookie, GetCookie, DeleteCookie = SetCookie, GetCookie, DeleteCookie
local function player_SetCookie(self, key, value, nw, global)
if nw then
NWCookie(self, key, value, global)
end
return SetCookie(SteamID64(self), key, value)
end
local function player_GetCookie(self, key, default)
return GetCookie(SteamID64(self), key, default)
end
local function player_DeleteCookie(self, key, nw, global)
if nw then
NWCookie(self, key, nil)
end
return DeleteCookie(SteamID64(self), key)
end
local function player_NWCookie(self, key, default, global, totype)
totype = totype or empty_func
NWCookie(self, key, totype(self:GetCookie(key, default)), global)
end
local function player_FindCookie(self, cback, needle, startPos, noPatterns)
FindCookie(SteamID64(self), cback, needle, startPos, noPatterns)
end
hook.Add("PlayerInitialSpawn", "incredible-gmod.ru/cookies/load", function(ply)
local sid = SteamID64(ply)
CookieCache[sid] = {}
local data = sql_Query("SELECT * FROM `incredible-gmod.ru/cookies` WHERE `SteamID` = ".. SQLStr(sid) ..";")
if not data then
return hook.Run("incredible-gmod.ru/cookies/loaded", ply)
end
local cache = CookieCache[sid]
for k, v in pairs(data) do
cache[v.Key] = v.Value
end
hook.Run("incredible-gmod.ru/cookies/loaded", ply)
end)
hook.Add("InitPostEntity", "incredible-gmod.ru/cookies/createdb", function()
sql_Query("CREATE TABLE IF NOT EXISTS `incredible-gmod.ru/cookies` (`SteamID` TEXT, `Key` TEXT NOT NULL, `Value` TEXT);")
end)
return {
GetCookies = GetCookies,
SetCookie = SetCookie,
GetCookie = GetCookie,
DeleteCookie = DeleteCookie,
ClearCookie = ClearCookie,
FindCookie = FindCookie,
Player = {
SetCookie = player_SetCookie,
GetCookie = player_GetCookie,
DeleteCookie = player_DeleteCookie,
NWCookie = player_NWCookie,
FindCookie = player_FindCookie
},
MakeMeta = function()
PLAYER.SetCookie = player_SetCookie
PLAYER.GetCookie = player_GetCookie
PLAYER.DeleteCookie = player_DeleteCookie
PLAYER.NWCookie = player_NWCookie
PLAYER.FindCookie = player_FindCookie
end
}
else
local net_ReadString, net_ReadType, net_ReadBool, net_ReadUInt, LocalPlayer, IsValid, Entity = net.ReadString, net.ReadType, net.ReadBool, net.ReadUInt, LocalPlayer, IsValid, Entity
local localSID, localPlayer
local k, v, ply, sid
net.Receive("incredible-gmod.ru/cookies", function()
k, v = net_ReadString(), net_ReadType()
if net_ReadBool() then
ply = Entity(net_ReadUInt(7))
if IsValid(ply) == false then return end
sid = SteamID64(ply)
CookieCache[sid] = CookieCache[sid] or {}
CookieCache[sid][k] = v
else
if localSID == nil then
localPlayer = LocalPlayer()
localSID = SteamID64(localPlayer)
end
CookieCache[localSID] = CookieCache[localSID] or {}
CookieCache[localSID][k] = v
end
end)
local function GetCookie(ply, key, default)
return GetCookies(ply or localPlayer)[key] or default
end
local function player_GetCookie(key, default)
return GetCookie(self, key, default)
end
return {
GetCookies = GetCookies,
GetCookie = GetCookie,
Player = {
GetCookie = player_GetCookie
},
MakeMeta = function()
PLAYER.GetCookie = player_GetCookie
end
}
end