mirror of https://github.com/OpenTTD/OpenTTD
(svn r15440) -Fix: Backup AIObject::GetAllowDoCommand and restore the old value so AIs can't work around it.
parent
3072a5d7c2
commit
1eeb0f6748
|
@ -272,13 +272,13 @@ void AIInstance::GameLoop()
|
||||||
|
|
||||||
if (!this->is_started) {
|
if (!this->is_started) {
|
||||||
try {
|
try {
|
||||||
|
AIObject::SetAllowDoCommand(false);
|
||||||
/* Run the constructor if it exists. Don't allow any DoCommands in it. */
|
/* Run the constructor if it exists. Don't allow any DoCommands in it. */
|
||||||
if (this->engine->MethodExists(*this->instance, "constructor")) {
|
if (this->engine->MethodExists(*this->instance, "constructor")) {
|
||||||
AIObject::SetAllowDoCommand(false);
|
|
||||||
this->engine->CallMethod(*this->instance, "constructor");
|
this->engine->CallMethod(*this->instance, "constructor");
|
||||||
AIObject::SetAllowDoCommand(true);
|
|
||||||
}
|
}
|
||||||
this->CallLoad();
|
this->CallLoad();
|
||||||
|
AIObject::SetAllowDoCommand(true);
|
||||||
/* Start the AI by calling Start() */
|
/* Start the AI by calling Start() */
|
||||||
if (!this->engine->CallMethod(*this->instance, "Start", _settings_game.ai.ai_max_opcode_till_suspend)) this->Died();
|
if (!this->engine->CallMethod(*this->instance, "Start", _settings_game.ai.ai_max_opcode_till_suspend)) this->Died();
|
||||||
} catch (AI_VMSuspend e) {
|
} catch (AI_VMSuspend e) {
|
||||||
|
@ -524,9 +524,10 @@ void AIInstance::Save()
|
||||||
} else if (this->engine->MethodExists(*this->instance, "Save")) {
|
} else if (this->engine->MethodExists(*this->instance, "Save")) {
|
||||||
HSQOBJECT savedata;
|
HSQOBJECT savedata;
|
||||||
/* We don't want to be interrupted during the save function. */
|
/* We don't want to be interrupted during the save function. */
|
||||||
|
bool backup_allow = AIObject::GetAllowDoCommand();
|
||||||
AIObject::SetAllowDoCommand(false);
|
AIObject::SetAllowDoCommand(false);
|
||||||
this->engine->CallMethod(*this->instance, "Save", &savedata);
|
this->engine->CallMethod(*this->instance, "Save", &savedata);
|
||||||
AIObject::SetAllowDoCommand(true);
|
AIObject::SetAllowDoCommand(backup_allow);
|
||||||
|
|
||||||
if (!sq_istable(savedata)) {
|
if (!sq_istable(savedata)) {
|
||||||
AILog::Error("Save function should return a table.");
|
AILog::Error("Save function should return a table.");
|
||||||
|
@ -653,8 +654,6 @@ void AIInstance::CallLoad()
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
AIObject::SetAllowDoCommand(false);
|
|
||||||
|
|
||||||
/* Go to the instance-root */
|
/* Go to the instance-root */
|
||||||
sq_pushobject(vm, *this->instance);
|
sq_pushobject(vm, *this->instance);
|
||||||
/* Find the function-name inside the script */
|
/* Find the function-name inside the script */
|
||||||
|
@ -673,6 +672,4 @@ void AIInstance::CallLoad()
|
||||||
|
|
||||||
/* Pop 1) The version, 2) the savegame data, 3) the object instance, 4) the function pointer. */
|
/* Pop 1) The version, 2) the savegame data, 3) the object instance, 4) the function pointer. */
|
||||||
sq_pop(vm, 4);
|
sq_pop(vm, 4);
|
||||||
|
|
||||||
AIObject::SetAllowDoCommand(true);
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -759,6 +759,7 @@ SQInteger AIAbstractList::Valuate(HSQUIRRELVM vm) {
|
||||||
|
|
||||||
/* Don't allow docommand from a Valuator, as we can't resume in
|
/* Don't allow docommand from a Valuator, as we can't resume in
|
||||||
* mid-code */
|
* mid-code */
|
||||||
|
bool backup_allow = AIObject::GetAllowDoCommand();
|
||||||
AIObject::SetAllowDoCommand(false);
|
AIObject::SetAllowDoCommand(false);
|
||||||
|
|
||||||
sq_addref(vm, &obj_func);
|
sq_addref(vm, &obj_func);
|
||||||
|
@ -788,7 +789,7 @@ SQInteger AIAbstractList::Valuate(HSQUIRRELVM vm) {
|
||||||
|
|
||||||
/* Call the function */
|
/* Call the function */
|
||||||
if (SQ_FAILED(sq_call(vm, nparam + 2, SQTrue, SQTrue))) {
|
if (SQ_FAILED(sq_call(vm, nparam + 2, SQTrue, SQTrue))) {
|
||||||
AIObject::SetAllowDoCommand(true);
|
AIObject::SetAllowDoCommand(backup_allow);
|
||||||
return SQ_ERROR;
|
return SQ_ERROR;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -810,7 +811,7 @@ SQInteger AIAbstractList::Valuate(HSQUIRRELVM vm) {
|
||||||
sq_release(vm, &obj_func);
|
sq_release(vm, &obj_func);
|
||||||
for (int i = 0; i < nparam; i++) sq_release(vm, &obj_params[i]);
|
for (int i = 0; i < nparam; i++) sq_release(vm, &obj_params[i]);
|
||||||
|
|
||||||
AIObject::SetAllowDoCommand(true);
|
AIObject::SetAllowDoCommand(backup_allow);
|
||||||
return sq_throwerror(vm, _SC("return value of valuator is not valid (not integer/bool)"));
|
return sq_throwerror(vm, _SC("return value of valuator is not valid (not integer/bool)"));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -826,6 +827,6 @@ SQInteger AIAbstractList::Valuate(HSQUIRRELVM vm) {
|
||||||
sq_release(vm, &obj_func);
|
sq_release(vm, &obj_func);
|
||||||
for (int i = 0; i < nparam; i++) sq_release(vm, &obj_params[i]);
|
for (int i = 0; i < nparam; i++) sq_release(vm, &obj_params[i]);
|
||||||
|
|
||||||
AIObject::SetAllowDoCommand(true);
|
AIObject::SetAllowDoCommand(backup_allow);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue