gdscript_cache.h 4.9 KB

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