nativescript.cpp 33 KB

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