godot.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /*************************************************************************/
  2. /* godot_c.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #include "godot.h"
  30. #include <cassert>
  31. #include <cstdlib>
  32. #include "class_db.h"
  33. #include "dl_script.h"
  34. #include "global_config.h"
  35. #include "variant.h"
  36. #include <memory.h>
  37. #ifdef __cplusplus
  38. extern "C" {
  39. #endif
  40. extern "C" void _string_api_anchor();
  41. extern "C" void _vector2_api_anchor();
  42. extern "C" void _rect2_api_anchor();
  43. extern "C" void _vector3_api_anchor();
  44. extern "C" void _transform2d_api_anchor();
  45. extern "C" void _plane_api_anchor();
  46. extern "C" void _quat_api_anchor();
  47. extern "C" void _basis_api_anchor();
  48. extern "C" void _rect3_api_anchor();
  49. extern "C" void _transform_api_anchor();
  50. extern "C" void _color_api_anchor();
  51. extern "C" void _image_api_anchor();
  52. extern "C" void _node_path_api_anchor();
  53. extern "C" void _rid_api_anchor();
  54. extern "C" void _input_event_api_anchor();
  55. extern "C" void _dictionary_api_anchor();
  56. extern "C" void _array_api_anchor();
  57. extern "C" void _pool_arrays_api_anchor();
  58. extern "C" void _variant_api_anchor();
  59. void _api_anchor() {
  60. _string_api_anchor();
  61. _vector2_api_anchor();
  62. _rect2_api_anchor();
  63. _vector3_api_anchor();
  64. _transform2d_api_anchor();
  65. _plane_api_anchor();
  66. _quat_api_anchor();
  67. _rect3_api_anchor();
  68. _basis_api_anchor();
  69. _transform_api_anchor();
  70. _color_api_anchor();
  71. _image_api_anchor();
  72. _node_path_api_anchor();
  73. _rid_api_anchor();
  74. _input_event_api_anchor();
  75. _dictionary_api_anchor();
  76. _array_api_anchor();
  77. _pool_arrays_api_anchor();
  78. _variant_api_anchor();
  79. }
  80. extern "C++" {
  81. template <class a, class b>
  82. _FORCE_INLINE_ a memcast(b v) {
  83. return *((a *)&v);
  84. }
  85. }
  86. void GDAPI godot_object_destroy(godot_object *p_o) {
  87. memdelete((Object *)p_o);
  88. }
  89. // Singleton API
  90. godot_object GDAPI *godot_global_get_singleton(char *p_name) {
  91. return (godot_object *)GlobalConfig::get_singleton()->get_singleton_object(String(p_name));
  92. } // result shouldn't be freed
  93. // MethodBind API
  94. godot_method_bind GDAPI *godot_method_bind_get_method(const char *p_classname, const char *p_methodname) {
  95. MethodBind *mb = ClassDB::get_method(StringName(p_classname), StringName(p_methodname));
  96. // MethodBind *mb = ClassDB::get_method("Node", "get_name");
  97. return (godot_method_bind *)mb;
  98. }
  99. void GDAPI godot_method_bind_ptrcall(godot_method_bind *p_method_bind, godot_object *p_instance, const void **p_args, void *p_ret) {
  100. MethodBind *mb = (MethodBind *)p_method_bind;
  101. Object *o = (Object *)p_instance;
  102. mb->ptrcall(o, p_args, p_ret);
  103. }
  104. // @Todo
  105. /*
  106. void GDAPI godot_method_bind_varcall(godot_method_bind *p_method_bind)
  107. {
  108. }
  109. */
  110. // Script API
  111. void GDAPI godot_script_register_class(const char *p_name, const char *p_base, godot_instance_create_func p_create_func, godot_instance_destroy_func p_destroy_func) {
  112. DLLibrary *library = DLLibrary::get_currently_initialized_library();
  113. if (!library) {
  114. ERR_EXPLAIN("Attempt to register script after initializing library!");
  115. ERR_FAIL();
  116. }
  117. library->_register_script(p_name, p_base, p_create_func, p_destroy_func);
  118. }
  119. void GDAPI godot_script_register_tool_class(const char *p_name, const char *p_base, godot_instance_create_func p_create_func, godot_instance_destroy_func p_destroy_func) {
  120. DLLibrary *library = DLLibrary::get_currently_initialized_library();
  121. if (!library) {
  122. ERR_EXPLAIN("Attempt to register script after initializing library!");
  123. ERR_FAIL();
  124. }
  125. library->_register_tool_script(p_name, p_base, p_create_func, p_destroy_func);
  126. }
  127. void GDAPI godot_script_register_method(const char *p_name, const char *p_function_name, godot_method_attributes p_attr, godot_instance_method p_method) {
  128. DLLibrary *library = DLLibrary::get_currently_initialized_library();
  129. if (!library) {
  130. ERR_EXPLAIN("Attempt to register script after initializing library!");
  131. ERR_FAIL();
  132. }
  133. library->_register_script_method(p_name, p_function_name, p_attr, p_method, MethodInfo());
  134. }
  135. void GDAPI godot_script_register_property(const char *p_name, const char *p_path, godot_property_attributes *p_attr, godot_property_set_func p_set_func, godot_property_get_func p_get_func) {
  136. DLLibrary *library = DLLibrary::get_currently_initialized_library();
  137. if (!library) {
  138. ERR_EXPLAIN("Attempt to register script after initializing library!");
  139. ERR_FAIL();
  140. }
  141. library->_register_script_property(p_name, p_path, p_attr, p_set_func, p_get_func);
  142. }
  143. void GDAPI godot_script_register_signal(const char *p_name, const godot_signal *p_signal) {
  144. DLLibrary *library = DLLibrary::get_currently_initialized_library();
  145. if (!library) {
  146. ERR_EXPLAIN("Attempt to register script after initializing library!");
  147. ERR_FAIL();
  148. }
  149. library->_register_script_signal(p_name, p_signal);
  150. }
  151. // System functions
  152. void GDAPI *godot_alloc(int p_bytes) {
  153. return memalloc(p_bytes);
  154. }
  155. void GDAPI *godot_realloc(void *p_ptr, int p_bytes) {
  156. return memrealloc(p_ptr, p_bytes);
  157. }
  158. void GDAPI godot_free(void *p_ptr) {
  159. memfree(p_ptr);
  160. }
  161. #ifdef __cplusplus
  162. }
  163. #endif