• Print

Author Topic: Need help improving code to parse names in a ulx luarun type environment.  (Read 4352 times)

0 Members and 1 Guest are viewing this topic.

Offline LuaTenshi

  • Hero Member
  • *****
  • Posts: 545
  • Karma: 47
  • Just your ordinary moon angel!
    • Mirai.Red
My previous posts regarding this: 6987 | 6943

The idea behind this code is to make a parser like on Metastruct and GCinema.

Gmatch Method
Code: Lua
  1. /*-------------------------------------------------------------------------------------------------------------------------
  2. |       ULX ExLua for ULX SVN/ULib SVN by LuaTenshi
  3. |       Steam: [url]http://steamcommunity.com/profiles/76561198096713277[/url]
  4. |       Email: luatenshi@gmail.com
  5. -------------------------------------------------------------------------------------------------------------------------*/
  6.  
  7. function ulx.exlua( calling_ply, str )
  8.         local tab, out, s, p, err = {}, "", ""
  9.         local ply = calling_ply
  10.        
  11.         if string.Trim(str) == "" or string.Trim(str) == "Lua Code" then ULib.tsayError( ply, "ExLua:1: Please enter some code to run." ) return end
  12.         if string.len(str) <= 3 then ULib.tsayError( ply, "ExLua:1: The string you entered is too short." ) return end
  13.        
  14.         for v in string.gmatch(str, "%S+:") do v = string.Replace(v,":","") table.insert(tab, v) end
  15.        
  16.         for k,v in pairs(tab) do
  17.                 s = tostring(tab[k])
  18.                 p,err = ULib.getUser(s,true,ply)
  19.                 if p and not err then
  20.                         out = string.Replace(str, s, "Entity(" .. tostring(p:EntIndex()) .. ")")
  21.                 elseif p and err then
  22.                         ULib.tsayError( ply, err )
  23.                 end
  24.         end
  25.        
  26.         local func = CompileString( out, "ExLua", false )
  27.        
  28.         if type(func) == "function" and func and not err then
  29.                 if Entity(p:EntIndex()) and IsValid(Entity(p:EntIndex())) then
  30.                         local succ, err = pcall(func)
  31.                         if err then ULib.tsayError( ply, err ) return end
  32.                         if succ then
  33.                                 ulx.fancyLogAdmin( calling_ply, true, "#A ran some code." )
  34.                         else
  35.                                 ulx.fancyLogAdmin( calling_ply, true, "#A attempted to run code." )
  36.                                 ULib.tsayError( ply, "ExLua:1: pcall returned false." )
  37.                         end
  38.                 else
  39.                         ULib.tsayError( ply, "ExLua:1: The target entity has vanished." )
  40.                 end
  41.         elseif func then
  42.                 ULib.tsayError( ply, tostring(func) )
  43.         else
  44.                 ULib.tsayError( ply, "ExLua:1: No callback found. This should not happen." )
  45.         end
  46. end
  47. local exlua = ulx.command( "Extra Utility", "ulx exlua", ulx.exlua, "!l" )
  48. exlua:addParam{ type=ULib.cmds.StringArg, hint="Lua Code", ULib.cmds.takeRestOfLine }
  49. exlua:defaultAccess( ULib.ACCESS_SUPERADMIN )
  50. exlua:help( [[Run a lua script on the server.
  51.  
  52. Excepts player arguments such as a players name or the ULX keywords.
  53.  
  54. Accepted Keywords: ^, @, $10 ]] )
  55.  

Setfenv Method
Code: Lua
  1. /*-------------------------------------------------------------------------------------------------------------------------
  2. |       ULX ExLua for ULX SVN/ULib SVN by LuaTenshi
  3. |       Steam: [url]http://steamcommunity.com/profiles/76561198096713277[/url]
  4. |       Email: luatenshi@gmail.com
  5. -------------------------------------------------------------------------------------------------------------------------*/
  6.  
  7. local ExLua = {}
  8. table.Inherit(ExLua, _G) -- Make a local global table.
  9.  
  10. ExLua.__index = function(t, k)
  11.         local tr = ULib.getUser(tostring(k),true,nil)
  12.         if tr then
  13.                 return tr
  14.         else
  15.                 return nil
  16.         end
  17. end; setmetatable(ExLua, ExLua)
  18.  
  19. function ulx.exlua( calling_ply, str )
  20.         if string.Trim(str) == "" or string.Trim(str) == "Lua Code" then ULib.tsayError( ply, "ExLua:1: Please enter some code to run." ) return end
  21.         if string.len(str) <= 3 then ULib.tsayError( ply, "ExLua:1: The string you entered is too short." ) return end
  22.  
  23.         local func = CompileString( str, "ExLua", false )
  24.  
  25.         if func and type(func) == "function" then
  26.                 func = setfenv(func, ExLua) -- Push our function into the environment.
  27.                 if not err then
  28.                         local succ, err = pcall(func)
  29.                         if err then ULib.tsayError( ply, err ) return end
  30.                         if succ then
  31.                                 ulx.fancyLogAdmin( calling_ply, true, "#A ran some code." )
  32.                         else
  33.                                 ulx.fancyLogAdmin( calling_ply, true, "#A attempted to run code." )
  34.                                 ULib.tsayError( ply, "ExLua:1: pcall returned false." )
  35.                         end
  36.                 else
  37.                         ULib.tsayError( ply, "ExLua:1: No callback found. This should not happen." )
  38.                 end
  39.         elseif func then
  40.                 ULib.tsayError( ply, tostring(func) )
  41.         end
  42. end
  43. local exlua = ulx.command( "Extra Utility", "ulx exlua", ulx.exlua, "#>" )
  44. exlua:addParam{ type=ULib.cmds.StringArg, hint="Lua Code", ULib.cmds.takeRestOfLine }
  45. exlua:defaultAccess( ULib.ACCESS_SUPERADMIN )
  46. exlua:help( [[Run a lua script on the server.
  47.  
  48. Excepts player arguments such as a players name or the ULX keywords.
  49.  
  50. Accepted Keywords: ^, @, $10 ]] )
  51.  
  52. -- _G.__index = function(t, k) local tr = ULib.getUser(tostring(k),true,nil) if tr then return tr else return nil end end setmetatable(_G, _G)
  53.  

Steam: http://steamcommunity.com/profiles/76561198096713277
Email: luatenshi@gmail.com

Now only if I could get a parser to target multiple players like... {Alpha, Beta, Bravo, Charlie}:SetHealth(200) or player.GetAll():SetHealth(200)



My Questions:
  • What method is better?
  • Do you guys think that this is a good idea?
  • Are there any mistakes that I should be aware of?
  • Any suggestions about making the code better?
  • Tips are nice too. :)
I cry every time I see that I am not a respected member of this community.

  • Print