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.
This commit is contained in:
Tamás Bálint Misius
2025-05-06 17:01:22 +02:00
parent e44e77f6d6
commit 6d720bc79c
4 changed files with 94 additions and 10 deletions

View File

@@ -10,6 +10,7 @@ enum EventTraits : uint32_t
eventTraitSimGraphics = UINT32_C(0x00000002), eventTraitSimGraphics = UINT32_C(0x00000002),
eventTraitHindersSrt = UINT32_C(0x00000004), eventTraitHindersSrt = UINT32_C(0x00000004),
eventTraitInterface = UINT32_C(0x00000008), eventTraitInterface = UINT32_C(0x00000008),
eventTraitInterfaceGraphics = UINT32_C(0x00000010),
}; };
constexpr EventTraits operator |(EventTraits lhs, EventTraits rhs) constexpr EventTraits operator |(EventTraits lhs, EventTraits rhs)
{ {
@@ -85,7 +86,7 @@ struct MouseWheelEvent
struct TickEvent struct TickEvent
{ {
static constexpr EventTraits traits = eventTraitInterface; static constexpr EventTraits traits = eventTraitInterface | eventTraitInterfaceGraphics;
}; };
struct BlurEvent struct BlurEvent

View File

@@ -2,6 +2,62 @@
#include "graphics/Graphics.h" #include "graphics/Graphics.h"
#include "graphics/Renderer.h" #include "graphics/Renderer.h"
void NonGraphicsContext::Die()
{
luaL_error(GetLSI()->L, "this functionality is restricted to graphics events");
}
void NonGraphicsContext::BlendPixel(Vec2<int>, RGBA)
{
Die();
}
Vec2<int> NonGraphicsContext::BlendText(Vec2<int>, const String &, RGBA)
{
Die();
return { 0, 0 };
}
void NonGraphicsContext::DrawLine(Vec2<int>, Vec2<int>, RGB)
{
Die();
}
void NonGraphicsContext::BlendLine(Vec2<int>, Vec2<int>, RGBA)
{
Die();
}
void NonGraphicsContext::DrawRect(Rect<int>, RGB)
{
Die();
}
void NonGraphicsContext::BlendRect(Rect<int>, RGBA)
{
Die();
}
void NonGraphicsContext::DrawFilledRect(Rect<int>, RGB)
{
Die();
}
void NonGraphicsContext::BlendFilledRect(Rect<int>, RGBA)
{
Die();
}
void NonGraphicsContext::BlendEllipse(Vec2<int>, Vec2<int>, RGBA)
{
Die();
}
void NonGraphicsContext::BlendFilledEllipse(Vec2<int>, Vec2<int>, RGBA)
{
Die();
}
static int32_t int32Truncate(double n) static int32_t int32Truncate(double n)
{ {
if (n >= 0x1p31) if (n >= 0x1p31)
@@ -255,6 +311,10 @@ static int setClipRect(lua_State *L)
{ {
return luaL_error(L, "simulation graphics do not support clip rects"); 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 x = luaL_optinteger(L, 1, 0);
int y = luaL_optinteger(L, 2, 0); int y = luaL_optinteger(L, 2, 0);
int w = luaL_optinteger(L, 3, WINDOWW); int w = luaL_optinteger(L, 3, WINDOWW);

View File

@@ -3,6 +3,7 @@
#include "LuaSmartRef.h" #include "LuaSmartRef.h"
#include "CommandInterface.h" #include "CommandInterface.h"
#include "gui/game/GameControllerEvents.h" #include "gui/game/GameControllerEvents.h"
#include "graphics/Pixel.h"
#include "simulation/StructProperty.h" #include "simulation/StructProperty.h"
#include "simulation/ElementDefs.h" #include "simulation/ElementDefs.h"
#include <cstdint> #include <cstdint>
@@ -69,6 +70,22 @@ struct CustomTool
LuaSmartRef select; LuaSmartRef select;
}; };
struct NonGraphicsContext
{
static void Die();
void BlendPixel(Vec2<int>, RGBA);
Vec2<int> BlendText(Vec2<int>, const String &, RGBA);
void DrawLine(Vec2<int>, Vec2<int>, RGB);
void BlendLine(Vec2<int>, Vec2<int>, RGBA);
void DrawRect(Rect<int>, RGB);
void BlendRect(Rect<int>, RGBA);
void DrawFilledRect(Rect<int>, RGB);
void BlendFilledRect(Rect<int>, RGBA);
void BlendEllipse(Vec2<int>, Vec2<int>, RGBA);
void BlendFilledEllipse(Vec2<int>, Vec2<int>, RGBA);
};
class LuaScriptInterface : public CommandInterface class LuaScriptInterface : public CommandInterface
{ {
LuaStatePtr luaState; LuaStatePtr luaState;
@@ -84,7 +101,9 @@ public:
Simulation *sim; Simulation *sim;
Graphics *g; Graphics *g;
std::variant<Graphics *, Renderer *> GetGraphics() NonGraphicsContext ngc;
std::variant<Graphics *, Renderer *, NonGraphicsContext *> GetGraphics()
{ {
if (eventTraits & eventTraitSimGraphics) if (eventTraits & eventTraitSimGraphics)
{ {
@@ -93,8 +112,12 @@ public:
// installed for eventTraitSimGraphics and *SimDraw events. // installed for eventTraitSimGraphics and *SimDraw events.
return ren; return ren;
} }
if (eventTraits & eventTraitInterfaceGraphics)
{
return g; return g;
} }
return &ngc;
}
std::vector<CustomElement> customElements; // must come after luaState std::vector<CustomElement> customElements; // must come after luaState
std::vector<CustomTool> customTools; std::vector<CustomTool> customTools;

View File

@@ -232,7 +232,7 @@ void LuaWindow::triggerOnTick()
{ {
lua_rawgeti(L, LUA_REGISTRYINDEX, onTickFunction); 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 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)); ci->Log(CommandInterface::LogError, tpt_lua_toString(L, -1));
} }
@@ -244,7 +244,7 @@ void LuaWindow::triggerOnDraw()
if(onDrawFunction) if(onDrawFunction)
{ {
lua_rawgeti(L, LUA_REGISTRYINDEX, 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)); ci->Log(CommandInterface::LogError, tpt_lua_toString(L, -1));
} }