mirror of
https://github.com/The-Powder-Toy/The-Powder-Toy.git
synced 2025-08-20 23:21:20 +02:00
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:
@@ -10,6 +10,7 @@ enum EventTraits : uint32_t
|
||||
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
|
||||
|
@@ -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<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)
|
||||
{
|
||||
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);
|
||||
|
@@ -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 <cstdint>
|
||||
@@ -69,6 +70,22 @@ struct CustomTool
|
||||
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
|
||||
{
|
||||
LuaStatePtr luaState;
|
||||
@@ -84,7 +101,9 @@ public:
|
||||
Simulation *sim;
|
||||
Graphics *g;
|
||||
|
||||
std::variant<Graphics *, Renderer *> GetGraphics()
|
||||
NonGraphicsContext ngc;
|
||||
|
||||
std::variant<Graphics *, Renderer *, NonGraphicsContext *> GetGraphics()
|
||||
{
|
||||
if (eventTraits & eventTraitSimGraphics)
|
||||
{
|
||||
@@ -93,8 +112,12 @@ public:
|
||||
// installed for eventTraitSimGraphics and *SimDraw events.
|
||||
return ren;
|
||||
}
|
||||
if (eventTraits & eventTraitInterfaceGraphics)
|
||||
{
|
||||
return g;
|
||||
}
|
||||
return &ngc;
|
||||
}
|
||||
|
||||
std::vector<CustomElement> customElements; // must come after luaState
|
||||
std::vector<CustomTool> customTools;
|
||||
|
@@ -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));
|
||||
}
|
||||
|
Reference in New Issue
Block a user