nativescript.cpp 33 KB

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