gd_mono_assembly.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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/hash_map.h"
  35. #include "core/map.h"
  36. #include "core/ustring.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. MonoAssembly *assembly;
  60. MonoImage *image;
  61. bool refonly;
  62. bool loaded;
  63. String name;
  64. String path;
  65. uint64_t modified_time;
  66. HashMap<ClassKey, GDMonoClass *, ClassKey::Hasher> cached_classes;
  67. Map<MonoClass *, GDMonoClass *> cached_raw;
  68. bool gdobject_class_cache_updated;
  69. Map<StringName, GDMonoClass *> gdobject_class_cache;
  70. #ifdef DEBUG_ENABLED
  71. Vector<uint8_t> pdb_data;
  72. #endif
  73. static bool no_search;
  74. static bool in_preload;
  75. static Vector<String> search_dirs;
  76. static void assembly_load_hook(MonoAssembly *assembly, void *user_data);
  77. static MonoAssembly *assembly_search_hook(MonoAssemblyName *aname, void *user_data);
  78. static MonoAssembly *assembly_refonly_search_hook(MonoAssemblyName *aname, void *user_data);
  79. static MonoAssembly *assembly_preload_hook(MonoAssemblyName *aname, char **assemblies_path, void *user_data);
  80. static MonoAssembly *assembly_refonly_preload_hook(MonoAssemblyName *aname, char **assemblies_path, void *user_data);
  81. static MonoAssembly *_search_hook(MonoAssemblyName *aname, void *user_data, bool refonly);
  82. static MonoAssembly *_preload_hook(MonoAssemblyName *aname, char **assemblies_path, void *user_data, bool refonly);
  83. static GDMonoAssembly *_load_assembly_from(const String &p_name, const String &p_path, bool p_refonly);
  84. static GDMonoAssembly *_load_assembly_search(const String &p_name, const Vector<String> &p_search_dirs, bool p_refonly);
  85. static void _wrap_mono_assembly(MonoAssembly *assembly);
  86. friend class GDMono;
  87. static void initialize();
  88. public:
  89. Error load(bool p_refonly);
  90. Error wrapper_for_image(MonoImage *p_image);
  91. void unload();
  92. _FORCE_INLINE_ bool is_refonly() const { return refonly; }
  93. _FORCE_INLINE_ bool is_loaded() const { return loaded; }
  94. _FORCE_INLINE_ MonoImage *get_image() const { return image; }
  95. _FORCE_INLINE_ MonoAssembly *get_assembly() const { return assembly; }
  96. _FORCE_INLINE_ String get_name() const { return name; }
  97. _FORCE_INLINE_ String get_path() const { return path; }
  98. _FORCE_INLINE_ uint64_t get_modified_time() const { return modified_time; }
  99. GDMonoClass *get_class(const StringName &p_namespace, const StringName &p_name);
  100. GDMonoClass *get_class(MonoClass *p_mono_class);
  101. GDMonoClass *get_object_derived_class(const StringName &p_class);
  102. static void fill_search_dirs(Vector<String> &r_search_dirs, const String &p_custom_config = String());
  103. static GDMonoAssembly *load_from(const String &p_name, const String &p_path, bool p_refonly);
  104. GDMonoAssembly(const String &p_name, const String &p_path = String());
  105. ~GDMonoAssembly();
  106. };
  107. #endif // GD_MONO_ASSEMBLY_H