nativescript.cpp 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495
  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. Map<String, Ref<GDNative> > erase_and_unload;
  680. for (Map<String, Map<StringName, NativeScriptDesc> >::Element *L = library_classes.front(); L; L = L->next()) {
  681. String lib_path = L->key();
  682. Map<StringName, NativeScriptDesc> classes = L->get();
  683. if (p_reload) {
  684. Map<String, Ref<GDNative> >::Element *E = library_gdnatives.find(lib_path);
  685. Ref<GDNative> gdn;
  686. if (E) {
  687. gdn = E->get();
  688. }
  689. bool should_reload = false;
  690. if (gdn.is_valid()) {
  691. Ref<GDNativeLibrary> lib = gdn->get_library();
  692. if (lib.is_valid()) {
  693. should_reload = lib->is_reloadable();
  694. }
  695. }
  696. if (!should_reload) {
  697. continue;
  698. }
  699. }
  700. Map<String, Ref<GDNative> >::Element *E = library_gdnatives.find(lib_path);
  701. Ref<GDNative> gdn;
  702. if (E) {
  703. gdn = E->get();
  704. }
  705. for (Map<StringName, NativeScriptDesc>::Element *C = classes.front(); C; C = C->next()) {
  706. // free property stuff first
  707. for (OrderedHashMap<StringName, NativeScriptDesc::Property>::Element P = C->get().properties.front(); P; P = P.next()) {
  708. if (P.get().getter.free_func)
  709. P.get().getter.free_func(P.get().getter.method_data);
  710. if (P.get().setter.free_func)
  711. P.get().setter.free_func(P.get().setter.method_data);
  712. }
  713. // free method stuff
  714. for (Map<StringName, NativeScriptDesc::Method>::Element *M = C->get().methods.front(); M; M = M->next()) {
  715. if (M->get().method.free_func)
  716. M->get().method.free_func(M->get().method.method_data);
  717. }
  718. // free constructor/destructor
  719. if (C->get().create_func.free_func)
  720. C->get().create_func.free_func(C->get().create_func.method_data);
  721. if (C->get().destroy_func.free_func)
  722. C->get().destroy_func.free_func(C->get().destroy_func.method_data);
  723. }
  724. erase_and_unload.insert(lib_path, gdn);
  725. }
  726. for (Map<String, Ref<GDNative> >::Element *E = erase_and_unload.front(); E; E = E->next()) {
  727. String lib_path = E->key();
  728. Ref<GDNative> gdn = E->get();
  729. library_classes.erase(lib_path);
  730. if (gdn.is_valid() && gdn->get_library().is_valid()) {
  731. Ref<GDNativeLibrary> lib = gdn->get_library();
  732. void *terminate_fn;
  733. Error err = gdn->get_symbol(lib->get_symbol_prefix() + _terminate_call_name, terminate_fn, true);
  734. if (err == OK) {
  735. void (*terminate)(void *) = (void (*)(void *))terminate_fn;
  736. terminate((void *)&lib_path);
  737. }
  738. }
  739. }
  740. }
  741. NativeScriptLanguage::NativeScriptLanguage() {
  742. NativeScriptLanguage::singleton = this;
  743. #ifndef NO_THREADS
  744. has_objects_to_register = false;
  745. mutex = Mutex::create();
  746. #endif
  747. }
  748. NativeScriptLanguage::~NativeScriptLanguage() {
  749. for (Map<String, Ref<GDNative> >::Element *L = NSL->library_gdnatives.front(); L; L = L->next()) {
  750. if (L->get().is_valid())
  751. L->get()->terminate();
  752. }
  753. NSL->library_classes.clear();
  754. NSL->library_gdnatives.clear();
  755. NSL->library_script_users.clear();
  756. #ifndef NO_THREADS
  757. memdelete(mutex);
  758. #endif
  759. }
  760. String NativeScriptLanguage::get_name() const {
  761. return "NativeScript";
  762. }
  763. void _add_reload_node() {
  764. #ifdef TOOLS_ENABLED
  765. NativeReloadNode *rn = memnew(NativeReloadNode);
  766. EditorNode::get_singleton()->add_child(rn);
  767. #endif
  768. }
  769. void NativeScriptLanguage::init() {
  770. #if defined(TOOLS_ENABLED) && defined(DEBUG_METHODS_ENABLED)
  771. List<String> args = OS::get_singleton()->get_cmdline_args();
  772. List<String>::Element *E = args.find("--gdnative-generate-json-api");
  773. if (E && E->next()) {
  774. if (generate_c_api(E->next()->get()) != OK) {
  775. ERR_PRINT("Failed to generate C API\n");
  776. }
  777. exit(0);
  778. }
  779. #endif
  780. #ifdef TOOLS_ENABLED
  781. EditorNode::add_init_callback(&_add_reload_node);
  782. #endif
  783. }
  784. String NativeScriptLanguage::get_type() const {
  785. return "NativeScript";
  786. }
  787. String NativeScriptLanguage::get_extension() const {
  788. return "gdns";
  789. }
  790. Error NativeScriptLanguage::execute_file(const String &p_path) {
  791. return OK; // Qué?
  792. }
  793. void NativeScriptLanguage::finish() {
  794. _unload_stuff();
  795. }
  796. void NativeScriptLanguage::get_reserved_words(List<String> *p_words) const {
  797. }
  798. void NativeScriptLanguage::get_comment_delimiters(List<String> *p_delimiters) const {
  799. }
  800. void NativeScriptLanguage::get_string_delimiters(List<String> *p_delimiters) const {
  801. }
  802. Ref<Script> NativeScriptLanguage::get_template(const String &p_class_name, const String &p_base_class_name) const {
  803. NativeScript *s = memnew(NativeScript);
  804. s->set_class_name(p_class_name);
  805. return Ref<NativeScript>(s);
  806. }
  807. 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 {
  808. return true;
  809. }
  810. Script *NativeScriptLanguage::create_script() const {
  811. NativeScript *script = memnew(NativeScript);
  812. return script;
  813. }
  814. bool NativeScriptLanguage::has_named_classes() const {
  815. return true;
  816. }
  817. bool NativeScriptLanguage::supports_builtin_mode() const {
  818. return true;
  819. }
  820. int NativeScriptLanguage::find_function(const String &p_function, const String &p_code) const {
  821. return -1;
  822. }
  823. String NativeScriptLanguage::make_function(const String &p_class, const String &p_name, const PoolStringArray &p_args) const {
  824. return "";
  825. }
  826. void NativeScriptLanguage::auto_indent_code(String &p_code, int p_from_line, int p_to_line) const {
  827. }
  828. void NativeScriptLanguage::add_global_constant(const StringName &p_variable, const Variant &p_value) {
  829. }
  830. // Debugging stuff here. Not used for now.
  831. String NativeScriptLanguage::debug_get_error() const {
  832. return "";
  833. }
  834. int NativeScriptLanguage::debug_get_stack_level_count() const {
  835. return -1;
  836. }
  837. int NativeScriptLanguage::debug_get_stack_level_line(int p_level) const {
  838. return -1;
  839. }
  840. String NativeScriptLanguage::debug_get_stack_level_function(int p_level) const {
  841. return "";
  842. }
  843. String NativeScriptLanguage::debug_get_stack_level_source(int p_level) const {
  844. return "";
  845. }
  846. 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) {
  847. }
  848. 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) {
  849. }
  850. void NativeScriptLanguage::debug_get_globals(List<String> *p_locals, List<Variant> *p_values, int p_max_subitems, int p_max_depth) {
  851. }
  852. String NativeScriptLanguage::debug_parse_stack_level_expression(int p_level, const String &p_expression, int p_max_subitems, int p_max_depth) {
  853. return "";
  854. }
  855. // Debugging stuff end.
  856. void NativeScriptLanguage::reload_all_scripts() {
  857. }
  858. void NativeScriptLanguage::reload_tool_script(const Ref<Script> &p_script, bool p_soft_reload) {
  859. }
  860. void NativeScriptLanguage::get_recognized_extensions(List<String> *p_extensions) const {
  861. p_extensions->push_back("gdns");
  862. }
  863. void NativeScriptLanguage::get_public_functions(List<MethodInfo> *p_functions) const {
  864. }
  865. void NativeScriptLanguage::get_public_constants(List<Pair<String, Variant> > *p_constants) const {
  866. }
  867. void NativeScriptLanguage::profiling_start() {
  868. }
  869. void NativeScriptLanguage::profiling_stop() {
  870. }
  871. int NativeScriptLanguage::profiling_get_accumulated_data(ProfilingInfo *p_info_arr, int p_info_max) {
  872. return 0;
  873. }
  874. int NativeScriptLanguage::profiling_get_frame_data(ProfilingInfo *p_info_arr, int p_info_max) {
  875. return 0;
  876. }
  877. int NativeScriptLanguage::register_binding_functions(godot_instance_binding_functions p_binding_functions) {
  878. // find index
  879. int idx = -1;
  880. for (int i = 0; i < binding_functions.size(); i++) {
  881. if (!binding_functions[i].first) {
  882. // free, we'll take it
  883. idx = i;
  884. break;
  885. }
  886. }
  887. if (idx == -1) {
  888. idx = binding_functions.size();
  889. binding_functions.resize(idx + 1);
  890. }
  891. // set the functions
  892. binding_functions[idx].first = true;
  893. binding_functions[idx].second = p_binding_functions;
  894. return idx;
  895. }
  896. void NativeScriptLanguage::unregister_binding_functions(int p_idx) {
  897. ERR_FAIL_INDEX(p_idx, binding_functions.size());
  898. for (Set<Vector<void *> *>::Element *E = binding_instances.front(); E; E = E->next()) {
  899. Vector<void *> &binding_data = *E->get();
  900. if (binding_data[p_idx] && binding_functions[p_idx].second.free_instance_binding_data)
  901. binding_functions[p_idx].second.free_instance_binding_data(binding_functions[p_idx].second.data, binding_data[p_idx]);
  902. }
  903. binding_functions[p_idx].first = false;
  904. if (binding_functions[p_idx].second.free_func)
  905. binding_functions[p_idx].second.free_func(binding_functions[p_idx].second.data);
  906. }
  907. void *NativeScriptLanguage::get_instance_binding_data(int p_idx, Object *p_object) {
  908. ERR_FAIL_INDEX_V(p_idx, binding_functions.size(), NULL);
  909. if (!binding_functions[p_idx].first) {
  910. ERR_EXPLAIN("Tried to get binding data for a nativescript binding that does not exist");
  911. ERR_FAIL_V(NULL);
  912. }
  913. Vector<void *> *binding_data = (Vector<void *> *)p_object->get_script_instance_binding(lang_idx);
  914. if (!binding_data)
  915. return NULL; // should never happen.
  916. if (binding_data->size() <= p_idx) {
  917. // okay, add new elements here.
  918. int old_size = binding_data->size();
  919. binding_data->resize(p_idx + 1);
  920. for (int i = old_size; i <= p_idx; i++) {
  921. (*binding_data)[i] = NULL;
  922. }
  923. }
  924. if (!(*binding_data)[p_idx]) {
  925. // no binding data yet, soooooo alloc new one \o/
  926. (*binding_data)[p_idx] = binding_functions[p_idx].second.alloc_instance_binding_data(binding_functions[p_idx].second.data, (godot_object *)p_object);
  927. }
  928. return (*binding_data)[p_idx];
  929. }
  930. void *NativeScriptLanguage::alloc_instance_binding_data(Object *p_object) {
  931. Vector<void *> *binding_data = new Vector<void *>;
  932. binding_data->resize(binding_functions.size());
  933. for (int i = 0; i < binding_functions.size(); i++) {
  934. (*binding_data)[i] = NULL;
  935. }
  936. binding_instances.insert(binding_data);
  937. return (void *)binding_data;
  938. }
  939. void NativeScriptLanguage::free_instance_binding_data(void *p_data) {
  940. if (!p_data)
  941. return;
  942. Vector<void *> &binding_data = *(Vector<void *> *)p_data;
  943. for (int i = 0; i < binding_data.size(); i++) {
  944. if (!binding_data[i])
  945. continue;
  946. if (binding_functions[i].first && binding_functions[i].second.free_instance_binding_data) {
  947. binding_functions[i].second.free_instance_binding_data(binding_functions[i].second.data, binding_data[i]);
  948. }
  949. }
  950. binding_instances.erase(&binding_data);
  951. delete &binding_data;
  952. }
  953. #ifndef NO_THREADS
  954. void NativeScriptLanguage::defer_init_library(Ref<GDNativeLibrary> lib, NativeScript *script) {
  955. MutexLock lock(mutex);
  956. libs_to_init.insert(lib);
  957. scripts_to_register.insert(script);
  958. has_objects_to_register = true;
  959. }
  960. #endif
  961. void NativeScriptLanguage::init_library(const Ref<GDNativeLibrary> &lib) {
  962. #ifndef NO_THREADS
  963. MutexLock lock(mutex);
  964. #endif
  965. // See if this library was "registered" already.
  966. const String &lib_path = lib->get_current_library_path();
  967. ERR_EXPLAIN(lib->get_name() + " does not have a library for the current platform");
  968. ERR_FAIL_COND(lib_path.length() == 0);
  969. Map<String, Ref<GDNative> >::Element *E = library_gdnatives.find(lib_path);
  970. if (!E) {
  971. Ref<GDNative> gdn;
  972. gdn.instance();
  973. gdn->set_library(lib);
  974. // TODO check the return value?
  975. gdn->initialize();
  976. library_gdnatives.insert(lib_path, gdn);
  977. library_classes.insert(lib_path, Map<StringName, NativeScriptDesc>());
  978. if (!library_script_users.has(lib_path))
  979. library_script_users.insert(lib_path, Set<NativeScript *>());
  980. void *proc_ptr;
  981. Error err = gdn->get_symbol(lib->get_symbol_prefix() + _init_call_name, proc_ptr);
  982. if (err != OK) {
  983. ERR_PRINT(String("No " + _init_call_name + " in \"" + lib_path + "\" found").utf8().get_data());
  984. } else {
  985. ((void (*)(godot_string *))proc_ptr)((godot_string *)&lib_path);
  986. }
  987. } else {
  988. // already initialized. Nice.
  989. }
  990. }
  991. void NativeScriptLanguage::register_script(NativeScript *script) {
  992. #ifndef NO_THREADS
  993. MutexLock lock(mutex);
  994. #endif
  995. library_script_users[script->lib_path].insert(script);
  996. }
  997. void NativeScriptLanguage::unregister_script(NativeScript *script) {
  998. #ifndef NO_THREADS
  999. MutexLock lock(mutex);
  1000. #endif
  1001. Map<String, Set<NativeScript *> >::Element *S = library_script_users.find(script->lib_path);
  1002. if (S) {
  1003. S->get().erase(script);
  1004. if (S->get().size() == 0) {
  1005. library_script_users.erase(S);
  1006. }
  1007. }
  1008. #ifndef NO_THREADS
  1009. scripts_to_register.erase(script);
  1010. #endif
  1011. }
  1012. void NativeScriptLanguage::call_libraries_cb(const StringName &name) {
  1013. // library_gdnatives is modified only from the main thread, so it's safe not to use mutex here
  1014. for (Map<String, Ref<GDNative> >::Element *L = library_gdnatives.front(); L; L = L->next()) {
  1015. if (L->get().is_null()) {
  1016. continue;
  1017. }
  1018. if (L->get()->is_initialized()) {
  1019. void *proc_ptr;
  1020. Error err = L->get()->get_symbol(L->get()->get_library()->get_symbol_prefix() + name, proc_ptr);
  1021. if (!err) {
  1022. ((void (*)())proc_ptr)();
  1023. }
  1024. }
  1025. }
  1026. }
  1027. void NativeScriptLanguage::frame() {
  1028. #ifndef NO_THREADS
  1029. if (has_objects_to_register) {
  1030. MutexLock lock(mutex);
  1031. for (Set<Ref<GDNativeLibrary> >::Element *L = libs_to_init.front(); L; L = L->next()) {
  1032. init_library(L->get());
  1033. }
  1034. libs_to_init.clear();
  1035. for (Set<NativeScript *>::Element *S = scripts_to_register.front(); S; S = S->next()) {
  1036. register_script(S->get());
  1037. }
  1038. scripts_to_register.clear();
  1039. has_objects_to_register = false;
  1040. }
  1041. #endif
  1042. call_libraries_cb(_frame_call_name);
  1043. }
  1044. #ifndef NO_THREADS
  1045. void NativeScriptLanguage::thread_enter() {
  1046. call_libraries_cb(_thread_enter_call_name);
  1047. }
  1048. void NativeScriptLanguage::thread_exit() {
  1049. call_libraries_cb(_thread_exit_call_name);
  1050. }
  1051. #endif // NO_THREADS
  1052. void NativeReloadNode::_bind_methods() {
  1053. ClassDB::bind_method(D_METHOD("_notification"), &NativeReloadNode::_notification);
  1054. }
  1055. void NativeReloadNode::_notification(int p_what) {
  1056. #ifdef TOOLS_ENABLED
  1057. switch (p_what) {
  1058. case MainLoop::NOTIFICATION_WM_FOCUS_OUT: {
  1059. if (unloaded)
  1060. break;
  1061. #ifndef NO_THREADS
  1062. MutexLock lock(NSL->mutex);
  1063. #endif
  1064. NSL->_unload_stuff(true);
  1065. for (Map<String, Ref<GDNative> >::Element *L = NSL->library_gdnatives.front(); L; L = L->next()) {
  1066. Ref<GDNative> gdn = L->get();
  1067. if (gdn.is_null()) {
  1068. continue;
  1069. }
  1070. if (!gdn->get_library()->is_reloadable()) {
  1071. continue;
  1072. }
  1073. gdn->terminate();
  1074. }
  1075. unloaded = true;
  1076. } break;
  1077. case MainLoop::NOTIFICATION_WM_FOCUS_IN: {
  1078. if (!unloaded)
  1079. break;
  1080. #ifndef NO_THREADS
  1081. MutexLock lock(NSL->mutex);
  1082. #endif
  1083. Set<StringName> libs_to_remove;
  1084. for (Map<String, Ref<GDNative> >::Element *L = NSL->library_gdnatives.front(); L; L = L->next()) {
  1085. Ref<GDNative> gdn = L->get();
  1086. if (gdn.is_null()) {
  1087. continue;
  1088. }
  1089. if (!gdn->get_library()->is_reloadable()) {
  1090. continue;
  1091. }
  1092. if (!gdn->initialize()) {
  1093. libs_to_remove.insert(L->key());
  1094. continue;
  1095. }
  1096. NSL->library_classes.insert(L->key(), Map<StringName, NativeScriptDesc>());
  1097. // here the library registers all the classes and stuff.
  1098. void *proc_ptr;
  1099. Error err = gdn->get_symbol(gdn->get_library()->get_symbol_prefix() + "nativescript_init", proc_ptr);
  1100. if (err != OK) {
  1101. ERR_PRINT(String("No godot_nativescript_init in \"" + L->key() + "\" found").utf8().get_data());
  1102. } else {
  1103. ((void (*)(void *))proc_ptr)((void *)&L->key());
  1104. }
  1105. for (Map<String, Set<NativeScript *> >::Element *U = NSL->library_script_users.front(); U; U = U->next()) {
  1106. for (Set<NativeScript *>::Element *S = U->get().front(); S; S = S->next()) {
  1107. NativeScript *script = S->get();
  1108. if (script->placeholders.size() == 0)
  1109. continue;
  1110. for (Set<PlaceHolderScriptInstance *>::Element *P = script->placeholders.front(); P; P = P->next()) {
  1111. script->_update_placeholder(P->get());
  1112. }
  1113. }
  1114. }
  1115. }
  1116. unloaded = false;
  1117. for (Set<StringName>::Element *R = libs_to_remove.front(); R; R = R->next()) {
  1118. NSL->library_gdnatives.erase(R->get());
  1119. }
  1120. } break;
  1121. default: {
  1122. };
  1123. }
  1124. #endif
  1125. }
  1126. RES ResourceFormatLoaderNativeScript::load(const String &p_path, const String &p_original_path, Error *r_error) {
  1127. ResourceFormatLoaderText rsflt;
  1128. return rsflt.load(p_path, p_original_path, r_error);
  1129. }
  1130. void ResourceFormatLoaderNativeScript::get_recognized_extensions(List<String> *p_extensions) const {
  1131. p_extensions->push_back("gdns");
  1132. }
  1133. bool ResourceFormatLoaderNativeScript::handles_type(const String &p_type) const {
  1134. return (p_type == "Script" || p_type == "NativeScript");
  1135. }
  1136. String ResourceFormatLoaderNativeScript::get_resource_type(const String &p_path) const {
  1137. String el = p_path.get_extension().to_lower();
  1138. if (el == "gdns")
  1139. return "NativeScript";
  1140. return "";
  1141. }
  1142. Error ResourceFormatSaverNativeScript::save(const String &p_path, const RES &p_resource, uint32_t p_flags) {
  1143. ResourceFormatSaverText rfst;
  1144. return rfst.save(p_path, p_resource, p_flags);
  1145. }
  1146. bool ResourceFormatSaverNativeScript::recognize(const RES &p_resource) const {
  1147. return Object::cast_to<NativeScript>(*p_resource) != NULL;
  1148. }
  1149. void ResourceFormatSaverNativeScript::get_recognized_extensions(const RES &p_resource, List<String> *p_extensions) const {
  1150. if (Object::cast_to<NativeScript>(*p_resource)) {
  1151. p_extensions->push_back("gdns");
  1152. }
  1153. }