nativescript.cpp 33 KB

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