nativescript.cpp 33 KB

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