nativescript.cpp 29 KB

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