Skip to content

Commit cc7f067

Browse files
authored
Use RAII guard to ensure lua context is always closed (#6757)
1 parent 8410ed0 commit cc7f067

1 file changed

Lines changed: 33 additions & 54 deletions

File tree

code/scripting/api/libs/graphics.cpp

Lines changed: 33 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,21 @@ static bool WarnedBadThicknessLine = false;
5252
namespace scripting {
5353
namespace api {
5454

55+
// Sets the graphics context to the Lua context on creation and resets it back to the main context on destruction
56+
// Ensures that no matter how the parent function is exited the context is reset properly
57+
struct LuaScreenContext {
58+
LuaScreenContext() noexcept
59+
{
60+
gr_lua_screen.active = true;
61+
}
62+
~LuaScreenContext() noexcept
63+
{
64+
gr_lua_screen.active = false;
65+
}
66+
LuaScreenContext(const LuaScreenContext&) = delete;
67+
LuaScreenContext& operator=(const LuaScreenContext&) = delete;
68+
};
69+
5570
model_draw_list *Current_scene = nullptr;
5671

5772
//**********LIBRARY: Graphics
@@ -136,16 +151,14 @@ ADE_VIRTVAR(CurrentFont, l_Graphics, "font", "Current font", "font", nullptr)
136151
if(!ade_get_args(L, "*|o", l_Font.GetPtr(&newFh)))
137152
return ade_set_error(L, "o", l_Font.Set(font_h()));
138153

139-
gr_lua_screen.active = true;
154+
LuaScreenContext context;
140155

141156
if(ADE_SETTING_VAR && newFh->isValid()) {
142157
font::FontManager::setCurrentFontIndex(newFh->GetIndex());
143158
}
144159

145160
int font = font::FontManager::getCurrentFontIndex();
146161

147-
gr_lua_screen.active = false;
148-
149162
return ade_set_args(L, "o", l_Font.Set(font_h(font)));
150163
}
151164

@@ -573,12 +586,10 @@ ADE_FUNC(setColor,
573586
ade_get_args(L, "o", l_Color.Get(&col));
574587
}
575588

576-
gr_lua_screen.active = true;
589+
LuaScreenContext context;
577590

578591
gr_set_color_fast(&col);
579592

580-
gr_lua_screen.active = false;
581-
582593
return ADE_RETURN_NIL;
583594
}
584595

@@ -595,11 +606,9 @@ ADE_FUNC(getColor,
595606
bool rc = false;
596607
ade_get_args(L, "|b", &rc);
597608

598-
gr_lua_screen.active = true;
609+
LuaScreenContext context;
599610

600611
const color cur = GR_CURRENT_COLOR;
601-
602-
gr_lua_screen.active = false;
603612

604613
if (!rc) {
605614
return ade_set_args(L, "iiii", static_cast<int>(cur.red), static_cast<int>(cur.green), static_cast<int>(cur.blue), static_cast<int>(cur.alpha));
@@ -622,12 +631,10 @@ ADE_FUNC(setLineWidth, l_Graphics, "[number width=1.0]", "Sets the line width fo
622631
return ADE_RETURN_FALSE;
623632
}
624633

625-
gr_lua_screen.active = true;
634+
LuaScreenContext context;
626635

627636
gr_set_line_width(width);
628637

629-
gr_lua_screen.active = false;
630-
631638
return ADE_RETURN_TRUE;
632639
}
633640

@@ -642,7 +649,7 @@ ADE_FUNC(drawCircle, l_Graphics, "number Radius, number X, number Y, [boolean Fi
642649
if(!ade_get_args(L, "iii|b", &ra,&x,&y,&fill))
643650
return ADE_RETURN_NIL;
644651

645-
gr_lua_screen.active = true;
652+
LuaScreenContext context;
646653

647654
if (fill) {
648655
//WMC - Circle takes...diameter.
@@ -651,8 +658,6 @@ ADE_FUNC(drawCircle, l_Graphics, "number Radius, number X, number Y, [boolean Fi
651658
gr_unfilled_circle(x,y, ra*2, lua_ResizeMode);
652659
}
653660

654-
gr_lua_screen.active = false;
655-
656661
return ADE_RETURN_NIL;
657662
}
658663

@@ -669,12 +674,10 @@ ADE_FUNC(drawArc, l_Graphics, "number Radius, number X, number Y, number StartAn
669674
return ADE_RETURN_NIL;
670675
}
671676

672-
gr_lua_screen.active = true;
677+
LuaScreenContext context;
673678

674679
gr_arc(x,y, ra, angle_start, angle_end, fill, lua_ResizeMode);
675680

676-
gr_lua_screen.active = false;
677-
678681
return ADE_RETURN_NIL;
679682
}
680683

@@ -688,14 +691,12 @@ ADE_FUNC(drawCurve, l_Graphics, "number X, number Y, number Radius", "Draws a cu
688691
if(!ade_get_args(L, "iii|i", &x,&y,&ra, &dir))
689692
return ADE_RETURN_NIL;
690693

691-
gr_lua_screen.active = true;
694+
LuaScreenContext context;
692695

693696
//WMC - direction should be settable at a certain point via enumerations.
694697
//Not gonna deal with it now.
695698
gr_curve(x, y, ra, dir, lua_ResizeMode);
696699

697-
gr_lua_screen.active = false;
698-
699700
return ADE_RETURN_NIL;
700701
}
701702

@@ -709,12 +710,10 @@ ADE_FUNC(drawGradientLine, l_Graphics, "number X1, number Y1, number X2, number
709710
if(!ade_get_args(L, "iiii", &x1, &y1, &x2, &y2))
710711
return ADE_RETURN_NIL;
711712

712-
gr_lua_screen.active = true;
713+
LuaScreenContext context;
713714

714715
gr_gradient(x1,y1,x2,y2,lua_ResizeMode);
715716

716-
gr_lua_screen.active = false;
717-
718717
return ADE_RETURN_NIL;
719718
}
720719

@@ -728,12 +727,10 @@ ADE_FUNC(drawLine, l_Graphics, "number X1, number Y1, number X2, number Y2", "Dr
728727
if(!ade_get_args(L, "iiii", &x1, &y1, &x2, &y2))
729728
return ADE_RETURN_NIL;
730729

731-
gr_lua_screen.active = true;
730+
LuaScreenContext context;
732731

733732
gr_line(x1,y1,x2,y2,lua_ResizeMode);
734733

735-
gr_lua_screen.active = false;
736-
737734
return ADE_RETURN_NIL;
738735
}
739736

@@ -747,12 +744,10 @@ ADE_FUNC(drawPixel, l_Graphics, "number X, number Y", "Draws a pixel using activ
747744
if(!ade_get_args(L, "ii", &x, &y))
748745
return ADE_RETURN_NIL;
749746

750-
gr_lua_screen.active = true;
747+
LuaScreenContext context;
751748

752749
gr_pixel(x,y,lua_ResizeMode);
753750

754-
gr_lua_screen.active = false;
755-
756751
return ADE_RETURN_NIL;
757752
}
758753

@@ -775,7 +770,7 @@ ADE_FUNC(drawPolygon,
775770
if (!tdx->isValid())
776771
return ADE_RETURN_FALSE;
777772

778-
gr_lua_screen.active = true;
773+
LuaScreenContext context;
779774

780775
matrix *orip = &vmd_identity_matrix;
781776
if(mh != NULL)
@@ -796,14 +791,12 @@ ADE_FUNC(drawPolygon,
796791
if(!in_frame)
797792
g3_end_frame();
798793

799-
gr_lua_screen.active = false;
800-
801794
return ADE_RETURN_TRUE;
802795
}
803796

804797
void drawRectInternal(int x1, int x2, int y1, int y2, bool f = true, float a = 0.f)
805798
{
806-
gr_lua_screen.active = true;
799+
LuaScreenContext context;
807800

808801
if(f)
809802
{
@@ -845,8 +838,6 @@ void drawRectInternal(int x1, int x2, int y1, int y2, bool f = true, float a = 0
845838
}
846839
}
847840

848-
gr_lua_screen.active = false;
849-
850841
}
851842

852843
ADE_FUNC(drawRectangle, l_Graphics, "number X1, number Y1, number X2, number Y2, [boolean Filled=true, number angle=0.0]", "Draws a rectangle using active context values (like color or width). May be rotated by passing the angle parameter in radians.", nullptr, nullptr)
@@ -896,7 +887,7 @@ ADE_FUNC(drawSphere, l_Graphics, "[number Radius = 1.0, vector Position]", "Draw
896887
vec3d pos = vmd_zero_vector;
897888
ade_get_args(L, "|fo", &rad, l_Vector.Get(&pos));
898889

899-
gr_lua_screen.active = true;
890+
LuaScreenContext context;
900891

901892
bool in_frame = g3_in_frame() > 0;
902893
if(!in_frame) {
@@ -936,8 +927,6 @@ ADE_FUNC(drawSphere, l_Graphics, "[number Radius = 1.0, vector Position]", "Draw
936927
g3_end_frame();
937928
}
938929

939-
gr_lua_screen.active = false;
940-
941930
return ADE_RETURN_TRUE;
942931
}
943932

@@ -954,7 +943,7 @@ ADE_FUNC(draw3dLine, l_Graphics, "vector origin, vector destination, [boolean tr
954943
if (!ade_get_args(L, "oo|bff", l_Vector.GetPtr(&v1), l_Vector.GetPtr(&v2), &translucent, &thickness, &thicknessEnd))
955944
return ADE_RETURN_NIL;
956945

957-
gr_lua_screen.active = true;
946+
LuaScreenContext context;
958947

959948
if (thickness < 0) thickness = 0;
960949
if (thicknessEnd < 0) thicknessEnd = thickness;
@@ -973,8 +962,6 @@ ADE_FUNC(draw3dLine, l_Graphics, "vector origin, vector destination, [boolean tr
973962

974963
batching_add_line(v1, v2, thickness, thicknessEnd, GR_CURRENT_COLOR, translucent);
975964

976-
gr_lua_screen.active = false;
977-
978965
return ADE_RETURN_NIL;
979966
}
980967

@@ -1138,7 +1125,7 @@ ADE_FUNC(drawTargetingBrackets, l_Graphics, "object Object, [boolean draw=true,
11381125
return ADE_RETURN_NIL;
11391126
}
11401127

1141-
gr_lua_screen.active = true;
1128+
LuaScreenContext context;
11421129

11431130
object *targetp = objh->objp();
11441131

@@ -1219,8 +1206,6 @@ ADE_FUNC(drawTargetingBrackets, l_Graphics, "object Object, [boolean draw=true,
12191206
if ( entered_frame )
12201207
g3_end_frame( );
12211208

1222-
gr_lua_screen.active = false;
1223-
12241209
return ade_set_args(L, "iiii", x1, y1, x2, y2);
12251210
}
12261211

@@ -1248,7 +1233,7 @@ ADE_FUNC(
12481233
return ADE_RETURN_NIL;
12491234
}
12501235

1251-
gr_lua_screen.active = true;
1236+
LuaScreenContext context;
12521237

12531238
bool entered_frame = false;
12541239

@@ -1267,8 +1252,6 @@ ADE_FUNC(
12671252
if ( entered_frame )
12681253
g3_end_frame( );
12691254

1270-
gr_lua_screen.active = false;
1271-
12721255
if (in_sight > 0)
12731256
{
12741257
return ade_set_args(L, "iiii", coords[0], coords[1], coords[2], coords[3]);
@@ -1303,7 +1286,7 @@ ADE_FUNC(drawOffscreenIndicator, l_Graphics, "object Object, [boolean draw=true,
13031286
return ADE_RETURN_NIL;
13041287
}
13051288

1306-
gr_lua_screen.active = true;
1289+
LuaScreenContext context;
13071290

13081291
object *targetp = objh->objp();
13091292
bool in_frame = g3_in_frame() > 0;
@@ -1369,8 +1352,6 @@ ADE_FUNC(drawOffscreenIndicator, l_Graphics, "object Object, [boolean draw=true,
13691352
}
13701353
}
13711354

1372-
gr_lua_screen.active = false;
1373-
13741355
if (outpoint.x >= 0 && outpoint.y >=0)
13751356
return ade_set_args(L, "ii", (int)outpoint.x, (int)outpoint.y);
13761357
else
@@ -1387,7 +1368,7 @@ static int drawString_sub(lua_State *L, bool use_resize_arg)
13871368

13881369
int resize_mode = lua_ResizeMode;
13891370

1390-
gr_lua_screen.active = true;
1371+
LuaScreenContext context;
13911372

13921373
// if the frame has changed since the last drawString call, reset the string position
13931374
if (PreviousFrametimeOverall != game_get_overall_frametime())
@@ -1511,8 +1492,6 @@ static int drawString_sub(lua_State *L, bool use_resize_arg)
15111492
NextDrawStringPos[1] = curr_y;
15121493
}
15131494

1514-
gr_lua_screen.active = false;
1515-
15161495
return ade_set_error(L, "i", num_lines);
15171496
}
15181497

0 commit comments

Comments
 (0)