nativescript.cpp 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228
  1. /*************************************************************************/
  2. /* nativescript.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2018 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/global_constants.h"
  33. #include "core/project_settings.h"
  34. #include "io/file_access_encrypted.h"
  35. #include "os/file_access.h"
  36. #include "os/os.h"
  37. #include "scene/main/scene_tree.h"
  38. #include "scene/resources/scene_format_text.h"
  39. #include <stdlib.h>
  40. #ifndef NO_THREADS
  41. #include "os/thread.h"
  42. #endif
  43. #if defined(TOOLS_ENABLED) && defined(DEBUG_METHODS_ENABLED)
  44. #include "api_generator.h"
  45. #endif
  46. #ifdef TOOLS_ENABLED
  47. #include "editor/editor_node.h"
  48. #endif
  49. //
  50. //
  51. // Script stuff
  52. //
  53. //
  54. void NativeScript::_bind_methods() {
  55. ClassDB::bind_method(D_METHOD("set_class_name", "class_name"), &NativeScript::set_class_name);
  56. ClassDB::bind_method(D_METHOD("get_class_name"), &NativeScript::get_class_name);
  57. ClassDB::bind_method(D_METHOD("set_library", "library"), &NativeScript::set_library);
  58. ClassDB::bind_method(D_METHOD("get_library"), &NativeScript::get_library);
  59. ADD_PROPERTYNZ(PropertyInfo(Variant::STRING, "class_name"), "set_class_name", "get_class_name");
  60. ADD_PROPERTYNZ(PropertyInfo(Variant::OBJECT, "library", PROPERTY_HINT_RESOURCE_TYPE, "GDNativeLibrary"), "set_library", "get_library");
  61. ClassDB::bind_vararg_method(METHOD_FLAGS_DEFAULT, "new", &NativeScript::_new, MethodInfo(Variant::OBJECT, "new"));
  62. }
  63. #define NSL NativeScriptLanguage::get_singleton()
  64. #ifdef TOOLS_ENABLED
  65. void NativeScript::_update_placeholder(PlaceHolderScriptInstance *p_placeholder) {
  66. NativeScriptDesc *script_data = get_script_desc();
  67. ERR_FAIL_COND(!script_data);
  68. List<PropertyInfo> info;
  69. get_script_property_list(&info);
  70. Map<StringName, Variant> values;
  71. for (List<PropertyInfo>::Element *E = info.front(); E; E = E->next()) {
  72. Variant value;
  73. get_property_default_value(E->get().name, value);
  74. values[E->get().name] = value;
  75. }
  76. p_placeholder->update(info, values);
  77. }
  78. void NativeScript::_placeholder_erased(PlaceHolderScriptInstance *p_placeholder) {
  79. placeholders.erase(p_placeholder);
  80. }
  81. #endif
  82. void NativeScript::set_class_name(String p_class_name) {
  83. class_name = p_class_name;
  84. }
  85. String NativeScript::get_class_name() const {
  86. return class_name;
  87. }
  88. void NativeScript::set_library(Ref<GDNativeLibrary> p_library) {
  89. if (!library.is_null()) {
  90. WARN_PRINT("library on NativeScript already set. Do nothing.");
  91. return;
  92. }
  93. library = p_library;
  94. lib_path = library->get_current_library_path();
  95. #ifndef NO_THREADS
  96. if (Thread::get_caller_id() != Thread::get_main_id()) {
  97. NSL->defer_init_library(p_library, this);
  98. } else
  99. #endif
  100. {
  101. NSL->init_library(p_library);
  102. NSL->register_script(this);
  103. }
  104. }
  105. Ref<GDNativeLibrary> NativeScript::get_library() const {
  106. return library;
  107. }
  108. bool NativeScript::can_instance() const {
  109. NativeScriptDesc *script_data = get_script_desc();
  110. #ifdef TOOLS_ENABLED
  111. return script_data || (!is_tool() && !ScriptServer::is_scripting_enabled());
  112. #else
  113. return script_data;
  114. #endif
  115. }
  116. Ref<Script> NativeScript::get_base_script() const {
  117. NativeScriptDesc *script_data = get_script_desc();
  118. if (!script_data)
  119. return Ref<Script>();
  120. Ref<NativeScript> ns = Ref<NativeScript>(NSL->create_script());
  121. ns->set_class_name(script_data->base);
  122. ns->set_library(get_library());
  123. return ns;
  124. }
  125. StringName NativeScript::get_instance_base_type() const {
  126. NativeScriptDesc *script_data = get_script_desc();
  127. if (!script_data)
  128. return "";
  129. return script_data->base_native_type;
  130. }
  131. ScriptInstance *NativeScript::instance_create(Object *p_this) {
  132. NativeScriptDesc *script_data = get_script_desc();
  133. if (!script_data) {
  134. return NULL;
  135. }
  136. #ifdef TOOLS_ENABLED
  137. if (!ScriptServer::is_scripting_enabled() && !is_tool()) {
  138. // placeholder for nodes. For tools we want the rool thing.
  139. PlaceHolderScriptInstance *sins = memnew(PlaceHolderScriptInstance(NSL, Ref<Script>(this), p_this));
  140. placeholders.insert(sins);
  141. if (script_data->create_func.create_func) {
  142. script_data->create_func.create_func(
  143. (godot_object *)p_this,
  144. script_data->create_func.method_data);
  145. }
  146. _update_placeholder(sins);
  147. return sins;
  148. }
  149. #endif
  150. NativeScriptInstance *nsi = memnew(NativeScriptInstance);
  151. nsi->owner = p_this;
  152. nsi->script = Ref<NativeScript>(this);
  153. #ifndef TOOLS_ENABLED
  154. if (!ScriptServer::is_scripting_enabled()) {
  155. nsi->userdata = NULL;
  156. } else {
  157. nsi->userdata = script_data->create_func.create_func((godot_object *)p_this, script_data->create_func.method_data);
  158. }
  159. #else
  160. nsi->userdata = script_data->create_func.create_func((godot_object *)p_this, script_data->create_func.method_data);
  161. #endif
  162. #ifndef NO_THREADS
  163. owners_lock->lock();
  164. #endif
  165. instance_owners.insert(p_this);
  166. #ifndef NO_THREADS
  167. owners_lock->unlock();
  168. #endif
  169. return nsi;
  170. }
  171. bool NativeScript::instance_has(const Object *p_this) const {
  172. return instance_owners.has((Object *)p_this);
  173. }
  174. bool NativeScript::has_source_code() const {
  175. return false;
  176. }
  177. String NativeScript::get_source_code() const {
  178. return "";
  179. }
  180. void NativeScript::set_source_code(const String &p_code) {
  181. }
  182. Error NativeScript::reload(bool p_keep_state) {
  183. return FAILED;
  184. }
  185. bool NativeScript::has_method(const StringName &p_method) const {
  186. NativeScriptDesc *script_data = get_script_desc();
  187. while (script_data) {
  188. if (script_data->methods.has(p_method))
  189. return true;
  190. script_data = script_data->base_data;
  191. }
  192. return false;
  193. }
  194. MethodInfo NativeScript::get_method_info(const StringName &p_method) const {
  195. NativeScriptDesc *script_data = get_script_desc();
  196. if (!script_data)
  197. return MethodInfo();
  198. while (script_data) {
  199. Map<StringName, NativeScriptDesc::Method>::Element *M = script_data->methods.find(p_method);
  200. if (M)
  201. return M->get().info;
  202. script_data = script_data->base_data;
  203. }
  204. return MethodInfo();
  205. }
  206. bool NativeScript::is_tool() const {
  207. NativeScriptDesc *script_data = get_script_desc();
  208. if (script_data)
  209. return script_data->is_tool;
  210. return false;
  211. }
  212. ScriptLanguage *NativeScript::get_language() const {
  213. return NativeScriptLanguage::get_singleton();
  214. }
  215. bool NativeScript::has_script_signal(const StringName &p_signal) const {
  216. NativeScriptDesc *script_data = get_script_desc();
  217. while (script_data) {
  218. if (script_data->signals_.has(p_signal))
  219. return true;
  220. script_data = script_data->base_data;
  221. }
  222. return false;
  223. }
  224. void NativeScript::get_script_signal_list(List<MethodInfo> *r_signals) const {
  225. NativeScriptDesc *script_data = get_script_desc();
  226. if (!script_data)
  227. return;
  228. Set<MethodInfo> signals_;
  229. while (script_data) {
  230. for (Map<StringName, NativeScriptDesc::Signal>::Element *S = script_data->signals_.front(); S; S = S->next()) {
  231. signals_.insert(S->get().signal);
  232. }
  233. script_data = script_data->base_data;
  234. }
  235. for (Set<MethodInfo>::Element *E = signals_.front(); E; E = E->next()) {
  236. r_signals->push_back(E->get());
  237. }
  238. }
  239. bool NativeScript::get_property_default_value(const StringName &p_property, Variant &r_value) const {
  240. NativeScriptDesc *script_data = get_script_desc();
  241. OrderedHashMap<StringName, NativeScriptDesc::Property>::Element P;
  242. while (!P && script_data) {
  243. P = script_data->properties.find(p_property);
  244. script_data = script_data->base_data;
  245. }
  246. if (!P)
  247. return false;
  248. r_value = P.get().default_value;
  249. return true;
  250. }
  251. void NativeScript::update_exports() {
  252. }
  253. void NativeScript::get_script_method_list(List<MethodInfo> *p_list) const {
  254. NativeScriptDesc *script_data = get_script_desc();
  255. if (!script_data)
  256. return;
  257. Set<MethodInfo> methods;
  258. while (script_data) {
  259. for (Map<StringName, NativeScriptDesc::Method>::Element *E = script_data->methods.front(); E; E = E->next()) {
  260. methods.insert(E->get().info);
  261. }
  262. script_data = script_data->base_data;
  263. }
  264. for (Set<MethodInfo>::Element *E = methods.front(); E; E = E->next()) {
  265. p_list->push_back(E->get());
  266. }
  267. }
  268. void NativeScript::get_script_property_list(List<PropertyInfo> *p_list) const {
  269. NativeScriptDesc *script_data = get_script_desc();
  270. Set<StringName> existing_properties;
  271. while (script_data) {
  272. List<PropertyInfo>::Element *insert_position = p_list->front();
  273. bool insert_before = true;
  274. for (OrderedHashMap<StringName, NativeScriptDesc::Property>::Element E = script_data->properties.front(); E; E = E.next()) {
  275. if (!existing_properties.has(E.key())) {
  276. insert_position = insert_before ? p_list->insert_before(insert_position, E.get().info) : p_list->insert_after(insert_position, E.get().info);
  277. insert_before = false;
  278. existing_properties.insert(E.key());
  279. }
  280. }
  281. script_data = script_data->base_data;
  282. }
  283. }
  284. Variant NativeScript::_new(const Variant **p_args, int p_argcount, Variant::CallError &r_error) {
  285. if (lib_path.empty() || class_name.empty() || library.is_null()) {
  286. r_error.error = Variant::CallError::CALL_ERROR_INSTANCE_IS_NULL;
  287. return Variant();
  288. }
  289. NativeScriptDesc *script_data = get_script_desc();
  290. if (!script_data) {
  291. r_error.error = Variant::CallError::CALL_ERROR_INSTANCE_IS_NULL;
  292. return Variant();
  293. }
  294. r_error.error = Variant::CallError::CALL_OK;
  295. REF ref;
  296. Object *owner = NULL;
  297. if (!(script_data->base_native_type == "")) {
  298. owner = ClassDB::instance(script_data->base_native_type);
  299. } else {
  300. owner = memnew(Reference);
  301. }
  302. Reference *r = Object::cast_to<Reference>(owner);
  303. if (r) {
  304. ref = REF(r);
  305. }
  306. NativeScriptInstance *instance = (NativeScriptInstance *)instance_create(owner);
  307. owner->set_script_instance(instance);
  308. if (!instance) {
  309. if (ref.is_null()) {
  310. memdelete(owner); //no owner, sorry
  311. }
  312. return Variant();
  313. }
  314. if (ref.is_valid()) {
  315. return ref;
  316. } else {
  317. return owner;
  318. }
  319. }
  320. NativeScript::NativeScript() {
  321. library = Ref<GDNative>();
  322. lib_path = "";
  323. class_name = "";
  324. #ifndef NO_THREADS
  325. owners_lock = Mutex::create();
  326. #endif
  327. }
  328. NativeScript::~NativeScript() {
  329. NSL->unregister_script(this);
  330. #ifndef NO_THREADS
  331. memdelete(owners_lock);
  332. #endif
  333. }
  334. //
  335. //
  336. // ScriptInstance stuff
  337. //
  338. //
  339. #define GET_SCRIPT_DESC() script->get_script_desc()
  340. void NativeScriptInstance::_ml_call_reversed(NativeScriptDesc *script_data, const StringName &p_method, const Variant **p_args, int p_argcount) {
  341. if (script_data->base_data) {
  342. _ml_call_reversed(script_data->base_data, p_method, p_args, p_argcount);
  343. }
  344. Map<StringName, NativeScriptDesc::Method>::Element *E = script_data->methods.find(p_method);
  345. if (E) {
  346. godot_variant res = E->get().method.method((godot_object *)owner, E->get().method.method_data, userdata, p_argcount, (godot_variant **)p_args);
  347. godot_variant_destroy(&res);
  348. }
  349. }
  350. bool NativeScriptInstance::set(const StringName &p_name, const Variant &p_value) {
  351. NativeScriptDesc *script_data = GET_SCRIPT_DESC();
  352. while (script_data) {
  353. OrderedHashMap<StringName, NativeScriptDesc::Property>::Element P = script_data->properties.find(p_name);
  354. if (P) {
  355. P.get().setter.set_func((godot_object *)owner,
  356. P.get().setter.method_data,
  357. userdata,
  358. (godot_variant *)&p_value);
  359. return true;
  360. }
  361. Map<StringName, NativeScriptDesc::Method>::Element *E = script_data->methods.find("_set");
  362. if (E) {
  363. Variant name = p_name;
  364. const Variant *args[2] = { &name, &p_value };
  365. E->get().method.method((godot_object *)owner,
  366. E->get().method.method_data,
  367. userdata,
  368. 2,
  369. (godot_variant **)args);
  370. return true;
  371. }
  372. script_data = script_data->base_data;
  373. }
  374. return false;
  375. }
  376. bool NativeScriptInstance::get(const StringName &p_name, Variant &r_ret) const {
  377. NativeScriptDesc *script_data = GET_SCRIPT_DESC();
  378. while (script_data) {
  379. OrderedHashMap<StringName, NativeScriptDesc::Property>::Element P = script_data->properties.find(p_name);
  380. if (P) {
  381. godot_variant value;
  382. value = P.get().getter.get_func((godot_object *)owner,
  383. P.get().getter.method_data,
  384. userdata);
  385. r_ret = *(Variant *)&value;
  386. godot_variant_destroy(&value);
  387. return true;
  388. }
  389. Map<StringName, NativeScriptDesc::Method>::Element *E = script_data->methods.find("_get");
  390. if (E) {
  391. Variant name = p_name;
  392. const Variant *args[1] = { &name };
  393. godot_variant result;
  394. result = E->get().method.method((godot_object *)owner,
  395. E->get().method.method_data,
  396. userdata,
  397. 1,
  398. (godot_variant **)args);
  399. r_ret = *(Variant *)&result;
  400. godot_variant_destroy(&result);
  401. if (r_ret.get_type() == Variant::NIL) {
  402. return false;
  403. }
  404. return true;
  405. }
  406. script_data = script_data->base_data;
  407. }
  408. return false;
  409. }
  410. void NativeScriptInstance::get_property_list(List<PropertyInfo> *p_properties) const {
  411. script->get_script_property_list(p_properties);
  412. NativeScriptDesc *script_data = GET_SCRIPT_DESC();
  413. while (script_data) {
  414. Map<StringName, NativeScriptDesc::Method>::Element *E = script_data->methods.find("_get_property_list");
  415. if (E) {
  416. godot_variant result;
  417. result = E->get().method.method((godot_object *)owner,
  418. E->get().method.method_data,
  419. userdata,
  420. 0,
  421. NULL);
  422. Variant res = *(Variant *)&result;
  423. godot_variant_destroy(&result);
  424. if (res.get_type() != Variant::ARRAY) {
  425. ERR_EXPLAIN("_get_property_list must return an array of dictionaries");
  426. ERR_FAIL();
  427. }
  428. Array arr = res;
  429. for (int i = 0; i < arr.size(); i++) {
  430. Dictionary d = arr[i];
  431. ERR_CONTINUE(!d.has("name"));
  432. ERR_CONTINUE(!d.has("type"));
  433. PropertyInfo info;
  434. info.type = Variant::Type(d["type"].operator int64_t());
  435. ERR_CONTINUE(info.type < 0 || info.type >= Variant::VARIANT_MAX);
  436. info.name = d["name"];
  437. ERR_CONTINUE(info.name == "");
  438. if (d.has("hint")) {
  439. info.hint = PropertyHint(d["hint"].operator int64_t());
  440. }
  441. if (d.has("hint_string")) {
  442. info.hint_string = d["hint_string"];
  443. }
  444. if (d.has("usage")) {
  445. info.usage = d["usage"];
  446. }
  447. p_properties->push_back(info);
  448. }
  449. }
  450. script_data = script_data->base_data;
  451. }
  452. return;
  453. }
  454. Variant::Type NativeScriptInstance::get_property_type(const StringName &p_name, bool *r_is_valid) const {
  455. NativeScriptDesc *script_data = GET_SCRIPT_DESC();
  456. while (script_data) {
  457. OrderedHashMap<StringName, NativeScriptDesc::Property>::Element P = script_data->properties.find(p_name);
  458. if (P) {
  459. *r_is_valid = true;
  460. return P.get().info.type;
  461. }
  462. script_data = script_data->base_data;
  463. }
  464. return Variant::NIL;
  465. }
  466. void NativeScriptInstance::get_method_list(List<MethodInfo> *p_list) const {
  467. script->get_method_list(p_list);
  468. }
  469. bool NativeScriptInstance::has_method(const StringName &p_method) const {
  470. return script->has_method(p_method);
  471. }
  472. Variant NativeScriptInstance::call(const StringName &p_method, const Variant **p_args, int p_argcount, Variant::CallError &r_error) {
  473. NativeScriptDesc *script_data = GET_SCRIPT_DESC();
  474. while (script_data) {
  475. Map<StringName, NativeScriptDesc::Method>::Element *E = script_data->methods.find(p_method);
  476. if (E) {
  477. godot_variant result;
  478. result = E->get().method.method((godot_object *)owner,
  479. E->get().method.method_data,
  480. userdata,
  481. p_argcount,
  482. (godot_variant **)p_args);
  483. Variant res = *(Variant *)&result;
  484. godot_variant_destroy(&result);
  485. r_error.error = Variant::CallError::CALL_OK;
  486. return res;
  487. }
  488. script_data = script_data->base_data;
  489. }
  490. r_error.error = Variant::CallError::CALL_ERROR_INVALID_METHOD;
  491. return Variant();
  492. }
  493. void NativeScriptInstance::notification(int p_notification) {
  494. Variant value = p_notification;
  495. const Variant *args[1] = { &value };
  496. call_multilevel("_notification", args, 1);
  497. }
  498. void NativeScriptInstance::refcount_incremented() {
  499. Variant::CallError err;
  500. call("_refcount_incremented", NULL, 0, err);
  501. if (err.error != Variant::CallError::CALL_OK && err.error != Variant::CallError::CALL_ERROR_INVALID_METHOD) {
  502. ERR_PRINT("Failed to invoke _refcount_incremented - should not happen");
  503. }
  504. }
  505. bool NativeScriptInstance::refcount_decremented() {
  506. Variant::CallError err;
  507. Variant ret = call("_refcount_decremented", NULL, 0, err);
  508. if (err.error != Variant::CallError::CALL_OK && err.error != Variant::CallError::CALL_ERROR_INVALID_METHOD) {
  509. ERR_PRINT("Failed to invoke _refcount_decremented - should not happen");
  510. return true; // assume we can destroy the object
  511. }
  512. if (err.error == Variant::CallError::CALL_ERROR_INVALID_METHOD) {
  513. // the method does not exist, default is true
  514. return true;
  515. }
  516. return ret;
  517. }
  518. Ref<Script> NativeScriptInstance::get_script() const {
  519. return script;
  520. }
  521. NativeScriptInstance::RPCMode NativeScriptInstance::get_rpc_mode(const StringName &p_method) const {
  522. NativeScriptDesc *script_data = GET_SCRIPT_DESC();
  523. while (script_data) {
  524. Map<StringName, NativeScriptDesc::Method>::Element *E = script_data->methods.find(p_method);
  525. if (E) {
  526. switch (E->get().rpc_mode) {
  527. case GODOT_METHOD_RPC_MODE_DISABLED:
  528. return RPC_MODE_DISABLED;
  529. case GODOT_METHOD_RPC_MODE_REMOTE:
  530. return RPC_MODE_REMOTE;
  531. case GODOT_METHOD_RPC_MODE_SYNC:
  532. return RPC_MODE_SYNC;
  533. case GODOT_METHOD_RPC_MODE_MASTER:
  534. return RPC_MODE_MASTER;
  535. case GODOT_METHOD_RPC_MODE_SLAVE:
  536. return RPC_MODE_SLAVE;
  537. default:
  538. return RPC_MODE_DISABLED;
  539. }
  540. }
  541. script_data = script_data->base_data;
  542. }
  543. return RPC_MODE_DISABLED;
  544. }
  545. NativeScriptInstance::RPCMode NativeScriptInstance::get_rset_mode(const StringName &p_variable) const {
  546. NativeScriptDesc *script_data = GET_SCRIPT_DESC();
  547. while (script_data) {
  548. OrderedHashMap<StringName, NativeScriptDesc::Property>::Element E = script_data->properties.find(p_variable);
  549. if (E) {
  550. switch (E.get().rset_mode) {
  551. case GODOT_METHOD_RPC_MODE_DISABLED:
  552. return RPC_MODE_DISABLED;
  553. case GODOT_METHOD_RPC_MODE_REMOTE:
  554. return RPC_MODE_REMOTE;
  555. case GODOT_METHOD_RPC_MODE_SYNC:
  556. return RPC_MODE_SYNC;
  557. case GODOT_METHOD_RPC_MODE_MASTER:
  558. return RPC_MODE_MASTER;
  559. case GODOT_METHOD_RPC_MODE_SLAVE:
  560. return RPC_MODE_SLAVE;
  561. default:
  562. return RPC_MODE_DISABLED;
  563. }
  564. }
  565. script_data = script_data->base_data;
  566. }
  567. return RPC_MODE_DISABLED;
  568. }
  569. ScriptLanguage *NativeScriptInstance::get_language() {
  570. return NativeScriptLanguage::get_singleton();
  571. }
  572. void NativeScriptInstance::call_multilevel(const StringName &p_method, const Variant **p_args, int p_argcount) {
  573. NativeScriptDesc *script_data = GET_SCRIPT_DESC();
  574. while (script_data) {
  575. Map<StringName, NativeScriptDesc::Method>::Element *E = script_data->methods.find(p_method);
  576. if (E) {
  577. godot_variant res = E->get().method.method((godot_object *)owner,
  578. E->get().method.method_data,
  579. userdata,
  580. p_argcount,
  581. (godot_variant **)p_args);
  582. godot_variant_destroy(&res);
  583. }
  584. script_data = script_data->base_data;
  585. }
  586. }
  587. void NativeScriptInstance::call_multilevel_reversed(const StringName &p_method, const Variant **p_args, int p_argcount) {
  588. NativeScriptDesc *script_data = GET_SCRIPT_DESC();
  589. if (script_data) {
  590. _ml_call_reversed(script_data, p_method, p_args, p_argcount);
  591. }
  592. }
  593. NativeScriptInstance::~NativeScriptInstance() {
  594. NativeScriptDesc *script_data = GET_SCRIPT_DESC();
  595. if (!script_data)
  596. return;
  597. script_data->destroy_func.destroy_func((godot_object *)owner, script_data->destroy_func.method_data, userdata);
  598. if (owner) {
  599. #ifndef NO_THREADS
  600. script->owners_lock->lock();
  601. #endif
  602. script->instance_owners.erase(owner);
  603. #ifndef NO_THREADS
  604. script->owners_lock->unlock();
  605. #endif
  606. }
  607. }
  608. //
  609. //
  610. // ScriptingLanguage stuff
  611. //
  612. //
  613. NativeScriptLanguage *NativeScriptLanguage::singleton;
  614. void NativeScriptLanguage::_unload_stuff(bool p_reload) {
  615. for (Map<String, Map<StringName, NativeScriptDesc> >::Element *L = library_classes.front(); L; L = L->next()) {
  616. if (p_reload && !library_gdnatives[L->key()]->get_library()->is_reloadable()) {
  617. continue;
  618. }
  619. for (Map<StringName, NativeScriptDesc>::Element *C = L->get().front(); C; C = C->next()) {
  620. // free property stuff first
  621. for (OrderedHashMap<StringName, NativeScriptDesc::Property>::Element P = C->get().properties.front(); P; P = P.next()) {
  622. if (P.get().getter.free_func)
  623. P.get().getter.free_func(P.get().getter.method_data);
  624. if (P.get().setter.free_func)
  625. P.get().setter.free_func(P.get().setter.method_data);
  626. }
  627. // free method stuff
  628. for (Map<StringName, NativeScriptDesc::Method>::Element *M = C->get().methods.front(); M; M = M->next()) {
  629. if (M->get().method.free_func)
  630. M->get().method.free_func(M->get().method.method_data);
  631. }
  632. // free constructor/destructor
  633. if (C->get().create_func.free_func)
  634. C->get().create_func.free_func(C->get().create_func.method_data);
  635. if (C->get().destroy_func.free_func)
  636. C->get().destroy_func.free_func(C->get().destroy_func.method_data);
  637. }
  638. }
  639. }
  640. NativeScriptLanguage::NativeScriptLanguage() {
  641. NativeScriptLanguage::singleton = this;
  642. #ifndef NO_THREADS
  643. mutex = Mutex::create();
  644. #endif
  645. }
  646. NativeScriptLanguage::~NativeScriptLanguage() {
  647. for (Map<String, Ref<GDNative> >::Element *L = NSL->library_gdnatives.front(); L; L = L->next()) {
  648. if (L->get().is_valid())
  649. L->get()->terminate();
  650. }
  651. NSL->library_classes.clear();
  652. NSL->library_gdnatives.clear();
  653. NSL->library_script_users.clear();
  654. #ifndef NO_THREADS
  655. memdelete(mutex);
  656. #endif
  657. }
  658. String NativeScriptLanguage::get_name() const {
  659. return "NativeScript";
  660. }
  661. void _add_reload_node() {
  662. #ifdef TOOLS_ENABLED
  663. NativeReloadNode *rn = memnew(NativeReloadNode);
  664. EditorNode::get_singleton()->add_child(rn);
  665. #endif
  666. }
  667. void NativeScriptLanguage::init() {
  668. #if defined(TOOLS_ENABLED) && defined(DEBUG_METHODS_ENABLED)
  669. List<String> args = OS::get_singleton()->get_cmdline_args();
  670. List<String>::Element *E = args.find("--gdnative-generate-json-api");
  671. if (E && E->next()) {
  672. if (generate_c_api(E->next()->get()) != OK) {
  673. ERR_PRINT("Failed to generate C API\n");
  674. }
  675. exit(0);
  676. }
  677. #endif
  678. #ifdef TOOLS_ENABLED
  679. EditorNode::add_init_callback(&_add_reload_node);
  680. #endif
  681. }
  682. String NativeScriptLanguage::get_type() const {
  683. return "NativeScript";
  684. }
  685. String NativeScriptLanguage::get_extension() const {
  686. return "gdns";
  687. }
  688. Error NativeScriptLanguage::execute_file(const String &p_path) {
  689. return OK; // Qué?
  690. }
  691. void NativeScriptLanguage::finish() {
  692. _unload_stuff();
  693. }
  694. void NativeScriptLanguage::get_reserved_words(List<String> *p_words) const {
  695. }
  696. void NativeScriptLanguage::get_comment_delimiters(List<String> *p_delimiters) const {
  697. }
  698. void NativeScriptLanguage::get_string_delimiters(List<String> *p_delimiters) const {
  699. }
  700. Ref<Script> NativeScriptLanguage::get_template(const String &p_class_name, const String &p_base_class_name) const {
  701. NativeScript *s = memnew(NativeScript);
  702. s->set_class_name(p_class_name);
  703. return Ref<NativeScript>(s);
  704. }
  705. 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) const {
  706. return false;
  707. }
  708. Script *NativeScriptLanguage::create_script() const {
  709. NativeScript *script = memnew(NativeScript);
  710. return script;
  711. }
  712. bool NativeScriptLanguage::has_named_classes() const {
  713. return true;
  714. }
  715. bool NativeScriptLanguage::supports_builtin_mode() const {
  716. return true;
  717. }
  718. int NativeScriptLanguage::find_function(const String &p_function, const String &p_code) const {
  719. return -1;
  720. }
  721. String NativeScriptLanguage::make_function(const String &p_class, const String &p_name, const PoolStringArray &p_args) const {
  722. return "";
  723. }
  724. void NativeScriptLanguage::auto_indent_code(String &p_code, int p_from_line, int p_to_line) const {
  725. }
  726. void NativeScriptLanguage::add_global_constant(const StringName &p_variable, const Variant &p_value) {
  727. }
  728. // Debugging stuff here. Not used for now.
  729. String NativeScriptLanguage::debug_get_error() const {
  730. return "";
  731. }
  732. int NativeScriptLanguage::debug_get_stack_level_count() const {
  733. return -1;
  734. }
  735. int NativeScriptLanguage::debug_get_stack_level_line(int p_level) const {
  736. return -1;
  737. }
  738. String NativeScriptLanguage::debug_get_stack_level_function(int p_level) const {
  739. return "";
  740. }
  741. String NativeScriptLanguage::debug_get_stack_level_source(int p_level) const {
  742. return "";
  743. }
  744. 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) {
  745. }
  746. 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) {
  747. }
  748. void NativeScriptLanguage::debug_get_globals(List<String> *p_locals, List<Variant> *p_values, int p_max_subitems, int p_max_depth) {
  749. }
  750. String NativeScriptLanguage::debug_parse_stack_level_expression(int p_level, const String &p_expression, int p_max_subitems, int p_max_depth) {
  751. return "";
  752. }
  753. // Debugging stuff end.
  754. void NativeScriptLanguage::reload_all_scripts() {
  755. }
  756. void NativeScriptLanguage::reload_tool_script(const Ref<Script> &p_script, bool p_soft_reload) {
  757. }
  758. void NativeScriptLanguage::get_recognized_extensions(List<String> *p_extensions) const {
  759. p_extensions->push_back("gdns");
  760. }
  761. void NativeScriptLanguage::get_public_functions(List<MethodInfo> *p_functions) const {
  762. }
  763. void NativeScriptLanguage::get_public_constants(List<Pair<String, Variant> > *p_constants) const {
  764. }
  765. void NativeScriptLanguage::profiling_start() {
  766. }
  767. void NativeScriptLanguage::profiling_stop() {
  768. }
  769. int NativeScriptLanguage::profiling_get_accumulated_data(ProfilingInfo *p_info_arr, int p_info_max) {
  770. return 0;
  771. }
  772. int NativeScriptLanguage::profiling_get_frame_data(ProfilingInfo *p_info_arr, int p_info_max) {
  773. return 0;
  774. }
  775. #ifndef NO_THREADS
  776. void NativeScriptLanguage::defer_init_library(Ref<GDNativeLibrary> lib, NativeScript *script) {
  777. MutexLock lock(mutex);
  778. libs_to_init.insert(lib);
  779. scripts_to_register.insert(script);
  780. has_objects_to_register = true;
  781. }
  782. #endif
  783. void NativeScriptLanguage::init_library(const Ref<GDNativeLibrary> &lib) {
  784. #ifndef NO_THREADS
  785. MutexLock lock(mutex);
  786. #endif
  787. // See if this library was "registered" already.
  788. const String &lib_path = lib->get_current_library_path();
  789. ERR_EXPLAIN(lib->get_name() + " does not have a library for the current platform");
  790. ERR_FAIL_COND(lib_path.length() == 0);
  791. Map<String, Ref<GDNative> >::Element *E = library_gdnatives.find(lib_path);
  792. if (!E) {
  793. Ref<GDNative> gdn;
  794. gdn.instance();
  795. gdn->set_library(lib);
  796. // TODO check the return value?
  797. gdn->initialize();
  798. library_gdnatives.insert(lib_path, gdn);
  799. library_classes.insert(lib_path, Map<StringName, NativeScriptDesc>());
  800. if (!library_script_users.has(lib_path))
  801. library_script_users.insert(lib_path, Set<NativeScript *>());
  802. void *proc_ptr;
  803. Error err = gdn->get_symbol(lib->get_symbol_prefix() + _init_call_name, proc_ptr);
  804. if (err != OK) {
  805. ERR_PRINT(String("No " + _init_call_name + " in \"" + lib_path + "\" found").utf8().get_data());
  806. } else {
  807. ((void (*)(godot_string *))proc_ptr)((godot_string *)&lib_path);
  808. }
  809. } else {
  810. // already initialized. Nice.
  811. }
  812. }
  813. void NativeScriptLanguage::register_script(NativeScript *script) {
  814. #ifndef NO_THREADS
  815. MutexLock lock(mutex);
  816. #endif
  817. library_script_users[script->lib_path].insert(script);
  818. }
  819. void NativeScriptLanguage::unregister_script(NativeScript *script) {
  820. #ifndef NO_THREADS
  821. MutexLock lock(mutex);
  822. #endif
  823. Map<String, Set<NativeScript *> >::Element *S = library_script_users.find(script->lib_path);
  824. if (S) {
  825. S->get().erase(script);
  826. if (S->get().size() == 0) {
  827. library_script_users.erase(S);
  828. }
  829. }
  830. #ifndef NO_THREADS
  831. scripts_to_register.erase(script);
  832. #endif
  833. }
  834. void NativeScriptLanguage::call_libraries_cb(const StringName &name) {
  835. // library_gdnatives is modified only from the main thread, so it's safe not to use mutex here
  836. for (Map<String, Ref<GDNative> >::Element *L = library_gdnatives.front(); L; L = L->next()) {
  837. if (L->get()->is_initialized()) {
  838. void *proc_ptr;
  839. Error err = L->get()->get_symbol(L->get()->get_library()->get_symbol_prefix() + name, proc_ptr);
  840. if (!err) {
  841. ((void (*)())proc_ptr)();
  842. }
  843. }
  844. }
  845. }
  846. void NativeScriptLanguage::frame() {
  847. #ifndef NO_THREADS
  848. if (has_objects_to_register) {
  849. MutexLock lock(mutex);
  850. for (Set<Ref<GDNativeLibrary> >::Element *L = libs_to_init.front(); L; L = L->next()) {
  851. init_library(L->get());
  852. }
  853. libs_to_init.clear();
  854. for (Set<NativeScript *>::Element *S = scripts_to_register.front(); S; S = S->next()) {
  855. register_script(S->get());
  856. }
  857. scripts_to_register.clear();
  858. has_objects_to_register = false;
  859. }
  860. #endif
  861. call_libraries_cb(_frame_call_name);
  862. }
  863. #ifndef NO_THREADS
  864. void NativeScriptLanguage::thread_enter() {
  865. call_libraries_cb(_thread_enter_call_name);
  866. }
  867. void NativeScriptLanguage::thread_exit() {
  868. call_libraries_cb(_thread_exit_call_name);
  869. }
  870. #endif // NO_THREADS
  871. void NativeReloadNode::_bind_methods() {
  872. ClassDB::bind_method(D_METHOD("_notification"), &NativeReloadNode::_notification);
  873. }
  874. void NativeReloadNode::_notification(int p_what) {
  875. #ifdef TOOLS_ENABLED
  876. switch (p_what) {
  877. case MainLoop::NOTIFICATION_WM_FOCUS_OUT: {
  878. if (unloaded)
  879. break;
  880. #ifndef NO_THREADS
  881. MutexLock lock(NSL->mutex);
  882. #endif
  883. NSL->_unload_stuff(true);
  884. for (Map<String, Ref<GDNative> >::Element *L = NSL->library_gdnatives.front(); L; L = L->next()) {
  885. Ref<GDNative> gdn = L->get();
  886. if (!gdn->get_library()->is_reloadable()) {
  887. continue;
  888. }
  889. gdn->terminate();
  890. NSL->library_classes.erase(L->key());
  891. }
  892. unloaded = true;
  893. } break;
  894. case MainLoop::NOTIFICATION_WM_FOCUS_IN: {
  895. if (!unloaded)
  896. break;
  897. #ifndef NO_THREADS
  898. MutexLock lock(NSL->mutex);
  899. #endif
  900. Set<StringName> libs_to_remove;
  901. for (Map<String, Ref<GDNative> >::Element *L = NSL->library_gdnatives.front(); L; L = L->next()) {
  902. Ref<GDNative> gdn = L->get();
  903. if (!gdn->get_library()->is_reloadable()) {
  904. continue;
  905. }
  906. if (!gdn->initialize()) {
  907. libs_to_remove.insert(L->key());
  908. continue;
  909. }
  910. NSL->library_classes.insert(L->key(), Map<StringName, NativeScriptDesc>());
  911. // here the library registers all the classes and stuff.
  912. void *proc_ptr;
  913. Error err = gdn->get_symbol(gdn->get_library()->get_symbol_prefix() + "nativescript_init", proc_ptr);
  914. if (err != OK) {
  915. ERR_PRINT(String("No godot_nativescript_init in \"" + L->key() + "\" found").utf8().get_data());
  916. } else {
  917. ((void (*)(void *))proc_ptr)((void *)&L->key());
  918. }
  919. for (Map<String, Set<NativeScript *> >::Element *U = NSL->library_script_users.front(); U; U = U->next()) {
  920. for (Set<NativeScript *>::Element *S = U->get().front(); S; S = S->next()) {
  921. NativeScript *script = S->get();
  922. if (script->placeholders.size() == 0)
  923. continue;
  924. for (Set<PlaceHolderScriptInstance *>::Element *P = script->placeholders.front(); P; P = P->next()) {
  925. script->_update_placeholder(P->get());
  926. }
  927. }
  928. }
  929. }
  930. unloaded = false;
  931. for (Set<StringName>::Element *R = libs_to_remove.front(); R; R = R->next()) {
  932. NSL->library_gdnatives.erase(R->get());
  933. }
  934. } break;
  935. default: {
  936. };
  937. }
  938. #endif
  939. }
  940. RES ResourceFormatLoaderNativeScript::load(const String &p_path, const String &p_original_path, Error *r_error) {
  941. ResourceFormatLoaderText rsflt;
  942. return rsflt.load(p_path, p_original_path, r_error);
  943. }
  944. void ResourceFormatLoaderNativeScript::get_recognized_extensions(List<String> *p_extensions) const {
  945. p_extensions->push_back("gdns");
  946. }
  947. bool ResourceFormatLoaderNativeScript::handles_type(const String &p_type) const {
  948. return (p_type == "Script" || p_type == "NativeScript");
  949. }
  950. String ResourceFormatLoaderNativeScript::get_resource_type(const String &p_path) const {
  951. String el = p_path.get_extension().to_lower();
  952. if (el == "gdns")
  953. return "NativeScript";
  954. return "";
  955. }
  956. Error ResourceFormatSaverNativeScript::save(const String &p_path, const RES &p_resource, uint32_t p_flags) {
  957. ResourceFormatSaverText rfst;
  958. return rfst.save(p_path, p_resource, p_flags);
  959. }
  960. bool ResourceFormatSaverNativeScript::recognize(const RES &p_resource) const {
  961. return Object::cast_to<NativeScript>(*p_resource) != NULL;
  962. }
  963. void ResourceFormatSaverNativeScript::get_recognized_extensions(const RES &p_resource, List<String> *p_extensions) const {
  964. if (Object::cast_to<NativeScript>(*p_resource)) {
  965. p_extensions->push_back("gdns");
  966. }
  967. }