godot_nativescript.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. /*************************************************************************/
  2. /* godot_nativescript.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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 "nativescript/godot_nativescript.h"
  31. #include "core/config/project_settings.h"
  32. #include "core/core_constants.h"
  33. #include "core/error/error_macros.h"
  34. #include "core/object/class_db.h"
  35. #include "core/variant/variant.h"
  36. #include "gdnative/gdnative.h"
  37. #include <stdint.h>
  38. #include "nativescript.h"
  39. #ifdef __cplusplus
  40. extern "C" {
  41. #endif
  42. extern "C" void _native_script_hook() {
  43. }
  44. #define NSL NativeScriptLanguage::get_singleton()
  45. // Script API
  46. void GDAPI godot_nativescript_register_class(void *p_gdnative_handle, const char *p_name, const char *p_base, godot_nativescript_instance_create_func p_create_func, godot_nativescript_instance_destroy_func p_destroy_func) {
  47. String *s = (String *)p_gdnative_handle;
  48. Map<StringName, NativeScriptDesc> *classes = &NSL->library_classes[*s];
  49. NativeScriptDesc desc;
  50. desc.create_func = p_create_func;
  51. desc.destroy_func = p_destroy_func;
  52. desc.is_tool = false;
  53. desc.base = p_base;
  54. if (classes->has(p_base)) {
  55. desc.base_data = &(*classes)[p_base];
  56. desc.base_native_type = desc.base_data->base_native_type;
  57. const NativeScriptDesc *b = desc.base_data;
  58. while (b) {
  59. desc.rpc_methods.append_array(b->rpc_methods);
  60. b = b->base_data;
  61. }
  62. } else {
  63. desc.base_data = nullptr;
  64. desc.base_native_type = p_base;
  65. }
  66. classes->insert(p_name, desc);
  67. }
  68. void GDAPI godot_nativescript_register_tool_class(void *p_gdnative_handle, const char *p_name, const char *p_base, godot_nativescript_instance_create_func p_create_func, godot_nativescript_instance_destroy_func p_destroy_func) {
  69. String *s = (String *)p_gdnative_handle;
  70. Map<StringName, NativeScriptDesc> *classes = &NSL->library_classes[*s];
  71. NativeScriptDesc desc;
  72. desc.create_func = p_create_func;
  73. desc.destroy_func = p_destroy_func;
  74. desc.is_tool = true;
  75. desc.base = p_base;
  76. if (classes->has(p_base)) {
  77. desc.base_data = &(*classes)[p_base];
  78. desc.base_native_type = desc.base_data->base_native_type;
  79. const NativeScriptDesc *b = desc.base_data;
  80. while (b) {
  81. desc.rpc_methods.append_array(b->rpc_methods);
  82. b = b->base_data;
  83. }
  84. } else {
  85. desc.base_data = nullptr;
  86. desc.base_native_type = p_base;
  87. }
  88. classes->insert(p_name, desc);
  89. }
  90. void GDAPI godot_nativescript_register_method(void *p_gdnative_handle, const char *p_name, const char *p_function_name, godot_nativescript_method_attributes p_attr, godot_nativescript_instance_method p_method) {
  91. String *s = (String *)p_gdnative_handle;
  92. Map<StringName, NativeScriptDesc>::Element *E = NSL->library_classes[*s].find(p_name);
  93. ERR_FAIL_COND_MSG(!E, "Attempted to register method on non-existent class.");
  94. NativeScriptDesc::Method method;
  95. method.method = p_method;
  96. method.rpc_mode = p_attr.rpc_type;
  97. method.rpc_method_id = UINT16_MAX;
  98. method.info = MethodInfo(p_function_name);
  99. E->get().methods.insert(p_function_name, method);
  100. if (p_attr.rpc_type != GODOT_METHOD_RPC_MODE_DISABLED) {
  101. MultiplayerAPI::RPCConfig nd;
  102. nd.name = String(p_name);
  103. nd.rpc_mode = MultiplayerAPI::RPCMode(p_attr.rpc_type);
  104. E->get().rpc_methods.push_back(nd);
  105. }
  106. }
  107. void GDAPI godot_nativescript_register_property(void *p_gdnative_handle, const char *p_name, const char *p_path, godot_nativescript_property_attributes *p_attr, godot_nativescript_property_set_func p_set_func, godot_nativescript_property_get_func p_get_func) {
  108. String *s = (String *)p_gdnative_handle;
  109. Map<StringName, NativeScriptDesc>::Element *E = NSL->library_classes[*s].find(p_name);
  110. ERR_FAIL_COND_MSG(!E, "Attempted to register method on non-existent class.");
  111. NativeScriptDesc::Property property;
  112. property.default_value = *(Variant *)&p_attr->default_value;
  113. property.getter = p_get_func;
  114. property.setter = p_set_func;
  115. property.info = PropertyInfo((Variant::Type)p_attr->type,
  116. p_path,
  117. (PropertyHint)p_attr->hint,
  118. *(String *)&p_attr->hint_string,
  119. (PropertyUsageFlags)p_attr->usage);
  120. E->get().properties.insert(p_path, property);
  121. }
  122. void GDAPI godot_nativescript_register_signal(void *p_gdnative_handle, const char *p_name, const godot_nativescript_signal *p_signal) {
  123. String *s = (String *)p_gdnative_handle;
  124. Map<StringName, NativeScriptDesc>::Element *E = NSL->library_classes[*s].find(p_name);
  125. ERR_FAIL_COND_MSG(!E, "Attempted to register method on non-existent class.");
  126. List<PropertyInfo> args;
  127. Vector<Variant> default_args;
  128. for (int i = 0; i < p_signal->num_args; i++) {
  129. PropertyInfo info;
  130. godot_nativescript_signal_argument arg = p_signal->args[i];
  131. info.hint = (PropertyHint)arg.hint;
  132. info.hint_string = *(String *)&arg.hint_string;
  133. info.name = *(String *)&arg.name;
  134. info.type = (Variant::Type)arg.type;
  135. info.usage = (PropertyUsageFlags)arg.usage;
  136. args.push_back(info);
  137. }
  138. for (int i = 0; i < p_signal->num_default_args; i++) {
  139. Variant *v;
  140. godot_nativescript_signal_argument attrib = p_signal->args[i];
  141. v = (Variant *)&attrib.default_value;
  142. default_args.push_back(*v);
  143. }
  144. MethodInfo method_info;
  145. method_info.name = *(String *)&p_signal->name;
  146. method_info.arguments = args;
  147. method_info.default_arguments = default_args;
  148. NativeScriptDesc::Signal signal;
  149. signal.signal = method_info;
  150. E->get().signals_.insert(*(String *)&p_signal->name, signal);
  151. }
  152. void GDAPI *godot_nativescript_get_userdata(godot_object *p_instance) {
  153. Object *instance = (Object *)p_instance;
  154. if (!instance) {
  155. return nullptr;
  156. }
  157. if (instance->get_script_instance() && instance->get_script_instance()->get_language() == NativeScriptLanguage::get_singleton()) {
  158. return ((NativeScriptInstance *)instance->get_script_instance())->userdata;
  159. }
  160. return nullptr;
  161. }
  162. /*
  163. *
  164. *
  165. * NativeScript 1.1
  166. *
  167. *
  168. */
  169. void GDAPI godot_nativescript_set_method_argument_information(void *p_gdnative_handle, const char *p_name, const char *p_function_name, int p_num_args, const godot_nativescript_method_argument *p_args) {
  170. String *s = (String *)p_gdnative_handle;
  171. Map<StringName, NativeScriptDesc>::Element *E = NSL->library_classes[*s].find(p_name);
  172. ERR_FAIL_COND_MSG(!E, "Attempted to add argument information for a method on a non-existent class.");
  173. Map<StringName, NativeScriptDesc::Method>::Element *method = E->get().methods.find(p_function_name);
  174. ERR_FAIL_COND_MSG(!method, "Attempted to add argument information to non-existent method.");
  175. MethodInfo *method_information = &method->get().info;
  176. List<PropertyInfo> args;
  177. for (int i = 0; i < p_num_args; i++) {
  178. godot_nativescript_method_argument arg = p_args[i];
  179. String name = *(String *)&arg.name;
  180. String hint_string = *(String *)&arg.hint_string;
  181. Variant::Type type = (Variant::Type)arg.type;
  182. PropertyHint hint = (PropertyHint)arg.hint;
  183. args.push_back(PropertyInfo(type, p_name, hint, hint_string));
  184. }
  185. method_information->arguments = args;
  186. }
  187. void GDAPI godot_nativescript_set_class_documentation(void *p_gdnative_handle, const char *p_name, godot_string p_documentation) {
  188. String *s = (String *)p_gdnative_handle;
  189. Map<StringName, NativeScriptDesc>::Element *E = NSL->library_classes[*s].find(p_name);
  190. ERR_FAIL_COND_MSG(!E, "Attempted to add documentation to a non-existent class.");
  191. E->get().documentation = *(String *)&p_documentation;
  192. }
  193. void GDAPI godot_nativescript_set_method_documentation(void *p_gdnative_handle, const char *p_name, const char *p_function_name, godot_string p_documentation) {
  194. String *s = (String *)p_gdnative_handle;
  195. Map<StringName, NativeScriptDesc>::Element *E = NSL->library_classes[*s].find(p_name);
  196. ERR_FAIL_COND_MSG(!E, "Attempted to add documentation to a method on a non-existent class.");
  197. Map<StringName, NativeScriptDesc::Method>::Element *method = E->get().methods.find(p_function_name);
  198. ERR_FAIL_COND_MSG(!method, "Attempted to add documentation to non-existent method.");
  199. method->get().documentation = *(String *)&p_documentation;
  200. }
  201. void GDAPI godot_nativescript_set_property_documentation(void *p_gdnative_handle, const char *p_name, const char *p_path, godot_string p_documentation) {
  202. String *s = (String *)p_gdnative_handle;
  203. Map<StringName, NativeScriptDesc>::Element *E = NSL->library_classes[*s].find(p_name);
  204. ERR_FAIL_COND_MSG(!E, "Attempted to add documentation to a property on a non-existent class.");
  205. OrderedHashMap<StringName, NativeScriptDesc::Property>::Element property = E->get().properties.find(p_path);
  206. ERR_FAIL_COND_MSG(!property, "Attempted to add documentation to non-existent property.");
  207. property.get().documentation = *(String *)&p_documentation;
  208. }
  209. void GDAPI godot_nativescript_set_signal_documentation(void *p_gdnative_handle, const char *p_name, const char *p_signal_name, godot_string p_documentation) {
  210. String *s = (String *)p_gdnative_handle;
  211. Map<StringName, NativeScriptDesc>::Element *E = NSL->library_classes[*s].find(p_name);
  212. ERR_FAIL_COND_MSG(!E, "Attempted to add documentation to a signal on a non-existent class.");
  213. Map<StringName, NativeScriptDesc::Signal>::Element *signal = E->get().signals_.find(p_signal_name);
  214. ERR_FAIL_COND_MSG(!signal, "Attempted to add documentation to non-existent signal.");
  215. signal->get().documentation = *(String *)&p_documentation;
  216. }
  217. void GDAPI godot_nativescript_set_global_type_tag(int p_idx, const char *p_name, const void *p_type_tag) {
  218. NativeScriptLanguage::get_singleton()->set_global_type_tag(p_idx, StringName(p_name), p_type_tag);
  219. }
  220. const void GDAPI *godot_nativescript_get_global_type_tag(int p_idx, const char *p_name) {
  221. return NativeScriptLanguage::get_singleton()->get_global_type_tag(p_idx, StringName(p_name));
  222. }
  223. void GDAPI godot_nativescript_set_type_tag(void *p_gdnative_handle, const char *p_name, const void *p_type_tag) {
  224. String *s = (String *)p_gdnative_handle;
  225. Map<StringName, NativeScriptDesc>::Element *E = NSL->library_classes[*s].find(p_name);
  226. ERR_FAIL_COND_MSG(!E, "Attempted to set type tag on a non-existent class.");
  227. E->get().type_tag = p_type_tag;
  228. }
  229. const void GDAPI *godot_nativescript_get_type_tag(const godot_object *p_object) {
  230. const Object *o = (Object *)p_object;
  231. if (!o->get_script_instance()) {
  232. return nullptr;
  233. } else {
  234. NativeScript *script = Object::cast_to<NativeScript>(o->get_script_instance()->get_script().ptr());
  235. if (!script) {
  236. return nullptr;
  237. }
  238. if (script->get_script_desc()) {
  239. return script->get_script_desc()->type_tag;
  240. }
  241. }
  242. return nullptr;
  243. }
  244. int GDAPI godot_nativescript_register_instance_binding_data_functions(godot_nativescript_instance_binding_functions p_binding_functions) {
  245. return NativeScriptLanguage::get_singleton()->register_binding_functions(p_binding_functions);
  246. }
  247. void GDAPI godot_nativescript_unregister_instance_binding_data_functions(int p_idx) {
  248. NativeScriptLanguage::get_singleton()->unregister_binding_functions(p_idx);
  249. }
  250. void GDAPI *godot_nativescript_get_instance_binding_data(int p_idx, godot_object *p_object) {
  251. return nullptr;
  252. }
  253. void GDAPI godot_nativescript_profiling_add_data(const char *p_signature, uint64_t p_time) {
  254. NativeScriptLanguage::get_singleton()->profiling_add_data(StringName(p_signature), p_time);
  255. }
  256. #ifdef __cplusplus
  257. }
  258. #endif