nativescript.cpp 40 KB

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