godot_nativescript.cpp 12 KB

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