- CVAR cleanup, in particular decoupling from game internals, which now get handled via callbacks.

This commit is contained in:
Christoph Oelckers 2020-04-11 13:48:55 +02:00
commit 1479e3a1c3
17 changed files with 432 additions and 374 deletions

View file

@ -52,6 +52,8 @@
#include "utf8.h"
#include "fontinternals.h"
#include "intermission/intermission.h"
#include "menu/menu.h"
#include "c_cvars.h"
DVector2 AM_GetPosition();
int Net_GetLatency(int *ld, int *ad);
@ -3147,12 +3149,6 @@ DEFINE_ACTION_FUNCTION_NATIVE(_AltHUD, GetLatency, Net_GetLatency)
//
//==========================================================================
//==========================================================================
//
// W_NumLumps
//
//==========================================================================
DEFINE_ACTION_FUNCTION(_Wads, GetNumEntries)
{
PARAM_PROLOGUE;
@ -3217,6 +3213,126 @@ DEFINE_ACTION_FUNCTION(_Wads, ReadLump)
ACTION_RETURN_STRING(isLumpValid ? fileSystem.ReadFile(lump).GetString() : FString());
}
//==========================================================================
//
// CVARs
//
//==========================================================================
DEFINE_ACTION_FUNCTION(_CVar, GetInt)
{
PARAM_SELF_STRUCT_PROLOGUE(FBaseCVar);
auto v = self->GetGenericRep(CVAR_Int);
ACTION_RETURN_INT(v.Int);
}
DEFINE_ACTION_FUNCTION(_CVar, GetFloat)
{
PARAM_SELF_STRUCT_PROLOGUE(FBaseCVar);
auto v = self->GetGenericRep(CVAR_Float);
ACTION_RETURN_FLOAT(v.Float);
}
DEFINE_ACTION_FUNCTION(_CVar, GetString)
{
PARAM_SELF_STRUCT_PROLOGUE(FBaseCVar);
auto v = self->GetGenericRep(CVAR_String);
ACTION_RETURN_STRING(v.String);
}
DEFINE_ACTION_FUNCTION(_CVar, SetInt)
{
// Only menus are allowed to change CVARs.
PARAM_SELF_STRUCT_PROLOGUE(FBaseCVar);
if (!(self->GetFlags() & CVAR_MOD))
{
// Only menus are allowed to change non-mod CVARs.
if (DMenu::InMenu == 0)
{
ThrowAbortException(X_OTHER, "Attempt to change CVAR '%s' outside of menu code", self->GetName());
}
}
PARAM_INT(val);
UCVarValue v;
v.Int = val;
self->SetGenericRep(v, CVAR_Int);
return 0;
}
DEFINE_ACTION_FUNCTION(_CVar, SetFloat)
{
PARAM_SELF_STRUCT_PROLOGUE(FBaseCVar);
if (!(self->GetFlags() & CVAR_MOD))
{
// Only menus are allowed to change non-mod CVARs.
if (DMenu::InMenu == 0)
{
ThrowAbortException(X_OTHER, "Attempt to change CVAR '%s' outside of menu code", self->GetName());
}
}
PARAM_FLOAT(val);
UCVarValue v;
v.Float = (float)val;
self->SetGenericRep(v, CVAR_Float);
return 0;
}
DEFINE_ACTION_FUNCTION(_CVar, SetString)
{
// Only menus are allowed to change CVARs.
PARAM_SELF_STRUCT_PROLOGUE(FBaseCVar);
if (!(self->GetFlags() & CVAR_MOD))
{
// Only menus are allowed to change non-mod CVARs.
if (DMenu::InMenu == 0)
{
ThrowAbortException(X_OTHER, "Attempt to change CVAR '%s' outside of menu code", self->GetName());
}
}
PARAM_STRING(val);
UCVarValue v;
v.String = val.GetChars();
self->SetGenericRep(v, CVAR_String);
return 0;
}
DEFINE_ACTION_FUNCTION(_CVar, GetRealType)
{
PARAM_SELF_STRUCT_PROLOGUE(FBaseCVar);
ACTION_RETURN_INT(self->GetRealType());
}
DEFINE_ACTION_FUNCTION(_CVar, ResetToDefault)
{
PARAM_SELF_STRUCT_PROLOGUE(FBaseCVar);
if (!(self->GetFlags() & CVAR_MOD))
{
// Only menus are allowed to change non-mod CVARs.
if (DMenu::InMenu == 0)
{
ThrowAbortException(X_OTHER, "Attempt to change CVAR '%s' outside of menu code", self->GetName());
}
}
self->ResetToDefault();
return 0;
}
DEFINE_ACTION_FUNCTION(_CVar, FindCVar)
{
PARAM_PROLOGUE;
PARAM_NAME(name);
ACTION_RETURN_POINTER(FindCVar(name.GetChars(), nullptr));
}
DEFINE_ACTION_FUNCTION(_CVar, GetCVar)
{
PARAM_PROLOGUE;
PARAM_NAME(name);
PARAM_POINTER(plyr, player_t);
ACTION_RETURN_POINTER(GetCVar(plyr ? int(plyr - players) : -1, name.GetChars()));
}