gd_mono.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /**************************************************************************/
  2. /* gd_mono.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 GD_MONO_H
  31. #define GD_MONO_H
  32. #include "../godotsharp_defs.h"
  33. #include "core/io/config_file.h"
  34. #ifndef GD_CLR_STDCALL
  35. #ifdef WIN32
  36. #define GD_CLR_STDCALL __stdcall
  37. #else
  38. #define GD_CLR_STDCALL
  39. #endif
  40. #endif
  41. namespace gdmono {
  42. #ifdef TOOLS_ENABLED
  43. struct PluginCallbacks {
  44. using FuncLoadProjectAssemblyCallback = bool(GD_CLR_STDCALL *)(const char16_t *, String *);
  45. using FuncLoadToolsAssemblyCallback = Object *(GD_CLR_STDCALL *)(const char16_t *, const void **, int32_t);
  46. using FuncUnloadProjectPluginCallback = bool(GD_CLR_STDCALL *)();
  47. FuncLoadProjectAssemblyCallback LoadProjectAssemblyCallback = nullptr;
  48. FuncLoadToolsAssemblyCallback LoadToolsAssemblyCallback = nullptr;
  49. FuncUnloadProjectPluginCallback UnloadProjectPluginCallback = nullptr;
  50. };
  51. #endif
  52. } // namespace gdmono
  53. class GDMono {
  54. bool initialized = false;
  55. bool runtime_initialized = false;
  56. bool finalizing_scripts_domain = false;
  57. void *hostfxr_dll_handle = nullptr;
  58. bool is_native_aot = false;
  59. String project_assembly_path;
  60. uint64_t project_assembly_modified_time = 0;
  61. #ifdef GD_MONO_HOT_RELOAD
  62. int project_load_failure_count = 0;
  63. #endif
  64. #ifdef TOOLS_ENABLED
  65. bool _load_project_assembly();
  66. void _try_load_project_assembly();
  67. #endif
  68. #ifdef DEBUG_METHODS_ENABLED
  69. uint64_t api_core_hash = 0;
  70. #endif
  71. #ifdef TOOLS_ENABLED
  72. uint64_t api_editor_hash = 0;
  73. #endif
  74. void _init_godot_api_hashes();
  75. #ifdef TOOLS_ENABLED
  76. gdmono::PluginCallbacks plugin_callbacks;
  77. #endif
  78. protected:
  79. static GDMono *singleton;
  80. public:
  81. #ifdef DEBUG_METHODS_ENABLED
  82. uint64_t get_api_core_hash() {
  83. if (api_core_hash == 0) {
  84. api_core_hash = ClassDB::get_api_hash(ClassDB::API_CORE);
  85. }
  86. return api_core_hash;
  87. }
  88. #ifdef TOOLS_ENABLED
  89. uint64_t get_api_editor_hash() {
  90. if (api_editor_hash == 0) {
  91. api_editor_hash = ClassDB::get_api_hash(ClassDB::API_EDITOR);
  92. }
  93. return api_editor_hash;
  94. }
  95. #endif // TOOLS_ENABLED
  96. #endif // DEBUG_METHODS_ENABLED
  97. _FORCE_INLINE_ static String get_expected_api_build_config() {
  98. #ifdef TOOLS_ENABLED
  99. return "Debug";
  100. #else
  101. #ifdef DEBUG_ENABLED
  102. return "Debug";
  103. #else
  104. return "Release";
  105. #endif
  106. #endif
  107. }
  108. static GDMono *get_singleton() {
  109. return singleton;
  110. }
  111. _FORCE_INLINE_ bool is_initialized() const {
  112. return initialized;
  113. }
  114. _FORCE_INLINE_ bool is_runtime_initialized() const {
  115. return runtime_initialized;
  116. }
  117. _FORCE_INLINE_ bool is_finalizing_scripts_domain() {
  118. return finalizing_scripts_domain;
  119. }
  120. _FORCE_INLINE_ const String &get_project_assembly_path() const {
  121. return project_assembly_path;
  122. }
  123. _FORCE_INLINE_ uint64_t get_project_assembly_modified_time() const {
  124. return project_assembly_modified_time;
  125. }
  126. #ifdef TOOLS_ENABLED
  127. const gdmono::PluginCallbacks &get_plugin_callbacks() {
  128. return plugin_callbacks;
  129. }
  130. #endif
  131. #ifdef GD_MONO_HOT_RELOAD
  132. void reload_failure();
  133. Error reload_project_assemblies();
  134. #endif
  135. bool should_initialize();
  136. void initialize();
  137. GDMono();
  138. ~GDMono();
  139. };
  140. namespace mono_bind {
  141. class GodotSharp : public Object {
  142. GDCLASS(GodotSharp, Object);
  143. friend class GDMono;
  144. void _reload_assemblies(bool p_soft_reload);
  145. bool _is_runtime_initialized();
  146. protected:
  147. static GodotSharp *singleton;
  148. static void _bind_methods();
  149. public:
  150. static GodotSharp *get_singleton() { return singleton; }
  151. GodotSharp();
  152. ~GodotSharp();
  153. };
  154. } // namespace mono_bind
  155. #endif // GD_MONO_H