2
0

gd_mono.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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 "core/io/config_file.h"
  33. #include "../godotsharp_defs.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 runtime_initialized;
  55. bool finalizing_scripts_domain;
  56. void *hostfxr_dll_handle = nullptr;
  57. bool is_native_aot = false;
  58. String project_assembly_path;
  59. uint64_t project_assembly_modified_time = 0;
  60. #ifdef TOOLS_ENABLED
  61. bool _load_project_assembly();
  62. #endif
  63. uint64_t api_core_hash;
  64. #ifdef TOOLS_ENABLED
  65. uint64_t api_editor_hash;
  66. #endif
  67. void _init_godot_api_hashes();
  68. #ifdef TOOLS_ENABLED
  69. gdmono::PluginCallbacks plugin_callbacks;
  70. #endif
  71. protected:
  72. static GDMono *singleton;
  73. public:
  74. #ifdef DEBUG_METHODS_ENABLED
  75. uint64_t get_api_core_hash() {
  76. if (api_core_hash == 0) {
  77. api_core_hash = ClassDB::get_api_hash(ClassDB::API_CORE);
  78. }
  79. return api_core_hash;
  80. }
  81. #ifdef TOOLS_ENABLED
  82. uint64_t get_api_editor_hash() {
  83. if (api_editor_hash == 0) {
  84. api_editor_hash = ClassDB::get_api_hash(ClassDB::API_EDITOR);
  85. }
  86. return api_editor_hash;
  87. }
  88. #endif // TOOLS_ENABLED
  89. #endif // DEBUG_METHODS_ENABLED
  90. _FORCE_INLINE_ static String get_expected_api_build_config() {
  91. #ifdef TOOLS_ENABLED
  92. return "Debug";
  93. #else
  94. #ifdef DEBUG_ENABLED
  95. return "Debug";
  96. #else
  97. return "Release";
  98. #endif
  99. #endif
  100. }
  101. static GDMono *get_singleton() {
  102. return singleton;
  103. }
  104. _FORCE_INLINE_ bool is_runtime_initialized() const {
  105. return runtime_initialized;
  106. }
  107. _FORCE_INLINE_ bool is_finalizing_scripts_domain() {
  108. return finalizing_scripts_domain;
  109. }
  110. _FORCE_INLINE_ const String &get_project_assembly_path() const {
  111. return project_assembly_path;
  112. }
  113. _FORCE_INLINE_ uint64_t get_project_assembly_modified_time() const {
  114. return project_assembly_modified_time;
  115. }
  116. #ifdef TOOLS_ENABLED
  117. const gdmono::PluginCallbacks &get_plugin_callbacks() {
  118. return plugin_callbacks;
  119. }
  120. #endif
  121. #ifdef GD_MONO_HOT_RELOAD
  122. Error reload_project_assemblies();
  123. #endif
  124. void initialize();
  125. #ifdef TOOLS_ENABLED
  126. void initialize_load_assemblies();
  127. #endif
  128. GDMono();
  129. ~GDMono();
  130. };
  131. namespace mono_bind {
  132. class GodotSharp : public Object {
  133. GDCLASS(GodotSharp, Object);
  134. friend class GDMono;
  135. void _reload_assemblies(bool p_soft_reload);
  136. bool _is_runtime_initialized();
  137. protected:
  138. static GodotSharp *singleton;
  139. static void _bind_methods();
  140. public:
  141. static GodotSharp *get_singleton() { return singleton; }
  142. GodotSharp();
  143. ~GodotSharp();
  144. };
  145. } // namespace mono_bind
  146. #endif // GD_MONO_H