nativescript.cpp 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233
  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. if (!owner) {
  303. r_error.error = Variant::CallError::CALL_ERROR_INSTANCE_IS_NULL;
  304. return Variant();
  305. }
  306. Reference *r = Object::cast_to<Reference>(owner);
  307. if (r) {
  308. ref = REF(r);
  309. }
  310. NativeScriptInstance *instance = (NativeScriptInstance *)instance_create(owner);
  311. owner->set_script_instance(instance);
  312. if (!instance) {
  313. if (ref.is_null()) {
  314. memdelete(owner); //no owner, sorry
  315. }
  316. return Variant();
  317. }
  318. if (ref.is_valid()) {
  319. return ref;
  320. } else {
  321. return owner;
  322. }
  323. }
  324. NativeScript::NativeScript() {
  325. library = Ref<GDNative>();
  326. lib_path = "";
  327. class_name = "";
  328. #ifndef NO_THREADS
  329. owners_lock = Mutex::create();
  330. #endif
  331. }
  332. NativeScript::~NativeScript() {
  333. NSL->unregister_script(this);
  334. #ifndef NO_THREADS
  335. memdelete(owners_lock);
  336. #endif
  337. }
  338. //
  339. //
  340. // ScriptInstance stuff
  341. //
  342. //
  343. #define GET_SCRIPT_DESC() script->get_script_desc()
  344. void NativeScriptInstance::_ml_call_reversed(NativeScriptDesc *script_data, const StringName &p_method, const Variant **p_args, int p_argcount) {
  345. if (script_data->base_data) {
  346. _ml_call_reversed(script_data->base_data, p_method, p_args, p_argcount);
  347. }
  348. Map<StringName, NativeScriptDesc::Method>::Element *E = script_data->methods.find(p_method);
  349. if (E) {
  350. godot_variant res = E->get().method.method((godot_object *)owner, E->get().method.method_data, userdata, p_argcount, (godot_variant **)p_args);
  351. godot_variant_destroy(&res);
  352. }
  353. }
  354. bool NativeScriptInstance::set(const StringName &p_name, const Variant &p_value) {
  355. NativeScriptDesc *script_data = GET_SCRIPT_DESC();
  356. while (script_data) {
  357. OrderedHashMap<StringName, NativeScriptDesc::Property>::Element P = script_data->properties.find(p_name);
  358. if (P) {
  359. P.get().setter.set_func((godot_object *)owner,
  360. P.get().setter.method_data,
  361. userdata,
  362. (godot_variant *)&p_value);
  363. return true;
  364. }
  365. Map<StringName, NativeScriptDesc::Method>::Element *E = script_data->methods.find("_set");
  366. if (E) {
  367. Variant name = p_name;
  368. const Variant *args[2] = { &name, &p_value };
  369. E->get().method.method((godot_object *)owner,
  370. E->get().method.method_data,
  371. userdata,
  372. 2,
  373. (godot_variant **)args);
  374. return true;
  375. }
  376. script_data = script_data->base_data;
  377. }
  378. return false;
  379. }
  380. bool NativeScriptInstance::get(const StringName &p_name, Variant &r_ret) const {
  381. NativeScriptDesc *script_data = GET_SCRIPT_DESC();
  382. while (script_data) {
  383. OrderedHashMap<StringName, NativeScriptDesc::Property>::Element P = script_data->properties.find(p_name);
  384. if (P) {
  385. godot_variant value;
  386. value = P.get().getter.get_func((godot_object *)owner,
  387. P.get().getter.method_data,
  388. userdata);
  389. r_ret = *(Variant *)&value;
  390. godot_variant_destroy(&value);
  391. return true;
  392. }
  393. Map<StringName, NativeScriptDesc::Method>::Element *E = script_data->methods.find("_get");
  394. if (E) {
  395. Variant name = p_name;
  396. const Variant *args[1] = { &name };
  397. godot_variant result;
  398. result = E->get().method.method((godot_object *)owner,
  399. E->get().method.method_data,
  400. userdata,
  401. 1,
  402. (godot_variant **)args);
  403. r_ret = *(Variant *)&result;
  404. godot_variant_destroy(&result);
  405. if (r_ret.get_type() == Variant::NIL) {
  406. return false;
  407. }
  408. return true;
  409. }
  410. script_data = script_data->base_data;
  411. }
  412. return false;
  413. }
  414. void NativeScriptInstance::get_property_list(List<PropertyInfo> *p_properties) const {
  415. script->get_script_property_list(p_properties);
  416. NativeScriptDesc *script_data = GET_SCRIPT_DESC();
  417. while (script_data) {
  418. Map<StringName, NativeScriptDesc::Method>::Element *E = script_data->methods.find("_get_property_list");
  419. if (E) {
  420. godot_variant result;
  421. result = E->get().method.method((godot_object *)owner,
  422. E->get().method.method_data,
  423. userdata,
  424. 0,
  425. NULL);
  426. Variant res = *(Variant *)&result;
  427. godot_variant_destroy(&result);
  428. if (res.get_type() != Variant::ARRAY) {
  429. ERR_EXPLAIN("_get_property_list must return an array of dictionaries");
  430. ERR_FAIL();
  431. }
  432. Array arr = res;
  433. for (int i = 0; i < arr.size(); i++) {
  434. Dictionary d = arr[i];
  435. ERR_CONTINUE(!d.has("name"));
  436. ERR_CONTINUE(!d.has("type"));
  437. PropertyInfo info;
  438. info.type = Variant::Type(d["type"].operator int64_t());
  439. ERR_CONTINUE(info.type < 0 || info.type >= Variant::VARIANT_MAX);
  440. info.name = d["name"];
  441. ERR_CONTINUE(info.name == "");
  442. if (d.has("hint")) {
  443. info.hint = PropertyHint(d["hint"].operator int64_t());
  444. }
  445. if (d.has("hint_string")) {
  446. info.hint_string = d["hint_string"];
  447. }
  448. if (d.has("usage")) {
  449. info.usage = d["usage"];
  450. }
  451. p_properties->push_back(info);
  452. }
  453. }
  454. script_data = script_data->base_data;
  455. }
  456. return;
  457. }
  458. Variant::Type NativeScriptInstance::get_property_type(const StringName &p_name, bool *r_is_valid) const {
  459. NativeScriptDesc *script_data = GET_SCRIPT_DESC();
  460. while (script_data) {
  461. OrderedHashMap<StringName, NativeScriptDesc::Property>::Element P = script_data->properties.find(p_name);
  462. if (P) {
  463. *r_is_valid = true;
  464. return P.get().info.type;
  465. }
  466. script_data = script_data->base_data;
  467. }
  468. return Variant::NIL;
  469. }
  470. void NativeScriptInstance::get_method_list(List<MethodInfo> *p_list) const {
  471. script->get_method_list(p_list);
  472. }
  473. bool NativeScriptInstance::has_method(const StringName &p_method) const {
  474. return script->has_method(p_method);
  475. }
  476. Variant NativeScriptInstance::call(const StringName &p_method, const Variant **p_args, int p_argcount, Variant::CallError &r_error) {
  477. NativeScriptDesc *script_data = GET_SCRIPT_DESC();
  478. while (script_data) {
  479. Map<StringName, NativeScriptDesc::Method>::Element *E = script_data->methods.find(p_method);
  480. if (E) {
  481. godot_variant result;
  482. result = E->get().method.method((godot_object *)owner,
  483. E->get().method.method_data,
  484. userdata,
  485. p_argcount,
  486. (godot_variant **)p_args);
  487. Variant res = *(Variant *)&result;
  488. godot_variant_destroy(&result);
  489. r_error.error = Variant::CallError::CALL_OK;
  490. return res;
  491. }
  492. script_data = script_data->base_data;
  493. }
  494. r_error.error = Variant::CallError::CALL_ERROR_INVALID_METHOD;
  495. return Variant();
  496. }
  497. void NativeScriptInstance::notification(int p_notification) {
  498. Variant value = p_notification;
  499. const Variant *args[1] = { &value };
  500. call_multilevel("_notification", args, 1);
  501. }
  502. void NativeScriptInstance::refcount_incremented() {
  503. Variant::CallError err;
  504. call("_refcount_incremented", NULL, 0, err);
  505. if (err.error != Variant::CallError::CALL_OK && err.error != Variant::CallError::CALL_ERROR_INVALID_METHOD) {
  506. ERR_PRINT("Failed to invoke _refcount_incremented - should not happen");
  507. }
  508. }
  509. bool NativeScriptInstance::refcount_decremented() {
  510. Variant::CallError err;
  511. Variant ret = call("_refcount_decremented", NULL, 0, err);
  512. if (err.error != Variant::CallError::CALL_OK && err.error != Variant::CallError::CALL_ERROR_INVALID_METHOD) {
  513. ERR_PRINT("Failed to invoke _refcount_decremented - should not happen");
  514. return true; // assume we can destroy the object
  515. }
  516. if (err.error == Variant::CallError::CALL_ERROR_INVALID_METHOD) {
  517. // the method does not exist, default is true
  518. return true;
  519. }
  520. return ret;
  521. }
  522. Ref<Script> NativeScriptInstance::get_script() const {
  523. return script;
  524. }
  525. NativeScriptInstance::RPCMode NativeScriptInstance::get_rpc_mode(const StringName &p_method) const {
  526. NativeScriptDesc *script_data = GET_SCRIPT_DESC();
  527. while (script_data) {
  528. Map<StringName, NativeScriptDesc::Method>::Element *E = script_data->methods.find(p_method);
  529. if (E) {
  530. switch (E->get().rpc_mode) {
  531. case GODOT_METHOD_RPC_MODE_DISABLED:
  532. return RPC_MODE_DISABLED;
  533. case GODOT_METHOD_RPC_MODE_REMOTE:
  534. return RPC_MODE_REMOTE;
  535. case GODOT_METHOD_RPC_MODE_SYNC:
  536. return RPC_MODE_SYNC;
  537. case GODOT_METHOD_RPC_MODE_MASTER:
  538. return RPC_MODE_MASTER;
  539. case GODOT_METHOD_RPC_MODE_SLAVE:
  540. return RPC_MODE_SLAVE;
  541. default:
  542. return RPC_MODE_DISABLED;
  543. }
  544. }
  545. script_data = script_data->base_data;
  546. }
  547. return RPC_MODE_DISABLED;
  548. }
  549. NativeScriptInstance::RPCMode NativeScriptInstance::get_rset_mode(const StringName &p_variable) const {
  550. NativeScriptDesc *script_data = GET_SCRIPT_DESC();
  551. while (script_data) {
  552. OrderedHashMap<StringName, NativeScriptDesc::Property>::Element E = script_data->properties.find(p_variable);
  553. if (E) {
  554. switch (E.get().rset_mode) {
  555. case GODOT_METHOD_RPC_MODE_DISABLED:
  556. return RPC_MODE_DISABLED;
  557. case GODOT_METHOD_RPC_MODE_REMOTE:
  558. return RPC_MODE_REMOTE;
  559. case GODOT_METHOD_RPC_MODE_SYNC:
  560. return RPC_MODE_SYNC;
  561. case GODOT_METHOD_RPC_MODE_MASTER:
  562. return RPC_MODE_MASTER;
  563. case GODOT_METHOD_RPC_MODE_SLAVE:
  564. return RPC_MODE_SLAVE;
  565. default:
  566. return RPC_MODE_DISABLED;
  567. }
  568. }
  569. script_data = script_data->base_data;
  570. }
  571. return RPC_MODE_DISABLED;
  572. }
  573. ScriptLanguage *NativeScriptInstance::get_language() {
  574. return NativeScriptLanguage::get_singleton();
  575. }
  576. void NativeScriptInstance::call_multilevel(const StringName &p_method, const Variant **p_args, int p_argcount) {
  577. NativeScriptDesc *script_data = GET_SCRIPT_DESC();
  578. while (script_data) {
  579. Map<StringName, NativeScriptDesc::Method>::Element *E = script_data->methods.find(p_method);
  580. if (E) {
  581. godot_variant res = E->get().method.method((godot_object *)owner,
  582. E->get().method.method_data,
  583. userdata,
  584. p_argcount,
  585. (godot_variant **)p_args);
  586. godot_variant_destroy(&res);
  587. }
  588. script_data = script_data->base_data;
  589. }
  590. }
  591. void NativeScriptInstance::call_multilevel_reversed(const StringName &p_method, const Variant **p_args, int p_argcount) {
  592. NativeScriptDesc *script_data = GET_SCRIPT_DESC();
  593. if (script_data) {
  594. _ml_call_reversed(script_data, p_method, p_args, p_argcount);
  595. }
  596. }
  597. NativeScriptInstance::~NativeScriptInstance() {
  598. NativeScriptDesc *script_data = GET_SCRIPT_DESC();
  599. if (!script_data)
  600. return;
  601. script_data->destroy_func.destroy_func((godot_object *)owner, script_data->destroy_func.method_data, userdata);
  602. if (owner) {
  603. #ifndef NO_THREADS
  604. script->owners_lock->lock();
  605. #endif
  606. script->instance_owners.erase(owner);
  607. #ifndef NO_THREADS
  608. script->owners_lock->unlock();
  609. #endif
  610. }
  611. }
  612. //
  613. //
  614. // ScriptingLanguage stuff
  615. //
  616. //
  617. NativeScriptLanguage *NativeScriptLanguage::singleton;
  618. void NativeScriptLanguage::_unload_stuff(bool p_reload) {
  619. for (Map<String, Map<StringName, NativeScriptDesc> >::Element *L = library_classes.front(); L; L = L->next()) {
  620. if (p_reload && library_gdnatives[L->key()].is_valid() && !library_gdnatives[L->key()]->get_library()->is_reloadable()) {
  621. continue;
  622. }
  623. for (Map<StringName, NativeScriptDesc>::Element *C = L->get().front(); C; C = C->next()) {
  624. // free property stuff first
  625. for (OrderedHashMap<StringName, NativeScriptDesc::Property>::Element P = C->get().properties.front(); P; P = P.next()) {
  626. if (P.get().getter.free_func)
  627. P.get().getter.free_func(P.get().getter.method_data);
  628. if (P.get().setter.free_func)
  629. P.get().setter.free_func(P.get().setter.method_data);
  630. }
  631. // free method stuff
  632. for (Map<StringName, NativeScriptDesc::Method>::Element *M = C->get().methods.front(); M; M = M->next()) {
  633. if (M->get().method.free_func)
  634. M->get().method.free_func(M->get().method.method_data);
  635. }
  636. // free constructor/destructor
  637. if (C->get().create_func.free_func)
  638. C->get().create_func.free_func(C->get().create_func.method_data);
  639. if (C->get().destroy_func.free_func)
  640. C->get().destroy_func.free_func(C->get().destroy_func.method_data);
  641. }
  642. }
  643. }
  644. NativeScriptLanguage::NativeScriptLanguage() {
  645. NativeScriptLanguage::singleton = this;
  646. #ifndef NO_THREADS
  647. mutex = Mutex::create();
  648. #endif
  649. }
  650. NativeScriptLanguage::~NativeScriptLanguage() {
  651. for (Map<String, Ref<GDNative> >::Element *L = NSL->library_gdnatives.front(); L; L = L->next()) {
  652. if (L->get().is_valid())
  653. L->get()->terminate();
  654. }
  655. NSL->library_classes.clear();
  656. NSL->library_gdnatives.clear();
  657. NSL->library_script_users.clear();
  658. #ifndef NO_THREADS
  659. memdelete(mutex);
  660. #endif
  661. }
  662. String NativeScriptLanguage::get_name() const {
  663. return "NativeScript";
  664. }
  665. void _add_reload_node() {
  666. #ifdef TOOLS_ENABLED
  667. NativeReloadNode *rn = memnew(NativeReloadNode);
  668. EditorNode::get_singleton()->add_child(rn);
  669. #endif
  670. }
  671. void NativeScriptLanguage::init() {
  672. #if defined(TOOLS_ENABLED) && defined(DEBUG_METHODS_ENABLED)
  673. List<String> args = OS::get_singleton()->get_cmdline_args();
  674. List<String>::Element *E = args.find("--gdnative-generate-json-api");
  675. if (E && E->next()) {
  676. if (generate_c_api(E->next()->get()) != OK) {
  677. ERR_PRINT("Failed to generate C API\n");
  678. }
  679. exit(0);
  680. }
  681. #endif
  682. #ifdef TOOLS_ENABLED
  683. EditorNode::add_init_callback(&_add_reload_node);
  684. #endif
  685. }
  686. String NativeScriptLanguage::get_type() const {
  687. return "NativeScript";
  688. }
  689. String NativeScriptLanguage::get_extension() const {
  690. return "gdns";
  691. }
  692. Error NativeScriptLanguage::execute_file(const String &p_path) {
  693. return OK; // Qué?
  694. }
  695. void NativeScriptLanguage::finish() {
  696. _unload_stuff();
  697. }
  698. void NativeScriptLanguage::get_reserved_words(List<String> *p_words) const {
  699. }
  700. void NativeScriptLanguage::get_comment_delimiters(List<String> *p_delimiters) const {
  701. }
  702. void NativeScriptLanguage::get_string_delimiters(List<String> *p_delimiters) const {
  703. }
  704. Ref<Script> NativeScriptLanguage::get_template(const String &p_class_name, const String &p_base_class_name) const {
  705. NativeScript *s = memnew(NativeScript);
  706. s->set_class_name(p_class_name);
  707. return Ref<NativeScript>(s);
  708. }
  709. 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 {
  710. return false;
  711. }
  712. Script *NativeScriptLanguage::create_script() const {
  713. NativeScript *script = memnew(NativeScript);
  714. return script;
  715. }
  716. bool NativeScriptLanguage::has_named_classes() const {
  717. return true;
  718. }
  719. bool NativeScriptLanguage::supports_builtin_mode() const {
  720. return true;
  721. }
  722. int NativeScriptLanguage::find_function(const String &p_function, const String &p_code) const {
  723. return -1;
  724. }
  725. String NativeScriptLanguage::make_function(const String &p_class, const String &p_name, const PoolStringArray &p_args) const {
  726. return "";
  727. }
  728. void NativeScriptLanguage::auto_indent_code(String &p_code, int p_from_line, int p_to_line) const {
  729. }
  730. void NativeScriptLanguage::add_global_constant(const StringName &p_variable, const Variant &p_value) {
  731. }
  732. // Debugging stuff here. Not used for now.
  733. String NativeScriptLanguage::debug_get_error() const {
  734. return "";
  735. }
  736. int NativeScriptLanguage::debug_get_stack_level_count() const {
  737. return -1;
  738. }
  739. int NativeScriptLanguage::debug_get_stack_level_line(int p_level) const {
  740. return -1;
  741. }
  742. String NativeScriptLanguage::debug_get_stack_level_function(int p_level) const {
  743. return "";
  744. }
  745. String NativeScriptLanguage::debug_get_stack_level_source(int p_level) const {
  746. return "";
  747. }
  748. 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) {
  749. }
  750. 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) {
  751. }
  752. void NativeScriptLanguage::debug_get_globals(List<String> *p_locals, List<Variant> *p_values, int p_max_subitems, int p_max_depth) {
  753. }
  754. String NativeScriptLanguage::debug_parse_stack_level_expression(int p_level, const String &p_expression, int p_max_subitems, int p_max_depth) {
  755. return "";
  756. }
  757. // Debugging stuff end.
  758. void NativeScriptLanguage::reload_all_scripts() {
  759. }
  760. void NativeScriptLanguage::reload_tool_script(const Ref<Script> &p_script, bool p_soft_reload) {
  761. }
  762. void NativeScriptLanguage::get_recognized_extensions(List<String> *p_extensions) const {
  763. p_extensions->push_back("gdns");
  764. }
  765. void NativeScriptLanguage::get_public_functions(List<MethodInfo> *p_functions) const {
  766. }
  767. void NativeScriptLanguage::get_public_constants(List<Pair<String, Variant> > *p_constants) const {
  768. }
  769. void NativeScriptLanguage::profiling_start() {
  770. }
  771. void NativeScriptLanguage::profiling_stop() {
  772. }
  773. int NativeScriptLanguage::profiling_get_accumulated_data(ProfilingInfo *p_info_arr, int p_info_max) {
  774. return 0;
  775. }
  776. int NativeScriptLanguage::profiling_get_frame_data(ProfilingInfo *p_info_arr, int p_info_max) {
  777. return 0;
  778. }
  779. #ifndef NO_THREADS
  780. void NativeScriptLanguage::defer_init_library(Ref<GDNativeLibrary> lib, NativeScript *script) {
  781. MutexLock lock(mutex);
  782. libs_to_init.insert(lib);
  783. scripts_to_register.insert(script);
  784. has_objects_to_register = true;
  785. }
  786. #endif
  787. void NativeScriptLanguage::init_library(const Ref<GDNativeLibrary> &lib) {
  788. #ifndef NO_THREADS
  789. MutexLock lock(mutex);
  790. #endif
  791. // See if this library was "registered" already.
  792. const String &lib_path = lib->get_current_library_path();
  793. ERR_EXPLAIN(lib->get_name() + " does not have a library for the current platform");
  794. ERR_FAIL_COND(lib_path.length() == 0);
  795. Map<String, Ref<GDNative> >::Element *E = library_gdnatives.find(lib_path);
  796. if (!E) {
  797. Ref<GDNative> gdn;
  798. gdn.instance();
  799. gdn->set_library(lib);
  800. // TODO check the return value?
  801. gdn->initialize();
  802. library_gdnatives.insert(lib_path, gdn);
  803. library_classes.insert(lib_path, Map<StringName, NativeScriptDesc>());
  804. if (!library_script_users.has(lib_path))
  805. library_script_users.insert(lib_path, Set<NativeScript *>());
  806. void *proc_ptr;
  807. Error err = gdn->get_symbol(lib->get_symbol_prefix() + _init_call_name, proc_ptr);
  808. if (err != OK) {
  809. ERR_PRINT(String("No " + _init_call_name + " in \"" + lib_path + "\" found").utf8().get_data());
  810. } else {
  811. ((void (*)(godot_string *))proc_ptr)((godot_string *)&lib_path);
  812. }
  813. } else {
  814. // already initialized. Nice.
  815. }
  816. }
  817. void NativeScriptLanguage::register_script(NativeScript *script) {
  818. #ifndef NO_THREADS
  819. MutexLock lock(mutex);
  820. #endif
  821. library_script_users[script->lib_path].insert(script);
  822. }
  823. void NativeScriptLanguage::unregister_script(NativeScript *script) {
  824. #ifndef NO_THREADS
  825. MutexLock lock(mutex);
  826. #endif
  827. Map<String, Set<NativeScript *> >::Element *S = library_script_users.find(script->lib_path);
  828. if (S) {
  829. S->get().erase(script);
  830. if (S->get().size() == 0) {
  831. library_script_users.erase(S);
  832. }
  833. }
  834. #ifndef NO_THREADS
  835. scripts_to_register.erase(script);
  836. #endif
  837. }
  838. void NativeScriptLanguage::call_libraries_cb(const StringName &name) {
  839. // library_gdnatives is modified only from the main thread, so it's safe not to use mutex here
  840. for (Map<String, Ref<GDNative> >::Element *L = library_gdnatives.front(); L; L = L->next()) {
  841. if (L->get()->is_initialized()) {
  842. void *proc_ptr;
  843. Error err = L->get()->get_symbol(L->get()->get_library()->get_symbol_prefix() + name, proc_ptr);
  844. if (!err) {
  845. ((void (*)())proc_ptr)();
  846. }
  847. }
  848. }
  849. }
  850. void NativeScriptLanguage::frame() {
  851. #ifndef NO_THREADS
  852. if (has_objects_to_register) {
  853. MutexLock lock(mutex);
  854. for (Set<Ref<GDNativeLibrary> >::Element *L = libs_to_init.front(); L; L = L->next()) {
  855. init_library(L->get());
  856. }
  857. libs_to_init.clear();
  858. for (Set<NativeScript *>::Element *S = scripts_to_register.front(); S; S = S->next()) {
  859. register_script(S->get());
  860. }
  861. scripts_to_register.clear();
  862. has_objects_to_register = false;
  863. }
  864. #endif
  865. call_libraries_cb(_frame_call_name);
  866. }
  867. #ifndef NO_THREADS
  868. void NativeScriptLanguage::thread_enter() {
  869. call_libraries_cb(_thread_enter_call_name);
  870. }
  871. void NativeScriptLanguage::thread_exit() {
  872. call_libraries_cb(_thread_exit_call_name);
  873. }
  874. #endif // NO_THREADS
  875. void NativeReloadNode::_bind_methods() {
  876. ClassDB::bind_method(D_METHOD("_notification"), &NativeReloadNode::_notification);
  877. }
  878. void NativeReloadNode::_notification(int p_what) {
  879. #ifdef TOOLS_ENABLED
  880. switch (p_what) {
  881. case MainLoop::NOTIFICATION_WM_FOCUS_OUT: {
  882. if (unloaded)
  883. break;
  884. #ifndef NO_THREADS
  885. MutexLock lock(NSL->mutex);
  886. #endif
  887. NSL->_unload_stuff(true);
  888. for (Map<String, Ref<GDNative> >::Element *L = NSL->library_gdnatives.front(); L; L = L->next()) {
  889. Ref<GDNative> gdn = L->get();
  890. if (!gdn->get_library()->is_reloadable()) {
  891. continue;
  892. }
  893. gdn->terminate();
  894. NSL->library_classes.erase(L->key());
  895. }
  896. unloaded = true;
  897. } break;
  898. case MainLoop::NOTIFICATION_WM_FOCUS_IN: {
  899. if (!unloaded)
  900. break;
  901. #ifndef NO_THREADS
  902. MutexLock lock(NSL->mutex);
  903. #endif
  904. Set<StringName> libs_to_remove;
  905. for (Map<String, Ref<GDNative> >::Element *L = NSL->library_gdnatives.front(); L; L = L->next()) {
  906. Ref<GDNative> gdn = L->get();
  907. if (!gdn->get_library()->is_reloadable()) {
  908. continue;
  909. }
  910. if (!gdn->initialize()) {
  911. libs_to_remove.insert(L->key());
  912. continue;
  913. }
  914. NSL->library_classes.insert(L->key(), Map<StringName, NativeScriptDesc>());
  915. // here the library registers all the classes and stuff.
  916. void *proc_ptr;
  917. Error err = gdn->get_symbol(gdn->get_library()->get_symbol_prefix() + "nativescript_init", proc_ptr);
  918. if (err != OK) {
  919. ERR_PRINT(String("No godot_nativescript_init in \"" + L->key() + "\" found").utf8().get_data());
  920. } else {
  921. ((void (*)(void *))proc_ptr)((void *)&L->key());
  922. }
  923. for (Map<String, Set<NativeScript *> >::Element *U = NSL->library_script_users.front(); U; U = U->next()) {
  924. for (Set<NativeScript *>::Element *S = U->get().front(); S; S = S->next()) {
  925. NativeScript *script = S->get();
  926. if (script->placeholders.size() == 0)
  927. continue;
  928. for (Set<PlaceHolderScriptInstance *>::Element *P = script->placeholders.front(); P; P = P->next()) {
  929. script->_update_placeholder(P->get());
  930. }
  931. }
  932. }
  933. }
  934. unloaded = false;
  935. for (Set<StringName>::Element *R = libs_to_remove.front(); R; R = R->next()) {
  936. NSL->library_gdnatives.erase(R->get());
  937. }
  938. } break;
  939. default: {
  940. };
  941. }
  942. #endif
  943. }
  944. RES ResourceFormatLoaderNativeScript::load(const String &p_path, const String &p_original_path, Error *r_error) {
  945. ResourceFormatLoaderText rsflt;
  946. return rsflt.load(p_path, p_original_path, r_error);
  947. }
  948. void ResourceFormatLoaderNativeScript::get_recognized_extensions(List<String> *p_extensions) const {
  949. p_extensions->push_back("gdns");
  950. }
  951. bool ResourceFormatLoaderNativeScript::handles_type(const String &p_type) const {
  952. return (p_type == "Script" || p_type == "NativeScript");
  953. }
  954. String ResourceFormatLoaderNativeScript::get_resource_type(const String &p_path) const {
  955. String el = p_path.get_extension().to_lower();
  956. if (el == "gdns")
  957. return "NativeScript";
  958. return "";
  959. }
  960. Error ResourceFormatSaverNativeScript::save(const String &p_path, const RES &p_resource, uint32_t p_flags) {
  961. ResourceFormatSaverText rfst;
  962. return rfst.save(p_path, p_resource, p_flags);
  963. }
  964. bool ResourceFormatSaverNativeScript::recognize(const RES &p_resource) const {
  965. return Object::cast_to<NativeScript>(*p_resource) != NULL;
  966. }
  967. void ResourceFormatSaverNativeScript::get_recognized_extensions(const RES &p_resource, List<String> *p_extensions) const {
  968. if (Object::cast_to<NativeScript>(*p_resource)) {
  969. p_extensions->push_back("gdns");
  970. }
  971. }