| 1 | /**************************************************************************/ |
| 2 | /* gdscript_cache.cpp */ |
| 3 | /**************************************************************************/ |
| 4 | /* This file is part of: */ |
| 5 | /* GODOT ENGINE */ |
| 6 | /* https://godotengine.org */ |
| 7 | /**************************************************************************/ |
| 8 | /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ |
| 9 | /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ |
| 10 | /* */ |
| 11 | /* Permission is hereby granted, free of charge, to any person obtaining */ |
| 12 | /* a copy of this software and associated documentation files (the */ |
| 13 | /* "Software"), to deal in the Software without restriction, including */ |
| 14 | /* without limitation the rights to use, copy, modify, merge, publish, */ |
| 15 | /* distribute, sublicense, and/or sell copies of the Software, and to */ |
| 16 | /* permit persons to whom the Software is furnished to do so, subject to */ |
| 17 | /* the following conditions: */ |
| 18 | /* */ |
| 19 | /* The above copyright notice and this permission notice shall be */ |
| 20 | /* included in all copies or substantial portions of the Software. */ |
| 21 | /* */ |
| 22 | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ |
| 23 | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ |
| 24 | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ |
| 25 | /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ |
| 26 | /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ |
| 27 | /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ |
| 28 | /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ |
| 29 | /**************************************************************************/ |
| 30 | |
| 31 | #include "gdscript_cache.h" |
| 32 | |
| 33 | #include "gdscript.h" |
| 34 | #include "gdscript_analyzer.h" |
| 35 | #include "gdscript_compiler.h" |
| 36 | #include "gdscript_parser.h" |
| 37 | |
| 38 | #include "core/io/file_access.h" |
| 39 | #include "core/templates/vector.h" |
| 40 | |
| 41 | GDScriptParserRef::Status GDScriptParserRef::get_status() const { |
| 42 | return status; |
| 43 | } |
| 44 | |
| 45 | String GDScriptParserRef::get_path() const { |
| 46 | return path; |
| 47 | } |
| 48 | |
| 49 | uint32_t GDScriptParserRef::get_source_hash() const { |
| 50 | return source_hash; |
| 51 | } |
| 52 | |
| 53 | GDScriptParser *GDScriptParserRef::get_parser() { |
| 54 | if (parser == nullptr) { |
| 55 | parser = memnew(GDScriptParser); |
| 56 | } |
| 57 | return parser; |
| 58 | } |
| 59 | |
| 60 | GDScriptAnalyzer *GDScriptParserRef::get_analyzer() { |
| 61 | if (analyzer == nullptr) { |
| 62 | analyzer = memnew(GDScriptAnalyzer(get_parser())); |
| 63 | } |
| 64 | return analyzer; |
| 65 | } |
| 66 | |
| 67 | Error GDScriptParserRef::raise_status(Status p_new_status) { |
| 68 | ERR_FAIL_COND_V(clearing, ERR_BUG); |
| 69 | ERR_FAIL_COND_V(parser == nullptr && status != EMPTY, ERR_BUG); |
| 70 | |
| 71 | while (result == OK && p_new_status > status) { |
| 72 | switch (status) { |
| 73 | case EMPTY: { |
| 74 | // Calling parse will clear the parser, which can destruct another GDScriptParserRef which can clear the last reference to the script with this path, calling remove_script, which clears this GDScriptParserRef. |
| 75 | // It's ok if its the first thing done here. |
| 76 | get_parser()->clear(); |
| 77 | status = PARSED; |
| 78 | String remapped_path = ResourceLoader::path_remap(path); |
| 79 | if (remapped_path.has_extension("gdc")) { |
| 80 | Vector<uint8_t> tokens = GDScriptCache::get_binary_tokens(remapped_path); |
| 81 | source_hash = hash_djb2_buffer(tokens.ptr(), tokens.size()); |
| 82 | result = get_parser()->parse_binary(tokens, path); |
| 83 | } else { |
| 84 | String source = GDScriptCache::get_source_code(remapped_path); |
| 85 | source_hash = source.hash(); |
| 86 | result = get_parser()->parse(source, path, false); |
| 87 | } |
| 88 | } break; |
| 89 | case PARSED: { |
| 90 | status = INHERITANCE_SOLVED; |
| 91 | result = get_analyzer()->resolve_inheritance(); |
| 92 | } break; |
| 93 | case INHERITANCE_SOLVED: { |
| 94 | status = INTERFACE_SOLVED; |
| 95 | result = get_analyzer()->resolve_interface(); |
| 96 | } break; |
| 97 | case INTERFACE_SOLVED: { |
| 98 | status = FULLY_SOLVED; |
| 99 | result = get_analyzer()->resolve_body(); |
| 100 | } break; |
| 101 | case FULLY_SOLVED: { |
| 102 | return result; |
| 103 | } |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | return result; |
| 108 | } |
| 109 | |
| 110 | void GDScriptParserRef::clear() { |
| 111 | if (clearing) { |
| 112 | return; |
| 113 | } |
| 114 | clearing = true; |
| 115 | |
| 116 | GDScriptParser *lparser = parser; |
| 117 | GDScriptAnalyzer *lanalyzer = analyzer; |
| 118 | |
| 119 | parser = nullptr; |
| 120 | analyzer = nullptr; |
| 121 | status = EMPTY; |
| 122 | result = OK; |
| 123 | source_hash = 0; |
| 124 | |
| 125 | clearing = false; |
| 126 | |
| 127 | if (lanalyzer != nullptr) { |
| 128 | memdelete(lanalyzer); |
| 129 | } |
| 130 | |
| 131 | if (lparser != nullptr) { |
| 132 | memdelete(lparser); |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | GDScriptParserRef::~GDScriptParserRef() { |
| 137 | clear(); |
| 138 | |
| 139 | if (!abandoned) { |
| 140 | MutexLock lock(GDScriptCache::singleton->mutex); |
| 141 | GDScriptCache::singleton->parser_map.erase(path); |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | GDScriptCache *GDScriptCache::singleton = nullptr; |
| 146 | |
| 147 | SafeBinaryMutex<GDScriptCache::BINARY_MUTEX_TAG> &_get_gdscript_cache_mutex() { |
| 148 | return GDScriptCache::mutex; |
| 149 | } |
| 150 | |
| 151 | template <> |
| 152 | thread_local SafeBinaryMutex<GDScriptCache::BINARY_MUTEX_TAG>::TLSData SafeBinaryMutex<GDScriptCache::BINARY_MUTEX_TAG>::tls_data(_get_gdscript_cache_mutex()); |
| 153 | SafeBinaryMutex<GDScriptCache::BINARY_MUTEX_TAG> GDScriptCache::mutex; |
| 154 | |
| 155 | void GDScriptCache::move_script(const String &p_from, const String &p_to) { |
| 156 | if (singleton == nullptr || p_from == p_to || p_from.is_empty()) { |
| 157 | return; |
| 158 | } |
| 159 | |
| 160 | MutexLock lock(singleton->mutex); |
| 161 | |
| 162 | if (singleton->cleared) { |
| 163 | return; |
| 164 | } |
| 165 | |
| 166 | remove_parser(p_from); |
| 167 | |
| 168 | if (singleton->shallow_gdscript_cache.has(p_from) && !p_from.is_empty()) { |
| 169 | singleton->shallow_gdscript_cache[p_to] = singleton->shallow_gdscript_cache[p_from]; |
| 170 | } |
| 171 | singleton->shallow_gdscript_cache.erase(p_from); |
| 172 | |
| 173 | if (singleton->full_gdscript_cache.has(p_from) && !p_from.is_empty()) { |
| 174 | singleton->full_gdscript_cache[p_to] = singleton->full_gdscript_cache[p_from]; |
| 175 | } |
| 176 | singleton->full_gdscript_cache.erase(p_from); |
| 177 | } |
| 178 | |
| 179 | void GDScriptCache::remove_script(const String &p_path) { |
| 180 | if (singleton == nullptr) { |
| 181 | return; |
| 182 | } |
| 183 | |
| 184 | MutexLock lock(singleton->mutex); |
| 185 | |
| 186 | if (singleton->cleared) { |
| 187 | return; |
| 188 | } |
| 189 | |
| 190 | if (HashMap<String, Vector<ObjectID>>::Iterator E = singleton->abandoned_parser_map.find(p_path)) { |
| 191 | for (ObjectID parser_ref_id : E->value) { |
| 192 | Ref<GDScriptParserRef> parser_ref = { ObjectDB::get_instance(parser_ref_id) }; |
| 193 | if (parser_ref.is_valid()) { |
| 194 | parser_ref->clear(); |
| 195 | } |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | singleton->abandoned_parser_map.erase(p_path); |
| 200 | |
| 201 | if (singleton->parser_map.has(p_path)) { |
| 202 | singleton->parser_map[p_path]->clear(); |
| 203 | } |
| 204 | |
| 205 | remove_parser(p_path); |
| 206 | |
| 207 | singleton->dependencies.erase(p_path); |
| 208 | singleton->shallow_gdscript_cache.erase(p_path); |
| 209 | singleton->full_gdscript_cache.erase(p_path); |
| 210 | } |
| 211 | |
| 212 | Ref<GDScriptParserRef> GDScriptCache::get_parser(const String &p_path, GDScriptParserRef::Status p_status, Error &r_error, const String &p_owner) { |
| 213 | MutexLock lock(singleton->mutex); |
| 214 | Ref<GDScriptParserRef> ref; |
| 215 | if (!p_owner.is_empty() && p_path != p_owner) { |
| 216 | singleton->dependencies[p_owner].insert(p_path); |
| 217 | singleton->parser_inverse_dependencies[p_path].insert(p_owner); |
| 218 | } |
| 219 | if (singleton->parser_map.has(p_path)) { |
| 220 | ref = Ref<GDScriptParserRef>(singleton->parser_map[p_path]); |
| 221 | if (ref.is_null()) { |
| 222 | r_error = ERR_INVALID_DATA; |
| 223 | return ref; |
| 224 | } |
| 225 | } else { |
| 226 | String remapped_path = ResourceLoader::path_remap(p_path); |
| 227 | if (!FileAccess::exists(remapped_path)) { |
| 228 | r_error = ERR_FILE_NOT_FOUND; |
| 229 | return ref; |
| 230 | } |
| 231 | ref.instantiate(); |
| 232 | ref->path = p_path; |
| 233 | singleton->parser_map[p_path] = ref.ptr(); |
| 234 | } |
| 235 | r_error = ref->raise_status(p_status); |
| 236 | |
| 237 | return ref; |
| 238 | } |
| 239 | |
| 240 | bool GDScriptCache::has_parser(const String &p_path) { |
| 241 | MutexLock lock(singleton->mutex); |
| 242 | return singleton->parser_map.has(p_path); |
| 243 | } |
| 244 | |
| 245 | void GDScriptCache::remove_parser(const String &p_path) { |
| 246 | MutexLock lock(singleton->mutex); |
| 247 | |
| 248 | if (singleton->parser_map.has(p_path)) { |
| 249 | GDScriptParserRef *parser_ref = singleton->parser_map[p_path]; |
| 250 | parser_ref->abandoned = true; |
| 251 | singleton->abandoned_parser_map[p_path].push_back(parser_ref->get_instance_id()); |
| 252 | } |
| 253 | |
| 254 | // Can't clear the parser because some other parser might be currently using it in the chain of calls. |
| 255 | singleton->parser_map.erase(p_path); |
| 256 | |
| 257 | // Have to copy while iterating, because parser_inverse_dependencies is modified. |
| 258 | HashSet<String> ideps = singleton->parser_inverse_dependencies[p_path]; |
| 259 | singleton->parser_inverse_dependencies.erase(p_path); |
| 260 | for (String idep_path : ideps) { |
| 261 | remove_parser(idep_path); |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | String GDScriptCache::get_source_code(const String &p_path) { |
| 266 | Vector<uint8_t> source_file; |
| 267 | Error err; |
| 268 | Ref<FileAccess> f = FileAccess::open(p_path, FileAccess::READ, &err); |
| 269 | ERR_FAIL_COND_V(err, ""); |
| 270 | |
| 271 | uint64_t len = f->get_length(); |
| 272 | source_file.resize(len + 1); |
| 273 | uint64_t r = f->get_buffer(source_file.ptrw(), len); |
| 274 | ERR_FAIL_COND_V(r != len, ""); |
| 275 | source_file.write[len] = 0; |
| 276 | |
| 277 | String source; |
| 278 | if (source.append_utf8((const char *)source_file.ptr(), len) != OK) { |
| 279 | ERR_FAIL_V_MSG("", "Script '" + p_path + "' contains invalid unicode (UTF-8), so it was not loaded. Please ensure that scripts are saved in valid UTF-8 unicode."); |
| 280 | } |
| 281 | return source; |
| 282 | } |
| 283 | |
| 284 | Vector<uint8_t> GDScriptCache::get_binary_tokens(const String &p_path) { |
| 285 | Vector<uint8_t> buffer; |
| 286 | Error err = OK; |
| 287 | Ref<FileAccess> f = FileAccess::open(p_path, FileAccess::READ, &err); |
| 288 | ERR_FAIL_COND_V_MSG(err != OK, buffer, "Failed to open binary GDScript file '" + p_path + "'."); |
| 289 | |
| 290 | uint64_t len = f->get_length(); |
| 291 | buffer.resize(len); |
| 292 | uint64_t read = f->get_buffer(buffer.ptrw(), buffer.size()); |
| 293 | ERR_FAIL_COND_V_MSG(read != len, Vector<uint8_t>(), "Failed to read binary GDScript file '" + p_path + "'."); |
| 294 | |
| 295 | return buffer; |
| 296 | } |
| 297 | |
| 298 | Ref<GDScript> GDScriptCache::get_shallow_script(const String &p_path, Error &r_error, const String &p_owner) { |
| 299 | MutexLock lock(singleton->mutex); |
| 300 | |
| 301 | if (!p_owner.is_empty() && p_path != p_owner) { |
| 302 | singleton->dependencies[p_owner].insert(p_path); |
| 303 | } |
| 304 | if (singleton->full_gdscript_cache.has(p_path)) { |
| 305 | return singleton->full_gdscript_cache[p_path]; |
| 306 | } |
| 307 | if (singleton->shallow_gdscript_cache.has(p_path)) { |
| 308 | return singleton->shallow_gdscript_cache[p_path]; |
| 309 | } |
| 310 | |
| 311 | const String remapped_path = ResourceLoader::path_remap(p_path); |
| 312 | |
| 313 | Ref<GDScript> script; |
| 314 | script.instantiate(); |
| 315 | |
| 316 | script->set_path_cache(p_path); |
| 317 | if (remapped_path.has_extension("gdc")) { |
| 318 | Vector<uint8_t> buffer = get_binary_tokens(remapped_path); |
| 319 | if (buffer.is_empty()) { |
| 320 | r_error = ERR_FILE_CANT_READ; |
| 321 | } |
| 322 | script->set_binary_tokens_source(buffer); |
| 323 | } else { |
| 324 | r_error = script->load_source_code(remapped_path); |
| 325 | } |
| 326 | |
| 327 | if (r_error) { |
| 328 | return Ref<GDScript>(); // Returns null and does not cache when the script fails to load. |
| 329 | } |
| 330 | |
| 331 | Ref<GDScriptParserRef> parser_ref = get_parser(p_path, GDScriptParserRef::PARSED, r_error); |
| 332 | if (r_error == OK) { |
| 333 | GDScriptCompiler::make_scripts(script.ptr(), parser_ref->get_parser()->get_tree(), true); |
| 334 | } |
| 335 | |
| 336 | singleton->shallow_gdscript_cache[p_path] = script; |
| 337 | |
| 338 | return script; |
| 339 | } |
| 340 | |
| 341 | Ref<GDScript> GDScriptCache::get_full_script(const String &p_path, Error &r_error, const String &p_owner, bool p_update_from_disk) { |
| 342 | MutexLock lock(singleton->mutex); |
| 343 | |
| 344 | if (!p_owner.is_empty() && p_path != p_owner) { |
| 345 | singleton->dependencies[p_owner].insert(p_path); |
| 346 | } |
| 347 | |
| 348 | Ref<GDScript> script; |
| 349 | r_error = OK; |
| 350 | if (singleton->full_gdscript_cache.has(p_path)) { |
| 351 | script = singleton->full_gdscript_cache[p_path]; |
| 352 | if (!p_update_from_disk) { |
| 353 | return script; |
| 354 | } |
| 355 | } |
| 356 | |
| 357 | if (script.is_null()) { |
| 358 | script = get_shallow_script(p_path, r_error); |
| 359 | // Only exit early if script failed to load, otherwise let reload report errors. |
| 360 | if (script.is_null()) { |
| 361 | return script; |
| 362 | } |
| 363 | } |
| 364 | |
| 365 | const String remapped_path = ResourceLoader::path_remap(p_path); |
| 366 | |
| 367 | if (p_update_from_disk) { |
| 368 | if (remapped_path.has_extension("gdc")) { |
| 369 | Vector<uint8_t> buffer = get_binary_tokens(remapped_path); |
| 370 | if (buffer.is_empty()) { |
| 371 | r_error = ERR_FILE_CANT_READ; |
| 372 | goto finish; |
| 373 | } |
| 374 | script->set_binary_tokens_source(buffer); |
| 375 | } else { |
| 376 | r_error = script->load_source_code(remapped_path); |
| 377 | if (r_error) { |
| 378 | goto finish; |
| 379 | } |
| 380 | } |
| 381 | } |
| 382 | |
| 383 | // Allowing lifting the lock might cause a script to be reloaded multiple times, |
| 384 | // which, as a last resort deadlock prevention strategy, is a good tradeoff. |
| 385 | { |
| 386 | uint32_t allowance_id = WorkerThreadPool::thread_enter_unlock_allowance_zone(singleton->mutex); |
| 387 | r_error = script->reload(true); |
| 388 | WorkerThreadPool::thread_exit_unlock_allowance_zone(allowance_id); |
| 389 | } |
| 390 | |
| 391 | finish: |
| 392 | singleton->full_gdscript_cache[p_path] = script; |
| 393 | singleton->shallow_gdscript_cache.erase(p_path); |
| 394 | |
| 395 | // Add the script to the resource cache. Usually ResourceLoader would take care of it, but cyclic references can break that sometimes so we do it ourselves. |
| 396 | // Resources don't know whether they are cached, so using `set_path()` after `set_path_cache()` does not add the resource to the cache if the path is the same. |
| 397 | // We reset the cached path from `get_shallow_script()` so that the subsequent call to `set_path()` caches everything correctly. |
| 398 | script->set_path_cache(String()); |
| 399 | script->set_path(p_path, true); |
| 400 | |
| 401 | return script; |
| 402 | } |
| 403 | |
| 404 | Ref<GDScript> GDScriptCache::get_cached_script(const String &p_path) { |
| 405 | MutexLock lock(singleton->mutex); |
| 406 | |
| 407 | if (singleton->full_gdscript_cache.has(p_path)) { |
| 408 | return singleton->full_gdscript_cache[p_path]; |
| 409 | } |
| 410 | |
| 411 | if (singleton->shallow_gdscript_cache.has(p_path)) { |
| 412 | return singleton->shallow_gdscript_cache[p_path]; |
| 413 | } |
| 414 | |
| 415 | return Ref<GDScript>(); |
| 416 | } |
| 417 | |
| 418 | Error GDScriptCache::finish_compiling(const String &p_owner) { |
| 419 | MutexLock lock(singleton->mutex); |
| 420 | |
| 421 | // Mark this as compiled. |
| 422 | Ref<GDScript> script = get_cached_script(p_owner); |
| 423 | singleton->full_gdscript_cache[p_owner] = script; |
| 424 | singleton->shallow_gdscript_cache.erase(p_owner); |
| 425 | |
| 426 | HashSet<String> depends = singleton->dependencies[p_owner]; |
| 427 | |
| 428 | Error err = OK; |
| 429 | for (const String &E : depends) { |
| 430 | Error this_err = OK; |
| 431 | // No need to save the script. We assume it's already referenced in the owner. |
| 432 | get_full_script(E, this_err); |
| 433 | |
| 434 | if (this_err != OK) { |
| 435 | err = this_err; |
| 436 | } |
| 437 | } |
| 438 | |
| 439 | singleton->dependencies.erase(p_owner); |
| 440 | |
| 441 | return err; |
| 442 | } |
| 443 | |
| 444 | void GDScriptCache::add_static_script(Ref<GDScript> p_script) { |
| 445 | ERR_FAIL_COND_MSG(p_script.is_null(), "Trying to cache empty script as static."); |
| 446 | ERR_FAIL_COND_MSG(!p_script->is_valid(), "Trying to cache non-compiled script as static."); |
| 447 | singleton->static_gdscript_cache[p_script->get_fully_qualified_name()] = p_script; |
| 448 | } |
| 449 | |
| 450 | void GDScriptCache::remove_static_script(const String &p_fqcn) { |
| 451 | singleton->static_gdscript_cache.erase(p_fqcn); |
| 452 | } |
| 453 | |
| 454 | void GDScriptCache::clear() { |
| 455 | if (singleton == nullptr) { |
| 456 | return; |
| 457 | } |
| 458 | |
| 459 | MutexLock lock(singleton->mutex); |
| 460 | |
| 461 | if (singleton->cleared) { |
| 462 | return; |
| 463 | } |
| 464 | singleton->cleared = true; |
| 465 | |
| 466 | singleton->parser_inverse_dependencies.clear(); |
| 467 | |
| 468 | for (const KeyValue<String, Vector<ObjectID>> &KV : singleton->abandoned_parser_map) { |
| 469 | for (ObjectID parser_ref_id : KV.value) { |
| 470 | Ref<GDScriptParserRef> parser_ref = { ObjectDB::get_instance(parser_ref_id) }; |
| 471 | if (parser_ref.is_valid()) { |
| 472 | parser_ref->clear(); |
| 473 | } |
| 474 | } |
| 475 | } |
| 476 | |
| 477 | singleton->abandoned_parser_map.clear(); |
| 478 | |
| 479 | RBSet<Ref<GDScriptParserRef>> parser_map_refs; |
| 480 | for (KeyValue<String, GDScriptParserRef *> &E : singleton->parser_map) { |
| 481 | parser_map_refs.insert(E.value); |
| 482 | } |
| 483 | |
| 484 | singleton->parser_map.clear(); |
| 485 | |
| 486 | for (Ref<GDScriptParserRef> &E : parser_map_refs) { |
| 487 | if (E.is_valid()) { |
| 488 | E->clear(); |
| 489 | } |
| 490 | } |
| 491 | |
| 492 | parser_map_refs.clear(); |
| 493 | singleton->shallow_gdscript_cache.clear(); |
| 494 | singleton->full_gdscript_cache.clear(); |
| 495 | singleton->static_gdscript_cache.clear(); |
| 496 | } |
| 497 | |
| 498 | GDScriptCache::GDScriptCache() { |
| 499 | singleton = this; |
| 500 | } |
| 501 | |
| 502 | GDScriptCache::~GDScriptCache() { |
| 503 | if (!cleared) { |
| 504 | clear(); |
| 505 | } |
| 506 | singleton = nullptr; |
| 507 | } |