gd_mono.h 5.3 KB

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