1
0
mirror of https://github.com/OpenTTD/OpenTTD synced 2026-01-16 00:42:45 +01:00

Codechange: Replace ScriptObject::[SG]etAllowDoCommand with ScriptObject::DisableDoCommandScope

This commit is contained in:
glx22
2025-06-03 00:47:48 +02:00
committed by Loïc Guilloux
parent 4beb23af30
commit 31fbb17c5b
5 changed files with 28 additions and 57 deletions

View File

@@ -910,8 +910,7 @@ SQInteger ScriptList::Valuate(HSQUIRRELVM vm)
/* Don't allow docommand from a Valuator, as we can't resume in
* mid C++-code. */
bool backup_allow = ScriptObject::GetAllowDoCommand();
ScriptObject::SetAllowDoCommand(false);
ScriptObject::DisableDoCommandScope disabler{};
/* Limit the total number of ops that can be consumed by a valuate operation */
SQOpsLimiter limiter(vm, MAX_VALUATE_OPS, "valuator function");
@@ -933,7 +932,6 @@ SQInteger ScriptList::Valuate(HSQUIRRELVM vm)
/* Call the function. Squirrel pops all parameters and pushes the return value. */
if (SQ_FAILED(sq_call(vm, nparam + 1, SQTrue, SQFalse))) {
ScriptObject::SetAllowDoCommand(backup_allow);
return SQ_ERROR;
}
@@ -956,7 +954,6 @@ SQInteger ScriptList::Valuate(HSQUIRRELVM vm)
/* See below for explanation. The extra pop is the return value. */
sq_pop(vm, nparam + 4);
ScriptObject::SetAllowDoCommand(backup_allow);
return sq_throwerror(vm, "return value of valuator is not valid (not integer/bool)");
}
}
@@ -966,7 +963,6 @@ SQInteger ScriptList::Valuate(HSQUIRRELVM vm)
/* See below for explanation. The extra pop is the return value. */
sq_pop(vm, nparam + 4);
ScriptObject::SetAllowDoCommand(backup_allow);
return sq_throwerror(vm, "modifying valuated list outside of valuator function");
}
@@ -984,6 +980,5 @@ SQInteger ScriptList::Valuate(HSQUIRRELVM vm)
* 4. The ScriptList instance object. */
sq_pop(vm, nparam + 3);
ScriptObject::SetAllowDoCommand(backup_allow);
return 0;
}

View File

@@ -88,11 +88,9 @@ protected:
sq_push(vm, 2);
}
/* Don't allow docommand from a Valuator, as we can't resume in
/* Don't allow docommand from a filter, as we can't resume in
* mid C++-code. */
bool backup_allow = ScriptObject::GetAllowDoCommand();
ScriptObject::SetAllowDoCommand(false);
ScriptObject::DisableDoCommandScope disabler{};
if (nparam < 1) {
ScriptList::FillList<T>(list, item_valid);
@@ -101,7 +99,7 @@ protected:
SQOpsLimiter limiter(vm, MAX_VALUATE_OPS, "list filter function");
ScriptList::FillList<T>(list, item_valid,
[vm, nparam, backup_allow](const T *item) {
[vm, nparam](const T *item) {
/* Push the root table as instance object, this is what squirrel does for meta-functions. */
sq_pushroottable(vm);
/* Push all arguments for the valuator function. */
@@ -112,7 +110,6 @@ protected:
/* Call the function. Squirrel pops all parameters and pushes the return value. */
if (SQ_FAILED(sq_call(vm, nparam + 1, SQTrue, SQFalse))) {
ScriptObject::SetAllowDoCommand(backup_allow);
throw static_cast<SQInteger>(SQ_ERROR);
}
@@ -125,7 +122,6 @@ protected:
break;
default:
ScriptObject::SetAllowDoCommand(backup_allow);
throw sq_throwerror(vm, "return value of filter is not valid (not bool)");
}
@@ -139,8 +135,6 @@ protected:
/* Pop the filter function */
sq_poptop(vm);
}
ScriptObject::SetAllowDoCommand(backup_allow);
}
template <typename T>

View File

@@ -63,6 +63,10 @@ ScriptObject::ActiveInstance::~ActiveInstance()
ScriptObject::ActiveInstance::active = this->last_active;
}
ScriptObject::DisableDoCommandScope::DisableDoCommandScope()
: AutoRestoreBackup(GetStorage()->allow_do_command, false)
{}
/* static */ ScriptInstance &ScriptObject::GetActiveInstance()
{
assert(ScriptObject::ActiveInstance::active != nullptr);
@@ -205,16 +209,6 @@ ScriptObject::ActiveInstance::~ActiveInstance()
return GetStorage()->last_cmd_ret;
}
/* static */ void ScriptObject::SetAllowDoCommand(bool allow)
{
GetStorage()->allow_do_command = allow;
}
/* static */ bool ScriptObject::GetAllowDoCommand()
{
return GetStorage()->allow_do_command;
}
/* static */ void ScriptObject::SetCompany(::CompanyID company)
{
if (GetStorage()->root_company == INVALID_OWNER) GetStorage()->root_company = company;

View File

@@ -84,6 +84,11 @@ protected:
static ScriptInstance *active; ///< The global current active instance.
};
class DisableDoCommandScope : private AutoRestoreBackup<bool> {
public:
DisableDoCommandScope();
};
/**
* Save this object.
* Must push 2 elements on the stack:
@@ -265,21 +270,6 @@ protected:
*/
static const CommandDataBuffer &GetLastCommandResData();
/**
* Store a allow_do_command per company.
* @param allow The new allow.
*/
static void SetAllowDoCommand(bool allow);
/**
* Get the internal value of allow_do_command. This can differ
* from CanSuspend() if the reason we are not allowed
* to execute a DoCommand is in squirrel and not the API.
* In that case use this function to restore the previous value.
* @return True iff DoCommands are allowed in the current scope.
*/
static bool GetAllowDoCommand();
/**
* Set the current company to execute commands for or request
* information about.