nativescript.cpp 34 KB

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