2
0

nativescript.cpp 50 KB

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