- be more thorough with 'in menu' checks for certain protected functions.

They would also pass the test if a menu just was open but not the actual invoker.
Also error out if this happens so that modders can see that they are doing unsupported things. Silent failure is not a good idea here.
This commit is contained in:
Christoph Oelckers 2018-09-15 12:30:05 +02:00 committed by drfrag666
parent 4cd4e01fff
commit 6df918341a
4 changed files with 48 additions and 4 deletions

View file

@ -48,6 +48,7 @@
#include "dobject.h"
#include "vm.h"
#include "i_time.h"
#include "menu/menu.h"
#include <math.h>
#include <stdlib.h>
@ -273,6 +274,14 @@ DEFINE_ACTION_FUNCTION(FKeyBindings, SetBind)
PARAM_SELF_STRUCT_PROLOGUE(FKeyBindings);
PARAM_INT(k);
PARAM_STRING(cmd);
// Only menus are allowed to change bindings.
if (DMenu::InMenu == 0)
{
I_FatalError("Attempt to change key bindings outside of menu code to '%s'", cmd.GetChars());
}
self->SetBind(k, cmd);
return 0;
}
@ -512,6 +521,13 @@ DEFINE_ACTION_FUNCTION(FKeyBindings, UnbindACommand)
{
PARAM_SELF_STRUCT_PROLOGUE(FKeyBindings);
PARAM_STRING(cmd);
// Only menus are allowed to change bindings.
if (DMenu::InMenu == 0)
{
I_FatalError("Attempt to unbind key bindings for '%s' outside of menu code", cmd.GetChars());
}
self->UnbindACommand(cmd);
return 0;
}

View file

@ -227,7 +227,14 @@ DEFINE_ACTION_FUNCTION(_CVar, SetInt)
{
// Only menus are allowed to change CVARs.
PARAM_SELF_STRUCT_PROLOGUE(FBaseCVar);
if (!(self->GetFlags() & CVAR_MOD) && CurrentMenu == nullptr) return 0;
if (!(self->GetFlags() & CVAR_MOD))
{
// Only menus are allowed to change non-mod CVARs.
if (DMenu::InMenu == 0)
{
I_FatalError("Attempt to change CVAR '%s' outside of menu code", self->GetName());
}
}
PARAM_INT(val);
UCVarValue v;
v.Int = val;
@ -237,9 +244,15 @@ DEFINE_ACTION_FUNCTION(_CVar, SetInt)
DEFINE_ACTION_FUNCTION(_CVar, SetFloat)
{
// Only menus are allowed to change CVARs.
PARAM_SELF_STRUCT_PROLOGUE(FBaseCVar);
if (!(self->GetFlags() & CVAR_MOD) && CurrentMenu == nullptr) return 0;
if (!(self->GetFlags() & CVAR_MOD))
{
// Only menus are allowed to change non-mod CVARs.
if (DMenu::InMenu == 0)
{
I_FatalError("Attempt to change CVAR '%s' outside of menu code", self->GetName());
}
}
PARAM_FLOAT(val);
UCVarValue v;
v.Float = (float)val;
@ -251,7 +264,14 @@ DEFINE_ACTION_FUNCTION(_CVar, SetString)
{
// Only menus are allowed to change CVARs.
PARAM_SELF_STRUCT_PROLOGUE(FBaseCVar);
if (!(self->GetFlags() & CVAR_MOD) && CurrentMenu == nullptr) return 0;
if (!(self->GetFlags() & CVAR_MOD))
{
// Only menus are allowed to change non-mod CVARs.
if (DMenu::InMenu == 0)
{
I_FatalError("Attempt to change CVAR '%s' outside of menu code", self->GetName());
}
}
PARAM_STRING(val);
UCVarValue v;
v.String = val.GetChars();

View file

@ -60,6 +60,7 @@
#include "gl/renderer/gl_renderer.h" // for menu blur
#include "scripting/types.h"
int DMenu::InMenu;
//
// Todo: Move these elsewhere
//
@ -184,7 +185,9 @@ bool DMenu::CallResponder(event_t *ev)
VMValue params[] = { (DObject*)this, &e };
int retval;
VMReturn ret(&retval);
InMenu++;
VMCall(func, params, 2, &ret, 1);
InMenu--;
return !!retval;
}
}
@ -196,7 +199,9 @@ bool DMenu::CallResponder(event_t *ev)
VMValue params[] = { (DObject*)this, &e };
int retval;
VMReturn ret(&retval);
InMenu++;
VMCall(func, params, 2, &ret, 1);
InMenu--;
return !!retval;
}
}
@ -216,7 +221,9 @@ bool DMenu::CallMenuEvent(int mkey, bool fromcontroller)
VMValue params[] = { (DObject*)this, mkey, fromcontroller };
int retval;
VMReturn ret(&retval);
InMenu++;
VMCall(func, params, 3, &ret, 1);
InMenu--;
return !!retval;
}
else return false;

View file

@ -265,6 +265,7 @@ public:
bool mMouseCapture;
bool mBackbuttonSelected;
bool DontDim;
static int InMenu;
DMenu(DMenu *parent = NULL);
bool TranslateKeyboardEvents();