nativescript.cpp 33 KB

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