nativescript.cpp 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240
  1. /*************************************************************************/
  2. /* nativescript.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "nativescript.h"
  31. #include "modules/gdnative/godot/gdnative.h"
  32. #include "global_constants.h"
  33. #include "io/file_access_encrypted.h"
  34. #include "os/file_access.h"
  35. #include "os/os.h"
  36. #include "project_settings.h"
  37. #include "scene/main/scene_tree.h"
  38. #include "scene/resources/scene_format_text.h"
  39. #ifndef NO_THREADS
  40. #include "os/thread.h"
  41. #endif
  42. #if defined(TOOLS_ENABLED) && defined(DEBUG_METHODS_ENABLED)
  43. #include "api_generator.h"
  44. #endif
  45. #ifdef TOOLS_ENABLED
  46. #include "editor/editor_node.h"
  47. #endif
  48. ////// Script stuff
  49. void NativeScript::_bind_methods() {
  50. ClassDB::bind_method(D_METHOD("set_class_name", "class_name:String"), &NativeScript::set_class_name);
  51. ClassDB::bind_method(D_METHOD("get_class_name:String"), &NativeScript::get_class_name);
  52. ClassDB::bind_method(D_METHOD("set_library", "library:GDNativeLibrary"), &NativeScript::set_library);
  53. ClassDB::bind_method(D_METHOD("get_library:GDNativeLibrary"), &NativeScript::get_library);
  54. ADD_PROPERTYNZ(PropertyInfo(Variant::STRING, "class_name"), "set_class_name", "get_class_name");
  55. ADD_PROPERTYNZ(PropertyInfo(Variant::OBJECT, "library", PROPERTY_HINT_RESOURCE_TYPE, "GDNativeLibrary"), "set_library", "get_library");
  56. ClassDB::bind_vararg_method(METHOD_FLAGS_DEFAULT, "new", &NativeScript::_new, MethodInfo(Variant::OBJECT, "new"));
  57. }
  58. #define NSL NativeScriptLanguage::get_singleton()
  59. #ifdef TOOLS_ENABLED
  60. void NativeScript::_update_placeholder(PlaceHolderScriptInstance *p_placeholder) {
  61. NativeScriptDesc *script_data = get_script_desc();
  62. ERR_FAIL_COND(!script_data);
  63. List<PropertyInfo> info;
  64. Map<StringName, Variant> values;
  65. for (Map<StringName, NativeScriptDesc::Property>::Element *E = script_data->properties.front(); E; E = E->next()) {
  66. PropertyInfo p = E->get().info;
  67. p.name = String(E->key());
  68. info.push_back(p);
  69. values[p.name] = E->get().default_value;
  70. }
  71. p_placeholder->update(info, values);
  72. }
  73. void NativeScript::_placeholder_erased(PlaceHolderScriptInstance *p_placeholder) {
  74. placeholders.erase(p_placeholder);
  75. }
  76. #endif
  77. void NativeScript::set_class_name(String p_class_name) {
  78. class_name = p_class_name;
  79. }
  80. String NativeScript::get_class_name() const {
  81. return class_name;
  82. }
  83. void NativeScript::set_library(Ref<GDNativeLibrary> p_library) {
  84. if (!library.is_null()) {
  85. WARN_PRINT("library on NativeScript already set. Do nothing.");
  86. return;
  87. }
  88. library = p_library;
  89. lib_path = library->get_active_library_path();
  90. #ifndef NO_THREADS
  91. if (Thread::get_caller_ID() != Thread::get_main_ID()) {
  92. NSL->defer_init_library(p_library, this);
  93. } else
  94. #endif
  95. {
  96. NSL->init_library(p_library);
  97. NSL->register_script(this);
  98. }
  99. }
  100. Ref<GDNativeLibrary> NativeScript::get_library() const {
  101. return library;
  102. }
  103. bool NativeScript::can_instance() const {
  104. NativeScriptDesc *script_data = get_script_desc();
  105. #ifdef TOOLS_ENABLED
  106. return script_data || (!is_tool() && !ScriptServer::is_scripting_enabled());
  107. #else
  108. return script_data;
  109. #endif
  110. }
  111. // TODO(karroffel): implement this
  112. Ref<Script> NativeScript::get_base_script() const {
  113. NativeScriptDesc *script_data = get_script_desc();
  114. if (!script_data)
  115. return Ref<Script>();
  116. Ref<NativeScript> ns = Ref<NativeScript>(NSL->create_script());
  117. ns->set_class_name(script_data->base);
  118. ns->set_library(get_library());
  119. return ns;
  120. }
  121. StringName NativeScript::get_instance_base_type() const {
  122. NativeScriptDesc *script_data = get_script_desc();
  123. if (!script_data)
  124. return "";
  125. return script_data->base_native_type;
  126. }
  127. ScriptInstance *NativeScript::instance_create(Object *p_this) {
  128. NativeScriptDesc *script_data = get_script_desc();
  129. if (!script_data) {
  130. return NULL;
  131. }
  132. #ifdef TOOLS_ENABLED
  133. if (!ScriptServer::is_scripting_enabled() && !is_tool()) {
  134. // placeholder for nodes. For tools we want the rool thing.
  135. PlaceHolderScriptInstance *sins = memnew(PlaceHolderScriptInstance(NSL, Ref<Script>(this), p_this));
  136. placeholders.insert(sins);
  137. if (script_data->create_func.create_func) {
  138. script_data->create_func.create_func(
  139. (godot_object *)p_this,
  140. script_data->create_func.method_data);
  141. }
  142. _update_placeholder(sins);
  143. return sins;
  144. }
  145. #endif
  146. NativeScriptInstance *nsi = memnew(NativeScriptInstance);
  147. nsi->owner = p_this;
  148. nsi->script = Ref<NativeScript>(this);
  149. #ifndef TOOLS_ENABLED
  150. if (!ScriptServer::is_scripting_enabled()) {
  151. nsi->userdata = NULL;
  152. } else {
  153. nsi->userdata = script_data->create_func.create_func((godot_object *)p_this, script_data->create_func.method_data);
  154. }
  155. #else
  156. nsi->userdata = script_data->create_func.create_func((godot_object *)p_this, script_data->create_func.method_data);
  157. #endif
  158. #ifndef NO_THREADS
  159. owners_lock->lock();
  160. #endif
  161. instance_owners.insert(p_this);
  162. #ifndef NO_THREADS
  163. owners_lock->unlock();
  164. #endif
  165. 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. if (!script_data)
  217. return false;
  218. return script_data->signals_.has(p_signal);
  219. }
  220. void NativeScript::get_script_signal_list(List<MethodInfo> *r_signals) const {
  221. NativeScriptDesc *script_data = get_script_desc();
  222. if (!script_data)
  223. return;
  224. Set<MethodInfo> signals_;
  225. while (script_data) {
  226. for (Map<StringName, NativeScriptDesc::Signal>::Element *S = script_data->signals_.front(); S; S = S->next()) {
  227. signals_.insert(S->get().signal);
  228. }
  229. script_data = script_data->base_data;
  230. }
  231. for (Set<MethodInfo>::Element *E = signals_.front(); E; E = E->next()) {
  232. r_signals->push_back(E->get());
  233. }
  234. }
  235. bool NativeScript::get_property_default_value(const StringName &p_property, Variant &r_value) const {
  236. NativeScriptDesc *script_data = get_script_desc();
  237. if (!script_data)
  238. return false;
  239. Map<StringName, NativeScriptDesc::Property>::Element *P = script_data->properties.find(p_property);
  240. if (!P)
  241. return false;
  242. r_value = P->get().default_value;
  243. return true;
  244. }
  245. void NativeScript::update_exports() {
  246. }
  247. void NativeScript::get_script_method_list(List<MethodInfo> *p_list) const {
  248. NativeScriptDesc *script_data = get_script_desc();
  249. if (!script_data)
  250. return;
  251. Set<MethodInfo> methods;
  252. while (script_data) {
  253. for (Map<StringName, NativeScriptDesc::Method>::Element *E = script_data->methods.front(); E; E = E->next()) {
  254. methods.insert(E->get().info);
  255. }
  256. script_data = script_data->base_data;
  257. }
  258. for (Set<MethodInfo>::Element *E = methods.front(); E; E = E->next()) {
  259. p_list->push_back(E->get());
  260. }
  261. }
  262. void NativeScript::get_script_property_list(List<PropertyInfo> *p_list) const {
  263. NativeScriptDesc *script_data = get_script_desc();
  264. if (!script_data)
  265. return;
  266. Set<PropertyInfo> properties;
  267. while (script_data) {
  268. for (Map<StringName, NativeScriptDesc::Property>::Element *E = script_data->properties.front(); E; E = E->next()) {
  269. properties.insert(E->get().info);
  270. }
  271. script_data = script_data->base_data;
  272. }
  273. for (Set<PropertyInfo>::Element *E = properties.front(); E; E = E->next()) {
  274. p_list->push_back(E->get());
  275. }
  276. }
  277. Variant NativeScript::_new(const Variant **p_args, int p_argcount, Variant::CallError &r_error) {
  278. if (lib_path.empty() || class_name.empty() || library.is_null()) {
  279. r_error.error = Variant::CallError::CALL_ERROR_INSTANCE_IS_NULL;
  280. return Variant();
  281. }
  282. NativeScriptDesc *script_data = get_script_desc();
  283. if (!script_data) {
  284. r_error.error = Variant::CallError::CALL_ERROR_INSTANCE_IS_NULL;
  285. return Variant();
  286. }
  287. r_error.error = Variant::CallError::CALL_OK;
  288. REF ref;
  289. Object *owner = NULL;
  290. if (!(script_data->base_native_type == "")) {
  291. owner = ClassDB::instance(script_data->base_native_type);
  292. } else {
  293. owner = memnew(Reference);
  294. }
  295. Reference *r = owner->cast_to<Reference>();
  296. if (r) {
  297. ref = REF(r);
  298. }
  299. NativeScriptInstance *instance = (NativeScriptInstance *)instance_create(owner);
  300. owner->set_script_instance(instance);
  301. if (!instance) {
  302. if (ref.is_null()) {
  303. memdelete(owner); //no owner, sorry
  304. }
  305. return Variant();
  306. }
  307. call("_init", p_args, p_argcount, r_error);
  308. if (r_error.error != Variant::CallError::CALL_OK) {
  309. instance->script = Ref<NativeScript>();
  310. instance->owner->set_script_instance(NULL);
  311. #ifndef NO_THREADS
  312. owners_lock->lock();
  313. #endif
  314. instance_owners.erase(owner);
  315. #ifndef NO_THREADS
  316. owners_lock->unlock();
  317. #endif
  318. ERR_FAIL_COND_V(r_error.error != Variant::CallError::CALL_OK, Variant());
  319. }
  320. if (ref.is_valid()) {
  321. return ref;
  322. } else {
  323. return owner;
  324. }
  325. }
  326. // TODO(karroffel): implement this
  327. NativeScript::NativeScript() {
  328. library = Ref<GDNative>();
  329. lib_path = "";
  330. class_name = "";
  331. #ifndef NO_THREADS
  332. owners_lock = Mutex::create();
  333. #endif
  334. }
  335. // TODO(karroffel): implement this
  336. NativeScript::~NativeScript() {
  337. NSL->unregister_script(this);
  338. #ifndef NO_THREADS
  339. memdelete(owners_lock);
  340. #endif
  341. }
  342. ////// ScriptInstance stuff
  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. Map<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. Map<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. Map<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. // TODO(karroffel): implement this
  550. NativeScriptInstance::RPCMode NativeScriptInstance::get_rset_mode(const StringName &p_variable) const {
  551. NativeScriptDesc *script_data = GET_SCRIPT_DESC();
  552. while (script_data) {
  553. Map<StringName, NativeScriptDesc::Property>::Element *E = script_data->properties.find(p_variable);
  554. if (E) {
  555. switch (E->get().rset_mode) {
  556. case GODOT_METHOD_RPC_MODE_DISABLED:
  557. return RPC_MODE_DISABLED;
  558. case GODOT_METHOD_RPC_MODE_REMOTE:
  559. return RPC_MODE_REMOTE;
  560. case GODOT_METHOD_RPC_MODE_SYNC:
  561. return RPC_MODE_SYNC;
  562. case GODOT_METHOD_RPC_MODE_MASTER:
  563. return RPC_MODE_MASTER;
  564. case GODOT_METHOD_RPC_MODE_SLAVE:
  565. return RPC_MODE_SLAVE;
  566. default:
  567. return RPC_MODE_DISABLED;
  568. }
  569. }
  570. script_data = script_data->base_data;
  571. }
  572. return RPC_MODE_DISABLED;
  573. }
  574. ScriptLanguage *NativeScriptInstance::get_language() {
  575. return NativeScriptLanguage::get_singleton();
  576. }
  577. void NativeScriptInstance::call_multilevel(const StringName &p_method, const Variant **p_args, int p_argcount) {
  578. NativeScriptDesc *script_data = GET_SCRIPT_DESC();
  579. while (script_data) {
  580. Map<StringName, NativeScriptDesc::Method>::Element *E = script_data->methods.find(p_method);
  581. if (E) {
  582. godot_variant res = E->get().method.method((godot_object *)owner,
  583. E->get().method.method_data,
  584. userdata,
  585. p_argcount,
  586. (godot_variant **)p_args);
  587. godot_variant_destroy(&res);
  588. }
  589. script_data = script_data->base_data;
  590. }
  591. }
  592. void NativeScriptInstance::call_multilevel_reversed(const StringName &p_method, const Variant **p_args, int p_argcount) {
  593. NativeScriptDesc *script_data = GET_SCRIPT_DESC();
  594. if (script_data) {
  595. _ml_call_reversed(script_data, p_method, p_args, p_argcount);
  596. }
  597. }
  598. NativeScriptInstance::~NativeScriptInstance() {
  599. NativeScriptDesc *script_data = GET_SCRIPT_DESC();
  600. if (!script_data)
  601. return;
  602. script_data->destroy_func.destroy_func((godot_object *)owner, script_data->destroy_func.method_data, userdata);
  603. if (owner) {
  604. #ifndef NO_THREADS
  605. script->owners_lock->lock();
  606. #endif
  607. script->instance_owners.erase(owner);
  608. #ifndef NO_THREADS
  609. script->owners_lock->lock();
  610. #endif
  611. }
  612. }
  613. ////// ScriptingLanguage stuff
  614. NativeScriptLanguage *NativeScriptLanguage::singleton;
  615. extern "C" void _native_script_hook();
  616. void NativeScriptLanguage::_hacky_api_anchor() {
  617. _native_script_hook();
  618. }
  619. void NativeScriptLanguage::_unload_stuff() {
  620. for (Map<String, Map<StringName, NativeScriptDesc> >::Element *L = library_classes.front(); L; L = L->next()) {
  621. for (Map<StringName, NativeScriptDesc>::Element *C = L->get().front(); C; C = C->next()) {
  622. // free property stuff first
  623. for (Map<StringName, NativeScriptDesc::Property>::Element *P = C->get().properties.front(); P; P = P->next()) {
  624. if (P->get().getter.free_func)
  625. P->get().getter.free_func(P->get().getter.method_data);
  626. if (P->get().setter.free_func)
  627. P->get().setter.free_func(P->get().setter.method_data);
  628. }
  629. // free method stuff
  630. for (Map<StringName, NativeScriptDesc::Method>::Element *M = C->get().methods.front(); M; M = M->next()) {
  631. if (M->get().method.free_func)
  632. M->get().method.free_func(M->get().method.method_data);
  633. }
  634. // free constructor/destructor
  635. if (C->get().create_func.free_func)
  636. C->get().create_func.free_func(C->get().create_func.method_data);
  637. if (C->get().destroy_func.free_func)
  638. C->get().destroy_func.free_func(C->get().destroy_func.method_data);
  639. }
  640. }
  641. }
  642. NativeScriptLanguage::NativeScriptLanguage() {
  643. NativeScriptLanguage::singleton = this;
  644. #ifndef NO_THREADS
  645. mutex = Mutex::create();
  646. #endif
  647. }
  648. // TODO(karroffel): implement this
  649. NativeScriptLanguage::~NativeScriptLanguage() {
  650. // _unload_stuff(); // NOTE(karroffel): This gets called in ::finish()
  651. for (Map<String, Ref<GDNative> >::Element *L = NSL->library_gdnatives.front(); L; L = L->next()) {
  652. L->get()->terminate();
  653. NSL->library_classes.clear();
  654. NSL->library_gdnatives.clear();
  655. NSL->library_script_users.clear();
  656. }
  657. #ifndef NO_THREADS
  658. memdelete(mutex);
  659. #endif
  660. }
  661. String NativeScriptLanguage::get_name() const {
  662. return "NativeScript";
  663. }
  664. void _add_reload_node() {
  665. #ifdef TOOLS_ENABLED
  666. NativeReloadNode *rn = memnew(NativeReloadNode);
  667. EditorNode::get_singleton()->add_child(rn);
  668. #endif
  669. }
  670. // TODO(karroffel): implement this
  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. }
  680. #endif
  681. #ifdef TOOLS_ENABLED
  682. EditorNode::add_init_callback(&_add_reload_node);
  683. #endif
  684. }
  685. String NativeScriptLanguage::get_type() const {
  686. return "NativeScript";
  687. }
  688. String NativeScriptLanguage::get_extension() const {
  689. return "gdns";
  690. }
  691. Error NativeScriptLanguage::execute_file(const String &p_path) {
  692. return OK; // Qué?
  693. }
  694. void NativeScriptLanguage::finish() {
  695. _unload_stuff();
  696. }
  697. void NativeScriptLanguage::get_reserved_words(List<String> *p_words) const {
  698. }
  699. void NativeScriptLanguage::get_comment_delimiters(List<String> *p_delimiters) const {
  700. }
  701. void NativeScriptLanguage::get_string_delimiters(List<String> *p_delimiters) const {
  702. }
  703. // TODO(karroffel): implement this
  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. // TODO(karroffel): use p_base_class_name
  708. return Ref<NativeScript>(s);
  709. }
  710. 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 {
  711. return false;
  712. }
  713. Script *NativeScriptLanguage::create_script() const {
  714. NativeScript *script = memnew(NativeScript);
  715. return script;
  716. }
  717. bool NativeScriptLanguage::has_named_classes() const {
  718. return true;
  719. }
  720. int NativeScriptLanguage::find_function(const String &p_function, const String &p_code) const {
  721. return -1;
  722. }
  723. String NativeScriptLanguage::make_function(const String &p_class, const String &p_name, const PoolStringArray &p_args) const {
  724. return "";
  725. }
  726. void NativeScriptLanguage::auto_indent_code(String &p_code, int p_from_line, int p_to_line) const {
  727. }
  728. void NativeScriptLanguage::add_global_constant(const StringName &p_variable, const Variant &p_value) {
  729. }
  730. // Debugging stuff here. Not used for now.
  731. String NativeScriptLanguage::debug_get_error() const {
  732. return "";
  733. }
  734. int NativeScriptLanguage::debug_get_stack_level_count() const {
  735. return -1;
  736. }
  737. int NativeScriptLanguage::debug_get_stack_level_line(int p_level) const {
  738. return -1;
  739. }
  740. String NativeScriptLanguage::debug_get_stack_level_function(int p_level) const {
  741. return "";
  742. }
  743. String NativeScriptLanguage::debug_get_stack_level_source(int p_level) const {
  744. return "";
  745. }
  746. 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) {
  747. }
  748. 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) {
  749. }
  750. void NativeScriptLanguage::debug_get_globals(List<String> *p_locals, List<Variant> *p_values, int p_max_subitems, int p_max_depth) {
  751. }
  752. String NativeScriptLanguage::debug_parse_stack_level_expression(int p_level, const String &p_expression, int p_max_subitems, int p_max_depth) {
  753. return "";
  754. }
  755. // Debugging stuff end.
  756. void NativeScriptLanguage::reload_all_scripts() {
  757. }
  758. void NativeScriptLanguage::reload_tool_script(const Ref<Script> &p_script, bool p_soft_reload) {
  759. }
  760. void NativeScriptLanguage::get_recognized_extensions(List<String> *p_extensions) const {
  761. p_extensions->push_back("gdns");
  762. }
  763. void NativeScriptLanguage::get_public_functions(List<MethodInfo> *p_functions) const {
  764. }
  765. void NativeScriptLanguage::get_public_constants(List<Pair<String, Variant> > *p_constants) const {
  766. }
  767. void NativeScriptLanguage::profiling_start() {
  768. }
  769. void NativeScriptLanguage::profiling_stop() {
  770. }
  771. int NativeScriptLanguage::profiling_get_accumulated_data(ProfilingInfo *p_info_arr, int p_info_max) {
  772. return -1;
  773. }
  774. int NativeScriptLanguage::profiling_get_frame_data(ProfilingInfo *p_info_arr, int p_info_max) {
  775. return -1;
  776. }
  777. #ifndef NO_THREADS
  778. void NativeScriptLanguage::defer_init_library(Ref<GDNativeLibrary> lib, NativeScript *script) {
  779. MutexLock lock(mutex);
  780. libs_to_init.insert(lib);
  781. scripts_to_register.insert(script);
  782. has_objects_to_register = true;
  783. }
  784. #endif
  785. void NativeScriptLanguage::init_library(const Ref<GDNativeLibrary> &lib) {
  786. #ifndef NO_THREADS
  787. MutexLock lock(mutex);
  788. #endif
  789. // See if this library was "registered" already.
  790. const String &lib_path = lib->get_active_library_path();
  791. Map<String, Ref<GDNative> >::Element *E = library_gdnatives.find(lib_path);
  792. if (!E) {
  793. Ref<GDNative> gdn;
  794. gdn.instance();
  795. gdn->set_library(lib);
  796. // TODO(karroffel): check the return value?
  797. gdn->initialize();
  798. library_gdnatives.insert(lib_path, gdn);
  799. library_classes.insert(lib_path, Map<StringName, NativeScriptDesc>());
  800. if (!library_script_users.has(lib_path))
  801. library_script_users.insert(lib_path, Set<NativeScript *>());
  802. void *args[1] = {
  803. (void *)&lib_path
  804. };
  805. // here the library registers all the classes and stuff.
  806. gdn->call_native_raw(_init_call_type,
  807. _init_call_name,
  808. NULL,
  809. 1,
  810. args,
  811. NULL);
  812. } else {
  813. // already initialized. Nice.
  814. }
  815. }
  816. void NativeScriptLanguage::register_script(NativeScript *script) {
  817. #ifndef NO_THREADS
  818. MutexLock lock(mutex);
  819. #endif
  820. library_script_users[script->lib_path].insert(script);
  821. }
  822. void NativeScriptLanguage::unregister_script(NativeScript *script) {
  823. #ifndef NO_THREADS
  824. MutexLock lock(mutex);
  825. #endif
  826. Map<String, Set<NativeScript *> >::Element *S = library_script_users.find(script->lib_path);
  827. if (S) {
  828. S->get().erase(script);
  829. if (S->get().size() == 0) {
  830. library_script_users.erase(S);
  831. }
  832. }
  833. #ifndef NO_THREADS
  834. scripts_to_register.erase(script);
  835. #endif
  836. }
  837. #ifndef NO_THREADS
  838. void NativeScriptLanguage::frame() {
  839. if (has_objects_to_register) {
  840. MutexLock lock(mutex);
  841. for (Set<Ref<GDNativeLibrary> >::Element *L = libs_to_init.front(); L; L = L->next()) {
  842. init_library(L->get());
  843. }
  844. libs_to_init.clear();
  845. for (Set<NativeScript *>::Element *S = scripts_to_register.front(); S; S = S->next()) {
  846. register_script(S->get());
  847. }
  848. scripts_to_register.clear();
  849. has_objects_to_register = false;
  850. }
  851. }
  852. void NativeScriptLanguage::thread_enter() {
  853. Vector<Ref<GDNative> > libs;
  854. {
  855. MutexLock lock(mutex);
  856. for (Map<String, Ref<GDNative> >::Element *L = library_gdnatives.front(); L; L = L->next()) {
  857. libs.push_back(L->get());
  858. }
  859. }
  860. for (int i = 0; i < libs.size(); ++i) {
  861. libs[i]->call_native_raw(
  862. _thread_cb_call_type,
  863. _thread_enter_call_name,
  864. NULL,
  865. 0,
  866. NULL,
  867. NULL);
  868. }
  869. }
  870. void NativeScriptLanguage::thread_exit() {
  871. Vector<Ref<GDNative> > libs;
  872. {
  873. MutexLock lock(mutex);
  874. for (Map<String, Ref<GDNative> >::Element *L = library_gdnatives.front(); L; L = L->next()) {
  875. libs.push_back(L->get());
  876. }
  877. }
  878. for (int i = 0; i < libs.size(); ++i) {
  879. libs[i]->call_native_raw(
  880. _thread_cb_call_type,
  881. _thread_exit_call_name,
  882. NULL,
  883. 0,
  884. NULL,
  885. NULL);
  886. }
  887. }
  888. #endif // NO_THREADS
  889. void NativeReloadNode::_bind_methods() {
  890. ClassDB::bind_method(D_METHOD("_notification"), &NativeReloadNode::_notification);
  891. }
  892. void NativeReloadNode::_notification(int p_what) {
  893. #ifdef TOOLS_ENABLED
  894. switch (p_what) {
  895. case MainLoop::NOTIFICATION_WM_FOCUS_OUT: {
  896. if (unloaded)
  897. break;
  898. #ifndef NO_THREADS
  899. MutexLock lock(NSL->mutex);
  900. #endif
  901. NSL->_unload_stuff();
  902. for (Map<String, Ref<GDNative> >::Element *L = NSL->library_gdnatives.front(); L; L = L->next()) {
  903. L->get()->terminate();
  904. NSL->library_classes.erase(L->key());
  905. }
  906. unloaded = true;
  907. } break;
  908. case MainLoop::NOTIFICATION_WM_FOCUS_IN: {
  909. if (!unloaded)
  910. break;
  911. #ifndef NO_THREADS
  912. MutexLock lock(NSL->mutex);
  913. #endif
  914. Set<StringName> libs_to_remove;
  915. for (Map<String, Ref<GDNative> >::Element *L = NSL->library_gdnatives.front(); L; L = L->next()) {
  916. if (!L->get()->initialize()) {
  917. libs_to_remove.insert(L->key());
  918. continue;
  919. }
  920. NSL->library_classes.insert(L->key(), Map<StringName, NativeScriptDesc>());
  921. void *args[1] = {
  922. (void *)&L->key()
  923. };
  924. // here the library registers all the classes and stuff.
  925. L->get()->call_native_raw(NSL->_init_call_type,
  926. NSL->_init_call_name,
  927. NULL,
  928. 1,
  929. args,
  930. NULL);
  931. for (Map<String, Set<NativeScript *> >::Element *U = NSL->library_script_users.front(); U; U = U->next()) {
  932. for (Set<NativeScript *>::Element *S = U->get().front(); S; S = S->next()) {
  933. NativeScript *script = S->get();
  934. if (script->placeholders.size() == 0)
  935. continue;
  936. for (Set<PlaceHolderScriptInstance *>::Element *P = script->placeholders.front(); P; P = P->next()) {
  937. script->_update_placeholder(P->get());
  938. }
  939. }
  940. }
  941. }
  942. unloaded = false;
  943. for (Set<StringName>::Element *R = libs_to_remove.front(); R; R = R->next()) {
  944. NSL->library_gdnatives.erase(R->get());
  945. }
  946. } break;
  947. default: {
  948. };
  949. }
  950. #endif
  951. }
  952. RES ResourceFormatLoaderNativeScript::load(const String &p_path, const String &p_original_path, Error *r_error) {
  953. ResourceFormatLoaderText rsflt;
  954. return rsflt.load(p_path, p_original_path, r_error);
  955. }
  956. void ResourceFormatLoaderNativeScript::get_recognized_extensions(List<String> *p_extensions) const {
  957. p_extensions->push_back("gdns");
  958. }
  959. bool ResourceFormatLoaderNativeScript::handles_type(const String &p_type) const {
  960. return (p_type == "Script" || p_type == "NativeScript");
  961. }
  962. String ResourceFormatLoaderNativeScript::get_resource_type(const String &p_path) const {
  963. String el = p_path.get_extension().to_lower();
  964. if (el == "gdns")
  965. return "NativeScript";
  966. return "";
  967. }
  968. Error ResourceFormatSaverNativeScript::save(const String &p_path, const RES &p_resource, uint32_t p_flags) {
  969. ResourceFormatSaverText rfst;
  970. return rfst.save(p_path, p_resource, p_flags);
  971. }
  972. bool ResourceFormatSaverNativeScript::recognize(const RES &p_resource) const {
  973. return p_resource->cast_to<NativeScript>() != NULL;
  974. }
  975. void ResourceFormatSaverNativeScript::get_recognized_extensions(const RES &p_resource, List<String> *p_extensions) const {
  976. if (p_resource->cast_to<NativeScript>()) {
  977. p_extensions->push_back("gdns");
  978. }
  979. }