nativescript.cpp 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792
  1. /*************************************************************************/
  2. /* 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.h"
  31. #include "gdnative/gdnative.h"
  32. #include "core/core_string_names.h"
  33. #include "core/global_constants.h"
  34. #include "core/io/file_access_encrypted.h"
  35. #include "core/os/file_access.h"
  36. #include "core/os/os.h"
  37. #include "core/project_settings.h"
  38. #include "scene/main/scene_tree.h"
  39. #include "scene/resources/resource_format_text.h"
  40. #include <stdlib.h>
  41. #ifndef NO_THREADS
  42. #include "core/os/thread.h"
  43. #endif
  44. #if defined(TOOLS_ENABLED) && defined(DEBUG_METHODS_ENABLED)
  45. #include "api_generator.h"
  46. #endif
  47. #ifdef TOOLS_ENABLED
  48. #include "editor/editor_node.h"
  49. #endif
  50. void NativeScript::_bind_methods() {
  51. ClassDB::bind_method(D_METHOD("set_class_name", "class_name"), &NativeScript::set_class_name);
  52. ClassDB::bind_method(D_METHOD("get_class_name"), &NativeScript::get_class_name);
  53. ClassDB::bind_method(D_METHOD("set_library", "library"), &NativeScript::set_library);
  54. ClassDB::bind_method(D_METHOD("get_library"), &NativeScript::get_library);
  55. ClassDB::bind_method(D_METHOD("set_script_class_name", "class_name"), &NativeScript::set_script_class_name);
  56. ClassDB::bind_method(D_METHOD("get_script_class_name"), &NativeScript::get_script_class_name);
  57. ClassDB::bind_method(D_METHOD("set_script_class_icon_path", "icon_path"), &NativeScript::set_script_class_icon_path);
  58. ClassDB::bind_method(D_METHOD("get_script_class_icon_path"), &NativeScript::get_script_class_icon_path);
  59. ClassDB::bind_method(D_METHOD("get_class_documentation"), &NativeScript::get_class_documentation);
  60. ClassDB::bind_method(D_METHOD("get_method_documentation", "method"), &NativeScript::get_method_documentation);
  61. ClassDB::bind_method(D_METHOD("get_signal_documentation", "signal_name"), &NativeScript::get_signal_documentation);
  62. ClassDB::bind_method(D_METHOD("get_property_documentation", "path"), &NativeScript::get_property_documentation);
  63. ADD_PROPERTY(PropertyInfo(Variant::STRING, "class_name"), "set_class_name", "get_class_name");
  64. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "library", PROPERTY_HINT_RESOURCE_TYPE, "GDNativeLibrary"), "set_library", "get_library");
  65. ADD_GROUP("Script Class", "script_class_");
  66. ADD_PROPERTY(PropertyInfo(Variant::STRING, "script_class_name"), "set_script_class_name", "get_script_class_name");
  67. ADD_PROPERTY(PropertyInfo(Variant::STRING, "script_class_icon_path", PROPERTY_HINT_FILE), "set_script_class_icon_path", "get_script_class_icon_path");
  68. ClassDB::bind_vararg_method(METHOD_FLAGS_DEFAULT, "new", &NativeScript::_new, MethodInfo(Variant::OBJECT, "new"));
  69. }
  70. #define NSL NativeScriptLanguage::get_singleton()
  71. #ifdef TOOLS_ENABLED
  72. void NativeScript::_update_placeholder(PlaceHolderScriptInstance *p_placeholder) {
  73. NativeScriptDesc *script_data = get_script_desc();
  74. ERR_FAIL_COND(!script_data);
  75. List<PropertyInfo> info;
  76. get_script_property_list(&info);
  77. Map<StringName, Variant> values;
  78. for (List<PropertyInfo>::Element *E = info.front(); E; E = E->next()) {
  79. Variant value;
  80. get_property_default_value(E->get().name, value);
  81. values[E->get().name] = value;
  82. }
  83. p_placeholder->update(info, values);
  84. }
  85. void NativeScript::_placeholder_erased(PlaceHolderScriptInstance *p_placeholder) {
  86. placeholders.erase(p_placeholder);
  87. }
  88. #endif
  89. void NativeScript::set_class_name(String p_class_name) {
  90. class_name = p_class_name;
  91. }
  92. String NativeScript::get_class_name() const {
  93. return class_name;
  94. }
  95. void NativeScript::set_library(Ref<GDNativeLibrary> p_library) {
  96. if (!library.is_null()) {
  97. WARN_PRINT("library on NativeScript already set. Do nothing.");
  98. return;
  99. }
  100. library = p_library;
  101. lib_path = library->get_current_library_path();
  102. #ifndef NO_THREADS
  103. if (Thread::get_caller_id() != Thread::get_main_id()) {
  104. NSL->defer_init_library(p_library, this);
  105. } else
  106. #endif
  107. {
  108. NSL->init_library(p_library);
  109. NSL->register_script(this);
  110. }
  111. }
  112. Ref<GDNativeLibrary> NativeScript::get_library() const {
  113. return library;
  114. }
  115. void NativeScript::set_script_class_name(String p_type) {
  116. script_class_name = p_type;
  117. }
  118. String NativeScript::get_script_class_name() const {
  119. return script_class_name;
  120. }
  121. void NativeScript::set_script_class_icon_path(String p_icon_path) {
  122. script_class_icon_path = p_icon_path;
  123. }
  124. String NativeScript::get_script_class_icon_path() const {
  125. return script_class_icon_path;
  126. }
  127. bool NativeScript::can_instance() const {
  128. NativeScriptDesc *script_data = get_script_desc();
  129. #ifdef TOOLS_ENABLED
  130. // Only valid if this is either a tool script or a "regular" script.
  131. // (so an environment whre scripting is disabled (and not the editor) would not
  132. // create objects).
  133. return script_data && (is_tool() || ScriptServer::is_scripting_enabled());
  134. #else
  135. return script_data;
  136. #endif
  137. }
  138. Ref<Script> NativeScript::get_base_script() const {
  139. NativeScriptDesc *script_data = get_script_desc();
  140. if (!script_data)
  141. return Ref<Script>();
  142. NativeScript *script = (NativeScript *)NSL->create_script();
  143. Ref<NativeScript> ns = Ref<NativeScript>(script);
  144. ERR_FAIL_COND_V(!ns.is_valid(), Ref<Script>());
  145. ns->set_class_name(script_data->base);
  146. ns->set_library(get_library());
  147. return ns;
  148. }
  149. StringName NativeScript::get_instance_base_type() const {
  150. NativeScriptDesc *script_data = get_script_desc();
  151. if (!script_data)
  152. return "";
  153. return script_data->base_native_type;
  154. }
  155. ScriptInstance *NativeScript::instance_create(Object *p_this) {
  156. NativeScriptDesc *script_data = get_script_desc();
  157. if (!script_data) {
  158. return NULL;
  159. }
  160. NativeScriptInstance *nsi = memnew(NativeScriptInstance);
  161. nsi->owner = p_this;
  162. nsi->script = Ref<NativeScript>(this);
  163. #ifndef TOOLS_ENABLED
  164. if (!ScriptServer::is_scripting_enabled()) {
  165. nsi->userdata = NULL;
  166. } else {
  167. nsi->userdata = script_data->create_func.create_func((godot_object *)p_this, script_data->create_func.method_data);
  168. }
  169. #else
  170. nsi->userdata = script_data->create_func.create_func((godot_object *)p_this, script_data->create_func.method_data);
  171. #endif
  172. #ifndef NO_THREADS
  173. owners_lock->lock();
  174. #endif
  175. instance_owners.insert(p_this);
  176. #ifndef NO_THREADS
  177. owners_lock->unlock();
  178. #endif
  179. return nsi;
  180. }
  181. PlaceHolderScriptInstance *NativeScript::placeholder_instance_create(Object *p_this) {
  182. #ifdef TOOLS_ENABLED
  183. PlaceHolderScriptInstance *sins = memnew(PlaceHolderScriptInstance(NSL, Ref<Script>(this), p_this));
  184. placeholders.insert(sins);
  185. _update_placeholder(sins);
  186. return sins;
  187. #else
  188. return NULL;
  189. #endif
  190. }
  191. bool NativeScript::instance_has(const Object *p_this) const {
  192. return instance_owners.has((Object *)p_this);
  193. }
  194. bool NativeScript::has_source_code() const {
  195. return false;
  196. }
  197. String NativeScript::get_source_code() const {
  198. return "";
  199. }
  200. void NativeScript::set_source_code(const String &p_code) {
  201. }
  202. Error NativeScript::reload(bool p_keep_state) {
  203. return FAILED;
  204. }
  205. bool NativeScript::has_method(const StringName &p_method) const {
  206. NativeScriptDesc *script_data = get_script_desc();
  207. while (script_data) {
  208. if (script_data->methods.has(p_method))
  209. return true;
  210. script_data = script_data->base_data;
  211. }
  212. return false;
  213. }
  214. MethodInfo NativeScript::get_method_info(const StringName &p_method) const {
  215. NativeScriptDesc *script_data = get_script_desc();
  216. if (!script_data)
  217. return MethodInfo();
  218. while (script_data) {
  219. Map<StringName, NativeScriptDesc::Method>::Element *M = script_data->methods.find(p_method);
  220. if (M)
  221. return M->get().info;
  222. script_data = script_data->base_data;
  223. }
  224. return MethodInfo();
  225. }
  226. bool NativeScript::is_valid() const {
  227. return true;
  228. }
  229. bool NativeScript::is_tool() const {
  230. NativeScriptDesc *script_data = get_script_desc();
  231. if (script_data)
  232. return script_data->is_tool;
  233. return false;
  234. }
  235. ScriptLanguage *NativeScript::get_language() const {
  236. return NativeScriptLanguage::get_singleton();
  237. }
  238. bool NativeScript::has_script_signal(const StringName &p_signal) const {
  239. NativeScriptDesc *script_data = get_script_desc();
  240. while (script_data) {
  241. if (script_data->signals_.has(p_signal))
  242. return true;
  243. script_data = script_data->base_data;
  244. }
  245. return false;
  246. }
  247. void NativeScript::get_script_signal_list(List<MethodInfo> *r_signals) const {
  248. NativeScriptDesc *script_data = get_script_desc();
  249. if (!script_data)
  250. return;
  251. Set<MethodInfo> signals_;
  252. while (script_data) {
  253. for (Map<StringName, NativeScriptDesc::Signal>::Element *S = script_data->signals_.front(); S; S = S->next()) {
  254. signals_.insert(S->get().signal);
  255. }
  256. script_data = script_data->base_data;
  257. }
  258. for (Set<MethodInfo>::Element *E = signals_.front(); E; E = E->next()) {
  259. r_signals->push_back(E->get());
  260. }
  261. }
  262. bool NativeScript::get_property_default_value(const StringName &p_property, Variant &r_value) const {
  263. NativeScriptDesc *script_data = get_script_desc();
  264. OrderedHashMap<StringName, NativeScriptDesc::Property>::Element P;
  265. while (!P && script_data) {
  266. P = script_data->properties.find(p_property);
  267. script_data = script_data->base_data;
  268. }
  269. if (!P)
  270. return false;
  271. r_value = P.get().default_value;
  272. return true;
  273. }
  274. void NativeScript::update_exports() {
  275. }
  276. void NativeScript::get_script_method_list(List<MethodInfo> *p_list) const {
  277. NativeScriptDesc *script_data = get_script_desc();
  278. if (!script_data)
  279. return;
  280. Set<MethodInfo> methods;
  281. while (script_data) {
  282. for (Map<StringName, NativeScriptDesc::Method>::Element *E = script_data->methods.front(); E; E = E->next()) {
  283. methods.insert(E->get().info);
  284. }
  285. script_data = script_data->base_data;
  286. }
  287. for (Set<MethodInfo>::Element *E = methods.front(); E; E = E->next()) {
  288. p_list->push_back(E->get());
  289. }
  290. }
  291. void NativeScript::get_script_property_list(List<PropertyInfo> *p_list) const {
  292. NativeScriptDesc *script_data = get_script_desc();
  293. Set<StringName> existing_properties;
  294. List<PropertyInfo>::Element *original_back = p_list->back();
  295. while (script_data) {
  296. List<PropertyInfo>::Element *insert_position = original_back;
  297. for (OrderedHashMap<StringName, NativeScriptDesc::Property>::Element E = script_data->properties.front(); E; E = E.next()) {
  298. if (!existing_properties.has(E.key())) {
  299. insert_position = p_list->insert_after(insert_position, E.get().info);
  300. existing_properties.insert(E.key());
  301. }
  302. }
  303. script_data = script_data->base_data;
  304. }
  305. }
  306. String NativeScript::get_class_documentation() const {
  307. NativeScriptDesc *script_data = get_script_desc();
  308. if (!script_data) {
  309. ERR_EXPLAIN("Attempt to get class documentation on invalid NativeScript");
  310. ERR_FAIL_V("");
  311. }
  312. return script_data->documentation;
  313. }
  314. String NativeScript::get_method_documentation(const StringName &p_method) const {
  315. NativeScriptDesc *script_data = get_script_desc();
  316. if (!script_data) {
  317. ERR_EXPLAIN("Attempt to get method documentation on invalid NativeScript");
  318. ERR_FAIL_V("");
  319. }
  320. while (script_data) {
  321. Map<StringName, NativeScriptDesc::Method>::Element *method = script_data->methods.find(p_method);
  322. if (method) {
  323. return method->get().documentation;
  324. }
  325. script_data = script_data->base_data;
  326. }
  327. ERR_EXPLAIN("Attempt to get method documentation for non-existent method");
  328. ERR_FAIL_V("");
  329. }
  330. String NativeScript::get_signal_documentation(const StringName &p_signal_name) const {
  331. NativeScriptDesc *script_data = get_script_desc();
  332. if (!script_data) {
  333. ERR_EXPLAIN("Attempt to get signal documentation on invalid NativeScript");
  334. ERR_FAIL_V("");
  335. }
  336. while (script_data) {
  337. Map<StringName, NativeScriptDesc::Signal>::Element *signal = script_data->signals_.find(p_signal_name);
  338. if (signal) {
  339. return signal->get().documentation;
  340. }
  341. script_data = script_data->base_data;
  342. }
  343. ERR_EXPLAIN("Attempt to get signal documentation for non-existent signal");
  344. ERR_FAIL_V("");
  345. }
  346. String NativeScript::get_property_documentation(const StringName &p_path) const {
  347. NativeScriptDesc *script_data = get_script_desc();
  348. if (!script_data) {
  349. ERR_EXPLAIN("Attempt to get property documentation on invalid NativeScript");
  350. ERR_FAIL_V("");
  351. }
  352. while (script_data) {
  353. OrderedHashMap<StringName, NativeScriptDesc::Property>::Element property = script_data->properties.find(p_path);
  354. if (property) {
  355. return property.get().documentation;
  356. }
  357. script_data = script_data->base_data;
  358. }
  359. ERR_EXPLAIN("Attempt to get property documentation for non-existent signal");
  360. ERR_FAIL_V("");
  361. }
  362. Variant NativeScript::_new(const Variant **p_args, int p_argcount, Variant::CallError &r_error) {
  363. if (lib_path.empty() || class_name.empty() || library.is_null()) {
  364. r_error.error = Variant::CallError::CALL_ERROR_INSTANCE_IS_NULL;
  365. return Variant();
  366. }
  367. NativeScriptDesc *script_data = get_script_desc();
  368. if (!script_data) {
  369. r_error.error = Variant::CallError::CALL_ERROR_INSTANCE_IS_NULL;
  370. return Variant();
  371. }
  372. r_error.error = Variant::CallError::CALL_OK;
  373. REF ref;
  374. Object *owner = NULL;
  375. if (!(script_data->base_native_type == "")) {
  376. owner = ClassDB::instance(script_data->base_native_type);
  377. } else {
  378. owner = memnew(Reference);
  379. }
  380. if (!owner) {
  381. r_error.error = Variant::CallError::CALL_ERROR_INSTANCE_IS_NULL;
  382. return Variant();
  383. }
  384. Reference *r = Object::cast_to<Reference>(owner);
  385. if (r) {
  386. ref = REF(r);
  387. }
  388. NativeScriptInstance *instance = (NativeScriptInstance *)instance_create(owner);
  389. owner->set_script_instance(instance);
  390. if (!instance) {
  391. if (ref.is_null()) {
  392. memdelete(owner); //no owner, sorry
  393. }
  394. return Variant();
  395. }
  396. if (ref.is_valid()) {
  397. return ref;
  398. } else {
  399. return owner;
  400. }
  401. }
  402. NativeScript::NativeScript() {
  403. library = Ref<GDNative>();
  404. lib_path = "";
  405. class_name = "";
  406. #ifndef NO_THREADS
  407. owners_lock = Mutex::create();
  408. #endif
  409. }
  410. NativeScript::~NativeScript() {
  411. NSL->unregister_script(this);
  412. #ifndef NO_THREADS
  413. memdelete(owners_lock);
  414. #endif
  415. }
  416. #define GET_SCRIPT_DESC() script->get_script_desc()
  417. void NativeScriptInstance::_ml_call_reversed(NativeScriptDesc *script_data, const StringName &p_method, const Variant **p_args, int p_argcount) {
  418. if (script_data->base_data) {
  419. _ml_call_reversed(script_data->base_data, p_method, p_args, p_argcount);
  420. }
  421. Map<StringName, NativeScriptDesc::Method>::Element *E = script_data->methods.find(p_method);
  422. if (E) {
  423. godot_variant res = E->get().method.method((godot_object *)owner, E->get().method.method_data, userdata, p_argcount, (godot_variant **)p_args);
  424. godot_variant_destroy(&res);
  425. }
  426. }
  427. bool NativeScriptInstance::set(const StringName &p_name, const Variant &p_value) {
  428. NativeScriptDesc *script_data = GET_SCRIPT_DESC();
  429. while (script_data) {
  430. OrderedHashMap<StringName, NativeScriptDesc::Property>::Element P = script_data->properties.find(p_name);
  431. if (P) {
  432. P.get().setter.set_func((godot_object *)owner,
  433. P.get().setter.method_data,
  434. userdata,
  435. (godot_variant *)&p_value);
  436. return true;
  437. }
  438. Map<StringName, NativeScriptDesc::Method>::Element *E = script_data->methods.find("_set");
  439. if (E) {
  440. Variant name = p_name;
  441. const Variant *args[2] = { &name, &p_value };
  442. godot_variant result;
  443. result = E->get().method.method((godot_object *)owner,
  444. E->get().method.method_data,
  445. userdata,
  446. 2,
  447. (godot_variant **)args);
  448. bool handled = *(Variant *)&result;
  449. godot_variant_destroy(&result);
  450. if (handled) {
  451. return true;
  452. }
  453. }
  454. script_data = script_data->base_data;
  455. }
  456. return false;
  457. }
  458. bool NativeScriptInstance::get(const StringName &p_name, Variant &r_ret) const {
  459. NativeScriptDesc *script_data = GET_SCRIPT_DESC();
  460. while (script_data) {
  461. OrderedHashMap<StringName, NativeScriptDesc::Property>::Element P = script_data->properties.find(p_name);
  462. if (P) {
  463. godot_variant value;
  464. value = P.get().getter.get_func((godot_object *)owner,
  465. P.get().getter.method_data,
  466. userdata);
  467. r_ret = *(Variant *)&value;
  468. godot_variant_destroy(&value);
  469. return true;
  470. }
  471. Map<StringName, NativeScriptDesc::Method>::Element *E = script_data->methods.find("_get");
  472. if (E) {
  473. Variant name = p_name;
  474. const Variant *args[1] = { &name };
  475. godot_variant result;
  476. result = E->get().method.method((godot_object *)owner,
  477. E->get().method.method_data,
  478. userdata,
  479. 1,
  480. (godot_variant **)args);
  481. r_ret = *(Variant *)&result;
  482. godot_variant_destroy(&result);
  483. if (r_ret.get_type() != Variant::NIL) {
  484. return true;
  485. }
  486. }
  487. script_data = script_data->base_data;
  488. }
  489. return false;
  490. }
  491. void NativeScriptInstance::get_property_list(List<PropertyInfo> *p_properties) const {
  492. script->get_script_property_list(p_properties);
  493. NativeScriptDesc *script_data = GET_SCRIPT_DESC();
  494. while (script_data) {
  495. Map<StringName, NativeScriptDesc::Method>::Element *E = script_data->methods.find("_get_property_list");
  496. if (E) {
  497. godot_variant result;
  498. result = E->get().method.method((godot_object *)owner,
  499. E->get().method.method_data,
  500. userdata,
  501. 0,
  502. NULL);
  503. Variant res = *(Variant *)&result;
  504. godot_variant_destroy(&result);
  505. if (res.get_type() != Variant::ARRAY) {
  506. ERR_EXPLAIN("_get_property_list must return an array of dictionaries");
  507. ERR_FAIL();
  508. }
  509. Array arr = res;
  510. for (int i = 0; i < arr.size(); i++) {
  511. Dictionary d = arr[i];
  512. ERR_CONTINUE(!d.has("name"));
  513. ERR_CONTINUE(!d.has("type"));
  514. PropertyInfo info;
  515. info.type = Variant::Type(d["type"].operator int64_t());
  516. ERR_CONTINUE(info.type < 0 || info.type >= Variant::VARIANT_MAX);
  517. info.name = d["name"];
  518. ERR_CONTINUE(info.name == "");
  519. if (d.has("hint")) {
  520. info.hint = PropertyHint(d["hint"].operator int64_t());
  521. }
  522. if (d.has("hint_string")) {
  523. info.hint_string = d["hint_string"];
  524. }
  525. if (d.has("usage")) {
  526. info.usage = d["usage"];
  527. }
  528. p_properties->push_back(info);
  529. }
  530. }
  531. script_data = script_data->base_data;
  532. }
  533. return;
  534. }
  535. Variant::Type NativeScriptInstance::get_property_type(const StringName &p_name, bool *r_is_valid) const {
  536. NativeScriptDesc *script_data = GET_SCRIPT_DESC();
  537. while (script_data) {
  538. OrderedHashMap<StringName, NativeScriptDesc::Property>::Element P = script_data->properties.find(p_name);
  539. if (P) {
  540. *r_is_valid = true;
  541. return P.get().info.type;
  542. }
  543. script_data = script_data->base_data;
  544. }
  545. return Variant::NIL;
  546. }
  547. void NativeScriptInstance::get_method_list(List<MethodInfo> *p_list) const {
  548. script->get_script_method_list(p_list);
  549. }
  550. bool NativeScriptInstance::has_method(const StringName &p_method) const {
  551. return script->has_method(p_method);
  552. }
  553. Variant NativeScriptInstance::call(const StringName &p_method, const Variant **p_args, int p_argcount, Variant::CallError &r_error) {
  554. NativeScriptDesc *script_data = GET_SCRIPT_DESC();
  555. while (script_data) {
  556. Map<StringName, NativeScriptDesc::Method>::Element *E = script_data->methods.find(p_method);
  557. if (E) {
  558. godot_variant result;
  559. #ifdef DEBUG_ENABLED
  560. current_method_call = p_method;
  561. #endif
  562. result = E->get().method.method((godot_object *)owner,
  563. E->get().method.method_data,
  564. userdata,
  565. p_argcount,
  566. (godot_variant **)p_args);
  567. #ifdef DEBUG_ENABLED
  568. current_method_call = "";
  569. #endif
  570. Variant res = *(Variant *)&result;
  571. godot_variant_destroy(&result);
  572. r_error.error = Variant::CallError::CALL_OK;
  573. return res;
  574. }
  575. script_data = script_data->base_data;
  576. }
  577. r_error.error = Variant::CallError::CALL_ERROR_INVALID_METHOD;
  578. return Variant();
  579. }
  580. void NativeScriptInstance::notification(int p_notification) {
  581. #ifdef DEBUG_ENABLED
  582. if (p_notification == MainLoop::NOTIFICATION_CRASH) {
  583. if (current_method_call != StringName("")) {
  584. ERR_PRINTS("NativeScriptInstance detected crash on method: " + current_method_call);
  585. current_method_call = "";
  586. }
  587. }
  588. #endif
  589. Variant value = p_notification;
  590. const Variant *args[1] = { &value };
  591. call_multilevel("_notification", args, 1);
  592. }
  593. String NativeScriptInstance::to_string(bool *r_valid) {
  594. if (has_method(CoreStringNames::get_singleton()->_to_string)) {
  595. Variant::CallError ce;
  596. Variant ret = call(CoreStringNames::get_singleton()->_to_string, NULL, 0, ce);
  597. if (ce.error == Variant::CallError::CALL_OK) {
  598. if (ret.get_type() != Variant::STRING) {
  599. if (r_valid)
  600. *r_valid = false;
  601. ERR_EXPLAIN("Wrong type for " + CoreStringNames::get_singleton()->_to_string + ", must be a String.");
  602. ERR_FAIL_V(String());
  603. }
  604. if (r_valid)
  605. *r_valid = true;
  606. return ret.operator String();
  607. }
  608. }
  609. if (r_valid)
  610. *r_valid = false;
  611. return String();
  612. }
  613. void NativeScriptInstance::refcount_incremented() {
  614. Variant::CallError err;
  615. call("_refcount_incremented", NULL, 0, err);
  616. if (err.error != Variant::CallError::CALL_OK && err.error != Variant::CallError::CALL_ERROR_INVALID_METHOD) {
  617. ERR_PRINT("Failed to invoke _refcount_incremented - should not happen");
  618. }
  619. }
  620. bool NativeScriptInstance::refcount_decremented() {
  621. Variant::CallError err;
  622. Variant ret = call("_refcount_decremented", NULL, 0, err);
  623. if (err.error != Variant::CallError::CALL_OK && err.error != Variant::CallError::CALL_ERROR_INVALID_METHOD) {
  624. ERR_PRINT("Failed to invoke _refcount_decremented - should not happen");
  625. return true; // assume we can destroy the object
  626. }
  627. if (err.error == Variant::CallError::CALL_ERROR_INVALID_METHOD) {
  628. // the method does not exist, default is true
  629. return true;
  630. }
  631. return ret;
  632. }
  633. Ref<Script> NativeScriptInstance::get_script() const {
  634. return script;
  635. }
  636. MultiplayerAPI::RPCMode NativeScriptInstance::get_rpc_mode(const StringName &p_method) const {
  637. NativeScriptDesc *script_data = GET_SCRIPT_DESC();
  638. while (script_data) {
  639. Map<StringName, NativeScriptDesc::Method>::Element *E = script_data->methods.find(p_method);
  640. if (E) {
  641. switch (E->get().rpc_mode) {
  642. case GODOT_METHOD_RPC_MODE_DISABLED:
  643. return MultiplayerAPI::RPC_MODE_DISABLED;
  644. case GODOT_METHOD_RPC_MODE_REMOTE:
  645. return MultiplayerAPI::RPC_MODE_REMOTE;
  646. case GODOT_METHOD_RPC_MODE_MASTER:
  647. return MultiplayerAPI::RPC_MODE_MASTER;
  648. case GODOT_METHOD_RPC_MODE_PUPPET:
  649. return MultiplayerAPI::RPC_MODE_PUPPET;
  650. case GODOT_METHOD_RPC_MODE_REMOTESYNC:
  651. return MultiplayerAPI::RPC_MODE_REMOTESYNC;
  652. case GODOT_METHOD_RPC_MODE_MASTERSYNC:
  653. return MultiplayerAPI::RPC_MODE_MASTERSYNC;
  654. case GODOT_METHOD_RPC_MODE_PUPPETSYNC:
  655. return MultiplayerAPI::RPC_MODE_PUPPETSYNC;
  656. default:
  657. return MultiplayerAPI::RPC_MODE_DISABLED;
  658. }
  659. }
  660. script_data = script_data->base_data;
  661. }
  662. return MultiplayerAPI::RPC_MODE_DISABLED;
  663. }
  664. MultiplayerAPI::RPCMode NativeScriptInstance::get_rset_mode(const StringName &p_variable) const {
  665. NativeScriptDesc *script_data = GET_SCRIPT_DESC();
  666. while (script_data) {
  667. OrderedHashMap<StringName, NativeScriptDesc::Property>::Element E = script_data->properties.find(p_variable);
  668. if (E) {
  669. switch (E.get().rset_mode) {
  670. case GODOT_METHOD_RPC_MODE_DISABLED:
  671. return MultiplayerAPI::RPC_MODE_DISABLED;
  672. case GODOT_METHOD_RPC_MODE_REMOTE:
  673. return MultiplayerAPI::RPC_MODE_REMOTE;
  674. case GODOT_METHOD_RPC_MODE_MASTER:
  675. return MultiplayerAPI::RPC_MODE_MASTER;
  676. case GODOT_METHOD_RPC_MODE_PUPPET:
  677. return MultiplayerAPI::RPC_MODE_PUPPET;
  678. case GODOT_METHOD_RPC_MODE_REMOTESYNC:
  679. return MultiplayerAPI::RPC_MODE_REMOTESYNC;
  680. case GODOT_METHOD_RPC_MODE_MASTERSYNC:
  681. return MultiplayerAPI::RPC_MODE_MASTERSYNC;
  682. case GODOT_METHOD_RPC_MODE_PUPPETSYNC:
  683. return MultiplayerAPI::RPC_MODE_PUPPETSYNC;
  684. default:
  685. return MultiplayerAPI::RPC_MODE_DISABLED;
  686. }
  687. }
  688. script_data = script_data->base_data;
  689. }
  690. return MultiplayerAPI::RPC_MODE_DISABLED;
  691. }
  692. ScriptLanguage *NativeScriptInstance::get_language() {
  693. return NativeScriptLanguage::get_singleton();
  694. }
  695. void NativeScriptInstance::call_multilevel(const StringName &p_method, const Variant **p_args, int p_argcount) {
  696. NativeScriptDesc *script_data = GET_SCRIPT_DESC();
  697. while (script_data) {
  698. Map<StringName, NativeScriptDesc::Method>::Element *E = script_data->methods.find(p_method);
  699. if (E) {
  700. godot_variant res = E->get().method.method((godot_object *)owner,
  701. E->get().method.method_data,
  702. userdata,
  703. p_argcount,
  704. (godot_variant **)p_args);
  705. godot_variant_destroy(&res);
  706. }
  707. script_data = script_data->base_data;
  708. }
  709. }
  710. void NativeScriptInstance::call_multilevel_reversed(const StringName &p_method, const Variant **p_args, int p_argcount) {
  711. NativeScriptDesc *script_data = GET_SCRIPT_DESC();
  712. if (script_data) {
  713. _ml_call_reversed(script_data, p_method, p_args, p_argcount);
  714. }
  715. }
  716. NativeScriptInstance::~NativeScriptInstance() {
  717. NativeScriptDesc *script_data = GET_SCRIPT_DESC();
  718. if (!script_data)
  719. return;
  720. script_data->destroy_func.destroy_func((godot_object *)owner, script_data->destroy_func.method_data, userdata);
  721. if (owner) {
  722. #ifndef NO_THREADS
  723. script->owners_lock->lock();
  724. #endif
  725. script->instance_owners.erase(owner);
  726. #ifndef NO_THREADS
  727. script->owners_lock->unlock();
  728. #endif
  729. }
  730. }
  731. NativeScriptLanguage *NativeScriptLanguage::singleton;
  732. void NativeScriptLanguage::_unload_stuff(bool p_reload) {
  733. Map<String, Ref<GDNative> > erase_and_unload;
  734. for (Map<String, Map<StringName, NativeScriptDesc> >::Element *L = library_classes.front(); L; L = L->next()) {
  735. String lib_path = L->key();
  736. Map<StringName, NativeScriptDesc> classes = L->get();
  737. if (p_reload) {
  738. Map<String, Ref<GDNative> >::Element *E = library_gdnatives.find(lib_path);
  739. Ref<GDNative> gdn;
  740. if (E) {
  741. gdn = E->get();
  742. }
  743. bool should_reload = false;
  744. if (gdn.is_valid()) {
  745. Ref<GDNativeLibrary> lib = gdn->get_library();
  746. if (lib.is_valid()) {
  747. should_reload = lib->is_reloadable();
  748. }
  749. }
  750. if (!should_reload) {
  751. continue;
  752. }
  753. }
  754. Map<String, Ref<GDNative> >::Element *E = library_gdnatives.find(lib_path);
  755. Ref<GDNative> gdn;
  756. if (E) {
  757. gdn = E->get();
  758. }
  759. for (Map<StringName, NativeScriptDesc>::Element *C = classes.front(); C; C = C->next()) {
  760. // free property stuff first
  761. for (OrderedHashMap<StringName, NativeScriptDesc::Property>::Element P = C->get().properties.front(); P; P = P.next()) {
  762. if (P.get().getter.free_func)
  763. P.get().getter.free_func(P.get().getter.method_data);
  764. if (P.get().setter.free_func)
  765. P.get().setter.free_func(P.get().setter.method_data);
  766. }
  767. // free method stuff
  768. for (Map<StringName, NativeScriptDesc::Method>::Element *M = C->get().methods.front(); M; M = M->next()) {
  769. if (M->get().method.free_func)
  770. M->get().method.free_func(M->get().method.method_data);
  771. }
  772. // free constructor/destructor
  773. if (C->get().create_func.free_func)
  774. C->get().create_func.free_func(C->get().create_func.method_data);
  775. if (C->get().destroy_func.free_func)
  776. C->get().destroy_func.free_func(C->get().destroy_func.method_data);
  777. }
  778. erase_and_unload.insert(lib_path, gdn);
  779. }
  780. for (Map<String, Ref<GDNative> >::Element *E = erase_and_unload.front(); E; E = E->next()) {
  781. String lib_path = E->key();
  782. Ref<GDNative> gdn = E->get();
  783. library_classes.erase(lib_path);
  784. if (gdn.is_valid() && gdn->get_library().is_valid()) {
  785. Ref<GDNativeLibrary> lib = gdn->get_library();
  786. void *terminate_fn;
  787. Error err = gdn->get_symbol(lib->get_symbol_prefix() + _terminate_call_name, terminate_fn, true);
  788. if (err == OK) {
  789. void (*terminate)(void *) = (void (*)(void *))terminate_fn;
  790. terminate((void *)&lib_path);
  791. }
  792. }
  793. }
  794. }
  795. NativeScriptLanguage::NativeScriptLanguage() {
  796. NativeScriptLanguage::singleton = this;
  797. #ifndef NO_THREADS
  798. has_objects_to_register = false;
  799. mutex = Mutex::create();
  800. #endif
  801. #ifdef DEBUG_ENABLED
  802. profiling = false;
  803. #endif
  804. _init_call_type = "nativescript_init";
  805. _init_call_name = "nativescript_init";
  806. _terminate_call_name = "nativescript_terminate";
  807. _noarg_call_type = "nativescript_no_arg";
  808. _frame_call_name = "nativescript_frame";
  809. #ifndef NO_THREADS
  810. _thread_enter_call_name = "nativescript_thread_enter";
  811. _thread_exit_call_name = "nativescript_thread_exit";
  812. #endif
  813. }
  814. NativeScriptLanguage::~NativeScriptLanguage() {
  815. for (Map<String, Ref<GDNative> >::Element *L = NSL->library_gdnatives.front(); L; L = L->next()) {
  816. Ref<GDNative> lib = L->get();
  817. // only shut down valid libs, duh!
  818. if (lib.is_valid()) {
  819. // If it's a singleton-library then the gdnative module
  820. // manages the destruction at engine shutdown, not NativeScript.
  821. if (!lib->get_library()->is_singleton()) {
  822. lib->terminate();
  823. }
  824. }
  825. }
  826. NSL->library_classes.clear();
  827. NSL->library_gdnatives.clear();
  828. NSL->library_script_users.clear();
  829. #ifndef NO_THREADS
  830. memdelete(mutex);
  831. #endif
  832. }
  833. String NativeScriptLanguage::get_name() const {
  834. return "NativeScript";
  835. }
  836. void _add_reload_node() {
  837. #ifdef TOOLS_ENABLED
  838. NativeReloadNode *rn = memnew(NativeReloadNode);
  839. EditorNode::get_singleton()->add_child(rn);
  840. #endif
  841. }
  842. void NativeScriptLanguage::init() {
  843. #if defined(TOOLS_ENABLED) && defined(DEBUG_METHODS_ENABLED)
  844. List<String> args = OS::get_singleton()->get_cmdline_args();
  845. List<String>::Element *E = args.find("--gdnative-generate-json-api");
  846. if (E && E->next()) {
  847. if (generate_c_api(E->next()->get()) != OK) {
  848. ERR_PRINT("Failed to generate C API\n");
  849. }
  850. exit(0);
  851. }
  852. #endif
  853. #ifdef TOOLS_ENABLED
  854. EditorNode::add_init_callback(&_add_reload_node);
  855. #endif
  856. }
  857. String NativeScriptLanguage::get_type() const {
  858. return "NativeScript";
  859. }
  860. String NativeScriptLanguage::get_extension() const {
  861. return "gdns";
  862. }
  863. Error NativeScriptLanguage::execute_file(const String &p_path) {
  864. return OK; // Qué?
  865. }
  866. void NativeScriptLanguage::finish() {
  867. _unload_stuff();
  868. }
  869. void NativeScriptLanguage::get_reserved_words(List<String> *p_words) const {
  870. }
  871. void NativeScriptLanguage::get_comment_delimiters(List<String> *p_delimiters) const {
  872. }
  873. void NativeScriptLanguage::get_string_delimiters(List<String> *p_delimiters) const {
  874. }
  875. Ref<Script> NativeScriptLanguage::get_template(const String &p_class_name, const String &p_base_class_name) const {
  876. NativeScript *s = memnew(NativeScript);
  877. s->set_class_name(p_class_name);
  878. return Ref<NativeScript>(s);
  879. }
  880. bool NativeScriptLanguage::validate(const String &p_script, int &r_line_error, int &r_col_error, String &r_test_error, const String &p_path, List<String> *r_functions, List<ScriptLanguage::Warning> *r_warnings, Set<int> *r_safe_lines) const {
  881. return true;
  882. }
  883. Script *NativeScriptLanguage::create_script() const {
  884. NativeScript *script = memnew(NativeScript);
  885. return script;
  886. }
  887. bool NativeScriptLanguage::has_named_classes() const {
  888. return true;
  889. }
  890. bool NativeScriptLanguage::supports_builtin_mode() const {
  891. return true;
  892. }
  893. int NativeScriptLanguage::find_function(const String &p_function, const String &p_code) const {
  894. return -1;
  895. }
  896. String NativeScriptLanguage::make_function(const String &p_class, const String &p_name, const PoolStringArray &p_args) const {
  897. return "";
  898. }
  899. void NativeScriptLanguage::auto_indent_code(String &p_code, int p_from_line, int p_to_line) const {
  900. }
  901. void NativeScriptLanguage::add_global_constant(const StringName &p_variable, const Variant &p_value) {
  902. }
  903. // Debugging stuff here. Not used for now.
  904. String NativeScriptLanguage::debug_get_error() const {
  905. return "";
  906. }
  907. int NativeScriptLanguage::debug_get_stack_level_count() const {
  908. return -1;
  909. }
  910. int NativeScriptLanguage::debug_get_stack_level_line(int p_level) const {
  911. return -1;
  912. }
  913. String NativeScriptLanguage::debug_get_stack_level_function(int p_level) const {
  914. return "";
  915. }
  916. String NativeScriptLanguage::debug_get_stack_level_source(int p_level) const {
  917. return "";
  918. }
  919. void NativeScriptLanguage::debug_get_stack_level_locals(int p_level, List<String> *p_locals, List<Variant> *p_values, int p_max_subitems, int p_max_depth) {
  920. }
  921. void NativeScriptLanguage::debug_get_stack_level_members(int p_level, List<String> *p_members, List<Variant> *p_values, int p_max_subitems, int p_max_depth) {
  922. }
  923. void NativeScriptLanguage::debug_get_globals(List<String> *p_locals, List<Variant> *p_values, int p_max_subitems, int p_max_depth) {
  924. }
  925. String NativeScriptLanguage::debug_parse_stack_level_expression(int p_level, const String &p_expression, int p_max_subitems, int p_max_depth) {
  926. return "";
  927. }
  928. // Debugging stuff end.
  929. void NativeScriptLanguage::reload_all_scripts() {
  930. }
  931. void NativeScriptLanguage::reload_tool_script(const Ref<Script> &p_script, bool p_soft_reload) {
  932. }
  933. void NativeScriptLanguage::get_recognized_extensions(List<String> *p_extensions) const {
  934. p_extensions->push_back("gdns");
  935. }
  936. void NativeScriptLanguage::get_public_functions(List<MethodInfo> *p_functions) const {
  937. }
  938. void NativeScriptLanguage::get_public_constants(List<Pair<String, Variant> > *p_constants) const {
  939. }
  940. void NativeScriptLanguage::profiling_start() {
  941. #ifdef DEBUG_ENABLED
  942. #ifndef NO_THREADS
  943. MutexLock lock(mutex);
  944. #endif
  945. profile_data.clear();
  946. profiling = true;
  947. #endif
  948. }
  949. void NativeScriptLanguage::profiling_stop() {
  950. #ifdef DEBUG_ENABLED
  951. #ifndef NO_THREADS
  952. MutexLock lock(mutex);
  953. #endif
  954. profiling = false;
  955. #endif
  956. }
  957. int NativeScriptLanguage::profiling_get_accumulated_data(ProfilingInfo *p_info_arr, int p_info_max) {
  958. #ifdef DEBUG_ENABLED
  959. #ifndef NO_THREADS
  960. MutexLock lock(mutex);
  961. #endif
  962. int current = 0;
  963. for (Map<StringName, ProfileData>::Element *d = profile_data.front(); d; d = d->next()) {
  964. if (current >= p_info_max)
  965. break;
  966. p_info_arr[current].call_count = d->get().call_count;
  967. p_info_arr[current].self_time = d->get().self_time;
  968. p_info_arr[current].total_time = d->get().total_time;
  969. p_info_arr[current].signature = d->get().signature;
  970. current++;
  971. }
  972. return current;
  973. #else
  974. return 0;
  975. #endif
  976. }
  977. int NativeScriptLanguage::profiling_get_frame_data(ProfilingInfo *p_info_arr, int p_info_max) {
  978. #ifdef DEBUG_ENABLED
  979. #ifndef NO_THREADS
  980. MutexLock lock(mutex);
  981. #endif
  982. int current = 0;
  983. for (Map<StringName, ProfileData>::Element *d = profile_data.front(); d; d = d->next()) {
  984. if (current >= p_info_max)
  985. break;
  986. if (d->get().last_frame_call_count) {
  987. p_info_arr[current].call_count = d->get().last_frame_call_count;
  988. p_info_arr[current].self_time = d->get().last_frame_self_time;
  989. p_info_arr[current].total_time = d->get().last_frame_total_time;
  990. p_info_arr[current].signature = d->get().signature;
  991. current++;
  992. }
  993. }
  994. return current;
  995. #else
  996. return 0;
  997. #endif
  998. }
  999. void NativeScriptLanguage::profiling_add_data(StringName p_signature, uint64_t p_time) {
  1000. #ifdef DEBUG_ENABLED
  1001. #ifndef NO_THREADS
  1002. MutexLock lock(mutex);
  1003. #endif
  1004. Map<StringName, ProfileData>::Element *d = profile_data.find(p_signature);
  1005. if (d) {
  1006. d->get().call_count += 1;
  1007. d->get().total_time += p_time;
  1008. d->get().frame_call_count += 1;
  1009. d->get().frame_total_time += p_time;
  1010. } else {
  1011. ProfileData data;
  1012. data.signature = p_signature;
  1013. data.call_count = 1;
  1014. data.self_time = 0;
  1015. data.total_time = p_time;
  1016. data.frame_call_count = 1;
  1017. data.frame_self_time = 0;
  1018. data.frame_total_time = p_time;
  1019. data.last_frame_call_count = 0;
  1020. data.last_frame_self_time = 0;
  1021. data.last_frame_total_time = 0;
  1022. profile_data.insert(p_signature, data);
  1023. }
  1024. #endif
  1025. }
  1026. int NativeScriptLanguage::register_binding_functions(godot_instance_binding_functions p_binding_functions) {
  1027. // find index
  1028. int idx = -1;
  1029. for (int i = 0; i < binding_functions.size(); i++) {
  1030. if (!binding_functions[i].first) {
  1031. // free, we'll take it
  1032. idx = i;
  1033. break;
  1034. }
  1035. }
  1036. if (idx == -1) {
  1037. idx = binding_functions.size();
  1038. binding_functions.resize(idx + 1);
  1039. }
  1040. // set the functions
  1041. binding_functions.write[idx].first = true;
  1042. binding_functions.write[idx].second = p_binding_functions;
  1043. return idx;
  1044. }
  1045. void NativeScriptLanguage::unregister_binding_functions(int p_idx) {
  1046. ERR_FAIL_INDEX(p_idx, binding_functions.size());
  1047. for (Set<Vector<void *> *>::Element *E = binding_instances.front(); E; E = E->next()) {
  1048. Vector<void *> &binding_data = *E->get();
  1049. if (binding_data[p_idx] && binding_functions[p_idx].second.free_instance_binding_data)
  1050. binding_functions[p_idx].second.free_instance_binding_data(binding_functions[p_idx].second.data, binding_data[p_idx]);
  1051. }
  1052. binding_functions.write[p_idx].first = false;
  1053. if (binding_functions[p_idx].second.free_func)
  1054. binding_functions[p_idx].second.free_func(binding_functions[p_idx].second.data);
  1055. }
  1056. void *NativeScriptLanguage::get_instance_binding_data(int p_idx, Object *p_object) {
  1057. ERR_FAIL_INDEX_V(p_idx, binding_functions.size(), NULL);
  1058. if (!binding_functions[p_idx].first) {
  1059. ERR_EXPLAIN("Tried to get binding data for a nativescript binding that does not exist");
  1060. ERR_FAIL_V(NULL);
  1061. }
  1062. Vector<void *> *binding_data = (Vector<void *> *)p_object->get_script_instance_binding(lang_idx);
  1063. if (!binding_data)
  1064. return NULL; // should never happen.
  1065. if (binding_data->size() <= p_idx) {
  1066. // okay, add new elements here.
  1067. int old_size = binding_data->size();
  1068. binding_data->resize(p_idx + 1);
  1069. for (int i = old_size; i <= p_idx; i++) {
  1070. (*binding_data).write[i] = NULL;
  1071. }
  1072. }
  1073. if (!(*binding_data)[p_idx]) {
  1074. const void *global_type_tag = global_type_tags[p_idx].get(p_object->get_class_name());
  1075. // no binding data yet, soooooo alloc new one \o/
  1076. (*binding_data).write[p_idx] = binding_functions[p_idx].second.alloc_instance_binding_data(binding_functions[p_idx].second.data, global_type_tag, (godot_object *)p_object);
  1077. }
  1078. return (*binding_data)[p_idx];
  1079. }
  1080. void *NativeScriptLanguage::alloc_instance_binding_data(Object *p_object) {
  1081. Vector<void *> *binding_data = new Vector<void *>;
  1082. binding_data->resize(binding_functions.size());
  1083. for (int i = 0; i < binding_functions.size(); i++) {
  1084. (*binding_data).write[i] = NULL;
  1085. }
  1086. binding_instances.insert(binding_data);
  1087. return (void *)binding_data;
  1088. }
  1089. void NativeScriptLanguage::free_instance_binding_data(void *p_data) {
  1090. if (!p_data)
  1091. return;
  1092. Vector<void *> &binding_data = *(Vector<void *> *)p_data;
  1093. for (int i = 0; i < binding_data.size(); i++) {
  1094. if (!binding_data[i])
  1095. continue;
  1096. if (binding_functions[i].first && binding_functions[i].second.free_instance_binding_data) {
  1097. binding_functions[i].second.free_instance_binding_data(binding_functions[i].second.data, binding_data[i]);
  1098. }
  1099. }
  1100. binding_instances.erase(&binding_data);
  1101. delete &binding_data;
  1102. }
  1103. void NativeScriptLanguage::refcount_incremented_instance_binding(Object *p_object) {
  1104. void *data = p_object->get_script_instance_binding(lang_idx);
  1105. if (!data)
  1106. return;
  1107. Vector<void *> &binding_data = *(Vector<void *> *)data;
  1108. for (int i = 0; i < binding_data.size(); i++) {
  1109. if (!binding_data[i])
  1110. continue;
  1111. if (!binding_functions[i].first)
  1112. continue;
  1113. if (binding_functions[i].second.refcount_incremented_instance_binding) {
  1114. binding_functions[i].second.refcount_incremented_instance_binding(binding_data[i], p_object);
  1115. }
  1116. }
  1117. }
  1118. bool NativeScriptLanguage::refcount_decremented_instance_binding(Object *p_object) {
  1119. void *data = p_object->get_script_instance_binding(lang_idx);
  1120. if (!data)
  1121. return true;
  1122. Vector<void *> &binding_data = *(Vector<void *> *)data;
  1123. bool can_die = true;
  1124. for (int i = 0; i < binding_data.size(); i++) {
  1125. if (!binding_data[i])
  1126. continue;
  1127. if (!binding_functions[i].first)
  1128. continue;
  1129. if (binding_functions[i].second.refcount_decremented_instance_binding) {
  1130. can_die = can_die && binding_functions[i].second.refcount_decremented_instance_binding(binding_data[i], p_object);
  1131. }
  1132. }
  1133. return can_die;
  1134. }
  1135. void NativeScriptLanguage::set_global_type_tag(int p_idx, StringName p_class_name, const void *p_type_tag) {
  1136. if (!global_type_tags.has(p_idx)) {
  1137. global_type_tags.insert(p_idx, HashMap<StringName, const void *>());
  1138. }
  1139. HashMap<StringName, const void *> &tags = global_type_tags[p_idx];
  1140. tags.set(p_class_name, p_type_tag);
  1141. }
  1142. const void *NativeScriptLanguage::get_global_type_tag(int p_idx, StringName p_class_name) const {
  1143. if (!global_type_tags.has(p_idx))
  1144. return NULL;
  1145. const HashMap<StringName, const void *> &tags = global_type_tags[p_idx];
  1146. const void *tag = tags.get(p_class_name);
  1147. return tag;
  1148. }
  1149. #ifndef NO_THREADS
  1150. void NativeScriptLanguage::defer_init_library(Ref<GDNativeLibrary> lib, NativeScript *script) {
  1151. MutexLock lock(mutex);
  1152. libs_to_init.insert(lib);
  1153. scripts_to_register.insert(script);
  1154. has_objects_to_register = true;
  1155. }
  1156. #endif
  1157. void NativeScriptLanguage::init_library(const Ref<GDNativeLibrary> &lib) {
  1158. #ifndef NO_THREADS
  1159. MutexLock lock(mutex);
  1160. #endif
  1161. // See if this library was "registered" already.
  1162. const String &lib_path = lib->get_current_library_path();
  1163. ERR_EXPLAIN(lib->get_name() + " does not have a library for the current platform");
  1164. ERR_FAIL_COND(lib_path.length() == 0);
  1165. Map<String, Ref<GDNative> >::Element *E = library_gdnatives.find(lib_path);
  1166. if (!E) {
  1167. Ref<GDNative> gdn;
  1168. gdn.instance();
  1169. gdn->set_library(lib);
  1170. // TODO check the return value?
  1171. gdn->initialize();
  1172. library_gdnatives.insert(lib_path, gdn);
  1173. library_classes.insert(lib_path, Map<StringName, NativeScriptDesc>());
  1174. if (!library_script_users.has(lib_path))
  1175. library_script_users.insert(lib_path, Set<NativeScript *>());
  1176. void *proc_ptr;
  1177. Error err = gdn->get_symbol(lib->get_symbol_prefix() + _init_call_name, proc_ptr);
  1178. if (err != OK) {
  1179. ERR_PRINT(String("No " + _init_call_name + " in \"" + lib_path + "\" found").utf8().get_data());
  1180. } else {
  1181. ((void (*)(godot_string *))proc_ptr)((godot_string *)&lib_path);
  1182. }
  1183. } else {
  1184. // already initialized. Nice.
  1185. }
  1186. }
  1187. void NativeScriptLanguage::register_script(NativeScript *script) {
  1188. #ifndef NO_THREADS
  1189. MutexLock lock(mutex);
  1190. #endif
  1191. library_script_users[script->lib_path].insert(script);
  1192. }
  1193. void NativeScriptLanguage::unregister_script(NativeScript *script) {
  1194. #ifndef NO_THREADS
  1195. MutexLock lock(mutex);
  1196. #endif
  1197. Map<String, Set<NativeScript *> >::Element *S = library_script_users.find(script->lib_path);
  1198. if (S) {
  1199. S->get().erase(script);
  1200. if (S->get().size() == 0) {
  1201. library_script_users.erase(S);
  1202. }
  1203. }
  1204. #ifndef NO_THREADS
  1205. scripts_to_register.erase(script);
  1206. #endif
  1207. }
  1208. void NativeScriptLanguage::call_libraries_cb(const StringName &name) {
  1209. // library_gdnatives is modified only from the main thread, so it's safe not to use mutex here
  1210. for (Map<String, Ref<GDNative> >::Element *L = library_gdnatives.front(); L; L = L->next()) {
  1211. if (L->get().is_null()) {
  1212. continue;
  1213. }
  1214. if (L->get()->is_initialized()) {
  1215. void *proc_ptr;
  1216. Error err = L->get()->get_symbol(L->get()->get_library()->get_symbol_prefix() + name, proc_ptr);
  1217. if (!err) {
  1218. ((void (*)())proc_ptr)();
  1219. }
  1220. }
  1221. }
  1222. }
  1223. void NativeScriptLanguage::frame() {
  1224. #ifndef NO_THREADS
  1225. if (has_objects_to_register) {
  1226. MutexLock lock(mutex);
  1227. for (Set<Ref<GDNativeLibrary> >::Element *L = libs_to_init.front(); L; L = L->next()) {
  1228. init_library(L->get());
  1229. }
  1230. libs_to_init.clear();
  1231. for (Set<NativeScript *>::Element *S = scripts_to_register.front(); S; S = S->next()) {
  1232. register_script(S->get());
  1233. }
  1234. scripts_to_register.clear();
  1235. has_objects_to_register = false;
  1236. }
  1237. #endif
  1238. #ifdef DEBUG_ENABLED
  1239. {
  1240. #ifndef NO_THREADS
  1241. MutexLock lock(mutex);
  1242. #endif
  1243. for (Map<StringName, ProfileData>::Element *d = profile_data.front(); d; d = d->next()) {
  1244. d->get().last_frame_call_count = d->get().frame_call_count;
  1245. d->get().last_frame_self_time = d->get().frame_self_time;
  1246. d->get().last_frame_total_time = d->get().frame_total_time;
  1247. d->get().frame_call_count = 0;
  1248. d->get().frame_self_time = 0;
  1249. d->get().frame_total_time = 0;
  1250. }
  1251. }
  1252. #endif
  1253. call_libraries_cb(_frame_call_name);
  1254. }
  1255. #ifndef NO_THREADS
  1256. void NativeScriptLanguage::thread_enter() {
  1257. call_libraries_cb(_thread_enter_call_name);
  1258. }
  1259. void NativeScriptLanguage::thread_exit() {
  1260. call_libraries_cb(_thread_exit_call_name);
  1261. }
  1262. #endif // NO_THREADS
  1263. bool NativeScriptLanguage::handles_global_class_type(const String &p_type) const {
  1264. return p_type == "NativeScript";
  1265. }
  1266. String NativeScriptLanguage::get_global_class_name(const String &p_path, String *r_base_type, String *r_icon_path) const {
  1267. if (!p_path.empty()) {
  1268. Ref<NativeScript> script = ResourceLoader::load(p_path, "NativeScript");
  1269. if (script.is_valid()) {
  1270. if (r_base_type)
  1271. *r_base_type = script->get_instance_base_type();
  1272. if (r_icon_path)
  1273. *r_icon_path = script->get_script_class_icon_path();
  1274. return script->get_script_class_name();
  1275. }
  1276. if (r_base_type)
  1277. *r_base_type = String();
  1278. if (r_icon_path)
  1279. *r_icon_path = String();
  1280. }
  1281. return String();
  1282. }
  1283. void NativeReloadNode::_bind_methods() {
  1284. ClassDB::bind_method(D_METHOD("_notification"), &NativeReloadNode::_notification);
  1285. }
  1286. void NativeReloadNode::_notification(int p_what) {
  1287. #ifdef TOOLS_ENABLED
  1288. switch (p_what) {
  1289. case MainLoop::NOTIFICATION_WM_FOCUS_OUT: {
  1290. if (unloaded)
  1291. break;
  1292. #ifndef NO_THREADS
  1293. MutexLock lock(NSL->mutex);
  1294. #endif
  1295. NSL->_unload_stuff(true);
  1296. for (Map<String, Ref<GDNative> >::Element *L = NSL->library_gdnatives.front(); L; L = L->next()) {
  1297. Ref<GDNative> gdn = L->get();
  1298. if (gdn.is_null()) {
  1299. continue;
  1300. }
  1301. // Don't unload what should not be reloaded!
  1302. if (!gdn->get_library()->is_reloadable()) {
  1303. continue;
  1304. }
  1305. // singleton libraries might have alive pointers living inside the
  1306. // editor. Also reloading a singleton library would mean that
  1307. // the singleton entry will not be called again, as this only
  1308. // happens at engine startup.
  1309. if (gdn->get_library()->is_singleton()) {
  1310. continue;
  1311. }
  1312. gdn->terminate();
  1313. }
  1314. unloaded = true;
  1315. } break;
  1316. case MainLoop::NOTIFICATION_WM_FOCUS_IN: {
  1317. if (!unloaded)
  1318. break;
  1319. #ifndef NO_THREADS
  1320. MutexLock lock(NSL->mutex);
  1321. #endif
  1322. Set<StringName> libs_to_remove;
  1323. for (Map<String, Ref<GDNative> >::Element *L = NSL->library_gdnatives.front(); L; L = L->next()) {
  1324. Ref<GDNative> gdn = L->get();
  1325. if (gdn.is_null()) {
  1326. continue;
  1327. }
  1328. if (!gdn->get_library()->is_reloadable()) {
  1329. continue;
  1330. }
  1331. // since singleton libraries are not unloaded there is no point
  1332. // in loading them again.
  1333. if (gdn->get_library()->is_singleton()) {
  1334. continue;
  1335. }
  1336. if (!gdn->initialize()) {
  1337. libs_to_remove.insert(L->key());
  1338. continue;
  1339. }
  1340. NSL->library_classes.insert(L->key(), Map<StringName, NativeScriptDesc>());
  1341. // here the library registers all the classes and stuff.
  1342. void *proc_ptr;
  1343. Error err = gdn->get_symbol(gdn->get_library()->get_symbol_prefix() + "nativescript_init", proc_ptr);
  1344. if (err != OK) {
  1345. ERR_PRINT(String("No godot_nativescript_init in \"" + L->key() + "\" found").utf8().get_data());
  1346. } else {
  1347. ((void (*)(void *))proc_ptr)((void *)&L->key());
  1348. }
  1349. for (Map<String, Set<NativeScript *> >::Element *U = NSL->library_script_users.front(); U; U = U->next()) {
  1350. for (Set<NativeScript *>::Element *S = U->get().front(); S; S = S->next()) {
  1351. NativeScript *script = S->get();
  1352. if (script->placeholders.size() == 0)
  1353. continue;
  1354. for (Set<PlaceHolderScriptInstance *>::Element *P = script->placeholders.front(); P; P = P->next()) {
  1355. script->_update_placeholder(P->get());
  1356. }
  1357. }
  1358. }
  1359. }
  1360. unloaded = false;
  1361. for (Set<StringName>::Element *R = libs_to_remove.front(); R; R = R->next()) {
  1362. NSL->library_gdnatives.erase(R->get());
  1363. }
  1364. } break;
  1365. default: {
  1366. };
  1367. }
  1368. #endif
  1369. }
  1370. RES ResourceFormatLoaderNativeScript::load(const String &p_path, const String &p_original_path, Error *r_error) {
  1371. return ResourceFormatLoaderText::singleton->load(p_path, p_original_path, r_error);
  1372. }
  1373. void ResourceFormatLoaderNativeScript::get_recognized_extensions(List<String> *p_extensions) const {
  1374. p_extensions->push_back("gdns");
  1375. }
  1376. bool ResourceFormatLoaderNativeScript::handles_type(const String &p_type) const {
  1377. return (p_type == "Script" || p_type == "NativeScript");
  1378. }
  1379. String ResourceFormatLoaderNativeScript::get_resource_type(const String &p_path) const {
  1380. String el = p_path.get_extension().to_lower();
  1381. if (el == "gdns")
  1382. return "NativeScript";
  1383. return "";
  1384. }
  1385. Error ResourceFormatSaverNativeScript::save(const String &p_path, const RES &p_resource, uint32_t p_flags) {
  1386. ResourceFormatSaverText rfst;
  1387. return rfst.save(p_path, p_resource, p_flags);
  1388. }
  1389. bool ResourceFormatSaverNativeScript::recognize(const RES &p_resource) const {
  1390. return Object::cast_to<NativeScript>(*p_resource) != NULL;
  1391. }
  1392. void ResourceFormatSaverNativeScript::get_recognized_extensions(const RES &p_resource, List<String> *p_extensions) const {
  1393. if (Object::cast_to<NativeScript>(*p_resource)) {
  1394. p_extensions->push_back("gdns");
  1395. }
  1396. }