| 246 | } |
| 247 | |
| 248 | int CompileScript(asIScriptEngine *engine) |
| 249 | { |
| 250 | int r; |
| 251 | |
| 252 | // We will load the script from a file on the disk. |
| 253 | FILE *f = fopen("script.as", "rb"); |
| 254 | if( f == 0 ) |
| 255 | { |
| 256 | cout << "Failed to open the script file 'script.as'." << endl; |
| 257 | return -1; |
| 258 | } |
| 259 | |
| 260 | // Determine the size of the file |
| 261 | fseek(f, 0, SEEK_END); |
| 262 | int len = ftell(f); |
| 263 | fseek(f, 0, SEEK_SET); |
| 264 | |
| 265 | // On Win32 it is possible to do the following instead |
| 266 | // int len = _filelength(_fileno(f)); |
| 267 | |
| 268 | // Read the entire file |
| 269 | string script; |
| 270 | script.resize(len); |
| 271 | size_t c = fread(&script[0], len, 1, f); |
| 272 | fclose(f); |
| 273 | |
| 274 | if( c == 0 ) |
| 275 | { |
| 276 | cout << "Failed to load script file." << endl; |
| 277 | return -1; |
| 278 | } |
| 279 | |
| 280 | // Add the script sections that will be compiled into executable code. |
| 281 | // If we want to combine more than one file into the same script, then |
| 282 | // we can call AddScriptSection() several times for the same module and |
| 283 | // the script engine will treat them all as if they were one. The script |
| 284 | // section name, will allow us to localize any errors in the script code. |
| 285 | asIScriptModule *mod = engine->GetModule(0, asGM_ALWAYS_CREATE); |
| 286 | r = mod->AddScriptSection("script", &script[0], len); |
| 287 | if( r < 0 ) |
| 288 | { |
| 289 | cout << "AddScriptSection() failed" << endl; |
| 290 | return -1; |
| 291 | } |
| 292 | |
| 293 | // Compile the script. If there are any compiler messages they will |
| 294 | // be written to the message stream that we set right after creating the |
| 295 | // script engine. If there are no errors, and no warnings, nothing will |
| 296 | // be written to the stream. |
| 297 | r = mod->Build(); |
| 298 | if( r < 0 ) |
| 299 | { |
| 300 | cout << "Build() failed" << endl; |
| 301 | return -1; |
| 302 | } |
| 303 | |
| 304 | // The engine doesn't keep a copy of the script sections after Build() has |
| 305 | // returned. So if the script needs to be recompiled, then all the script |
no test coverage detected