nativescript.cpp 41 KB

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