-
Notifications
You must be signed in to change notification settings - Fork 0
/
easylua.lua
500 lines (389 loc) · 9.87 KB
/
easylua.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
easylua = {} local s = easylua
local function compare(a, b)
if a == b then return true end
if a:find(b, nil, true) then return true end
if a:lower() == b:lower() then return true end
if a:lower():find(b:lower(), nil, true) then return true end
return false
end
local function compareentity(ent, str)
if ent.GetName and compare(ent:GetName(), str) then
return true
end
if ent:GetModel() and compare(ent:GetModel(), str) then
return true
end
return false
end
if CLIENT then
function easylua.PrintOnServer(...)
local args = {...}
local new = {}
for key, value in pairs(args) do
table.insert(new, luadata.ToString(value) or tostring(value))
end
RunConsoleCommand("easylua_print", unpack(new))
end
end
function easylua.Print(...)
if CLIENT then
easylua.PrintOnServer(...)
end
if SERVER then
local args = {...}
local str = ""
Msg(string.format("[EasyLua %s] ", IsValid(me) and me:Nick() or "Sv"))
for key, value in pairs(args) do
str = str .. type(value) == "string" and value or luadata.ToString(value) or tostring(value)
if key ~= #args then
str = str .. ","
end
end
print(str)
end
end
if SERVER then
function easylua.CMDPrint(ply, cmd, args)
args = table.concat(args, ", ")
Msg(string.format("[EasyLua %s] ", IsValid(ply) and ply:Nick() or "Sv"))
print(args)
end
concommand.Add("easylua_print", easylua.CMDPrint)
end
function easylua.FindEntity(str)
if not str then return NULL end
str = tostring(str)
if str == "#this" and IsEntity(this) and this:IsValid() then
return this
end
if str == "#me" and IsEntity(me) and me:IsPlayer() then
return me
end
if str == "#all" then
return all
end
if str:sub(1,1) == "#" then
local str = str:sub(2)
if #str > 0 then
str = str:lower()
local found
for key, data in pairs(team.GetAllTeams()) do
if data.Name:lower() == str then
found = data.Name:lower()
print("found!!!")
break
end
end
if not found then
local classes = {}
for key, ent in pairs(ents.GetAll()) do
classes[ent:GetClass():lower()] = true
end
for class in pairs(classes) do
if class:lower() == str then
print("found", class)
found = class
end
end
end
if found then
local func = CreateAllFuncton(function(v) return v:GetClass():lower() == class end)
print(func:GetName())
return func
end
end
end
-- unique id
local ply = player.GetByUniqueID(str)
if ply and ply:IsPlayer() then
return ply
end
-- steam id
if str:find("STEAM") then
for key, _ply in pairs(player.GetAll()) do
if _ply:SteamID() == str then
return _ply
end
end
end
if str:sub(1,1) == "_" and tonumber(str:sub(2)) then
str = str:sub(2)
end
if tonumber(str) then
ply = Entity(tonumber(str))
if ply:IsValid() then
return ply
end
end
-- community id
if #str == 17 then
end
-- ip
if SERVER then
if str:find("%d+%.%d+%.%d+%.%d+") then
for key, _ply in pairs(player.GetAll()) do
if _ply:IPAddress():find(str) then
return _ply
end
end
end
end
-- Search again with colorcode stripped
for key, ply in pairs(player.GetAll()) do
if compare(ply:Nick(), str) then
return ply
end
if compare(ply:Nick():gsub("%^%d", ""), str) then
return ply
end
end
for key, ent in pairs(ents.GetAll()) do
if compareentity(ent, str) then
return ent
end
end
do -- class
local _str, idx = str:match("(.-)(%d+)")
if idx then
idx = tonumber(idx)
str = _str
else
str = str
idx = (me and me.easylua_iterator) or 0
end
local found = {}
for key, ent in pairs(ents.GetAll()) do
if compare(ent:GetClass(), str) then
table.insert(found, ent)
end
end
return found[math.Clamp(idx%#found, 1, #found)] or NULL
end
end
function easylua.CreateEntity(class)
local mdl = "error.mdl"
if IsEntity(class) and class:IsValid() then
this = class
elseif class:find(".mdl", nil, true) then
mdl = class
class = "prop_physics"
this = ents.Create(class)
this:SetModel(mdl)
else
this = ents.Create(class)
end
this:Spawn()
this:SetPos(there + Vector(0,0,this:BoundingRadius() * 2))
this:DropToFloor()
this:PhysWake()
undo.Create(class)
undo.SetPlayer(me)
undo.AddEntity(this)
undo.Finish()
return this
end
function easylua.CopyToClipboard(var, ply)
ply = ply or me
if luadata then
local str = luadata.ToString(var)
if not str and IsEntity(var) and var:IsValid() then
if var:IsPlayer() then
str = string.format("player.GetByUniqueID(--[[%s]] %q)", var:GetName(), var:UniqueID())
else
str = string.format("Entity(%i)", var:EntIndex())
end
end
if CLIENT then
SetClipboardText(str)
end
if SERVER then
local str = string.format("SetClipboardText(%q)", str)
if #str > 255 then
if luadev and luadev.RunOnClient then
luadev.RunOnClient(str, ply)
else
error("Text too long to send and luadev not found",1)
end
else
ply:SendLua(str)
end
end
end
end
function easylua.Start(ply)
ply = ply or CLIENT and LocalPlayer() or nil
if not ply or not IsValid(ply) then return end
local vars = {}
local trace = util.QuickTrace(ply:EyePos(), ply:GetAimVector() * 10000, {ply, ply:GetVehicle()})
if trace.Entity:IsWorld() then
trace.Entity = NULL
end
vars.me = ply
vars.this = trace.Entity
vars.wep = ply:GetActiveWeapon()
vars.there = trace.HitPos
vars.here = trace.StartPos
vars.dir = ply:GetAimVector()
vars.trace = trace
vars.length = trace.StartPos:Distance(trace.HitPos)
vars.copy = s.CopyToClipboard
vars.create = s.CreateEntity
vars.prints = s.PrintOnServer
if vars.this:IsValid() then
vars.phys = vars.this:GetPhysicsObject()
vars.model = vars.this:GetModel()
end
vars.E = s.FindEntity
vars.last = ply.easylua_lastvars
s.vars = vars
local old_G={}
s.oldvars=old_G
for k,v in pairs(vars) do old_G[k]=_G[k] _G[k] = v end
ply.easylua_lastvars = vars
ply.easylua_iterator = (ply.easylua_iterator or 0) + 1
end
function easylua.End()
if s.vars then
for key, value in pairs(s.vars) do
if s.oldvars and s.oldvars[key] then
_G[key] = s.oldvars[key]
else
_G[key] = nil
end
end
end
end
do -- env meta
local META = {}
local _G = _G
local easylua = easylua
local tonumber = tonumber
local nils={
["CLIENT"]=true,
["SERVER"]=true,
}
function META:__index(key)
local var = _G[key]
if var ~= nil then
return var
end
if not nils [key] then -- uh oh
var = easylua.FindEntity(key)
if var:IsValid() then
return var
end
end
return nil
end
function META:__newindex(key, value)
_G[key] = value
end
easylua.EnvMeta = setmetatable({}, META)
end
function easylua.RunLua(ply, code, env_name)
if not ply:GetNWBool("glua_access",false) then return end
local data =
{
error = false,
args = {},
}
easylua.Start(ply)
local header = ""
for key, value in pairs(s.vars) do
header = header .. string.format("local %s = %s ", key, key)
end
code = header .. "; " .. code
env_name = env_name or string.format("%s", tostring(
IsValid(ply) and ply:IsPlayer()
and "["..ply:SteamID():gsub("STEAM_","").."]"..ply:Name()
or ply))
data.env_name = env_name
local func = CompileString(code, env_name, false)
if type(func) == "function" then
setfenv(func, easylua.EnvMeta)
local args = {pcall(func)}
if args[1] == false then
data.error = args[2]
end
table.remove(args, 1)
data.args = args
else
data.error = func
end
easylua.End()
return data
end
function easylua.StartWeapon(classname)
_G.SWEP = {Primary = {}, Secondary = {}}
SWEP.Base = "weapon_cs_base"
SWEP.ClassName = classname or "no_swep_name_" .. me:Nick() .. "_" .. me:UniqueID()
end
function easylua.EndWeapon(spawn, reinit)
weapons.Register(SWEP, SWEP.ClassName, true)
for key, entity in pairs(ents.FindByClass(SWEP.ClassName)) do
if entity:GetTable() then table.Merge(entity:GetTable(), SWEP) end
if reinit then
entity:Initialize()
end
end
if SERVER and spawn then
SafeRemoveEntity(me:GetWeapon(SWEP.ClassName))
local me = me
local class = SWEP.ClassName
timer.Simple(0.2, function() if me:IsPlayer() then me:Give(class) end end)
end
SWEP = nil
end
function easylua.StartEntity(classname)
_G.ENT = {}
ENT.Type = "anim"
ENT.Base = "base_anim"
ENT.Model = Model("models/props_borealis/bluebarrel001.mdl")
ENT.ClassName = classname or "no_ent_name_" .. me:Nick() .. "_" .. me:UniqueID()
end
function easylua.EndEntity(spawn, reinit)
scripted_ents.Register(ENT, ENT.ClassName, true)
for key, entity in pairs(ents.FindByClass(ENT.ClassName)) do
table.Merge(entity:GetTable(), ENT)
if reinit then
entity:Initialize()
end
end
if SERVER and spawn then
create(ENT.ClassName)
end
ENT = nil
end
do -- all
local META = {}
function META:__index(key)
return function(_, ...)
local args = {}
//for _, ent in pairs(ents.GetAll()) do
// if (not self.func or self.func(ent)) then
// if type(ent[key]) == "function" or ent[key] == "table" and type(ent[key].__call) == "function" and getmetatable(ent[key]) then
// table.insert(args, {ent = ent, args = (ent[key](ent, ...))})
// else
// ErrorNoHalt("attempt to call field '" .. key .. "' on ".. tostring(ent) .." a " .. type(ent[key]) .. " value\n")
// end
// end
//end
return args
end
end
function META:__newindex(key, value)
for _, ent in pairs(ents.GetAll()) do
if not self.func or self.func(ent) then
ent[key] = value
end
end
end
function CreateAllFuncton(func)
return setmetatable({func = func}, META)
end
all = CreateAllFuncton(function(v) return v:IsPlayer() end)
props = CreateAllFuncton(function(v) return v:GetClass() == "prop_physics" end)
props = CreateAllFuncton(function(v) return util.IsValidPhysicsObject(vm) end)
bots = CreateAllFuncton(function(v) return v:IsBot() end)
these = CreateAllFuncton(function(v) return easylua and table.HasValue(constraint.GetAllConstrainedEntities(this), v) end)
end