gd_mono_assembly.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*************************************************************************/
  2. /* gd_mono_assembly.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
  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 GD_MONO_ASSEMBLY_H
  31. #define GD_MONO_ASSEMBLY_H
  32. #include <mono/jit/jit.h>
  33. #include <mono/metadata/assembly.h>
  34. #include "core/string/ustring.h"
  35. #include "core/templates/hash_map.h"
  36. #include "core/templates/map.h"
  37. #include "gd_mono_utils.h"
  38. class GDMonoAssembly {
  39. struct ClassKey {
  40. struct Hasher {
  41. static _FORCE_INLINE_ uint32_t hash(const ClassKey &p_key) {
  42. uint32_t hash = 0;
  43. GDMonoUtils::hash_combine(hash, p_key.namespace_name.hash());
  44. GDMonoUtils::hash_combine(hash, p_key.class_name.hash());
  45. return hash;
  46. }
  47. };
  48. _FORCE_INLINE_ bool operator==(const ClassKey &p_a) const {
  49. return p_a.class_name == class_name && p_a.namespace_name == namespace_name;
  50. }
  51. ClassKey() {}
  52. ClassKey(const StringName &p_namespace_name, const StringName &p_class_name) {
  53. namespace_name = p_namespace_name;
  54. class_name = p_class_name;
  55. }
  56. StringName namespace_name;
  57. StringName class_name;
  58. };
  59. String name;
  60. MonoImage *image;
  61. MonoAssembly *assembly;
  62. #ifdef GD_MONO_HOT_RELOAD
  63. uint64_t modified_time = 0;
  64. #endif
  65. bool gdobject_class_cache_updated = false;
  66. Map<StringName, GDMonoClass *> gdobject_class_cache;
  67. HashMap<ClassKey, GDMonoClass *, ClassKey::Hasher> cached_classes;
  68. Map<MonoClass *, GDMonoClass *> cached_raw;
  69. static Vector<String> search_dirs;
  70. static void assembly_load_hook(MonoAssembly *assembly, void *user_data);
  71. static MonoAssembly *assembly_search_hook(MonoAssemblyName *aname, void *user_data);
  72. static MonoAssembly *assembly_refonly_search_hook(MonoAssemblyName *aname, void *user_data);
  73. static MonoAssembly *assembly_preload_hook(MonoAssemblyName *aname, char **assemblies_path, void *user_data);
  74. static MonoAssembly *assembly_refonly_preload_hook(MonoAssemblyName *aname, char **assemblies_path, void *user_data);
  75. static MonoAssembly *_search_hook(MonoAssemblyName *aname, void *user_data, bool refonly);
  76. static MonoAssembly *_preload_hook(MonoAssemblyName *aname, char **assemblies_path, void *user_data, bool refonly);
  77. static MonoAssembly *_real_load_assembly_from(const String &p_path, bool p_refonly, MonoAssemblyName *p_aname = nullptr);
  78. static MonoAssembly *_load_assembly_search(const String &p_name, MonoAssemblyName *p_aname, bool p_refonly, const Vector<String> &p_search_dirs);
  79. friend class GDMono;
  80. static void initialize();
  81. public:
  82. void unload();
  83. _FORCE_INLINE_ MonoImage *get_image() const { return image; }
  84. _FORCE_INLINE_ MonoAssembly *get_assembly() const { return assembly; }
  85. _FORCE_INLINE_ String get_name() const { return name; }
  86. #ifdef GD_MONO_HOT_RELOAD
  87. _FORCE_INLINE_ uint64_t get_modified_time() const { return modified_time; }
  88. #endif
  89. String get_path() const;
  90. GDMonoClass *get_class(const StringName &p_namespace, const StringName &p_name);
  91. GDMonoClass *get_class(MonoClass *p_mono_class);
  92. GDMonoClass *get_object_derived_class(const StringName &p_class);
  93. static String find_assembly(const String &p_name);
  94. static void fill_search_dirs(Vector<String> &r_search_dirs, const String &p_custom_config = String(), const String &p_custom_bcl_dir = String());
  95. static const Vector<String> &get_default_search_dirs() { return search_dirs; }
  96. static GDMonoAssembly *load(const String &p_name, MonoAssemblyName *p_aname, bool p_refonly, const Vector<String> &p_search_dirs);
  97. static GDMonoAssembly *load_from(const String &p_name, const String &p_path, bool p_refonly);
  98. GDMonoAssembly(const String &p_name, MonoImage *p_image, MonoAssembly *p_assembly) :
  99. name(p_name),
  100. image(p_image),
  101. assembly(p_assembly) {
  102. }
  103. ~GDMonoAssembly();
  104. };
  105. #endif // GD_MONO_ASSEMBLY_H