Skip to content

Commit

Permalink
fixes for command part and luapad
Browse files Browse the repository at this point in the history
luapad QoL
-"run" button now repositions with performlayout
-add more buttons for whether to remember position/size, and whether to auto-kill the luapad panel when dragging the view
-trim newlines to help the property's display
-drag check on the resizes
-prevent resizing to smaller than 100 pixels
-compile only when pressing enter, the run button or exiting the window. no more errors spammed for every new character typed

command update
-make onshow, delayed strings work with lua
-limit the error message for when sv_allowcslua 0
-error handling applied to luapad. it does change the compilestring handleError arg but there's not much lost here. end users don't care about the extra lines that were printed from the stacktrace (they were only UI backend functions, not the code worked on)
  • Loading branch information
pingu7867 committed Dec 16, 2024
1 parent e5c3ac1 commit d030e0e
Show file tree
Hide file tree
Showing 5 changed files with 249 additions and 39 deletions.
112 changes: 98 additions & 14 deletions lua/pac3/core/client/parts/command.lua
Original file line number Diff line number Diff line change
Expand Up @@ -112,12 +112,79 @@ function PART:SetUseLua(b)
self:SetString(self:GetString())
end

function PART:SetString(str)
if self.UseLua and canRunLua() and self:GetPlayerOwner() == pac.LocalPlayer then
self.func = CompileString(str, "pac_event")
function PART:HandleErrors(result, mode)
if isstring(result) then
pac.Message(result)
self.Error = "[" .. mode .. "] " .. result
self.erroring_mode = mode
self:SetError(result)
if pace.ActiveSpecialPanel and pace.ActiveSpecialPanel.luapad then
pace.ActiveSpecialPanel.special_title = self.Error
end
elseif isfunction(result) then
if pace.ActiveSpecialPanel and pace.ActiveSpecialPanel.luapad then
if not self.Error then --good compile
pace.ActiveSpecialPanel.special_title = "[" .. mode .. "] " .. "successfully compiled"
self.Error = nil
self:SetError()
elseif (self.erroring_mode~= nil and self.erroring_mode ~= mode) then --good compile but already had an error from somewhere else (there are 3 script areas: main, onhide, delayed)
pace.ActiveSpecialPanel.special_title = "successfully compiled, but another erroring script may remain at " .. self.erroring_mode
else -- if we fixed our previous error from the same mode
pace.ActiveSpecialPanel.special_title = "[" .. mode .. "] " .. "successfully compiled"
self.Error = nil
self:SetError()
end
end
end
end

function PART:SetString(str)
str = string.Trim(str,"\n")
self.func = nil
if self.UseLua and canRunLua() and self:GetPlayerOwner() == pac.LocalPlayer and str ~= "" then
self.func = CompileString(str, "pac_event", false)
self:HandleErrors(self.func, "Main string")
end
self.String = str
if self.UseLua and not canRunLua() then
self:SetError("clientside lua is disabled (sv_allowcslua 0)")
end
end

function PART:SetOnHideString(str)
str = string.Trim(str,"\n")
self.onhide_func = nil
if self.erroring_mode == "OnHide string" then self.erroring_mode = nil end
if self.UseLua and canRunLua() and self:GetPlayerOwner() == pac.LocalPlayer and str ~= "" then
self.onhide_func = CompileString(str, "pac_event", false)
self:HandleErrors(self.onhide_func, "OnHide string")
end
self.OnHideString = str
if self.UseLua and not canRunLua() then
self:SetError("clientside lua is disabled (sv_allowcslua 0)")
end
end

function PART:SetDelayedString(str)
str = string.Trim(str,"\n")
self.delayed_func = nil
if self.erroring_mode == "Delayed string" then self.erroring_mode = nil end
if self.UseLua and canRunLua() and self:GetPlayerOwner() == pac.LocalPlayer and str ~= "" then
self.delayed_func = CompileString(str, "pac_event", false)
self:HandleErrors(self.delayed_func, "Delayed string")
end
self.DelayedString = str
if self.UseLua and not canRunLua() then
self:SetError("clientside lua is disabled (sv_allowcslua 0)")
end
end

function PART:Initialize()
--yield for the compile until other vars are available (UseLua)
timer.Simple(0, function()
self:SetOnHideString(self:GetOnHideString())
self:SetDelayedString(self:GetDelayedString())
end)
end

function PART:GetCode()
Expand Down Expand Up @@ -160,22 +227,39 @@ function PART:GetNiceName()
return "command: " .. self.String
end

local function try_lua_exec(self, func)
if canRunLua() then
if isstring(func) then return end
local status, err = pcall(func)

if not status then
self:SetError(err)
ErrorNoHalt(err .. "\n")
end
else
local msg = "clientside lua is disabled (sv_allowcslua 0)"
self:SetError(msg)
pac.Message(tostring(self) .. " - ".. msg)
end
end

function PART:Execute(commandstring)
local ent = self:GetPlayerOwner()

if ent == pac.LocalPlayer then
if self.UseLua and self.func then
if canRunLua() then
local status, err = pcall(self.func)

if not status then
self:SetError(err)
ErrorNoHalt(err .. "\n")
if self.UseLua then
if (self.func or self.onhide_func or self.delayed_func) then
if commandstring == nil then --regular string
try_lua_exec(self, self.func)
else --other modes
if ((commandstring == self.OnHideString) and self.onhide_func) then
try_lua_exec(self, self.onhide_func)
elseif ((commandstring == self.DelayedString) and self.delayed_func) then
try_lua_exec(self, self.delayed_func)
elseif ((commandstring == self.String) and self.func) then
try_lua_exec(self, self.func)
end
end
else
local msg = "clientside lua is disabled (sv_allowcslua 0)"
self:SetError(msg)
pac.Message(tostring(self) .. " - ".. msg)
end
else
if hook.Run("PACCanRunConsoleCommand", self.String) == false then return end
Expand Down
6 changes: 6 additions & 0 deletions lua/pac3/core/client/parts/proxy.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1595,6 +1595,7 @@ local allowed = {
}

function PART:SetExpression(str, slot)
str = string.Trim(str,"\n")
if not slot then --that's the default expression
self.Expression = str
self.ExpressionFunc = nil
Expand Down Expand Up @@ -1659,26 +1660,31 @@ function PART:SetExpression(str, slot)
end

function PART:SetExpressionOnHide(str)
str = string.Trim(str,"\n")
self.ExpressionOnHide = str
self:SetExpression(str, 0)
end

function PART:SetExtra1(str)
str = string.Trim(str,"\n")
self.Extra1 = str
self:SetExpression(str, 1)
end

function PART:SetExtra2(str)
str = string.Trim(str,"\n")
self.Extra2 = str
self:SetExpression(str, 2)
end

function PART:SetExtra3(str)
str = string.Trim(str,"\n")
self.Extra3 = str
self:SetExpression(str, 3)
end

function PART:SetExtra4(str)
str = string.Trim(str,"\n")
self.Extra4 = str
self:SetExpression(str, 4)
end
Expand Down
Loading

0 comments on commit d030e0e

Please sign in to comment.