| 1 | /**************************************************************************/ |
| 2 | /* gdscript_cache.h */ |
| 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 | #ifndef GDSCRIPT_CACHE_H |
| 32 | #define GDSCRIPT_CACHE_H |
| 33 | |
| 34 | #include "gdscript.h" |
| 35 | |
| 36 | #include "core/object/ref_counted.h" |
| 37 | #include "core/os/mutex.h" |
| 38 | #include "core/templates/hash_map.h" |
| 39 | #include "core/templates/hash_set.h" |
| 40 | #include "scene/resources/packed_scene.h" |
| 41 | |
| 42 | class GDScriptAnalyzer; |
| 43 | class GDScriptParser; |
| 44 | |
| 45 | class GDScriptParserRef : public RefCounted { |
| 46 | public: |
| 47 | enum Status { |
| 48 | EMPTY, |
| 49 | PARSED, |
| 50 | INHERITANCE_SOLVED, |
| 51 | INTERFACE_SOLVED, |
| 52 | FULLY_SOLVED, |
| 53 | }; |
| 54 | |
| 55 | private: |
| 56 | GDScriptParser *parser = nullptr; |
| 57 | GDScriptAnalyzer *analyzer = nullptr; |
| 58 | Status status = EMPTY; |
| 59 | Error result = OK; |
| 60 | String path; |
| 61 | bool cleared = false; |
| 62 | |
| 63 | friend class GDScriptCache; |
| 64 | |
| 65 | public: |
| 66 | bool is_valid() const; |
| 67 | Status get_status() const; |
| 68 | GDScriptParser *get_parser() const; |
| 69 | GDScriptAnalyzer *get_analyzer(); |
| 70 | Error raise_status(Status p_new_status); |
| 71 | void clear(); |
| 72 | |
| 73 | GDScriptParserRef() {} |
| 74 | ~GDScriptParserRef(); |
| 75 | }; |
| 76 | |
| 77 | class GDScriptCache { |
| 78 | // String key is full path. |
| 79 | HashMap<String, GDScriptParserRef *> parser_map; |
| 80 | HashMap<String, Ref<GDScript>> shallow_gdscript_cache; |
| 81 | HashMap<String, Ref<GDScript>> full_gdscript_cache; |
| 82 | HashMap<String, Ref<GDScript>> static_gdscript_cache; |
| 83 | HashMap<String, HashSet<String>> dependencies; |
| 84 | HashMap<String, Ref<PackedScene>> packed_scene_cache; |
| 85 | HashMap<String, HashSet<String>> packed_scene_dependencies; |
| 86 | |
| 87 | friend class GDScript; |
| 88 | friend class GDScriptParserRef; |
| 89 | friend class GDScriptInstance; |
| 90 | |
| 91 | static GDScriptCache *singleton; |
| 92 | |
| 93 | bool cleared = false; |
| 94 | |
| 95 | Mutex mutex; |
| 96 | |
| 97 | public: |
| 98 | static void move_script(const String &p_from, const String &p_to); |
| 99 | static void remove_script(const String &p_path); |
| 100 | static Ref<GDScriptParserRef> get_parser(const String &p_path, GDScriptParserRef::Status status, Error &r_error, const String &p_owner = String()); |
| 101 | static String get_source_code(const String &p_path); |
| 102 | static Ref<GDScript> get_shallow_script(const String &p_path, Error &r_error, const String &p_owner = String()); |
| 103 | static Ref<GDScript> get_full_script(const String &p_path, Error &r_error, const String &p_owner = String(), bool p_update_from_disk = false); |
| 104 | static Ref<GDScript> get_cached_script(const String &p_path); |
| 105 | static Error finish_compiling(const String &p_owner); |
| 106 | static void add_static_script(Ref<GDScript> p_script); |
| 107 | static void remove_static_script(const String &p_fqcn); |
| 108 | |
| 109 | static Ref<PackedScene> get_packed_scene(const String &p_path, Error &r_error, const String &p_owner = "" ); |
| 110 | static void clear_unreferenced_packed_scenes(); |
| 111 | |
| 112 | static void clear(); |
| 113 | |
| 114 | GDScriptCache(); |
| 115 | ~GDScriptCache(); |
| 116 | }; |
| 117 | |
| 118 | #endif // GDSCRIPT_CACHE_H |
| 119 | |