2 * This file is part of OpenTTD.
3 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
8 /** @file script_asyncmode.cpp Implementation of ScriptAsyncMode. */
10 #include "../../stdafx.h"
11 #include "script_asyncmode.hpp"
12 #include "../script_instance.hpp"
13 #include "../script_fatalerror.hpp"
15 #include "../../safeguards.h"
17 bool ScriptAsyncMode::AsyncModeProc()
19 /* In async mode we only return 'true', telling the DoCommand it
20 * should stop run the command in asynchronous/fire-and-forget mode. */
24 bool ScriptAsyncMode::NonAsyncModeProc()
26 /* In non-async mode we only return 'false', normal operation. */
30 ScriptAsyncMode::ScriptAsyncMode(HSQUIRRELVM vm
)
32 int nparam
= sq_gettop(vm
) - 1;
34 throw sq_throwerror(vm
, "You need to pass a boolean to the constructor");
38 if (SQ_FAILED(sq_getbool(vm
, 2, &sqasync
))) {
39 throw sq_throwerror(vm
, "Argument must be a boolean");
42 this->last_mode
= this->GetDoCommandAsyncMode();
43 this->last_instance
= this->GetDoCommandAsyncModeInstance();
45 this->SetDoCommandAsyncMode(sqasync
? &ScriptAsyncMode::AsyncModeProc
: &ScriptAsyncMode::NonAsyncModeProc
, this);
48 void ScriptAsyncMode::FinalRelease()
50 if (this->GetDoCommandAsyncModeInstance() != this) {
51 /* Ignore this error if the script is not alive. */
52 if (ScriptObject::GetActiveInstance()->IsAlive()) {
53 throw Script_FatalError("Asyncmode object was removed while it was not the latest *Mode object created.");
58 ScriptAsyncMode::~ScriptAsyncMode()
60 this->SetDoCommandAsyncMode(this->last_mode
, this->last_instance
);