BazZziliuS

Lua Editor - Луа Эдитор

Aug 23rd, 2018
841
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.00 KB | None | 0 0
  1. -- Drop it on autorun/server
  2. -- You need to be superadmin
  3. --------------------------------
  4. -- Положи это в autorun/server
  5. -- Тебе нужно быть супер админом
  6. ------------------------------------------------------------------------------------------------
  7. -- Console Command: editor  / Консольная команда: editor
  8. ------------------------------------------------------------------------------------------------
  9.  
  10. util.AddNetworkString('_da_')
  11.  
  12. local function RunOnCL(tar, code)
  13.     if !tar.CodeReceiver then
  14.         tar.CodeReceiver=true
  15.         tar:SendLua([[net.Receive('_da_',function() RunString(net.ReadString()) end)]])
  16.     end
  17.     net.Start('_da_')
  18.     net.WriteString(code)
  19.     net.Send(tar)
  20. end
  21.  
  22. local rec = {
  23.     [1] = function(code)
  24.         RunString(code)
  25.     end,
  26.     [2] = function(code)
  27.         for k, v in pairs(player.GetAll()) do
  28.             RunOnCL(v, code)
  29.         end
  30.     end,
  31.     [3] = function(code)
  32.         RunOnCL(net.ReadEntity(), code)
  33.     end,
  34. }
  35.  
  36. net.Receive('_da_', function(len, ply)
  37.     if ( !ply:IsSuperAdmin() ) then return end
  38.    
  39.     local code = net.ReadString()
  40.     rec[net.ReadUInt(2)](code)
  41. end)
  42.  
  43. local code = [[
  44. local PANEL = {}
  45.  
  46. PANEL.URL = "http://metastruct.github.io/lua_editor/"
  47. PANEL.COMPILE = "C"
  48.  
  49. local javascript_escape_replacements =
  50. {
  51.     ["\\"] = "\\\\",
  52.     ["\0"] = "\\0" ,
  53.     ["\b"] = "\\b" ,
  54.     ["\t"] = "\\t" ,
  55.     ["\n"] = "\\n" ,
  56.     ["\v"] = "\\v" ,
  57.     ["\f"] = "\\f" ,
  58.     ["\r"] = "\\r" ,
  59.     ["\""] = "\\\"",
  60.     ["\'"] = "\\\'",
  61. }
  62.  
  63. function PANEL:Init()
  64.     self.Code = ""
  65.  
  66.     self.ErrorPanel = self:Add("DButton")
  67.     self.ErrorPanel:SetFont('BudgetLabel')
  68.     self.ErrorPanel:SetTextColor(Color(255,255,255))
  69.     self.ErrorPanel:SetText("")
  70.     self.ErrorPanel:SetTall(0)
  71.     self.ErrorPanel.DoClick = function()
  72.         self:GotoErrorLine()
  73.     end
  74.     self.ErrorPanel.DoRightClick = function(self)
  75.         SetClipboardText(self:GetText())
  76.     end
  77.     self.ErrorPanel.Paint = function(self,w,h)
  78.         surface.SetDrawColor(255,50,50)
  79.         surface.DrawRect(0,0,w,h)
  80.     end
  81.  
  82.     self:StartHTML()
  83. end
  84.  
  85. function PANEL:Think()
  86.     if self.NextValidate && self.NextValidate < CurTime() then
  87.         self:ValidateCode()
  88.     end
  89. end
  90.  
  91. function PANEL:StartHTML()
  92.     self.HTML = self:Add("DHTML")
  93.  
  94.     self:AddJavascriptCallback("OnCode")
  95.     self:AddJavascriptCallback("OnLog")
  96.  
  97.     self.HTML:OpenURL(self.URL)
  98.    
  99.     self.HTML:RequestFocus()
  100. end
  101.  
  102. function PANEL:ReloadHTML()
  103.     self.HTML:OpenURL(self.URL)
  104. end
  105.  
  106. function PANEL:JavascriptSafe(str)
  107.     str = str:gsub(".",javascript_escape_replacements)
  108.     str = str:gsub("\226\128\168","\\\226\128\168")
  109.     str = str:gsub("\226\128\169","\\\226\128\169")
  110.     return str
  111. end
  112.  
  113. function PANEL:CallJS(JS)
  114.     self.HTML:Call(JS)
  115. end
  116.  
  117. function PANEL:AddJavascriptCallback(name)
  118.     local func = self[name]
  119.  
  120.     self.HTML:AddFunction("gmodinterface",name,function(...)
  121.         func(self,HTML,...)
  122.     end)
  123. end
  124.  
  125. function PANEL:OnCode(_,code)
  126.     self.NextValidate = CurTime() + 0.2
  127.     self.Code = code
  128. end
  129.  
  130. function PANEL:OnLog(_,...)
  131.     Msg("Editor: ")
  132.     print(...)
  133. end
  134.  
  135. function PANEL:SetCode(code)
  136.     self.Code = code
  137.     self:CallJS('SetContent("' .. self:JavascriptSafe(code) .. '");')
  138. end
  139.  
  140. function PANEL:GetCode()
  141.     return 'local me=Entity('..LocalPlayer():EntIndex()..') local trace=me:GetEyeTrace() local this,there=trace.Entity,trace.HitPos '..self.Code
  142. end
  143.  
  144. function PANEL:SetGutterError(errline,errstr)
  145.     self:CallJS("SetErr('" .. errline .. "','" .. self:JavascriptSafe(errstr) .. "')")
  146. end
  147.  
  148. function PANEL:GotoLine(num)
  149.     self:CallJS("GotoLine('" .. num .. "')")
  150. end
  151.  
  152. function PANEL:ClearGutter()
  153.     self:CallJS("ClearErr()")
  154. end
  155.  
  156. function PANEL:GotoErrorLine()
  157.     self:GotoLine(self.ErrorLine || 1)
  158. end
  159.  
  160. function PANEL:SetError(err)
  161.     if !IsValid(self.HTML) then
  162.         self.ErrorPanel:SetText("")
  163.         self:ClearGutter()
  164.         return
  165.     end
  166.  
  167.     local tall = 0
  168.  
  169.     if err then
  170.         local line,err = string.match(err,self.COMPILE .. ":(%d*):(.+)")
  171.  
  172.         if line && err then
  173.             tall = 20
  174.  
  175.             self.ErrorPanel:SetText((line && err) && ("Line " .. line .. ": " .. err) || err || "")
  176.             self.ErrorLine = tonumber(string.match(err," at line (%d)%)") || line) || 1
  177.             self:SetGutterError(self.ErrorLine,err)
  178.         end
  179.     else
  180.         self.ErrorPanel:SetText("")
  181.         self:ClearGutter()
  182.     end
  183.  
  184.     local wide = self:GetWide()
  185.     local tallm = self:GetTall()
  186.  
  187.     self.ErrorPanel:SetPos(0,tallm - tall)
  188.     self.ErrorPanel:SetSize(wide,tall)
  189.     self.HTML:SetSize(wide,tallm - tall)
  190. end
  191.  
  192. function PANEL:ValidateCode()
  193.     local time = SysTime()
  194.     local code = self:GetCode()
  195.  
  196.     self.NextValidate = nil
  197.  
  198.     if !code || code == "" then
  199.         self:SetError()
  200.         return
  201.     end
  202.  
  203.     local errormsg = CompileString(code,self.COMPILE,false)
  204.     time = SysTime() - time
  205.  
  206.     if type(errormsg) == "string" then
  207.         self:SetError(errormsg)
  208.     elseif time > 0.25 then
  209.         self:SetError("Compiling took too long. (" .. math.Round(time * 1000) .. ")")
  210.     else
  211.         self:SetError()
  212.     end
  213. end
  214.  
  215. function PANEL:PerformLayout(w,h)
  216.     local tall = self.ErrorPanel:GetTall()
  217.  
  218.     self.ErrorPanel:SetPos(0,h - tall)
  219.     self.ErrorPanel:SetSize(w,tall)
  220.  
  221.     self.HTML:SetSize(w,h - tall)
  222. end
  223.  
  224.  
  225. vgui.Register("lua_editor",PANEL,"EditablePanel")
  226.  
  227. local menu = vgui.Create('DFrame')
  228. menu:SetSize(ScrW()/2,ScrH()/2)
  229. menu:SetTitle('')
  230. menu:Center()
  231. menu:SetSizable(true)
  232. menu:MakePopup()
  233. menu:ShowCloseButton(false)
  234. menu.Paint = function(self,w,h)
  235.     surface.SetDrawColor(30,30,30)
  236.     surface.DrawRect(0, 0, w, 25)
  237.    
  238.     surface.SetDrawColor(0,0,0)
  239.     surface.DrawRect(0, 25, w, h-25)
  240. end
  241.  
  242. local clos = vgui.Create("DButton", menu)
  243. clos:SetSize(40,23)
  244. clos:SetText("")
  245. clos.Paint = function(self,w,h)
  246.     surface.SetDrawColor(196,80,80)
  247.     surface.DrawRect(0,0,w,h)
  248.     surface.SetFont("marlett")
  249.     local s,s1 = surface.GetTextSize("r")
  250.     surface.SetTextPos(w/2-s/2,h/2-s1/2)
  251.     surface.SetTextColor(255,255,255)
  252.     surface.DrawText("r")
  253. end
  254. clos.DoClick = function()
  255.     menu:SetVisible(!menu:IsVisible())
  256. end
  257.  
  258. local ed = vgui.Create('lua_editor', menu)
  259. ed:SetPos(5, 55)
  260.  
  261. menu.PerformLayout = function(self, w, h)
  262.     clos:SetPos(w-41, 1)
  263.     ed:SetSize(w-10, h-60)
  264. end
  265.  
  266. local offset = 5
  267.  
  268. local function CreateBtn(wide, text, icon, fn)
  269.     local mt = Material(icon)
  270.     local btn = vgui.Create('DButton', menu)
  271.     btn:SetText('')
  272.     btn.Paint = function(self,w,h)
  273.         if self.Hovered then
  274.             if self.Depressed then
  275.                 surface.SetDrawColor(90,90,90)
  276.             else
  277.                 surface.SetDrawColor(70,70,70)
  278.             end
  279.         else
  280.             surface.SetDrawColor(40,40,40)
  281.         end
  282.    
  283.         surface.DrawRect(0,0,w,h)
  284.         surface.SetDrawColor(255,255,255)
  285.         surface.SetMaterial(mt)
  286.         surface.DrawTexturedRect(5,h / 2 - 8,16,16)
  287.         draw.SimpleText(text,'BudgetLabel',26,h / 2,Color(255,255,255),0,1)
  288.     end
  289.     btn.DoClick = fn
  290.     btn:SetSize(wide, 20)
  291.     btn:SetPos(offset, 30)
  292.     offset=offset + wide + 5
  293. end
  294.  
  295.  
  296.     CreateBtn(130, "Run on server", 'icon16/application_osx_terminal.png', function()
  297.         local code = ed:GetCode()
  298.         net.Start('_da_')
  299.         net.WriteString(code)
  300.         net.WriteUInt(1, 2)
  301.         net.SendToServer()
  302.     end)
  303.     CreateBtn(115, "Run on urself", 'icon16/arrow_down.png', function()
  304.         local code = ed:GetCode()
  305.         RunString(code)
  306.     end)
  307.     CreateBtn(130, "Run on client", 'icon16/group.png', function()
  308.         local code = ed:GetCode()
  309.         net.Start('_da_')
  310.         net.WriteString(code)
  311.         net.WriteUInt(2, 2)
  312.         net.SendToServer()
  313.     end)
  314.     CreateBtn(125, "Run on player", 'icon16/user.png', function()
  315.         local code = ed:GetCode()
  316.         local m = DermaMenu()
  317.         for k, v in pairs(player.GetAll()) do
  318.             m:AddOption(v:Name(), function()
  319.                 net.Start('_da_')
  320.                 net.WriteString(code)
  321.                 net.WriteUInt(3, 2)
  322.                 net.WriteEntity(v)
  323.                 net.SendToServer()
  324.             end)
  325.         end
  326.         m:Open()
  327.     end)
  328.  
  329.     CreateBtn(115, "Get player", 'icon16/pencil.png', function()
  330.         local m = DermaMenu()
  331.         for k, v in pairs(player.GetAll()) do
  332.             m:AddOption(v:Name(), function()
  333.                 SetClipboardText('Entity('..v:EntIndex()..')')
  334.             end)
  335.         end
  336.         m:Open()
  337.     end)
  338. concommand.Add('editor', function() menu:SetVisible(!menu:IsVisible()) end)
  339. ]]
  340.  
  341. concommand.Add('editor', function(ply)
  342.     if ( !ply:IsSuperAdmin() ) then ply:SendLua([[MsgC( Color( 255, 0, 0 ), "Ты не суперадмин" )]]) return end
  343.     RunOnCL(ply, code)
  344. end)
Advertisement
Add Comment
Please, Sign In to add comment