nativescript.cpp 42 KB

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