nativescript.cpp 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085
  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().getter.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. void NativeScriptInstance::refcount_incremented() {
  494. Variant::CallError err;
  495. call("_refcount_incremented", NULL, 0, err);
  496. if (err.error != Variant::CallError::CALL_OK && err.error != Variant::CallError::CALL_ERROR_INVALID_METHOD) {
  497. ERR_PRINT("Failed to invoke _refcount_incremented - should not happen");
  498. }
  499. }
  500. bool NativeScriptInstance::refcount_decremented() {
  501. Variant::CallError err;
  502. Variant ret = call("_refcount_decremented", NULL, 0, err);
  503. if (err.error != Variant::CallError::CALL_OK && err.error != Variant::CallError::CALL_ERROR_INVALID_METHOD) {
  504. ERR_PRINT("Failed to invoke _refcount_decremented - should not happen");
  505. return true; // assume we can destroy the object
  506. }
  507. if (err.error == Variant::CallError::CALL_ERROR_INVALID_METHOD) {
  508. // the method does not exist, default is true
  509. return true;
  510. }
  511. return ret;
  512. }
  513. Ref<Script> NativeScriptInstance::get_script() const {
  514. return script;
  515. }
  516. NativeScriptInstance::RPCMode NativeScriptInstance::get_rpc_mode(const StringName &p_method) const {
  517. NativeScriptDesc *script_data = GET_SCRIPT_DESC();
  518. while (script_data) {
  519. Map<StringName, NativeScriptDesc::Method>::Element *E = script_data->methods.find(p_method);
  520. if (E) {
  521. switch (E->get().rpc_mode) {
  522. case GODOT_METHOD_RPC_MODE_DISABLED:
  523. return RPC_MODE_DISABLED;
  524. case GODOT_METHOD_RPC_MODE_REMOTE:
  525. return RPC_MODE_REMOTE;
  526. case GODOT_METHOD_RPC_MODE_SYNC:
  527. return RPC_MODE_SYNC;
  528. case GODOT_METHOD_RPC_MODE_MASTER:
  529. return RPC_MODE_MASTER;
  530. case GODOT_METHOD_RPC_MODE_SLAVE:
  531. return RPC_MODE_SLAVE;
  532. default:
  533. return RPC_MODE_DISABLED;
  534. }
  535. }
  536. script_data = script_data->base_data;
  537. }
  538. return RPC_MODE_DISABLED;
  539. }
  540. // TODO(karroffel): implement this
  541. NativeScriptInstance::RPCMode NativeScriptInstance::get_rset_mode(const StringName &p_variable) const {
  542. NativeScriptDesc *script_data = GET_SCRIPT_DESC();
  543. while (script_data) {
  544. Map<StringName, NativeScriptDesc::Property>::Element *E = script_data->properties.find(p_variable);
  545. if (E) {
  546. switch (E->get().rset_mode) {
  547. case GODOT_METHOD_RPC_MODE_DISABLED:
  548. return RPC_MODE_DISABLED;
  549. case GODOT_METHOD_RPC_MODE_REMOTE:
  550. return RPC_MODE_REMOTE;
  551. case GODOT_METHOD_RPC_MODE_SYNC:
  552. return RPC_MODE_SYNC;
  553. case GODOT_METHOD_RPC_MODE_MASTER:
  554. return RPC_MODE_MASTER;
  555. case GODOT_METHOD_RPC_MODE_SLAVE:
  556. return RPC_MODE_SLAVE;
  557. default:
  558. return RPC_MODE_DISABLED;
  559. }
  560. }
  561. script_data = script_data->base_data;
  562. }
  563. return RPC_MODE_DISABLED;
  564. }
  565. ScriptLanguage *NativeScriptInstance::get_language() {
  566. return NativeScriptLanguage::get_singleton();
  567. }
  568. void NativeScriptInstance::call_multilevel(const StringName &p_method, const Variant **p_args, int p_argcount) {
  569. NativeScriptDesc *script_data = GET_SCRIPT_DESC();
  570. while (script_data) {
  571. Map<StringName, NativeScriptDesc::Method>::Element *E = script_data->methods.find(p_method);
  572. if (E) {
  573. godot_variant res = E->get().method.method((godot_object *)owner,
  574. E->get().method.method_data,
  575. userdata,
  576. p_argcount,
  577. (godot_variant **)p_args);
  578. godot_variant_destroy(&res);
  579. }
  580. script_data = script_data->base_data;
  581. }
  582. }
  583. void NativeScriptInstance::call_multilevel_reversed(const StringName &p_method, const Variant **p_args, int p_argcount) {
  584. NativeScriptDesc *script_data = GET_SCRIPT_DESC();
  585. if (script_data) {
  586. _ml_call_reversed(script_data, p_method, p_args, p_argcount);
  587. }
  588. }
  589. NativeScriptInstance::~NativeScriptInstance() {
  590. NativeScriptDesc *script_data = GET_SCRIPT_DESC();
  591. if (!script_data)
  592. return;
  593. script_data->destroy_func.destroy_func((godot_object *)owner, script_data->destroy_func.method_data, userdata);
  594. if (owner) {
  595. script->instance_owners.erase(owner);
  596. }
  597. }
  598. ////// ScriptingLanguage stuff
  599. NativeScriptLanguage *NativeScriptLanguage::singleton;
  600. extern "C" void _native_script_hook();
  601. void NativeScriptLanguage::_hacky_api_anchor() {
  602. _native_script_hook();
  603. }
  604. void NativeScriptLanguage::_unload_stuff() {
  605. for (Map<String, Map<StringName, NativeScriptDesc> >::Element *L = library_classes.front(); L; L = L->next()) {
  606. for (Map<StringName, NativeScriptDesc>::Element *C = L->get().front(); C; C = C->next()) {
  607. // free property stuff first
  608. for (Map<StringName, NativeScriptDesc::Property>::Element *P = C->get().properties.front(); P; P = P->next()) {
  609. if (P->get().getter.free_func)
  610. P->get().getter.free_func(P->get().getter.method_data);
  611. if (P->get().setter.free_func)
  612. P->get().setter.free_func(P->get().setter.method_data);
  613. }
  614. // free method stuff
  615. for (Map<StringName, NativeScriptDesc::Method>::Element *M = C->get().methods.front(); M; M = M->next()) {
  616. if (M->get().method.free_func)
  617. M->get().method.free_func(M->get().method.method_data);
  618. }
  619. // free constructor/destructor
  620. if (C->get().create_func.free_func)
  621. C->get().create_func.free_func(C->get().create_func.method_data);
  622. if (C->get().destroy_func.free_func)
  623. C->get().destroy_func.free_func(C->get().destroy_func.method_data);
  624. }
  625. }
  626. }
  627. NativeScriptLanguage::NativeScriptLanguage() {
  628. NativeScriptLanguage::singleton = this;
  629. }
  630. // TODO(karroffel): implement this
  631. NativeScriptLanguage::~NativeScriptLanguage() {
  632. // _unload_stuff(); // NOTE(karroffel): This gets called in ::finish()
  633. for (Map<String, Ref<GDNative> >::Element *L = NSL->library_gdnatives.front(); L; L = L->next()) {
  634. L->get()->terminate();
  635. NSL->library_classes.clear();
  636. NSL->library_gdnatives.clear();
  637. NSL->library_script_users.clear();
  638. }
  639. }
  640. String NativeScriptLanguage::get_name() const {
  641. return "NativeScript";
  642. }
  643. void _add_reload_node() {
  644. #ifdef TOOLS_ENABLED
  645. NativeReloadNode *rn = memnew(NativeReloadNode);
  646. EditorNode::get_singleton()->add_child(rn);
  647. #endif
  648. }
  649. // TODO(karroffel): implement this
  650. void NativeScriptLanguage::init() {
  651. #if defined(TOOLS_ENABLED) && defined(DEBUG_METHODS_ENABLED)
  652. List<String> args = OS::get_singleton()->get_cmdline_args();
  653. List<String>::Element *E = args.find("--gdnative-generate-json-api");
  654. if (E && E->next()) {
  655. if (generate_c_api(E->next()->get()) != OK) {
  656. ERR_PRINT("Failed to generate C API\n");
  657. }
  658. }
  659. #endif
  660. #ifdef TOOLS_ENABLED
  661. EditorNode::add_init_callback(&_add_reload_node);
  662. #endif
  663. }
  664. String NativeScriptLanguage::get_type() const {
  665. return "NativeScript";
  666. }
  667. String NativeScriptLanguage::get_extension() const {
  668. return "gdns";
  669. }
  670. Error NativeScriptLanguage::execute_file(const String &p_path) {
  671. return OK; // Qué?
  672. }
  673. void NativeScriptLanguage::finish() {
  674. _unload_stuff();
  675. }
  676. void NativeScriptLanguage::get_reserved_words(List<String> *p_words) const {
  677. }
  678. void NativeScriptLanguage::get_comment_delimiters(List<String> *p_delimiters) const {
  679. }
  680. void NativeScriptLanguage::get_string_delimiters(List<String> *p_delimiters) const {
  681. }
  682. // TODO(karroffel): implement this
  683. Ref<Script> NativeScriptLanguage::get_template(const String &p_class_name, const String &p_base_class_name) const {
  684. NativeScript *s = memnew(NativeScript);
  685. s->set_class_name(p_class_name);
  686. // TODO(karroffel): use p_base_class_name
  687. return Ref<NativeScript>(s);
  688. }
  689. 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 {
  690. return false;
  691. }
  692. Script *NativeScriptLanguage::create_script() const {
  693. NativeScript *script = memnew(NativeScript);
  694. return script;
  695. }
  696. bool NativeScriptLanguage::has_named_classes() const {
  697. return true;
  698. }
  699. int NativeScriptLanguage::find_function(const String &p_function, const String &p_code) const {
  700. return -1;
  701. }
  702. String NativeScriptLanguage::make_function(const String &p_class, const String &p_name, const PoolStringArray &p_args) const {
  703. return "";
  704. }
  705. void NativeScriptLanguage::auto_indent_code(String &p_code, int p_from_line, int p_to_line) const {
  706. }
  707. void NativeScriptLanguage::add_global_constant(const StringName &p_variable, const Variant &p_value) {
  708. }
  709. // Debugging stuff here. Not used for now.
  710. String NativeScriptLanguage::debug_get_error() const {
  711. return "";
  712. }
  713. int NativeScriptLanguage::debug_get_stack_level_count() const {
  714. return -1;
  715. }
  716. int NativeScriptLanguage::debug_get_stack_level_line(int p_level) const {
  717. return -1;
  718. }
  719. String NativeScriptLanguage::debug_get_stack_level_function(int p_level) const {
  720. return "";
  721. }
  722. String NativeScriptLanguage::debug_get_stack_level_source(int p_level) const {
  723. return "";
  724. }
  725. 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) {
  726. }
  727. 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) {
  728. }
  729. void NativeScriptLanguage::debug_get_globals(List<String> *p_locals, List<Variant> *p_values, int p_max_subitems, int p_max_depth) {
  730. }
  731. String NativeScriptLanguage::debug_parse_stack_level_expression(int p_level, const String &p_expression, int p_max_subitems, int p_max_depth) {
  732. return "";
  733. }
  734. // Debugging stuff end.
  735. void NativeScriptLanguage::reload_all_scripts() {
  736. }
  737. void NativeScriptLanguage::reload_tool_script(const Ref<Script> &p_script, bool p_soft_reload) {
  738. }
  739. void NativeScriptLanguage::get_recognized_extensions(List<String> *p_extensions) const {
  740. p_extensions->push_back("gdns");
  741. }
  742. void NativeScriptLanguage::get_public_functions(List<MethodInfo> *p_functions) const {
  743. }
  744. void NativeScriptLanguage::get_public_constants(List<Pair<String, Variant> > *p_constants) const {
  745. }
  746. void NativeScriptLanguage::profiling_start() {
  747. }
  748. void NativeScriptLanguage::profiling_stop() {
  749. }
  750. int NativeScriptLanguage::profiling_get_accumulated_data(ProfilingInfo *p_info_arr, int p_info_max) {
  751. return -1;
  752. }
  753. int NativeScriptLanguage::profiling_get_frame_data(ProfilingInfo *p_info_arr, int p_info_max) {
  754. return -1;
  755. }
  756. void NativeReloadNode::_bind_methods() {
  757. ClassDB::bind_method(D_METHOD("_notification"), &NativeReloadNode::_notification);
  758. }
  759. void NativeReloadNode::_notification(int p_what) {
  760. #ifdef TOOLS_ENABLED
  761. switch (p_what) {
  762. case MainLoop::NOTIFICATION_WM_FOCUS_OUT: {
  763. if (unloaded)
  764. break;
  765. NSL->_unload_stuff();
  766. for (Map<String, Ref<GDNative> >::Element *L = NSL->library_gdnatives.front(); L; L = L->next()) {
  767. L->get()->terminate();
  768. NSL->library_classes.erase(L->key());
  769. }
  770. unloaded = true;
  771. } break;
  772. case MainLoop::NOTIFICATION_WM_FOCUS_IN: {
  773. if (!unloaded)
  774. break;
  775. Set<StringName> libs_to_remove;
  776. for (Map<String, Ref<GDNative> >::Element *L = NSL->library_gdnatives.front(); L; L = L->next()) {
  777. if (!L->get()->initialize()) {
  778. libs_to_remove.insert(L->key());
  779. continue;
  780. }
  781. NSL->library_classes.insert(L->key(), Map<StringName, NativeScriptDesc>());
  782. void *args[1] = {
  783. (void *)&L->key()
  784. };
  785. // here the library registers all the classes and stuff.
  786. L->get()->call_native_raw(NSL->_init_call_type,
  787. NSL->_init_call_name,
  788. NULL,
  789. 1,
  790. args,
  791. NULL);
  792. for (Map<String, Set<NativeScript *> >::Element *U = NSL->library_script_users.front(); U; U = U->next()) {
  793. for (Set<NativeScript *>::Element *S = U->get().front(); S; S = S->next()) {
  794. NativeScript *script = S->get();
  795. if (script->placeholders.size() == 0)
  796. continue;
  797. for (Set<PlaceHolderScriptInstance *>::Element *P = script->placeholders.front(); P; P = P->next()) {
  798. script->_update_placeholder(P->get());
  799. }
  800. }
  801. }
  802. }
  803. unloaded = false;
  804. for (Set<StringName>::Element *R = libs_to_remove.front(); R; R = R->next()) {
  805. NSL->library_gdnatives.erase(R->get());
  806. }
  807. } break;
  808. default: {
  809. };
  810. }
  811. #endif
  812. }
  813. RES ResourceFormatLoaderNativeScript::load(const String &p_path, const String &p_original_path, Error *r_error) {
  814. ResourceFormatLoaderText rsflt;
  815. return rsflt.load(p_path, p_original_path, r_error);
  816. }
  817. void ResourceFormatLoaderNativeScript::get_recognized_extensions(List<String> *p_extensions) const {
  818. p_extensions->push_back("gdns");
  819. }
  820. bool ResourceFormatLoaderNativeScript::handles_type(const String &p_type) const {
  821. return (p_type == "Script" || p_type == "NativeScript");
  822. }
  823. String ResourceFormatLoaderNativeScript::get_resource_type(const String &p_path) const {
  824. String el = p_path.get_extension().to_lower();
  825. if (el == "gdns")
  826. return "NativeScript";
  827. return "";
  828. }
  829. Error ResourceFormatSaverNativeScript::save(const String &p_path, const RES &p_resource, uint32_t p_flags) {
  830. ResourceFormatSaverText rfst;
  831. return rfst.save(p_path, p_resource, p_flags);
  832. }
  833. bool ResourceFormatSaverNativeScript::recognize(const RES &p_resource) const {
  834. return p_resource->cast_to<NativeScript>() != NULL;
  835. }
  836. void ResourceFormatSaverNativeScript::get_recognized_extensions(const RES &p_resource, List<String> *p_extensions) const {
  837. if (p_resource->cast_to<NativeScript>()) {
  838. p_extensions->push_back("gdns");
  839. }
  840. }