2
0

nativescript.cpp 50 KB

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