- added restrictions to CVAR and CCMD access functions for the menus. CVAR changes are only allowed when the menu is open or for mod-CVARs. The CCMD execution function is now private to the control requiring it and heavily guarded against improper access from the outside so that abuse is mostly impossible.

This also means that the remaining scriptification of the menu is on hold. The player menu would require even more access to critical game data, which is a no-go, and the other remaining menus offer little benefit from getting scriptified.
This commit is contained in:
Christoph Oelckers 2017-02-13 19:18:45 +01:00
parent c403fc5635
commit 6525e04118
5 changed files with 25 additions and 5 deletions

View file

@ -51,6 +51,7 @@
#include "v_palette.h" #include "v_palette.h"
#include "v_video.h" #include "v_video.h"
#include "colormatcher.h" #include "colormatcher.h"
#include "menu/menu.h"
struct FLatchedValue struct FLatchedValue
{ {
@ -204,7 +205,9 @@ DEFINE_ACTION_FUNCTION(_CVar, GetString)
DEFINE_ACTION_FUNCTION(_CVar, SetInt) DEFINE_ACTION_FUNCTION(_CVar, SetInt)
{ {
// Only menus are allowed to change CVARs.
PARAM_SELF_STRUCT_PROLOGUE(FBaseCVar); PARAM_SELF_STRUCT_PROLOGUE(FBaseCVar);
if (!(self->GetFlags() & CVAR_MOD) && DMenu::CurrentMenu == nullptr) return 0;
PARAM_INT(val); PARAM_INT(val);
UCVarValue v; UCVarValue v;
v.Int = val; v.Int = val;
@ -214,17 +217,21 @@ DEFINE_ACTION_FUNCTION(_CVar, SetInt)
DEFINE_ACTION_FUNCTION(_CVar, SetFloat) DEFINE_ACTION_FUNCTION(_CVar, SetFloat)
{ {
// Only menus are allowed to change CVARs.
PARAM_SELF_STRUCT_PROLOGUE(FBaseCVar); PARAM_SELF_STRUCT_PROLOGUE(FBaseCVar);
if (!(self->GetFlags() & CVAR_MOD) && DMenu::CurrentMenu == nullptr) return 0;
PARAM_FLOAT(val); PARAM_FLOAT(val);
UCVarValue v; UCVarValue v;
v.Float = val; v.Float = (float)val;
self->SetGenericRep(v, CVAR_Float); self->SetGenericRep(v, CVAR_Float);
return 0; return 0;
} }
DEFINE_ACTION_FUNCTION(_CVar, SetString) DEFINE_ACTION_FUNCTION(_CVar, SetString)
{ {
// Only menus are allowed to change CVARs.
PARAM_SELF_STRUCT_PROLOGUE(FBaseCVar); PARAM_SELF_STRUCT_PROLOGUE(FBaseCVar);
if (!(self->GetFlags() & CVAR_MOD) && DMenu::CurrentMenu == nullptr) return 0;
PARAM_STRING(val); PARAM_STRING(val);
UCVarValue v; UCVarValue v;
v.String = val.GetChars(); v.String = val.GetChars();

View file

@ -54,6 +54,7 @@
#include "d_net.h" #include "d_net.h"
#include "d_main.h" #include "d_main.h"
#include "serializer.h" #include "serializer.h"
#include "menu/menu.h"
// MACROS ------------------------------------------------------------------ // MACROS ------------------------------------------------------------------
@ -662,8 +663,10 @@ void C_DoCommand (const char *cmd, int keynum)
} }
} }
DEFINE_ACTION_FUNCTION(_Console, DoCommand) // This is only accessible to the special menu item to run CCMDs.
DEFINE_ACTION_FUNCTION(DOptionMenuItemCommand, DoCommand)
{ {
if (DMenu::CurrentMenu == nullptr) return 0;
PARAM_PROLOGUE; PARAM_PROLOGUE;
PARAM_STRING(cmd); PARAM_STRING(cmd);
C_DoCommand(cmd); C_DoCommand(cmd);

View file

@ -272,7 +272,6 @@ struct Console native
native static void HideConsole(); native static void HideConsole();
native static void MidPrint(Font fontname, string textlabel, bool bold = false); native static void MidPrint(Font fontname, string textlabel, bool bold = false);
native static vararg void Printf(string fmt, ...); native static vararg void Printf(string fmt, ...);
native static void DoCommand(String cmd);
} }
struct DamageTypeDefinition native struct DamageTypeDefinition native

View file

@ -336,7 +336,10 @@ class ColorpickerMenu : OptionMenu
if (mStartItem >= 0) if (mStartItem >= 0)
{ {
mDesc.mItems.Resize(mStartItem); mDesc.mItems.Resize(mStartItem);
if (mCVar != null) mCVar.SetInt(Color(int(mRed), int(mGreen), int(mBlue))); if (mCVar != null)
{
mCVar.SetInt(Color(int(mRed), int(mGreen), int(mBlue)));
}
mStartItem = -1; mStartItem = -1;
} }
} }

View file

@ -127,8 +127,16 @@ class OptionMenuItemCommand : OptionMenuItemSubmenu
return self; return self;
} }
private native static void DoCommand(String cmd); // This is very intentionally limited to this menu item to prevent abuse.
override bool Activate() override bool Activate()
{ {
// This needs to perform a few checks to prevent abuse by malicious modders.
let m = Menu.GetCurrentMenu();
// don't execute if no menu is active
if (m == null) return false;
// don't execute if this item cannot be found in the current menu.
if (m.GetItem(mAction) != self) return false;
Menu.MenuSound("menu/choose"); Menu.MenuSound("menu/choose");
Console.DoCommand(mAction); Console.DoCommand(mAction);
return true; return true;
@ -158,7 +166,7 @@ class OptionMenuItemSafeCommand : OptionMenuItemCommand
{ {
if (mkey == Menu.MKEY_MBYes) if (mkey == Menu.MKEY_MBYes)
{ {
Console.DoCommand(mAction); Super.Activate(mKey, fromController);
return true; return true;
} }
return Super.MenuEvent(mkey, fromcontroller); return Super.MenuEvent(mkey, fromcontroller);