Skip to content

Commit

Permalink
Merge pull request #1459 from nicolasnoble/imgui-safe
Browse files Browse the repository at this point in the history
Adding safe ImGui wrappers for Lua.
  • Loading branch information
nicolasnoble authored Nov 8, 2023
2 parents d8ca558 + cf61830 commit a83c75f
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 1 deletion.
71 changes: 71 additions & 0 deletions src/gui/imguisafe.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
-- lualoader, R"EOF(--
-- Copyright (C) 2023 PCSX-Redux authors
--
-- This program is free software; you can redistribute it and/or modify
-- it under the terms of the GNU General Public License as published by
-- the Free Software Foundation; either version 2 of the License, or
-- (at your option) any later version.
--
-- This program is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-- GNU General Public License for more details.
--
-- You should have received a copy of the GNU General Public License
-- along with this program; if not, write to the
-- Free Software Foundation, Inc.,
-- 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

if not imgui.safe then
imgui.safe = {}
end

function imgui.safe.builder(proxy, finalIfShown, final)
local function builder(...)
local args = { ... }
local lambda = args[#args]
if type(lambda) ~= 'function' then
error('Last argument must be a function')
end
args[#args] = nil
local rets = { proxy(...) }
local shown = rets[1]
local status, err = pcall(function() lambda(table.unpack(args)) end)
if shown and finalIfShown then
finalIfShown()
end
if final then
final()
end
if not status then
error(err)
end
return table.unpack(rets)
end
return builder
end

imgui.safe.Begin = imgui.safe.builder(imgui.Begin, nil, imgui.End)
imgui.safe.BeginChild = imgui.safe.builder(imgui.BeginChild, imgui.EndChild)
imgui.safe.BeginChildFrame = imgui.safe.builder(imgui.BeginChildFrame, imgui.EndChildFrame)
imgui.safe.BeginChild_4 = imgui.safe.builder(imgui.BeginChild_4, imgui.EndChild)
imgui.safe.BeginCombo = imgui.safe.builder(imgui.BeginCombo, imgui.EndCombo)
imgui.safe.BeginDisabled = imgui.safe.builder(imgui.BeginDisabled, imgui.EndDisabled)
imgui.safe.BeginDragDropSource = imgui.safe.builder(imgui.BeginDragDropSource, imgui.EndDragDropSource)
imgui.safe.BeginDragDropTarget = imgui.safe.builder(imgui.BeginDragDropTarget, imgui.EndDragDropTarget)
imgui.safe.BeginGroup = imgui.safe.builder(imgui.BeginGroup, imgui.EndGroup)
imgui.safe.BeginListBox = imgui.safe.builder(imgui.BeginListBox, imgui.EndListBox)
imgui.safe.BeginMainMenuBar = imgui.safe.builder(imgui.BeginMainMenuBar, imgui.EndMainMenuBar)
imgui.safe.BeginMenu = imgui.safe.builder(imgui.BeginMenu, imgui.EndMenu)
imgui.safe.BeginMenuBar = imgui.safe.builder(imgui.BeginMenuBar, imgui.EndMenuBar)
imgui.safe.BeginPopup = imgui.safe.builder(imgui.BeginPopup, imgui.EndPopup)
imgui.safe.BeginPopupContextItem = imgui.safe.builder(imgui.BeginPopupContextItem, imgui.EndPopup)
imgui.safe.BeginPopupContextVoid = imgui.safe.builder(imgui.BeginPopupContextVoid, imgui.EndPopup)
imgui.safe.BeginPopupContextWindow = imgui.safe.builder(imgui.BeginPopupContextWindow, imgui.EndPopup)
imgui.safe.BeginPopupModal = imgui.safe.builder(imgui.BeginPopupModal, imgui.EndPopup)
imgui.safe.BeginTabBar = imgui.safe.builder(imgui.BeginTabBar, imgui.EndTabBar)
imgui.safe.BeginTabItem = imgui.safe.builder(imgui.BeginTabItem, imgui.EndTabItem)
imgui.safe.BeginTable = imgui.safe.builder(imgui.BeginTable, imgui.EndTable)
imgui.safe.BeginTooltip = imgui.safe.builder(imgui.BeginTooltip, imgui.EndTooltip)

-- )EOF"
6 changes: 5 additions & 1 deletion src/gui/luaimguiextra.cc
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,15 @@ void registerAllSymbols(PCSX::Lua L) {

void PCSX::LuaFFI::open_imguiextra(Lua L) {
registerAllSymbols(L);
static int lualoader = 1;
static int lualoader = 2;
static const char* imguiextra = (
#include "gui/imguiextraffi.lua"
);
L.load(imguiextra, "internal:gui/imguiextraffi.lua");
static const char* imguisafe = (
#include "gui/imguisafe.lua"
);
L.load(imguisafe, "internal:gui/imguisafe.lua");

L.getfieldtable("imgui", LUA_GLOBALSINDEX);
L.getfieldtable("extra");
Expand Down

0 comments on commit a83c75f

Please sign in to comment.