| 130 | } |
| 131 | |
| 132 | void ComScript::ChangeScript(const Guid &scriptGuid) { |
| 133 | #if WITH_EDITOR |
| 134 | // Disconnect with previously connected script asset. |
| 135 | if (scriptAsset) { |
| 136 | scriptAsset->Disconnect(&Asset::SIG_Reloaded, this); |
| 137 | scriptAsset = nullptr; |
| 138 | } |
| 139 | #endif |
| 140 | |
| 141 | if (!sandboxName.IsEmpty()) { |
| 142 | state->SetToNil(sandboxName.c_str()); |
| 143 | //state->ForceGC(); |
| 144 | } |
| 145 | |
| 146 | if (sandbox.IsValid()) { |
| 147 | sandbox = LuaCpp::Selector(); |
| 148 | } |
| 149 | |
| 150 | hasError = false; |
| 151 | |
| 152 | ClearFunctionMap(); |
| 153 | |
| 154 | this->scriptGuid = scriptGuid; |
| 155 | |
| 156 | if (scriptGuid.IsZero()) { |
| 157 | sandboxName = ""; |
| 158 | return; |
| 159 | } |
| 160 | |
| 161 | // Sandbox name is same as component GUID in string. |
| 162 | sandboxName = GetGuid().ToString(); |
| 163 | |
| 164 | const Str scriptPath = resourceGuidMapper.Get(scriptGuid); |
| 165 | char *text; |
| 166 | size_t size = fileSystem.LoadFile(scriptPath, true, (void **)&text); |
| 167 | if (!text) { |
| 168 | sandboxName = ""; |
| 169 | BE_WARNLOG("ComScript::ChangeScript: Failed to load script '%s'\n", scriptPath.c_str()); |
| 170 | return; |
| 171 | } |
| 172 | |
| 173 | if (!ParsePropertyNames(scriptPath, text, propertyNames)) { |
| 174 | propertyNames.Clear(); |
| 175 | } |
| 176 | |
| 177 | // Load a script with sandboxed on current Lua state. |
| 178 | if (!state->LoadBuffer(scriptPath.c_str(), text, size, sandboxName)) { |
| 179 | hasError = true; |
| 180 | } |
| 181 | |
| 182 | fileSystem.FreeFile(text); |
| 183 | |
| 184 | if (!hasError) { |
| 185 | // Get the state of current loaded script. |
| 186 | sandbox = (*state)[sandboxName]; |
| 187 | |
| 188 | // Run this script. |
| 189 | state->Run(); |
nothing calls this directly
no test coverage detected