CapsAdmin

Untitled

Sep 5th, 2011
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.82 KB | None | 0 0
  1. easylua = {} local s = easylua
  2.  
  3. local function compare(a, b)
  4.  
  5.     if a == b then return true end
  6.     if a:find(b, nil, true) then return true end
  7.     if a:lower() == b:lower() then return true end
  8.     if a:lower():find(b:lower(), nil, true) then return true end
  9.  
  10.     return false
  11. end
  12.  
  13. if CLIENT then
  14.     function easylua.PrintOnServer(...)
  15.         local args = {...}
  16.         local new = {}
  17.  
  18.         for key, value in pairs(args) do
  19.             table.insert(new, luadata.ToString(value))
  20.         end
  21.  
  22.         RunConsoleCommand("easylua_print", unpack(new))
  23.     end
  24. end
  25.  
  26. if SERVER then
  27.     function easylua.CMDPrint(ply, cmd, args)
  28.         args = table.concat(args, ", ")
  29.  
  30.         print(Format("[easylua %s] : %s", ply:Nick(), args))
  31.     end
  32.  
  33.     concommand.Add("easylua_print", easylua.CMDPrint)
  34. end
  35.  
  36. function easylua.FindEntity(str)
  37.     str = tostring(str)
  38.     if not str then return NULL end
  39.  
  40.     -- unique id
  41.     local ply = player.GetByUniqueID(str)
  42.     if ply and ply:IsPlayer() then
  43.         return ply
  44.     end
  45.  
  46.     -- steam id
  47.     if str:find("STEAM") then
  48.         for key, _ply in pairs(player.GetAll()) do
  49.             if _ply:SteamID() == str then
  50.                 return _ply
  51.             end
  52.         end
  53.     end
  54.  
  55.     if tonumber(str) then
  56.         ply = Entity(tonumber(str))
  57.         if ply:IsValid() then
  58.             return ply
  59.         end
  60.     end
  61.  
  62.     -- community id
  63.     if #str == 17 then
  64.  
  65.     end
  66.  
  67.     -- ip
  68.     if str:find("%a+.%a+.%a+.%a+") then
  69.         for key, _ply in pairs(player.GetAll()) do
  70.             if _ply:IPAddress():find(str) then
  71.                 return _ply
  72.             end
  73.         end
  74.     end
  75.  
  76.     for key, ent in pairs(ents.GetAll()) do
  77.  
  78.         if ent.GetName and compare(ent:GetName(), str) then
  79.             return ent
  80.         end
  81.  
  82.         if compare(ent:GetClass(), str) then
  83.             return ent
  84.         end
  85.  
  86.         if key == tonumber(str) then return ent end
  87.         if key == tonumber(str:sub(2)) then return ent end
  88.  
  89.         if ent:GetModel() and compare(ent:GetModel(), str) then
  90.             return ent
  91.         end
  92.     end
  93.  
  94.     return ents.FindByClass(str)[1] or NULL
  95. end
  96.  
  97. function easylua.CreateEntity(class)
  98.     local mdl = "error.mdl"
  99.  
  100.     if IsEntity(class) and class:IsValid() then
  101.         this = class
  102.     elseif class:find(".mdl", nil, true) then
  103.         mdl = class
  104.         class = "prop_physics"
  105.  
  106.         this = ents.Create(class)
  107.         this:SetModel(  )
  108.     else
  109.         this = ents.Create(class)
  110.     end
  111.  
  112.     this:Spawn()
  113.     this:SetPos(there + Vector(0,0,this:BoundingRadius() * 2))
  114.     this:DropToFloor()
  115.     this:PhysWake()
  116.  
  117.     undo.Create(class)
  118.         undo.SetPlayer(me)
  119.         undo.AddEntity(this)
  120.     undo.Finish()
  121. end
  122.  
  123. function easylua.CopyToClipboard(var, ply)
  124.     ply = ply or me
  125.     if luadata then
  126.         local str = luadata.ToString(var)
  127.  
  128.         if not str and IsEntity(var) and var:IsValid() then
  129.             if var:IsPlayer() then
  130.                 str = Format("player.GetByUniqueID(--[[%s]] %q)", var:GetName(), var:UniqueID())
  131.             else
  132.                 str = Format("Entity(%i)", var:EntIndex())
  133.             end
  134.  
  135.         end
  136.  
  137.         if CLIENT then
  138.             SetClipboardText(str)
  139.         end
  140.  
  141.         if SERVER then
  142.             ply:SendLua(Format("SetClipboardText(%q)", str))
  143.         end
  144.     end
  145. end
  146.  
  147. function easylua.Start(ply)
  148.     ply = ply or CLIENT and LocalPlayer() or nil
  149.  
  150.     local vars = {}
  151.         local trace = ply:GetEyeTrace()
  152.  
  153.         if trace.Entity:IsWorld() then
  154.             trace.Entity = NULL
  155.         end
  156.  
  157.         vars.me = ply
  158.         vars.there = trace.HitPos
  159.         vars.here = trace.StartPos
  160.         vars.dir = ply:GetAimVector()
  161.         vars.length = trace.StartPos:Distance(trace.HitPos)
  162.         vars.this = trace.Entity
  163.         vars.trace = trace
  164.         vars.wep = ply:GetActiveWeapon()
  165.         vars.copy = s.CopyToClipboard
  166.         vars.create = s.CreateEntity
  167.  
  168.         if vars.this:IsValid() then
  169.             vars.phys = vars.this:GetPhysicsObject()
  170.             vars.model = vars.this:GetModel()
  171.         end
  172.  
  173.         vars.E = s.FindEntity
  174.         s.vars = vars
  175.     table.Merge(_G, vars)
  176. end
  177.  
  178. function easylua.End()
  179.     if s.vars then
  180.         for key, value in pairs(s.vars) do
  181.             _G[key] = nil
  182.         end
  183.     end
  184. end
  185.  
  186. function easylua.RunLua(ply, code)
  187.     easylua.Start(ply)
  188.  
  189.         local header = ""
  190.  
  191.         for key, value in pairs(s.vars) do
  192.             header = header .. Format("local %s = %s ", key, key)
  193.         end
  194.  
  195.         code = header .. "\n" .. code
  196.  
  197.         local func = CompileString(code, tostring(ply))
  198.         if func then
  199.             local noerr, errormsg = pcall(func)
  200.  
  201.             if noerr == false then
  202.                 ErrorNoHalt("Script from '"..tostring(ply).."' errored: "..tostring(errormsg).."\n")
  203.             end
  204.         end
  205.     easylua.End()
  206. end
  207.  
  208. function easylua.StartWeapon(classname)
  209.     _G.SWEP = {Primary = {}, Secondary = {}}
  210.  
  211.     SWEP.Base = "weapon_cs_base"
  212.  
  213.     SWEP.ClassName = classname or "no_swep_name_" .. me:Nick() .. "_" .. me:UniqueID()
  214. end
  215.  
  216. function easylua.EndWeapon(spawn, reinit)
  217.     weapons.Register(SWEP, SWEP.ClassName, true)
  218.  
  219.     for key, entity in pairs(ents.FindByClass(SWEP.ClassName)) do
  220.         if entity:GetTable() then table.Merge(entity:GetTable(), SWEP) end
  221.         if reinit then
  222.             entity:Initialize()
  223.         end
  224.     end
  225.  
  226.     if SERVER and spawn then
  227.         SafeRemoveEntity(me:GetWeapon(SWEP.ClassName))
  228.         local me = me
  229.         local class = SWEP.ClassName
  230.         timer.Simple(0.2, function() if me:IsPlayer() then me:Give(class) end end)
  231.     end
  232.  
  233.     SWEP = nil
  234. end
  235.  
  236. function easylua.StartEntity(classname)
  237.     _G.ENT = {}
  238.  
  239.     ENT.Type = "anim"
  240.     ENT.Base = "base_anim"
  241.  
  242.     ENT.Model = Model("models/props_borealis/bluebarrel001.mdl")
  243.     ENT.ClassName = classname or "no_ent_name_" .. me:Nick() .. "_" .. me:UniqueID()
  244. end
  245.  
  246. function easylua.EndEntity(spawn, reinit)
  247.     scripted_ents.Register(ENT, ENT.ClassName, true)
  248.  
  249.     for key, entity in pairs(ents.FindByClass(ENT.ClassName)) do
  250.         table.Merge(entity:GetTable(), ENT)
  251.         if reinit then
  252.             entity:Initialize()
  253.         end
  254.     end
  255.  
  256.     if SERVER and spawn then
  257.         create(ENT.ClassName)
  258.     end
  259.  
  260.     ENT = nil
  261. end
  262.  
  263. do -- all
  264.     local META = {}
  265.  
  266.     function META:__index(key)
  267.         return function(_, ...)
  268.             local args = {}
  269.  
  270.             for _, ent in pairs(ents.GetAll()) do
  271.                 if (not self.func or self.func(ent)) then
  272.                     if type(ent[key]) == "function" or ent[key] == "table" and type(ent[key].__call) == "function" and getmetatable(ent[key]) then
  273.                         table.insert(args, {ent = ent, args = (ent[key](ent, ...))})
  274.                     else
  275.                         ErrorNoHalt("attempt to call field '" .. key .. "' on ".. tostring(ent) .." a " .. type(ent[key]) .. " value\n")
  276.                     end
  277.                 end
  278.             end
  279.  
  280.             return args
  281.         end
  282.     end
  283.  
  284.     function META:__newindex(key, value)
  285.         for _, ent in pairs(ents.GetAll()) do
  286.             if not self.func or self.func(ent) then
  287.                 ent[key] = value
  288.             end
  289.         end
  290.     end
  291.  
  292.  
  293.  
  294.     function CreateAllFuncton(func)
  295.         return setmetatable({func = func}, META)
  296.     end
  297.  
  298.     all = CreateAllFuncton(function(v) return v:IsPlayer() end)
  299.     props = CreateAllFuncton(function(v) return v:GetClass() == "prop_physics" end)
  300.     props = CreateAllFuncton(function(v) return util.IsValidPhysicsObject(vm) end)
  301.     bots = CreateAllFuncton(function(v) return v:IsBot() end)
  302.     these = CreateAllFuncton(function(v) return easylua and table.HasValue(constraint.GetAllConstrainedEntities(this), v) end)
  303. end
  304.  
  305. hook.Add("PreLuaDevRun", "easylua_luadev", function(script, info)
  306.     easylua.Start(info.ply)
  307. end)
  308.  
  309. hook.Add("PostLuaDevRun", "easylua_luadev", function(script, info)
  310.     easylua.End()
  311. end)
Advertisement
Add Comment
Please, Sign In to add comment