-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1_meta.lua
More file actions
208 lines (175 loc) · 5.43 KB
/
1_meta.lua
File metadata and controls
208 lines (175 loc) · 5.43 KB
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
207
208
do
local getmetatable = getmetatable
do
local meta = debug.getmetatable("")
function isstring(val) return val and getmetatable(val) == meta end
end
do
local _type = 0
local meta = debug.getmetatable(_type)
if not meta then
meta = { MetaName = "number", MetaID = TypeID(_type) }
debug.setmetatable(_type, meta)
end
function isnumber(val) return val and getmetatable(val) == meta end
end
do
local _type = function() end
local meta = debug.getmetatable(_type)
if not meta then
meta = { MetaName = "function", MetaID = TypeID(_type) }
debug.setmetatable(_type, meta)
end
function isfunction(val) return val and getmetatable(val) == meta end
end
do
local _type = true
local meta = debug.getmetatable(_type)
if not meta then
meta = { MetaName = "boolean", MetaID = TypeID(_type) }
debug.setmetatable(_type, meta)
end
function isbool(val) return val and getmetatable(val) == meta end
end
do
local _type = coroutine.create(function() end)
local meta = debug.getmetatable(_type)
if not meta then
meta = { MetaName = "thread", MetaID = TypeID(_type) }
debug.setmetatable(_type, meta)
end
function iscoroutine(val) return val and getmetatable(val) == meta end
end
for k, v in pairs({
["Vector"] = "vector", ["Angle"] = "angle",
["VMatrix"] = "matrix", ["Panel"] = "panel"
}) do
local meta = FindMetaTable(k)
_G["is" .. v] = function(val) return val and getmetatable(val) == meta end
end
end
do
-- Unique meta-tables for each
setmetatable(FindMetaTable("Weapon"), {__index = FindMetaTable("Entity")})
setmetatable(FindMetaTable("NPC"), {__index = FindMetaTable("Entity")})
setmetatable(FindMetaTable("Vehicle"), {__index = FindMetaTable("Entity")})
-- Credits: https://github.com/swampservers/contrib/blob/master/lua/swamp/sh_meta.lua
if isfunction(Entity) then
local EntityFunction = Entity
Entity = FindMetaTable("Entity")
setmetatable(Entity, { __call = function(_, x) return EntityFunction(x) end })
end
if isfunction(Player) then
local PlayerFunction = Player
Player = FindMetaTable("Player")
setmetatable(Player, { __call = function(_, x) return PlayerFunction(x) end })
end
local ENT = Entity
do
-- caches the Entity.GetTable so stuff is super fast
CEntityGetTable = CEntityGetTable or ENT.GetTable
local cgettable, rawset = CEntityGetTable, rawset
-- __mode = "kv",
EntityTable = setmetatable({}, {
__mode = "k",
__index = function(self, ent)
local tab = cgettable(ent)
-- extension: perhaps initialize default values in the entity table here?
rawset(self, ent, tab)
return tab
end
})
end
local GetTable, simple = EntityTable, timer.Simple
-- Apparently entities cant be weak keys
hook.Add("EntityRemoved", "CleanupEntityTableCache", function(ent)
simple(0, function() GetTable[ent] = nil end)
end)
function ENT:GetTable() return GetTable[self] end
do
local rawequal = rawequal
function ENT.__eq(a, b) return rawequal(a, b) end
end
local dt = {}
do
local PLY = Player
function PLY:__index(key)
local val = PLY[key]
if val ~= nil then return val end
val = ENT[key]
if val ~= nil then return val end
return (GetTable[self] or dt)[key]
end
end
local GetOwner = ENT.GetOwner
local ownerkey = "Owner"
function ENT:__index(key)
if (key == ownerkey) then return GetOwner(self) end
local val = ENT[key]
if val ~= nil then return val end
return (GetTable[self] or dt)[key]
end
do
local WEP = FindMetaTable("Weapon")
function WEP:__index(key)
if (key == ownerkey) then return GetOwner(self) end
local val = WEP[key]
if val ~= nil then return val end
val = ENT[key]
if val ~= nil then return val end
return (GetTable[self] or dt)[key]
end
end
do
local VEH = FindMetaTable("Vehicle")
function VEH:__index(key)
if (key == ownerkey) then return GetOwner(self) end
local val = VEH[key]
if val ~= nil then return val end
val = ENT[key]
if val ~= nil then return val end
return (GetTable[self] or dt)[key]
end
end
-- do
-- local _R = debug.getregistry()
-- local ENTITY = _R.Entity
-- local entTabs = {
-- ["Entity"] = { __index = ENTITY },
-- ["Player"] = { __index = setmetatable( table.Copy( _R.Player ), { __index = ENTITY } ) },
-- ["Weapon"] = { __index = setmetatable( table.Copy( _R.Weapon ), { __index = ENTITY } ) },
-- ["Vehicle"] = { __index = setmetatable( table.Copy( _R.Vehicle ), { __index = ENTITY } ) },
-- ["NPC"] = { __index = setmetatable( table.Copy( _R.NPC ), { __index = ENTITY } ) }
-- }
-- local ownerkey = "Owner"
-- local copyKeys = {"MetaID", "MetaName", "__tostring", "__eq", "__concat"}
-- local copyKeysLength = #copyKeys
-- local function copyMetatable( ent, entTabName )
-- if (!entTabs[entTabName]) then return end
-- local tab = GetTable[ent]
-- setmetatable(tab, entTabs[entTabName])
-- local mt = {
-- __index = function( self, key )
-- if key == ownerkey then return GetOwner(self, key) end
-- return tab[key]
-- end,
-- __newindex = tab,
-- __metatable = ENTITY
-- }
-- local v
-- for i = 1, copyKeysLength do
-- v = copyKeys[i]
-- mt[v] = ENTITY[v]
-- end
-- v = nil
-- debug.setmetatable(ent, mt)
-- end
-- hook.Add("OnEntityCreated", "turbo.ChangeEntMeta", function(ent)
-- timer.Simple(0, function()
-- if (IsValid(ent)) then
-- copyMetatable(ent, debug.getmetatable(ent).MetaName)
-- end
-- end)
-- end, HOOK_MONITOR_HIGH)
-- end
end