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