gdscript_cache.h 4.7 KB

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