gd_mono_internals.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*************************************************************************/
  2. /* gd_mono_internals.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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. #include "gd_mono_internals.h"
  31. #include "../csharp_script.h"
  32. #include "../mono_gc_handle.h"
  33. #include "../utils/macros.h"
  34. #include "gd_mono_class.h"
  35. #include "gd_mono_marshal.h"
  36. #include "gd_mono_utils.h"
  37. #include "core/debugger/engine_debugger.h"
  38. #include "core/debugger/script_debugger.h"
  39. #include <mono/metadata/exception.h>
  40. namespace GDMonoInternals {
  41. void tie_managed_to_unmanaged(MonoObject *managed, Object *unmanaged) {
  42. // This method should not fail
  43. CRASH_COND(!unmanaged);
  44. // All mono objects created from the managed world (e.g.: 'new Player()')
  45. // need to have a CSharpScript in order for their methods to be callable from the unmanaged side
  46. Reference *ref = Object::cast_to<Reference>(unmanaged);
  47. GDMonoClass *klass = GDMonoUtils::get_object_class(managed);
  48. CRASH_COND(!klass);
  49. GDMonoClass *native = GDMonoUtils::get_class_native_base(klass);
  50. CRASH_COND(native == nullptr);
  51. if (native == klass) {
  52. // If it's just a wrapper Godot class and not a custom inheriting class, then attach a
  53. // script binding instead. One of the advantages of this is that if a script is attached
  54. // later and it's not a C# script, then the managed object won't have to be disposed.
  55. // Another reason for doing this is that this instance could outlive CSharpLanguage, which would
  56. // be problematic when using a script. See: https://github.com/godotengine/godot/issues/25621
  57. CSharpScriptBinding script_binding;
  58. script_binding.inited = true;
  59. script_binding.type_name = NATIVE_GDMONOCLASS_NAME(klass);
  60. script_binding.wrapper_class = klass;
  61. script_binding.gchandle = ref ? MonoGCHandleData::new_weak_handle(managed) : MonoGCHandleData::new_strong_handle(managed);
  62. script_binding.owner = unmanaged;
  63. if (ref) {
  64. // Unsafe refcount increment. The managed instance also counts as a reference.
  65. // This way if the unmanaged world has no references to our owner
  66. // but the managed instance is alive, the refcount will be 1 instead of 0.
  67. // See: godot_icall_Reference_Dtor(MonoObject *p_obj, Object *p_ptr)
  68. // May not me referenced yet, so we must use init_ref() instead of reference()
  69. if (ref->init_ref()) {
  70. CSharpLanguage::get_singleton()->post_unsafe_reference(ref);
  71. }
  72. }
  73. // The object was just created, no script instance binding should have been attached
  74. CRASH_COND(unmanaged->has_script_instance_binding(CSharpLanguage::get_singleton()->get_language_index()));
  75. void *data = (void *)CSharpLanguage::get_singleton()->insert_script_binding(unmanaged, script_binding);
  76. // Should be thread safe because the object was just created and nothing else should be referencing it
  77. unmanaged->set_script_instance_binding(CSharpLanguage::get_singleton()->get_language_index(), data);
  78. return;
  79. }
  80. MonoGCHandleData gchandle = ref ? MonoGCHandleData::new_weak_handle(managed) : MonoGCHandleData::new_strong_handle(managed);
  81. Ref<CSharpScript> script = CSharpScript::create_for_managed_type(klass, native);
  82. CRASH_COND(script.is_null());
  83. CSharpInstance *csharp_instance = CSharpInstance::create_for_managed_type(unmanaged, script.ptr(), gchandle);
  84. unmanaged->set_script_and_instance(script, csharp_instance);
  85. csharp_instance->connect_event_signals();
  86. }
  87. void unhandled_exception(MonoException *p_exc) {
  88. mono_unhandled_exception((MonoObject *)p_exc); // prints the exception as well
  89. if (GDMono::get_singleton()->get_unhandled_exception_policy() == GDMono::POLICY_TERMINATE_APP) {
  90. // Too bad 'mono_invoke_unhandled_exception_hook' is not exposed to embedders
  91. GDMono::unhandled_exception_hook((MonoObject *)p_exc, nullptr);
  92. GD_UNREACHABLE();
  93. } else {
  94. #ifdef DEBUG_ENABLED
  95. GDMonoUtils::debug_send_unhandled_exception_error((MonoException *)p_exc);
  96. if (EngineDebugger::is_active())
  97. EngineDebugger::get_singleton()->poll_events(false);
  98. #endif
  99. }
  100. }
  101. } // namespace GDMonoInternals