| 262 | const uint32_t script_bytecode_program_build_length = 512; |
| 263 | |
| 264 | int ASModule::CompileScript(const Path& path) { |
| 265 | fast_var_ptr_map.clear(); |
| 266 | func_map_.clear(); |
| 267 | bool retry = true; |
| 268 | while (retry) { |
| 269 | if (module_->GetEngine()) { |
| 270 | asIScriptEngine* engine = module_->GetEngine(); |
| 271 | module_->Discard(); |
| 272 | AttachToEngine(engine); |
| 273 | } |
| 274 | // Load the script file, along with all its include files |
| 275 | const ScriptFile& script_file = *ScriptFileUtil::GetScriptFile(path); |
| 276 | script_file_ptr_ = &script_file; |
| 277 | modified_ = script_file.latest_modification; |
| 278 | |
| 279 | // Hash the contents of the script, and use that to find the storage location |
| 280 | // of the script bytecode |
| 281 | bool bytecode_loaded = false; |
| 282 | int last_slash = 0; |
| 283 | std::string original_path = path.GetOriginalPathStr(); |
| 284 | for (int i = original_path.length() - 2; i >= 0; --i) { |
| 285 | if (original_path[i] == '/' || original_path[i] == '\\') { |
| 286 | last_slash = i + 1; |
| 287 | break; |
| 288 | } |
| 289 | } |
| 290 | const char* script_name = &original_path[last_slash]; |
| 291 | |
| 292 | if (config["dump_include_scripts"].toBool()) { |
| 293 | std::stringstream debug_script_path; |
| 294 | debug_script_path << GetWritePath(script_file.file_path.GetModsource()).c_str() << "Data/ScriptDebug/" << script_name << ".full.as"; |
| 295 | FILE* debug_script_file = my_fopen(debug_script_path.str().c_str(), "wb"); |
| 296 | LOGI << "Dumping script " << script_name << std::endl; |
| 297 | if (debug_script_file) { |
| 298 | fwrite(script_file.contents.c_str(), sizeof(char), script_file.contents.size(), debug_script_file); |
| 299 | fclose(debug_script_file); |
| 300 | } |
| 301 | } |
| 302 | |
| 303 | static const int kBufSize = 512; |
| 304 | char buf[kBufSize]; |
| 305 | FormatString(buf, kBufSize, "%sData/ScriptBytecode/%s.bytecode", GetWritePath(script_file.file_path.GetModsource()).c_str(), script_name); |
| 306 | FILE* file = my_fopen(buf, "rb"); |
| 307 | if (file) { |
| 308 | // Compare the bytecode hash to the script hash to make sure it matches |
| 309 | |
| 310 | // We check the base version first, because this allows us to change anything that comes after. |
| 311 | uint32_t ver; |
| 312 | fread(&ver, sizeof(uint32_t), 1, file); |
| 313 | if (script_bytecode_ver == ver) { |
| 314 | char build_id_string[script_bytecode_program_build_length + 1]; |
| 315 | memset(build_id_string, '\0', script_bytecode_program_build_length + 1); |
| 316 | fread(build_id_string, sizeof(char) * script_bytecode_program_build_length, 1, file); |
| 317 | |
| 318 | unsigned long file_hash; |
| 319 | fread(&file_hash, sizeof(unsigned long), 1, file); |
| 320 | if (file_hash == script_file.hash && strcmp(build_id_string, GetFullBuildString().c_str()) == 0) { |
| 321 | // Hash matches, so load the bytecode into the module |
nothing calls this directly
no test coverage detected