nativescript.cpp 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208
  1. /*************************************************************************/
  2. /* nativescript.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "nativescript.h"
  31. #include "gdnative/gdnative.h"
  32. #include "global_constants.h"
  33. #include "io/file_access_encrypted.h"
  34. #include "os/file_access.h"
  35. #include "os/os.h"
  36. #include "project_settings.h"
  37. #include "scene/main/scene_tree.h"
  38. #include "scene/resources/scene_format_text.h"
  39. #ifndef NO_THREADS
  40. #include "os/thread.h"
  41. #endif
  42. #if defined(TOOLS_ENABLED) && defined(DEBUG_METHODS_ENABLED)
  43. #include "api_generator.h"
  44. #endif
  45. #ifdef TOOLS_ENABLED
  46. #include "editor/editor_node.h"
  47. #endif
  48. ////// Script stuff
  49. void NativeScript::_bind_methods() {
  50. ClassDB::bind_method(D_METHOD("set_class_name", "class_name"), &NativeScript::set_class_name);
  51. ClassDB::bind_method(D_METHOD("get_class_name"), &NativeScript::get_class_name);
  52. ClassDB::bind_method(D_METHOD("set_library", "library"), &NativeScript::set_library);
  53. ClassDB::bind_method(D_METHOD("get_library"), &NativeScript::get_library);
  54. ADD_PROPERTYNZ(PropertyInfo(Variant::STRING, "class_name"), "set_class_name", "get_class_name");
  55. ADD_PROPERTYNZ(PropertyInfo(Variant::OBJECT, "library", PROPERTY_HINT_RESOURCE_TYPE, "GDNativeLibrary"), "set_library", "get_library");
  56. ClassDB::bind_vararg_method(METHOD_FLAGS_DEFAULT, "new", &NativeScript::_new, MethodInfo(Variant::OBJECT, "new"));
  57. }
  58. #define NSL NativeScriptLanguage::get_singleton()
  59. #ifdef TOOLS_ENABLED
  60. void NativeScript::_update_placeholder(PlaceHolderScriptInstance *p_placeholder) {
  61. NativeScriptDesc *script_data = get_script_desc();
  62. ERR_FAIL_COND(!script_data);
  63. List<PropertyInfo> info;
  64. get_script_property_list(&info);
  65. Map<StringName, Variant> values;
  66. for (List<PropertyInfo>::Element *E = info.front(); E; E = E->next()) {
  67. Variant value;
  68. get_property_default_value(E->get().name, value);
  69. values[E->get().name] = value;
  70. }
  71. p_placeholder->update(info, values);
  72. }
  73. void NativeScript::_placeholder_erased(PlaceHolderScriptInstance *p_placeholder) {
  74. placeholders.erase(p_placeholder);
  75. }
  76. #endif
  77. void NativeScript::set_class_name(String p_class_name) {
  78. class_name = p_class_name;
  79. }
  80. String NativeScript::get_class_name() const {
  81. return class_name;
  82. }
  83. void NativeScript::set_library(Ref<GDNativeLibrary> p_library) {
  84. if (!library.is_null()) {
  85. WARN_PRINT("library on NativeScript already set. Do nothing.");
  86. return;
  87. }
  88. library = p_library;
  89. lib_path = library->get_active_library_path();
  90. #ifndef NO_THREADS
  91. if (Thread::get_caller_id() != Thread::get_main_id()) {
  92. NSL->defer_init_library(p_library, this);
  93. } else
  94. #endif
  95. {
  96. NSL->init_library(p_library);
  97. NSL->register_script(this);
  98. }
  99. }
  100. Ref<GDNativeLibrary> NativeScript::get_library() const {
  101. return library;
  102. }
  103. bool NativeScript::can_instance() const {
  104. NativeScriptDesc *script_data = get_script_desc();
  105. #ifdef TOOLS_ENABLED
  106. return script_data || (!is_tool() && !ScriptServer::is_scripting_enabled());
  107. #else
  108. return script_data;
  109. #endif
  110. }
  111. // TODO(karroffel): implement this
  112. Ref<Script> NativeScript::get_base_script() const {
  113. NativeScriptDesc *script_data = get_script_desc();
  114. if (!script_data)
  115. return Ref<Script>();
  116. Ref<NativeScript> ns = Ref<NativeScript>(NSL->create_script());
  117. ns->set_class_name(script_data->base);
  118. ns->set_library(get_library());
  119. return ns;
  120. }
  121. StringName NativeScript::get_instance_base_type() const {
  122. NativeScriptDesc *script_data = get_script_desc();
  123. if (!script_data)
  124. return "";
  125. return script_data->base_native_type;
  126. }
  127. ScriptInstance *NativeScript::instance_create(Object *p_this) {
  128. NativeScriptDesc *script_data = get_script_desc();
  129. if (!script_data) {
  130. return NULL;
  131. }
  132. #ifdef TOOLS_ENABLED
  133. if (!ScriptServer::is_scripting_enabled() && !is_tool()) {
  134. // placeholder for nodes. For tools we want the rool thing.
  135. PlaceHolderScriptInstance *sins = memnew(PlaceHolderScriptInstance(NSL, Ref<Script>(this), p_this));
  136. placeholders.insert(sins);
  137. if (script_data->create_func.create_func) {
  138. script_data->create_func.create_func(
  139. (godot_object *)p_this,
  140. script_data->create_func.method_data);
  141. }
  142. _update_placeholder(sins);
  143. return sins;
  144. }
  145. #endif
  146. NativeScriptInstance *nsi = memnew(NativeScriptInstance);
  147. nsi->owner = p_this;
  148. nsi->script = Ref<NativeScript>(this);
  149. #ifndef TOOLS_ENABLED
  150. if (!ScriptServer::is_scripting_enabled()) {
  151. nsi->userdata = NULL;
  152. } else {
  153. nsi->userdata = script_data->create_func.create_func((godot_object *)p_this, script_data->create_func.method_data);
  154. }
  155. #else
  156. nsi->userdata = script_data->create_func.create_func((godot_object *)p_this, script_data->create_func.method_data);
  157. #endif
  158. #ifndef NO_THREADS
  159. owners_lock->lock();
  160. #endif
  161. instance_owners.insert(p_this);
  162. #ifndef NO_THREADS
  163. owners_lock->unlock();
  164. #endif
  165. return nsi;
  166. }
  167. bool NativeScript::instance_has(const Object *p_this) const {
  168. return instance_owners.has((Object *)p_this);
  169. }
  170. bool NativeScript::has_source_code() const {
  171. return false;
  172. }
  173. String NativeScript::get_source_code() const {
  174. return "";
  175. }
  176. void NativeScript::set_source_code(const String &p_code) {
  177. }
  178. Error NativeScript::reload(bool p_keep_state) {
  179. return FAILED;
  180. }
  181. bool NativeScript::has_method(const StringName &p_method) const {
  182. NativeScriptDesc *script_data = get_script_desc();
  183. while (script_data) {
  184. if (script_data->methods.has(p_method))
  185. return true;
  186. script_data = script_data->base_data;
  187. }
  188. return false;
  189. }
  190. MethodInfo NativeScript::get_method_info(const StringName &p_method) const {
  191. NativeScriptDesc *script_data = get_script_desc();
  192. if (!script_data)
  193. return MethodInfo();
  194. while (script_data) {
  195. Map<StringName, NativeScriptDesc::Method>::Element *M = script_data->methods.find(p_method);
  196. if (M)
  197. return M->get().info;
  198. script_data = script_data->base_data;
  199. }
  200. return MethodInfo();
  201. }
  202. bool NativeScript::is_tool() const {
  203. NativeScriptDesc *script_data = get_script_desc();
  204. if (script_data)
  205. return script_data->is_tool;
  206. return false;
  207. }
  208. String NativeScript::get_node_type() const {
  209. return ""; // NOTE(karroffel): uhm?
  210. }
  211. ScriptLanguage *NativeScript::get_language() const {
  212. return NativeScriptLanguage::get_singleton();
  213. }
  214. bool NativeScript::has_script_signal(const StringName &p_signal) const {
  215. NativeScriptDesc *script_data = get_script_desc();
  216. while (script_data) {
  217. if (script_data->signals_.has(p_signal))
  218. return true;
  219. script_data = script_data->base_data;
  220. }
  221. return false;
  222. }
  223. void NativeScript::get_script_signal_list(List<MethodInfo> *r_signals) const {
  224. NativeScriptDesc *script_data = get_script_desc();
  225. if (!script_data)
  226. return;
  227. Set<MethodInfo> signals_;
  228. while (script_data) {
  229. for (Map<StringName, NativeScriptDesc::Signal>::Element *S = script_data->signals_.front(); S; S = S->next()) {
  230. signals_.insert(S->get().signal);
  231. }
  232. script_data = script_data->base_data;
  233. }
  234. for (Set<MethodInfo>::Element *E = signals_.front(); E; E = E->next()) {
  235. r_signals->push_back(E->get());
  236. }
  237. }
  238. bool NativeScript::get_property_default_value(const StringName &p_property, Variant &r_value) const {
  239. NativeScriptDesc *script_data = get_script_desc();
  240. OrderedHashMap<StringName, NativeScriptDesc::Property>::Element P;
  241. while (!P && script_data) {
  242. P = script_data->properties.find(p_property);
  243. script_data = script_data->base_data;
  244. }
  245. if (!P)
  246. return false;
  247. r_value = P.get().default_value;
  248. return true;
  249. }
  250. void NativeScript::update_exports() {
  251. }
  252. void NativeScript::get_script_method_list(List<MethodInfo> *p_list) const {
  253. NativeScriptDesc *script_data = get_script_desc();
  254. if (!script_data)
  255. return;
  256. Set<MethodInfo> methods;
  257. while (script_data) {
  258. for (Map<StringName, NativeScriptDesc::Method>::Element *E = script_data->methods.front(); E; E = E->next()) {
  259. methods.insert(E->get().info);
  260. }
  261. script_data = script_data->base_data;
  262. }
  263. for (Set<MethodInfo>::Element *E = methods.front(); E; E = E->next()) {
  264. p_list->push_back(E->get());
  265. }
  266. }
  267. void NativeScript::get_script_property_list(List<PropertyInfo> *p_list) const {
  268. NativeScriptDesc *script_data = get_script_desc();
  269. Set<StringName> existing_properties;
  270. while (script_data) {
  271. List<PropertyInfo>::Element *insert_position = p_list->front();
  272. bool insert_before = true;
  273. for (OrderedHashMap<StringName, NativeScriptDesc::Property>::Element E = script_data->properties.front(); E; E = E.next()) {
  274. if (!existing_properties.has(E.key())) {
  275. insert_position = insert_before ? p_list->insert_before(insert_position, E.get().info) : p_list->insert_after(insert_position, E.get().info);
  276. insert_before = false;
  277. existing_properties.insert(E.key());
  278. }
  279. }
  280. script_data = script_data->base_data;
  281. }
  282. }
  283. Variant NativeScript::_new(const Variant **p_args, int p_argcount, Variant::CallError &r_error) {
  284. if (lib_path.empty() || class_name.empty() || library.is_null()) {
  285. r_error.error = Variant::CallError::CALL_ERROR_INSTANCE_IS_NULL;
  286. return Variant();
  287. }
  288. NativeScriptDesc *script_data = get_script_desc();
  289. if (!script_data) {
  290. r_error.error = Variant::CallError::CALL_ERROR_INSTANCE_IS_NULL;
  291. return Variant();
  292. }
  293. r_error.error = Variant::CallError::CALL_OK;
  294. REF ref;
  295. Object *owner = NULL;
  296. if (!(script_data->base_native_type == "")) {
  297. owner = ClassDB::instance(script_data->base_native_type);
  298. } else {
  299. owner = memnew(Reference);
  300. }
  301. Reference *r = Object::cast_to<Reference>(owner);
  302. if (r) {
  303. ref = REF(r);
  304. }
  305. NativeScriptInstance *instance = (NativeScriptInstance *)instance_create(owner);
  306. owner->set_script_instance(instance);
  307. if (!instance) {
  308. if (ref.is_null()) {
  309. memdelete(owner); //no owner, sorry
  310. }
  311. return Variant();
  312. }
  313. if (ref.is_valid()) {
  314. return ref;
  315. } else {
  316. return owner;
  317. }
  318. }
  319. // TODO(karroffel): implement this
  320. NativeScript::NativeScript() {
  321. library = Ref<GDNative>();
  322. lib_path = "";
  323. class_name = "";
  324. #ifndef NO_THREADS
  325. owners_lock = Mutex::create();
  326. #endif
  327. }
  328. // TODO(karroffel): implement this
  329. NativeScript::~NativeScript() {
  330. NSL->unregister_script(this);
  331. #ifndef NO_THREADS
  332. memdelete(owners_lock);
  333. #endif
  334. }
  335. ////// ScriptInstance stuff
  336. #define GET_SCRIPT_DESC() script->get_script_desc()
  337. void NativeScriptInstance::_ml_call_reversed(NativeScriptDesc *script_data, const StringName &p_method, const Variant **p_args, int p_argcount) {
  338. if (script_data->base_data) {
  339. _ml_call_reversed(script_data->base_data, p_method, p_args, p_argcount);
  340. }
  341. Map<StringName, NativeScriptDesc::Method>::Element *E = script_data->methods.find(p_method);
  342. if (E) {
  343. godot_variant res = E->get().method.method((godot_object *)owner, E->get().method.method_data, userdata, p_argcount, (godot_variant **)p_args);
  344. godot_variant_destroy(&res);
  345. }
  346. }
  347. bool NativeScriptInstance::set(const StringName &p_name, const Variant &p_value) {
  348. NativeScriptDesc *script_data = GET_SCRIPT_DESC();
  349. while (script_data) {
  350. OrderedHashMap<StringName, NativeScriptDesc::Property>::Element P = script_data->properties.find(p_name);
  351. if (P) {
  352. P.get().setter.set_func((godot_object *)owner,
  353. P.get().setter.method_data,
  354. userdata,
  355. (godot_variant *)&p_value);
  356. return true;
  357. }
  358. Map<StringName, NativeScriptDesc::Method>::Element *E = script_data->methods.find("_set");
  359. if (E) {
  360. Variant name = p_name;
  361. const Variant *args[2] = { &name, &p_value };
  362. E->get().method.method((godot_object *)owner,
  363. E->get().method.method_data,
  364. userdata,
  365. 2,
  366. (godot_variant **)args);
  367. return true;
  368. }
  369. script_data = script_data->base_data;
  370. }
  371. return false;
  372. }
  373. bool NativeScriptInstance::get(const StringName &p_name, Variant &r_ret) const {
  374. NativeScriptDesc *script_data = GET_SCRIPT_DESC();
  375. while (script_data) {
  376. OrderedHashMap<StringName, NativeScriptDesc::Property>::Element P = script_data->properties.find(p_name);
  377. if (P) {
  378. godot_variant value;
  379. value = P.get().getter.get_func((godot_object *)owner,
  380. P.get().getter.method_data,
  381. userdata);
  382. r_ret = *(Variant *)&value;
  383. godot_variant_destroy(&value);
  384. return true;
  385. }
  386. Map<StringName, NativeScriptDesc::Method>::Element *E = script_data->methods.find("_get");
  387. if (E) {
  388. Variant name = p_name;
  389. const Variant *args[1] = { &name };
  390. godot_variant result;
  391. result = E->get().method.method((godot_object *)owner,
  392. E->get().method.method_data,
  393. userdata,
  394. 1,
  395. (godot_variant **)args);
  396. r_ret = *(Variant *)&result;
  397. godot_variant_destroy(&result);
  398. if (r_ret.get_type() == Variant::NIL) {
  399. return false;
  400. }
  401. return true;
  402. }
  403. script_data = script_data->base_data;
  404. }
  405. return false;
  406. }
  407. void NativeScriptInstance::get_property_list(List<PropertyInfo> *p_properties) const {
  408. script->get_script_property_list(p_properties);
  409. NativeScriptDesc *script_data = GET_SCRIPT_DESC();
  410. while (script_data) {
  411. Map<StringName, NativeScriptDesc::Method>::Element *E = script_data->methods.find("_get_property_list");
  412. if (E) {
  413. godot_variant result;
  414. result = E->get().method.method((godot_object *)owner,
  415. E->get().method.method_data,
  416. userdata,
  417. 0,
  418. NULL);
  419. Variant res = *(Variant *)&result;
  420. godot_variant_destroy(&result);
  421. if (res.get_type() != Variant::ARRAY) {
  422. ERR_EXPLAIN("_get_property_list must return an array of dictionaries");
  423. ERR_FAIL();
  424. }
  425. Array arr = res;
  426. for (int i = 0; i < arr.size(); i++) {
  427. Dictionary d = arr[i];
  428. ERR_CONTINUE(!d.has("name"));
  429. ERR_CONTINUE(!d.has("type"));
  430. PropertyInfo info;
  431. info.type = Variant::Type(d["type"].operator int64_t());
  432. ERR_CONTINUE(info.type < 0 || info.type >= Variant::VARIANT_MAX);
  433. info.name = d["name"];
  434. ERR_CONTINUE(info.name == "");
  435. if (d.has("hint")) {
  436. info.hint = PropertyHint(d["hint"].operator int64_t());
  437. }
  438. if (d.has("hint_string")) {
  439. info.hint_string = d["hint_string"];
  440. }
  441. if (d.has("usage")) {
  442. info.usage = d["usage"];
  443. }
  444. p_properties->push_back(info);
  445. }
  446. }
  447. script_data = script_data->base_data;
  448. }
  449. return;
  450. }
  451. Variant::Type NativeScriptInstance::get_property_type(const StringName &p_name, bool *r_is_valid) const {
  452. NativeScriptDesc *script_data = GET_SCRIPT_DESC();
  453. while (script_data) {
  454. OrderedHashMap<StringName, NativeScriptDesc::Property>::Element P = script_data->properties.find(p_name);
  455. if (P) {
  456. *r_is_valid = true;
  457. return P.get().info.type;
  458. }
  459. script_data = script_data->base_data;
  460. }
  461. return Variant::NIL;
  462. }
  463. void NativeScriptInstance::get_method_list(List<MethodInfo> *p_list) const {
  464. script->get_method_list(p_list);
  465. }
  466. bool NativeScriptInstance::has_method(const StringName &p_method) const {
  467. return script->has_method(p_method);
  468. }
  469. Variant NativeScriptInstance::call(const StringName &p_method, const Variant **p_args, int p_argcount, Variant::CallError &r_error) {
  470. NativeScriptDesc *script_data = GET_SCRIPT_DESC();
  471. while (script_data) {
  472. Map<StringName, NativeScriptDesc::Method>::Element *E = script_data->methods.find(p_method);
  473. if (E) {
  474. godot_variant result;
  475. result = E->get().method.method((godot_object *)owner,
  476. E->get().method.method_data,
  477. userdata,
  478. p_argcount,
  479. (godot_variant **)p_args);
  480. Variant res = *(Variant *)&result;
  481. godot_variant_destroy(&result);
  482. r_error.error = Variant::CallError::CALL_OK;
  483. return res;
  484. }
  485. script_data = script_data->base_data;
  486. }
  487. r_error.error = Variant::CallError::CALL_ERROR_INVALID_METHOD;
  488. return Variant();
  489. }
  490. void NativeScriptInstance::notification(int p_notification) {
  491. Variant value = p_notification;
  492. const Variant *args[1] = { &value };
  493. call_multilevel("_notification", args, 1);
  494. }
  495. void NativeScriptInstance::refcount_incremented() {
  496. Variant::CallError err;
  497. call("_refcount_incremented", NULL, 0, err);
  498. if (err.error != Variant::CallError::CALL_OK && err.error != Variant::CallError::CALL_ERROR_INVALID_METHOD) {
  499. ERR_PRINT("Failed to invoke _refcount_incremented - should not happen");
  500. }
  501. }
  502. bool NativeScriptInstance::refcount_decremented() {
  503. Variant::CallError err;
  504. Variant ret = call("_refcount_decremented", NULL, 0, err);
  505. if (err.error != Variant::CallError::CALL_OK && err.error != Variant::CallError::CALL_ERROR_INVALID_METHOD) {
  506. ERR_PRINT("Failed to invoke _refcount_decremented - should not happen");
  507. return true; // assume we can destroy the object
  508. }
  509. if (err.error == Variant::CallError::CALL_ERROR_INVALID_METHOD) {
  510. // the method does not exist, default is true
  511. return true;
  512. }
  513. return ret;
  514. }
  515. Ref<Script> NativeScriptInstance::get_script() const {
  516. return script;
  517. }
  518. NativeScriptInstance::RPCMode NativeScriptInstance::get_rpc_mode(const StringName &p_method) const {
  519. NativeScriptDesc *script_data = GET_SCRIPT_DESC();
  520. while (script_data) {
  521. Map<StringName, NativeScriptDesc::Method>::Element *E = script_data->methods.find(p_method);
  522. if (E) {
  523. switch (E->get().rpc_mode) {
  524. case GODOT_METHOD_RPC_MODE_DISABLED:
  525. return RPC_MODE_DISABLED;
  526. case GODOT_METHOD_RPC_MODE_REMOTE:
  527. return RPC_MODE_REMOTE;
  528. case GODOT_METHOD_RPC_MODE_SYNC:
  529. return RPC_MODE_SYNC;
  530. case GODOT_METHOD_RPC_MODE_MASTER:
  531. return RPC_MODE_MASTER;
  532. case GODOT_METHOD_RPC_MODE_SLAVE:
  533. return RPC_MODE_SLAVE;
  534. default:
  535. return RPC_MODE_DISABLED;
  536. }
  537. }
  538. script_data = script_data->base_data;
  539. }
  540. return RPC_MODE_DISABLED;
  541. }
  542. // TODO(karroffel): implement this
  543. NativeScriptInstance::RPCMode NativeScriptInstance::get_rset_mode(const StringName &p_variable) const {
  544. NativeScriptDesc *script_data = GET_SCRIPT_DESC();
  545. while (script_data) {
  546. OrderedHashMap<StringName, NativeScriptDesc::Property>::Element E = script_data->properties.find(p_variable);
  547. if (E) {
  548. switch (E.get().rset_mode) {
  549. case GODOT_METHOD_RPC_MODE_DISABLED:
  550. return RPC_MODE_DISABLED;
  551. case GODOT_METHOD_RPC_MODE_REMOTE:
  552. return RPC_MODE_REMOTE;
  553. case GODOT_METHOD_RPC_MODE_SYNC:
  554. return RPC_MODE_SYNC;
  555. case GODOT_METHOD_RPC_MODE_MASTER:
  556. return RPC_MODE_MASTER;
  557. case GODOT_METHOD_RPC_MODE_SLAVE:
  558. return RPC_MODE_SLAVE;
  559. default:
  560. return RPC_MODE_DISABLED;
  561. }
  562. }
  563. script_data = script_data->base_data;
  564. }
  565. return RPC_MODE_DISABLED;
  566. }
  567. ScriptLanguage *NativeScriptInstance::get_language() {
  568. return NativeScriptLanguage::get_singleton();
  569. }
  570. void NativeScriptInstance::call_multilevel(const StringName &p_method, const Variant **p_args, int p_argcount) {
  571. NativeScriptDesc *script_data = GET_SCRIPT_DESC();
  572. while (script_data) {
  573. Map<StringName, NativeScriptDesc::Method>::Element *E = script_data->methods.find(p_method);
  574. if (E) {
  575. godot_variant res = E->get().method.method((godot_object *)owner,
  576. E->get().method.method_data,
  577. userdata,
  578. p_argcount,
  579. (godot_variant **)p_args);
  580. godot_variant_destroy(&res);
  581. }
  582. script_data = script_data->base_data;
  583. }
  584. }
  585. void NativeScriptInstance::call_multilevel_reversed(const StringName &p_method, const Variant **p_args, int p_argcount) {
  586. NativeScriptDesc *script_data = GET_SCRIPT_DESC();
  587. if (script_data) {
  588. _ml_call_reversed(script_data, p_method, p_args, p_argcount);
  589. }
  590. }
  591. NativeScriptInstance::~NativeScriptInstance() {
  592. NativeScriptDesc *script_data = GET_SCRIPT_DESC();
  593. if (!script_data)
  594. return;
  595. script_data->destroy_func.destroy_func((godot_object *)owner, script_data->destroy_func.method_data, userdata);
  596. if (owner) {
  597. #ifndef NO_THREADS
  598. script->owners_lock->lock();
  599. #endif
  600. script->instance_owners.erase(owner);
  601. #ifndef NO_THREADS
  602. script->owners_lock->unlock();
  603. #endif
  604. }
  605. }
  606. ////// ScriptingLanguage stuff
  607. NativeScriptLanguage *NativeScriptLanguage::singleton;
  608. extern "C" void _native_script_hook();
  609. void NativeScriptLanguage::_hacky_api_anchor() {
  610. _native_script_hook();
  611. }
  612. void NativeScriptLanguage::_unload_stuff() {
  613. for (Map<String, Map<StringName, NativeScriptDesc> >::Element *L = library_classes.front(); L; L = L->next()) {
  614. for (Map<StringName, NativeScriptDesc>::Element *C = L->get().front(); C; C = C->next()) {
  615. // free property stuff first
  616. for (OrderedHashMap<StringName, NativeScriptDesc::Property>::Element P = C->get().properties.front(); P; P = P.next()) {
  617. if (P.get().getter.free_func)
  618. P.get().getter.free_func(P.get().getter.method_data);
  619. if (P.get().setter.free_func)
  620. P.get().setter.free_func(P.get().setter.method_data);
  621. }
  622. // free method stuff
  623. for (Map<StringName, NativeScriptDesc::Method>::Element *M = C->get().methods.front(); M; M = M->next()) {
  624. if (M->get().method.free_func)
  625. M->get().method.free_func(M->get().method.method_data);
  626. }
  627. // free constructor/destructor
  628. if (C->get().create_func.free_func)
  629. C->get().create_func.free_func(C->get().create_func.method_data);
  630. if (C->get().destroy_func.free_func)
  631. C->get().destroy_func.free_func(C->get().destroy_func.method_data);
  632. }
  633. }
  634. }
  635. NativeScriptLanguage::NativeScriptLanguage() {
  636. NativeScriptLanguage::singleton = this;
  637. #ifndef NO_THREADS
  638. mutex = Mutex::create();
  639. #endif
  640. }
  641. // TODO(karroffel): implement this
  642. NativeScriptLanguage::~NativeScriptLanguage() {
  643. // _unload_stuff(); // NOTE(karroffel): This gets called in ::finish()
  644. for (Map<String, Ref<GDNative> >::Element *L = NSL->library_gdnatives.front(); L; L = L->next()) {
  645. L->get()->terminate();
  646. NSL->library_classes.clear();
  647. NSL->library_gdnatives.clear();
  648. NSL->library_script_users.clear();
  649. }
  650. #ifndef NO_THREADS
  651. memdelete(mutex);
  652. #endif
  653. }
  654. String NativeScriptLanguage::get_name() const {
  655. return "NativeScript";
  656. }
  657. void _add_reload_node() {
  658. #ifdef TOOLS_ENABLED
  659. NativeReloadNode *rn = memnew(NativeReloadNode);
  660. EditorNode::get_singleton()->add_child(rn);
  661. #endif
  662. }
  663. // TODO(karroffel): implement this
  664. void NativeScriptLanguage::init() {
  665. #if defined(TOOLS_ENABLED) && defined(DEBUG_METHODS_ENABLED)
  666. List<String> args = OS::get_singleton()->get_cmdline_args();
  667. List<String>::Element *E = args.find("--gdnative-generate-json-api");
  668. if (E && E->next()) {
  669. if (generate_c_api(E->next()->get()) != OK) {
  670. ERR_PRINT("Failed to generate C API\n");
  671. }
  672. }
  673. #endif
  674. #ifdef TOOLS_ENABLED
  675. EditorNode::add_init_callback(&_add_reload_node);
  676. #endif
  677. }
  678. String NativeScriptLanguage::get_type() const {
  679. return "NativeScript";
  680. }
  681. String NativeScriptLanguage::get_extension() const {
  682. return "gdns";
  683. }
  684. Error NativeScriptLanguage::execute_file(const String &p_path) {
  685. return OK; // Qué?
  686. }
  687. void NativeScriptLanguage::finish() {
  688. _unload_stuff();
  689. }
  690. void NativeScriptLanguage::get_reserved_words(List<String> *p_words) const {
  691. }
  692. void NativeScriptLanguage::get_comment_delimiters(List<String> *p_delimiters) const {
  693. }
  694. void NativeScriptLanguage::get_string_delimiters(List<String> *p_delimiters) const {
  695. }
  696. // TODO(karroffel): implement this
  697. Ref<Script> NativeScriptLanguage::get_template(const String &p_class_name, const String &p_base_class_name) const {
  698. NativeScript *s = memnew(NativeScript);
  699. s->set_class_name(p_class_name);
  700. // TODO(karroffel): use p_base_class_name
  701. return Ref<NativeScript>(s);
  702. }
  703. 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 {
  704. return false;
  705. }
  706. Script *NativeScriptLanguage::create_script() const {
  707. NativeScript *script = memnew(NativeScript);
  708. return script;
  709. }
  710. bool NativeScriptLanguage::has_named_classes() const {
  711. return true;
  712. }
  713. int NativeScriptLanguage::find_function(const String &p_function, const String &p_code) const {
  714. return -1;
  715. }
  716. String NativeScriptLanguage::make_function(const String &p_class, const String &p_name, const PoolStringArray &p_args) const {
  717. return "";
  718. }
  719. void NativeScriptLanguage::auto_indent_code(String &p_code, int p_from_line, int p_to_line) const {
  720. }
  721. void NativeScriptLanguage::add_global_constant(const StringName &p_variable, const Variant &p_value) {
  722. }
  723. // Debugging stuff here. Not used for now.
  724. String NativeScriptLanguage::debug_get_error() const {
  725. return "";
  726. }
  727. int NativeScriptLanguage::debug_get_stack_level_count() const {
  728. return -1;
  729. }
  730. int NativeScriptLanguage::debug_get_stack_level_line(int p_level) const {
  731. return -1;
  732. }
  733. String NativeScriptLanguage::debug_get_stack_level_function(int p_level) const {
  734. return "";
  735. }
  736. String NativeScriptLanguage::debug_get_stack_level_source(int p_level) const {
  737. return "";
  738. }
  739. 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) {
  740. }
  741. 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) {
  742. }
  743. void NativeScriptLanguage::debug_get_globals(List<String> *p_locals, List<Variant> *p_values, int p_max_subitems, int p_max_depth) {
  744. }
  745. String NativeScriptLanguage::debug_parse_stack_level_expression(int p_level, const String &p_expression, int p_max_subitems, int p_max_depth) {
  746. return "";
  747. }
  748. // Debugging stuff end.
  749. void NativeScriptLanguage::reload_all_scripts() {
  750. }
  751. void NativeScriptLanguage::reload_tool_script(const Ref<Script> &p_script, bool p_soft_reload) {
  752. }
  753. void NativeScriptLanguage::get_recognized_extensions(List<String> *p_extensions) const {
  754. p_extensions->push_back("gdns");
  755. }
  756. void NativeScriptLanguage::get_public_functions(List<MethodInfo> *p_functions) const {
  757. }
  758. void NativeScriptLanguage::get_public_constants(List<Pair<String, Variant> > *p_constants) const {
  759. }
  760. void NativeScriptLanguage::profiling_start() {
  761. }
  762. void NativeScriptLanguage::profiling_stop() {
  763. }
  764. int NativeScriptLanguage::profiling_get_accumulated_data(ProfilingInfo *p_info_arr, int p_info_max) {
  765. return 0;
  766. }
  767. int NativeScriptLanguage::profiling_get_frame_data(ProfilingInfo *p_info_arr, int p_info_max) {
  768. return 0;
  769. }
  770. #ifndef NO_THREADS
  771. void NativeScriptLanguage::defer_init_library(Ref<GDNativeLibrary> lib, NativeScript *script) {
  772. MutexLock lock(mutex);
  773. libs_to_init.insert(lib);
  774. scripts_to_register.insert(script);
  775. has_objects_to_register = true;
  776. }
  777. #endif
  778. void NativeScriptLanguage::init_library(const Ref<GDNativeLibrary> &lib) {
  779. #ifndef NO_THREADS
  780. MutexLock lock(mutex);
  781. #endif
  782. // See if this library was "registered" already.
  783. const String &lib_path = lib->get_active_library_path();
  784. ERR_EXPLAIN(lib->get_name() + " does not have a library for the current platform");
  785. ERR_FAIL_COND(lib_path.length() == 0);
  786. Map<String, Ref<GDNative> >::Element *E = library_gdnatives.find(lib_path);
  787. if (!E) {
  788. Ref<GDNative> gdn;
  789. gdn.instance();
  790. gdn->set_library(lib);
  791. // TODO(karroffel): check the return value?
  792. gdn->initialize();
  793. library_gdnatives.insert(lib_path, gdn);
  794. library_classes.insert(lib_path, Map<StringName, NativeScriptDesc>());
  795. if (!library_script_users.has(lib_path))
  796. library_script_users.insert(lib_path, Set<NativeScript *>());
  797. void *proc_ptr;
  798. gdn->get_symbol(_init_call_name, proc_ptr);
  799. ((void (*)(godot_string *))proc_ptr)((godot_string *)&lib_path);
  800. } else {
  801. // already initialized. Nice.
  802. }
  803. }
  804. void NativeScriptLanguage::register_script(NativeScript *script) {
  805. #ifndef NO_THREADS
  806. MutexLock lock(mutex);
  807. #endif
  808. library_script_users[script->lib_path].insert(script);
  809. }
  810. void NativeScriptLanguage::unregister_script(NativeScript *script) {
  811. #ifndef NO_THREADS
  812. MutexLock lock(mutex);
  813. #endif
  814. Map<String, Set<NativeScript *> >::Element *S = library_script_users.find(script->lib_path);
  815. if (S) {
  816. S->get().erase(script);
  817. if (S->get().size() == 0) {
  818. library_script_users.erase(S);
  819. }
  820. }
  821. #ifndef NO_THREADS
  822. scripts_to_register.erase(script);
  823. #endif
  824. }
  825. void NativeScriptLanguage::call_libraries_cb(const StringName &name) {
  826. // library_gdnatives is modified only from the main thread, so it's safe not to use mutex here
  827. for (Map<String, Ref<GDNative> >::Element *L = library_gdnatives.front(); L; L = L->next()) {
  828. if (L->get()->is_initialized()) {
  829. void *proc_ptr;
  830. Error err = L->get()->get_symbol(name, proc_ptr);
  831. if (err != OK) {
  832. ERR_PRINT((String("No godot_gdnative_init in \"" + L->get()->get_library()->get_active_library_path()) + "\" found").utf8().get_data());
  833. } else {
  834. ((void (*)())proc_ptr)();
  835. }
  836. }
  837. }
  838. }
  839. void NativeScriptLanguage::frame() {
  840. #ifndef NO_THREADS
  841. if (has_objects_to_register) {
  842. MutexLock lock(mutex);
  843. for (Set<Ref<GDNativeLibrary> >::Element *L = libs_to_init.front(); L; L = L->next()) {
  844. init_library(L->get());
  845. }
  846. libs_to_init.clear();
  847. for (Set<NativeScript *>::Element *S = scripts_to_register.front(); S; S = S->next()) {
  848. register_script(S->get());
  849. }
  850. scripts_to_register.clear();
  851. has_objects_to_register = false;
  852. }
  853. #endif
  854. call_libraries_cb(_frame_call_name);
  855. }
  856. #ifndef NO_THREADS
  857. void NativeScriptLanguage::thread_enter() {
  858. call_libraries_cb(_thread_enter_call_name);
  859. }
  860. void NativeScriptLanguage::thread_exit() {
  861. call_libraries_cb(_thread_exit_call_name);
  862. }
  863. #endif // NO_THREADS
  864. void NativeReloadNode::_bind_methods() {
  865. ClassDB::bind_method(D_METHOD("_notification"), &NativeReloadNode::_notification);
  866. }
  867. void NativeReloadNode::_notification(int p_what) {
  868. #ifdef TOOLS_ENABLED
  869. switch (p_what) {
  870. case MainLoop::NOTIFICATION_WM_FOCUS_OUT: {
  871. if (unloaded)
  872. break;
  873. #ifndef NO_THREADS
  874. MutexLock lock(NSL->mutex);
  875. #endif
  876. NSL->_unload_stuff();
  877. for (Map<String, Ref<GDNative> >::Element *L = NSL->library_gdnatives.front(); L; L = L->next()) {
  878. L->get()->terminate();
  879. NSL->library_classes.erase(L->key());
  880. }
  881. unloaded = true;
  882. } break;
  883. case MainLoop::NOTIFICATION_WM_FOCUS_IN: {
  884. if (!unloaded)
  885. break;
  886. #ifndef NO_THREADS
  887. MutexLock lock(NSL->mutex);
  888. #endif
  889. Set<StringName> libs_to_remove;
  890. for (Map<String, Ref<GDNative> >::Element *L = NSL->library_gdnatives.front(); L; L = L->next()) {
  891. if (!L->get()->initialize()) {
  892. libs_to_remove.insert(L->key());
  893. continue;
  894. }
  895. NSL->library_classes.insert(L->key(), Map<StringName, NativeScriptDesc>());
  896. void *args[1] = {
  897. (void *)&L->key()
  898. };
  899. // here the library registers all the classes and stuff.
  900. void *proc_ptr;
  901. L->get()->get_symbol("godot_nativescript_init", proc_ptr);
  902. ((void (*)(void *))proc_ptr)((void *)&L->key());
  903. for (Map<String, Set<NativeScript *> >::Element *U = NSL->library_script_users.front(); U; U = U->next()) {
  904. for (Set<NativeScript *>::Element *S = U->get().front(); S; S = S->next()) {
  905. NativeScript *script = S->get();
  906. if (script->placeholders.size() == 0)
  907. continue;
  908. for (Set<PlaceHolderScriptInstance *>::Element *P = script->placeholders.front(); P; P = P->next()) {
  909. script->_update_placeholder(P->get());
  910. }
  911. }
  912. }
  913. }
  914. unloaded = false;
  915. for (Set<StringName>::Element *R = libs_to_remove.front(); R; R = R->next()) {
  916. NSL->library_gdnatives.erase(R->get());
  917. }
  918. } break;
  919. default: {
  920. };
  921. }
  922. #endif
  923. }
  924. RES ResourceFormatLoaderNativeScript::load(const String &p_path, const String &p_original_path, Error *r_error) {
  925. ResourceFormatLoaderText rsflt;
  926. return rsflt.load(p_path, p_original_path, r_error);
  927. }
  928. void ResourceFormatLoaderNativeScript::get_recognized_extensions(List<String> *p_extensions) const {
  929. p_extensions->push_back("gdns");
  930. }
  931. bool ResourceFormatLoaderNativeScript::handles_type(const String &p_type) const {
  932. return (p_type == "Script" || p_type == "NativeScript");
  933. }
  934. String ResourceFormatLoaderNativeScript::get_resource_type(const String &p_path) const {
  935. String el = p_path.get_extension().to_lower();
  936. if (el == "gdns")
  937. return "NativeScript";
  938. return "";
  939. }
  940. Error ResourceFormatSaverNativeScript::save(const String &p_path, const RES &p_resource, uint32_t p_flags) {
  941. ResourceFormatSaverText rfst;
  942. return rfst.save(p_path, p_resource, p_flags);
  943. }
  944. bool ResourceFormatSaverNativeScript::recognize(const RES &p_resource) const {
  945. return Object::cast_to<NativeScript>(*p_resource) != NULL;
  946. }
  947. void ResourceFormatSaverNativeScript::get_recognized_extensions(const RES &p_resource, List<String> *p_extensions) const {
  948. if (Object::cast_to<NativeScript>(*p_resource)) {
  949. p_extensions->push_back("gdns");
  950. }
  951. }