nativescript.cpp 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830
  1. /**************************************************************************/
  2. /* nativescript.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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/core_string_names.h"
  33. #include "core/global_constants.h"
  34. #include "core/io/file_access_encrypted.h"
  35. #include "core/os/file_access.h"
  36. #include "core/os/os.h"
  37. #include "core/project_settings.h"
  38. #include "main/main.h"
  39. #include "scene/main/scene_tree.h"
  40. #include "scene/resources/resource_format_text.h"
  41. #include <stdlib.h>
  42. #ifndef NO_THREADS
  43. #include "core/os/thread.h"
  44. #endif
  45. #if defined(TOOLS_ENABLED) && defined(DEBUG_METHODS_ENABLED)
  46. #include "api_generator.h"
  47. #endif
  48. #ifdef TOOLS_ENABLED
  49. #include "editor/editor_node.h"
  50. #endif
  51. void NativeScript::_bind_methods() {
  52. ClassDB::bind_method(D_METHOD("set_class_name", "class_name"), &NativeScript::set_class_name);
  53. ClassDB::bind_method(D_METHOD("get_class_name"), &NativeScript::get_class_name);
  54. ClassDB::bind_method(D_METHOD("set_library", "library"), &NativeScript::set_library);
  55. ClassDB::bind_method(D_METHOD("get_library"), &NativeScript::get_library);
  56. ClassDB::bind_method(D_METHOD("set_script_class_name", "class_name"), &NativeScript::set_script_class_name);
  57. ClassDB::bind_method(D_METHOD("get_script_class_name"), &NativeScript::get_script_class_name);
  58. ClassDB::bind_method(D_METHOD("set_script_class_icon_path", "icon_path"), &NativeScript::set_script_class_icon_path);
  59. ClassDB::bind_method(D_METHOD("get_script_class_icon_path"), &NativeScript::get_script_class_icon_path);
  60. ClassDB::bind_method(D_METHOD("get_class_documentation"), &NativeScript::get_class_documentation);
  61. ClassDB::bind_method(D_METHOD("get_method_documentation", "method"), &NativeScript::get_method_documentation);
  62. ClassDB::bind_method(D_METHOD("get_signal_documentation", "signal_name"), &NativeScript::get_signal_documentation);
  63. ClassDB::bind_method(D_METHOD("get_property_documentation", "path"), &NativeScript::get_property_documentation);
  64. ADD_PROPERTY(PropertyInfo(Variant::STRING, "class_name"), "set_class_name", "get_class_name");
  65. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "library", PROPERTY_HINT_RESOURCE_TYPE, "GDNativeLibrary"), "set_library", "get_library");
  66. ADD_GROUP("Script Class", "script_class_");
  67. ADD_PROPERTY(PropertyInfo(Variant::STRING, "script_class_name"), "set_script_class_name", "get_script_class_name");
  68. ADD_PROPERTY(PropertyInfo(Variant::STRING, "script_class_icon_path", PROPERTY_HINT_FILE), "set_script_class_icon_path", "get_script_class_icon_path");
  69. ClassDB::bind_vararg_method(METHOD_FLAGS_DEFAULT, "new", &NativeScript::_new, MethodInfo("new"));
  70. }
  71. #define NSL NativeScriptLanguage::get_singleton()
  72. #ifdef TOOLS_ENABLED
  73. void NativeScript::_update_placeholder(PlaceHolderScriptInstance *p_placeholder) {
  74. NativeScriptDesc *script_data = get_script_desc();
  75. ERR_FAIL_COND(!script_data);
  76. List<PropertyInfo> info;
  77. get_script_property_list(&info);
  78. Map<StringName, Variant> values;
  79. for (List<PropertyInfo>::Element *E = info.front(); E; E = E->next()) {
  80. Variant value;
  81. get_property_default_value(E->get().name, value);
  82. values[E->get().name] = value;
  83. }
  84. p_placeholder->update(info, values);
  85. }
  86. void NativeScript::_placeholder_erased(PlaceHolderScriptInstance *p_placeholder) {
  87. placeholders.erase(p_placeholder);
  88. }
  89. #endif
  90. bool NativeScript::inherits_script(const Ref<Script> &p_script) const {
  91. Ref<NativeScript> ns = p_script;
  92. if (ns.is_null()) {
  93. return false;
  94. }
  95. const NativeScriptDesc *other_s = ns->get_script_desc();
  96. if (!other_s) {
  97. return false;
  98. }
  99. const NativeScriptDesc *s = get_script_desc();
  100. while (s) {
  101. if (s == other_s) {
  102. return true;
  103. }
  104. s = s->base_data;
  105. }
  106. return false;
  107. }
  108. void NativeScript::set_class_name(String p_class_name) {
  109. class_name = p_class_name;
  110. }
  111. String NativeScript::get_class_name() const {
  112. return class_name;
  113. }
  114. void NativeScript::set_library(Ref<GDNativeLibrary> p_library) {
  115. if (!library.is_null()) {
  116. WARN_PRINT("Library in NativeScript already set. Do nothing.");
  117. return;
  118. }
  119. if (p_library.is_null()) {
  120. return;
  121. }
  122. library = p_library;
  123. lib_path = library->get_current_library_path();
  124. #ifndef NO_THREADS
  125. if (Thread::get_caller_id() != Thread::get_main_id()) {
  126. NSL->defer_init_library(p_library, this);
  127. } else
  128. #endif
  129. {
  130. NSL->init_library(p_library);
  131. NSL->register_script(this);
  132. }
  133. }
  134. Ref<GDNativeLibrary> NativeScript::get_library() const {
  135. return library;
  136. }
  137. void NativeScript::set_script_class_name(String p_type) {
  138. script_class_name = p_type;
  139. }
  140. String NativeScript::get_script_class_name() const {
  141. return script_class_name;
  142. }
  143. void NativeScript::set_script_class_icon_path(String p_icon_path) {
  144. script_class_icon_path = p_icon_path;
  145. }
  146. String NativeScript::get_script_class_icon_path() const {
  147. return script_class_icon_path;
  148. }
  149. bool NativeScript::can_instance() const {
  150. NativeScriptDesc *script_data = get_script_desc();
  151. #ifdef TOOLS_ENABLED
  152. // Only valid if this is either a tool script or a "regular" script.
  153. // (so an environment where scripting is disabled (and not the editor) would not
  154. // create objects).
  155. return script_data && (is_tool() || ScriptServer::is_scripting_enabled());
  156. #else
  157. return script_data;
  158. #endif
  159. }
  160. Ref<Script> NativeScript::get_base_script() const {
  161. NativeScriptDesc *script_data = get_script_desc();
  162. if (!script_data) {
  163. return Ref<Script>();
  164. }
  165. NativeScript *script = (NativeScript *)NSL->create_script();
  166. Ref<NativeScript> ns = Ref<NativeScript>(script);
  167. ERR_FAIL_COND_V(!ns.is_valid(), Ref<Script>());
  168. ns->set_class_name(script_data->base);
  169. ns->set_library(get_library());
  170. return ns;
  171. }
  172. StringName NativeScript::get_instance_base_type() const {
  173. NativeScriptDesc *script_data = get_script_desc();
  174. if (!script_data) {
  175. return "";
  176. }
  177. return script_data->base_native_type;
  178. }
  179. ScriptInstance *NativeScript::instance_create(Object *p_this) {
  180. NativeScriptDesc *script_data = get_script_desc();
  181. if (!script_data) {
  182. return nullptr;
  183. }
  184. NativeScriptInstance *nsi = memnew(NativeScriptInstance);
  185. nsi->owner = p_this;
  186. nsi->script = Ref<NativeScript>(this);
  187. #ifndef TOOLS_ENABLED
  188. if (!ScriptServer::is_scripting_enabled()) {
  189. nsi->userdata = NULL;
  190. } else {
  191. nsi->userdata = script_data->create_func.create_func((godot_object *)p_this, script_data->create_func.method_data);
  192. }
  193. #else
  194. nsi->userdata = script_data->create_func.create_func((godot_object *)p_this, script_data->create_func.method_data);
  195. #endif
  196. owners_lock.lock();
  197. instance_owners.insert(p_this);
  198. owners_lock.unlock();
  199. return nsi;
  200. }
  201. PlaceHolderScriptInstance *NativeScript::placeholder_instance_create(Object *p_this) {
  202. #ifdef TOOLS_ENABLED
  203. PlaceHolderScriptInstance *sins = memnew(PlaceHolderScriptInstance(NSL, Ref<Script>(this), p_this));
  204. placeholders.insert(sins);
  205. _update_placeholder(sins);
  206. return sins;
  207. #else
  208. return NULL;
  209. #endif
  210. }
  211. bool NativeScript::instance_has(const Object *p_this) const {
  212. return instance_owners.has((Object *)p_this);
  213. }
  214. bool NativeScript::has_source_code() const {
  215. return false;
  216. }
  217. String NativeScript::get_source_code() const {
  218. return "";
  219. }
  220. void NativeScript::set_source_code(const String &p_code) {
  221. }
  222. Error NativeScript::reload(bool p_keep_state) {
  223. return FAILED;
  224. }
  225. bool NativeScript::has_method(const StringName &p_method) const {
  226. NativeScriptDesc *script_data = get_script_desc();
  227. while (script_data) {
  228. if (script_data->methods.has(p_method)) {
  229. return true;
  230. }
  231. script_data = script_data->base_data;
  232. }
  233. return false;
  234. }
  235. MethodInfo NativeScript::get_method_info(const StringName &p_method) const {
  236. NativeScriptDesc *script_data = get_script_desc();
  237. if (!script_data) {
  238. return MethodInfo();
  239. }
  240. while (script_data) {
  241. Map<StringName, NativeScriptDesc::Method>::Element *M = script_data->methods.find(p_method);
  242. if (M) {
  243. return M->get().info;
  244. }
  245. script_data = script_data->base_data;
  246. }
  247. return MethodInfo();
  248. }
  249. bool NativeScript::is_valid() const {
  250. return true;
  251. }
  252. bool NativeScript::is_tool() const {
  253. NativeScriptDesc *script_data = get_script_desc();
  254. if (script_data) {
  255. return script_data->is_tool;
  256. }
  257. return false;
  258. }
  259. ScriptLanguage *NativeScript::get_language() const {
  260. return NativeScriptLanguage::get_singleton();
  261. }
  262. bool NativeScript::has_script_signal(const StringName &p_signal) const {
  263. NativeScriptDesc *script_data = get_script_desc();
  264. while (script_data) {
  265. if (script_data->signals_.has(p_signal)) {
  266. return true;
  267. }
  268. script_data = script_data->base_data;
  269. }
  270. return false;
  271. }
  272. void NativeScript::get_script_signal_list(List<MethodInfo> *r_signals) const {
  273. NativeScriptDesc *script_data = get_script_desc();
  274. if (!script_data) {
  275. return;
  276. }
  277. Set<MethodInfo> signals_;
  278. while (script_data) {
  279. for (Map<StringName, NativeScriptDesc::Signal>::Element *S = script_data->signals_.front(); S; S = S->next()) {
  280. signals_.insert(S->get().signal);
  281. }
  282. script_data = script_data->base_data;
  283. }
  284. for (Set<MethodInfo>::Element *E = signals_.front(); E; E = E->next()) {
  285. r_signals->push_back(E->get());
  286. }
  287. }
  288. bool NativeScript::get_property_default_value(const StringName &p_property, Variant &r_value) const {
  289. NativeScriptDesc *script_data = get_script_desc();
  290. OrderedHashMap<StringName, NativeScriptDesc::Property>::Element P;
  291. while (!P && script_data) {
  292. P = script_data->properties.find(p_property);
  293. script_data = script_data->base_data;
  294. }
  295. if (!P) {
  296. return false;
  297. }
  298. r_value = P.get().default_value;
  299. return true;
  300. }
  301. void NativeScript::update_exports() {
  302. }
  303. void NativeScript::get_script_method_list(List<MethodInfo> *p_list) const {
  304. NativeScriptDesc *script_data = get_script_desc();
  305. if (!script_data) {
  306. return;
  307. }
  308. Set<MethodInfo> methods;
  309. while (script_data) {
  310. for (Map<StringName, NativeScriptDesc::Method>::Element *E = script_data->methods.front(); E; E = E->next()) {
  311. methods.insert(E->get().info);
  312. }
  313. script_data = script_data->base_data;
  314. }
  315. for (Set<MethodInfo>::Element *E = methods.front(); E; E = E->next()) {
  316. p_list->push_back(E->get());
  317. }
  318. }
  319. void NativeScript::get_script_property_list(List<PropertyInfo> *p_list) const {
  320. NativeScriptDesc *script_data = get_script_desc();
  321. Set<StringName> existing_properties;
  322. List<PropertyInfo>::Element *original_back = p_list->back();
  323. while (script_data) {
  324. List<PropertyInfo>::Element *insert_position = original_back;
  325. for (OrderedHashMap<StringName, NativeScriptDesc::Property>::Element E = script_data->properties.front(); E; E = E.next()) {
  326. if (!existing_properties.has(E.key())) {
  327. insert_position = p_list->insert_after(insert_position, E.get().info);
  328. existing_properties.insert(E.key());
  329. }
  330. }
  331. script_data = script_data->base_data;
  332. }
  333. }
  334. String NativeScript::get_class_documentation() const {
  335. NativeScriptDesc *script_data = get_script_desc();
  336. ERR_FAIL_COND_V_MSG(!script_data, "", "Attempt to get class documentation on invalid NativeScript.");
  337. return script_data->documentation;
  338. }
  339. String NativeScript::get_method_documentation(const StringName &p_method) const {
  340. NativeScriptDesc *script_data = get_script_desc();
  341. ERR_FAIL_COND_V_MSG(!script_data, "", "Attempt to get method documentation on invalid NativeScript.");
  342. while (script_data) {
  343. Map<StringName, NativeScriptDesc::Method>::Element *method = script_data->methods.find(p_method);
  344. if (method) {
  345. return method->get().documentation;
  346. }
  347. script_data = script_data->base_data;
  348. }
  349. ERR_FAIL_V_MSG("", "Attempt to get method documentation for non-existent method.");
  350. }
  351. String NativeScript::get_signal_documentation(const StringName &p_signal_name) const {
  352. NativeScriptDesc *script_data = get_script_desc();
  353. ERR_FAIL_COND_V_MSG(!script_data, "", "Attempt to get signal documentation on invalid NativeScript.");
  354. while (script_data) {
  355. Map<StringName, NativeScriptDesc::Signal>::Element *signal = script_data->signals_.find(p_signal_name);
  356. if (signal) {
  357. return signal->get().documentation;
  358. }
  359. script_data = script_data->base_data;
  360. }
  361. ERR_FAIL_V_MSG("", "Attempt to get signal documentation for non-existent signal.");
  362. }
  363. String NativeScript::get_property_documentation(const StringName &p_path) const {
  364. NativeScriptDesc *script_data = get_script_desc();
  365. ERR_FAIL_COND_V_MSG(!script_data, "", "Attempt to get property documentation on invalid NativeScript.");
  366. while (script_data) {
  367. OrderedHashMap<StringName, NativeScriptDesc::Property>::Element property = script_data->properties.find(p_path);
  368. if (property) {
  369. return property.get().documentation;
  370. }
  371. script_data = script_data->base_data;
  372. }
  373. ERR_FAIL_V_MSG("", "Attempt to get property documentation for non-existent signal.");
  374. }
  375. Variant NativeScript::_new(const Variant **p_args, int p_argcount, Variant::CallError &r_error) {
  376. if (lib_path.empty() || class_name.empty() || library.is_null()) {
  377. r_error.error = Variant::CallError::CALL_ERROR_INSTANCE_IS_NULL;
  378. return Variant();
  379. }
  380. NativeScriptDesc *script_data = get_script_desc();
  381. if (!script_data) {
  382. r_error.error = Variant::CallError::CALL_ERROR_INSTANCE_IS_NULL;
  383. return Variant();
  384. }
  385. r_error.error = Variant::CallError::CALL_OK;
  386. REF ref;
  387. Object *owner = nullptr;
  388. if (!(script_data->base_native_type == "")) {
  389. owner = ClassDB::instance(script_data->base_native_type);
  390. } else {
  391. owner = memnew(Reference);
  392. }
  393. if (!owner) {
  394. r_error.error = Variant::CallError::CALL_ERROR_INSTANCE_IS_NULL;
  395. return Variant();
  396. }
  397. Reference *r = Object::cast_to<Reference>(owner);
  398. if (r) {
  399. ref = REF(r);
  400. }
  401. NativeScriptInstance *instance = (NativeScriptInstance *)instance_create(owner);
  402. owner->set_script_instance(instance);
  403. if (!instance) {
  404. if (ref.is_null()) {
  405. memdelete(owner); //no owner, sorry
  406. }
  407. return Variant();
  408. }
  409. if (ref.is_valid()) {
  410. return ref;
  411. } else {
  412. return owner;
  413. }
  414. }
  415. NativeScript::NativeScript() {
  416. library = Ref<GDNative>();
  417. lib_path = "";
  418. class_name = "";
  419. }
  420. NativeScript::~NativeScript() {
  421. NSL->unregister_script(this);
  422. }
  423. #define GET_SCRIPT_DESC() script->get_script_desc()
  424. void NativeScriptInstance::_ml_call_reversed(NativeScriptDesc *script_data, const StringName &p_method, const Variant **p_args, int p_argcount) {
  425. if (script_data->base_data) {
  426. _ml_call_reversed(script_data->base_data, p_method, p_args, p_argcount);
  427. }
  428. Map<StringName, NativeScriptDesc::Method>::Element *E = script_data->methods.find(p_method);
  429. if (E) {
  430. godot_variant res = E->get().method.method((godot_object *)owner, E->get().method.method_data, userdata, p_argcount, (godot_variant **)p_args);
  431. godot_variant_destroy(&res);
  432. }
  433. }
  434. bool NativeScriptInstance::set(const StringName &p_name, const Variant &p_value) {
  435. NativeScriptDesc *script_data = GET_SCRIPT_DESC();
  436. while (script_data) {
  437. OrderedHashMap<StringName, NativeScriptDesc::Property>::Element P = script_data->properties.find(p_name);
  438. if (P) {
  439. P.get().setter.set_func((godot_object *)owner,
  440. P.get().setter.method_data,
  441. userdata,
  442. (godot_variant *)&p_value);
  443. return true;
  444. }
  445. Map<StringName, NativeScriptDesc::Method>::Element *E = script_data->methods.find("_set");
  446. if (E) {
  447. Variant name = p_name;
  448. const Variant *args[2] = { &name, &p_value };
  449. godot_variant result;
  450. result = E->get().method.method((godot_object *)owner,
  451. E->get().method.method_data,
  452. userdata,
  453. 2,
  454. (godot_variant **)args);
  455. bool handled = *(Variant *)&result;
  456. godot_variant_destroy(&result);
  457. if (handled) {
  458. return true;
  459. }
  460. }
  461. script_data = script_data->base_data;
  462. }
  463. return false;
  464. }
  465. bool NativeScriptInstance::get(const StringName &p_name, Variant &r_ret) const {
  466. NativeScriptDesc *script_data = GET_SCRIPT_DESC();
  467. while (script_data) {
  468. OrderedHashMap<StringName, NativeScriptDesc::Property>::Element P = script_data->properties.find(p_name);
  469. if (P) {
  470. godot_variant value;
  471. value = P.get().getter.get_func((godot_object *)owner,
  472. P.get().getter.method_data,
  473. userdata);
  474. r_ret = *(Variant *)&value;
  475. godot_variant_destroy(&value);
  476. return true;
  477. }
  478. Map<StringName, NativeScriptDesc::Method>::Element *E = script_data->methods.find("_get");
  479. if (E) {
  480. Variant name = p_name;
  481. const Variant *args[1] = { &name };
  482. godot_variant result;
  483. result = E->get().method.method((godot_object *)owner,
  484. E->get().method.method_data,
  485. userdata,
  486. 1,
  487. (godot_variant **)args);
  488. r_ret = *(Variant *)&result;
  489. godot_variant_destroy(&result);
  490. if (r_ret.get_type() != Variant::NIL) {
  491. return true;
  492. }
  493. }
  494. script_data = script_data->base_data;
  495. }
  496. return false;
  497. }
  498. void NativeScriptInstance::get_property_list(List<PropertyInfo> *p_properties) const {
  499. script->get_script_property_list(p_properties);
  500. NativeScriptDesc *script_data = GET_SCRIPT_DESC();
  501. while (script_data) {
  502. Map<StringName, NativeScriptDesc::Method>::Element *E = script_data->methods.find("_get_property_list");
  503. if (E) {
  504. godot_variant result;
  505. result = E->get().method.method((godot_object *)owner,
  506. E->get().method.method_data,
  507. userdata,
  508. 0,
  509. nullptr);
  510. Variant res = *(Variant *)&result;
  511. godot_variant_destroy(&result);
  512. ERR_FAIL_COND_MSG(res.get_type() != Variant::ARRAY, "_get_property_list must return an array of dictionaries.");
  513. Array arr = res;
  514. for (int i = 0; i < arr.size(); i++) {
  515. Dictionary d = arr[i];
  516. ERR_CONTINUE(!d.has("name"));
  517. ERR_CONTINUE(!d.has("type"));
  518. PropertyInfo info;
  519. info.type = Variant::Type(d["type"].operator int64_t());
  520. ERR_CONTINUE(info.type < 0 || info.type >= Variant::VARIANT_MAX);
  521. info.name = d["name"];
  522. ERR_CONTINUE(info.name == "");
  523. if (d.has("hint")) {
  524. info.hint = PropertyHint(d["hint"].operator int64_t());
  525. }
  526. if (d.has("hint_string")) {
  527. info.hint_string = d["hint_string"];
  528. }
  529. if (d.has("usage")) {
  530. info.usage = d["usage"];
  531. }
  532. p_properties->push_back(info);
  533. }
  534. }
  535. script_data = script_data->base_data;
  536. }
  537. return;
  538. }
  539. Variant::Type NativeScriptInstance::get_property_type(const StringName &p_name, bool *r_is_valid) const {
  540. NativeScriptDesc *script_data = GET_SCRIPT_DESC();
  541. while (script_data) {
  542. OrderedHashMap<StringName, NativeScriptDesc::Property>::Element P = script_data->properties.find(p_name);
  543. if (P) {
  544. *r_is_valid = true;
  545. return P.get().info.type;
  546. }
  547. script_data = script_data->base_data;
  548. }
  549. return Variant::NIL;
  550. }
  551. void NativeScriptInstance::get_method_list(List<MethodInfo> *p_list) const {
  552. script->get_script_method_list(p_list);
  553. }
  554. bool NativeScriptInstance::has_method(const StringName &p_method) const {
  555. return script->has_method(p_method);
  556. }
  557. Variant NativeScriptInstance::call(const StringName &p_method, const Variant **p_args, int p_argcount, Variant::CallError &r_error) {
  558. NativeScriptDesc *script_data = GET_SCRIPT_DESC();
  559. while (script_data) {
  560. Map<StringName, NativeScriptDesc::Method>::Element *E = script_data->methods.find(p_method);
  561. if (E) {
  562. godot_variant result;
  563. #ifdef DEBUG_ENABLED
  564. current_method_call = p_method;
  565. #endif
  566. result = E->get().method.method((godot_object *)owner,
  567. E->get().method.method_data,
  568. userdata,
  569. p_argcount,
  570. (godot_variant **)p_args);
  571. #ifdef DEBUG_ENABLED
  572. current_method_call = "";
  573. #endif
  574. Variant res = *(Variant *)&result;
  575. godot_variant_destroy(&result);
  576. r_error.error = Variant::CallError::CALL_OK;
  577. return res;
  578. }
  579. script_data = script_data->base_data;
  580. }
  581. r_error.error = Variant::CallError::CALL_ERROR_INVALID_METHOD;
  582. return Variant();
  583. }
  584. void NativeScriptInstance::notification(int p_notification) {
  585. #ifdef DEBUG_ENABLED
  586. if (p_notification == MainLoop::NOTIFICATION_CRASH) {
  587. if (current_method_call != StringName("")) {
  588. ERR_PRINT("NativeScriptInstance detected crash on method: " + current_method_call);
  589. current_method_call = "";
  590. }
  591. }
  592. #endif
  593. Variant value = p_notification;
  594. const Variant *args[1] = { &value };
  595. call_multilevel("_notification", args, 1);
  596. }
  597. String NativeScriptInstance::to_string(bool *r_valid) {
  598. if (has_method(CoreStringNames::get_singleton()->_to_string)) {
  599. Variant::CallError ce;
  600. Variant ret = call(CoreStringNames::get_singleton()->_to_string, nullptr, 0, ce);
  601. if (ce.error == Variant::CallError::CALL_OK) {
  602. if (ret.get_type() != Variant::STRING) {
  603. if (r_valid) {
  604. *r_valid = false;
  605. }
  606. ERR_FAIL_V_MSG(String(), "Wrong type for " + CoreStringNames::get_singleton()->_to_string + ", must be a String.");
  607. }
  608. if (r_valid) {
  609. *r_valid = true;
  610. }
  611. return ret.operator String();
  612. }
  613. }
  614. if (r_valid) {
  615. *r_valid = false;
  616. }
  617. return String();
  618. }
  619. void NativeScriptInstance::refcount_incremented() {
  620. Variant::CallError err;
  621. call("_refcount_incremented", nullptr, 0, err);
  622. if (err.error != Variant::CallError::CALL_OK && err.error != Variant::CallError::CALL_ERROR_INVALID_METHOD) {
  623. ERR_PRINT("Failed to invoke _refcount_incremented - should not happen");
  624. }
  625. }
  626. bool NativeScriptInstance::refcount_decremented() {
  627. Variant::CallError err;
  628. Variant ret = call("_refcount_decremented", nullptr, 0, err);
  629. if (err.error != Variant::CallError::CALL_OK && err.error != Variant::CallError::CALL_ERROR_INVALID_METHOD) {
  630. ERR_PRINT("Failed to invoke _refcount_decremented - should not happen");
  631. return true; // assume we can destroy the object
  632. }
  633. if (err.error == Variant::CallError::CALL_ERROR_INVALID_METHOD) {
  634. // the method does not exist, default is true
  635. return true;
  636. }
  637. return ret;
  638. }
  639. Ref<Script> NativeScriptInstance::get_script() const {
  640. return script;
  641. }
  642. MultiplayerAPI::RPCMode NativeScriptInstance::get_rpc_mode(const StringName &p_method) const {
  643. NativeScriptDesc *script_data = GET_SCRIPT_DESC();
  644. while (script_data) {
  645. Map<StringName, NativeScriptDesc::Method>::Element *E = script_data->methods.find(p_method);
  646. if (E) {
  647. switch (E->get().rpc_mode) {
  648. case GODOT_METHOD_RPC_MODE_DISABLED:
  649. return MultiplayerAPI::RPC_MODE_DISABLED;
  650. case GODOT_METHOD_RPC_MODE_REMOTE:
  651. return MultiplayerAPI::RPC_MODE_REMOTE;
  652. case GODOT_METHOD_RPC_MODE_MASTER:
  653. return MultiplayerAPI::RPC_MODE_MASTER;
  654. case GODOT_METHOD_RPC_MODE_PUPPET:
  655. return MultiplayerAPI::RPC_MODE_PUPPET;
  656. case GODOT_METHOD_RPC_MODE_REMOTESYNC:
  657. return MultiplayerAPI::RPC_MODE_REMOTESYNC;
  658. case GODOT_METHOD_RPC_MODE_MASTERSYNC:
  659. return MultiplayerAPI::RPC_MODE_MASTERSYNC;
  660. case GODOT_METHOD_RPC_MODE_PUPPETSYNC:
  661. return MultiplayerAPI::RPC_MODE_PUPPETSYNC;
  662. default:
  663. return MultiplayerAPI::RPC_MODE_DISABLED;
  664. }
  665. }
  666. script_data = script_data->base_data;
  667. }
  668. return MultiplayerAPI::RPC_MODE_DISABLED;
  669. }
  670. MultiplayerAPI::RPCMode NativeScriptInstance::get_rset_mode(const StringName &p_variable) const {
  671. NativeScriptDesc *script_data = GET_SCRIPT_DESC();
  672. while (script_data) {
  673. OrderedHashMap<StringName, NativeScriptDesc::Property>::Element E = script_data->properties.find(p_variable);
  674. if (E) {
  675. switch (E.get().rset_mode) {
  676. case GODOT_METHOD_RPC_MODE_DISABLED:
  677. return MultiplayerAPI::RPC_MODE_DISABLED;
  678. case GODOT_METHOD_RPC_MODE_REMOTE:
  679. return MultiplayerAPI::RPC_MODE_REMOTE;
  680. case GODOT_METHOD_RPC_MODE_MASTER:
  681. return MultiplayerAPI::RPC_MODE_MASTER;
  682. case GODOT_METHOD_RPC_MODE_PUPPET:
  683. return MultiplayerAPI::RPC_MODE_PUPPET;
  684. case GODOT_METHOD_RPC_MODE_REMOTESYNC:
  685. return MultiplayerAPI::RPC_MODE_REMOTESYNC;
  686. case GODOT_METHOD_RPC_MODE_MASTERSYNC:
  687. return MultiplayerAPI::RPC_MODE_MASTERSYNC;
  688. case GODOT_METHOD_RPC_MODE_PUPPETSYNC:
  689. return MultiplayerAPI::RPC_MODE_PUPPETSYNC;
  690. default:
  691. return MultiplayerAPI::RPC_MODE_DISABLED;
  692. }
  693. }
  694. script_data = script_data->base_data;
  695. }
  696. return MultiplayerAPI::RPC_MODE_DISABLED;
  697. }
  698. ScriptLanguage *NativeScriptInstance::get_language() {
  699. return NativeScriptLanguage::get_singleton();
  700. }
  701. void NativeScriptInstance::call_multilevel(const StringName &p_method, const Variant **p_args, int p_argcount) {
  702. NativeScriptDesc *script_data = GET_SCRIPT_DESC();
  703. while (script_data) {
  704. Map<StringName, NativeScriptDesc::Method>::Element *E = script_data->methods.find(p_method);
  705. if (E) {
  706. godot_variant res = E->get().method.method((godot_object *)owner,
  707. E->get().method.method_data,
  708. userdata,
  709. p_argcount,
  710. (godot_variant **)p_args);
  711. godot_variant_destroy(&res);
  712. }
  713. script_data = script_data->base_data;
  714. }
  715. }
  716. void NativeScriptInstance::call_multilevel_reversed(const StringName &p_method, const Variant **p_args, int p_argcount) {
  717. NativeScriptDesc *script_data = GET_SCRIPT_DESC();
  718. if (script_data) {
  719. _ml_call_reversed(script_data, p_method, p_args, p_argcount);
  720. }
  721. }
  722. NativeScriptInstance::~NativeScriptInstance() {
  723. NativeScriptDesc *script_data = GET_SCRIPT_DESC();
  724. if (!script_data) {
  725. return;
  726. }
  727. script_data->destroy_func.destroy_func((godot_object *)owner, script_data->destroy_func.method_data, userdata);
  728. if (owner) {
  729. script->owners_lock.lock();
  730. script->instance_owners.erase(owner);
  731. script->owners_lock.unlock();
  732. }
  733. }
  734. NativeScriptLanguage *NativeScriptLanguage::singleton;
  735. void NativeScriptLanguage::_unload_stuff(bool p_reload) {
  736. Map<String, Ref<GDNative>> erase_and_unload;
  737. for (Map<String, Map<StringName, NativeScriptDesc>>::Element *L = library_classes.front(); L; L = L->next()) {
  738. String lib_path = L->key();
  739. Map<StringName, NativeScriptDesc> classes = L->get();
  740. if (p_reload) {
  741. Map<String, Ref<GDNative>>::Element *E = library_gdnatives.find(lib_path);
  742. Ref<GDNative> gdn;
  743. if (E) {
  744. gdn = E->get();
  745. }
  746. bool should_reload = false;
  747. if (gdn.is_valid()) {
  748. Ref<GDNativeLibrary> lib = gdn->get_library();
  749. if (lib.is_valid()) {
  750. should_reload = lib->is_reloadable();
  751. }
  752. }
  753. if (!should_reload) {
  754. continue;
  755. }
  756. }
  757. Map<String, Ref<GDNative>>::Element *E = library_gdnatives.find(lib_path);
  758. Ref<GDNative> gdn;
  759. if (E) {
  760. gdn = E->get();
  761. }
  762. for (Map<StringName, NativeScriptDesc>::Element *C = classes.front(); C; C = C->next()) {
  763. // free property stuff first
  764. for (OrderedHashMap<StringName, NativeScriptDesc::Property>::Element P = C->get().properties.front(); P; P = P.next()) {
  765. if (P.get().getter.free_func) {
  766. P.get().getter.free_func(P.get().getter.method_data);
  767. }
  768. if (P.get().setter.free_func) {
  769. P.get().setter.free_func(P.get().setter.method_data);
  770. }
  771. }
  772. // free method stuff
  773. for (Map<StringName, NativeScriptDesc::Method>::Element *M = C->get().methods.front(); M; M = M->next()) {
  774. if (M->get().method.free_func) {
  775. M->get().method.free_func(M->get().method.method_data);
  776. }
  777. }
  778. // free constructor/destructor
  779. if (C->get().create_func.free_func) {
  780. C->get().create_func.free_func(C->get().create_func.method_data);
  781. }
  782. if (C->get().destroy_func.free_func) {
  783. C->get().destroy_func.free_func(C->get().destroy_func.method_data);
  784. }
  785. }
  786. erase_and_unload.insert(lib_path, gdn);
  787. }
  788. for (Map<String, Ref<GDNative>>::Element *E = erase_and_unload.front(); E; E = E->next()) {
  789. String lib_path = E->key();
  790. Ref<GDNative> gdn = E->get();
  791. library_classes.erase(lib_path);
  792. if (gdn.is_valid() && gdn->get_library().is_valid()) {
  793. Ref<GDNativeLibrary> lib = gdn->get_library();
  794. void *terminate_fn;
  795. Error err = gdn->get_symbol(lib->get_symbol_prefix() + _terminate_call_name, terminate_fn, true);
  796. if (err == OK) {
  797. void (*terminate)(void *) = (void (*)(void *))terminate_fn;
  798. terminate((void *)&lib_path);
  799. }
  800. }
  801. }
  802. }
  803. NativeScriptLanguage::NativeScriptLanguage() {
  804. NativeScriptLanguage::singleton = this;
  805. _init_call_type = "nativescript_init";
  806. _init_call_name = "nativescript_init";
  807. _terminate_call_name = "nativescript_terminate";
  808. _noarg_call_type = "nativescript_no_arg";
  809. _frame_call_name = "nativescript_frame";
  810. #ifndef NO_THREADS
  811. _thread_enter_call_name = "nativescript_thread_enter";
  812. _thread_exit_call_name = "nativescript_thread_exit";
  813. #endif
  814. }
  815. NativeScriptLanguage::~NativeScriptLanguage() {
  816. for (Map<String, Ref<GDNative>>::Element *L = NSL->library_gdnatives.front(); L; L = L->next()) {
  817. Ref<GDNative> lib = L->get();
  818. // only shut down valid libs, duh!
  819. if (lib.is_valid()) {
  820. // If it's a singleton-library then the gdnative module
  821. // manages the destruction at engine shutdown, not NativeScript.
  822. if (!lib->get_library()->is_singleton()) {
  823. lib->terminate();
  824. }
  825. }
  826. }
  827. NSL->library_classes.clear();
  828. NSL->library_gdnatives.clear();
  829. NSL->library_script_users.clear();
  830. }
  831. String NativeScriptLanguage::get_name() const {
  832. return "NativeScript";
  833. }
  834. void _add_reload_node() {
  835. #ifdef TOOLS_ENABLED
  836. NativeReloadNode *rn = memnew(NativeReloadNode);
  837. EditorNode::get_singleton()->add_child(rn);
  838. #endif
  839. }
  840. void NativeScriptLanguage::init() {
  841. #if defined(TOOLS_ENABLED) && defined(DEBUG_METHODS_ENABLED)
  842. List<String> args = OS::get_singleton()->get_cmdline_args();
  843. List<String>::Element *E = args.find("--gdnative-generate-json-api");
  844. if (E && E->next()) {
  845. if (generate_c_api(E->next()->get()) != OK) {
  846. ERR_PRINT("Failed to generate C API\n");
  847. }
  848. Main::cleanup(true);
  849. exit(0);
  850. }
  851. #endif
  852. #ifdef TOOLS_ENABLED
  853. EditorNode::add_init_callback(&_add_reload_node);
  854. #endif
  855. }
  856. String NativeScriptLanguage::get_type() const {
  857. return "NativeScript";
  858. }
  859. String NativeScriptLanguage::get_extension() const {
  860. return "gdns";
  861. }
  862. Error NativeScriptLanguage::execute_file(const String &p_path) {
  863. return OK; // Qué?
  864. }
  865. void NativeScriptLanguage::finish() {
  866. _unload_stuff();
  867. }
  868. void NativeScriptLanguage::get_reserved_words(List<String> *p_words) const {
  869. }
  870. bool NativeScriptLanguage::is_control_flow_keyword(String p_keyword) const {
  871. return false;
  872. }
  873. void NativeScriptLanguage::get_comment_delimiters(List<String> *p_delimiters) const {
  874. }
  875. void NativeScriptLanguage::get_string_delimiters(List<String> *p_delimiters) const {
  876. }
  877. Ref<Script> NativeScriptLanguage::get_template(const String &p_class_name, const String &p_base_class_name) const {
  878. NativeScript *s = memnew(NativeScript);
  879. s->set_class_name(p_class_name);
  880. return Ref<NativeScript>(s);
  881. }
  882. 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, List<ScriptLanguage::Warning> *r_warnings, Set<int> *r_safe_lines) const {
  883. return true;
  884. }
  885. Script *NativeScriptLanguage::create_script() const {
  886. NativeScript *script = memnew(NativeScript);
  887. return script;
  888. }
  889. bool NativeScriptLanguage::has_named_classes() const {
  890. return true;
  891. }
  892. bool NativeScriptLanguage::supports_builtin_mode() const {
  893. return true;
  894. }
  895. int NativeScriptLanguage::find_function(const String &p_function, const String &p_code) const {
  896. return -1;
  897. }
  898. String NativeScriptLanguage::make_function(const String &p_class, const String &p_name, const PoolStringArray &p_args) const {
  899. return "";
  900. }
  901. void NativeScriptLanguage::auto_indent_code(String &p_code, int p_from_line, int p_to_line) const {
  902. }
  903. void NativeScriptLanguage::add_global_constant(const StringName &p_variable, const Variant &p_value) {
  904. }
  905. // Debugging stuff here. Not used for now.
  906. String NativeScriptLanguage::debug_get_error() const {
  907. return "";
  908. }
  909. int NativeScriptLanguage::debug_get_stack_level_count() const {
  910. return -1;
  911. }
  912. int NativeScriptLanguage::debug_get_stack_level_line(int p_level) const {
  913. return -1;
  914. }
  915. String NativeScriptLanguage::debug_get_stack_level_function(int p_level) const {
  916. return "";
  917. }
  918. String NativeScriptLanguage::debug_get_stack_level_source(int p_level) const {
  919. return "";
  920. }
  921. 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) {
  922. }
  923. 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) {
  924. }
  925. void NativeScriptLanguage::debug_get_globals(List<String> *p_locals, List<Variant> *p_values, int p_max_subitems, int p_max_depth) {
  926. }
  927. String NativeScriptLanguage::debug_parse_stack_level_expression(int p_level, const String &p_expression, int p_max_subitems, int p_max_depth) {
  928. return "";
  929. }
  930. // Debugging stuff end.
  931. void NativeScriptLanguage::reload_all_scripts() {
  932. }
  933. void NativeScriptLanguage::reload_tool_script(const Ref<Script> &p_script, bool p_soft_reload) {
  934. }
  935. void NativeScriptLanguage::get_recognized_extensions(List<String> *p_extensions) const {
  936. p_extensions->push_back("gdns");
  937. }
  938. void NativeScriptLanguage::get_public_functions(List<MethodInfo> *p_functions) const {
  939. }
  940. void NativeScriptLanguage::get_public_constants(List<Pair<String, Variant>> *p_constants) const {
  941. }
  942. void NativeScriptLanguage::profiling_start() {
  943. #ifdef DEBUG_ENABLED
  944. #ifndef NO_THREADS
  945. MutexLock lock(mutex);
  946. #endif
  947. profile_data.clear();
  948. #endif
  949. }
  950. void NativeScriptLanguage::profiling_stop() {
  951. #ifdef DEBUG_ENABLED
  952. #ifndef NO_THREADS
  953. MutexLock lock(mutex);
  954. #endif
  955. #endif
  956. }
  957. int NativeScriptLanguage::profiling_get_accumulated_data(ProfilingInfo *p_info_arr, int p_info_max) {
  958. #ifdef DEBUG_ENABLED
  959. #ifndef NO_THREADS
  960. MutexLock lock(mutex);
  961. #endif
  962. int current = 0;
  963. for (Map<StringName, ProfileData>::Element *d = profile_data.front(); d; d = d->next()) {
  964. if (current >= p_info_max) {
  965. break;
  966. }
  967. p_info_arr[current].call_count = d->get().call_count;
  968. p_info_arr[current].self_time = d->get().self_time;
  969. p_info_arr[current].total_time = d->get().total_time;
  970. p_info_arr[current].signature = d->get().signature;
  971. current++;
  972. }
  973. return current;
  974. #else
  975. return 0;
  976. #endif
  977. }
  978. int NativeScriptLanguage::profiling_get_frame_data(ProfilingInfo *p_info_arr, int p_info_max) {
  979. #ifdef DEBUG_ENABLED
  980. #ifndef NO_THREADS
  981. MutexLock lock(mutex);
  982. #endif
  983. int current = 0;
  984. for (Map<StringName, ProfileData>::Element *d = profile_data.front(); d; d = d->next()) {
  985. if (current >= p_info_max) {
  986. break;
  987. }
  988. if (d->get().last_frame_call_count) {
  989. p_info_arr[current].call_count = d->get().last_frame_call_count;
  990. p_info_arr[current].self_time = d->get().last_frame_self_time;
  991. p_info_arr[current].total_time = d->get().last_frame_total_time;
  992. p_info_arr[current].signature = d->get().signature;
  993. current++;
  994. }
  995. }
  996. return current;
  997. #else
  998. return 0;
  999. #endif
  1000. }
  1001. void NativeScriptLanguage::profiling_add_data(StringName p_signature, uint64_t p_time) {
  1002. #ifdef DEBUG_ENABLED
  1003. #ifndef NO_THREADS
  1004. MutexLock lock(mutex);
  1005. #endif
  1006. Map<StringName, ProfileData>::Element *d = profile_data.find(p_signature);
  1007. if (d) {
  1008. d->get().call_count += 1;
  1009. d->get().total_time += p_time;
  1010. d->get().frame_call_count += 1;
  1011. d->get().frame_total_time += p_time;
  1012. } else {
  1013. ProfileData data;
  1014. data.signature = p_signature;
  1015. data.call_count = 1;
  1016. data.self_time = 0;
  1017. data.total_time = p_time;
  1018. data.frame_call_count = 1;
  1019. data.frame_self_time = 0;
  1020. data.frame_total_time = p_time;
  1021. data.last_frame_call_count = 0;
  1022. data.last_frame_self_time = 0;
  1023. data.last_frame_total_time = 0;
  1024. profile_data.insert(p_signature, data);
  1025. }
  1026. #endif
  1027. }
  1028. int NativeScriptLanguage::register_binding_functions(godot_instance_binding_functions p_binding_functions) {
  1029. // find index
  1030. int idx = -1;
  1031. for (int i = 0; i < binding_functions.size(); i++) {
  1032. if (!binding_functions[i].first) {
  1033. // free, we'll take it
  1034. idx = i;
  1035. break;
  1036. }
  1037. }
  1038. if (idx == -1) {
  1039. idx = binding_functions.size();
  1040. binding_functions.resize(idx + 1);
  1041. }
  1042. // set the functions
  1043. binding_functions.write[idx].first = true;
  1044. binding_functions.write[idx].second = p_binding_functions;
  1045. return idx;
  1046. }
  1047. void NativeScriptLanguage::unregister_binding_functions(int p_idx) {
  1048. ERR_FAIL_INDEX(p_idx, binding_functions.size());
  1049. for (Set<Vector<void *> *>::Element *E = binding_instances.front(); E; E = E->next()) {
  1050. Vector<void *> &binding_data = *E->get();
  1051. if (p_idx < binding_data.size() && binding_data[p_idx] && binding_functions[p_idx].second.free_instance_binding_data) {
  1052. binding_functions[p_idx].second.free_instance_binding_data(binding_functions[p_idx].second.data, binding_data[p_idx]);
  1053. }
  1054. }
  1055. binding_functions.write[p_idx].first = false;
  1056. if (binding_functions[p_idx].second.free_func) {
  1057. binding_functions[p_idx].second.free_func(binding_functions[p_idx].second.data);
  1058. }
  1059. }
  1060. void *NativeScriptLanguage::get_instance_binding_data(int p_idx, Object *p_object) {
  1061. ERR_FAIL_INDEX_V(p_idx, binding_functions.size(), nullptr);
  1062. ERR_FAIL_COND_V_MSG(!binding_functions[p_idx].first, nullptr, "Tried to get binding data for a nativescript binding that does not exist.");
  1063. Vector<void *> *binding_data = (Vector<void *> *)p_object->get_script_instance_binding(lang_idx);
  1064. if (!binding_data) {
  1065. return nullptr; // should never happen.
  1066. }
  1067. if (binding_data->size() <= p_idx) {
  1068. // okay, add new elements here.
  1069. int old_size = binding_data->size();
  1070. binding_data->resize(p_idx + 1);
  1071. for (int i = old_size; i <= p_idx; i++) {
  1072. (*binding_data).write[i] = NULL;
  1073. }
  1074. }
  1075. if (!(*binding_data)[p_idx]) {
  1076. const void *global_type_tag = get_global_type_tag(p_idx, p_object->get_class_name());
  1077. // no binding data yet, soooooo alloc new one \o/
  1078. (*binding_data).write[p_idx] = binding_functions[p_idx].second.alloc_instance_binding_data(binding_functions[p_idx].second.data, global_type_tag, (godot_object *)p_object);
  1079. }
  1080. return (*binding_data)[p_idx];
  1081. }
  1082. void *NativeScriptLanguage::alloc_instance_binding_data(Object *p_object) {
  1083. Vector<void *> *binding_data = new Vector<void *>;
  1084. binding_data->resize(binding_functions.size());
  1085. for (int i = 0; i < binding_functions.size(); i++) {
  1086. (*binding_data).write[i] = NULL;
  1087. }
  1088. binding_instances.insert(binding_data);
  1089. return (void *)binding_data;
  1090. }
  1091. void NativeScriptLanguage::free_instance_binding_data(void *p_data) {
  1092. if (!p_data) {
  1093. return;
  1094. }
  1095. Vector<void *> &binding_data = *(Vector<void *> *)p_data;
  1096. for (int i = 0; i < binding_data.size(); i++) {
  1097. if (!binding_data[i]) {
  1098. continue;
  1099. }
  1100. if (binding_functions[i].first && binding_functions[i].second.free_instance_binding_data) {
  1101. binding_functions[i].second.free_instance_binding_data(binding_functions[i].second.data, binding_data[i]);
  1102. }
  1103. }
  1104. binding_instances.erase(&binding_data);
  1105. delete &binding_data;
  1106. }
  1107. void NativeScriptLanguage::refcount_incremented_instance_binding(Object *p_object) {
  1108. void *data = p_object->get_script_instance_binding(lang_idx);
  1109. if (!data) {
  1110. return;
  1111. }
  1112. Vector<void *> &binding_data = *(Vector<void *> *)data;
  1113. for (int i = 0; i < binding_data.size(); i++) {
  1114. if (!binding_data[i]) {
  1115. continue;
  1116. }
  1117. if (!binding_functions[i].first) {
  1118. continue;
  1119. }
  1120. if (binding_functions[i].second.refcount_incremented_instance_binding) {
  1121. binding_functions[i].second.refcount_incremented_instance_binding(binding_data[i], p_object);
  1122. }
  1123. }
  1124. }
  1125. bool NativeScriptLanguage::refcount_decremented_instance_binding(Object *p_object) {
  1126. void *data = p_object->get_script_instance_binding(lang_idx);
  1127. if (!data) {
  1128. return true;
  1129. }
  1130. Vector<void *> &binding_data = *(Vector<void *> *)data;
  1131. bool can_die = true;
  1132. for (int i = 0; i < binding_data.size(); i++) {
  1133. if (!binding_data[i]) {
  1134. continue;
  1135. }
  1136. if (!binding_functions[i].first) {
  1137. continue;
  1138. }
  1139. if (binding_functions[i].second.refcount_decremented_instance_binding) {
  1140. can_die = can_die && binding_functions[i].second.refcount_decremented_instance_binding(binding_data[i], p_object);
  1141. }
  1142. }
  1143. return can_die;
  1144. }
  1145. void NativeScriptLanguage::set_global_type_tag(int p_idx, StringName p_class_name, const void *p_type_tag) {
  1146. if (!global_type_tags.has(p_idx)) {
  1147. global_type_tags.insert(p_idx, HashMap<StringName, const void *>());
  1148. }
  1149. HashMap<StringName, const void *> &tags = global_type_tags[p_idx];
  1150. tags.set(p_class_name, p_type_tag);
  1151. }
  1152. const void *NativeScriptLanguage::get_global_type_tag(int p_idx, StringName p_class_name) const {
  1153. if (!global_type_tags.has(p_idx)) {
  1154. return nullptr;
  1155. }
  1156. const HashMap<StringName, const void *> &tags = global_type_tags[p_idx];
  1157. if (!tags.has(p_class_name)) {
  1158. return nullptr;
  1159. }
  1160. const void *tag = tags.get(p_class_name);
  1161. return tag;
  1162. }
  1163. #ifndef NO_THREADS
  1164. void NativeScriptLanguage::defer_init_library(Ref<GDNativeLibrary> lib, NativeScript *script) {
  1165. MutexLock lock(mutex);
  1166. libs_to_init.insert(lib);
  1167. scripts_to_register.insert(script);
  1168. has_objects_to_register.set();
  1169. }
  1170. #endif
  1171. void NativeScriptLanguage::init_library(const Ref<GDNativeLibrary> &lib) {
  1172. #ifndef NO_THREADS
  1173. MutexLock lock(mutex);
  1174. #endif
  1175. // See if this library was "registered" already.
  1176. const String &lib_path = lib->get_current_library_path();
  1177. ERR_FAIL_COND_MSG(lib_path.length() == 0, lib->get_name() + " does not have a library for the current platform.");
  1178. Map<String, Ref<GDNative>>::Element *E = library_gdnatives.find(lib_path);
  1179. if (!E) {
  1180. Ref<GDNative> gdn;
  1181. gdn.instance();
  1182. gdn->set_library(lib);
  1183. // TODO check the return value?
  1184. gdn->initialize();
  1185. library_gdnatives.insert(lib_path, gdn);
  1186. library_classes.insert(lib_path, Map<StringName, NativeScriptDesc>());
  1187. if (!library_script_users.has(lib_path)) {
  1188. library_script_users.insert(lib_path, Set<NativeScript *>());
  1189. }
  1190. void *proc_ptr;
  1191. Error err = gdn->get_symbol(lib->get_symbol_prefix() + _init_call_name, proc_ptr);
  1192. if (err != OK) {
  1193. ERR_PRINT(String("No " + _init_call_name + " in \"" + lib_path + "\" found").utf8().get_data());
  1194. } else {
  1195. ((void (*)(godot_string *))proc_ptr)((godot_string *)&lib_path);
  1196. }
  1197. } else {
  1198. // already initialized. Nice.
  1199. }
  1200. }
  1201. void NativeScriptLanguage::register_script(NativeScript *script) {
  1202. #ifndef NO_THREADS
  1203. MutexLock lock(mutex);
  1204. #endif
  1205. library_script_users[script->lib_path].insert(script);
  1206. }
  1207. void NativeScriptLanguage::unregister_script(NativeScript *script) {
  1208. #ifndef NO_THREADS
  1209. MutexLock lock(mutex);
  1210. #endif
  1211. Map<String, Set<NativeScript *>>::Element *S = library_script_users.find(script->lib_path);
  1212. if (S) {
  1213. S->get().erase(script);
  1214. if (S->get().size() == 0) {
  1215. library_script_users.erase(S);
  1216. Map<String, Ref<GDNative>>::Element *G = library_gdnatives.find(script->lib_path);
  1217. if (G && G->get()->get_library()->is_reloadable()) {
  1218. // ONLY if the library is marked as reloadable, and no more instances of its scripts exist do we unload the library
  1219. // First remove meta data related to the library
  1220. Map<String, Map<StringName, NativeScriptDesc>>::Element *L = library_classes.find(script->lib_path);
  1221. if (L) {
  1222. Map<StringName, NativeScriptDesc> classes = L->get();
  1223. for (Map<StringName, NativeScriptDesc>::Element *C = classes.front(); C; C = C->next()) {
  1224. // free property stuff first
  1225. for (OrderedHashMap<StringName, NativeScriptDesc::Property>::Element P = C->get().properties.front(); P; P = P.next()) {
  1226. if (P.get().getter.free_func) {
  1227. P.get().getter.free_func(P.get().getter.method_data);
  1228. }
  1229. if (P.get().setter.free_func) {
  1230. P.get().setter.free_func(P.get().setter.method_data);
  1231. }
  1232. }
  1233. // free method stuff
  1234. for (Map<StringName, NativeScriptDesc::Method>::Element *M = C->get().methods.front(); M; M = M->next()) {
  1235. if (M->get().method.free_func) {
  1236. M->get().method.free_func(M->get().method.method_data);
  1237. }
  1238. }
  1239. // free constructor/destructor
  1240. if (C->get().create_func.free_func) {
  1241. C->get().create_func.free_func(C->get().create_func.method_data);
  1242. }
  1243. if (C->get().destroy_func.free_func) {
  1244. C->get().destroy_func.free_func(C->get().destroy_func.method_data);
  1245. }
  1246. }
  1247. library_classes.erase(script->lib_path);
  1248. }
  1249. // now unload the library
  1250. G->get()->terminate();
  1251. library_gdnatives.erase(G);
  1252. }
  1253. }
  1254. }
  1255. #ifndef NO_THREADS
  1256. scripts_to_register.erase(script);
  1257. #endif
  1258. }
  1259. void NativeScriptLanguage::call_libraries_cb(const StringName &name) {
  1260. // library_gdnatives is modified only from the main thread, so it's safe not to use mutex here
  1261. for (Map<String, Ref<GDNative>>::Element *L = library_gdnatives.front(); L; L = L->next()) {
  1262. if (L->get().is_null()) {
  1263. continue;
  1264. }
  1265. if (L->get()->is_initialized()) {
  1266. void *proc_ptr;
  1267. Error err = L->get()->get_symbol(L->get()->get_library()->get_symbol_prefix() + name, proc_ptr);
  1268. if (!err) {
  1269. ((void (*)())proc_ptr)();
  1270. }
  1271. }
  1272. }
  1273. }
  1274. void NativeScriptLanguage::frame() {
  1275. #ifndef NO_THREADS
  1276. if (has_objects_to_register.is_set()) {
  1277. MutexLock lock(mutex);
  1278. for (Set<Ref<GDNativeLibrary>>::Element *L = libs_to_init.front(); L; L = L->next()) {
  1279. init_library(L->get());
  1280. }
  1281. libs_to_init.clear();
  1282. for (Set<NativeScript *>::Element *S = scripts_to_register.front(); S; S = S->next()) {
  1283. register_script(S->get());
  1284. }
  1285. scripts_to_register.clear();
  1286. has_objects_to_register.clear();
  1287. }
  1288. #endif
  1289. #ifdef DEBUG_ENABLED
  1290. {
  1291. #ifndef NO_THREADS
  1292. MutexLock lock(mutex);
  1293. #endif
  1294. for (Map<StringName, ProfileData>::Element *d = profile_data.front(); d; d = d->next()) {
  1295. d->get().last_frame_call_count = d->get().frame_call_count;
  1296. d->get().last_frame_self_time = d->get().frame_self_time;
  1297. d->get().last_frame_total_time = d->get().frame_total_time;
  1298. d->get().frame_call_count = 0;
  1299. d->get().frame_self_time = 0;
  1300. d->get().frame_total_time = 0;
  1301. }
  1302. }
  1303. #endif
  1304. call_libraries_cb(_frame_call_name);
  1305. }
  1306. #ifndef NO_THREADS
  1307. void NativeScriptLanguage::thread_enter() {
  1308. call_libraries_cb(_thread_enter_call_name);
  1309. }
  1310. void NativeScriptLanguage::thread_exit() {
  1311. call_libraries_cb(_thread_exit_call_name);
  1312. }
  1313. #endif // NO_THREADS
  1314. bool NativeScriptLanguage::handles_global_class_type(const String &p_type) const {
  1315. return p_type == "NativeScript";
  1316. }
  1317. String NativeScriptLanguage::get_global_class_name(const String &p_path, String *r_base_type, String *r_icon_path) const {
  1318. if (!p_path.empty()) {
  1319. Ref<NativeScript> script = ResourceLoader::load(p_path, "NativeScript");
  1320. if (script.is_valid()) {
  1321. if (r_base_type) {
  1322. *r_base_type = script->get_instance_base_type();
  1323. }
  1324. if (r_icon_path) {
  1325. *r_icon_path = script->get_script_class_icon_path();
  1326. }
  1327. return script->get_script_class_name();
  1328. }
  1329. if (r_base_type) {
  1330. *r_base_type = String();
  1331. }
  1332. if (r_icon_path) {
  1333. *r_icon_path = String();
  1334. }
  1335. }
  1336. return String();
  1337. }
  1338. void NativeReloadNode::_bind_methods() {
  1339. ClassDB::bind_method(D_METHOD("_notification"), &NativeReloadNode::_notification);
  1340. }
  1341. void NativeReloadNode::_notification(int p_what) {
  1342. #ifdef TOOLS_ENABLED
  1343. switch (p_what) {
  1344. case MainLoop::NOTIFICATION_WM_FOCUS_OUT: {
  1345. if (unloaded) {
  1346. break;
  1347. }
  1348. #ifndef NO_THREADS
  1349. MutexLock lock(NSL->mutex);
  1350. #endif
  1351. NSL->_unload_stuff(true);
  1352. for (Map<String, Ref<GDNative>>::Element *L = NSL->library_gdnatives.front(); L; L = L->next()) {
  1353. Ref<GDNative> gdn = L->get();
  1354. if (gdn.is_null()) {
  1355. continue;
  1356. }
  1357. // Don't unload what should not be reloaded!
  1358. if (!gdn->get_library()->is_reloadable()) {
  1359. continue;
  1360. }
  1361. // singleton libraries might have alive pointers living inside the
  1362. // editor. Also reloading a singleton library would mean that
  1363. // the singleton entry will not be called again, as this only
  1364. // happens at engine startup.
  1365. if (gdn->get_library()->is_singleton()) {
  1366. continue;
  1367. }
  1368. gdn->terminate();
  1369. }
  1370. unloaded = true;
  1371. } break;
  1372. case MainLoop::NOTIFICATION_WM_FOCUS_IN: {
  1373. if (!unloaded) {
  1374. break;
  1375. }
  1376. #ifndef NO_THREADS
  1377. MutexLock lock(NSL->mutex);
  1378. #endif
  1379. Set<StringName> libs_to_remove;
  1380. for (Map<String, Ref<GDNative>>::Element *L = NSL->library_gdnatives.front(); L; L = L->next()) {
  1381. Ref<GDNative> gdn = L->get();
  1382. if (gdn.is_null()) {
  1383. continue;
  1384. }
  1385. if (!gdn->get_library()->is_reloadable()) {
  1386. continue;
  1387. }
  1388. // since singleton libraries are not unloaded there is no point
  1389. // in loading them again.
  1390. if (gdn->get_library()->is_singleton()) {
  1391. continue;
  1392. }
  1393. if (!gdn->initialize()) {
  1394. libs_to_remove.insert(L->key());
  1395. continue;
  1396. }
  1397. NSL->library_classes.insert(L->key(), Map<StringName, NativeScriptDesc>());
  1398. // here the library registers all the classes and stuff.
  1399. void *proc_ptr;
  1400. Error err = gdn->get_symbol(gdn->get_library()->get_symbol_prefix() + "nativescript_init", proc_ptr);
  1401. if (err != OK) {
  1402. ERR_PRINT(String("No godot_nativescript_init in \"" + L->key() + "\" found").utf8().get_data());
  1403. } else {
  1404. ((void (*)(void *))proc_ptr)((void *)&L->key());
  1405. }
  1406. for (Map<String, Set<NativeScript *>>::Element *U = NSL->library_script_users.front(); U; U = U->next()) {
  1407. // Multiple GDNative libraries may be reloaded. The library and script
  1408. // path should match to prevent failing `NSL->library_classes` lookup
  1409. // from `get_script_desc()` in `script->_update_placeholder` below.
  1410. // This check also prevents "!script_data is true" error from occurring
  1411. // when e. g. re-focusing editor window.
  1412. if (L->key() != U->key()) {
  1413. continue;
  1414. }
  1415. for (Set<NativeScript *>::Element *S = U->get().front(); S; S = S->next()) {
  1416. NativeScript *script = S->get();
  1417. if (script->placeholders.size() == 0) {
  1418. continue;
  1419. }
  1420. for (Set<PlaceHolderScriptInstance *>::Element *P = script->placeholders.front(); P; P = P->next()) {
  1421. script->_update_placeholder(P->get());
  1422. }
  1423. }
  1424. }
  1425. }
  1426. unloaded = false;
  1427. for (Set<StringName>::Element *R = libs_to_remove.front(); R; R = R->next()) {
  1428. NSL->library_gdnatives.erase(R->get());
  1429. }
  1430. } break;
  1431. default: {
  1432. };
  1433. }
  1434. #endif
  1435. }
  1436. RES ResourceFormatLoaderNativeScript::load(const String &p_path, const String &p_original_path, Error *r_error, bool p_no_subresource_cache) {
  1437. return ResourceFormatLoaderText::singleton->load(p_path, p_original_path, r_error, p_no_subresource_cache);
  1438. }
  1439. void ResourceFormatLoaderNativeScript::get_recognized_extensions(List<String> *p_extensions) const {
  1440. p_extensions->push_back("gdns");
  1441. }
  1442. bool ResourceFormatLoaderNativeScript::handles_type(const String &p_type) const {
  1443. return (p_type == "Script" || p_type == "NativeScript");
  1444. }
  1445. String ResourceFormatLoaderNativeScript::get_resource_type(const String &p_path) const {
  1446. String el = p_path.get_extension().to_lower();
  1447. if (el == "gdns") {
  1448. return "NativeScript";
  1449. }
  1450. return "";
  1451. }
  1452. Error ResourceFormatSaverNativeScript::save(const String &p_path, const RES &p_resource, uint32_t p_flags) {
  1453. ResourceFormatSaverText rfst;
  1454. return rfst.save(p_path, p_resource, p_flags);
  1455. }
  1456. bool ResourceFormatSaverNativeScript::recognize(const RES &p_resource) const {
  1457. return Object::cast_to<NativeScript>(*p_resource) != nullptr;
  1458. }
  1459. void ResourceFormatSaverNativeScript::get_recognized_extensions(const RES &p_resource, List<String> *p_extensions) const {
  1460. if (Object::cast_to<NativeScript>(*p_resource)) {
  1461. p_extensions->push_back("gdns");
  1462. }
  1463. }