nativescript.cpp 41 KB

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