nativescript.cpp 41 KB

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