From 6d720bc79caca91b275f0f1c44a7327f28bc566b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tam=C3=A1s=20B=C3=A1lint=20Misius?= Date: Tue, 6 May 2025 17:01:22 +0200 Subject: [PATCH] Restrict graphics functions to graphics contexts gfx.textSize/getColors/getHexColor are exempt because they don't change any state that we care about and seem easy to deal with in general. --- src/gui/game/GameControllerEvents.h | 13 ++++--- src/lua/LuaGraphics.cpp | 60 +++++++++++++++++++++++++++++ src/lua/LuaScriptInterface.h | 27 ++++++++++++- src/lua/LuaWindow.cpp | 4 +- 4 files changed, 94 insertions(+), 10 deletions(-) diff --git a/src/gui/game/GameControllerEvents.h b/src/gui/game/GameControllerEvents.h index 946007a5e..aff209b9f 100644 --- a/src/gui/game/GameControllerEvents.h +++ b/src/gui/game/GameControllerEvents.h @@ -5,11 +5,12 @@ enum EventTraits : uint32_t { - eventTraitNone = UINT32_C(0x00000000), - eventTraitSimRng = UINT32_C(0x00000001), - eventTraitSimGraphics = UINT32_C(0x00000002), - eventTraitHindersSrt = UINT32_C(0x00000004), - eventTraitInterface = UINT32_C(0x00000008), + eventTraitNone = UINT32_C(0x00000000), + eventTraitSimRng = UINT32_C(0x00000001), + eventTraitSimGraphics = UINT32_C(0x00000002), + eventTraitHindersSrt = UINT32_C(0x00000004), + eventTraitInterface = UINT32_C(0x00000008), + eventTraitInterfaceGraphics = UINT32_C(0x00000010), }; constexpr EventTraits operator |(EventTraits lhs, EventTraits rhs) { @@ -85,7 +86,7 @@ struct MouseWheelEvent struct TickEvent { - static constexpr EventTraits traits = eventTraitInterface; + static constexpr EventTraits traits = eventTraitInterface | eventTraitInterfaceGraphics; }; struct BlurEvent diff --git a/src/lua/LuaGraphics.cpp b/src/lua/LuaGraphics.cpp index b7bb1cfef..60ddc3f89 100644 --- a/src/lua/LuaGraphics.cpp +++ b/src/lua/LuaGraphics.cpp @@ -2,6 +2,62 @@ #include "graphics/Graphics.h" #include "graphics/Renderer.h" +void NonGraphicsContext::Die() +{ + luaL_error(GetLSI()->L, "this functionality is restricted to graphics events"); +} + +void NonGraphicsContext::BlendPixel(Vec2, RGBA) +{ + Die(); +} + +Vec2 NonGraphicsContext::BlendText(Vec2, const String &, RGBA) +{ + Die(); + return { 0, 0 }; +} + +void NonGraphicsContext::DrawLine(Vec2, Vec2, RGB) +{ + Die(); +} + +void NonGraphicsContext::BlendLine(Vec2, Vec2, RGBA) +{ + Die(); +} + +void NonGraphicsContext::DrawRect(Rect, RGB) +{ + Die(); +} + +void NonGraphicsContext::BlendRect(Rect, RGBA) +{ + Die(); +} + +void NonGraphicsContext::DrawFilledRect(Rect, RGB) +{ + Die(); +} + +void NonGraphicsContext::BlendFilledRect(Rect, RGBA) +{ + Die(); +} + +void NonGraphicsContext::BlendEllipse(Vec2, Vec2, RGBA) +{ + Die(); +} + +void NonGraphicsContext::BlendFilledEllipse(Vec2, Vec2, RGBA) +{ + Die(); +} + static int32_t int32Truncate(double n) { if (n >= 0x1p31) @@ -255,6 +311,10 @@ static int setClipRect(lua_State *L) { return luaL_error(L, "simulation graphics do not support clip rects"); } + if (!(lsi->eventTraits & eventTraitInterfaceGraphics)) + { + NonGraphicsContext::Die(); + } int x = luaL_optinteger(L, 1, 0); int y = luaL_optinteger(L, 2, 0); int w = luaL_optinteger(L, 3, WINDOWW); diff --git a/src/lua/LuaScriptInterface.h b/src/lua/LuaScriptInterface.h index 33bc5c36b..88fba86c1 100644 --- a/src/lua/LuaScriptInterface.h +++ b/src/lua/LuaScriptInterface.h @@ -3,6 +3,7 @@ #include "LuaSmartRef.h" #include "CommandInterface.h" #include "gui/game/GameControllerEvents.h" +#include "graphics/Pixel.h" #include "simulation/StructProperty.h" #include "simulation/ElementDefs.h" #include @@ -69,6 +70,22 @@ struct CustomTool LuaSmartRef select; }; +struct NonGraphicsContext +{ + static void Die(); + + void BlendPixel(Vec2, RGBA); + Vec2 BlendText(Vec2, const String &, RGBA); + void DrawLine(Vec2, Vec2, RGB); + void BlendLine(Vec2, Vec2, RGBA); + void DrawRect(Rect, RGB); + void BlendRect(Rect, RGBA); + void DrawFilledRect(Rect, RGB); + void BlendFilledRect(Rect, RGBA); + void BlendEllipse(Vec2, Vec2, RGBA); + void BlendFilledEllipse(Vec2, Vec2, RGBA); +}; + class LuaScriptInterface : public CommandInterface { LuaStatePtr luaState; @@ -84,7 +101,9 @@ public: Simulation *sim; Graphics *g; - std::variant GetGraphics() + NonGraphicsContext ngc; + + std::variant GetGraphics() { if (eventTraits & eventTraitSimGraphics) { @@ -93,7 +112,11 @@ public: // installed for eventTraitSimGraphics and *SimDraw events. return ren; } - return g; + if (eventTraits & eventTraitInterfaceGraphics) + { + return g; + } + return &ngc; } std::vector customElements; // must come after luaState diff --git a/src/lua/LuaWindow.cpp b/src/lua/LuaWindow.cpp index 431970109..cc5355942 100644 --- a/src/lua/LuaWindow.cpp +++ b/src/lua/LuaWindow.cpp @@ -232,7 +232,7 @@ void LuaWindow::triggerOnTick() { lua_rawgeti(L, LUA_REGISTRYINDEX, onTickFunction); lua_pushnumber(L, 1); // this used to be dt, which was measured in 60ths of a second; this hardcodes 60fps - if(tpt_lua_pcall(L, 1, 0, 0, eventTraitInterface)) + if(tpt_lua_pcall(L, 1, 0, 0, eventTraitInterface | eventTraitInterfaceGraphics)) { ci->Log(CommandInterface::LogError, tpt_lua_toString(L, -1)); } @@ -244,7 +244,7 @@ void LuaWindow::triggerOnDraw() if(onDrawFunction) { lua_rawgeti(L, LUA_REGISTRYINDEX, onDrawFunction); - if(tpt_lua_pcall(L, 0, 0, 0, eventTraitInterface)) + if(tpt_lua_pcall(L, 0, 0, 0, eventTraitInterface | eventTraitInterfaceGraphics)) { ci->Log(CommandInterface::LogError, tpt_lua_toString(L, -1)); }