| 56 | } |
| 57 | |
| 58 | void ScriptInstance::Initialize(const std::string &main_script, const std::string &instance_name, CompanyID company) |
| 59 | { |
| 60 | ScriptObject::ActiveInstance active(*this); |
| 61 | |
| 62 | this->controller = std::make_unique<ScriptController>(company); |
| 63 | |
| 64 | /* Register the API functions and classes */ |
| 65 | this->engine->SetGlobalPointer(this->engine.get()); |
| 66 | this->RegisterAPI(); |
| 67 | if (this->IsDead()) { |
| 68 | /* Failed to register API; a message has already been logged. */ |
| 69 | return; |
| 70 | } |
| 71 | |
| 72 | try { |
| 73 | ScriptObject::DisableDoCommandScope disabler{}; |
| 74 | /* Load and execute the script for this script */ |
| 75 | if (main_script == "%_dummy") { |
| 76 | this->LoadDummyScript(); |
| 77 | } else if (!this->engine->LoadScript(main_script) || this->engine->IsSuspended()) { |
| 78 | if (this->engine->IsSuspended()) ScriptLog::Error("This script took too long to load script. AI is not started."); |
| 79 | this->Died(); |
| 80 | return; |
| 81 | } |
| 82 | |
| 83 | /* Create the main-class */ |
| 84 | this->instance = std::make_unique<SQObject>(); |
| 85 | if (!this->engine->CreateClassInstance(instance_name, this->controller.get(), this->instance.get())) { |
| 86 | /* If CreateClassInstance has returned false instance has not been |
| 87 | * registered with squirrel, so avoid trying to Release it by clearing it now */ |
| 88 | this->instance.reset(); |
| 89 | this->Died(); |
| 90 | return; |
| 91 | } |
| 92 | } catch (Script_FatalError &e) { |
| 93 | this->is_dead = true; |
| 94 | this->engine->ThrowError(e.GetErrorMessage()); |
| 95 | this->engine->ResumeError(); |
| 96 | this->Died(); |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | void ScriptInstance::RegisterAPI() |
| 101 | { |
nothing calls this directly
no test coverage detected