nativescript.cpp 41 KB

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