gdscript_cache.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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/safe_binary_mutex.h"
  35. #include "core/templates/hash_map.h"
  36. #include "core/templates/hash_set.h"
  37. class GDScriptAnalyzer;
  38. class GDScriptParser;
  39. class GDScriptParserRef : public RefCounted {
  40. public:
  41. enum Status {
  42. EMPTY,
  43. PARSED,
  44. INHERITANCE_SOLVED,
  45. INTERFACE_SOLVED,
  46. FULLY_SOLVED,
  47. };
  48. private:
  49. GDScriptParser *parser = nullptr;
  50. GDScriptAnalyzer *analyzer = nullptr;
  51. Status status = EMPTY;
  52. Error result = OK;
  53. String path;
  54. uint32_t source_hash = 0;
  55. bool clearing = false;
  56. bool abandoned = false;
  57. friend class GDScriptCache;
  58. friend class GDScript;
  59. public:
  60. Status get_status() const;
  61. String get_path() const;
  62. uint32_t get_source_hash() const;
  63. GDScriptParser *get_parser();
  64. GDScriptAnalyzer *get_analyzer();
  65. Error raise_status(Status p_new_status);
  66. void clear();
  67. GDScriptParserRef() {}
  68. ~GDScriptParserRef();
  69. };
  70. class GDScriptCache {
  71. // String key is full path.
  72. HashMap<String, GDScriptParserRef *> parser_map;
  73. HashMap<String, Vector<ObjectID>> abandoned_parser_map;
  74. HashMap<String, Ref<GDScript>> shallow_gdscript_cache;
  75. HashMap<String, Ref<GDScript>> full_gdscript_cache;
  76. HashMap<String, Ref<GDScript>> static_gdscript_cache;
  77. HashMap<String, HashSet<String>> dependencies;
  78. HashMap<String, HashSet<String>> parser_inverse_dependencies;
  79. friend class GDScript;
  80. friend class GDScriptParserRef;
  81. friend class GDScriptInstance;
  82. static GDScriptCache *singleton;
  83. bool cleared = false;
  84. public:
  85. static const int BINARY_MUTEX_TAG = 2;
  86. private:
  87. static SafeBinaryMutex<BINARY_MUTEX_TAG> mutex;
  88. friend SafeBinaryMutex<BINARY_MUTEX_TAG> &_get_gdscript_cache_mutex();
  89. public:
  90. static void move_script(const String &p_from, const String &p_to);
  91. static void remove_script(const String &p_path);
  92. static Ref<GDScriptParserRef> get_parser(const String &p_path, GDScriptParserRef::Status status, Error &r_error, const String &p_owner = String());
  93. static bool has_parser(const String &p_path);
  94. static void remove_parser(const String &p_path);
  95. static String get_source_code(const String &p_path);
  96. static Vector<uint8_t> get_binary_tokens(const String &p_path);
  97. static Ref<GDScript> get_shallow_script(const String &p_path, Error &r_error, const String &p_owner = String());
  98. static Ref<GDScript> get_full_script(const String &p_path, Error &r_error, const String &p_owner = String(), bool p_update_from_disk = false);
  99. static Ref<GDScript> get_cached_script(const String &p_path);
  100. static Error finish_compiling(const String &p_owner);
  101. static void add_static_script(Ref<GDScript> p_script);
  102. static void remove_static_script(const String &p_fqcn);
  103. static void clear();
  104. GDScriptCache();
  105. ~GDScriptCache();
  106. };
  107. #endif // GDSCRIPT_CACHE_H