nativescript.cpp 33 KB

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