MCPcopy Create free account
hub / github.com/OpenTTD/OpenTTD / Save

Method Save

src/script/script_instance.cpp:513–579  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

511}
512
513void ScriptInstance::Save()
514{
515 ScriptObject::ActiveInstance active(*this);
516
517 /* Don't save data if the script didn't start yet or if it crashed. */
518 if (this->engine == nullptr || this->engine->HasScriptCrashed()) {
519 SaveEmpty();
520 return;
521 }
522
523 HSQUIRRELVM vm = this->engine->GetVM();
524 if (this->is_save_data_on_stack) {
525 _script_sl_byte = 1;
526 SlObject(nullptr, _script_byte);
527 /* Save the data that was just loaded. */
528 SaveObject(vm, -1, SQUIRREL_MAX_DEPTH, false);
529 } else if (!this->is_started) {
530 SaveEmpty();
531 return;
532 } else if (this->engine->MethodExists(*this->instance, "Save")) {
533 HSQOBJECT savedata;
534 try {
535 /* We don't want to be interrupted during the save function. */
536 ScriptObject::DisableDoCommandScope disabler{};
537 if (!this->engine->CallMethod(*this->instance, "Save", &savedata, MAX_SL_OPS)) {
538 /* The script crashed in the Save function. We can't kill
539 * it here, but do so in the next script tick. */
540 SaveEmpty();
541 this->engine->CrashOccurred();
542 return;
543 }
544 } catch (Script_FatalError &e) {
545 /* If we don't mark the script as dead here cleaning up the squirrel
546 * stack could throw Script_FatalError again. */
547 this->is_dead = true;
548 this->engine->ThrowError(e.GetErrorMessage());
549 this->engine->ResumeError();
550 SaveEmpty();
551 /* We can't kill the script here, so mark it as crashed (not dead) and
552 * kill it in the next script tick. */
553 this->is_dead = false;
554 this->engine->CrashOccurred();
555 return;
556 }
557
558 if (!sq_istable(savedata)) {
559 ScriptLog::Error(this->GetOpsTillSuspend() <= 0 ? "This script took too long to Save." : "Save function should return a table.");
560 SaveEmpty();
561 this->engine->CrashOccurred();
562 return;
563 }
564 sq_pushobject(vm, savedata);
565 if (SaveObject(vm, -1, SQUIRREL_MAX_DEPTH, true)) {
566 _script_sl_byte = 1;
567 SlObject(nullptr, _script_byte);
568 SaveObject(vm, -1, SQUIRREL_MAX_DEPTH, false);
569 this->is_save_data_on_stack = true;
570 } else {

Callers

nothing calls this directly

Calls 11

GetOpsTillSuspendMethod · 0.95
SlObjectFunction · 0.85
sq_pushobjectFunction · 0.85
HasScriptCrashedMethod · 0.80
GetVMMethod · 0.80
MethodExistsMethod · 0.80
CrashOccurredMethod · 0.80
ThrowErrorMethod · 0.80
GetErrorMessageMethod · 0.80
ResumeErrorMethod · 0.80
CallMethodMethod · 0.45

Tested by

no test coverage detected